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