blob: c769f12e7abd33621f2ab8330af72304beaadaab [file] [log] [blame]
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001/*
2 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
3 * Copyright 2002 Markus Friedl <markus@openbsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000028RCSID("$OpenBSD: monitor.c,v 1.15 2002/06/19 18:01:00 markus Exp $");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000029
30#include <openssl/dh.h>
31
32#ifdef SKEY
33#include <skey.h>
34#endif
35
36#include "ssh.h"
37#include "auth.h"
38#include "kex.h"
39#include "dh.h"
40#include "zlib.h"
41#include "packet.h"
42#include "auth-options.h"
43#include "sshpty.h"
44#include "channels.h"
45#include "session.h"
46#include "sshlogin.h"
47#include "canohost.h"
48#include "log.h"
49#include "servconf.h"
50#include "monitor.h"
51#include "monitor_mm.h"
52#include "monitor_wrap.h"
53#include "monitor_fdpass.h"
54#include "xmalloc.h"
55#include "misc.h"
56#include "buffer.h"
57#include "bufaux.h"
58#include "compat.h"
59#include "ssh2.h"
60#include "mpaux.h"
61
62/* Imports */
63extern ServerOptions options;
64extern u_int utmp_len;
65extern Newkeys *current_keys[];
66extern z_stream incoming_stream;
67extern z_stream outgoing_stream;
68extern u_char session_id[];
69extern Buffer input, output;
70extern Buffer auth_debug;
71extern int auth_debug_init;
72
73/* State exported from the child */
74
75struct {
76 z_stream incoming;
77 z_stream outgoing;
78 u_char *keyin;
79 u_int keyinlen;
80 u_char *keyout;
81 u_int keyoutlen;
82 u_char *ivin;
83 u_int ivinlen;
84 u_char *ivout;
85 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000086 u_char *ssh1key;
87 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000088 int ssh1cipher;
89 int ssh1protoflags;
90 u_char *input;
91 u_int ilen;
92 u_char *output;
93 u_int olen;
94} child_state;
95
96/* Functions on the montior that answer unprivileged requests */
97
98int mm_answer_moduli(int, Buffer *);
99int mm_answer_sign(int, Buffer *);
100int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000101int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000102int mm_answer_authserv(int, Buffer *);
103int mm_answer_authpassword(int, Buffer *);
104int mm_answer_bsdauthquery(int, Buffer *);
105int mm_answer_bsdauthrespond(int, Buffer *);
106int mm_answer_skeyquery(int, Buffer *);
107int mm_answer_skeyrespond(int, Buffer *);
108int mm_answer_keyallowed(int, Buffer *);
109int mm_answer_keyverify(int, Buffer *);
110int mm_answer_pty(int, Buffer *);
111int mm_answer_pty_cleanup(int, Buffer *);
112int mm_answer_term(int, Buffer *);
113int mm_answer_rsa_keyallowed(int, Buffer *);
114int mm_answer_rsa_challenge(int, Buffer *);
115int mm_answer_rsa_response(int, Buffer *);
116int mm_answer_sesskey(int, Buffer *);
117int mm_answer_sessid(int, Buffer *);
118
Damien Miller79418552002-04-23 20:28:48 +1000119#ifdef USE_PAM
120int mm_answer_pam_start(int, Buffer *);
121#endif
122
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000123static Authctxt *authctxt;
124static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
125
126/* local state for key verify */
127static u_char *key_blob = NULL;
128static u_int key_bloblen = 0;
129static int key_blobtype = MM_NOKEY;
130static u_char *hostbased_cuser = NULL;
131static u_char *hostbased_chost = NULL;
132static char *auth_method = "unknown";
Ben Lindstromf67e0772002-06-06 20:58:19 +0000133static int session_id2_len = 0;
134static u_char *session_id2 = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000135
136struct mon_table {
137 enum monitor_reqtype type;
138 int flags;
139 int (*f)(int, Buffer *);
140};
141
142#define MON_ISAUTH 0x0004 /* Required for Authentication */
143#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
144#define MON_ONCE 0x0010 /* Disable after calling */
145
146#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
147
148#define MON_PERMIT 0x1000 /* Request is permitted */
149
150struct mon_table mon_dispatch_proto20[] = {
151 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
152 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
153 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
154 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000155 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000156 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000157#ifdef USE_PAM
158 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000159#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000160#ifdef BSD_AUTH
161 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
162 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
163#endif
164#ifdef SKEY
165 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
166 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
167#endif
168 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
169 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
170 {0, 0, NULL}
171};
172
173struct mon_table mon_dispatch_postauth20[] = {
174 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
175 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
176 {MONITOR_REQ_PTY, 0, mm_answer_pty},
177 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
178 {MONITOR_REQ_TERM, 0, mm_answer_term},
179 {0, 0, NULL}
180};
181
182struct mon_table mon_dispatch_proto15[] = {
183 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
184 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
185 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
186 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
187 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
188 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
189 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
190 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
Damien Millera33501b2002-05-08 12:24:42 +1000191#ifdef USE_PAM
192 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
193#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000194#ifdef BSD_AUTH
195 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
196 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
197#endif
198#ifdef SKEY
199 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
200 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
201#endif
Damien Millera33501b2002-05-08 12:24:42 +1000202#ifdef USE_PAM
203 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
204#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000205 {0, 0, NULL}
206};
207
208struct mon_table mon_dispatch_postauth15[] = {
209 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
210 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
211 {MONITOR_REQ_TERM, 0, mm_answer_term},
212 {0, 0, NULL}
213};
214
215struct mon_table *mon_dispatch;
216
217/* Specifies if a certain message is allowed at the moment */
218
219static void
220monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
221{
222 while (ent->f != NULL) {
223 if (ent->type == type) {
224 ent->flags &= ~MON_PERMIT;
225 ent->flags |= permit ? MON_PERMIT : 0;
226 return;
227 }
228 ent++;
229 }
230}
231
232static void
233monitor_permit_authentications(int permit)
234{
235 struct mon_table *ent = mon_dispatch;
236
237 while (ent->f != NULL) {
238 if (ent->flags & MON_AUTH) {
239 ent->flags &= ~MON_PERMIT;
240 ent->flags |= permit ? MON_PERMIT : 0;
241 }
242 ent++;
243 }
244}
245
246Authctxt *
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000247monitor_child_preauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000248{
249 struct mon_table *ent;
250 int authenticated = 0;
251
252 debug3("preauth child monitor started");
253
254 if (compat20) {
255 mon_dispatch = mon_dispatch_proto20;
256
257 /* Permit requests for moduli and signatures */
258 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
259 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
260 } else {
261 mon_dispatch = mon_dispatch_proto15;
262
263 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
264 }
265
266 authctxt = authctxt_new();
267
268 /* The first few requests do not require asynchronous access */
269 while (!authenticated) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000270 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000271 if (authenticated) {
272 if (!(ent->flags & MON_AUTHDECIDE))
273 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000274 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000275 if (authctxt->pw->pw_uid == 0 &&
276 !auth_root_allowed(auth_method))
277 authenticated = 0;
Damien Miller79418552002-04-23 20:28:48 +1000278#ifdef USE_PAM
279 if (!do_pam_account(authctxt->pw->pw_name, NULL))
280 authenticated = 0;
281#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000282 }
283
284 if (ent->flags & MON_AUTHDECIDE) {
285 auth_log(authctxt, authenticated, auth_method,
286 compat20 ? " ssh2" : "");
287 if (!authenticated)
288 authctxt->failures++;
289 }
290 }
291
292 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000293 fatal("%s: authenticated invalid user", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000294
295 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000296 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000297
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000298 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000299
300 return (authctxt);
301}
302
303void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000304monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000305{
306 if (compat20) {
307 mon_dispatch = mon_dispatch_postauth20;
308
309 /* Permit requests for moduli and signatures */
310 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
311 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
312 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
313
314 } else {
315 mon_dispatch = mon_dispatch_postauth15;
316 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
317 }
318 if (!no_pty_flag) {
319 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
320 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
321 }
322
323 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000324 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000325}
326
327void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000328monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000329{
330 /* The member allocation is not visible, so sync it */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000331 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000332}
333
334int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000335monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000336 struct mon_table **pent)
337{
338 Buffer m;
339 int ret;
340 u_char type;
341
342 buffer_init(&m);
343
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000344 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000345 type = buffer_get_char(&m);
346
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000347 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000348
349 while (ent->f != NULL) {
350 if (ent->type == type)
351 break;
352 ent++;
353 }
354
355 if (ent->f != NULL) {
356 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000357 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000358 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000359 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000360 buffer_free(&m);
361
362 /* The child may use this request only once, disable it */
363 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000364 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000365 type);
366 ent->flags &= ~MON_PERMIT;
367 }
368
369 if (pent != NULL)
370 *pent = ent;
371
372 return ret;
373 }
374
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000375 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000376
377 /* NOTREACHED */
378 return (-1);
379}
380
381/* allowed key state */
382static int
383monitor_allowed_key(u_char *blob, u_int bloblen)
384{
385 /* make sure key is allowed */
386 if (key_blob == NULL || key_bloblen != bloblen ||
387 memcmp(key_blob, blob, key_bloblen))
388 return (0);
389 return (1);
390}
391
392static void
393monitor_reset_key_state(void)
394{
395 /* reset state */
396 if (key_blob != NULL)
397 xfree(key_blob);
398 if (hostbased_cuser != NULL)
399 xfree(hostbased_cuser);
400 if (hostbased_chost != NULL)
401 xfree(hostbased_chost);
402 key_blob = NULL;
403 key_bloblen = 0;
404 key_blobtype = MM_NOKEY;
405 hostbased_cuser = NULL;
406 hostbased_chost = NULL;
407}
408
409int
410mm_answer_moduli(int socket, Buffer *m)
411{
412 DH *dh;
413 int min, want, max;
414
415 min = buffer_get_int(m);
416 want = buffer_get_int(m);
417 max = buffer_get_int(m);
418
419 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000420 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000421 /* We need to check here, too, in case the child got corrupted */
422 if (max < min || want < min || max < want)
423 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000424 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000425
426 buffer_clear(m);
427
428 dh = choose_dh(min, want, max);
429 if (dh == NULL) {
430 buffer_put_char(m, 0);
431 return (0);
432 } else {
433 /* Send first bignum */
434 buffer_put_char(m, 1);
435 buffer_put_bignum2(m, dh->p);
436 buffer_put_bignum2(m, dh->g);
437
438 DH_free(dh);
439 }
440 mm_request_send(socket, MONITOR_ANS_MODULI, m);
441 return (0);
442}
443
444int
445mm_answer_sign(int socket, Buffer *m)
446{
447 Key *key;
448 u_char *p;
449 u_char *signature;
450 u_int siglen, datlen;
451 int keyid;
452
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000453 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000454
455 keyid = buffer_get_int(m);
456 p = buffer_get_string(m, &datlen);
457
458 if (datlen != 20)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000459 fatal("%s: data length incorrect: %d", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000460
Ben Lindstromf67e0772002-06-06 20:58:19 +0000461 /* save session id, it will be passed on the first call */
462 if (session_id2_len == 0) {
463 session_id2_len = datlen;
464 session_id2 = xmalloc(session_id2_len);
465 memcpy(session_id2, p, session_id2_len);
466 }
467
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000468 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000469 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000470 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000471 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000472
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000473 debug3("%s: signature %p(%d)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000474
475 buffer_clear(m);
476 buffer_put_string(m, signature, siglen);
477
478 xfree(p);
479 xfree(signature);
480
481 mm_request_send(socket, MONITOR_ANS_SIGN, m);
482
483 /* Turn on permissions for getpwnam */
484 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
485
486 return (0);
487}
488
489/* Retrieves the password entry and also checks if the user is permitted */
490
491int
492mm_answer_pwnamallow(int socket, Buffer *m)
493{
494 char *login;
495 struct passwd *pwent;
496 int allowed = 0;
497
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000498 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000499
500 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000501 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000502
503 login = buffer_get_string(m, NULL);
504
505 pwent = getpwnamallow(login);
506
507 authctxt->user = xstrdup(login);
508 setproctitle("%s [priv]", pwent ? login : "unknown");
509 xfree(login);
510
511 buffer_clear(m);
512
513 if (pwent == NULL) {
514 buffer_put_char(m, 0);
515 goto out;
516 }
517
518 allowed = 1;
519 authctxt->pw = pwent;
520 authctxt->valid = 1;
521
522 buffer_put_char(m, 1);
523 buffer_put_string(m, pwent, sizeof(struct passwd));
524 buffer_put_cstring(m, pwent->pw_name);
525 buffer_put_cstring(m, "*");
526 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000527#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000528 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000529#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000530 buffer_put_cstring(m, pwent->pw_dir);
531 buffer_put_cstring(m, pwent->pw_shell);
532
533 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000534 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000535 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
536
537 /* For SSHv1 allow authentication now */
538 if (!compat20)
539 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000540 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000541 /* Allow service/style information on the auth context */
542 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000543 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
544 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000545
Damien Millera33501b2002-05-08 12:24:42 +1000546#ifdef USE_PAM
547 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
548#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000549
550 return (0);
551}
552
Damien Miller5ad9fd92002-05-13 11:07:41 +1000553int mm_answer_auth2_read_banner(int socket, Buffer *m)
554{
555 char *banner;
556
557 buffer_clear(m);
558 banner = auth2_read_banner();
559 buffer_put_cstring(m, banner != NULL ? banner : "");
560 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
561
562 if (banner != NULL)
563 free(banner);
564
565 return (0);
566}
567
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000568int
569mm_answer_authserv(int socket, Buffer *m)
570{
571 monitor_permit_authentications(1);
572
573 authctxt->service = buffer_get_string(m, NULL);
574 authctxt->style = buffer_get_string(m, NULL);
575 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000576 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000577
578 if (strlen(authctxt->style) == 0) {
579 xfree(authctxt->style);
580 authctxt->style = NULL;
581 }
582
583 return (0);
584}
585
586int
587mm_answer_authpassword(int socket, Buffer *m)
588{
589 static int call_count;
590 char *passwd;
591 int authenticated, plen;
592
593 passwd = buffer_get_string(m, &plen);
594 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000595 authenticated = options.password_authentication &&
596 authctxt->valid && auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000597 memset(passwd, 0, strlen(passwd));
598 xfree(passwd);
599
600 buffer_clear(m);
601 buffer_put_int(m, authenticated);
602
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000603 debug3("%s: sending result %d", __func__, authenticated);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000604 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
605
606 call_count++;
607 if (plen == 0 && call_count == 1)
608 auth_method = "none";
609 else
610 auth_method = "password";
611
612 /* Causes monitor loop to terminate if authenticated */
613 return (authenticated);
614}
615
616#ifdef BSD_AUTH
617int
618mm_answer_bsdauthquery(int socket, Buffer *m)
619{
620 char *name, *infotxt;
621 u_int numprompts;
622 u_int *echo_on;
623 char **prompts;
624 int res;
625
626 res = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
627 &prompts, &echo_on);
628
629 buffer_clear(m);
630 buffer_put_int(m, res);
631 if (res != -1)
632 buffer_put_cstring(m, prompts[0]);
633
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000634 debug3("%s: sending challenge res: %d", __func__, res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000635 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
636
637 if (res != -1) {
638 xfree(name);
639 xfree(infotxt);
640 xfree(prompts);
641 xfree(echo_on);
642 }
643
644 return (0);
645}
646
647int
648mm_answer_bsdauthrespond(int socket, Buffer *m)
649{
650 char *response;
651 int authok;
652
653 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000654 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000655
656 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000657 authok = options.challenge_response_authentication &&
658 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000659 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000660 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000661 xfree(response);
662
663 buffer_clear(m);
664 buffer_put_int(m, authok);
665
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000666 debug3("%s: sending authenticated: %d", __func__, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000667 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
668
669 auth_method = "bsdauth";
670
671 return (authok != 0);
672}
673#endif
674
675#ifdef SKEY
676int
677mm_answer_skeyquery(int socket, Buffer *m)
678{
679 struct skey skey;
680 char challenge[1024];
681 int res;
682
683 res = skeychallenge(&skey, authctxt->user, challenge);
684
685 buffer_clear(m);
686 buffer_put_int(m, res);
687 if (res != -1)
688 buffer_put_cstring(m, challenge);
689
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000690 debug3("%s: sending challenge res: %d", __func__, res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000691 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
692
693 return (0);
694}
695
696int
697mm_answer_skeyrespond(int socket, Buffer *m)
698{
699 char *response;
700 int authok;
701
702 response = buffer_get_string(m, NULL);
703
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000704 authok = (options.challenge_response_authentication &&
705 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000706 skey_haskey(authctxt->pw->pw_name) == 0 &&
707 skey_passcheck(authctxt->pw->pw_name, response) != -1);
708
709 xfree(response);
710
711 buffer_clear(m);
712 buffer_put_int(m, authok);
713
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000714 debug3("%s: sending authenticated: %d", __func__, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000715 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
716
717 auth_method = "skey";
718
719 return (authok != 0);
720}
721#endif
722
Damien Miller79418552002-04-23 20:28:48 +1000723#ifdef USE_PAM
724int
725mm_answer_pam_start(int socket, Buffer *m)
726{
727 char *user;
728
729 user = buffer_get_string(m, NULL);
730
731 start_pam(user);
732
733 xfree(user);
734
735 return (0);
736}
737#endif
738
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000739static void
740mm_append_debug(Buffer *m)
741{
742 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000743 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000744 buffer_append(m, buffer_ptr(&auth_debug),
745 buffer_len(&auth_debug));
746 buffer_clear(&auth_debug);
747 }
748}
749
750int
751mm_answer_keyallowed(int socket, Buffer *m)
752{
753 Key *key;
754 u_char *cuser, *chost, *blob;
755 u_int bloblen;
756 enum mm_keytype type = 0;
757 int allowed = 0;
758
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000759 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000760
761 type = buffer_get_int(m);
762 cuser = buffer_get_string(m, NULL);
763 chost = buffer_get_string(m, NULL);
764 blob = buffer_get_string(m, &bloblen);
765
766 key = key_from_blob(blob, bloblen);
767
768 if ((compat20 && type == MM_RSAHOSTKEY) ||
769 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000770 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000771
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000772 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000773
774 if (key != NULL && authctxt->pw != NULL) {
775 switch(type) {
776 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000777 allowed = options.pubkey_authentication &&
778 user_key_allowed(authctxt->pw, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000779 break;
780 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000781 allowed = options.hostbased_authentication &&
782 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000783 cuser, chost, key);
784 break;
785 case MM_RSAHOSTKEY:
786 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000787 allowed = options.rhosts_rsa_authentication &&
788 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000789 cuser, chost, key);
790 break;
791 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000792 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000793 break;
794 }
795 key_free(key);
796 }
797
798 /* clear temporarily storage (used by verify) */
799 monitor_reset_key_state();
800
801 if (allowed) {
802 /* Save temporarily for comparison in verify */
803 key_blob = blob;
804 key_bloblen = bloblen;
805 key_blobtype = type;
806 hostbased_cuser = cuser;
807 hostbased_chost = chost;
808 }
809
810 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000811 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000812
813 buffer_clear(m);
814 buffer_put_int(m, allowed);
815
816 mm_append_debug(m);
817
818 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
819
820 if (type == MM_RSAHOSTKEY)
821 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
822
823 return (0);
824}
825
826static int
827monitor_valid_userblob(u_char *data, u_int datalen)
828{
829 Buffer b;
830 u_char *p;
831 u_int len;
832 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000833
834 buffer_init(&b);
835 buffer_append(&b, data, datalen);
836
837 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +0000838 p = buffer_ptr(&b);
839 len = buffer_len(&b);
840 if ((session_id2 == NULL) ||
841 (len < session_id2_len) ||
842 (memcmp(p, session_id2, session_id2_len) != 0))
843 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000844 buffer_consume(&b, session_id2_len);
845 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +0000846 p = buffer_get_string(&b, &len);
847 if ((session_id2 == NULL) ||
848 (len != session_id2_len) ||
849 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000850 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000851 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000852 }
853 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
854 fail++;
855 p = buffer_get_string(&b, NULL);
856 if (strcmp(authctxt->user, p) != 0) {
857 log("wrong user name passed to monitor: expected %s != %.100s",
858 authctxt->user, p);
859 fail++;
860 }
861 xfree(p);
862 buffer_skip_string(&b);
863 if (datafellows & SSH_BUG_PKAUTH) {
864 if (!buffer_get_char(&b))
865 fail++;
866 } else {
867 p = buffer_get_string(&b, NULL);
868 if (strcmp("publickey", p) != 0)
869 fail++;
870 xfree(p);
871 if (!buffer_get_char(&b))
872 fail++;
873 buffer_skip_string(&b);
874 }
875 buffer_skip_string(&b);
876 if (buffer_len(&b) != 0)
877 fail++;
878 buffer_free(&b);
879 return (fail == 0);
880}
881
882static int
883monitor_valid_hostbasedblob(u_char *data, u_int datalen, u_char *cuser,
884 u_char *chost)
885{
886 Buffer b;
887 u_char *p;
888 u_int len;
889 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000890
891 buffer_init(&b);
892 buffer_append(&b, data, datalen);
893
Ben Lindstromf67e0772002-06-06 20:58:19 +0000894 p = buffer_get_string(&b, &len);
895 if ((session_id2 == NULL) ||
896 (len != session_id2_len) ||
897 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000898 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000899 xfree(p);
900
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000901 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
902 fail++;
903 p = buffer_get_string(&b, NULL);
904 if (strcmp(authctxt->user, p) != 0) {
905 log("wrong user name passed to monitor: expected %s != %.100s",
906 authctxt->user, p);
907 fail++;
908 }
909 xfree(p);
910 buffer_skip_string(&b); /* service */
911 p = buffer_get_string(&b, NULL);
912 if (strcmp(p, "hostbased") != 0)
913 fail++;
914 xfree(p);
915 buffer_skip_string(&b); /* pkalg */
916 buffer_skip_string(&b); /* pkblob */
917
918 /* verify client host, strip trailing dot if necessary */
919 p = buffer_get_string(&b, NULL);
920 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
921 p[len - 1] = '\0';
922 if (strcmp(p, chost) != 0)
923 fail++;
924 xfree(p);
925
926 /* verify client user */
927 p = buffer_get_string(&b, NULL);
928 if (strcmp(p, cuser) != 0)
929 fail++;
930 xfree(p);
931
932 if (buffer_len(&b) != 0)
933 fail++;
934 buffer_free(&b);
935 return (fail == 0);
936}
937
938int
939mm_answer_keyverify(int socket, Buffer *m)
940{
941 Key *key;
942 u_char *signature, *data, *blob;
943 u_int signaturelen, datalen, bloblen;
944 int verified = 0;
945 int valid_data = 0;
946
947 blob = buffer_get_string(m, &bloblen);
948 signature = buffer_get_string(m, &signaturelen);
949 data = buffer_get_string(m, &datalen);
950
951 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +0000952 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000953 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000954
955 key = key_from_blob(blob, bloblen);
956 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000957 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000958
959 switch (key_blobtype) {
960 case MM_USERKEY:
961 valid_data = monitor_valid_userblob(data, datalen);
962 break;
963 case MM_HOSTKEY:
964 valid_data = monitor_valid_hostbasedblob(data, datalen,
965 hostbased_cuser, hostbased_chost);
966 break;
967 default:
968 valid_data = 0;
969 break;
970 }
971 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000972 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000973
974 verified = key_verify(key, signature, signaturelen, data, datalen);
975 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000976 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000977
978 key_free(key);
979 xfree(blob);
980 xfree(signature);
981 xfree(data);
982
983 monitor_reset_key_state();
984
985 buffer_clear(m);
986 buffer_put_int(m, verified);
987 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
988
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000989 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000990
991 return (verified);
992}
993
994static void
995mm_record_login(Session *s, struct passwd *pw)
996{
997 socklen_t fromlen;
998 struct sockaddr_storage from;
999
1000 /*
1001 * Get IP address of client. If the connection is not a socket, let
1002 * the address be 0.0.0.0.
1003 */
1004 memset(&from, 0, sizeof(from));
1005 if (packet_connection_is_on_socket()) {
1006 fromlen = sizeof(from);
1007 if (getpeername(packet_get_connection_in(),
1008 (struct sockaddr *) & from, &fromlen) < 0) {
1009 debug("getpeername: %.100s", strerror(errno));
1010 fatal_cleanup();
1011 }
1012 }
1013 /* Record that there was a login on that tty from the remote host. */
1014 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1015 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
1016 (struct sockaddr *)&from);
1017}
1018
1019static void
1020mm_session_close(Session *s)
1021{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001022 debug3("%s: session %d pid %d", __func__, s->self, s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001023 if (s->ttyfd != -1) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001024 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001025 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1026 session_pty_cleanup2(s);
1027 }
1028 s->used = 0;
1029}
1030
1031int
1032mm_answer_pty(int socket, Buffer *m)
1033{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001034 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001035 Session *s;
1036 int res, fd0;
1037
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001038 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001039
1040 buffer_clear(m);
1041 s = session_new();
1042 if (s == NULL)
1043 goto error;
1044 s->authctxt = authctxt;
1045 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001046 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001047 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1048 if (res == 0)
1049 goto error;
1050 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1051 pty_setowner(authctxt->pw, s->tty);
1052
1053 buffer_put_int(m, 1);
1054 buffer_put_cstring(m, s->tty);
1055 mm_request_send(socket, MONITOR_ANS_PTY, m);
1056
1057 mm_send_fd(socket, s->ptyfd);
1058 mm_send_fd(socket, s->ttyfd);
1059
1060 /* We need to trick ttyslot */
1061 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001062 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001063
1064 mm_record_login(s, authctxt->pw);
1065
1066 /* Now we can close the file descriptor again */
1067 close(0);
1068
1069 /* make sure nothing uses fd 0 */
1070 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001071 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001072 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001073 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001074
1075 /* slave is not needed */
1076 close(s->ttyfd);
1077 s->ttyfd = s->ptyfd;
1078 /* no need to dup() because nobody closes ptyfd */
1079 s->ptymaster = s->ptyfd;
1080
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001081 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001082
1083 return (0);
1084
1085 error:
1086 if (s != NULL)
1087 mm_session_close(s);
1088 buffer_put_int(m, 0);
1089 mm_request_send(socket, MONITOR_ANS_PTY, m);
1090 return (0);
1091}
1092
1093int
1094mm_answer_pty_cleanup(int socket, Buffer *m)
1095{
1096 Session *s;
1097 char *tty;
1098
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001099 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001100
1101 tty = buffer_get_string(m, NULL);
1102 if ((s = session_by_tty(tty)) != NULL)
1103 mm_session_close(s);
1104 buffer_clear(m);
1105 xfree(tty);
1106 return (0);
1107}
1108
1109int
1110mm_answer_sesskey(int socket, Buffer *m)
1111{
1112 BIGNUM *p;
1113 int rsafail;
1114
1115 /* Turn off permissions */
1116 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1117
1118 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001119 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001120
1121 buffer_get_bignum2(m, p);
1122
1123 rsafail = ssh1_session_key(p);
1124
1125 buffer_clear(m);
1126 buffer_put_int(m, rsafail);
1127 buffer_put_bignum2(m, p);
1128
1129 BN_clear_free(p);
1130
1131 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1132
1133 /* Turn on permissions for sessid passing */
1134 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1135
1136 return (0);
1137}
1138
1139int
1140mm_answer_sessid(int socket, Buffer *m)
1141{
1142 int i;
1143
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001144 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001145
1146 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001147 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001148 for (i = 0; i < 16; i++)
1149 session_id[i] = buffer_get_char(m);
1150
1151 /* Turn on permissions for getpwnam */
1152 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1153
1154 return (0);
1155}
1156
1157int
1158mm_answer_rsa_keyallowed(int socket, Buffer *m)
1159{
1160 BIGNUM *client_n;
1161 Key *key = NULL;
1162 u_char *blob = NULL;
1163 u_int blen = 0;
1164 int allowed = 0;
1165
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001166 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001167
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001168 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001169 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001170 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001171 buffer_get_bignum2(m, client_n);
1172 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1173 BN_clear_free(client_n);
1174 }
1175 buffer_clear(m);
1176 buffer_put_int(m, allowed);
1177
1178 /* clear temporarily storage (used by generate challenge) */
1179 monitor_reset_key_state();
1180
1181 if (allowed && key != NULL) {
1182 key->type = KEY_RSA; /* cheat for key_to_blob */
1183 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001184 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001185 buffer_put_string(m, blob, blen);
1186
1187 /* Save temporarily for comparison in verify */
1188 key_blob = blob;
1189 key_bloblen = blen;
1190 key_blobtype = MM_RSAUSERKEY;
1191 key_free(key);
1192 }
1193
1194 mm_append_debug(m);
1195
1196 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1197
1198 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1199 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1200 return (0);
1201}
1202
1203int
1204mm_answer_rsa_challenge(int socket, Buffer *m)
1205{
1206 Key *key = NULL;
1207 u_char *blob;
1208 u_int blen;
1209
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001210 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001211
1212 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001213 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001214 blob = buffer_get_string(m, &blen);
1215 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001216 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001217 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001218 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001219 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001220 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001221
1222 if (ssh1_challenge)
1223 BN_clear_free(ssh1_challenge);
1224 ssh1_challenge = auth_rsa_generate_challenge(key);
1225
1226 buffer_clear(m);
1227 buffer_put_bignum2(m, ssh1_challenge);
1228
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001229 debug3("%s sending reply", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001230 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1231
1232 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1233 return (0);
1234}
1235
1236int
1237mm_answer_rsa_response(int socket, Buffer *m)
1238{
1239 Key *key = NULL;
1240 u_char *blob, *response;
1241 u_int blen, len;
1242 int success;
1243
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001244 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001245
1246 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001247 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001248 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001249 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001250
1251 blob = buffer_get_string(m, &blen);
1252 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001253 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001254 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001255 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001256 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001257 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001258 response = buffer_get_string(m, &len);
1259 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001260 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001261 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1262
1263 key_free(key);
1264 xfree(response);
1265
1266 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1267
1268 /* reset state */
1269 BN_clear_free(ssh1_challenge);
1270 ssh1_challenge = NULL;
1271 monitor_reset_key_state();
1272
1273 buffer_clear(m);
1274 buffer_put_int(m, success);
1275 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1276
1277 return (success);
1278}
1279
1280int
1281mm_answer_term(int socket, Buffer *req)
1282{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001283 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001284 int res, status;
1285
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001286 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001287
1288 /* The child is terminating */
1289 session_destroy_all(&mm_session_close);
1290
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001291 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001292 if (errno != EINTR)
1293 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001294
1295 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1296
1297 /* Terminate process */
1298 exit (res);
1299}
1300
1301void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001302monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001303{
1304 if (compat20) {
1305 set_newkeys(MODE_IN);
1306 set_newkeys(MODE_OUT);
1307 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001308 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001309 packet_set_encryption_key(child_state.ssh1key,
1310 child_state.ssh1keylen, child_state.ssh1cipher);
1311 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001312 }
1313
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001314 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001315 packet_set_keycontext(MODE_OUT, child_state.keyout);
1316 xfree(child_state.keyout);
1317 packet_set_keycontext(MODE_IN, child_state.keyin);
1318 xfree(child_state.keyin);
1319
1320 if (!compat20) {
1321 packet_set_iv(MODE_OUT, child_state.ivout);
1322 xfree(child_state.ivout);
1323 packet_set_iv(MODE_IN, child_state.ivin);
1324 xfree(child_state.ivin);
1325 }
1326
1327 memcpy(&incoming_stream, &child_state.incoming,
1328 sizeof(incoming_stream));
1329 memcpy(&outgoing_stream, &child_state.outgoing,
1330 sizeof(outgoing_stream));
1331
1332 /* Update with new address */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001333 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001334
1335 /* Network I/O buffers */
1336 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1337 buffer_clear(&input);
1338 buffer_append(&input, child_state.input, child_state.ilen);
1339 memset(child_state.input, 0, child_state.ilen);
1340 xfree(child_state.input);
1341
1342 buffer_clear(&output);
1343 buffer_append(&output, child_state.output, child_state.olen);
1344 memset(child_state.output, 0, child_state.olen);
1345 xfree(child_state.output);
1346}
1347
1348static Kex *
1349mm_get_kex(Buffer *m)
1350{
1351 Kex *kex;
1352 void *blob;
1353 u_int bloblen;
1354
1355 kex = xmalloc(sizeof(*kex));
1356 memset(kex, 0, sizeof(*kex));
1357 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001358 if ((session_id2 == NULL) ||
1359 (kex->session_id_len != session_id2_len) ||
1360 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1361 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001362 kex->we_need = buffer_get_int(m);
1363 kex->server = 1;
1364 kex->hostkey_type = buffer_get_int(m);
1365 kex->kex_type = buffer_get_int(m);
1366 blob = buffer_get_string(m, &bloblen);
1367 buffer_init(&kex->my);
1368 buffer_append(&kex->my, blob, bloblen);
1369 xfree(blob);
1370 blob = buffer_get_string(m, &bloblen);
1371 buffer_init(&kex->peer);
1372 buffer_append(&kex->peer, blob, bloblen);
1373 xfree(blob);
1374 kex->done = 1;
1375 kex->flags = buffer_get_int(m);
1376 kex->client_version_string = buffer_get_string(m, NULL);
1377 kex->server_version_string = buffer_get_string(m, NULL);
1378 kex->load_host_key=&get_hostkey_by_type;
1379 kex->host_key_index=&get_hostkey_index;
1380
1381 return (kex);
1382}
1383
1384/* This function requries careful sanity checking */
1385
1386void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001387mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001388{
1389 Buffer m;
1390 u_char *blob, *p;
1391 u_int bloblen, plen;
1392
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001393 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001394
1395 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001396 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001397 if (!compat20) {
1398 child_state.ssh1protoflags = buffer_get_int(&m);
1399 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001400 child_state.ssh1key = buffer_get_string(&m,
1401 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001402 child_state.ivout = buffer_get_string(&m,
1403 &child_state.ivoutlen);
1404 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1405 goto skip;
1406 } else {
1407 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001408 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001409 }
1410
1411 blob = buffer_get_string(&m, &bloblen);
1412 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1413 xfree(blob);
1414
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001415 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001416 blob = buffer_get_string(&m, &bloblen);
1417 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1418 xfree(blob);
1419
1420 /* Now get sequence numbers for the packets */
1421 packet_set_seqnr(MODE_OUT, buffer_get_int(&m));
1422 packet_set_seqnr(MODE_IN, buffer_get_int(&m));
1423
1424 skip:
1425 /* Get the key context */
1426 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1427 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1428
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001429 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001430 /* Get compression state */
1431 p = buffer_get_string(&m, &plen);
1432 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001433 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001434 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1435 xfree(p);
1436
1437 p = buffer_get_string(&m, &plen);
1438 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001439 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001440 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1441 xfree(p);
1442
1443 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001444 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001445 child_state.input = buffer_get_string(&m, &child_state.ilen);
1446 child_state.output = buffer_get_string(&m, &child_state.olen);
1447
1448 buffer_free(&m);
1449}
1450
1451
1452/* Allocation functions for zlib */
1453void *
1454mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1455{
1456 void *address;
1457
1458 address = mm_malloc(mm, size * ncount);
1459
1460 return (address);
1461}
1462
1463void
1464mm_zfree(struct mm_master *mm, void *address)
1465{
1466 mm_free(mm, address);
1467}
1468
1469void
1470mm_init_compression(struct mm_master *mm)
1471{
1472 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1473 outgoing_stream.zfree = (free_func)mm_zfree;
1474 outgoing_stream.opaque = mm;
1475
1476 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1477 incoming_stream.zfree = (free_func)mm_zfree;
1478 incoming_stream.opaque = mm;
1479}
1480
1481/* XXX */
1482
1483#define FD_CLOSEONEXEC(x) do { \
1484 if (fcntl(x, F_SETFD, 1) == -1) \
1485 fatal("fcntl(%d, F_SETFD)", x); \
1486} while (0)
1487
1488static void
1489monitor_socketpair(int *pair)
1490{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001491#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001492 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001493 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001494#else
1495 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001496 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001497#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001498 FD_CLOSEONEXEC(pair[0]);
1499 FD_CLOSEONEXEC(pair[1]);
1500}
1501
1502#define MM_MEMSIZE 65536
1503
1504struct monitor *
1505monitor_init(void)
1506{
1507 struct monitor *mon;
1508 int pair[2];
1509
1510 mon = xmalloc(sizeof(*mon));
1511
1512 monitor_socketpair(pair);
1513
1514 mon->m_recvfd = pair[0];
1515 mon->m_sendfd = pair[1];
1516
1517 /* Used to share zlib space across processes */
1518 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1519 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1520
1521 /* Compression needs to share state across borders */
1522 mm_init_compression(mon->m_zlib);
1523
1524 return mon;
1525}
1526
1527void
1528monitor_reinit(struct monitor *mon)
1529{
1530 int pair[2];
1531
1532 monitor_socketpair(pair);
1533
1534 mon->m_recvfd = pair[0];
1535 mon->m_sendfd = pair[1];
1536}