Annotation of src/usr.bin/ssh/monitor.c, Revision 1.118
1.118 ! djm 1: /* $OpenBSD: monitor.c,v 1.117 2012/06/22 12:30:26 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.95 djm 33: #include <sys/queue.h>
1.1 provos 34:
1.78 stevesk 35: #include <openssl/dh.h>
36:
1.81 stevesk 37: #include <errno.h>
1.80 stevesk 38: #include <fcntl.h>
1.65 stevesk 39: #include <paths.h>
1.114 djm 40: #include <poll.h>
1.78 stevesk 41: #include <pwd.h>
1.68 stevesk 42: #include <signal.h>
1.84 stevesk 43: #include <stdlib.h>
1.82 stevesk 44: #include <string.h>
1.1 provos 45:
1.114 djm 46: #include "atomicio.h"
1.85 deraadt 47: #include "xmalloc.h"
1.1 provos 48: #include "ssh.h"
1.85 deraadt 49: #include "key.h"
50: #include "buffer.h"
51: #include "hostfile.h"
1.1 provos 52: #include "auth.h"
1.85 deraadt 53: #include "cipher.h"
1.1 provos 54: #include "kex.h"
55: #include "dh.h"
1.88 miod 56: #include <zlib.h>
1.1 provos 57: #include "packet.h"
58: #include "auth-options.h"
59: #include "sshpty.h"
60: #include "channels.h"
61: #include "session.h"
62: #include "sshlogin.h"
63: #include "canohost.h"
64: #include "log.h"
65: #include "servconf.h"
66: #include "monitor.h"
67: #include "monitor_mm.h"
1.85 deraadt 68: #ifdef GSSAPI
69: #include "ssh-gss.h"
70: #endif
1.1 provos 71: #include "monitor_wrap.h"
72: #include "monitor_fdpass.h"
73: #include "misc.h"
74: #include "compat.h"
75: #include "ssh2.h"
1.100 djm 76: #include "jpake.h"
1.104 andreas 77: #include "roaming.h"
1.1 provos 78:
1.46 markus 79: #ifdef GSSAPI
80: static Gssctxt *gsscontext = NULL;
81: #endif
82:
1.1 provos 83: /* Imports */
84: extern ServerOptions options;
85: extern u_int utmp_len;
86: extern Newkeys *current_keys[];
87: extern z_stream incoming_stream;
88: extern z_stream outgoing_stream;
89: extern u_char session_id[];
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;
1.103 andreas 115: u_int64_t sent_bytes;
116: u_int64_t recv_bytes;
1.1 provos 117: } child_state;
118:
1.43 markus 119: /* Functions on the monitor that answer unprivileged requests */
1.1 provos 120:
121: int mm_answer_moduli(int, Buffer *);
122: int mm_answer_sign(int, Buffer *);
123: int mm_answer_pwnamallow(int, Buffer *);
1.10 djm 124: int mm_answer_auth2_read_banner(int, Buffer *);
1.1 provos 125: int mm_answer_authserv(int, Buffer *);
126: int mm_answer_authpassword(int, Buffer *);
127: int mm_answer_bsdauthquery(int, Buffer *);
128: int mm_answer_bsdauthrespond(int, Buffer *);
129: int mm_answer_skeyquery(int, Buffer *);
130: int mm_answer_skeyrespond(int, Buffer *);
131: int mm_answer_keyallowed(int, Buffer *);
132: int mm_answer_keyverify(int, Buffer *);
133: int mm_answer_pty(int, Buffer *);
134: int mm_answer_pty_cleanup(int, Buffer *);
135: int mm_answer_term(int, Buffer *);
136: int mm_answer_rsa_keyallowed(int, Buffer *);
137: int mm_answer_rsa_challenge(int, Buffer *);
138: int mm_answer_rsa_response(int, Buffer *);
139: int mm_answer_sesskey(int, Buffer *);
140: int mm_answer_sessid(int, Buffer *);
1.100 djm 141: int mm_answer_jpake_get_pwdata(int, Buffer *);
142: int mm_answer_jpake_step1(int, Buffer *);
143: int mm_answer_jpake_step2(int, Buffer *);
144: int mm_answer_jpake_key_confirm(int, Buffer *);
145: int mm_answer_jpake_check_confirm(int, Buffer *);
1.1 provos 146:
1.46 markus 147: #ifdef GSSAPI
148: int mm_answer_gss_setup_ctx(int, Buffer *);
149: int mm_answer_gss_accept_ctx(int, Buffer *);
150: int mm_answer_gss_userok(int, Buffer *);
1.52 markus 151: int mm_answer_gss_checkmic(int, Buffer *);
1.46 markus 152: #endif
1.25 itojun 153:
1.114 djm 154: static int monitor_read_log(struct monitor *);
155:
1.1 provos 156: static Authctxt *authctxt;
157: static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
158:
159: /* local state for key verify */
160: static u_char *key_blob = NULL;
161: static u_int key_bloblen = 0;
162: static int key_blobtype = MM_NOKEY;
1.26 markus 163: static char *hostbased_cuser = NULL;
164: static char *hostbased_chost = NULL;
1.1 provos 165: static char *auth_method = "unknown";
1.44 markus 166: static u_int session_id2_len = 0;
1.13 markus 167: static u_char *session_id2 = NULL;
1.40 markus 168: static pid_t monitor_child_pid;
1.1 provos 169:
170: struct mon_table {
171: enum monitor_reqtype type;
172: int flags;
173: int (*f)(int, Buffer *);
174: };
175:
176: #define MON_ISAUTH 0x0004 /* Required for Authentication */
177: #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
178: #define MON_ONCE 0x0010 /* Disable after calling */
1.77 dtucker 179: #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
1.1 provos 180:
181: #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
182:
183: #define MON_PERMIT 0x1000 /* Request is permitted */
184:
185: struct mon_table mon_dispatch_proto20[] = {
186: {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
187: {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
188: {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
189: {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
1.10 djm 190: {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
1.1 provos 191: {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
192: {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
1.66 stevesk 193: {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
1.1 provos 194: {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
195: {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
1.46 markus 196: #ifdef GSSAPI
197: {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
198: {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
199: {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
1.52 markus 200: {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
1.46 markus 201: #endif
1.100 djm 202: #ifdef JPAKE
203: {MONITOR_REQ_JPAKE_GET_PWDATA, MON_ONCE, mm_answer_jpake_get_pwdata},
204: {MONITOR_REQ_JPAKE_STEP1, MON_ISAUTH, mm_answer_jpake_step1},
205: {MONITOR_REQ_JPAKE_STEP2, MON_ONCE, mm_answer_jpake_step2},
206: {MONITOR_REQ_JPAKE_KEY_CONFIRM, MON_ONCE, mm_answer_jpake_key_confirm},
207: {MONITOR_REQ_JPAKE_CHECK_CONFIRM, MON_AUTH, mm_answer_jpake_check_confirm},
208: #endif
1.1 provos 209: {0, 0, NULL}
210: };
211:
212: struct mon_table mon_dispatch_postauth20[] = {
213: {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
214: {MONITOR_REQ_SIGN, 0, mm_answer_sign},
215: {MONITOR_REQ_PTY, 0, mm_answer_pty},
216: {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
217: {MONITOR_REQ_TERM, 0, mm_answer_term},
218: {0, 0, NULL}
219: };
220:
221: struct mon_table mon_dispatch_proto15[] = {
222: {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
223: {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
224: {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
225: {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
1.77 dtucker 226: {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
227: {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
1.1 provos 228: {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
229: {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
230: {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
1.66 stevesk 231: {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
1.1 provos 232: {0, 0, NULL}
233: };
234:
235: struct mon_table mon_dispatch_postauth15[] = {
236: {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
237: {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
238: {MONITOR_REQ_TERM, 0, mm_answer_term},
239: {0, 0, NULL}
240: };
241:
242: struct mon_table *mon_dispatch;
243:
244: /* Specifies if a certain message is allowed at the moment */
245:
246: static void
247: monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
248: {
249: while (ent->f != NULL) {
250: if (ent->type == type) {
251: ent->flags &= ~MON_PERMIT;
252: ent->flags |= permit ? MON_PERMIT : 0;
253: return;
254: }
255: ent++;
256: }
257: }
258:
259: static void
260: monitor_permit_authentications(int permit)
261: {
262: struct mon_table *ent = mon_dispatch;
263:
264: while (ent->f != NULL) {
265: if (ent->flags & MON_AUTH) {
266: ent->flags &= ~MON_PERMIT;
267: ent->flags |= permit ? MON_PERMIT : 0;
268: }
269: ent++;
270: }
271: }
272:
1.50 markus 273: void
274: monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
1.1 provos 275: {
276: struct mon_table *ent;
277: int authenticated = 0;
278:
279: debug3("preauth child monitor started");
280:
1.114 djm 281: close(pmonitor->m_recvfd);
282: close(pmonitor->m_log_sendfd);
283: pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
284:
1.50 markus 285: authctxt = _authctxt;
286: memset(authctxt, 0, sizeof(*authctxt));
287:
1.1 provos 288: if (compat20) {
289: mon_dispatch = mon_dispatch_proto20;
290:
291: /* Permit requests for moduli and signatures */
292: monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
293: monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
294: } else {
295: mon_dispatch = mon_dispatch_proto15;
296:
297: monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
298: }
299:
300: /* The first few requests do not require asynchronous access */
301: while (!authenticated) {
1.77 dtucker 302: auth_method = "unknown";
1.89 markus 303: authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
1.118 ! djm 304:
! 305: /* Special handling for multiple required authentications */
! 306: if (options.num_auth_methods != 0) {
! 307: if (!compat20)
! 308: fatal("AuthenticationMethods is not supported"
! 309: "with SSH protocol 1");
! 310: if (authenticated &&
! 311: !auth2_update_methods_lists(authctxt,
! 312: auth_method)) {
! 313: debug3("%s: method %s: partial", __func__,
! 314: auth_method);
! 315: authenticated = 0;
! 316: }
! 317: }
! 318:
1.1 provos 319: if (authenticated) {
320: if (!(ent->flags & MON_AUTHDECIDE))
321: fatal("%s: unexpected authentication from %d",
1.14 markus 322: __func__, ent->type);
1.1 provos 323: if (authctxt->pw->pw_uid == 0 &&
324: !auth_root_allowed(auth_method))
325: authenticated = 0;
326: }
1.77 dtucker 327: if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
1.1 provos 328: auth_log(authctxt, authenticated, auth_method,
329: compat20 ? " ssh2" : "");
330: if (!authenticated)
331: authctxt->failures++;
332: }
1.100 djm 333: #ifdef JPAKE
334: /* Cleanup JPAKE context after authentication */
335: if (ent->flags & MON_AUTHDECIDE) {
336: if (authctxt->jpake_ctx != NULL) {
337: jpake_free(authctxt->jpake_ctx);
338: authctxt->jpake_ctx = NULL;
339: }
340: }
341: #endif
1.1 provos 342: }
343:
1.114 djm 344: /* Drain any buffered messages from the child */
345: while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
346: ;
347:
1.1 provos 348: if (!authctxt->valid)
1.14 markus 349: fatal("%s: authenticated invalid user", __func__);
1.77 dtucker 350: if (strcmp(auth_method, "unknown") == 0)
351: fatal("%s: authentication method name unknown", __func__);
1.1 provos 352:
353: debug("%s: %s has been authenticated by privileged process",
1.14 markus 354: __func__, authctxt->user);
1.1 provos 355:
1.11 mouring 356: mm_get_keystate(pmonitor);
1.114 djm 357:
358: close(pmonitor->m_sendfd);
359: close(pmonitor->m_log_recvfd);
360: pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
1.1 provos 361: }
362:
1.40 markus 363: static void
364: monitor_set_child_handler(pid_t pid)
365: {
366: monitor_child_pid = pid;
367: }
368:
369: static void
1.59 avsm 370: monitor_child_handler(int sig)
1.40 markus 371: {
1.59 avsm 372: kill(monitor_child_pid, sig);
1.40 markus 373: }
374:
1.1 provos 375: void
1.11 mouring 376: monitor_child_postauth(struct monitor *pmonitor)
1.1 provos 377: {
1.114 djm 378: close(pmonitor->m_recvfd);
379: pmonitor->m_recvfd = -1;
380:
1.40 markus 381: monitor_set_child_handler(pmonitor->m_pid);
382: signal(SIGHUP, &monitor_child_handler);
383: signal(SIGTERM, &monitor_child_handler);
1.91 djm 384: signal(SIGINT, &monitor_child_handler);
1.40 markus 385:
1.1 provos 386: if (compat20) {
387: mon_dispatch = mon_dispatch_postauth20;
388:
389: /* Permit requests for moduli and signatures */
390: monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
391: monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
392: monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
393: } else {
394: mon_dispatch = mon_dispatch_postauth15;
395: monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
396: }
397: if (!no_pty_flag) {
398: monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
399: monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
400: }
401:
402: for (;;)
1.11 mouring 403: monitor_read(pmonitor, mon_dispatch, NULL);
1.1 provos 404: }
405:
406: void
1.11 mouring 407: monitor_sync(struct monitor *pmonitor)
1.1 provos 408: {
1.16 djm 409: if (options.compression) {
410: /* The member allocation is not visible, so sync it */
411: mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
412: }
1.1 provos 413: }
414:
1.114 djm 415: static int
416: monitor_read_log(struct monitor *pmonitor)
417: {
418: Buffer logmsg;
419: u_int len, level;
420: char *msg;
421:
422: buffer_init(&logmsg);
423:
424: /* Read length */
425: buffer_append_space(&logmsg, 4);
426: if (atomicio(read, pmonitor->m_log_recvfd,
427: buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
428: if (errno == EPIPE) {
1.116 djm 429: buffer_free(&logmsg);
1.114 djm 430: debug("%s: child log fd closed", __func__);
431: close(pmonitor->m_log_recvfd);
432: pmonitor->m_log_recvfd = -1;
433: return -1;
434: }
435: fatal("%s: log fd read: %s", __func__, strerror(errno));
436: }
437: len = buffer_get_int(&logmsg);
438: if (len <= 4 || len > 8192)
439: fatal("%s: invalid log message length %u", __func__, len);
440:
441: /* Read severity, message */
442: buffer_clear(&logmsg);
443: buffer_append_space(&logmsg, len);
444: if (atomicio(read, pmonitor->m_log_recvfd,
445: buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
446: fatal("%s: log fd read: %s", __func__, strerror(errno));
447:
448: /* Log it */
449: level = buffer_get_int(&logmsg);
450: msg = buffer_get_string(&logmsg, NULL);
451: if (log_level_name(level) == NULL)
452: fatal("%s: invalid log level %u (corrupted message?)",
453: __func__, level);
454: do_log2(level, "%s [preauth]", msg);
455:
456: buffer_free(&logmsg);
457: xfree(msg);
458:
459: return 0;
460: }
461:
1.1 provos 462: int
1.11 mouring 463: monitor_read(struct monitor *pmonitor, struct mon_table *ent,
1.1 provos 464: struct mon_table **pent)
465: {
466: Buffer m;
467: int ret;
468: u_char type;
1.114 djm 469: struct pollfd pfd[2];
470:
471: for (;;) {
472: bzero(&pfd, sizeof(pfd));
473: pfd[0].fd = pmonitor->m_sendfd;
474: pfd[0].events = POLLIN;
475: pfd[1].fd = pmonitor->m_log_recvfd;
476: pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
1.115 djm 477: if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
478: if (errno == EINTR || errno == EAGAIN)
479: continue;
1.114 djm 480: fatal("%s: poll: %s", __func__, strerror(errno));
1.115 djm 481: }
1.114 djm 482: if (pfd[1].revents) {
483: /*
484: * Drain all log messages before processing next
485: * monitor request.
486: */
487: monitor_read_log(pmonitor);
488: continue;
489: }
490: if (pfd[0].revents)
491: break; /* Continues below */
492: }
1.1 provos 493:
494: buffer_init(&m);
495:
1.11 mouring 496: mm_request_receive(pmonitor->m_sendfd, &m);
1.1 provos 497: type = buffer_get_char(&m);
498:
1.14 markus 499: debug3("%s: checking request %d", __func__, type);
1.1 provos 500:
501: while (ent->f != NULL) {
502: if (ent->type == type)
503: break;
504: ent++;
505: }
506:
507: if (ent->f != NULL) {
508: if (!(ent->flags & MON_PERMIT))
1.14 markus 509: fatal("%s: unpermitted request %d", __func__,
1.1 provos 510: type);
1.11 mouring 511: ret = (*ent->f)(pmonitor->m_sendfd, &m);
1.1 provos 512: buffer_free(&m);
513:
514: /* The child may use this request only once, disable it */
515: if (ent->flags & MON_ONCE) {
1.14 markus 516: debug2("%s: %d used once, disabling now", __func__,
1.1 provos 517: type);
518: ent->flags &= ~MON_PERMIT;
519: }
520:
521: if (pent != NULL)
522: *pent = ent;
1.3 markus 523:
1.1 provos 524: return ret;
525: }
526:
1.14 markus 527: fatal("%s: unsupported request: %d", __func__, type);
1.1 provos 528:
529: /* NOTREACHED */
530: return (-1);
531: }
532:
533: /* allowed key state */
534: static int
535: monitor_allowed_key(u_char *blob, u_int bloblen)
536: {
537: /* make sure key is allowed */
538: if (key_blob == NULL || key_bloblen != bloblen ||
1.108 djm 539: timingsafe_bcmp(key_blob, blob, key_bloblen))
1.1 provos 540: return (0);
541: return (1);
542: }
543:
544: static void
545: monitor_reset_key_state(void)
546: {
547: /* reset state */
548: if (key_blob != NULL)
549: xfree(key_blob);
550: if (hostbased_cuser != NULL)
551: xfree(hostbased_cuser);
552: if (hostbased_chost != NULL)
553: xfree(hostbased_chost);
554: key_blob = NULL;
555: key_bloblen = 0;
556: key_blobtype = MM_NOKEY;
557: hostbased_cuser = NULL;
558: hostbased_chost = NULL;
559: }
560:
561: int
1.59 avsm 562: mm_answer_moduli(int sock, Buffer *m)
1.1 provos 563: {
564: DH *dh;
565: int min, want, max;
566:
567: min = buffer_get_int(m);
568: want = buffer_get_int(m);
569: max = buffer_get_int(m);
570:
571: debug3("%s: got parameters: %d %d %d",
1.14 markus 572: __func__, min, want, max);
1.1 provos 573: /* We need to check here, too, in case the child got corrupted */
574: if (max < min || want < min || max < want)
575: fatal("%s: bad parameters: %d %d %d",
1.14 markus 576: __func__, min, want, max);
1.1 provos 577:
578: buffer_clear(m);
579:
580: dh = choose_dh(min, want, max);
581: if (dh == NULL) {
582: buffer_put_char(m, 0);
583: return (0);
584: } else {
585: /* Send first bignum */
586: buffer_put_char(m, 1);
587: buffer_put_bignum2(m, dh->p);
588: buffer_put_bignum2(m, dh->g);
1.3 markus 589:
1.1 provos 590: DH_free(dh);
591: }
1.59 avsm 592: mm_request_send(sock, MONITOR_ANS_MODULI, m);
1.1 provos 593: return (0);
594: }
595:
596: int
1.59 avsm 597: mm_answer_sign(int sock, Buffer *m)
1.1 provos 598: {
599: Key *key;
600: u_char *p;
601: u_char *signature;
602: u_int siglen, datlen;
603: int keyid;
1.3 markus 604:
1.14 markus 605: debug3("%s", __func__);
1.1 provos 606:
1.3 markus 607: keyid = buffer_get_int(m);
608: p = buffer_get_string(m, &datlen);
1.1 provos 609:
1.69 djm 610: /*
1.110 djm 611: * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
612: * SHA384 (48 bytes) and SHA512 (64 bytes).
1.69 djm 613: */
1.110 djm 614: if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64)
1.19 deraadt 615: fatal("%s: data length incorrect: %u", __func__, datlen);
1.1 provos 616:
1.13 markus 617: /* save session id, it will be passed on the first call */
618: if (session_id2_len == 0) {
619: session_id2_len = datlen;
620: session_id2 = xmalloc(session_id2_len);
621: memcpy(session_id2, p, session_id2_len);
622: }
623:
1.1 provos 624: if ((key = get_hostkey_by_index(keyid)) == NULL)
1.14 markus 625: fatal("%s: no hostkey from index %d", __func__, keyid);
1.1 provos 626: if (key_sign(key, &signature, &siglen, p, datlen) < 0)
1.14 markus 627: fatal("%s: key_sign failed", __func__);
1.1 provos 628:
1.19 deraadt 629: debug3("%s: signature %p(%u)", __func__, signature, siglen);
1.1 provos 630:
631: buffer_clear(m);
632: buffer_put_string(m, signature, siglen);
633:
634: xfree(p);
635: xfree(signature);
636:
1.59 avsm 637: mm_request_send(sock, MONITOR_ANS_SIGN, m);
1.1 provos 638:
639: /* Turn on permissions for getpwnam */
640: monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
641:
642: return (0);
643: }
644:
645: /* Retrieves the password entry and also checks if the user is permitted */
646:
647: int
1.59 avsm 648: mm_answer_pwnamallow(int sock, Buffer *m)
1.1 provos 649: {
1.60 dtucker 650: char *username;
1.1 provos 651: struct passwd *pwent;
652: int allowed = 0;
1.113 djm 653: u_int i;
1.3 markus 654:
1.14 markus 655: debug3("%s", __func__);
1.1 provos 656:
657: if (authctxt->attempt++ != 0)
1.14 markus 658: fatal("%s: multiple attempts for getpwnam", __func__);
1.1 provos 659:
1.60 dtucker 660: username = buffer_get_string(m, NULL);
1.1 provos 661:
1.60 dtucker 662: pwent = getpwnamallow(username);
1.1 provos 663:
1.60 dtucker 664: authctxt->user = xstrdup(username);
665: setproctitle("%s [priv]", pwent ? username : "unknown");
666: xfree(username);
1.1 provos 667:
668: buffer_clear(m);
669:
670: if (pwent == NULL) {
671: buffer_put_char(m, 0);
1.53 djm 672: authctxt->pw = fakepw();
1.1 provos 673: goto out;
674: }
675:
676: allowed = 1;
1.4 markus 677: authctxt->pw = pwent;
1.1 provos 678: authctxt->valid = 1;
679:
680: buffer_put_char(m, 1);
681: buffer_put_string(m, pwent, sizeof(struct passwd));
682: buffer_put_cstring(m, pwent->pw_name);
683: buffer_put_cstring(m, "*");
684: buffer_put_cstring(m, pwent->pw_gecos);
685: buffer_put_cstring(m, pwent->pw_class);
686: buffer_put_cstring(m, pwent->pw_dir);
687: buffer_put_cstring(m, pwent->pw_shell);
1.94 dtucker 688:
689: out:
1.90 dtucker 690: buffer_put_string(m, &options, sizeof(options));
1.112 djm 691:
692: #define M_CP_STROPT(x) do { \
693: if (options.x != NULL) \
694: buffer_put_cstring(m, options.x); \
695: } while (0)
1.113 djm 696: #define M_CP_STRARRAYOPT(x, nx) do { \
697: for (i = 0; i < options.nx; i++) \
698: buffer_put_cstring(m, options.x[i]); \
699: } while (0)
1.112 djm 700: /* See comment in servconf.h */
701: COPY_MATCH_STRING_OPTS();
702: #undef M_CP_STROPT
1.113 djm 703: #undef M_CP_STRARRAYOPT
1.118 ! djm 704:
! 705: /* Create valid auth method lists */
! 706: if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
! 707: /*
! 708: * The monitor will continue long enough to let the child
! 709: * run to it's packet_disconnect(), but it must not allow any
! 710: * authentication to succeed.
! 711: */
! 712: debug("%s: no valid authentication method lists", __func__);
! 713: }
! 714:
1.14 markus 715: debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
1.59 avsm 716: mm_request_send(sock, MONITOR_ANS_PWNAM, m);
1.1 provos 717:
718: /* For SSHv1 allow authentication now */
719: if (!compat20)
720: monitor_permit_authentications(1);
1.10 djm 721: else {
1.1 provos 722: /* Allow service/style information on the auth context */
723: monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
1.10 djm 724: monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
725: }
726:
727: return (0);
728: }
729:
1.59 avsm 730: int mm_answer_auth2_read_banner(int sock, Buffer *m)
1.10 djm 731: {
732: char *banner;
733:
734: buffer_clear(m);
735: banner = auth2_read_banner();
736: buffer_put_cstring(m, banner != NULL ? banner : "");
1.59 avsm 737: mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
1.1 provos 738:
1.10 djm 739: if (banner != NULL)
1.21 deraadt 740: xfree(banner);
1.1 provos 741:
742: return (0);
743: }
744:
745: int
1.59 avsm 746: mm_answer_authserv(int sock, Buffer *m)
1.1 provos 747: {
748: monitor_permit_authentications(1);
749:
750: authctxt->service = buffer_get_string(m, NULL);
751: authctxt->style = buffer_get_string(m, NULL);
1.6 stevesk 752: debug3("%s: service=%s, style=%s",
1.14 markus 753: __func__, authctxt->service, authctxt->style);
1.6 stevesk 754:
1.1 provos 755: if (strlen(authctxt->style) == 0) {
756: xfree(authctxt->style);
757: authctxt->style = NULL;
758: }
759:
760: return (0);
761: }
762:
763: int
1.59 avsm 764: mm_answer_authpassword(int sock, Buffer *m)
1.1 provos 765: {
766: static int call_count;
767: char *passwd;
1.22 stevesk 768: int authenticated;
769: u_int plen;
1.1 provos 770:
771: passwd = buffer_get_string(m, &plen);
772: /* Only authenticate if the context is valid */
1.12 markus 773: authenticated = options.password_authentication &&
1.48 markus 774: auth_password(authctxt, passwd);
1.1 provos 775: memset(passwd, 0, strlen(passwd));
776: xfree(passwd);
777:
778: buffer_clear(m);
779: buffer_put_int(m, authenticated);
780:
1.14 markus 781: debug3("%s: sending result %d", __func__, authenticated);
1.59 avsm 782: mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
1.1 provos 783:
784: call_count++;
785: if (plen == 0 && call_count == 1)
786: auth_method = "none";
787: else
788: auth_method = "password";
789:
790: /* Causes monitor loop to terminate if authenticated */
791: return (authenticated);
792: }
793:
794: int
1.59 avsm 795: mm_answer_bsdauthquery(int sock, Buffer *m)
1.1 provos 796: {
797: char *name, *infotxt;
798: u_int numprompts;
799: u_int *echo_on;
800: char **prompts;
1.31 markus 801: u_int success;
1.1 provos 802:
1.31 markus 803: success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
804: &prompts, &echo_on) < 0 ? 0 : 1;
1.1 provos 805:
806: buffer_clear(m);
1.31 markus 807: buffer_put_int(m, success);
808: if (success)
1.1 provos 809: buffer_put_cstring(m, prompts[0]);
810:
1.31 markus 811: debug3("%s: sending challenge success: %u", __func__, success);
1.59 avsm 812: mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
1.1 provos 813:
1.31 markus 814: if (success) {
1.1 provos 815: xfree(name);
816: xfree(infotxt);
817: xfree(prompts);
818: xfree(echo_on);
819: }
820:
821: return (0);
822: }
823:
824: int
1.59 avsm 825: mm_answer_bsdauthrespond(int sock, Buffer *m)
1.1 provos 826: {
827: char *response;
828: int authok;
829:
830: if (authctxt->as == 0)
1.14 markus 831: fatal("%s: no bsd auth session", __func__);
1.1 provos 832:
833: response = buffer_get_string(m, NULL);
1.12 markus 834: authok = options.challenge_response_authentication &&
835: auth_userresponse(authctxt->as, response, 0);
1.1 provos 836: authctxt->as = NULL;
1.14 markus 837: debug3("%s: <%s> = <%d>", __func__, response, authok);
1.1 provos 838: xfree(response);
839:
840: buffer_clear(m);
841: buffer_put_int(m, authok);
842:
1.14 markus 843: debug3("%s: sending authenticated: %d", __func__, authok);
1.59 avsm 844: mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
1.1 provos 845:
1.118 ! djm 846: if (compat20)
! 847: auth_method = "keyboard-interactive";
! 848: else
! 849: auth_method = "bsdauth";
1.1 provos 850:
851: return (authok != 0);
852: }
853:
854: int
1.59 avsm 855: mm_answer_keyallowed(int sock, Buffer *m)
1.1 provos 856: {
857: Key *key;
1.26 markus 858: char *cuser, *chost;
859: u_char *blob;
1.1 provos 860: u_int bloblen;
861: enum mm_keytype type = 0;
862: int allowed = 0;
863:
1.14 markus 864: debug3("%s entering", __func__);
1.3 markus 865:
1.1 provos 866: type = buffer_get_int(m);
867: cuser = buffer_get_string(m, NULL);
868: chost = buffer_get_string(m, NULL);
869: blob = buffer_get_string(m, &bloblen);
870:
871: key = key_from_blob(blob, bloblen);
872:
873: if ((compat20 && type == MM_RSAHOSTKEY) ||
874: (!compat20 && type != MM_RSAHOSTKEY))
1.14 markus 875: fatal("%s: key type and protocol mismatch", __func__);
1.1 provos 876:
1.14 markus 877: debug3("%s: key_from_blob: %p", __func__, key);
1.1 provos 878:
1.51 djm 879: if (key != NULL && authctxt->valid) {
1.63 deraadt 880: switch (type) {
1.1 provos 881: case MM_USERKEY:
1.12 markus 882: allowed = options.pubkey_authentication &&
883: user_key_allowed(authctxt->pw, key);
1.77 dtucker 884: auth_method = "publickey";
1.97 dtucker 885: if (options.pubkey_authentication && allowed != 1)
886: auth_clear_options();
1.1 provos 887: break;
888: case MM_HOSTKEY:
1.12 markus 889: allowed = options.hostbased_authentication &&
890: hostbased_key_allowed(authctxt->pw,
1.1 provos 891: cuser, chost, key);
1.77 dtucker 892: auth_method = "hostbased";
1.1 provos 893: break;
894: case MM_RSAHOSTKEY:
895: key->type = KEY_RSA1; /* XXX */
1.12 markus 896: allowed = options.rhosts_rsa_authentication &&
897: auth_rhosts_rsa_key_allowed(authctxt->pw,
1.1 provos 898: cuser, chost, key);
1.97 dtucker 899: if (options.rhosts_rsa_authentication && allowed != 1)
900: auth_clear_options();
1.77 dtucker 901: auth_method = "rsa";
1.1 provos 902: break;
903: default:
1.14 markus 904: fatal("%s: unknown key type %d", __func__, type);
1.1 provos 905: break;
906: }
1.33 markus 907: }
908: if (key != NULL)
1.1 provos 909: key_free(key);
910:
911: /* clear temporarily storage (used by verify) */
912: monitor_reset_key_state();
913:
914: if (allowed) {
915: /* Save temporarily for comparison in verify */
916: key_blob = blob;
917: key_bloblen = bloblen;
918: key_blobtype = type;
919: hostbased_cuser = cuser;
920: hostbased_chost = chost;
1.72 djm 921: } else {
1.77 dtucker 922: /* Log failed attempt */
923: auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1.72 djm 924: xfree(blob);
925: xfree(cuser);
926: xfree(chost);
1.1 provos 927: }
928:
929: debug3("%s: key %p is %s",
1.98 dtucker 930: __func__, key, allowed ? "allowed" : "not allowed");
1.1 provos 931:
932: buffer_clear(m);
933: buffer_put_int(m, allowed);
1.32 markus 934: buffer_put_int(m, forced_command != NULL);
1.1 provos 935:
1.59 avsm 936: mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1.1 provos 937:
938: if (type == MM_RSAHOSTKEY)
939: monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
940:
941: return (0);
942: }
943:
944: static int
945: monitor_valid_userblob(u_char *data, u_int datalen)
946: {
947: Buffer b;
1.26 markus 948: char *p;
1.1 provos 949: u_int len;
950: int fail = 0;
951:
952: buffer_init(&b);
953: buffer_append(&b, data, datalen);
1.3 markus 954:
1.1 provos 955: if (datafellows & SSH_OLD_SESSIONID) {
1.13 markus 956: p = buffer_ptr(&b);
957: len = buffer_len(&b);
958: if ((session_id2 == NULL) ||
959: (len < session_id2_len) ||
1.108 djm 960: (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1.13 markus 961: fail++;
1.1 provos 962: buffer_consume(&b, session_id2_len);
963: } else {
1.13 markus 964: p = buffer_get_string(&b, &len);
965: if ((session_id2 == NULL) ||
966: (len != session_id2_len) ||
1.108 djm 967: (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1.1 provos 968: fail++;
1.13 markus 969: xfree(p);
1.1 provos 970: }
971: if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
972: fail++;
973: p = buffer_get_string(&b, NULL);
974: if (strcmp(authctxt->user, p) != 0) {
1.38 itojun 975: logit("wrong user name passed to monitor: expected %s != %.100s",
1.1 provos 976: authctxt->user, p);
977: fail++;
978: }
979: xfree(p);
980: buffer_skip_string(&b);
981: if (datafellows & SSH_BUG_PKAUTH) {
982: if (!buffer_get_char(&b))
983: fail++;
984: } else {
985: p = buffer_get_string(&b, NULL);
986: if (strcmp("publickey", p) != 0)
987: fail++;
988: xfree(p);
989: if (!buffer_get_char(&b))
990: fail++;
991: buffer_skip_string(&b);
992: }
993: buffer_skip_string(&b);
994: if (buffer_len(&b) != 0)
995: fail++;
996: buffer_free(&b);
997: return (fail == 0);
998: }
999:
1000: static int
1.26 markus 1001: monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1002: char *chost)
1.1 provos 1003: {
1004: Buffer b;
1.26 markus 1005: char *p;
1.1 provos 1006: u_int len;
1007: int fail = 0;
1008:
1009: buffer_init(&b);
1010: buffer_append(&b, data, datalen);
1.3 markus 1011:
1.13 markus 1012: p = buffer_get_string(&b, &len);
1013: if ((session_id2 == NULL) ||
1014: (len != session_id2_len) ||
1.108 djm 1015: (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1.1 provos 1016: fail++;
1.13 markus 1017: xfree(p);
1018:
1.1 provos 1019: if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1020: fail++;
1021: p = buffer_get_string(&b, NULL);
1022: if (strcmp(authctxt->user, p) != 0) {
1.38 itojun 1023: logit("wrong user name passed to monitor: expected %s != %.100s",
1.1 provos 1024: authctxt->user, p);
1025: fail++;
1026: }
1027: xfree(p);
1028: buffer_skip_string(&b); /* service */
1029: p = buffer_get_string(&b, NULL);
1030: if (strcmp(p, "hostbased") != 0)
1031: fail++;
1032: xfree(p);
1033: buffer_skip_string(&b); /* pkalg */
1034: buffer_skip_string(&b); /* pkblob */
1035:
1036: /* verify client host, strip trailing dot if necessary */
1037: p = buffer_get_string(&b, NULL);
1038: if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1039: p[len - 1] = '\0';
1040: if (strcmp(p, chost) != 0)
1041: fail++;
1042: xfree(p);
1043:
1044: /* verify client user */
1045: p = buffer_get_string(&b, NULL);
1046: if (strcmp(p, cuser) != 0)
1047: fail++;
1048: xfree(p);
1049:
1050: if (buffer_len(&b) != 0)
1051: fail++;
1052: buffer_free(&b);
1053: return (fail == 0);
1054: }
1055:
1056: int
1.59 avsm 1057: mm_answer_keyverify(int sock, Buffer *m)
1.1 provos 1058: {
1059: Key *key;
1060: u_char *signature, *data, *blob;
1061: u_int signaturelen, datalen, bloblen;
1062: int verified = 0;
1063: int valid_data = 0;
1064:
1065: blob = buffer_get_string(m, &bloblen);
1066: signature = buffer_get_string(m, &signaturelen);
1067: data = buffer_get_string(m, &datalen);
1068:
1069: if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1.8 mouring 1070: !monitor_allowed_key(blob, bloblen))
1.14 markus 1071: fatal("%s: bad key, not previously allowed", __func__);
1.1 provos 1072:
1073: key = key_from_blob(blob, bloblen);
1074: if (key == NULL)
1.14 markus 1075: fatal("%s: bad public key blob", __func__);
1.1 provos 1076:
1077: switch (key_blobtype) {
1078: case MM_USERKEY:
1079: valid_data = monitor_valid_userblob(data, datalen);
1080: break;
1081: case MM_HOSTKEY:
1082: valid_data = monitor_valid_hostbasedblob(data, datalen,
1083: hostbased_cuser, hostbased_chost);
1084: break;
1085: default:
1086: valid_data = 0;
1087: break;
1088: }
1089: if (!valid_data)
1.14 markus 1090: fatal("%s: bad signature data blob", __func__);
1.1 provos 1091:
1092: verified = key_verify(key, signature, signaturelen, data, datalen);
1093: debug3("%s: key %p signature %s",
1.89 markus 1094: __func__, key, (verified == 1) ? "verified" : "unverified");
1.1 provos 1095:
1096: key_free(key);
1097: xfree(blob);
1098: xfree(signature);
1099: xfree(data);
1100:
1.17 stevesk 1101: auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1102:
1.1 provos 1103: monitor_reset_key_state();
1.3 markus 1104:
1.1 provos 1105: buffer_clear(m);
1106: buffer_put_int(m, verified);
1.59 avsm 1107: mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1.1 provos 1108:
1.89 markus 1109: return (verified == 1);
1.1 provos 1110: }
1111:
1.2 markus 1112: static void
1.1 provos 1113: mm_record_login(Session *s, struct passwd *pw)
1114: {
1115: socklen_t fromlen;
1116: struct sockaddr_storage from;
1117:
1118: /*
1119: * Get IP address of client. If the connection is not a socket, let
1120: * the address be 0.0.0.0.
1121: */
1122: memset(&from, 0, sizeof(from));
1.24 stevesk 1123: fromlen = sizeof(from);
1.1 provos 1124: if (packet_connection_is_on_socket()) {
1125: if (getpeername(packet_get_connection_in(),
1.74 deraadt 1126: (struct sockaddr *)&from, &fromlen) < 0) {
1.1 provos 1127: debug("getpeername: %.100s", strerror(errno));
1.50 markus 1128: cleanup_exit(255);
1.1 provos 1129: }
1130: }
1131: /* Record that there was a login on that tty from the remote host. */
1132: record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1.42 markus 1133: get_remote_name_or_ip(utmp_len, options.use_dns),
1.24 stevesk 1134: (struct sockaddr *)&from, fromlen);
1.1 provos 1135: }
1136:
1137: static void
1138: mm_session_close(Session *s)
1139: {
1.41 djm 1140: debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1.1 provos 1141: if (s->ttyfd != -1) {
1.86 stevesk 1142: debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1.1 provos 1143: session_pty_cleanup2(s);
1144: }
1.96 djm 1145: session_unused(s->self);
1.1 provos 1146: }
1147:
1148: int
1.59 avsm 1149: mm_answer_pty(int sock, Buffer *m)
1.1 provos 1150: {
1.11 mouring 1151: extern struct monitor *pmonitor;
1.1 provos 1152: Session *s;
1153: int res, fd0;
1154:
1.14 markus 1155: debug3("%s entering", __func__);
1.1 provos 1156:
1157: buffer_clear(m);
1158: s = session_new();
1159: if (s == NULL)
1160: goto error;
1161: s->authctxt = authctxt;
1162: s->pw = authctxt->pw;
1.11 mouring 1163: s->pid = pmonitor->m_pid;
1.1 provos 1164: res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1165: if (res == 0)
1166: goto error;
1167: pty_setowner(authctxt->pw, s->tty);
1168:
1169: buffer_put_int(m, 1);
1170: buffer_put_cstring(m, s->tty);
1171:
1172: /* We need to trick ttyslot */
1173: if (dup2(s->ttyfd, 0) == -1)
1.14 markus 1174: fatal("%s: dup2", __func__);
1.1 provos 1175:
1176: mm_record_login(s, authctxt->pw);
1177:
1178: /* Now we can close the file descriptor again */
1179: close(0);
1.61 dtucker 1180:
1181: /* send messages generated by record_login */
1182: buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1183: buffer_clear(&loginmsg);
1184:
1185: mm_request_send(sock, MONITOR_ANS_PTY, m);
1186:
1.92 djm 1187: if (mm_send_fd(sock, s->ptyfd) == -1 ||
1188: mm_send_fd(sock, s->ttyfd) == -1)
1189: fatal("%s: send fds failed", __func__);
1.1 provos 1190:
1191: /* make sure nothing uses fd 0 */
1192: if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1.14 markus 1193: fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1.1 provos 1194: if (fd0 != 0)
1.14 markus 1195: error("%s: fd0 %d != 0", __func__, fd0);
1.1 provos 1196:
1197: /* slave is not needed */
1198: close(s->ttyfd);
1199: s->ttyfd = s->ptyfd;
1200: /* no need to dup() because nobody closes ptyfd */
1201: s->ptymaster = s->ptyfd;
1202:
1.86 stevesk 1203: debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1.1 provos 1204:
1205: return (0);
1206:
1207: error:
1208: if (s != NULL)
1209: mm_session_close(s);
1210: buffer_put_int(m, 0);
1.59 avsm 1211: mm_request_send(sock, MONITOR_ANS_PTY, m);
1.1 provos 1212: return (0);
1213: }
1214:
1215: int
1.59 avsm 1216: mm_answer_pty_cleanup(int sock, Buffer *m)
1.1 provos 1217: {
1218: Session *s;
1219: char *tty;
1220:
1.14 markus 1221: debug3("%s entering", __func__);
1.1 provos 1222:
1223: tty = buffer_get_string(m, NULL);
1224: if ((s = session_by_tty(tty)) != NULL)
1225: mm_session_close(s);
1226: buffer_clear(m);
1227: xfree(tty);
1228: return (0);
1229: }
1230:
1231: int
1.59 avsm 1232: mm_answer_sesskey(int sock, Buffer *m)
1.1 provos 1233: {
1234: BIGNUM *p;
1235: int rsafail;
1236:
1237: /* Turn off permissions */
1.62 dtucker 1238: monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1.1 provos 1239:
1240: if ((p = BN_new()) == NULL)
1.14 markus 1241: fatal("%s: BN_new", __func__);
1.1 provos 1242:
1243: buffer_get_bignum2(m, p);
1244:
1245: rsafail = ssh1_session_key(p);
1246:
1247: buffer_clear(m);
1248: buffer_put_int(m, rsafail);
1249: buffer_put_bignum2(m, p);
1250:
1251: BN_clear_free(p);
1252:
1.59 avsm 1253: mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1.1 provos 1254:
1255: /* Turn on permissions for sessid passing */
1256: monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1257:
1258: return (0);
1259: }
1260:
1261: int
1.59 avsm 1262: mm_answer_sessid(int sock, Buffer *m)
1.1 provos 1263: {
1264: int i;
1265:
1.14 markus 1266: debug3("%s entering", __func__);
1.1 provos 1267:
1268: if (buffer_len(m) != 16)
1.14 markus 1269: fatal("%s: bad ssh1 session id", __func__);
1.1 provos 1270: for (i = 0; i < 16; i++)
1271: session_id[i] = buffer_get_char(m);
1272:
1273: /* Turn on permissions for getpwnam */
1274: monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1275:
1276: return (0);
1277: }
1278:
1279: int
1.59 avsm 1280: mm_answer_rsa_keyallowed(int sock, Buffer *m)
1.1 provos 1281: {
1282: BIGNUM *client_n;
1283: Key *key = NULL;
1.3 markus 1284: u_char *blob = NULL;
1285: u_int blen = 0;
1.1 provos 1286: int allowed = 0;
1287:
1.14 markus 1288: debug3("%s entering", __func__);
1.1 provos 1289:
1.77 dtucker 1290: auth_method = "rsa";
1.12 markus 1291: if (options.rsa_authentication && authctxt->valid) {
1.1 provos 1292: if ((client_n = BN_new()) == NULL)
1.14 markus 1293: fatal("%s: BN_new", __func__);
1.1 provos 1294: buffer_get_bignum2(m, client_n);
1295: allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1296: BN_clear_free(client_n);
1297: }
1298: buffer_clear(m);
1299: buffer_put_int(m, allowed);
1.32 markus 1300: buffer_put_int(m, forced_command != NULL);
1.1 provos 1301:
1302: /* clear temporarily storage (used by generate challenge) */
1303: monitor_reset_key_state();
1304:
1305: if (allowed && key != NULL) {
1306: key->type = KEY_RSA; /* cheat for key_to_blob */
1307: if (key_to_blob(key, &blob, &blen) == 0)
1.14 markus 1308: fatal("%s: key_to_blob failed", __func__);
1.1 provos 1309: buffer_put_string(m, blob, blen);
1310:
1311: /* Save temporarily for comparison in verify */
1312: key_blob = blob;
1313: key_bloblen = blen;
1314: key_blobtype = MM_RSAUSERKEY;
1.33 markus 1315: }
1316: if (key != NULL)
1.1 provos 1317: key_free(key);
1318:
1.59 avsm 1319: mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1.1 provos 1320:
1321: monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1322: monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1323: return (0);
1324: }
1325:
1326: int
1.59 avsm 1327: mm_answer_rsa_challenge(int sock, Buffer *m)
1.1 provos 1328: {
1329: Key *key = NULL;
1.3 markus 1330: u_char *blob;
1331: u_int blen;
1.1 provos 1332:
1.14 markus 1333: debug3("%s entering", __func__);
1.1 provos 1334:
1335: if (!authctxt->valid)
1.14 markus 1336: fatal("%s: authctxt not valid", __func__);
1.1 provos 1337: blob = buffer_get_string(m, &blen);
1338: if (!monitor_allowed_key(blob, blen))
1.14 markus 1339: fatal("%s: bad key, not previously allowed", __func__);
1.1 provos 1340: if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1.14 markus 1341: fatal("%s: key type mismatch", __func__);
1.1 provos 1342: if ((key = key_from_blob(blob, blen)) == NULL)
1.14 markus 1343: fatal("%s: received bad key", __func__);
1.101 djm 1344: if (key->type != KEY_RSA)
1345: fatal("%s: received bad key type %d", __func__, key->type);
1346: key->type = KEY_RSA1;
1.1 provos 1347: if (ssh1_challenge)
1348: BN_clear_free(ssh1_challenge);
1349: ssh1_challenge = auth_rsa_generate_challenge(key);
1350:
1351: buffer_clear(m);
1352: buffer_put_bignum2(m, ssh1_challenge);
1353:
1.14 markus 1354: debug3("%s sending reply", __func__);
1.59 avsm 1355: mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1.1 provos 1356:
1357: monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1.33 markus 1358:
1359: xfree(blob);
1360: key_free(key);
1.1 provos 1361: return (0);
1362: }
1363:
1364: int
1.59 avsm 1365: mm_answer_rsa_response(int sock, Buffer *m)
1.1 provos 1366: {
1367: Key *key = NULL;
1.3 markus 1368: u_char *blob, *response;
1369: u_int blen, len;
1370: int success;
1.1 provos 1371:
1.14 markus 1372: debug3("%s entering", __func__);
1.1 provos 1373:
1374: if (!authctxt->valid)
1.14 markus 1375: fatal("%s: authctxt not valid", __func__);
1.1 provos 1376: if (ssh1_challenge == NULL)
1.14 markus 1377: fatal("%s: no ssh1_challenge", __func__);
1.1 provos 1378:
1379: blob = buffer_get_string(m, &blen);
1380: if (!monitor_allowed_key(blob, blen))
1.14 markus 1381: fatal("%s: bad key, not previously allowed", __func__);
1.1 provos 1382: if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1.14 markus 1383: fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1.1 provos 1384: if ((key = key_from_blob(blob, blen)) == NULL)
1.14 markus 1385: fatal("%s: received bad key", __func__);
1.1 provos 1386: response = buffer_get_string(m, &len);
1387: if (len != 16)
1.14 markus 1388: fatal("%s: received bad response to challenge", __func__);
1.1 provos 1389: success = auth_rsa_verify_response(key, ssh1_challenge, response);
1390:
1.33 markus 1391: xfree(blob);
1.1 provos 1392: key_free(key);
1393: xfree(response);
1394:
1395: auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1396:
1397: /* reset state */
1398: BN_clear_free(ssh1_challenge);
1399: ssh1_challenge = NULL;
1400: monitor_reset_key_state();
1401:
1402: buffer_clear(m);
1403: buffer_put_int(m, success);
1.59 avsm 1404: mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1.1 provos 1405:
1406: return (success);
1407: }
1408:
1409: int
1.59 avsm 1410: mm_answer_term(int sock, Buffer *req)
1.1 provos 1411: {
1.11 mouring 1412: extern struct monitor *pmonitor;
1.1 provos 1413: int res, status;
1414:
1.14 markus 1415: debug3("%s: tearing down sessions", __func__);
1.1 provos 1416:
1417: /* The child is terminating */
1418: session_destroy_all(&mm_session_close);
1419:
1.11 mouring 1420: while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1.9 markus 1421: if (errno != EINTR)
1422: exit(1);
1.1 provos 1423:
1424: res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1425:
1426: /* Terminate process */
1.57 deraadt 1427: exit(res);
1.1 provos 1428: }
1429:
1430: void
1.11 mouring 1431: monitor_apply_keystate(struct monitor *pmonitor)
1.1 provos 1432: {
1433: if (compat20) {
1434: set_newkeys(MODE_IN);
1435: set_newkeys(MODE_OUT);
1436: } else {
1437: packet_set_protocol_flags(child_state.ssh1protoflags);
1.15 markus 1438: packet_set_encryption_key(child_state.ssh1key,
1439: child_state.ssh1keylen, child_state.ssh1cipher);
1440: xfree(child_state.ssh1key);
1.1 provos 1441: }
1442:
1.15 markus 1443: /* for rc4 and other stateful ciphers */
1.1 provos 1444: packet_set_keycontext(MODE_OUT, child_state.keyout);
1445: xfree(child_state.keyout);
1446: packet_set_keycontext(MODE_IN, child_state.keyin);
1447: xfree(child_state.keyin);
1448:
1449: if (!compat20) {
1450: packet_set_iv(MODE_OUT, child_state.ivout);
1451: xfree(child_state.ivout);
1452: packet_set_iv(MODE_IN, child_state.ivin);
1453: xfree(child_state.ivin);
1454: }
1455:
1456: memcpy(&incoming_stream, &child_state.incoming,
1457: sizeof(incoming_stream));
1458: memcpy(&outgoing_stream, &child_state.outgoing,
1459: sizeof(outgoing_stream));
1.3 markus 1460:
1.1 provos 1461: /* Update with new address */
1.16 djm 1462: if (options.compression)
1463: mm_init_compression(pmonitor->m_zlib);
1.1 provos 1464:
1465: /* Network I/O buffers */
1466: /* XXX inefficient for large buffers, need: buffer_init_from_string */
1.102 andreas 1467: buffer_clear(packet_get_input());
1468: buffer_append(packet_get_input(), child_state.input, child_state.ilen);
1.1 provos 1469: memset(child_state.input, 0, child_state.ilen);
1470: xfree(child_state.input);
1471:
1.102 andreas 1472: buffer_clear(packet_get_output());
1473: buffer_append(packet_get_output(), child_state.output,
1474: child_state.olen);
1.1 provos 1475: memset(child_state.output, 0, child_state.olen);
1476: xfree(child_state.output);
1.103 andreas 1477:
1478: /* Roaming */
1479: if (compat20)
1480: roam_set_bytes(child_state.sent_bytes, child_state.recv_bytes);
1.1 provos 1481: }
1482:
1.2 markus 1483: static Kex *
1.1 provos 1484: mm_get_kex(Buffer *m)
1485: {
1486: Kex *kex;
1487: void *blob;
1488: u_int bloblen;
1489:
1.75 djm 1490: kex = xcalloc(1, sizeof(*kex));
1.1 provos 1491: kex->session_id = buffer_get_string(m, &kex->session_id_len);
1.107 djm 1492: if (session_id2 == NULL ||
1493: kex->session_id_len != session_id2_len ||
1.108 djm 1494: timingsafe_bcmp(kex->session_id, session_id2, session_id2_len) != 0)
1.13 markus 1495: fatal("mm_get_get: internal error: bad session id");
1.1 provos 1496: kex->we_need = buffer_get_int(m);
1.34 markus 1497: kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1.58 djm 1498: kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1.34 markus 1499: kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1.69 djm 1500: kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1.109 djm 1501: kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
1.1 provos 1502: kex->server = 1;
1503: kex->hostkey_type = buffer_get_int(m);
1504: kex->kex_type = buffer_get_int(m);
1505: blob = buffer_get_string(m, &bloblen);
1506: buffer_init(&kex->my);
1507: buffer_append(&kex->my, blob, bloblen);
1508: xfree(blob);
1509: blob = buffer_get_string(m, &bloblen);
1510: buffer_init(&kex->peer);
1511: buffer_append(&kex->peer, blob, bloblen);
1512: xfree(blob);
1513: kex->done = 1;
1514: kex->flags = buffer_get_int(m);
1515: kex->client_version_string = buffer_get_string(m, NULL);
1516: kex->server_version_string = buffer_get_string(m, NULL);
1.105 djm 1517: kex->load_host_public_key=&get_hostkey_public_by_type;
1518: kex->load_host_private_key=&get_hostkey_private_by_type;
1.1 provos 1519: kex->host_key_index=&get_hostkey_index;
1520:
1521: return (kex);
1522: }
1523:
1524: /* This function requries careful sanity checking */
1525:
1526: void
1.11 mouring 1527: mm_get_keystate(struct monitor *pmonitor)
1.1 provos 1528: {
1529: Buffer m;
1530: u_char *blob, *p;
1531: u_int bloblen, plen;
1.37 markus 1532: u_int32_t seqnr, packets;
1.99 markus 1533: u_int64_t blocks, bytes;
1.1 provos 1534:
1.14 markus 1535: debug3("%s: Waiting for new keys", __func__);
1.1 provos 1536:
1537: buffer_init(&m);
1.11 mouring 1538: mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1.1 provos 1539: if (!compat20) {
1540: child_state.ssh1protoflags = buffer_get_int(&m);
1541: child_state.ssh1cipher = buffer_get_int(&m);
1.15 markus 1542: child_state.ssh1key = buffer_get_string(&m,
1543: &child_state.ssh1keylen);
1.1 provos 1544: child_state.ivout = buffer_get_string(&m,
1545: &child_state.ivoutlen);
1546: child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1547: goto skip;
1548: } else {
1549: /* Get the Kex for rekeying */
1.11 mouring 1550: *pmonitor->m_pkex = mm_get_kex(&m);
1.1 provos 1551: }
1552:
1553: blob = buffer_get_string(&m, &bloblen);
1554: current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1555: xfree(blob);
1556:
1.14 markus 1557: debug3("%s: Waiting for second key", __func__);
1.1 provos 1558: blob = buffer_get_string(&m, &bloblen);
1559: current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1560: xfree(blob);
1.3 markus 1561:
1.1 provos 1562: /* Now get sequence numbers for the packets */
1.37 markus 1563: seqnr = buffer_get_int(&m);
1564: blocks = buffer_get_int64(&m);
1565: packets = buffer_get_int(&m);
1.99 markus 1566: bytes = buffer_get_int64(&m);
1567: packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
1.37 markus 1568: seqnr = buffer_get_int(&m);
1569: blocks = buffer_get_int64(&m);
1570: packets = buffer_get_int(&m);
1.99 markus 1571: bytes = buffer_get_int64(&m);
1572: packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
1.1 provos 1573:
1574: skip:
1575: /* Get the key context */
1576: child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1577: child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1578:
1.14 markus 1579: debug3("%s: Getting compression state", __func__);
1.1 provos 1580: /* Get compression state */
1581: p = buffer_get_string(&m, &plen);
1582: if (plen != sizeof(child_state.outgoing))
1.14 markus 1583: fatal("%s: bad request size", __func__);
1.1 provos 1584: memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1585: xfree(p);
1586:
1587: p = buffer_get_string(&m, &plen);
1588: if (plen != sizeof(child_state.incoming))
1.14 markus 1589: fatal("%s: bad request size", __func__);
1.1 provos 1590: memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1591: xfree(p);
1592:
1593: /* Network I/O buffers */
1.14 markus 1594: debug3("%s: Getting Network I/O buffers", __func__);
1.1 provos 1595: child_state.input = buffer_get_string(&m, &child_state.ilen);
1596: child_state.output = buffer_get_string(&m, &child_state.olen);
1.103 andreas 1597:
1598: /* Roaming */
1599: if (compat20) {
1600: child_state.sent_bytes = buffer_get_int64(&m);
1601: child_state.recv_bytes = buffer_get_int64(&m);
1602: }
1.1 provos 1603:
1604: buffer_free(&m);
1605: }
1606:
1607:
1608: /* Allocation functions for zlib */
1609: void *
1610: mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1611: {
1.30 markus 1612: size_t len = (size_t) size * ncount;
1.1 provos 1613: void *address;
1614:
1.23 millert 1615: if (len == 0 || ncount > SIZE_T_MAX / size)
1.18 deraadt 1616: fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1617:
1618: address = mm_malloc(mm, len);
1.1 provos 1619:
1620: return (address);
1621: }
1622:
1623: void
1624: mm_zfree(struct mm_master *mm, void *address)
1625: {
1626: mm_free(mm, address);
1627: }
1628:
1629: void
1630: mm_init_compression(struct mm_master *mm)
1631: {
1632: outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1633: outgoing_stream.zfree = (free_func)mm_zfree;
1634: outgoing_stream.opaque = mm;
1635:
1636: incoming_stream.zalloc = (alloc_func)mm_zalloc;
1637: incoming_stream.zfree = (free_func)mm_zfree;
1638: incoming_stream.opaque = mm;
1639: }
1640:
1641: /* XXX */
1642:
1643: #define FD_CLOSEONEXEC(x) do { \
1.111 djm 1644: if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
1.1 provos 1645: fatal("fcntl(%d, F_SETFD)", x); \
1646: } while (0)
1647:
1.2 markus 1648: static void
1.114 djm 1649: monitor_openfds(struct monitor *mon, int do_logfds)
1.3 markus 1650: {
1.114 djm 1651: int pair[2];
1652:
1.1 provos 1653: if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1.114 djm 1654: fatal("%s: socketpair: %s", __func__, strerror(errno));
1.1 provos 1655: FD_CLOSEONEXEC(pair[0]);
1656: FD_CLOSEONEXEC(pair[1]);
1.114 djm 1657: mon->m_recvfd = pair[0];
1658: mon->m_sendfd = pair[1];
1659:
1660: if (do_logfds) {
1661: if (pipe(pair) == -1)
1662: fatal("%s: pipe: %s", __func__, strerror(errno));
1663: FD_CLOSEONEXEC(pair[0]);
1664: FD_CLOSEONEXEC(pair[1]);
1665: mon->m_log_recvfd = pair[0];
1666: mon->m_log_sendfd = pair[1];
1667: } else
1668: mon->m_log_recvfd = mon->m_log_sendfd = -1;
1.1 provos 1669: }
1670:
1671: #define MM_MEMSIZE 65536
1672:
1673: struct monitor *
1674: monitor_init(void)
1675: {
1676: struct monitor *mon;
1677:
1.75 djm 1678: mon = xcalloc(1, sizeof(*mon));
1.1 provos 1679:
1.114 djm 1680: monitor_openfds(mon, 1);
1.1 provos 1681:
1682: /* Used to share zlib space across processes */
1.16 djm 1683: if (options.compression) {
1684: mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1685: mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1.1 provos 1686:
1.16 djm 1687: /* Compression needs to share state across borders */
1688: mm_init_compression(mon->m_zlib);
1689: }
1.1 provos 1690:
1691: return mon;
1692: }
1693:
1694: void
1695: monitor_reinit(struct monitor *mon)
1696: {
1.114 djm 1697: monitor_openfds(mon, 0);
1.1 provos 1698: }
1.46 markus 1699:
1700: #ifdef GSSAPI
1701: int
1.59 avsm 1702: mm_answer_gss_setup_ctx(int sock, Buffer *m)
1.46 markus 1703: {
1.59 avsm 1704: gss_OID_desc goid;
1.46 markus 1705: OM_uint32 major;
1706: u_int len;
1707:
1.59 avsm 1708: goid.elements = buffer_get_string(m, &len);
1709: goid.length = len;
1.46 markus 1710:
1.59 avsm 1711: major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1.46 markus 1712:
1.59 avsm 1713: xfree(goid.elements);
1.46 markus 1714:
1715: buffer_clear(m);
1716: buffer_put_int(m, major);
1717:
1.64 stevesk 1718: mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1.46 markus 1719:
1720: /* Now we have a context, enable the step */
1721: monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1722:
1723: return (0);
1724: }
1725:
1726: int
1.59 avsm 1727: mm_answer_gss_accept_ctx(int sock, Buffer *m)
1.46 markus 1728: {
1729: gss_buffer_desc in;
1730: gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1.64 stevesk 1731: OM_uint32 major, minor;
1.46 markus 1732: OM_uint32 flags = 0; /* GSI needs this */
1.47 deraadt 1733: u_int len;
1.46 markus 1734:
1.47 deraadt 1735: in.value = buffer_get_string(m, &len);
1736: in.length = len;
1.46 markus 1737: major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1738: xfree(in.value);
1739:
1740: buffer_clear(m);
1741: buffer_put_int(m, major);
1742: buffer_put_string(m, out.value, out.length);
1743: buffer_put_int(m, flags);
1.59 avsm 1744: mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1.46 markus 1745:
1746: gss_release_buffer(&minor, &out);
1747:
1.64 stevesk 1748: if (major == GSS_S_COMPLETE) {
1.46 markus 1749: monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1750: monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1.52 markus 1751: monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1.46 markus 1752: }
1753: return (0);
1754: }
1755:
1756: int
1.59 avsm 1757: mm_answer_gss_checkmic(int sock, Buffer *m)
1.52 markus 1758: {
1759: gss_buffer_desc gssbuf, mic;
1760: OM_uint32 ret;
1761: u_int len;
1.54 djm 1762:
1.52 markus 1763: gssbuf.value = buffer_get_string(m, &len);
1764: gssbuf.length = len;
1765: mic.value = buffer_get_string(m, &len);
1766: mic.length = len;
1.54 djm 1767:
1.52 markus 1768: ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1.54 djm 1769:
1.52 markus 1770: xfree(gssbuf.value);
1771: xfree(mic.value);
1.54 djm 1772:
1.52 markus 1773: buffer_clear(m);
1774: buffer_put_int(m, ret);
1.54 djm 1775:
1.59 avsm 1776: mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1.54 djm 1777:
1.52 markus 1778: if (!GSS_ERROR(ret))
1779: monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1.54 djm 1780:
1.52 markus 1781: return (0);
1782: }
1783:
1784: int
1.59 avsm 1785: mm_answer_gss_userok(int sock, Buffer *m)
1.46 markus 1786: {
1787: int authenticated;
1788:
1789: authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1790:
1791: buffer_clear(m);
1792: buffer_put_int(m, authenticated);
1793:
1794: debug3("%s: sending result %d", __func__, authenticated);
1.59 avsm 1795: mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1.46 markus 1796:
1.64 stevesk 1797: auth_method = "gssapi-with-mic";
1.46 markus 1798:
1799: /* Monitor loop will terminate if authenticated */
1800: return (authenticated);
1801: }
1802: #endif /* GSSAPI */
1.100 djm 1803:
1804: #ifdef JPAKE
1805: int
1806: mm_answer_jpake_step1(int sock, Buffer *m)
1807: {
1808: struct jpake_ctx *pctx;
1809: u_char *x3_proof, *x4_proof;
1810: u_int x3_proof_len, x4_proof_len;
1811:
1812: if (!options.zero_knowledge_password_authentication)
1813: fatal("zero_knowledge_password_authentication disabled");
1814:
1815: if (authctxt->jpake_ctx != NULL)
1816: fatal("%s: authctxt->jpake_ctx already set (%p)",
1817: __func__, authctxt->jpake_ctx);
1818: authctxt->jpake_ctx = pctx = jpake_new();
1819:
1820: jpake_step1(pctx->grp,
1821: &pctx->server_id, &pctx->server_id_len,
1822: &pctx->x3, &pctx->x4, &pctx->g_x3, &pctx->g_x4,
1823: &x3_proof, &x3_proof_len,
1824: &x4_proof, &x4_proof_len);
1825:
1826: JPAKE_DEBUG_CTX((pctx, "step1 done in %s", __func__));
1827:
1828: buffer_clear(m);
1829:
1830: buffer_put_string(m, pctx->server_id, pctx->server_id_len);
1831: buffer_put_bignum2(m, pctx->g_x3);
1832: buffer_put_bignum2(m, pctx->g_x4);
1833: buffer_put_string(m, x3_proof, x3_proof_len);
1834: buffer_put_string(m, x4_proof, x4_proof_len);
1835:
1836: debug3("%s: sending step1", __func__);
1837: mm_request_send(sock, MONITOR_ANS_JPAKE_STEP1, m);
1838:
1839: bzero(x3_proof, x3_proof_len);
1840: bzero(x4_proof, x4_proof_len);
1841: xfree(x3_proof);
1842: xfree(x4_proof);
1843:
1844: monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_GET_PWDATA, 1);
1845: monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 0);
1846:
1847: return 0;
1848: }
1849:
1850: int
1851: mm_answer_jpake_get_pwdata(int sock, Buffer *m)
1852: {
1853: struct jpake_ctx *pctx = authctxt->jpake_ctx;
1854: char *hash_scheme, *salt;
1855:
1856: if (pctx == NULL)
1857: fatal("%s: pctx == NULL", __func__);
1858:
1859: auth2_jpake_get_pwdata(authctxt, &pctx->s, &hash_scheme, &salt);
1860:
1861: buffer_clear(m);
1862: /* pctx->s is sensitive, not returned to slave */
1863: buffer_put_cstring(m, hash_scheme);
1864: buffer_put_cstring(m, salt);
1865:
1866: debug3("%s: sending pwdata", __func__);
1867: mm_request_send(sock, MONITOR_ANS_JPAKE_GET_PWDATA, m);
1868:
1869: bzero(hash_scheme, strlen(hash_scheme));
1870: bzero(salt, strlen(salt));
1871: xfree(hash_scheme);
1872: xfree(salt);
1873:
1874: monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP2, 1);
1875:
1876: return 0;
1877: }
1878:
1879: int
1880: mm_answer_jpake_step2(int sock, Buffer *m)
1881: {
1882: struct jpake_ctx *pctx = authctxt->jpake_ctx;
1883: u_char *x1_proof, *x2_proof, *x4_s_proof;
1884: u_int x1_proof_len, x2_proof_len, x4_s_proof_len;
1885:
1886: if (pctx == NULL)
1887: fatal("%s: pctx == NULL", __func__);
1888:
1889: if ((pctx->g_x1 = BN_new()) == NULL ||
1890: (pctx->g_x2 = BN_new()) == NULL)
1891: fatal("%s: BN_new", __func__);
1892: buffer_get_bignum2(m, pctx->g_x1);
1893: buffer_get_bignum2(m, pctx->g_x2);
1894: pctx->client_id = buffer_get_string(m, &pctx->client_id_len);
1895: x1_proof = buffer_get_string(m, &x1_proof_len);
1896: x2_proof = buffer_get_string(m, &x2_proof_len);
1897:
1898: jpake_step2(pctx->grp, pctx->s, pctx->g_x3,
1899: pctx->g_x1, pctx->g_x2, pctx->x4,
1900: pctx->client_id, pctx->client_id_len,
1901: pctx->server_id, pctx->server_id_len,
1902: x1_proof, x1_proof_len,
1903: x2_proof, x2_proof_len,
1904: &pctx->b,
1905: &x4_s_proof, &x4_s_proof_len);
1906:
1907: JPAKE_DEBUG_CTX((pctx, "step2 done in %s", __func__));
1908:
1909: bzero(x1_proof, x1_proof_len);
1910: bzero(x2_proof, x2_proof_len);
1911: xfree(x1_proof);
1912: xfree(x2_proof);
1913:
1914: buffer_clear(m);
1915:
1916: buffer_put_bignum2(m, pctx->b);
1917: buffer_put_string(m, x4_s_proof, x4_s_proof_len);
1918:
1919: debug3("%s: sending step2", __func__);
1920: mm_request_send(sock, MONITOR_ANS_JPAKE_STEP2, m);
1921:
1922: bzero(x4_s_proof, x4_s_proof_len);
1923: xfree(x4_s_proof);
1924:
1925: monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_KEY_CONFIRM, 1);
1926:
1927: return 0;
1928: }
1929:
1930: int
1931: mm_answer_jpake_key_confirm(int sock, Buffer *m)
1932: {
1933: struct jpake_ctx *pctx = authctxt->jpake_ctx;
1934: u_char *x2_s_proof;
1935: u_int x2_s_proof_len;
1936:
1937: if (pctx == NULL)
1938: fatal("%s: pctx == NULL", __func__);
1939:
1940: if ((pctx->a = BN_new()) == NULL)
1941: fatal("%s: BN_new", __func__);
1942: buffer_get_bignum2(m, pctx->a);
1943: x2_s_proof = buffer_get_string(m, &x2_s_proof_len);
1944:
1945: jpake_key_confirm(pctx->grp, pctx->s, pctx->a,
1946: pctx->x4, pctx->g_x3, pctx->g_x4, pctx->g_x1, pctx->g_x2,
1947: pctx->server_id, pctx->server_id_len,
1948: pctx->client_id, pctx->client_id_len,
1949: session_id2, session_id2_len,
1950: x2_s_proof, x2_s_proof_len,
1951: &pctx->k,
1952: &pctx->h_k_sid_sessid, &pctx->h_k_sid_sessid_len);
1953:
1954: JPAKE_DEBUG_CTX((pctx, "key_confirm done in %s", __func__));
1955:
1956: bzero(x2_s_proof, x2_s_proof_len);
1957: buffer_clear(m);
1958:
1959: /* pctx->k is sensitive, not sent */
1960: buffer_put_string(m, pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
1961:
1962: debug3("%s: sending confirmation hash", __func__);
1963: mm_request_send(sock, MONITOR_ANS_JPAKE_KEY_CONFIRM, m);
1964:
1965: monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_CHECK_CONFIRM, 1);
1966:
1967: return 0;
1968: }
1969:
1970: int
1971: mm_answer_jpake_check_confirm(int sock, Buffer *m)
1972: {
1973: int authenticated = 0;
1974: u_char *peer_confirm_hash;
1975: u_int peer_confirm_hash_len;
1976: struct jpake_ctx *pctx = authctxt->jpake_ctx;
1977:
1978: if (pctx == NULL)
1979: fatal("%s: pctx == NULL", __func__);
1980:
1981: peer_confirm_hash = buffer_get_string(m, &peer_confirm_hash_len);
1982:
1983: authenticated = jpake_check_confirm(pctx->k,
1984: pctx->client_id, pctx->client_id_len,
1985: session_id2, session_id2_len,
1986: peer_confirm_hash, peer_confirm_hash_len) && authctxt->valid;
1987:
1988: JPAKE_DEBUG_CTX((pctx, "check_confirm done in %s", __func__));
1989:
1990: bzero(peer_confirm_hash, peer_confirm_hash_len);
1991: xfree(peer_confirm_hash);
1992:
1993: buffer_clear(m);
1994: buffer_put_int(m, authenticated);
1995:
1996: debug3("%s: sending result %d", __func__, authenticated);
1997: mm_request_send(sock, MONITOR_ANS_JPAKE_CHECK_CONFIRM, m);
1998:
1999: monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 1);
2000:
2001: auth_method = "jpake-01@openssh.com";
2002: return authenticated;
2003: }
2004:
2005: #endif /* JPAKE */