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