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