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