Annotation of src/usr.bin/ssh/monitor.c, Revision 1.180
1.180 ! djm 1: /* $OpenBSD: monitor.c,v 1.179 2018/02/05 05:37:46 tb 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.95 djm 32: #include <sys/queue.h>
1.1 provos 33:
1.132 markus 34: #ifdef WITH_OPENSSL
1.78 stevesk 35: #include <openssl/dh.h>
1.132 markus 36: #endif
1.78 stevesk 37:
1.81 stevesk 38: #include <errno.h>
1.80 stevesk 39: #include <fcntl.h>
1.159 djm 40: #include <limits.h>
1.65 stevesk 41: #include <paths.h>
1.114 djm 42: #include <poll.h>
1.78 stevesk 43: #include <pwd.h>
1.68 stevesk 44: #include <signal.h>
1.134 djm 45: #include <stdarg.h>
1.142 millert 46: #include <stdint.h>
1.134 djm 47: #include <stdio.h>
1.84 stevesk 48: #include <stdlib.h>
1.82 stevesk 49: #include <string.h>
1.1 provos 50:
1.114 djm 51: #include "atomicio.h"
1.85 deraadt 52: #include "xmalloc.h"
1.1 provos 53: #include "ssh.h"
1.85 deraadt 54: #include "key.h"
55: #include "buffer.h"
56: #include "hostfile.h"
1.1 provos 57: #include "auth.h"
1.85 deraadt 58: #include "cipher.h"
1.1 provos 59: #include "kex.h"
60: #include "dh.h"
1.88 miod 61: #include <zlib.h>
1.1 provos 62: #include "packet.h"
63: #include "auth-options.h"
64: #include "sshpty.h"
65: #include "channels.h"
66: #include "session.h"
67: #include "sshlogin.h"
68: #include "canohost.h"
69: #include "log.h"
1.135 millert 70: #include "misc.h"
1.1 provos 71: #include "servconf.h"
72: #include "monitor.h"
1.85 deraadt 73: #ifdef GSSAPI
74: #include "ssh-gss.h"
75: #endif
1.1 provos 76: #include "monitor_wrap.h"
77: #include "monitor_fdpass.h"
78: #include "compat.h"
79: #include "ssh2.h"
1.127 markus 80: #include "authfd.h"
1.137 djm 81: #include "match.h"
1.138 djm 82: #include "ssherr.h"
1.1 provos 83:
1.46 markus 84: #ifdef GSSAPI
85: static Gssctxt *gsscontext = NULL;
86: #endif
87:
1.1 provos 88: /* Imports */
89: extern ServerOptions options;
90: extern u_int utmp_len;
91: extern u_char session_id[];
92: extern Buffer auth_debug;
93: extern int auth_debug_init;
1.61 dtucker 94: extern Buffer loginmsg;
1.180 ! djm 95: extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */
1.1 provos 96:
97: /* State exported from the child */
1.139 markus 98: static struct sshbuf *child_state;
1.1 provos 99:
1.43 markus 100: /* Functions on the monitor that answer unprivileged requests */
1.1 provos 101:
102: int mm_answer_moduli(int, Buffer *);
103: int mm_answer_sign(int, Buffer *);
104: int mm_answer_pwnamallow(int, Buffer *);
1.10 djm 105: int mm_answer_auth2_read_banner(int, Buffer *);
1.1 provos 106: int mm_answer_authserv(int, Buffer *);
107: int mm_answer_authpassword(int, Buffer *);
108: int mm_answer_bsdauthquery(int, Buffer *);
109: int mm_answer_bsdauthrespond(int, Buffer *);
110: int mm_answer_skeyquery(int, Buffer *);
111: int mm_answer_skeyrespond(int, Buffer *);
112: int mm_answer_keyallowed(int, Buffer *);
113: int mm_answer_keyverify(int, Buffer *);
114: int mm_answer_pty(int, Buffer *);
115: int mm_answer_pty_cleanup(int, Buffer *);
116: int mm_answer_term(int, Buffer *);
117: int mm_answer_rsa_keyallowed(int, Buffer *);
118: int mm_answer_rsa_challenge(int, Buffer *);
119: int mm_answer_rsa_response(int, Buffer *);
120: int mm_answer_sesskey(int, Buffer *);
121: int mm_answer_sessid(int, Buffer *);
122:
1.46 markus 123: #ifdef GSSAPI
124: int mm_answer_gss_setup_ctx(int, Buffer *);
125: int mm_answer_gss_accept_ctx(int, Buffer *);
126: int mm_answer_gss_userok(int, Buffer *);
1.52 markus 127: int mm_answer_gss_checkmic(int, Buffer *);
1.46 markus 128: #endif
1.25 itojun 129:
1.114 djm 130: static int monitor_read_log(struct monitor *);
131:
1.1 provos 132: static Authctxt *authctxt;
1.132 markus 133:
1.1 provos 134: /* local state for key verify */
135: static u_char *key_blob = NULL;
136: static u_int key_bloblen = 0;
137: static int key_blobtype = MM_NOKEY;
1.180 ! djm 138: static struct sshauthopt *key_opts = NULL;
1.26 markus 139: static char *hostbased_cuser = NULL;
140: static char *hostbased_chost = NULL;
1.1 provos 141: static char *auth_method = "unknown";
1.119 djm 142: static char *auth_submethod = NULL;
1.44 markus 143: static u_int session_id2_len = 0;
1.13 markus 144: static u_char *session_id2 = NULL;
1.40 markus 145: static pid_t monitor_child_pid;
1.1 provos 146:
147: struct mon_table {
148: enum monitor_reqtype type;
149: int flags;
150: int (*f)(int, Buffer *);
151: };
152:
153: #define MON_ISAUTH 0x0004 /* Required for Authentication */
154: #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
155: #define MON_ONCE 0x0010 /* Disable after calling */
1.77 dtucker 156: #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
1.1 provos 157:
158: #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
159:
160: #define MON_PERMIT 0x1000 /* Request is permitted */
161:
162: struct mon_table mon_dispatch_proto20[] = {
1.132 markus 163: #ifdef WITH_OPENSSL
1.1 provos 164: {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
1.132 markus 165: #endif
1.1 provos 166: {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
167: {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
168: {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
1.10 djm 169: {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
1.1 provos 170: {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
171: {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
1.66 stevesk 172: {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
1.1 provos 173: {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
174: {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
1.46 markus 175: #ifdef GSSAPI
176: {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
1.165 djm 177: {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
178: {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
179: {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
1.46 markus 180: #endif
1.1 provos 181: {0, 0, NULL}
182: };
183:
184: struct mon_table mon_dispatch_postauth20[] = {
1.132 markus 185: #ifdef WITH_OPENSSL
1.1 provos 186: {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
1.132 markus 187: #endif
1.1 provos 188: {MONITOR_REQ_SIGN, 0, mm_answer_sign},
189: {MONITOR_REQ_PTY, 0, mm_answer_pty},
190: {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
191: {MONITOR_REQ_TERM, 0, mm_answer_term},
192: {0, 0, NULL}
193: };
194:
195: struct mon_table *mon_dispatch;
196:
197: /* Specifies if a certain message is allowed at the moment */
198: static void
199: monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
200: {
201: while (ent->f != NULL) {
202: if (ent->type == type) {
203: ent->flags &= ~MON_PERMIT;
204: ent->flags |= permit ? MON_PERMIT : 0;
205: return;
206: }
207: ent++;
208: }
209: }
210:
211: static void
212: monitor_permit_authentications(int permit)
213: {
214: struct mon_table *ent = mon_dispatch;
215:
216: while (ent->f != NULL) {
217: if (ent->flags & MON_AUTH) {
218: ent->flags &= ~MON_PERMIT;
219: ent->flags |= permit ? MON_PERMIT : 0;
220: }
221: ent++;
222: }
223: }
224:
1.50 markus 225: void
226: monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
1.1 provos 227: {
1.167 djm 228: struct ssh *ssh = active_state; /* XXX */
1.1 provos 229: struct mon_table *ent;
1.119 djm 230: int authenticated = 0, partial = 0;
1.1 provos 231:
232: debug3("preauth child monitor started");
233:
1.179 tb 234: if (pmonitor->m_recvfd >= 0)
235: close(pmonitor->m_recvfd);
236: if (pmonitor->m_log_sendfd >= 0)
237: close(pmonitor->m_log_sendfd);
1.114 djm 238: pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
239:
1.50 markus 240: authctxt = _authctxt;
241: memset(authctxt, 0, sizeof(*authctxt));
1.180 ! djm 242: ssh->authctxt = authctxt;
1.50 markus 243:
1.162 markus 244: mon_dispatch = mon_dispatch_proto20;
245: /* Permit requests for moduli and signatures */
246: monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
247: monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
1.1 provos 248:
249: /* The first few requests do not require asynchronous access */
250: while (!authenticated) {
1.119 djm 251: partial = 0;
1.77 dtucker 252: auth_method = "unknown";
1.119 djm 253: auth_submethod = NULL;
1.172 djm 254: auth2_authctxt_reset_info(authctxt);
255:
1.89 markus 256: authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
1.118 djm 257:
258: /* Special handling for multiple required authentications */
259: if (options.num_auth_methods != 0) {
260: if (authenticated &&
261: !auth2_update_methods_lists(authctxt,
1.122 markus 262: auth_method, auth_submethod)) {
1.118 djm 263: debug3("%s: method %s: partial", __func__,
264: auth_method);
265: authenticated = 0;
1.119 djm 266: partial = 1;
1.118 djm 267: }
268: }
269:
1.1 provos 270: if (authenticated) {
271: if (!(ent->flags & MON_AUTHDECIDE))
272: fatal("%s: unexpected authentication from %d",
1.14 markus 273: __func__, ent->type);
1.1 provos 274: if (authctxt->pw->pw_uid == 0 &&
1.180 ! djm 275: !auth_root_allowed(ssh, auth_method))
1.1 provos 276: authenticated = 0;
277: }
1.77 dtucker 278: if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
1.119 djm 279: auth_log(authctxt, authenticated, partial,
1.125 djm 280: auth_method, auth_submethod);
1.150 djm 281: if (!partial && !authenticated)
1.1 provos 282: authctxt->failures++;
1.172 djm 283: if (authenticated || partial) {
284: auth2_update_session_info(authctxt,
285: auth_method, auth_submethod);
286: }
1.1 provos 287: }
288: }
289:
290: if (!authctxt->valid)
1.14 markus 291: fatal("%s: authenticated invalid user", __func__);
1.77 dtucker 292: if (strcmp(auth_method, "unknown") == 0)
293: fatal("%s: authentication method name unknown", __func__);
1.1 provos 294:
295: debug("%s: %s has been authenticated by privileged process",
1.14 markus 296: __func__, authctxt->user);
1.180 ! djm 297: ssh->authctxt = NULL;
1.167 djm 298: ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
1.1 provos 299:
1.11 mouring 300: mm_get_keystate(pmonitor);
1.120 markus 301:
302: /* Drain any buffered messages from the child */
303: while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
304: ;
1.114 djm 305:
1.179 tb 306: if (pmonitor->m_recvfd >= 0)
307: close(pmonitor->m_recvfd);
308: if (pmonitor->m_log_sendfd >= 0)
309: close(pmonitor->m_log_sendfd);
1.114 djm 310: pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
1.1 provos 311: }
312:
1.40 markus 313: static void
314: monitor_set_child_handler(pid_t pid)
315: {
316: monitor_child_pid = pid;
317: }
318:
319: static void
1.59 avsm 320: monitor_child_handler(int sig)
1.40 markus 321: {
1.59 avsm 322: kill(monitor_child_pid, sig);
1.40 markus 323: }
324:
1.1 provos 325: void
1.11 mouring 326: monitor_child_postauth(struct monitor *pmonitor)
1.1 provos 327: {
1.114 djm 328: close(pmonitor->m_recvfd);
329: pmonitor->m_recvfd = -1;
330:
1.40 markus 331: monitor_set_child_handler(pmonitor->m_pid);
332: signal(SIGHUP, &monitor_child_handler);
333: signal(SIGTERM, &monitor_child_handler);
1.91 djm 334: signal(SIGINT, &monitor_child_handler);
1.40 markus 335:
1.162 markus 336: mon_dispatch = mon_dispatch_postauth20;
337:
338: /* Permit requests for moduli and signatures */
339: monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
340: monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
341: monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
1.1 provos 342:
1.180 ! djm 343: if (auth_opts->permit_pty_flag) {
1.1 provos 344: monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
345: monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
346: }
347:
348: for (;;)
1.11 mouring 349: monitor_read(pmonitor, mon_dispatch, NULL);
1.1 provos 350: }
351:
1.114 djm 352: static int
353: monitor_read_log(struct monitor *pmonitor)
354: {
355: Buffer logmsg;
356: u_int len, level;
357: char *msg;
358:
359: buffer_init(&logmsg);
360:
361: /* Read length */
362: buffer_append_space(&logmsg, 4);
363: if (atomicio(read, pmonitor->m_log_recvfd,
364: buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
365: if (errno == EPIPE) {
1.116 djm 366: buffer_free(&logmsg);
1.114 djm 367: debug("%s: child log fd closed", __func__);
368: close(pmonitor->m_log_recvfd);
369: pmonitor->m_log_recvfd = -1;
370: return -1;
371: }
372: fatal("%s: log fd read: %s", __func__, strerror(errno));
373: }
374: len = buffer_get_int(&logmsg);
375: if (len <= 4 || len > 8192)
376: fatal("%s: invalid log message length %u", __func__, len);
377:
378: /* Read severity, message */
379: buffer_clear(&logmsg);
380: buffer_append_space(&logmsg, len);
381: if (atomicio(read, pmonitor->m_log_recvfd,
382: buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
383: fatal("%s: log fd read: %s", __func__, strerror(errno));
384:
385: /* Log it */
386: level = buffer_get_int(&logmsg);
387: msg = buffer_get_string(&logmsg, NULL);
388: if (log_level_name(level) == NULL)
389: fatal("%s: invalid log level %u (corrupted message?)",
390: __func__, level);
391: do_log2(level, "%s [preauth]", msg);
392:
393: buffer_free(&logmsg);
1.124 djm 394: free(msg);
1.114 djm 395:
396: return 0;
397: }
398:
1.1 provos 399: int
1.11 mouring 400: monitor_read(struct monitor *pmonitor, struct mon_table *ent,
1.1 provos 401: struct mon_table **pent)
402: {
403: Buffer m;
404: int ret;
405: u_char type;
1.114 djm 406: struct pollfd pfd[2];
407:
408: for (;;) {
1.130 tedu 409: memset(&pfd, 0, sizeof(pfd));
1.114 djm 410: pfd[0].fd = pmonitor->m_sendfd;
411: pfd[0].events = POLLIN;
412: pfd[1].fd = pmonitor->m_log_recvfd;
413: pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
1.115 djm 414: if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
415: if (errno == EINTR || errno == EAGAIN)
416: continue;
1.114 djm 417: fatal("%s: poll: %s", __func__, strerror(errno));
1.115 djm 418: }
1.114 djm 419: if (pfd[1].revents) {
420: /*
421: * Drain all log messages before processing next
422: * monitor request.
423: */
424: monitor_read_log(pmonitor);
425: continue;
426: }
427: if (pfd[0].revents)
428: break; /* Continues below */
429: }
1.1 provos 430:
431: buffer_init(&m);
432:
1.11 mouring 433: mm_request_receive(pmonitor->m_sendfd, &m);
1.1 provos 434: type = buffer_get_char(&m);
435:
1.14 markus 436: debug3("%s: checking request %d", __func__, type);
1.1 provos 437:
438: while (ent->f != NULL) {
439: if (ent->type == type)
440: break;
441: ent++;
442: }
443:
444: if (ent->f != NULL) {
445: if (!(ent->flags & MON_PERMIT))
1.14 markus 446: fatal("%s: unpermitted request %d", __func__,
1.1 provos 447: type);
1.11 mouring 448: ret = (*ent->f)(pmonitor->m_sendfd, &m);
1.1 provos 449: buffer_free(&m);
450:
451: /* The child may use this request only once, disable it */
452: if (ent->flags & MON_ONCE) {
1.14 markus 453: debug2("%s: %d used once, disabling now", __func__,
1.1 provos 454: type);
455: ent->flags &= ~MON_PERMIT;
456: }
457:
458: if (pent != NULL)
459: *pent = ent;
1.3 markus 460:
1.1 provos 461: return ret;
462: }
463:
1.14 markus 464: fatal("%s: unsupported request: %d", __func__, type);
1.1 provos 465:
466: /* NOTREACHED */
467: return (-1);
468: }
469:
470: /* allowed key state */
471: static int
472: monitor_allowed_key(u_char *blob, u_int bloblen)
473: {
474: /* make sure key is allowed */
475: if (key_blob == NULL || key_bloblen != bloblen ||
1.108 djm 476: timingsafe_bcmp(key_blob, blob, key_bloblen))
1.1 provos 477: return (0);
478: return (1);
479: }
480:
481: static void
482: monitor_reset_key_state(void)
483: {
484: /* reset state */
1.124 djm 485: free(key_blob);
486: free(hostbased_cuser);
487: free(hostbased_chost);
1.180 ! djm 488: sshauthopt_free(key_opts);
1.1 provos 489: key_blob = NULL;
490: key_bloblen = 0;
491: key_blobtype = MM_NOKEY;
1.180 ! djm 492: key_opts = NULL;
1.1 provos 493: hostbased_cuser = NULL;
494: hostbased_chost = NULL;
495: }
496:
1.132 markus 497: #ifdef WITH_OPENSSL
1.1 provos 498: int
1.59 avsm 499: mm_answer_moduli(int sock, Buffer *m)
1.1 provos 500: {
501: DH *dh;
502: int min, want, max;
503:
504: min = buffer_get_int(m);
505: want = buffer_get_int(m);
506: max = buffer_get_int(m);
507:
508: debug3("%s: got parameters: %d %d %d",
1.14 markus 509: __func__, min, want, max);
1.1 provos 510: /* We need to check here, too, in case the child got corrupted */
511: if (max < min || want < min || max < want)
512: fatal("%s: bad parameters: %d %d %d",
1.14 markus 513: __func__, min, want, max);
1.1 provos 514:
515: buffer_clear(m);
516:
517: dh = choose_dh(min, want, max);
518: if (dh == NULL) {
519: buffer_put_char(m, 0);
520: return (0);
521: } else {
522: /* Send first bignum */
523: buffer_put_char(m, 1);
524: buffer_put_bignum2(m, dh->p);
525: buffer_put_bignum2(m, dh->g);
1.3 markus 526:
1.1 provos 527: DH_free(dh);
528: }
1.59 avsm 529: mm_request_send(sock, MONITOR_ANS_MODULI, m);
1.1 provos 530: return (0);
531: }
1.132 markus 532: #endif
1.1 provos 533:
534: int
1.59 avsm 535: mm_answer_sign(int sock, Buffer *m)
1.1 provos 536: {
1.144 djm 537: struct ssh *ssh = active_state; /* XXX */
1.138 djm 538: extern int auth_sock; /* XXX move to state struct? */
539: struct sshkey *key;
1.157 djm 540: struct sshbuf *sigbuf = NULL;
541: u_char *p = NULL, *signature = NULL;
542: char *alg = NULL;
1.155 markus 543: size_t datlen, siglen, alglen;
1.159 djm 544: int r, is_proof = 0;
545: u_int keyid;
1.145 djm 546: const char proof_req[] = "hostkeys-prove-00@openssh.com";
1.3 markus 547:
1.14 markus 548: debug3("%s", __func__);
1.1 provos 549:
1.138 djm 550: if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
1.155 markus 551: (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
552: (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0)
1.138 djm 553: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.159 djm 554: if (keyid > INT_MAX)
555: fatal("%s: invalid key ID", __func__);
1.1 provos 556:
1.69 djm 557: /*
1.110 djm 558: * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
559: * SHA384 (48 bytes) and SHA512 (64 bytes).
1.144 djm 560: *
561: * Otherwise, verify the signature request is for a hostkey
562: * proof.
563: *
564: * XXX perform similar check for KEX signature requests too?
565: * it's not trivial, since what is signed is the hash, rather
566: * than the full kex structure...
1.69 djm 567: */
1.144 djm 568: if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
569: /*
570: * Construct expected hostkey proof and compare it to what
571: * the client sent us.
572: */
573: if (session_id2_len == 0) /* hostkeys is never first */
574: fatal("%s: bad data length: %zu", __func__, datlen);
575: if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
576: fatal("%s: no hostkey for index %d", __func__, keyid);
577: if ((sigbuf = sshbuf_new()) == NULL)
578: fatal("%s: sshbuf_new", __func__);
1.145 djm 579: if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
580: (r = sshbuf_put_string(sigbuf, session_id2,
1.152 jsg 581: session_id2_len)) != 0 ||
1.144 djm 582: (r = sshkey_puts(key, sigbuf)) != 0)
583: fatal("%s: couldn't prepare private key "
584: "proof buffer: %s", __func__, ssh_err(r));
585: if (datlen != sshbuf_len(sigbuf) ||
586: memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
587: fatal("%s: bad data length: %zu, hostkey proof len %zu",
588: __func__, datlen, sshbuf_len(sigbuf));
589: sshbuf_free(sigbuf);
590: is_proof = 1;
591: }
1.1 provos 592:
1.13 markus 593: /* save session id, it will be passed on the first call */
594: if (session_id2_len == 0) {
595: session_id2_len = datlen;
596: session_id2 = xmalloc(session_id2_len);
597: memcpy(session_id2, p, session_id2_len);
598: }
599:
1.127 markus 600: if ((key = get_hostkey_by_index(keyid)) != NULL) {
1.155 markus 601: if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
1.138 djm 602: datafellows)) != 0)
603: fatal("%s: sshkey_sign failed: %s",
604: __func__, ssh_err(r));
1.144 djm 605: } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
1.138 djm 606: auth_sock > 0) {
607: if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
1.155 markus 608: p, datlen, alg, datafellows)) != 0) {
1.138 djm 609: fatal("%s: ssh_agent_sign failed: %s",
610: __func__, ssh_err(r));
611: }
1.127 markus 612: } else
1.14 markus 613: fatal("%s: no hostkey from index %d", __func__, keyid);
1.1 provos 614:
1.144 djm 615: debug3("%s: %s signature %p(%zu)", __func__,
616: is_proof ? "KEX" : "hostkey proof", signature, siglen);
1.1 provos 617:
1.138 djm 618: sshbuf_reset(m);
619: if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
620: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.1 provos 621:
1.157 djm 622: free(alg);
1.124 djm 623: free(p);
624: free(signature);
1.1 provos 625:
1.59 avsm 626: mm_request_send(sock, MONITOR_ANS_SIGN, m);
1.1 provos 627:
628: /* Turn on permissions for getpwnam */
629: monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
630:
631: return (0);
632: }
633:
634: /* Retrieves the password entry and also checks if the user is permitted */
635:
636: int
1.59 avsm 637: mm_answer_pwnamallow(int sock, Buffer *m)
1.1 provos 638: {
1.167 djm 639: struct ssh *ssh = active_state; /* XXX */
1.60 dtucker 640: char *username;
1.1 provos 641: struct passwd *pwent;
642: int allowed = 0;
1.113 djm 643: u_int i;
1.3 markus 644:
1.14 markus 645: debug3("%s", __func__);
1.1 provos 646:
647: if (authctxt->attempt++ != 0)
1.14 markus 648: fatal("%s: multiple attempts for getpwnam", __func__);
1.1 provos 649:
1.60 dtucker 650: username = buffer_get_string(m, NULL);
1.1 provos 651:
1.60 dtucker 652: pwent = getpwnamallow(username);
1.1 provos 653:
1.60 dtucker 654: authctxt->user = xstrdup(username);
655: setproctitle("%s [priv]", pwent ? username : "unknown");
1.124 djm 656: free(username);
1.1 provos 657:
658: buffer_clear(m);
659:
660: if (pwent == NULL) {
661: buffer_put_char(m, 0);
1.53 djm 662: authctxt->pw = fakepw();
1.1 provos 663: goto out;
664: }
665:
666: allowed = 1;
1.4 markus 667: authctxt->pw = pwent;
1.1 provos 668: authctxt->valid = 1;
669:
670: buffer_put_char(m, 1);
671: buffer_put_string(m, pwent, sizeof(struct passwd));
672: buffer_put_cstring(m, pwent->pw_name);
673: buffer_put_cstring(m, "*");
674: buffer_put_cstring(m, pwent->pw_gecos);
675: buffer_put_cstring(m, pwent->pw_class);
676: buffer_put_cstring(m, pwent->pw_dir);
677: buffer_put_cstring(m, pwent->pw_shell);
1.94 dtucker 678:
679: out:
1.167 djm 680: ssh_packet_set_log_preamble(ssh, "%suser %s",
681: authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
1.90 dtucker 682: buffer_put_string(m, &options, sizeof(options));
1.112 djm 683:
684: #define M_CP_STROPT(x) do { \
685: if (options.x != NULL) \
686: buffer_put_cstring(m, options.x); \
687: } while (0)
1.113 djm 688: #define M_CP_STRARRAYOPT(x, nx) do { \
689: for (i = 0; i < options.nx; i++) \
690: buffer_put_cstring(m, options.x[i]); \
691: } while (0)
1.112 djm 692: /* See comment in servconf.h */
693: COPY_MATCH_STRING_OPTS();
694: #undef M_CP_STROPT
1.113 djm 695: #undef M_CP_STRARRAYOPT
1.118 djm 696:
697: /* Create valid auth method lists */
1.162 markus 698: if (auth2_setup_methods_lists(authctxt) != 0) {
1.118 djm 699: /*
700: * The monitor will continue long enough to let the child
701: * run to it's packet_disconnect(), but it must not allow any
702: * authentication to succeed.
703: */
704: debug("%s: no valid authentication method lists", __func__);
705: }
706:
1.14 markus 707: debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
1.59 avsm 708: mm_request_send(sock, MONITOR_ANS_PWNAM, m);
1.1 provos 709:
1.162 markus 710: /* Allow service/style information on the auth context */
711: monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
712: monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
1.10 djm 713:
714: return (0);
715: }
716:
1.59 avsm 717: int mm_answer_auth2_read_banner(int sock, Buffer *m)
1.10 djm 718: {
719: char *banner;
720:
721: buffer_clear(m);
722: banner = auth2_read_banner();
723: buffer_put_cstring(m, banner != NULL ? banner : "");
1.59 avsm 724: mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
1.124 djm 725: free(banner);
1.1 provos 726:
727: return (0);
728: }
729:
730: int
1.59 avsm 731: mm_answer_authserv(int sock, Buffer *m)
1.1 provos 732: {
733: monitor_permit_authentications(1);
734:
735: authctxt->service = buffer_get_string(m, NULL);
736: authctxt->style = buffer_get_string(m, NULL);
1.6 stevesk 737: debug3("%s: service=%s, style=%s",
1.14 markus 738: __func__, authctxt->service, authctxt->style);
1.6 stevesk 739:
1.1 provos 740: if (strlen(authctxt->style) == 0) {
1.124 djm 741: free(authctxt->style);
1.1 provos 742: authctxt->style = NULL;
743: }
744:
745: return (0);
746: }
747:
748: int
1.59 avsm 749: mm_answer_authpassword(int sock, Buffer *m)
1.1 provos 750: {
1.180 ! djm 751: struct ssh *ssh = active_state; /* XXX */
1.1 provos 752: static int call_count;
753: char *passwd;
1.22 stevesk 754: int authenticated;
755: u_int plen;
1.1 provos 756:
1.164 djm 757: if (!options.password_authentication)
758: fatal("%s: password authentication not enabled", __func__);
1.1 provos 759: passwd = buffer_get_string(m, &plen);
760: /* Only authenticate if the context is valid */
1.12 markus 761: authenticated = options.password_authentication &&
1.180 ! djm 762: auth_password(ssh, passwd);
1.131 djm 763: explicit_bzero(passwd, strlen(passwd));
1.124 djm 764: free(passwd);
1.1 provos 765:
766: buffer_clear(m);
767: buffer_put_int(m, authenticated);
768:
1.14 markus 769: debug3("%s: sending result %d", __func__, authenticated);
1.59 avsm 770: mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
1.1 provos 771:
772: call_count++;
773: if (plen == 0 && call_count == 1)
774: auth_method = "none";
775: else
776: auth_method = "password";
777:
778: /* Causes monitor loop to terminate if authenticated */
779: return (authenticated);
780: }
781:
782: int
1.59 avsm 783: mm_answer_bsdauthquery(int sock, Buffer *m)
1.1 provos 784: {
785: char *name, *infotxt;
786: u_int numprompts;
787: u_int *echo_on;
788: char **prompts;
1.31 markus 789: u_int success;
1.1 provos 790:
1.164 djm 791: if (!options.kbd_interactive_authentication)
792: fatal("%s: kbd-int authentication not enabled", __func__);
1.31 markus 793: success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
794: &prompts, &echo_on) < 0 ? 0 : 1;
1.1 provos 795:
796: buffer_clear(m);
1.31 markus 797: buffer_put_int(m, success);
798: if (success)
1.1 provos 799: buffer_put_cstring(m, prompts[0]);
800:
1.31 markus 801: debug3("%s: sending challenge success: %u", __func__, success);
1.59 avsm 802: mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
1.1 provos 803:
1.31 markus 804: if (success) {
1.124 djm 805: free(name);
806: free(infotxt);
807: free(prompts);
808: free(echo_on);
1.1 provos 809: }
810:
811: return (0);
812: }
813:
814: int
1.59 avsm 815: mm_answer_bsdauthrespond(int sock, Buffer *m)
1.1 provos 816: {
817: char *response;
818: int authok;
819:
1.164 djm 820: if (!options.kbd_interactive_authentication)
821: fatal("%s: kbd-int authentication not enabled", __func__);
1.154 mmcc 822: if (authctxt->as == NULL)
1.14 markus 823: fatal("%s: no bsd auth session", __func__);
1.1 provos 824:
825: response = buffer_get_string(m, NULL);
1.12 markus 826: authok = options.challenge_response_authentication &&
827: auth_userresponse(authctxt->as, response, 0);
1.1 provos 828: authctxt->as = NULL;
1.14 markus 829: debug3("%s: <%s> = <%d>", __func__, response, authok);
1.124 djm 830: free(response);
1.1 provos 831:
832: buffer_clear(m);
833: buffer_put_int(m, authok);
834:
1.14 markus 835: debug3("%s: sending authenticated: %d", __func__, authok);
1.59 avsm 836: mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
1.1 provos 837:
1.162 markus 838: auth_method = "keyboard-interactive";
839: auth_submethod = "bsdauth";
1.1 provos 840:
841: return (authok != 0);
842: }
843:
844: int
1.59 avsm 845: mm_answer_keyallowed(int sock, Buffer *m)
1.1 provos 846: {
1.180 ! djm 847: struct ssh *ssh = active_state; /* XXX */
1.168 markus 848: struct sshkey *key;
1.26 markus 849: char *cuser, *chost;
850: u_char *blob;
1.148 djm 851: u_int bloblen, pubkey_auth_attempt;
1.1 provos 852: enum mm_keytype type = 0;
1.180 ! djm 853: int r, allowed = 0;
! 854: struct sshauthopt *opts = NULL;
1.1 provos 855:
1.14 markus 856: debug3("%s entering", __func__);
1.1 provos 857: type = buffer_get_int(m);
858: cuser = buffer_get_string(m, NULL);
859: chost = buffer_get_string(m, NULL);
860: blob = buffer_get_string(m, &bloblen);
1.148 djm 861: pubkey_auth_attempt = buffer_get_int(m);
1.1 provos 862:
863: key = key_from_blob(blob, bloblen);
864:
1.14 markus 865: debug3("%s: key_from_blob: %p", __func__, key);
1.1 provos 866:
1.51 djm 867: if (key != NULL && authctxt->valid) {
1.137 djm 868: /* These should not make it past the privsep child */
869: if (key_type_plain(key->type) == KEY_RSA &&
870: (datafellows & SSH_BUG_RSASIGMD5) != 0)
871: fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
872:
1.63 deraadt 873: switch (type) {
1.1 provos 874: case MM_USERKEY:
1.77 dtucker 875: auth_method = "publickey";
1.180 ! djm 876: if (!options.pubkey_authentication)
! 877: break;
! 878: if (auth2_key_already_used(authctxt, key))
! 879: break;
! 880: if (match_pattern_list(sshkey_ssh_name(key),
! 881: options.pubkey_key_types, 0) != 1)
! 882: break;
! 883: allowed = user_key_allowed(ssh, authctxt->pw, key,
! 884: pubkey_auth_attempt, &opts);
1.1 provos 885: break;
886: case MM_HOSTKEY:
1.180 ! djm 887: auth_method = "hostbased";
! 888: if (!options.hostbased_authentication)
! 889: break;
! 890: if (auth2_key_already_used(authctxt, key))
! 891: break;
! 892: if (match_pattern_list(sshkey_ssh_name(key),
! 893: options.hostbased_key_types, 0) != 1)
! 894: break;
! 895: allowed = hostbased_key_allowed(authctxt->pw,
1.1 provos 896: cuser, chost, key);
1.172 djm 897: auth2_record_info(authctxt,
1.126 djm 898: "client user \"%.100s\", client host \"%.100s\"",
899: cuser, chost);
1.1 provos 900: break;
901: default:
1.14 markus 902: fatal("%s: unknown key type %d", __func__, type);
1.1 provos 903: break;
904: }
1.33 markus 905: }
1.161 djm 906:
1.180 ! djm 907: debug3("%s: %s authentication%s: %s key is %s", __func__,
! 908: auth_method, pubkey_auth_attempt ? "" : " test",
! 909: (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key),
! 910: allowed ? "allowed" : "not allowed");
1.161 djm 911:
1.172 djm 912: auth2_record_key(authctxt, 0, key);
913: sshkey_free(key);
1.1 provos 914:
915: /* clear temporarily storage (used by verify) */
916: monitor_reset_key_state();
917:
918: if (allowed) {
919: /* Save temporarily for comparison in verify */
920: key_blob = blob;
921: key_bloblen = bloblen;
922: key_blobtype = type;
1.180 ! djm 923: key_opts = opts;
1.1 provos 924: hostbased_cuser = cuser;
925: hostbased_chost = chost;
1.72 djm 926: } else {
1.77 dtucker 927: /* Log failed attempt */
1.125 djm 928: auth_log(authctxt, 0, 0, auth_method, NULL);
1.124 djm 929: free(blob);
930: free(cuser);
931: free(chost);
1.1 provos 932: }
933:
934: buffer_clear(m);
935: buffer_put_int(m, allowed);
1.180 ! djm 936: if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0)
! 937: fatal("%s: sshauthopt_serialise: %s", __func__, ssh_err(r));
! 938: mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1.1 provos 939:
1.180 ! djm 940: if (!allowed)
! 941: sshauthopt_free(opts);
1.1 provos 942:
943: return (0);
944: }
945:
946: static int
947: monitor_valid_userblob(u_char *data, u_int datalen)
948: {
949: Buffer b;
1.159 djm 950: u_char *p;
951: char *userstyle, *cp;
1.1 provos 952: u_int len;
953: int fail = 0;
954:
955: buffer_init(&b);
956: buffer_append(&b, data, datalen);
1.3 markus 957:
1.1 provos 958: if (datafellows & SSH_OLD_SESSIONID) {
1.13 markus 959: p = buffer_ptr(&b);
960: len = buffer_len(&b);
961: if ((session_id2 == NULL) ||
962: (len < session_id2_len) ||
1.108 djm 963: (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1.13 markus 964: fail++;
1.1 provos 965: buffer_consume(&b, session_id2_len);
966: } else {
1.13 markus 967: p = buffer_get_string(&b, &len);
968: if ((session_id2 == NULL) ||
969: (len != session_id2_len) ||
1.108 djm 970: (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1.1 provos 971: fail++;
1.124 djm 972: free(p);
1.1 provos 973: }
974: if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
975: fail++;
1.159 djm 976: cp = buffer_get_cstring(&b, NULL);
1.121 djm 977: xasprintf(&userstyle, "%s%s%s", authctxt->user,
978: authctxt->style ? ":" : "",
979: authctxt->style ? authctxt->style : "");
1.159 djm 980: if (strcmp(userstyle, cp) != 0) {
981: logit("wrong user name passed to monitor: "
982: "expected %s != %.100s", userstyle, cp);
1.1 provos 983: fail++;
984: }
1.124 djm 985: free(userstyle);
1.159 djm 986: free(cp);
1.1 provos 987: buffer_skip_string(&b);
1.178 djm 988: cp = buffer_get_cstring(&b, NULL);
989: if (strcmp("publickey", cp) != 0)
990: fail++;
991: free(cp);
992: if (!buffer_get_char(&b))
993: fail++;
994: buffer_skip_string(&b);
1.1 provos 995: buffer_skip_string(&b);
996: if (buffer_len(&b) != 0)
997: fail++;
998: buffer_free(&b);
999: return (fail == 0);
1000: }
1001:
1002: static int
1.26 markus 1003: monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1004: char *chost)
1.1 provos 1005: {
1006: Buffer b;
1.121 djm 1007: char *p, *userstyle;
1.1 provos 1008: u_int len;
1009: int fail = 0;
1010:
1011: buffer_init(&b);
1012: buffer_append(&b, data, datalen);
1.3 markus 1013:
1.13 markus 1014: p = buffer_get_string(&b, &len);
1015: if ((session_id2 == NULL) ||
1016: (len != session_id2_len) ||
1.108 djm 1017: (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1.1 provos 1018: fail++;
1.124 djm 1019: free(p);
1.13 markus 1020:
1.1 provos 1021: if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1022: fail++;
1.121 djm 1023: p = buffer_get_cstring(&b, NULL);
1024: xasprintf(&userstyle, "%s%s%s", authctxt->user,
1025: authctxt->style ? ":" : "",
1026: authctxt->style ? authctxt->style : "");
1027: if (strcmp(userstyle, p) != 0) {
1.38 itojun 1028: logit("wrong user name passed to monitor: expected %s != %.100s",
1.121 djm 1029: userstyle, p);
1.1 provos 1030: fail++;
1031: }
1.121 djm 1032: free(userstyle);
1.124 djm 1033: free(p);
1.1 provos 1034: buffer_skip_string(&b); /* service */
1.121 djm 1035: p = buffer_get_cstring(&b, NULL);
1.1 provos 1036: if (strcmp(p, "hostbased") != 0)
1037: fail++;
1.124 djm 1038: free(p);
1.1 provos 1039: buffer_skip_string(&b); /* pkalg */
1040: buffer_skip_string(&b); /* pkblob */
1041:
1042: /* verify client host, strip trailing dot if necessary */
1043: p = buffer_get_string(&b, NULL);
1044: if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1045: p[len - 1] = '\0';
1046: if (strcmp(p, chost) != 0)
1047: fail++;
1.124 djm 1048: free(p);
1.1 provos 1049:
1050: /* verify client user */
1051: p = buffer_get_string(&b, NULL);
1052: if (strcmp(p, cuser) != 0)
1053: fail++;
1.124 djm 1054: free(p);
1.1 provos 1055:
1056: if (buffer_len(&b) != 0)
1057: fail++;
1058: buffer_free(&b);
1059: return (fail == 0);
1060: }
1061:
1062: int
1.169 markus 1063: mm_answer_keyverify(int sock, struct sshbuf *m)
1.1 provos 1064: {
1.180 ! djm 1065: struct ssh *ssh = active_state; /* XXX */
1.168 markus 1066: struct sshkey *key;
1.1 provos 1067: u_char *signature, *data, *blob;
1.176 djm 1068: char *sigalg;
1.169 markus 1069: size_t signaturelen, datalen, bloblen;
1070: int r, ret, valid_data = 0, encoded_ret;
1.1 provos 1071:
1.169 markus 1072: if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
1073: (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 ||
1.176 djm 1074: (r = sshbuf_get_string(m, &data, &datalen)) != 0 ||
1075: (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
1.169 markus 1076: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.1 provos 1077:
1078: if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1.8 mouring 1079: !monitor_allowed_key(blob, bloblen))
1.14 markus 1080: fatal("%s: bad key, not previously allowed", __func__);
1.1 provos 1081:
1.177 djm 1082: /* Empty signature algorithm means NULL. */
1083: if (*sigalg == '\0') {
1084: free(sigalg);
1085: sigalg = NULL;
1086: }
1087:
1.169 markus 1088: /* XXX use sshkey_froms here; need to change key_blob, etc. */
1089: if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1090: fatal("%s: bad public key blob: %s", __func__, ssh_err(r));
1.1 provos 1091:
1092: switch (key_blobtype) {
1093: case MM_USERKEY:
1094: valid_data = monitor_valid_userblob(data, datalen);
1.172 djm 1095: auth_method = "publickey";
1.1 provos 1096: break;
1097: case MM_HOSTKEY:
1098: valid_data = monitor_valid_hostbasedblob(data, datalen,
1099: hostbased_cuser, hostbased_chost);
1.172 djm 1100: auth_method = "hostbased";
1.1 provos 1101: break;
1102: default:
1103: valid_data = 0;
1104: break;
1105: }
1106: if (!valid_data)
1.14 markus 1107: fatal("%s: bad signature data blob", __func__);
1.1 provos 1108:
1.169 markus 1109: ret = sshkey_verify(key, signature, signaturelen, data, datalen,
1.176 djm 1110: sigalg, active_state->compat);
1.172 djm 1111: debug3("%s: %s %p signature %s", __func__, auth_method, key,
1112: (ret == 0) ? "verified" : "unverified");
1113: auth2_record_key(authctxt, ret == 0, key);
1.136 djm 1114:
1.124 djm 1115: free(blob);
1116: free(signature);
1117: free(data);
1.177 djm 1118: free(sigalg);
1.1 provos 1119:
1.180 ! djm 1120: if (key_blobtype == MM_USERKEY)
! 1121: auth_activate_options(ssh, key_opts);
1.1 provos 1122: monitor_reset_key_state();
1.3 markus 1123:
1.172 djm 1124: sshkey_free(key);
1.169 markus 1125: sshbuf_reset(m);
1126:
1127: /* encode ret != 0 as positive integer, since we're sending u32 */
1128: encoded_ret = (ret != 0);
1129: if ((r = sshbuf_put_u32(m, encoded_ret)) != 0)
1130: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.59 avsm 1131: mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1.1 provos 1132:
1.169 markus 1133: return ret == 0;
1.1 provos 1134: }
1135:
1.2 markus 1136: static void
1.1 provos 1137: mm_record_login(Session *s, struct passwd *pw)
1138: {
1.158 djm 1139: struct ssh *ssh = active_state; /* XXX */
1.1 provos 1140: socklen_t fromlen;
1141: struct sockaddr_storage from;
1142:
1143: /*
1144: * Get IP address of client. If the connection is not a socket, let
1145: * the address be 0.0.0.0.
1146: */
1147: memset(&from, 0, sizeof(from));
1.24 stevesk 1148: fromlen = sizeof(from);
1.1 provos 1149: if (packet_connection_is_on_socket()) {
1150: if (getpeername(packet_get_connection_in(),
1.74 deraadt 1151: (struct sockaddr *)&from, &fromlen) < 0) {
1.1 provos 1152: debug("getpeername: %.100s", strerror(errno));
1.50 markus 1153: cleanup_exit(255);
1.1 provos 1154: }
1155: }
1156: /* Record that there was a login on that tty from the remote host. */
1157: record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1.158 djm 1158: session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
1.24 stevesk 1159: (struct sockaddr *)&from, fromlen);
1.1 provos 1160: }
1161:
1162: static void
1163: mm_session_close(Session *s)
1164: {
1.41 djm 1165: debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1.1 provos 1166: if (s->ttyfd != -1) {
1.86 stevesk 1167: debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1.1 provos 1168: session_pty_cleanup2(s);
1169: }
1.96 djm 1170: session_unused(s->self);
1.1 provos 1171: }
1172:
1173: int
1.59 avsm 1174: mm_answer_pty(int sock, Buffer *m)
1.1 provos 1175: {
1.11 mouring 1176: extern struct monitor *pmonitor;
1.1 provos 1177: Session *s;
1178: int res, fd0;
1179:
1.14 markus 1180: debug3("%s entering", __func__);
1.1 provos 1181:
1182: buffer_clear(m);
1183: s = session_new();
1184: if (s == NULL)
1185: goto error;
1186: s->authctxt = authctxt;
1187: s->pw = authctxt->pw;
1.11 mouring 1188: s->pid = pmonitor->m_pid;
1.1 provos 1189: res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1190: if (res == 0)
1191: goto error;
1192: pty_setowner(authctxt->pw, s->tty);
1193:
1194: buffer_put_int(m, 1);
1195: buffer_put_cstring(m, s->tty);
1196:
1197: /* We need to trick ttyslot */
1198: if (dup2(s->ttyfd, 0) == -1)
1.14 markus 1199: fatal("%s: dup2", __func__);
1.1 provos 1200:
1201: mm_record_login(s, authctxt->pw);
1202:
1203: /* Now we can close the file descriptor again */
1204: close(0);
1.61 dtucker 1205:
1206: /* send messages generated by record_login */
1207: buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1208: buffer_clear(&loginmsg);
1209:
1210: mm_request_send(sock, MONITOR_ANS_PTY, m);
1211:
1.92 djm 1212: if (mm_send_fd(sock, s->ptyfd) == -1 ||
1213: mm_send_fd(sock, s->ttyfd) == -1)
1214: fatal("%s: send fds failed", __func__);
1.1 provos 1215:
1216: /* make sure nothing uses fd 0 */
1217: if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1.14 markus 1218: fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1.1 provos 1219: if (fd0 != 0)
1.14 markus 1220: error("%s: fd0 %d != 0", __func__, fd0);
1.1 provos 1221:
1222: /* slave is not needed */
1223: close(s->ttyfd);
1224: s->ttyfd = s->ptyfd;
1225: /* no need to dup() because nobody closes ptyfd */
1226: s->ptymaster = s->ptyfd;
1227:
1.86 stevesk 1228: debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1.1 provos 1229:
1230: return (0);
1231:
1232: error:
1233: if (s != NULL)
1234: mm_session_close(s);
1235: buffer_put_int(m, 0);
1.59 avsm 1236: mm_request_send(sock, MONITOR_ANS_PTY, m);
1.1 provos 1237: return (0);
1238: }
1239:
1240: int
1.59 avsm 1241: mm_answer_pty_cleanup(int sock, Buffer *m)
1.1 provos 1242: {
1243: Session *s;
1244: char *tty;
1245:
1.14 markus 1246: debug3("%s entering", __func__);
1.1 provos 1247:
1248: tty = buffer_get_string(m, NULL);
1249: if ((s = session_by_tty(tty)) != NULL)
1250: mm_session_close(s);
1251: buffer_clear(m);
1.124 djm 1252: free(tty);
1.1 provos 1253: return (0);
1254: }
1255:
1256: int
1.59 avsm 1257: mm_answer_term(int sock, Buffer *req)
1.1 provos 1258: {
1.173 djm 1259: struct ssh *ssh = active_state; /* XXX */
1.11 mouring 1260: extern struct monitor *pmonitor;
1.1 provos 1261: int res, status;
1262:
1.14 markus 1263: debug3("%s: tearing down sessions", __func__);
1.1 provos 1264:
1265: /* The child is terminating */
1.173 djm 1266: session_destroy_all(ssh, &mm_session_close);
1.1 provos 1267:
1.11 mouring 1268: while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1.9 markus 1269: if (errno != EINTR)
1270: exit(1);
1.1 provos 1271:
1272: res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1273:
1274: /* Terminate process */
1.57 deraadt 1275: exit(res);
1.170 markus 1276: }
1277:
1278: void
1279: monitor_clear_keystate(struct monitor *pmonitor)
1280: {
1281: struct ssh *ssh = active_state; /* XXX */
1282:
1283: ssh_clear_newkeys(ssh, MODE_IN);
1284: ssh_clear_newkeys(ssh, MODE_OUT);
1285: sshbuf_free(child_state);
1286: child_state = NULL;
1.1 provos 1287: }
1288:
1289: void
1.11 mouring 1290: monitor_apply_keystate(struct monitor *pmonitor)
1.1 provos 1291: {
1.139 markus 1292: struct ssh *ssh = active_state; /* XXX */
1293: struct kex *kex;
1294: int r;
1295:
1296: debug3("%s: packet_set_state", __func__);
1297: if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1298: fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1299: sshbuf_free(child_state);
1300: child_state = NULL;
1301:
1.154 mmcc 1302: if ((kex = ssh->kex) != NULL) {
1.139 markus 1303: /* XXX set callbacks */
1.147 djm 1304: #ifdef WITH_OPENSSL
1.139 markus 1305: kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1306: kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1.160 djm 1307: kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
1308: kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
1309: kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
1.139 markus 1310: kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1311: kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1312: kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
1.147 djm 1313: #endif
1.139 markus 1314: kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1315: kex->load_host_public_key=&get_hostkey_public_by_type;
1316: kex->load_host_private_key=&get_hostkey_private_by_type;
1317: kex->host_key_index=&get_hostkey_index;
1318: kex->sign = sshd_hostkey_sign;
1319: }
1.1 provos 1320: }
1321:
1322: /* This function requries careful sanity checking */
1323:
1324: void
1.11 mouring 1325: mm_get_keystate(struct monitor *pmonitor)
1.1 provos 1326: {
1.14 markus 1327: debug3("%s: Waiting for new keys", __func__);
1.1 provos 1328:
1.139 markus 1329: if ((child_state = sshbuf_new()) == NULL)
1330: fatal("%s: sshbuf_new failed", __func__);
1331: mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1332: child_state);
1333: debug3("%s: GOT new keys", __func__);
1.1 provos 1334: }
1335:
1336:
1337: /* XXX */
1338:
1339: #define FD_CLOSEONEXEC(x) do { \
1.111 djm 1340: if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
1.1 provos 1341: fatal("fcntl(%d, F_SETFD)", x); \
1342: } while (0)
1343:
1.2 markus 1344: static void
1.114 djm 1345: monitor_openfds(struct monitor *mon, int do_logfds)
1.3 markus 1346: {
1.114 djm 1347: int pair[2];
1.171 markus 1348: #ifdef SO_ZEROIZE
1349: int on = 1;
1350: #endif
1.114 djm 1351:
1.1 provos 1352: if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1.114 djm 1353: fatal("%s: socketpair: %s", __func__, strerror(errno));
1.171 markus 1354: #ifdef SO_ZEROIZE
1355: if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1356: error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
1357: if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1358: error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
1359: #endif
1.1 provos 1360: FD_CLOSEONEXEC(pair[0]);
1361: FD_CLOSEONEXEC(pair[1]);
1.114 djm 1362: mon->m_recvfd = pair[0];
1363: mon->m_sendfd = pair[1];
1364:
1365: if (do_logfds) {
1366: if (pipe(pair) == -1)
1367: fatal("%s: pipe: %s", __func__, strerror(errno));
1368: FD_CLOSEONEXEC(pair[0]);
1369: FD_CLOSEONEXEC(pair[1]);
1370: mon->m_log_recvfd = pair[0];
1371: mon->m_log_sendfd = pair[1];
1372: } else
1373: mon->m_log_recvfd = mon->m_log_sendfd = -1;
1.1 provos 1374: }
1375:
1376: #define MM_MEMSIZE 65536
1377:
1378: struct monitor *
1379: monitor_init(void)
1380: {
1381: struct monitor *mon;
1382:
1.75 djm 1383: mon = xcalloc(1, sizeof(*mon));
1.114 djm 1384: monitor_openfds(mon, 1);
1.1 provos 1385:
1386: return mon;
1387: }
1388:
1389: void
1390: monitor_reinit(struct monitor *mon)
1391: {
1.114 djm 1392: monitor_openfds(mon, 0);
1.1 provos 1393: }
1.46 markus 1394:
1395: #ifdef GSSAPI
1396: int
1.59 avsm 1397: mm_answer_gss_setup_ctx(int sock, Buffer *m)
1.46 markus 1398: {
1.59 avsm 1399: gss_OID_desc goid;
1.46 markus 1400: OM_uint32 major;
1401: u_int len;
1402:
1.164 djm 1403: if (!options.gss_authentication)
1404: fatal("%s: GSSAPI authentication not enabled", __func__);
1405:
1.59 avsm 1406: goid.elements = buffer_get_string(m, &len);
1407: goid.length = len;
1.46 markus 1408:
1.59 avsm 1409: major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1.46 markus 1410:
1.124 djm 1411: free(goid.elements);
1.46 markus 1412:
1413: buffer_clear(m);
1414: buffer_put_int(m, major);
1415:
1.64 stevesk 1416: mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1.46 markus 1417:
1418: /* Now we have a context, enable the step */
1419: monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1420:
1421: return (0);
1422: }
1423:
1424: int
1.59 avsm 1425: mm_answer_gss_accept_ctx(int sock, Buffer *m)
1.46 markus 1426: {
1427: gss_buffer_desc in;
1428: gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1.64 stevesk 1429: OM_uint32 major, minor;
1.46 markus 1430: OM_uint32 flags = 0; /* GSI needs this */
1.47 deraadt 1431: u_int len;
1.46 markus 1432:
1.164 djm 1433: if (!options.gss_authentication)
1434: fatal("%s: GSSAPI authentication not enabled", __func__);
1435:
1.47 deraadt 1436: in.value = buffer_get_string(m, &len);
1437: in.length = len;
1.46 markus 1438: major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1.124 djm 1439: free(in.value);
1.46 markus 1440:
1441: buffer_clear(m);
1442: buffer_put_int(m, major);
1443: buffer_put_string(m, out.value, out.length);
1444: buffer_put_int(m, flags);
1.59 avsm 1445: mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1.46 markus 1446:
1447: gss_release_buffer(&minor, &out);
1448:
1.64 stevesk 1449: if (major == GSS_S_COMPLETE) {
1.46 markus 1450: monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1451: monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1.52 markus 1452: monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1.46 markus 1453: }
1454: return (0);
1455: }
1456:
1457: int
1.59 avsm 1458: mm_answer_gss_checkmic(int sock, Buffer *m)
1.52 markus 1459: {
1460: gss_buffer_desc gssbuf, mic;
1461: OM_uint32 ret;
1462: u_int len;
1.54 djm 1463:
1.164 djm 1464: if (!options.gss_authentication)
1465: fatal("%s: GSSAPI authentication not enabled", __func__);
1466:
1.52 markus 1467: gssbuf.value = buffer_get_string(m, &len);
1468: gssbuf.length = len;
1469: mic.value = buffer_get_string(m, &len);
1470: mic.length = len;
1.54 djm 1471:
1.52 markus 1472: ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1.54 djm 1473:
1.124 djm 1474: free(gssbuf.value);
1475: free(mic.value);
1.54 djm 1476:
1.52 markus 1477: buffer_clear(m);
1478: buffer_put_int(m, ret);
1.54 djm 1479:
1.59 avsm 1480: mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1.54 djm 1481:
1.52 markus 1482: if (!GSS_ERROR(ret))
1483: monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1.54 djm 1484:
1.52 markus 1485: return (0);
1486: }
1487:
1488: int
1.59 avsm 1489: mm_answer_gss_userok(int sock, Buffer *m)
1.46 markus 1490: {
1491: int authenticated;
1.172 djm 1492: const char *displayname;
1.164 djm 1493:
1494: if (!options.gss_authentication)
1495: fatal("%s: GSSAPI authentication not enabled", __func__);
1.46 markus 1496:
1497: authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1498:
1499: buffer_clear(m);
1500: buffer_put_int(m, authenticated);
1501:
1502: debug3("%s: sending result %d", __func__, authenticated);
1.59 avsm 1503: mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1.46 markus 1504:
1.64 stevesk 1505: auth_method = "gssapi-with-mic";
1.172 djm 1506:
1507: if ((displayname = ssh_gssapi_displayname()) != NULL)
1508: auth2_record_info(authctxt, "%s", displayname);
1.46 markus 1509:
1510: /* Monitor loop will terminate if authenticated */
1511: return (authenticated);
1512: }
1513: #endif /* GSSAPI */
1.100 djm 1514: