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