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