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