Annotation of src/usr.bin/ssh/monitor.c, Revision 1.34.2.1
1.1 provos 1: /*
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"
1.34.2.1! brad 28: RCSID("$OpenBSD: monitor.c,v 1.49 2003/08/28 12:54:34 markus Exp $");
1.1 provos 29:
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:
1.34.2.1! brad 62: #ifdef GSSAPI
! 63: #include "ssh-gss.h"
! 64: static Gssctxt *gsscontext = NULL;
! 65: #endif
! 66:
1.1 provos 67: /* Imports */
68: extern ServerOptions options;
69: extern u_int utmp_len;
70: extern Newkeys *current_keys[];
71: extern z_stream incoming_stream;
72: extern z_stream outgoing_stream;
73: extern u_char session_id[];
74: extern Buffer input, output;
75: extern Buffer auth_debug;
76: extern int auth_debug_init;
77:
78: /* State exported from the child */
79:
80: struct {
81: z_stream incoming;
82: z_stream outgoing;
83: u_char *keyin;
84: u_int keyinlen;
85: u_char *keyout;
86: u_int keyoutlen;
87: u_char *ivin;
88: u_int ivinlen;
89: u_char *ivout;
90: u_int ivoutlen;
1.15 markus 91: u_char *ssh1key;
92: u_int ssh1keylen;
1.1 provos 93: int ssh1cipher;
94: int ssh1protoflags;
95: u_char *input;
96: u_int ilen;
97: u_char *output;
98: u_int olen;
99: } child_state;
100:
1.34.2.1! brad 101: /* Functions on the monitor that answer unprivileged requests */
1.1 provos 102:
103: int mm_answer_moduli(int, Buffer *);
104: int mm_answer_sign(int, Buffer *);
105: int mm_answer_pwnamallow(int, Buffer *);
1.10 djm 106: int mm_answer_auth2_read_banner(int, Buffer *);
1.1 provos 107: int mm_answer_authserv(int, Buffer *);
108: int mm_answer_authpassword(int, Buffer *);
109: int mm_answer_bsdauthquery(int, Buffer *);
110: int mm_answer_bsdauthrespond(int, Buffer *);
111: int mm_answer_skeyquery(int, Buffer *);
112: int mm_answer_skeyrespond(int, Buffer *);
113: int mm_answer_keyallowed(int, Buffer *);
114: int mm_answer_keyverify(int, Buffer *);
115: int mm_answer_pty(int, Buffer *);
116: int mm_answer_pty_cleanup(int, Buffer *);
117: int mm_answer_term(int, Buffer *);
118: int mm_answer_rsa_keyallowed(int, Buffer *);
119: int mm_answer_rsa_challenge(int, Buffer *);
120: int mm_answer_rsa_response(int, Buffer *);
121: int mm_answer_sesskey(int, Buffer *);
122: int mm_answer_sessid(int, Buffer *);
123:
1.34.2.1! brad 124: #ifdef GSSAPI
! 125: int mm_answer_gss_setup_ctx(int, Buffer *);
! 126: int mm_answer_gss_accept_ctx(int, Buffer *);
! 127: int mm_answer_gss_userok(int, Buffer *);
1.25 itojun 128: #endif
129:
1.1 provos 130: static Authctxt *authctxt;
131: static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
132:
133: /* local state for key verify */
134: static u_char *key_blob = NULL;
135: static u_int key_bloblen = 0;
136: static int key_blobtype = MM_NOKEY;
1.26 markus 137: static char *hostbased_cuser = NULL;
138: static char *hostbased_chost = NULL;
1.1 provos 139: static char *auth_method = "unknown";
1.34.2.1! brad 140: static u_int session_id2_len = 0;
1.13 markus 141: static u_char *session_id2 = NULL;
1.34.2.1! brad 142: static pid_t monitor_child_pid;
1.1 provos 143:
144: struct mon_table {
145: enum monitor_reqtype type;
146: int flags;
147: int (*f)(int, Buffer *);
148: };
149:
150: #define MON_ISAUTH 0x0004 /* Required for Authentication */
151: #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
152: #define MON_ONCE 0x0010 /* Disable after calling */
153:
154: #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
155:
156: #define MON_PERMIT 0x1000 /* Request is permitted */
157:
158: struct mon_table mon_dispatch_proto20[] = {
159: {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
160: {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
161: {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
162: {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
1.10 djm 163: {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
1.1 provos 164: {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
165: #ifdef BSD_AUTH
166: {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
167: {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
168: #endif
169: #ifdef SKEY
170: {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
171: {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
172: #endif
173: {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
174: {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
1.34.2.1! brad 175: #ifdef GSSAPI
! 176: {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
! 177: {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
! 178: {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
! 179: #endif
1.1 provos 180: {0, 0, NULL}
181: };
182:
183: struct mon_table mon_dispatch_postauth20[] = {
184: {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
185: {MONITOR_REQ_SIGN, 0, mm_answer_sign},
186: {MONITOR_REQ_PTY, 0, mm_answer_pty},
187: {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
188: {MONITOR_REQ_TERM, 0, mm_answer_term},
189: {0, 0, NULL}
190: };
191:
192: struct mon_table mon_dispatch_proto15[] = {
193: {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
194: {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
195: {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
196: {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
197: {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
198: {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
199: {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
200: {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
201: #ifdef BSD_AUTH
202: {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
203: {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
204: #endif
205: #ifdef SKEY
206: {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
207: {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
208: #endif
209: {0, 0, NULL}
210: };
211:
212: struct mon_table mon_dispatch_postauth15[] = {
213: {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
214: {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
215: {MONITOR_REQ_TERM, 0, mm_answer_term},
216: {0, 0, NULL}
217: };
218:
219: struct mon_table *mon_dispatch;
220:
221: /* Specifies if a certain message is allowed at the moment */
222:
223: static void
224: monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
225: {
226: while (ent->f != NULL) {
227: if (ent->type == type) {
228: ent->flags &= ~MON_PERMIT;
229: ent->flags |= permit ? MON_PERMIT : 0;
230: return;
231: }
232: ent++;
233: }
234: }
235:
236: static void
237: monitor_permit_authentications(int permit)
238: {
239: struct mon_table *ent = mon_dispatch;
240:
241: while (ent->f != NULL) {
242: if (ent->flags & MON_AUTH) {
243: ent->flags &= ~MON_PERMIT;
244: ent->flags |= permit ? MON_PERMIT : 0;
245: }
246: ent++;
247: }
248: }
249:
250: Authctxt *
1.11 mouring 251: monitor_child_preauth(struct monitor *pmonitor)
1.1 provos 252: {
253: struct mon_table *ent;
254: int authenticated = 0;
255:
256: debug3("preauth child monitor started");
257:
258: if (compat20) {
259: mon_dispatch = mon_dispatch_proto20;
260:
261: /* Permit requests for moduli and signatures */
262: monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
263: monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
264: } else {
265: mon_dispatch = mon_dispatch_proto15;
266:
267: monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
268: }
269:
270: authctxt = authctxt_new();
271:
272: /* The first few requests do not require asynchronous access */
273: while (!authenticated) {
1.11 mouring 274: authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
1.1 provos 275: if (authenticated) {
276: if (!(ent->flags & MON_AUTHDECIDE))
277: fatal("%s: unexpected authentication from %d",
1.14 markus 278: __func__, ent->type);
1.1 provos 279: if (authctxt->pw->pw_uid == 0 &&
280: !auth_root_allowed(auth_method))
281: authenticated = 0;
282: }
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)
1.14 markus 293: fatal("%s: authenticated invalid user", __func__);
1.1 provos 294:
295: debug("%s: %s has been authenticated by privileged process",
1.14 markus 296: __func__, authctxt->user);
1.1 provos 297:
1.11 mouring 298: mm_get_keystate(pmonitor);
1.1 provos 299:
300: return (authctxt);
301: }
302:
1.34.2.1! brad 303: static void
! 304: monitor_set_child_handler(pid_t pid)
! 305: {
! 306: monitor_child_pid = pid;
! 307: }
! 308:
! 309: static void
! 310: monitor_child_handler(int signal)
! 311: {
! 312: kill(monitor_child_pid, signal);
! 313: }
! 314:
1.1 provos 315: void
1.11 mouring 316: monitor_child_postauth(struct monitor *pmonitor)
1.1 provos 317: {
1.34.2.1! brad 318: monitor_set_child_handler(pmonitor->m_pid);
! 319: signal(SIGHUP, &monitor_child_handler);
! 320: signal(SIGTERM, &monitor_child_handler);
! 321:
1.1 provos 322: if (compat20) {
323: mon_dispatch = mon_dispatch_postauth20;
324:
325: /* Permit requests for moduli and signatures */
326: monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
327: monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
328: monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
329: } else {
330: mon_dispatch = mon_dispatch_postauth15;
331: monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
332: }
333: if (!no_pty_flag) {
334: monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
335: monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
336: }
337:
338: for (;;)
1.11 mouring 339: monitor_read(pmonitor, mon_dispatch, NULL);
1.1 provos 340: }
341:
342: void
1.11 mouring 343: monitor_sync(struct monitor *pmonitor)
1.1 provos 344: {
1.16 djm 345: if (options.compression) {
346: /* The member allocation is not visible, so sync it */
347: mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
348: }
1.1 provos 349: }
350:
351: int
1.11 mouring 352: monitor_read(struct monitor *pmonitor, struct mon_table *ent,
1.1 provos 353: struct mon_table **pent)
354: {
355: Buffer m;
356: int ret;
357: u_char type;
358:
359: buffer_init(&m);
360:
1.11 mouring 361: mm_request_receive(pmonitor->m_sendfd, &m);
1.1 provos 362: type = buffer_get_char(&m);
363:
1.14 markus 364: debug3("%s: checking request %d", __func__, type);
1.1 provos 365:
366: while (ent->f != NULL) {
367: if (ent->type == type)
368: break;
369: ent++;
370: }
371:
372: if (ent->f != NULL) {
373: if (!(ent->flags & MON_PERMIT))
1.14 markus 374: fatal("%s: unpermitted request %d", __func__,
1.1 provos 375: type);
1.11 mouring 376: ret = (*ent->f)(pmonitor->m_sendfd, &m);
1.1 provos 377: buffer_free(&m);
378:
379: /* The child may use this request only once, disable it */
380: if (ent->flags & MON_ONCE) {
1.14 markus 381: debug2("%s: %d used once, disabling now", __func__,
1.1 provos 382: type);
383: ent->flags &= ~MON_PERMIT;
384: }
385:
386: if (pent != NULL)
387: *pent = ent;
1.3 markus 388:
1.1 provos 389: return ret;
390: }
391:
1.14 markus 392: fatal("%s: unsupported request: %d", __func__, type);
1.1 provos 393:
394: /* NOTREACHED */
395: return (-1);
396: }
397:
398: /* allowed key state */
399: static int
400: monitor_allowed_key(u_char *blob, u_int bloblen)
401: {
402: /* make sure key is allowed */
403: if (key_blob == NULL || key_bloblen != bloblen ||
404: memcmp(key_blob, blob, key_bloblen))
405: return (0);
406: return (1);
407: }
408:
409: static void
410: monitor_reset_key_state(void)
411: {
412: /* reset state */
413: if (key_blob != NULL)
414: xfree(key_blob);
415: if (hostbased_cuser != NULL)
416: xfree(hostbased_cuser);
417: if (hostbased_chost != NULL)
418: xfree(hostbased_chost);
419: key_blob = NULL;
420: key_bloblen = 0;
421: key_blobtype = MM_NOKEY;
422: hostbased_cuser = NULL;
423: hostbased_chost = NULL;
424: }
425:
426: int
427: mm_answer_moduli(int socket, Buffer *m)
428: {
429: DH *dh;
430: int min, want, max;
431:
432: min = buffer_get_int(m);
433: want = buffer_get_int(m);
434: max = buffer_get_int(m);
435:
436: debug3("%s: got parameters: %d %d %d",
1.14 markus 437: __func__, min, want, max);
1.1 provos 438: /* We need to check here, too, in case the child got corrupted */
439: if (max < min || want < min || max < want)
440: fatal("%s: bad parameters: %d %d %d",
1.14 markus 441: __func__, min, want, max);
1.1 provos 442:
443: buffer_clear(m);
444:
445: dh = choose_dh(min, want, max);
446: if (dh == NULL) {
447: buffer_put_char(m, 0);
448: return (0);
449: } else {
450: /* Send first bignum */
451: buffer_put_char(m, 1);
452: buffer_put_bignum2(m, dh->p);
453: buffer_put_bignum2(m, dh->g);
1.3 markus 454:
1.1 provos 455: DH_free(dh);
456: }
457: mm_request_send(socket, MONITOR_ANS_MODULI, m);
458: return (0);
459: }
460:
461: int
462: mm_answer_sign(int socket, Buffer *m)
463: {
464: Key *key;
465: u_char *p;
466: u_char *signature;
467: u_int siglen, datlen;
468: int keyid;
1.3 markus 469:
1.14 markus 470: debug3("%s", __func__);
1.1 provos 471:
1.3 markus 472: keyid = buffer_get_int(m);
473: p = buffer_get_string(m, &datlen);
1.1 provos 474:
475: if (datlen != 20)
1.19 deraadt 476: fatal("%s: data length incorrect: %u", __func__, datlen);
1.1 provos 477:
1.13 markus 478: /* save session id, it will be passed on the first call */
479: if (session_id2_len == 0) {
480: session_id2_len = datlen;
481: session_id2 = xmalloc(session_id2_len);
482: memcpy(session_id2, p, session_id2_len);
483: }
484:
1.1 provos 485: if ((key = get_hostkey_by_index(keyid)) == NULL)
1.14 markus 486: fatal("%s: no hostkey from index %d", __func__, keyid);
1.1 provos 487: if (key_sign(key, &signature, &siglen, p, datlen) < 0)
1.14 markus 488: fatal("%s: key_sign failed", __func__);
1.1 provos 489:
1.19 deraadt 490: debug3("%s: signature %p(%u)", __func__, signature, siglen);
1.1 provos 491:
492: buffer_clear(m);
493: buffer_put_string(m, signature, siglen);
494:
495: xfree(p);
496: xfree(signature);
497:
498: mm_request_send(socket, MONITOR_ANS_SIGN, m);
499:
500: /* Turn on permissions for getpwnam */
501: monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
502:
503: return (0);
504: }
505:
506: /* Retrieves the password entry and also checks if the user is permitted */
507:
508: int
509: mm_answer_pwnamallow(int socket, Buffer *m)
510: {
511: char *login;
512: struct passwd *pwent;
513: int allowed = 0;
1.3 markus 514:
1.14 markus 515: debug3("%s", __func__);
1.1 provos 516:
517: if (authctxt->attempt++ != 0)
1.14 markus 518: fatal("%s: multiple attempts for getpwnam", __func__);
1.1 provos 519:
520: login = buffer_get_string(m, NULL);
521:
522: pwent = getpwnamallow(login);
523:
524: authctxt->user = xstrdup(login);
525: setproctitle("%s [priv]", pwent ? login : "unknown");
526: xfree(login);
527:
528: buffer_clear(m);
529:
530: if (pwent == NULL) {
531: buffer_put_char(m, 0);
532: goto out;
533: }
534:
535: allowed = 1;
1.4 markus 536: authctxt->pw = pwent;
1.1 provos 537: authctxt->valid = 1;
538:
539: buffer_put_char(m, 1);
540: buffer_put_string(m, pwent, sizeof(struct passwd));
541: buffer_put_cstring(m, pwent->pw_name);
542: buffer_put_cstring(m, "*");
543: buffer_put_cstring(m, pwent->pw_gecos);
544: buffer_put_cstring(m, pwent->pw_class);
545: buffer_put_cstring(m, pwent->pw_dir);
546: buffer_put_cstring(m, pwent->pw_shell);
547:
548: out:
1.14 markus 549: debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
1.1 provos 550: mm_request_send(socket, MONITOR_ANS_PWNAM, m);
551:
552: /* For SSHv1 allow authentication now */
553: if (!compat20)
554: monitor_permit_authentications(1);
1.10 djm 555: else {
1.1 provos 556: /* Allow service/style information on the auth context */
557: monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
1.10 djm 558: monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
559: }
560:
561:
562: return (0);
563: }
564:
565: int mm_answer_auth2_read_banner(int socket, Buffer *m)
566: {
567: char *banner;
568:
569: buffer_clear(m);
570: banner = auth2_read_banner();
571: buffer_put_cstring(m, banner != NULL ? banner : "");
572: mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
1.1 provos 573:
1.10 djm 574: if (banner != NULL)
1.21 deraadt 575: xfree(banner);
1.1 provos 576:
577: return (0);
578: }
579:
580: int
581: mm_answer_authserv(int socket, Buffer *m)
582: {
583: monitor_permit_authentications(1);
584:
585: authctxt->service = buffer_get_string(m, NULL);
586: authctxt->style = buffer_get_string(m, NULL);
1.6 stevesk 587: debug3("%s: service=%s, style=%s",
1.14 markus 588: __func__, authctxt->service, authctxt->style);
1.6 stevesk 589:
1.1 provos 590: if (strlen(authctxt->style) == 0) {
591: xfree(authctxt->style);
592: authctxt->style = NULL;
593: }
594:
595: return (0);
596: }
597:
598: int
599: mm_answer_authpassword(int socket, Buffer *m)
600: {
601: static int call_count;
602: char *passwd;
1.22 stevesk 603: int authenticated;
604: u_int plen;
1.1 provos 605:
606: passwd = buffer_get_string(m, &plen);
607: /* Only authenticate if the context is valid */
1.12 markus 608: authenticated = options.password_authentication &&
1.34.2.1! brad 609: auth_password(authctxt, passwd);
1.1 provos 610: memset(passwd, 0, strlen(passwd));
611: xfree(passwd);
612:
613: buffer_clear(m);
614: buffer_put_int(m, authenticated);
615:
1.14 markus 616: debug3("%s: sending result %d", __func__, authenticated);
1.1 provos 617: mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
618:
619: call_count++;
620: if (plen == 0 && call_count == 1)
621: auth_method = "none";
622: else
623: auth_method = "password";
624:
625: /* Causes monitor loop to terminate if authenticated */
626: return (authenticated);
627: }
628:
629: #ifdef BSD_AUTH
630: int
631: mm_answer_bsdauthquery(int socket, Buffer *m)
632: {
633: char *name, *infotxt;
634: u_int numprompts;
635: u_int *echo_on;
636: char **prompts;
1.31 markus 637: u_int success;
1.1 provos 638:
1.31 markus 639: success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
640: &prompts, &echo_on) < 0 ? 0 : 1;
1.1 provos 641:
642: buffer_clear(m);
1.31 markus 643: buffer_put_int(m, success);
644: if (success)
1.1 provos 645: buffer_put_cstring(m, prompts[0]);
646:
1.31 markus 647: debug3("%s: sending challenge success: %u", __func__, success);
1.1 provos 648: mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
649:
1.31 markus 650: if (success) {
1.1 provos 651: xfree(name);
652: xfree(infotxt);
653: xfree(prompts);
654: xfree(echo_on);
655: }
656:
657: return (0);
658: }
659:
660: int
661: mm_answer_bsdauthrespond(int socket, Buffer *m)
662: {
663: char *response;
664: int authok;
665:
666: if (authctxt->as == 0)
1.14 markus 667: fatal("%s: no bsd auth session", __func__);
1.1 provos 668:
669: response = buffer_get_string(m, NULL);
1.12 markus 670: authok = options.challenge_response_authentication &&
671: auth_userresponse(authctxt->as, response, 0);
1.1 provos 672: authctxt->as = NULL;
1.14 markus 673: debug3("%s: <%s> = <%d>", __func__, response, authok);
1.1 provos 674: xfree(response);
675:
676: buffer_clear(m);
677: buffer_put_int(m, authok);
678:
1.14 markus 679: debug3("%s: sending authenticated: %d", __func__, authok);
1.1 provos 680: mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
681:
682: auth_method = "bsdauth";
683:
684: return (authok != 0);
685: }
686: #endif
687:
688: #ifdef SKEY
689: int
690: mm_answer_skeyquery(int socket, Buffer *m)
691: {
692: struct skey skey;
693: char challenge[1024];
1.31 markus 694: u_int success;
1.1 provos 695:
1.31 markus 696: success = skeychallenge(&skey, authctxt->user, challenge) < 0 ? 0 : 1;
1.1 provos 697:
698: buffer_clear(m);
1.31 markus 699: buffer_put_int(m, success);
700: if (success)
1.1 provos 701: buffer_put_cstring(m, challenge);
702:
1.31 markus 703: debug3("%s: sending challenge success: %u", __func__, success);
1.1 provos 704: mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
705:
706: return (0);
707: }
708:
709: int
710: mm_answer_skeyrespond(int socket, Buffer *m)
711: {
712: char *response;
713: int authok;
714:
715: response = buffer_get_string(m, NULL);
716:
1.12 markus 717: authok = (options.challenge_response_authentication &&
718: authctxt->valid &&
1.1 provos 719: skey_haskey(authctxt->pw->pw_name) == 0 &&
720: skey_passcheck(authctxt->pw->pw_name, response) != -1);
721:
722: xfree(response);
723:
724: buffer_clear(m);
725: buffer_put_int(m, authok);
726:
1.14 markus 727: debug3("%s: sending authenticated: %d", __func__, authok);
1.1 provos 728: mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
729:
730: auth_method = "skey";
731:
732: return (authok != 0);
733: }
734: #endif
735:
1.2 markus 736: static void
1.1 provos 737: mm_append_debug(Buffer *m)
738: {
739: if (auth_debug_init && buffer_len(&auth_debug)) {
1.14 markus 740: debug3("%s: Appending debug messages for child", __func__);
1.1 provos 741: buffer_append(m, buffer_ptr(&auth_debug),
742: buffer_len(&auth_debug));
743: buffer_clear(&auth_debug);
744: }
745: }
746:
747: int
748: mm_answer_keyallowed(int socket, Buffer *m)
749: {
750: Key *key;
1.26 markus 751: char *cuser, *chost;
752: u_char *blob;
1.1 provos 753: u_int bloblen;
754: enum mm_keytype type = 0;
755: int allowed = 0;
756:
1.14 markus 757: debug3("%s entering", __func__);
1.3 markus 758:
1.1 provos 759: type = buffer_get_int(m);
760: cuser = buffer_get_string(m, NULL);
761: chost = buffer_get_string(m, NULL);
762: blob = buffer_get_string(m, &bloblen);
763:
764: key = key_from_blob(blob, bloblen);
765:
766: if ((compat20 && type == MM_RSAHOSTKEY) ||
767: (!compat20 && type != MM_RSAHOSTKEY))
1.14 markus 768: fatal("%s: key type and protocol mismatch", __func__);
1.1 provos 769:
1.14 markus 770: debug3("%s: key_from_blob: %p", __func__, key);
1.1 provos 771:
772: if (key != NULL && authctxt->pw != NULL) {
773: switch(type) {
774: case MM_USERKEY:
1.12 markus 775: allowed = options.pubkey_authentication &&
776: user_key_allowed(authctxt->pw, key);
1.1 provos 777: break;
778: case MM_HOSTKEY:
1.12 markus 779: allowed = options.hostbased_authentication &&
780: hostbased_key_allowed(authctxt->pw,
1.1 provos 781: cuser, chost, key);
782: break;
783: case MM_RSAHOSTKEY:
784: key->type = KEY_RSA1; /* XXX */
1.12 markus 785: allowed = options.rhosts_rsa_authentication &&
786: auth_rhosts_rsa_key_allowed(authctxt->pw,
1.1 provos 787: cuser, chost, key);
788: break;
789: default:
1.14 markus 790: fatal("%s: unknown key type %d", __func__, type);
1.1 provos 791: break;
792: }
1.33 markus 793: }
794: if (key != NULL)
1.1 provos 795: key_free(key);
796:
797: /* clear temporarily storage (used by verify) */
798: monitor_reset_key_state();
799:
800: if (allowed) {
801: /* Save temporarily for comparison in verify */
802: key_blob = blob;
803: key_bloblen = bloblen;
804: key_blobtype = type;
805: hostbased_cuser = cuser;
806: hostbased_chost = chost;
807: }
808:
809: debug3("%s: key %p is %s",
1.14 markus 810: __func__, key, allowed ? "allowed" : "disallowed");
1.1 provos 811:
812: buffer_clear(m);
813: buffer_put_int(m, allowed);
1.32 markus 814: buffer_put_int(m, forced_command != NULL);
1.1 provos 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:
826: static int
827: monitor_valid_userblob(u_char *data, u_int datalen)
828: {
829: Buffer b;
1.26 markus 830: char *p;
1.1 provos 831: u_int len;
832: int fail = 0;
833:
834: buffer_init(&b);
835: buffer_append(&b, data, datalen);
1.3 markus 836:
1.1 provos 837: if (datafellows & SSH_OLD_SESSIONID) {
1.13 markus 838: 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++;
1.1 provos 844: buffer_consume(&b, session_id2_len);
845: } else {
1.13 markus 846: 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))
1.1 provos 850: fail++;
1.13 markus 851: xfree(p);
1.1 provos 852: }
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) {
1.34.2.1! brad 857: logit("wrong user name passed to monitor: expected %s != %.100s",
1.1 provos 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:
882: static int
1.26 markus 883: monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
884: char *chost)
1.1 provos 885: {
886: Buffer b;
1.26 markus 887: char *p;
1.1 provos 888: u_int len;
889: int fail = 0;
890:
891: buffer_init(&b);
892: buffer_append(&b, data, datalen);
1.3 markus 893:
1.13 markus 894: 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))
1.1 provos 898: fail++;
1.13 markus 899: xfree(p);
900:
1.1 provos 901: 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) {
1.34.2.1! brad 905: logit("wrong user name passed to monitor: expected %s != %.100s",
1.1 provos 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:
938: int
939: mm_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 ||
1.8 mouring 952: !monitor_allowed_key(blob, bloblen))
1.14 markus 953: fatal("%s: bad key, not previously allowed", __func__);
1.1 provos 954:
955: key = key_from_blob(blob, bloblen);
956: if (key == NULL)
1.14 markus 957: fatal("%s: bad public key blob", __func__);
1.1 provos 958:
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)
1.14 markus 972: fatal("%s: bad signature data blob", __func__);
1.1 provos 973:
974: verified = key_verify(key, signature, signaturelen, data, datalen);
975: debug3("%s: key %p signature %s",
1.14 markus 976: __func__, key, verified ? "verified" : "unverified");
1.1 provos 977:
978: key_free(key);
979: xfree(blob);
980: xfree(signature);
981: xfree(data);
982:
1.17 stevesk 983: auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
984:
1.1 provos 985: monitor_reset_key_state();
1.3 markus 986:
1.1 provos 987: buffer_clear(m);
988: buffer_put_int(m, verified);
989: mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
990:
991: return (verified);
992: }
993:
1.2 markus 994: static void
1.1 provos 995: mm_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));
1.24 stevesk 1005: fromlen = sizeof(from);
1.1 provos 1006: if (packet_connection_is_on_socket()) {
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,
1.34.2.1! brad 1015: get_remote_name_or_ip(utmp_len, options.use_dns),
1.24 stevesk 1016: (struct sockaddr *)&from, fromlen);
1.1 provos 1017: }
1018:
1019: static void
1020: mm_session_close(Session *s)
1021: {
1.34.2.1! brad 1022: debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1.1 provos 1023: if (s->ttyfd != -1) {
1.14 markus 1024: debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1.1 provos 1025: fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1026: session_pty_cleanup2(s);
1027: }
1028: s->used = 0;
1029: }
1030:
1031: int
1032: mm_answer_pty(int socket, Buffer *m)
1033: {
1.11 mouring 1034: extern struct monitor *pmonitor;
1.1 provos 1035: Session *s;
1036: int res, fd0;
1037:
1.14 markus 1038: debug3("%s entering", __func__);
1.1 provos 1039:
1040: buffer_clear(m);
1041: s = session_new();
1042: if (s == NULL)
1043: goto error;
1044: s->authctxt = authctxt;
1045: s->pw = authctxt->pw;
1.11 mouring 1046: s->pid = pmonitor->m_pid;
1.1 provos 1047: 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)
1.14 markus 1062: fatal("%s: dup2", __func__);
1.1 provos 1063:
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)
1.14 markus 1071: fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1.1 provos 1072: if (fd0 != 0)
1.14 markus 1073: error("%s: fd0 %d != 0", __func__, fd0);
1.1 provos 1074:
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:
1.14 markus 1081: debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1.1 provos 1082:
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:
1093: int
1094: mm_answer_pty_cleanup(int socket, Buffer *m)
1095: {
1096: Session *s;
1097: char *tty;
1098:
1.14 markus 1099: debug3("%s entering", __func__);
1.1 provos 1100:
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:
1109: int
1110: mm_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)
1.14 markus 1119: fatal("%s: BN_new", __func__);
1.1 provos 1120:
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:
1139: int
1140: mm_answer_sessid(int socket, Buffer *m)
1141: {
1142: int i;
1143:
1.14 markus 1144: debug3("%s entering", __func__);
1.1 provos 1145:
1146: if (buffer_len(m) != 16)
1.14 markus 1147: fatal("%s: bad ssh1 session id", __func__);
1.1 provos 1148: 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:
1157: int
1158: mm_answer_rsa_keyallowed(int socket, Buffer *m)
1159: {
1160: BIGNUM *client_n;
1161: Key *key = NULL;
1.3 markus 1162: u_char *blob = NULL;
1163: u_int blen = 0;
1.1 provos 1164: int allowed = 0;
1165:
1.14 markus 1166: debug3("%s entering", __func__);
1.1 provos 1167:
1.12 markus 1168: if (options.rsa_authentication && authctxt->valid) {
1.1 provos 1169: if ((client_n = BN_new()) == NULL)
1.14 markus 1170: fatal("%s: BN_new", __func__);
1.1 provos 1171: 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);
1.32 markus 1177: buffer_put_int(m, forced_command != NULL);
1.1 provos 1178:
1179: /* clear temporarily storage (used by generate challenge) */
1180: monitor_reset_key_state();
1181:
1182: if (allowed && key != NULL) {
1183: key->type = KEY_RSA; /* cheat for key_to_blob */
1184: if (key_to_blob(key, &blob, &blen) == 0)
1.14 markus 1185: fatal("%s: key_to_blob failed", __func__);
1.1 provos 1186: buffer_put_string(m, blob, blen);
1187:
1188: /* Save temporarily for comparison in verify */
1189: key_blob = blob;
1190: key_bloblen = blen;
1191: key_blobtype = MM_RSAUSERKEY;
1.33 markus 1192: }
1193: if (key != NULL)
1.1 provos 1194: key_free(key);
1195:
1196: mm_append_debug(m);
1197:
1198: mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1199:
1200: monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1201: monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1202: return (0);
1203: }
1204:
1205: int
1206: mm_answer_rsa_challenge(int socket, Buffer *m)
1207: {
1208: Key *key = NULL;
1.3 markus 1209: u_char *blob;
1210: u_int blen;
1.1 provos 1211:
1.14 markus 1212: debug3("%s entering", __func__);
1.1 provos 1213:
1214: if (!authctxt->valid)
1.14 markus 1215: fatal("%s: authctxt not valid", __func__);
1.1 provos 1216: blob = buffer_get_string(m, &blen);
1217: if (!monitor_allowed_key(blob, blen))
1.14 markus 1218: fatal("%s: bad key, not previously allowed", __func__);
1.1 provos 1219: if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1.14 markus 1220: fatal("%s: key type mismatch", __func__);
1.1 provos 1221: if ((key = key_from_blob(blob, blen)) == NULL)
1.14 markus 1222: fatal("%s: received bad key", __func__);
1.1 provos 1223:
1224: if (ssh1_challenge)
1225: BN_clear_free(ssh1_challenge);
1226: ssh1_challenge = auth_rsa_generate_challenge(key);
1227:
1228: buffer_clear(m);
1229: buffer_put_bignum2(m, ssh1_challenge);
1230:
1.14 markus 1231: debug3("%s sending reply", __func__);
1.1 provos 1232: mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1233:
1234: monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1.33 markus 1235:
1236: xfree(blob);
1237: key_free(key);
1.1 provos 1238: return (0);
1239: }
1240:
1241: int
1242: mm_answer_rsa_response(int socket, Buffer *m)
1243: {
1244: Key *key = NULL;
1.3 markus 1245: u_char *blob, *response;
1246: u_int blen, len;
1247: int success;
1.1 provos 1248:
1.14 markus 1249: debug3("%s entering", __func__);
1.1 provos 1250:
1251: if (!authctxt->valid)
1.14 markus 1252: fatal("%s: authctxt not valid", __func__);
1.1 provos 1253: if (ssh1_challenge == NULL)
1.14 markus 1254: fatal("%s: no ssh1_challenge", __func__);
1.1 provos 1255:
1256: blob = buffer_get_string(m, &blen);
1257: if (!monitor_allowed_key(blob, blen))
1.14 markus 1258: fatal("%s: bad key, not previously allowed", __func__);
1.1 provos 1259: if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1.14 markus 1260: fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1.1 provos 1261: if ((key = key_from_blob(blob, blen)) == NULL)
1.14 markus 1262: fatal("%s: received bad key", __func__);
1.1 provos 1263: response = buffer_get_string(m, &len);
1264: if (len != 16)
1.14 markus 1265: fatal("%s: received bad response to challenge", __func__);
1.1 provos 1266: success = auth_rsa_verify_response(key, ssh1_challenge, response);
1267:
1.33 markus 1268: xfree(blob);
1.1 provos 1269: key_free(key);
1270: xfree(response);
1271:
1272: auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1273:
1274: /* reset state */
1275: BN_clear_free(ssh1_challenge);
1276: ssh1_challenge = NULL;
1277: monitor_reset_key_state();
1278:
1279: buffer_clear(m);
1280: buffer_put_int(m, success);
1281: mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1282:
1283: return (success);
1284: }
1.25 itojun 1285:
1.1 provos 1286: int
1287: mm_answer_term(int socket, Buffer *req)
1288: {
1.11 mouring 1289: extern struct monitor *pmonitor;
1.1 provos 1290: int res, status;
1291:
1.14 markus 1292: debug3("%s: tearing down sessions", __func__);
1.1 provos 1293:
1294: /* The child is terminating */
1295: session_destroy_all(&mm_session_close);
1296:
1.11 mouring 1297: while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1.9 markus 1298: if (errno != EINTR)
1299: exit(1);
1.1 provos 1300:
1301: res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1302:
1303: /* Terminate process */
1304: exit (res);
1305: }
1306:
1307: void
1.11 mouring 1308: monitor_apply_keystate(struct monitor *pmonitor)
1.1 provos 1309: {
1310: if (compat20) {
1311: set_newkeys(MODE_IN);
1312: set_newkeys(MODE_OUT);
1313: } else {
1314: packet_set_protocol_flags(child_state.ssh1protoflags);
1.15 markus 1315: packet_set_encryption_key(child_state.ssh1key,
1316: child_state.ssh1keylen, child_state.ssh1cipher);
1317: xfree(child_state.ssh1key);
1.1 provos 1318: }
1319:
1.15 markus 1320: /* for rc4 and other stateful ciphers */
1.1 provos 1321: packet_set_keycontext(MODE_OUT, child_state.keyout);
1322: xfree(child_state.keyout);
1323: packet_set_keycontext(MODE_IN, child_state.keyin);
1324: xfree(child_state.keyin);
1325:
1326: if (!compat20) {
1327: packet_set_iv(MODE_OUT, child_state.ivout);
1328: xfree(child_state.ivout);
1329: packet_set_iv(MODE_IN, child_state.ivin);
1330: xfree(child_state.ivin);
1331: }
1332:
1333: memcpy(&incoming_stream, &child_state.incoming,
1334: sizeof(incoming_stream));
1335: memcpy(&outgoing_stream, &child_state.outgoing,
1336: sizeof(outgoing_stream));
1.3 markus 1337:
1.1 provos 1338: /* Update with new address */
1.16 djm 1339: if (options.compression)
1340: mm_init_compression(pmonitor->m_zlib);
1.1 provos 1341:
1342: /* Network I/O buffers */
1343: /* XXX inefficient for large buffers, need: buffer_init_from_string */
1344: buffer_clear(&input);
1345: buffer_append(&input, child_state.input, child_state.ilen);
1346: memset(child_state.input, 0, child_state.ilen);
1347: xfree(child_state.input);
1348:
1349: buffer_clear(&output);
1350: buffer_append(&output, child_state.output, child_state.olen);
1351: memset(child_state.output, 0, child_state.olen);
1352: xfree(child_state.output);
1353: }
1354:
1.2 markus 1355: static Kex *
1.1 provos 1356: mm_get_kex(Buffer *m)
1357: {
1358: Kex *kex;
1359: void *blob;
1360: u_int bloblen;
1361:
1362: kex = xmalloc(sizeof(*kex));
1363: memset(kex, 0, sizeof(*kex));
1364: kex->session_id = buffer_get_string(m, &kex->session_id_len);
1.13 markus 1365: if ((session_id2 == NULL) ||
1366: (kex->session_id_len != session_id2_len) ||
1367: (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1368: fatal("mm_get_get: internal error: bad session id");
1.1 provos 1369: kex->we_need = buffer_get_int(m);
1.34 markus 1370: kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1371: kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1.1 provos 1372: kex->server = 1;
1373: kex->hostkey_type = buffer_get_int(m);
1374: kex->kex_type = buffer_get_int(m);
1375: blob = buffer_get_string(m, &bloblen);
1376: buffer_init(&kex->my);
1377: buffer_append(&kex->my, blob, bloblen);
1378: xfree(blob);
1379: blob = buffer_get_string(m, &bloblen);
1380: buffer_init(&kex->peer);
1381: buffer_append(&kex->peer, blob, bloblen);
1382: xfree(blob);
1383: kex->done = 1;
1384: kex->flags = buffer_get_int(m);
1385: kex->client_version_string = buffer_get_string(m, NULL);
1386: kex->server_version_string = buffer_get_string(m, NULL);
1387: kex->load_host_key=&get_hostkey_by_type;
1388: kex->host_key_index=&get_hostkey_index;
1389:
1390: return (kex);
1391: }
1392:
1393: /* This function requries careful sanity checking */
1394:
1395: void
1.11 mouring 1396: mm_get_keystate(struct monitor *pmonitor)
1.1 provos 1397: {
1398: Buffer m;
1399: u_char *blob, *p;
1400: u_int bloblen, plen;
1.34.2.1! brad 1401: u_int32_t seqnr, packets;
! 1402: u_int64_t blocks;
1.1 provos 1403:
1.14 markus 1404: debug3("%s: Waiting for new keys", __func__);
1.1 provos 1405:
1406: buffer_init(&m);
1.11 mouring 1407: mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1.1 provos 1408: if (!compat20) {
1409: child_state.ssh1protoflags = buffer_get_int(&m);
1410: child_state.ssh1cipher = buffer_get_int(&m);
1.15 markus 1411: child_state.ssh1key = buffer_get_string(&m,
1412: &child_state.ssh1keylen);
1.1 provos 1413: child_state.ivout = buffer_get_string(&m,
1414: &child_state.ivoutlen);
1415: child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1416: goto skip;
1417: } else {
1418: /* Get the Kex for rekeying */
1.11 mouring 1419: *pmonitor->m_pkex = mm_get_kex(&m);
1.1 provos 1420: }
1421:
1422: blob = buffer_get_string(&m, &bloblen);
1423: current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1424: xfree(blob);
1425:
1.14 markus 1426: debug3("%s: Waiting for second key", __func__);
1.1 provos 1427: blob = buffer_get_string(&m, &bloblen);
1428: current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1429: xfree(blob);
1.3 markus 1430:
1.1 provos 1431: /* Now get sequence numbers for the packets */
1.34.2.1! brad 1432: seqnr = buffer_get_int(&m);
! 1433: blocks = buffer_get_int64(&m);
! 1434: packets = buffer_get_int(&m);
! 1435: packet_set_state(MODE_OUT, seqnr, blocks, packets);
! 1436: seqnr = buffer_get_int(&m);
! 1437: blocks = buffer_get_int64(&m);
! 1438: packets = buffer_get_int(&m);
! 1439: packet_set_state(MODE_IN, seqnr, blocks, packets);
1.1 provos 1440:
1441: skip:
1442: /* Get the key context */
1443: child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1444: child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1445:
1.14 markus 1446: debug3("%s: Getting compression state", __func__);
1.1 provos 1447: /* Get compression state */
1448: p = buffer_get_string(&m, &plen);
1449: if (plen != sizeof(child_state.outgoing))
1.14 markus 1450: fatal("%s: bad request size", __func__);
1.1 provos 1451: memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1452: xfree(p);
1453:
1454: p = buffer_get_string(&m, &plen);
1455: if (plen != sizeof(child_state.incoming))
1.14 markus 1456: fatal("%s: bad request size", __func__);
1.1 provos 1457: memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1458: xfree(p);
1459:
1460: /* Network I/O buffers */
1.14 markus 1461: debug3("%s: Getting Network I/O buffers", __func__);
1.1 provos 1462: child_state.input = buffer_get_string(&m, &child_state.ilen);
1463: child_state.output = buffer_get_string(&m, &child_state.olen);
1464:
1465: buffer_free(&m);
1466: }
1467:
1468:
1469: /* Allocation functions for zlib */
1470: void *
1471: mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1472: {
1.30 markus 1473: size_t len = (size_t) size * ncount;
1.1 provos 1474: void *address;
1475:
1.23 millert 1476: if (len == 0 || ncount > SIZE_T_MAX / size)
1.18 deraadt 1477: fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1478:
1479: address = mm_malloc(mm, len);
1.1 provos 1480:
1481: return (address);
1482: }
1483:
1484: void
1485: mm_zfree(struct mm_master *mm, void *address)
1486: {
1487: mm_free(mm, address);
1488: }
1489:
1490: void
1491: mm_init_compression(struct mm_master *mm)
1492: {
1493: outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1494: outgoing_stream.zfree = (free_func)mm_zfree;
1495: outgoing_stream.opaque = mm;
1496:
1497: incoming_stream.zalloc = (alloc_func)mm_zalloc;
1498: incoming_stream.zfree = (free_func)mm_zfree;
1499: incoming_stream.opaque = mm;
1500: }
1501:
1502: /* XXX */
1503:
1504: #define FD_CLOSEONEXEC(x) do { \
1505: if (fcntl(x, F_SETFD, 1) == -1) \
1506: fatal("fcntl(%d, F_SETFD)", x); \
1507: } while (0)
1508:
1.2 markus 1509: static void
1.1 provos 1510: monitor_socketpair(int *pair)
1.3 markus 1511: {
1.1 provos 1512: if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1.14 markus 1513: fatal("%s: socketpair", __func__);
1.1 provos 1514: FD_CLOSEONEXEC(pair[0]);
1515: FD_CLOSEONEXEC(pair[1]);
1516: }
1517:
1518: #define MM_MEMSIZE 65536
1519:
1520: struct monitor *
1521: monitor_init(void)
1522: {
1523: struct monitor *mon;
1524: int pair[2];
1525:
1526: mon = xmalloc(sizeof(*mon));
1527:
1528: monitor_socketpair(pair);
1529:
1530: mon->m_recvfd = pair[0];
1531: mon->m_sendfd = pair[1];
1532:
1533: /* Used to share zlib space across processes */
1.16 djm 1534: if (options.compression) {
1535: mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1536: mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1.1 provos 1537:
1.16 djm 1538: /* Compression needs to share state across borders */
1539: mm_init_compression(mon->m_zlib);
1540: }
1.1 provos 1541:
1542: return mon;
1543: }
1544:
1545: void
1546: monitor_reinit(struct monitor *mon)
1547: {
1548: int pair[2];
1549:
1550: monitor_socketpair(pair);
1551:
1552: mon->m_recvfd = pair[0];
1553: mon->m_sendfd = pair[1];
1554: }
1.34.2.1! brad 1555:
! 1556: #ifdef GSSAPI
! 1557: int
! 1558: mm_answer_gss_setup_ctx(int socket, Buffer *m)
! 1559: {
! 1560: gss_OID_desc oid;
! 1561: OM_uint32 major;
! 1562: u_int len;
! 1563:
! 1564: oid.elements = buffer_get_string(m, &len);
! 1565: oid.length = len;
! 1566:
! 1567: major = ssh_gssapi_server_ctx(&gsscontext, &oid);
! 1568:
! 1569: xfree(oid.elements);
! 1570:
! 1571: buffer_clear(m);
! 1572: buffer_put_int(m, major);
! 1573:
! 1574: mm_request_send(socket,MONITOR_ANS_GSSSETUP, m);
! 1575:
! 1576: /* Now we have a context, enable the step */
! 1577: monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
! 1578:
! 1579: return (0);
! 1580: }
! 1581:
! 1582: int
! 1583: mm_answer_gss_accept_ctx(int socket, Buffer *m)
! 1584: {
! 1585: gss_buffer_desc in;
! 1586: gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
! 1587: OM_uint32 major,minor;
! 1588: OM_uint32 flags = 0; /* GSI needs this */
! 1589: u_int len;
! 1590:
! 1591: in.value = buffer_get_string(m, &len);
! 1592: in.length = len;
! 1593: major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
! 1594: xfree(in.value);
! 1595:
! 1596: buffer_clear(m);
! 1597: buffer_put_int(m, major);
! 1598: buffer_put_string(m, out.value, out.length);
! 1599: buffer_put_int(m, flags);
! 1600: mm_request_send(socket, MONITOR_ANS_GSSSTEP, m);
! 1601:
! 1602: gss_release_buffer(&minor, &out);
! 1603:
! 1604: /* Complete - now we can do signing */
! 1605: if (major==GSS_S_COMPLETE) {
! 1606: monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
! 1607: monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
! 1608: }
! 1609: return (0);
! 1610: }
! 1611:
! 1612: int
! 1613: mm_answer_gss_userok(int socket, Buffer *m)
! 1614: {
! 1615: int authenticated;
! 1616:
! 1617: authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
! 1618:
! 1619: buffer_clear(m);
! 1620: buffer_put_int(m, authenticated);
! 1621:
! 1622: debug3("%s: sending result %d", __func__, authenticated);
! 1623: mm_request_send(socket, MONITOR_ANS_GSSUSEROK, m);
! 1624:
! 1625: auth_method="gssapi";
! 1626:
! 1627: /* Monitor loop will terminate if authenticated */
! 1628: return (authenticated);
! 1629: }
! 1630: #endif /* GSSAPI */