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