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