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