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