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