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