Annotation of src/usr.bin/ssh/monitor.c, Revision 1.91
1.91 ! djm 1: /* $OpenBSD: monitor.c,v 1.90 2007/02/19 10:45:58 dtucker 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:
1108: mm_send_fd(sock, s->ptyfd);
1109: mm_send_fd(sock, s->ttyfd);
1.1 provos 1110:
1111: /* make sure nothing uses fd 0 */
1112: if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1.14 markus 1113: fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1.1 provos 1114: if (fd0 != 0)
1.14 markus 1115: error("%s: fd0 %d != 0", __func__, fd0);
1.1 provos 1116:
1117: /* slave is not needed */
1118: close(s->ttyfd);
1119: s->ttyfd = s->ptyfd;
1120: /* no need to dup() because nobody closes ptyfd */
1121: s->ptymaster = s->ptyfd;
1122:
1.86 stevesk 1123: debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1.1 provos 1124:
1125: return (0);
1126:
1127: error:
1128: if (s != NULL)
1129: mm_session_close(s);
1130: buffer_put_int(m, 0);
1.59 avsm 1131: mm_request_send(sock, MONITOR_ANS_PTY, m);
1.1 provos 1132: return (0);
1133: }
1134:
1135: int
1.59 avsm 1136: mm_answer_pty_cleanup(int sock, Buffer *m)
1.1 provos 1137: {
1138: Session *s;
1139: char *tty;
1140:
1.14 markus 1141: debug3("%s entering", __func__);
1.1 provos 1142:
1143: tty = buffer_get_string(m, NULL);
1144: if ((s = session_by_tty(tty)) != NULL)
1145: mm_session_close(s);
1146: buffer_clear(m);
1147: xfree(tty);
1148: return (0);
1149: }
1150:
1151: int
1.59 avsm 1152: mm_answer_sesskey(int sock, Buffer *m)
1.1 provos 1153: {
1154: BIGNUM *p;
1155: int rsafail;
1156:
1157: /* Turn off permissions */
1.62 dtucker 1158: monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1.1 provos 1159:
1160: if ((p = BN_new()) == NULL)
1.14 markus 1161: fatal("%s: BN_new", __func__);
1.1 provos 1162:
1163: buffer_get_bignum2(m, p);
1164:
1165: rsafail = ssh1_session_key(p);
1166:
1167: buffer_clear(m);
1168: buffer_put_int(m, rsafail);
1169: buffer_put_bignum2(m, p);
1170:
1171: BN_clear_free(p);
1172:
1.59 avsm 1173: mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1.1 provos 1174:
1175: /* Turn on permissions for sessid passing */
1176: monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1177:
1178: return (0);
1179: }
1180:
1181: int
1.59 avsm 1182: mm_answer_sessid(int sock, Buffer *m)
1.1 provos 1183: {
1184: int i;
1185:
1.14 markus 1186: debug3("%s entering", __func__);
1.1 provos 1187:
1188: if (buffer_len(m) != 16)
1.14 markus 1189: fatal("%s: bad ssh1 session id", __func__);
1.1 provos 1190: for (i = 0; i < 16; i++)
1191: session_id[i] = buffer_get_char(m);
1192:
1193: /* Turn on permissions for getpwnam */
1194: monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1195:
1196: return (0);
1197: }
1198:
1199: int
1.59 avsm 1200: mm_answer_rsa_keyallowed(int sock, Buffer *m)
1.1 provos 1201: {
1202: BIGNUM *client_n;
1203: Key *key = NULL;
1.3 markus 1204: u_char *blob = NULL;
1205: u_int blen = 0;
1.1 provos 1206: int allowed = 0;
1207:
1.14 markus 1208: debug3("%s entering", __func__);
1.1 provos 1209:
1.77 dtucker 1210: auth_method = "rsa";
1.12 markus 1211: if (options.rsa_authentication && authctxt->valid) {
1.1 provos 1212: if ((client_n = BN_new()) == NULL)
1.14 markus 1213: fatal("%s: BN_new", __func__);
1.1 provos 1214: buffer_get_bignum2(m, client_n);
1215: allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1216: BN_clear_free(client_n);
1217: }
1218: buffer_clear(m);
1219: buffer_put_int(m, allowed);
1.32 markus 1220: buffer_put_int(m, forced_command != NULL);
1.1 provos 1221:
1222: /* clear temporarily storage (used by generate challenge) */
1223: monitor_reset_key_state();
1224:
1225: if (allowed && key != NULL) {
1226: key->type = KEY_RSA; /* cheat for key_to_blob */
1227: if (key_to_blob(key, &blob, &blen) == 0)
1.14 markus 1228: fatal("%s: key_to_blob failed", __func__);
1.1 provos 1229: buffer_put_string(m, blob, blen);
1230:
1231: /* Save temporarily for comparison in verify */
1232: key_blob = blob;
1233: key_bloblen = blen;
1234: key_blobtype = MM_RSAUSERKEY;
1.33 markus 1235: }
1236: if (key != NULL)
1.1 provos 1237: key_free(key);
1238:
1239: mm_append_debug(m);
1240:
1.59 avsm 1241: mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1.1 provos 1242:
1243: monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1244: monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1245: return (0);
1246: }
1247:
1248: int
1.59 avsm 1249: mm_answer_rsa_challenge(int sock, Buffer *m)
1.1 provos 1250: {
1251: Key *key = NULL;
1.3 markus 1252: u_char *blob;
1253: u_int blen;
1.1 provos 1254:
1.14 markus 1255: debug3("%s entering", __func__);
1.1 provos 1256:
1257: if (!authctxt->valid)
1.14 markus 1258: fatal("%s: authctxt not valid", __func__);
1.1 provos 1259: blob = buffer_get_string(m, &blen);
1260: if (!monitor_allowed_key(blob, blen))
1.14 markus 1261: fatal("%s: bad key, not previously allowed", __func__);
1.1 provos 1262: if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1.14 markus 1263: fatal("%s: key type mismatch", __func__);
1.1 provos 1264: if ((key = key_from_blob(blob, blen)) == NULL)
1.14 markus 1265: fatal("%s: received bad key", __func__);
1.1 provos 1266:
1267: if (ssh1_challenge)
1268: BN_clear_free(ssh1_challenge);
1269: ssh1_challenge = auth_rsa_generate_challenge(key);
1270:
1271: buffer_clear(m);
1272: buffer_put_bignum2(m, ssh1_challenge);
1273:
1.14 markus 1274: debug3("%s sending reply", __func__);
1.59 avsm 1275: mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1.1 provos 1276:
1277: monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1.33 markus 1278:
1279: xfree(blob);
1280: key_free(key);
1.1 provos 1281: return (0);
1282: }
1283:
1284: int
1.59 avsm 1285: mm_answer_rsa_response(int sock, Buffer *m)
1.1 provos 1286: {
1287: Key *key = NULL;
1.3 markus 1288: u_char *blob, *response;
1289: u_int blen, len;
1290: int success;
1.1 provos 1291:
1.14 markus 1292: debug3("%s entering", __func__);
1.1 provos 1293:
1294: if (!authctxt->valid)
1.14 markus 1295: fatal("%s: authctxt not valid", __func__);
1.1 provos 1296: if (ssh1_challenge == NULL)
1.14 markus 1297: fatal("%s: no ssh1_challenge", __func__);
1.1 provos 1298:
1299: blob = buffer_get_string(m, &blen);
1300: if (!monitor_allowed_key(blob, blen))
1.14 markus 1301: fatal("%s: bad key, not previously allowed", __func__);
1.1 provos 1302: if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1.14 markus 1303: fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1.1 provos 1304: if ((key = key_from_blob(blob, blen)) == NULL)
1.14 markus 1305: fatal("%s: received bad key", __func__);
1.1 provos 1306: response = buffer_get_string(m, &len);
1307: if (len != 16)
1.14 markus 1308: fatal("%s: received bad response to challenge", __func__);
1.1 provos 1309: success = auth_rsa_verify_response(key, ssh1_challenge, response);
1310:
1.33 markus 1311: xfree(blob);
1.1 provos 1312: key_free(key);
1313: xfree(response);
1314:
1315: auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1316:
1317: /* reset state */
1318: BN_clear_free(ssh1_challenge);
1319: ssh1_challenge = NULL;
1320: monitor_reset_key_state();
1321:
1322: buffer_clear(m);
1323: buffer_put_int(m, success);
1.59 avsm 1324: mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1.1 provos 1325:
1326: return (success);
1327: }
1328:
1329: int
1.59 avsm 1330: mm_answer_term(int sock, Buffer *req)
1.1 provos 1331: {
1.11 mouring 1332: extern struct monitor *pmonitor;
1.1 provos 1333: int res, status;
1334:
1.14 markus 1335: debug3("%s: tearing down sessions", __func__);
1.1 provos 1336:
1337: /* The child is terminating */
1338: session_destroy_all(&mm_session_close);
1339:
1.11 mouring 1340: while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1.9 markus 1341: if (errno != EINTR)
1342: exit(1);
1.1 provos 1343:
1344: res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1345:
1346: /* Terminate process */
1.57 deraadt 1347: exit(res);
1.1 provos 1348: }
1349:
1350: void
1.11 mouring 1351: monitor_apply_keystate(struct monitor *pmonitor)
1.1 provos 1352: {
1353: if (compat20) {
1354: set_newkeys(MODE_IN);
1355: set_newkeys(MODE_OUT);
1356: } else {
1357: packet_set_protocol_flags(child_state.ssh1protoflags);
1.15 markus 1358: packet_set_encryption_key(child_state.ssh1key,
1359: child_state.ssh1keylen, child_state.ssh1cipher);
1360: xfree(child_state.ssh1key);
1.1 provos 1361: }
1362:
1.15 markus 1363: /* for rc4 and other stateful ciphers */
1.1 provos 1364: packet_set_keycontext(MODE_OUT, child_state.keyout);
1365: xfree(child_state.keyout);
1366: packet_set_keycontext(MODE_IN, child_state.keyin);
1367: xfree(child_state.keyin);
1368:
1369: if (!compat20) {
1370: packet_set_iv(MODE_OUT, child_state.ivout);
1371: xfree(child_state.ivout);
1372: packet_set_iv(MODE_IN, child_state.ivin);
1373: xfree(child_state.ivin);
1374: }
1375:
1376: memcpy(&incoming_stream, &child_state.incoming,
1377: sizeof(incoming_stream));
1378: memcpy(&outgoing_stream, &child_state.outgoing,
1379: sizeof(outgoing_stream));
1.3 markus 1380:
1.1 provos 1381: /* Update with new address */
1.16 djm 1382: if (options.compression)
1383: mm_init_compression(pmonitor->m_zlib);
1.1 provos 1384:
1385: /* Network I/O buffers */
1386: /* XXX inefficient for large buffers, need: buffer_init_from_string */
1387: buffer_clear(&input);
1388: buffer_append(&input, child_state.input, child_state.ilen);
1389: memset(child_state.input, 0, child_state.ilen);
1390: xfree(child_state.input);
1391:
1392: buffer_clear(&output);
1393: buffer_append(&output, child_state.output, child_state.olen);
1394: memset(child_state.output, 0, child_state.olen);
1395: xfree(child_state.output);
1396: }
1397:
1.2 markus 1398: static Kex *
1.1 provos 1399: mm_get_kex(Buffer *m)
1400: {
1401: Kex *kex;
1402: void *blob;
1403: u_int bloblen;
1404:
1.75 djm 1405: kex = xcalloc(1, sizeof(*kex));
1.1 provos 1406: kex->session_id = buffer_get_string(m, &kex->session_id_len);
1.13 markus 1407: if ((session_id2 == NULL) ||
1408: (kex->session_id_len != session_id2_len) ||
1409: (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1410: fatal("mm_get_get: internal error: bad session id");
1.1 provos 1411: kex->we_need = buffer_get_int(m);
1.34 markus 1412: kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1.58 djm 1413: kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1.34 markus 1414: kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1.69 djm 1415: kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1.1 provos 1416: kex->server = 1;
1417: kex->hostkey_type = buffer_get_int(m);
1418: kex->kex_type = buffer_get_int(m);
1419: blob = buffer_get_string(m, &bloblen);
1420: buffer_init(&kex->my);
1421: buffer_append(&kex->my, blob, bloblen);
1422: xfree(blob);
1423: blob = buffer_get_string(m, &bloblen);
1424: buffer_init(&kex->peer);
1425: buffer_append(&kex->peer, blob, bloblen);
1426: xfree(blob);
1427: kex->done = 1;
1428: kex->flags = buffer_get_int(m);
1429: kex->client_version_string = buffer_get_string(m, NULL);
1430: kex->server_version_string = buffer_get_string(m, NULL);
1431: kex->load_host_key=&get_hostkey_by_type;
1432: kex->host_key_index=&get_hostkey_index;
1433:
1434: return (kex);
1435: }
1436:
1437: /* This function requries careful sanity checking */
1438:
1439: void
1.11 mouring 1440: mm_get_keystate(struct monitor *pmonitor)
1.1 provos 1441: {
1442: Buffer m;
1443: u_char *blob, *p;
1444: u_int bloblen, plen;
1.37 markus 1445: u_int32_t seqnr, packets;
1446: u_int64_t blocks;
1.1 provos 1447:
1.14 markus 1448: debug3("%s: Waiting for new keys", __func__);
1.1 provos 1449:
1450: buffer_init(&m);
1.11 mouring 1451: mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1.1 provos 1452: if (!compat20) {
1453: child_state.ssh1protoflags = buffer_get_int(&m);
1454: child_state.ssh1cipher = buffer_get_int(&m);
1.15 markus 1455: child_state.ssh1key = buffer_get_string(&m,
1456: &child_state.ssh1keylen);
1.1 provos 1457: child_state.ivout = buffer_get_string(&m,
1458: &child_state.ivoutlen);
1459: child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1460: goto skip;
1461: } else {
1462: /* Get the Kex for rekeying */
1.11 mouring 1463: *pmonitor->m_pkex = mm_get_kex(&m);
1.1 provos 1464: }
1465:
1466: blob = buffer_get_string(&m, &bloblen);
1467: current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1468: xfree(blob);
1469:
1.14 markus 1470: debug3("%s: Waiting for second key", __func__);
1.1 provos 1471: blob = buffer_get_string(&m, &bloblen);
1472: current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1473: xfree(blob);
1.3 markus 1474:
1.1 provos 1475: /* Now get sequence numbers for the packets */
1.37 markus 1476: seqnr = buffer_get_int(&m);
1477: blocks = buffer_get_int64(&m);
1478: packets = buffer_get_int(&m);
1479: packet_set_state(MODE_OUT, seqnr, blocks, packets);
1480: seqnr = buffer_get_int(&m);
1481: blocks = buffer_get_int64(&m);
1482: packets = buffer_get_int(&m);
1483: packet_set_state(MODE_IN, seqnr, blocks, packets);
1.1 provos 1484:
1485: skip:
1486: /* Get the key context */
1487: child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1488: child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1489:
1.14 markus 1490: debug3("%s: Getting compression state", __func__);
1.1 provos 1491: /* Get compression state */
1492: p = buffer_get_string(&m, &plen);
1493: if (plen != sizeof(child_state.outgoing))
1.14 markus 1494: fatal("%s: bad request size", __func__);
1.1 provos 1495: memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1496: xfree(p);
1497:
1498: p = buffer_get_string(&m, &plen);
1499: if (plen != sizeof(child_state.incoming))
1.14 markus 1500: fatal("%s: bad request size", __func__);
1.1 provos 1501: memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1502: xfree(p);
1503:
1504: /* Network I/O buffers */
1.14 markus 1505: debug3("%s: Getting Network I/O buffers", __func__);
1.1 provos 1506: child_state.input = buffer_get_string(&m, &child_state.ilen);
1507: child_state.output = buffer_get_string(&m, &child_state.olen);
1508:
1509: buffer_free(&m);
1510: }
1511:
1512:
1513: /* Allocation functions for zlib */
1514: void *
1515: mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1516: {
1.30 markus 1517: size_t len = (size_t) size * ncount;
1.1 provos 1518: void *address;
1519:
1.23 millert 1520: if (len == 0 || ncount > SIZE_T_MAX / size)
1.18 deraadt 1521: fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1522:
1523: address = mm_malloc(mm, len);
1.1 provos 1524:
1525: return (address);
1526: }
1527:
1528: void
1529: mm_zfree(struct mm_master *mm, void *address)
1530: {
1531: mm_free(mm, address);
1532: }
1533:
1534: void
1535: mm_init_compression(struct mm_master *mm)
1536: {
1537: outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1538: outgoing_stream.zfree = (free_func)mm_zfree;
1539: outgoing_stream.opaque = mm;
1540:
1541: incoming_stream.zalloc = (alloc_func)mm_zalloc;
1542: incoming_stream.zfree = (free_func)mm_zfree;
1543: incoming_stream.opaque = mm;
1544: }
1545:
1546: /* XXX */
1547:
1548: #define FD_CLOSEONEXEC(x) do { \
1549: if (fcntl(x, F_SETFD, 1) == -1) \
1550: fatal("fcntl(%d, F_SETFD)", x); \
1551: } while (0)
1552:
1.2 markus 1553: static void
1.1 provos 1554: monitor_socketpair(int *pair)
1.3 markus 1555: {
1.1 provos 1556: if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1.14 markus 1557: fatal("%s: socketpair", __func__);
1.1 provos 1558: FD_CLOSEONEXEC(pair[0]);
1559: FD_CLOSEONEXEC(pair[1]);
1560: }
1561:
1562: #define MM_MEMSIZE 65536
1563:
1564: struct monitor *
1565: monitor_init(void)
1566: {
1567: struct monitor *mon;
1568: int pair[2];
1569:
1.75 djm 1570: mon = xcalloc(1, sizeof(*mon));
1.1 provos 1571:
1572: monitor_socketpair(pair);
1573:
1574: mon->m_recvfd = pair[0];
1575: mon->m_sendfd = pair[1];
1576:
1577: /* Used to share zlib space across processes */
1.16 djm 1578: if (options.compression) {
1579: mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1580: mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1.1 provos 1581:
1.16 djm 1582: /* Compression needs to share state across borders */
1583: mm_init_compression(mon->m_zlib);
1584: }
1.1 provos 1585:
1586: return mon;
1587: }
1588:
1589: void
1590: monitor_reinit(struct monitor *mon)
1591: {
1592: int pair[2];
1593:
1594: monitor_socketpair(pair);
1595:
1596: mon->m_recvfd = pair[0];
1597: mon->m_sendfd = pair[1];
1598: }
1.46 markus 1599:
1600: #ifdef GSSAPI
1601: int
1.59 avsm 1602: mm_answer_gss_setup_ctx(int sock, Buffer *m)
1.46 markus 1603: {
1.59 avsm 1604: gss_OID_desc goid;
1.46 markus 1605: OM_uint32 major;
1606: u_int len;
1607:
1.59 avsm 1608: goid.elements = buffer_get_string(m, &len);
1609: goid.length = len;
1.46 markus 1610:
1.59 avsm 1611: major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1.46 markus 1612:
1.59 avsm 1613: xfree(goid.elements);
1.46 markus 1614:
1615: buffer_clear(m);
1616: buffer_put_int(m, major);
1617:
1.64 stevesk 1618: mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1.46 markus 1619:
1620: /* Now we have a context, enable the step */
1621: monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1622:
1623: return (0);
1624: }
1625:
1626: int
1.59 avsm 1627: mm_answer_gss_accept_ctx(int sock, Buffer *m)
1.46 markus 1628: {
1629: gss_buffer_desc in;
1630: gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1.64 stevesk 1631: OM_uint32 major, minor;
1.46 markus 1632: OM_uint32 flags = 0; /* GSI needs this */
1.47 deraadt 1633: u_int len;
1.46 markus 1634:
1.47 deraadt 1635: in.value = buffer_get_string(m, &len);
1636: in.length = len;
1.46 markus 1637: major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1638: xfree(in.value);
1639:
1640: buffer_clear(m);
1641: buffer_put_int(m, major);
1642: buffer_put_string(m, out.value, out.length);
1643: buffer_put_int(m, flags);
1.59 avsm 1644: mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1.46 markus 1645:
1646: gss_release_buffer(&minor, &out);
1647:
1.64 stevesk 1648: if (major == GSS_S_COMPLETE) {
1.46 markus 1649: monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1650: monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1.52 markus 1651: monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1.46 markus 1652: }
1653: return (0);
1654: }
1655:
1656: int
1.59 avsm 1657: mm_answer_gss_checkmic(int sock, Buffer *m)
1.52 markus 1658: {
1659: gss_buffer_desc gssbuf, mic;
1660: OM_uint32 ret;
1661: u_int len;
1.54 djm 1662:
1.52 markus 1663: gssbuf.value = buffer_get_string(m, &len);
1664: gssbuf.length = len;
1665: mic.value = buffer_get_string(m, &len);
1666: mic.length = len;
1.54 djm 1667:
1.52 markus 1668: ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1.54 djm 1669:
1.52 markus 1670: xfree(gssbuf.value);
1671: xfree(mic.value);
1.54 djm 1672:
1.52 markus 1673: buffer_clear(m);
1674: buffer_put_int(m, ret);
1.54 djm 1675:
1.59 avsm 1676: mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1.54 djm 1677:
1.52 markus 1678: if (!GSS_ERROR(ret))
1679: monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1.54 djm 1680:
1.52 markus 1681: return (0);
1682: }
1683:
1684: int
1.59 avsm 1685: mm_answer_gss_userok(int sock, Buffer *m)
1.46 markus 1686: {
1687: int authenticated;
1688:
1689: authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1690:
1691: buffer_clear(m);
1692: buffer_put_int(m, authenticated);
1693:
1694: debug3("%s: sending result %d", __func__, authenticated);
1.59 avsm 1695: mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1.46 markus 1696:
1.64 stevesk 1697: auth_method = "gssapi-with-mic";
1.46 markus 1698:
1699: /* Monitor loop will terminate if authenticated */
1700: return (authenticated);
1701: }
1702: #endif /* GSSAPI */