Annotation of src/usr.bin/ssh/monitor.c, Revision 1.236
1.236 ! dtucker 1: /* $OpenBSD: monitor.c,v 1.235 2023/02/17 04:22:50 dtucker Exp $ */
1.1 provos 2: /*
3: * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4: * Copyright 2002 Markus Friedl <markus@openbsd.org>
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: *
16: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26: */
27:
1.67 stevesk 28: #include <sys/types.h>
29: #include <sys/wait.h>
1.79 stevesk 30: #include <sys/socket.h>
1.85 deraadt 31: #include <sys/tree.h>
1.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.186 djm 50: #include <unistd.h>
1.1 provos 51:
1.114 djm 52: #include "atomicio.h"
1.85 deraadt 53: #include "xmalloc.h"
1.1 provos 54: #include "ssh.h"
1.185 markus 55: #include "sshkey.h"
1.183 markus 56: #include "sshbuf.h"
1.85 deraadt 57: #include "hostfile.h"
1.1 provos 58: #include "auth.h"
1.85 deraadt 59: #include "cipher.h"
1.1 provos 60: #include "kex.h"
61: #include "dh.h"
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.203 djm 83: #include "sk-api.h"
1.189 djm 84:
1.46 markus 85: #ifdef GSSAPI
86: static Gssctxt *gsscontext = NULL;
87: #endif
88:
1.1 provos 89: /* Imports */
90: extern ServerOptions options;
91: extern u_int utmp_len;
1.181 markus 92: extern struct sshbuf *loginmsg;
1.180 djm 93: extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */
1.1 provos 94:
95: /* State exported from the child */
1.139 markus 96: static struct sshbuf *child_state;
1.1 provos 97:
1.43 markus 98: /* Functions on the monitor that answer unprivileged requests */
1.1 provos 99:
1.191 djm 100: int mm_answer_moduli(struct ssh *, int, struct sshbuf *);
101: int mm_answer_sign(struct ssh *, int, struct sshbuf *);
102: int mm_answer_pwnamallow(struct ssh *, int, struct sshbuf *);
103: int mm_answer_auth2_read_banner(struct ssh *, int, struct sshbuf *);
104: int mm_answer_authserv(struct ssh *, int, struct sshbuf *);
105: int mm_answer_authpassword(struct ssh *, int, struct sshbuf *);
106: int mm_answer_bsdauthquery(struct ssh *, int, struct sshbuf *);
107: int mm_answer_bsdauthrespond(struct ssh *, int, struct sshbuf *);
108: int mm_answer_keyallowed(struct ssh *, int, struct sshbuf *);
109: int mm_answer_keyverify(struct ssh *, int, struct sshbuf *);
110: int mm_answer_pty(struct ssh *, int, struct sshbuf *);
111: int mm_answer_pty_cleanup(struct ssh *, int, struct sshbuf *);
112: int mm_answer_term(struct ssh *, int, struct sshbuf *);
113: int mm_answer_sesskey(struct ssh *, int, struct sshbuf *);
114: int mm_answer_sessid(struct ssh *, int, struct sshbuf *);
1.1 provos 115:
1.46 markus 116: #ifdef GSSAPI
1.191 djm 117: int mm_answer_gss_setup_ctx(struct ssh *, int, struct sshbuf *);
118: int mm_answer_gss_accept_ctx(struct ssh *, int, struct sshbuf *);
119: int mm_answer_gss_userok(struct ssh *, int, struct sshbuf *);
120: int mm_answer_gss_checkmic(struct ssh *, int, struct sshbuf *);
1.46 markus 121: #endif
1.25 itojun 122:
1.1 provos 123: static Authctxt *authctxt;
1.132 markus 124:
1.1 provos 125: /* local state for key verify */
126: static u_char *key_blob = NULL;
1.183 markus 127: static size_t key_bloblen = 0;
1.224 djm 128: static u_int key_blobtype = MM_NOKEY;
1.180 djm 129: static struct sshauthopt *key_opts = NULL;
1.26 markus 130: static char *hostbased_cuser = NULL;
131: static char *hostbased_chost = NULL;
1.1 provos 132: static char *auth_method = "unknown";
1.119 djm 133: static char *auth_submethod = NULL;
1.44 markus 134: static u_int session_id2_len = 0;
1.13 markus 135: static u_char *session_id2 = NULL;
1.40 markus 136: static pid_t monitor_child_pid;
1.1 provos 137:
138: struct mon_table {
139: enum monitor_reqtype type;
140: int flags;
1.191 djm 141: int (*f)(struct ssh *, int, struct sshbuf *);
1.1 provos 142: };
143:
144: #define MON_ISAUTH 0x0004 /* Required for Authentication */
145: #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
146: #define MON_ONCE 0x0010 /* Disable after calling */
1.77 dtucker 147: #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
1.1 provos 148:
149: #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
150:
151: #define MON_PERMIT 0x1000 /* Request is permitted */
152:
1.191 djm 153: static int monitor_read(struct ssh *, struct monitor *, struct mon_table *,
154: struct mon_table **);
155: static int monitor_read_log(struct monitor *);
156:
1.1 provos 157: struct mon_table mon_dispatch_proto20[] = {
1.132 markus 158: #ifdef WITH_OPENSSL
1.1 provos 159: {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
1.132 markus 160: #endif
1.1 provos 161: {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
162: {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
163: {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
1.10 djm 164: {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
1.1 provos 165: {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
166: {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
1.66 stevesk 167: {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
1.1 provos 168: {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
169: {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
1.46 markus 170: #ifdef GSSAPI
171: {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
1.165 djm 172: {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
173: {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
174: {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
1.46 markus 175: #endif
1.1 provos 176: {0, 0, NULL}
177: };
178:
179: struct mon_table mon_dispatch_postauth20[] = {
1.132 markus 180: #ifdef WITH_OPENSSL
1.1 provos 181: {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
1.132 markus 182: #endif
1.1 provos 183: {MONITOR_REQ_SIGN, 0, mm_answer_sign},
184: {MONITOR_REQ_PTY, 0, mm_answer_pty},
185: {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
186: {MONITOR_REQ_TERM, 0, mm_answer_term},
187: {0, 0, NULL}
188: };
189:
190: struct mon_table *mon_dispatch;
191:
192: /* Specifies if a certain message is allowed at the moment */
193: static void
194: monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
195: {
196: while (ent->f != NULL) {
197: if (ent->type == type) {
198: ent->flags &= ~MON_PERMIT;
199: ent->flags |= permit ? MON_PERMIT : 0;
200: return;
201: }
202: ent++;
203: }
204: }
205:
206: static void
207: monitor_permit_authentications(int permit)
208: {
209: struct mon_table *ent = mon_dispatch;
210:
211: while (ent->f != NULL) {
212: if (ent->flags & MON_AUTH) {
213: ent->flags &= ~MON_PERMIT;
214: ent->flags |= permit ? MON_PERMIT : 0;
215: }
216: ent++;
217: }
218: }
219:
1.50 markus 220: void
1.191 djm 221: monitor_child_preauth(struct ssh *ssh, struct monitor *pmonitor)
1.1 provos 222: {
223: struct mon_table *ent;
1.119 djm 224: int authenticated = 0, partial = 0;
1.1 provos 225:
226: debug3("preauth child monitor started");
227:
1.179 tb 228: if (pmonitor->m_recvfd >= 0)
229: close(pmonitor->m_recvfd);
230: if (pmonitor->m_log_sendfd >= 0)
231: close(pmonitor->m_log_sendfd);
1.114 djm 232: pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
233:
1.191 djm 234: authctxt = (Authctxt *)ssh->authctxt;
1.50 markus 235: memset(authctxt, 0, sizeof(*authctxt));
1.180 djm 236: ssh->authctxt = authctxt;
1.50 markus 237:
1.162 markus 238: mon_dispatch = mon_dispatch_proto20;
239: /* Permit requests for moduli and signatures */
240: monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
241: monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
1.1 provos 242:
243: /* The first few requests do not require asynchronous access */
244: while (!authenticated) {
1.119 djm 245: partial = 0;
1.77 dtucker 246: auth_method = "unknown";
1.119 djm 247: auth_submethod = NULL;
1.172 djm 248: auth2_authctxt_reset_info(authctxt);
249:
1.191 djm 250: authenticated = (monitor_read(ssh, pmonitor,
251: mon_dispatch, &ent) == 1);
1.118 djm 252:
253: /* Special handling for multiple required authentications */
254: if (options.num_auth_methods != 0) {
255: if (authenticated &&
256: !auth2_update_methods_lists(authctxt,
1.122 markus 257: auth_method, auth_submethod)) {
1.217 djm 258: debug3_f("method %s: partial", auth_method);
1.118 djm 259: authenticated = 0;
1.119 djm 260: partial = 1;
1.118 djm 261: }
262: }
263:
1.1 provos 264: if (authenticated) {
265: if (!(ent->flags & MON_AUTHDECIDE))
1.217 djm 266: fatal_f("unexpected authentication from %d",
267: ent->type);
1.1 provos 268: if (authctxt->pw->pw_uid == 0 &&
1.180 djm 269: !auth_root_allowed(ssh, auth_method))
1.1 provos 270: authenticated = 0;
271: }
1.77 dtucker 272: if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
1.190 djm 273: auth_log(ssh, authenticated, partial,
1.125 djm 274: auth_method, auth_submethod);
1.150 djm 275: if (!partial && !authenticated)
1.1 provos 276: authctxt->failures++;
1.172 djm 277: if (authenticated || partial) {
278: auth2_update_session_info(authctxt,
279: auth_method, auth_submethod);
280: }
1.1 provos 281: }
282: }
283:
284: if (!authctxt->valid)
1.217 djm 285: fatal_f("authenticated invalid user");
1.77 dtucker 286: if (strcmp(auth_method, "unknown") == 0)
1.217 djm 287: fatal_f("authentication method name unknown");
1.1 provos 288:
1.217 djm 289: debug_f("user %s authenticated by privileged process", authctxt->user);
1.180 djm 290: ssh->authctxt = NULL;
1.167 djm 291: ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
1.1 provos 292:
1.192 djm 293: mm_get_keystate(ssh, pmonitor);
1.120 markus 294:
295: /* Drain any buffered messages from the child */
296: while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
297: ;
1.114 djm 298:
1.179 tb 299: if (pmonitor->m_recvfd >= 0)
300: close(pmonitor->m_recvfd);
301: if (pmonitor->m_log_sendfd >= 0)
302: close(pmonitor->m_log_sendfd);
1.114 djm 303: pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
1.1 provos 304: }
305:
1.40 markus 306: static void
307: monitor_set_child_handler(pid_t pid)
308: {
309: monitor_child_pid = pid;
310: }
311:
312: static void
1.59 avsm 313: monitor_child_handler(int sig)
1.40 markus 314: {
1.59 avsm 315: kill(monitor_child_pid, sig);
1.40 markus 316: }
317:
1.1 provos 318: void
1.191 djm 319: monitor_child_postauth(struct ssh *ssh, struct monitor *pmonitor)
1.1 provos 320: {
1.114 djm 321: close(pmonitor->m_recvfd);
322: pmonitor->m_recvfd = -1;
323:
1.40 markus 324: monitor_set_child_handler(pmonitor->m_pid);
1.207 dtucker 325: ssh_signal(SIGHUP, &monitor_child_handler);
326: ssh_signal(SIGTERM, &monitor_child_handler);
327: ssh_signal(SIGINT, &monitor_child_handler);
1.40 markus 328:
1.162 markus 329: mon_dispatch = mon_dispatch_postauth20;
330:
331: /* Permit requests for moduli and signatures */
332: monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
333: monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
334: monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
1.1 provos 335:
1.180 djm 336: if (auth_opts->permit_pty_flag) {
1.1 provos 337: monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
338: monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
339: }
340:
341: for (;;)
1.191 djm 342: monitor_read(ssh, pmonitor, mon_dispatch, NULL);
1.1 provos 343: }
344:
1.114 djm 345: static int
346: monitor_read_log(struct monitor *pmonitor)
347: {
1.183 markus 348: struct sshbuf *logmsg;
1.225 markus 349: u_int len, level, forced;
350: char *msg;
1.183 markus 351: u_char *p;
352: int r;
1.114 djm 353:
1.183 markus 354: if ((logmsg = sshbuf_new()) == NULL)
1.217 djm 355: fatal_f("sshbuf_new");
1.114 djm 356:
357: /* Read length */
1.183 markus 358: if ((r = sshbuf_reserve(logmsg, 4, &p)) != 0)
1.217 djm 359: fatal_fr(r, "reserve len");
1.183 markus 360: if (atomicio(read, pmonitor->m_log_recvfd, p, 4) != 4) {
1.114 djm 361: if (errno == EPIPE) {
1.183 markus 362: sshbuf_free(logmsg);
1.217 djm 363: debug_f("child log fd closed");
1.114 djm 364: close(pmonitor->m_log_recvfd);
365: pmonitor->m_log_recvfd = -1;
366: return -1;
367: }
1.217 djm 368: fatal_f("log fd read: %s", strerror(errno));
1.114 djm 369: }
1.183 markus 370: if ((r = sshbuf_get_u32(logmsg, &len)) != 0)
1.217 djm 371: fatal_fr(r, "parse len");
1.114 djm 372: if (len <= 4 || len > 8192)
1.217 djm 373: fatal_f("invalid log message length %u", len);
1.114 djm 374:
375: /* Read severity, message */
1.183 markus 376: sshbuf_reset(logmsg);
377: if ((r = sshbuf_reserve(logmsg, len, &p)) != 0)
1.217 djm 378: fatal_fr(r, "reserve msg");
1.183 markus 379: if (atomicio(read, pmonitor->m_log_recvfd, p, len) != len)
1.217 djm 380: fatal_f("log fd read: %s", strerror(errno));
1.225 markus 381: if ((r = sshbuf_get_u32(logmsg, &level)) != 0 ||
382: (r = sshbuf_get_u32(logmsg, &forced)) != 0 ||
1.183 markus 383: (r = sshbuf_get_cstring(logmsg, &msg, NULL)) != 0)
1.217 djm 384: fatal_fr(r, "parse");
1.114 djm 385:
386: /* Log it */
387: if (log_level_name(level) == NULL)
1.216 djm 388: fatal_f("invalid log level %u (corrupted message?)", level);
1.225 markus 389: sshlogdirect(level, forced, "%s [preauth]", msg);
1.114 djm 390:
1.183 markus 391: sshbuf_free(logmsg);
1.124 djm 392: free(msg);
1.114 djm 393:
394: return 0;
395: }
396:
1.191 djm 397: static int
398: monitor_read(struct ssh *ssh, struct monitor *pmonitor, struct mon_table *ent,
1.1 provos 399: struct mon_table **pent)
400: {
1.183 markus 401: struct sshbuf *m;
402: int r, ret;
1.1 provos 403: u_char type;
1.114 djm 404: struct pollfd pfd[2];
405:
406: for (;;) {
1.130 tedu 407: memset(&pfd, 0, sizeof(pfd));
1.114 djm 408: pfd[0].fd = pmonitor->m_sendfd;
409: pfd[0].events = POLLIN;
410: pfd[1].fd = pmonitor->m_log_recvfd;
411: pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
1.115 djm 412: if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
413: if (errno == EINTR || errno == EAGAIN)
414: continue;
1.217 djm 415: fatal_f("poll: %s", strerror(errno));
1.115 djm 416: }
1.114 djm 417: if (pfd[1].revents) {
418: /*
419: * Drain all log messages before processing next
420: * monitor request.
421: */
422: monitor_read_log(pmonitor);
423: continue;
424: }
425: if (pfd[0].revents)
426: break; /* Continues below */
427: }
1.1 provos 428:
1.183 markus 429: if ((m = sshbuf_new()) == NULL)
1.217 djm 430: fatal_f("sshbuf_new");
1.1 provos 431:
1.183 markus 432: mm_request_receive(pmonitor->m_sendfd, m);
433: if ((r = sshbuf_get_u8(m, &type)) != 0)
1.217 djm 434: fatal_fr(r, "parse type");
1.1 provos 435:
1.217 djm 436: debug3_f("checking request %d", 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.217 djm 446: fatal_f("unpermitted request %d", type);
1.191 djm 447: ret = (*ent->f)(ssh, pmonitor->m_sendfd, m);
1.183 markus 448: sshbuf_free(m);
1.1 provos 449:
450: /* The child may use this request only once, disable it */
451: if (ent->flags & MON_ONCE) {
1.217 djm 452: debug2_f("%d used once, disabling now", type);
1.1 provos 453: ent->flags &= ~MON_PERMIT;
454: }
455:
456: if (pent != NULL)
457: *pent = ent;
1.3 markus 458:
1.1 provos 459: return ret;
460: }
461:
1.217 djm 462: fatal_f("unsupported request: %d", type);
1.1 provos 463:
464: /* NOTREACHED */
465: return (-1);
466: }
467:
468: /* allowed key state */
469: static int
1.203 djm 470: monitor_allowed_key(const u_char *blob, u_int bloblen)
1.1 provos 471: {
472: /* make sure key is allowed */
473: if (key_blob == NULL || key_bloblen != bloblen ||
1.108 djm 474: timingsafe_bcmp(key_blob, blob, key_bloblen))
1.1 provos 475: return (0);
476: return (1);
477: }
478:
479: static void
480: monitor_reset_key_state(void)
481: {
482: /* reset state */
1.124 djm 483: free(key_blob);
484: free(hostbased_cuser);
485: free(hostbased_chost);
1.180 djm 486: sshauthopt_free(key_opts);
1.1 provos 487: key_blob = NULL;
488: key_bloblen = 0;
489: key_blobtype = MM_NOKEY;
1.180 djm 490: key_opts = NULL;
1.1 provos 491: hostbased_cuser = NULL;
492: hostbased_chost = NULL;
493: }
494:
1.132 markus 495: #ifdef WITH_OPENSSL
1.1 provos 496: int
1.191 djm 497: mm_answer_moduli(struct ssh *ssh, int sock, struct sshbuf *m)
1.1 provos 498: {
499: DH *dh;
1.187 djm 500: const BIGNUM *dh_p, *dh_g;
1.183 markus 501: int r;
502: u_int min, want, max;
1.1 provos 503:
1.183 markus 504: if ((r = sshbuf_get_u32(m, &min)) != 0 ||
505: (r = sshbuf_get_u32(m, &want)) != 0 ||
506: (r = sshbuf_get_u32(m, &max)) != 0)
1.217 djm 507: fatal_fr(r, "parse");
1.1 provos 508:
1.217 djm 509: debug3_f("got parameters: %d %d %d", 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)
1.217 djm 512: fatal_f("bad parameters: %d %d %d", min, want, max);
1.1 provos 513:
1.183 markus 514: sshbuf_reset(m);
1.1 provos 515:
516: dh = choose_dh(min, want, max);
517: if (dh == NULL) {
1.183 markus 518: if ((r = sshbuf_put_u8(m, 0)) != 0)
1.217 djm 519: fatal_fr(r, "assemble empty");
1.1 provos 520: return (0);
521: } else {
522: /* Send first bignum */
1.187 djm 523: DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
1.183 markus 524: if ((r = sshbuf_put_u8(m, 1)) != 0 ||
1.187 djm 525: (r = sshbuf_put_bignum2(m, dh_p)) != 0 ||
526: (r = sshbuf_put_bignum2(m, dh_g)) != 0)
1.217 djm 527: fatal_fr(r, "assemble");
1.3 markus 528:
1.1 provos 529: DH_free(dh);
530: }
1.59 avsm 531: mm_request_send(sock, MONITOR_ANS_MODULI, m);
1.1 provos 532: return (0);
533: }
1.132 markus 534: #endif
1.1 provos 535:
536: int
1.191 djm 537: mm_answer_sign(struct ssh *ssh, int sock, struct sshbuf *m)
1.1 provos 538: {
1.138 djm 539: extern int auth_sock; /* XXX move to state struct? */
540: struct sshkey *key;
1.157 djm 541: struct sshbuf *sigbuf = NULL;
542: u_char *p = NULL, *signature = NULL;
543: char *alg = NULL;
1.155 markus 544: size_t datlen, siglen, alglen;
1.159 djm 545: int r, is_proof = 0;
1.185 markus 546: u_int keyid, compat;
1.145 djm 547: const char proof_req[] = "hostkeys-prove-00@openssh.com";
1.3 markus 548:
1.217 djm 549: debug3_f("entering");
1.1 provos 550:
1.138 djm 551: if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
1.155 markus 552: (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
1.185 markus 553: (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0 ||
554: (r = sshbuf_get_u32(m, &compat)) != 0)
1.217 djm 555: fatal_fr(r, "parse");
1.159 djm 556: if (keyid > INT_MAX)
1.217 djm 557: fatal_f("invalid key ID");
1.1 provos 558:
1.69 djm 559: /*
1.110 djm 560: * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
561: * SHA384 (48 bytes) and SHA512 (64 bytes).
1.144 djm 562: *
563: * Otherwise, verify the signature request is for a hostkey
564: * proof.
565: *
566: * XXX perform similar check for KEX signature requests too?
567: * it's not trivial, since what is signed is the hash, rather
568: * than the full kex structure...
1.69 djm 569: */
1.144 djm 570: if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
571: /*
572: * Construct expected hostkey proof and compare it to what
573: * the client sent us.
574: */
575: if (session_id2_len == 0) /* hostkeys is never first */
1.217 djm 576: fatal_f("bad data length: %zu", datlen);
1.144 djm 577: if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
1.217 djm 578: fatal_f("no hostkey for index %d", keyid);
1.144 djm 579: if ((sigbuf = sshbuf_new()) == NULL)
1.217 djm 580: fatal_f("sshbuf_new");
1.145 djm 581: if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
582: (r = sshbuf_put_string(sigbuf, session_id2,
1.152 jsg 583: session_id2_len)) != 0 ||
1.144 djm 584: (r = sshkey_puts(key, sigbuf)) != 0)
1.217 djm 585: fatal_fr(r, "assemble private key proof");
1.144 djm 586: if (datlen != sshbuf_len(sigbuf) ||
587: memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
1.217 djm 588: fatal_f("bad data length: %zu, hostkey proof len %zu",
589: datlen, sshbuf_len(sigbuf));
1.144 djm 590: sshbuf_free(sigbuf);
591: is_proof = 1;
592: }
1.1 provos 593:
1.13 markus 594: /* save session id, it will be passed on the first call */
595: if (session_id2_len == 0) {
596: session_id2_len = datlen;
597: session_id2 = xmalloc(session_id2_len);
598: memcpy(session_id2, p, session_id2_len);
599: }
600:
1.127 markus 601: if ((key = get_hostkey_by_index(keyid)) != NULL) {
1.155 markus 602: if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
1.213 djm 603: options.sk_provider, NULL, compat)) != 0)
1.217 djm 604: fatal_fr(r, "sign");
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.217 djm 608: p, datlen, alg, compat)) != 0)
609: fatal_fr(r, "agent sign");
1.127 markus 610: } else
1.217 djm 611: fatal_f("no hostkey from index %d", keyid);
1.1 provos 612:
1.228 djm 613: debug3_f("%s %s signature len=%zu", alg,
614: is_proof ? "hostkey proof" : "KEX", siglen);
1.1 provos 615:
1.138 djm 616: sshbuf_reset(m);
617: if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
1.217 djm 618: fatal_fr(r, "assemble");
1.1 provos 619:
1.157 djm 620: free(alg);
1.124 djm 621: free(p);
622: free(signature);
1.1 provos 623:
1.59 avsm 624: mm_request_send(sock, MONITOR_ANS_SIGN, m);
1.1 provos 625:
626: /* Turn on permissions for getpwnam */
627: monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
628:
629: return (0);
630: }
631:
1.218 djm 632: #define PUTPW(b, id) \
633: do { \
634: if ((r = sshbuf_put_string(b, \
635: &pwent->id, sizeof(pwent->id))) != 0) \
636: fatal_fr(r, "assemble %s", #id); \
637: } while (0)
638:
1.1 provos 639: /* Retrieves the password entry and also checks if the user is permitted */
640: int
1.191 djm 641: mm_answer_pwnamallow(struct ssh *ssh, int sock, struct sshbuf *m)
1.1 provos 642: {
643: struct passwd *pwent;
1.183 markus 644: int r, allowed = 0;
1.113 djm 645: u_int i;
1.3 markus 646:
1.217 djm 647: debug3_f("entering");
1.1 provos 648:
649: if (authctxt->attempt++ != 0)
1.217 djm 650: fatal_f("multiple attempts for getpwnam");
1.1 provos 651:
1.232 djm 652: if ((r = sshbuf_get_cstring(m, &authctxt->user, NULL)) != 0)
1.217 djm 653: fatal_fr(r, "parse");
1.1 provos 654:
1.232 djm 655: pwent = getpwnamallow(ssh, authctxt->user);
1.1 provos 656:
1.232 djm 657: setproctitle("%s [priv]", pwent ? authctxt->user : "unknown");
1.1 provos 658:
1.183 markus 659: sshbuf_reset(m);
1.1 provos 660:
661: if (pwent == NULL) {
1.183 markus 662: if ((r = sshbuf_put_u8(m, 0)) != 0)
1.217 djm 663: fatal_fr(r, "assemble fakepw");
1.53 djm 664: authctxt->pw = fakepw();
1.1 provos 665: goto out;
666: }
667:
668: allowed = 1;
1.4 markus 669: authctxt->pw = pwent;
1.1 provos 670: authctxt->valid = 1;
671:
1.218 djm 672: /* XXX send fake class/dir/shell, etc. */
673: if ((r = sshbuf_put_u8(m, 1)) != 0)
674: fatal_fr(r, "assemble ok");
675: PUTPW(m, pw_uid);
676: PUTPW(m, pw_gid);
677: PUTPW(m, pw_change);
678: PUTPW(m, pw_expire);
679: if ((r = sshbuf_put_cstring(m, pwent->pw_name)) != 0 ||
1.183 markus 680: (r = sshbuf_put_cstring(m, "*")) != 0 ||
681: (r = sshbuf_put_cstring(m, pwent->pw_gecos)) != 0 ||
682: (r = sshbuf_put_cstring(m, pwent->pw_class)) != 0 ||
683: (r = sshbuf_put_cstring(m, pwent->pw_dir)) != 0 ||
684: (r = sshbuf_put_cstring(m, pwent->pw_shell)) != 0)
1.217 djm 685: fatal_fr(r, "assemble pw");
1.94 dtucker 686:
687: out:
1.167 djm 688: ssh_packet_set_log_preamble(ssh, "%suser %s",
689: authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
1.183 markus 690: if ((r = sshbuf_put_string(m, &options, sizeof(options))) != 0)
1.217 djm 691: fatal_fr(r, "assemble options");
1.112 djm 692:
693: #define M_CP_STROPT(x) do { \
1.217 djm 694: if (options.x != NULL && \
695: (r = sshbuf_put_cstring(m, options.x)) != 0) \
696: fatal_fr(r, "assemble %s", #x); \
1.112 djm 697: } while (0)
1.113 djm 698: #define M_CP_STRARRAYOPT(x, nx) do { \
1.183 markus 699: for (i = 0; i < options.nx; i++) { \
700: if ((r = sshbuf_put_cstring(m, options.x[i])) != 0) \
1.217 djm 701: fatal_fr(r, "assemble %s", #x); \
1.183 markus 702: } \
1.113 djm 703: } while (0)
1.112 djm 704: /* See comment in servconf.h */
705: COPY_MATCH_STRING_OPTS();
706: #undef M_CP_STROPT
1.113 djm 707: #undef M_CP_STRARRAYOPT
1.118 djm 708:
709: /* Create valid auth method lists */
1.162 markus 710: if (auth2_setup_methods_lists(authctxt) != 0) {
1.118 djm 711: /*
712: * The monitor will continue long enough to let the child
1.231 guenther 713: * run to its packet_disconnect(), but it must not allow any
1.118 djm 714: * authentication to succeed.
715: */
1.217 djm 716: debug_f("no valid authentication method lists");
1.118 djm 717: }
718:
1.217 djm 719: debug3_f("sending MONITOR_ANS_PWNAM: %d", allowed);
1.59 avsm 720: mm_request_send(sock, MONITOR_ANS_PWNAM, m);
1.1 provos 721:
1.162 markus 722: /* Allow service/style information on the auth context */
723: monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
724: monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
1.10 djm 725:
726: return (0);
727: }
728:
1.191 djm 729: int mm_answer_auth2_read_banner(struct ssh *ssh, int sock, struct sshbuf *m)
1.10 djm 730: {
731: char *banner;
1.183 markus 732: int r;
1.10 djm 733:
1.183 markus 734: sshbuf_reset(m);
1.10 djm 735: banner = auth2_read_banner();
1.183 markus 736: if ((r = sshbuf_put_cstring(m, banner != NULL ? banner : "")) != 0)
1.217 djm 737: fatal_fr(r, "assemble");
1.59 avsm 738: mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
1.124 djm 739: free(banner);
1.1 provos 740:
741: return (0);
742: }
743:
744: int
1.191 djm 745: mm_answer_authserv(struct ssh *ssh, int sock, struct sshbuf *m)
1.1 provos 746: {
1.183 markus 747: int r;
748:
1.1 provos 749: monitor_permit_authentications(1);
750:
1.183 markus 751: if ((r = sshbuf_get_cstring(m, &authctxt->service, NULL)) != 0 ||
752: (r = sshbuf_get_cstring(m, &authctxt->style, NULL)) != 0)
1.217 djm 753: fatal_fr(r, "parse");
754: debug3_f("service=%s, style=%s", authctxt->service, authctxt->style);
1.6 stevesk 755:
1.1 provos 756: if (strlen(authctxt->style) == 0) {
1.124 djm 757: free(authctxt->style);
1.1 provos 758: authctxt->style = NULL;
759: }
760:
761: return (0);
762: }
763:
764: int
1.191 djm 765: mm_answer_authpassword(struct ssh *ssh, int sock, struct sshbuf *m)
1.1 provos 766: {
767: static int call_count;
768: char *passwd;
1.183 markus 769: int r, authenticated;
770: size_t plen;
1.1 provos 771:
1.164 djm 772: if (!options.password_authentication)
1.217 djm 773: fatal_f("password authentication not enabled");
1.183 markus 774: if ((r = sshbuf_get_cstring(m, &passwd, &plen)) != 0)
1.217 djm 775: fatal_fr(r, "parse");
1.1 provos 776: /* Only authenticate if the context is valid */
1.12 markus 777: authenticated = options.password_authentication &&
1.180 djm 778: auth_password(ssh, passwd);
1.209 jsg 779: freezero(passwd, plen);
1.1 provos 780:
1.183 markus 781: sshbuf_reset(m);
782: if ((r = sshbuf_put_u32(m, authenticated)) != 0)
1.217 djm 783: fatal_fr(r, "assemble");
1.1 provos 784:
1.217 djm 785: debug3_f("sending result %d", authenticated);
1.59 avsm 786: mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
1.1 provos 787:
788: call_count++;
789: if (plen == 0 && call_count == 1)
790: auth_method = "none";
791: else
792: auth_method = "password";
793:
794: /* Causes monitor loop to terminate if authenticated */
795: return (authenticated);
796: }
797:
798: int
1.191 djm 799: mm_answer_bsdauthquery(struct ssh *ssh, int sock, struct sshbuf *m)
1.1 provos 800: {
801: char *name, *infotxt;
1.183 markus 802: u_int numprompts, *echo_on, success;
1.1 provos 803: char **prompts;
1.183 markus 804: int r;
1.1 provos 805:
1.164 djm 806: if (!options.kbd_interactive_authentication)
1.217 djm 807: fatal_f("kbd-int authentication not enabled");
1.31 markus 808: success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
809: &prompts, &echo_on) < 0 ? 0 : 1;
1.1 provos 810:
1.183 markus 811: sshbuf_reset(m);
812: if ((r = sshbuf_put_u32(m, success)) != 0)
1.217 djm 813: fatal_fr(r, "assemble");
1.183 markus 814: if (success) {
815: if ((r = sshbuf_put_cstring(m, prompts[0])) != 0)
1.217 djm 816: fatal_fr(r, "assemble prompt");
1.183 markus 817: }
1.1 provos 818:
1.217 djm 819: debug3_f("sending challenge success: %u", 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.191 djm 833: mm_answer_bsdauthrespond(struct ssh *ssh, int sock, struct sshbuf *m)
1.1 provos 834: {
835: char *response;
1.183 markus 836: int r, authok;
1.1 provos 837:
1.164 djm 838: if (!options.kbd_interactive_authentication)
1.217 djm 839: fatal_f("kbd-int authentication not enabled");
1.154 mmcc 840: if (authctxt->as == NULL)
1.217 djm 841: fatal_f("no bsd auth session");
1.1 provos 842:
1.183 markus 843: if ((r = sshbuf_get_cstring(m, &response, NULL)) != 0)
1.217 djm 844: fatal_fr(r, "parse");
1.227 dtucker 845: authok = options.kbd_interactive_authentication &&
1.12 markus 846: auth_userresponse(authctxt->as, response, 0);
1.1 provos 847: authctxt->as = NULL;
1.217 djm 848: debug3_f("<%s> = <%d>", response, authok);
1.124 djm 849: free(response);
1.1 provos 850:
1.183 markus 851: sshbuf_reset(m);
852: if ((r = sshbuf_put_u32(m, authok)) != 0)
1.217 djm 853: fatal_fr(r, "assemble");
1.1 provos 854:
1.217 djm 855: debug3_f("sending authenticated: %d", authok);
1.59 avsm 856: mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
1.1 provos 857:
1.162 markus 858: auth_method = "keyboard-interactive";
859: auth_submethod = "bsdauth";
1.1 provos 860:
861: return (authok != 0);
862: }
863:
1.188 djm 864: /*
865: * Check that the key type appears in the supplied pattern list, ignoring
866: * mismatches in the signature algorithm. (Signature algorithm checks are
867: * performed in the unprivileged authentication code).
868: * Returns 1 on success, 0 otherwise.
869: */
870: static int
871: key_base_type_match(const char *method, const struct sshkey *key,
872: const char *list)
873: {
874: char *s, *l, *ol = xstrdup(list);
875: int found = 0;
876:
877: l = ol;
878: for ((s = strsep(&l, ",")); s && *s != '\0'; (s = strsep(&l, ","))) {
879: if (sshkey_type_from_name(s) == key->type) {
880: found = 1;
881: break;
882: }
883: }
884: if (!found) {
885: error("%s key type %s is not in permitted list %s", method,
886: sshkey_ssh_name(key), list);
887: }
888:
889: free(ol);
890: return found;
891: }
892:
1.1 provos 893: int
1.191 djm 894: mm_answer_keyallowed(struct ssh *ssh, int sock, struct sshbuf *m)
1.1 provos 895: {
1.183 markus 896: struct sshkey *key = NULL;
1.26 markus 897: char *cuser, *chost;
1.183 markus 898: u_int pubkey_auth_attempt;
1.224 djm 899: u_int type = 0;
1.180 djm 900: int r, allowed = 0;
901: struct sshauthopt *opts = NULL;
1.1 provos 902:
1.217 djm 903: debug3_f("entering");
1.183 markus 904: if ((r = sshbuf_get_u32(m, &type)) != 0 ||
905: (r = sshbuf_get_cstring(m, &cuser, NULL)) != 0 ||
906: (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
907: (r = sshkey_froms(m, &key)) != 0 ||
908: (r = sshbuf_get_u32(m, &pubkey_auth_attempt)) != 0)
1.217 djm 909: fatal_fr(r, "parse");
1.1 provos 910:
1.51 djm 911: if (key != NULL && authctxt->valid) {
1.63 deraadt 912: switch (type) {
1.1 provos 913: case MM_USERKEY:
1.77 dtucker 914: auth_method = "publickey";
1.180 djm 915: if (!options.pubkey_authentication)
916: break;
917: if (auth2_key_already_used(authctxt, key))
918: break;
1.188 djm 919: if (!key_base_type_match(auth_method, key,
1.220 dtucker 920: options.pubkey_accepted_algos))
1.180 djm 921: break;
1.234 djm 922: allowed = user_key_allowed(ssh, authctxt->pw, key,
923: pubkey_auth_attempt, &opts);
1.1 provos 924: break;
925: case MM_HOSTKEY:
1.180 djm 926: auth_method = "hostbased";
927: if (!options.hostbased_authentication)
928: break;
929: if (auth2_key_already_used(authctxt, key))
930: break;
1.188 djm 931: if (!key_base_type_match(auth_method, key,
1.221 dtucker 932: options.hostbased_accepted_algos))
1.180 djm 933: break;
1.192 djm 934: allowed = hostbased_key_allowed(ssh, authctxt->pw,
1.1 provos 935: cuser, chost, key);
1.172 djm 936: auth2_record_info(authctxt,
1.126 djm 937: "client user \"%.100s\", client host \"%.100s\"",
938: cuser, chost);
1.1 provos 939: break;
940: default:
1.224 djm 941: fatal_f("unknown key type %u", type);
1.1 provos 942: break;
943: }
1.33 markus 944: }
1.161 djm 945:
1.217 djm 946: debug3_f("%s authentication%s: %s key is %s", auth_method,
947: pubkey_auth_attempt ? "" : " test",
1.180 djm 948: (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key),
949: allowed ? "allowed" : "not allowed");
1.161 djm 950:
1.172 djm 951: auth2_record_key(authctxt, 0, key);
1.1 provos 952:
953: /* clear temporarily storage (used by verify) */
954: monitor_reset_key_state();
955:
956: if (allowed) {
957: /* Save temporarily for comparison in verify */
1.183 markus 958: if ((r = sshkey_to_blob(key, &key_blob, &key_bloblen)) != 0)
1.217 djm 959: fatal_fr(r, "sshkey_to_blob");
1.1 provos 960: key_blobtype = type;
1.180 djm 961: key_opts = opts;
1.1 provos 962: hostbased_cuser = cuser;
963: hostbased_chost = chost;
1.72 djm 964: } else {
1.77 dtucker 965: /* Log failed attempt */
1.190 djm 966: auth_log(ssh, 0, 0, auth_method, NULL);
1.124 djm 967: free(cuser);
968: free(chost);
1.1 provos 969: }
1.183 markus 970: sshkey_free(key);
1.1 provos 971:
1.183 markus 972: sshbuf_reset(m);
973: if ((r = sshbuf_put_u32(m, allowed)) != 0)
1.217 djm 974: fatal_fr(r, "assemble");
1.180 djm 975: if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0)
1.217 djm 976: fatal_fr(r, "sshauthopt_serialise");
1.180 djm 977: mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1.1 provos 978:
1.180 djm 979: if (!allowed)
980: sshauthopt_free(opts);
1.1 provos 981:
982: return (0);
983: }
984:
985: static int
1.222 djm 986: monitor_valid_userblob(struct ssh *ssh, const u_char *data, u_int datalen)
1.1 provos 987: {
1.183 markus 988: struct sshbuf *b;
1.229 djm 989: struct sshkey *hostkey = NULL;
1.183 markus 990: const u_char *p;
1.159 djm 991: char *userstyle, *cp;
1.183 markus 992: size_t len;
993: u_char type;
1.229 djm 994: int hostbound = 0, r, fail = 0;
1.1 provos 995:
1.203 djm 996: if ((b = sshbuf_from(data, datalen)) == NULL)
1.217 djm 997: fatal_f("sshbuf_from");
1.3 markus 998:
1.222 djm 999: if (ssh->compat & SSH_OLD_SESSIONID) {
1.183 markus 1000: p = sshbuf_ptr(b);
1001: len = sshbuf_len(b);
1.13 markus 1002: if ((session_id2 == NULL) ||
1003: (len < session_id2_len) ||
1.108 djm 1004: (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1.13 markus 1005: fail++;
1.183 markus 1006: if ((r = sshbuf_consume(b, session_id2_len)) != 0)
1.217 djm 1007: fatal_fr(r, "consume");
1.1 provos 1008: } else {
1.183 markus 1009: if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1.217 djm 1010: fatal_fr(r, "parse sessionid");
1.13 markus 1011: if ((session_id2 == NULL) ||
1012: (len != session_id2_len) ||
1.108 djm 1013: (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1.1 provos 1014: fail++;
1015: }
1.183 markus 1016: if ((r = sshbuf_get_u8(b, &type)) != 0)
1.217 djm 1017: fatal_fr(r, "parse type");
1.183 markus 1018: if (type != SSH2_MSG_USERAUTH_REQUEST)
1.1 provos 1019: fail++;
1.183 markus 1020: if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1.217 djm 1021: fatal_fr(r, "parse userstyle");
1.121 djm 1022: xasprintf(&userstyle, "%s%s%s", authctxt->user,
1023: authctxt->style ? ":" : "",
1024: authctxt->style ? authctxt->style : "");
1.159 djm 1025: if (strcmp(userstyle, cp) != 0) {
1026: logit("wrong user name passed to monitor: "
1027: "expected %s != %.100s", userstyle, cp);
1.1 provos 1028: fail++;
1029: }
1.124 djm 1030: free(userstyle);
1.159 djm 1031: free(cp);
1.183 markus 1032: if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1033: (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1.217 djm 1034: fatal_fr(r, "parse method");
1.229 djm 1035: if (strcmp("publickey", cp) != 0) {
1036: if (strcmp("publickey-hostbound-v00@openssh.com", cp) == 0)
1037: hostbound = 1;
1038: else
1039: fail++;
1040: }
1.178 djm 1041: free(cp);
1.183 markus 1042: if ((r = sshbuf_get_u8(b, &type)) != 0)
1.217 djm 1043: fatal_fr(r, "parse pktype");
1.183 markus 1044: if (type == 0)
1.178 djm 1045: fail++;
1.183 markus 1046: if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1.229 djm 1047: (r = sshbuf_skip_string(b)) != 0 || /* pkblob */
1048: (hostbound && (r = sshkey_froms(b, &hostkey)) != 0))
1.217 djm 1049: fatal_fr(r, "parse pk");
1.183 markus 1050: if (sshbuf_len(b) != 0)
1.1 provos 1051: fail++;
1.183 markus 1052: sshbuf_free(b);
1.229 djm 1053: if (hostkey != NULL) {
1054: /*
1055: * Ensure this is actually one of our hostkeys; unfortunately
1056: * can't check ssh->kex->initial_hostkey directly at this point
1057: * as packet state has not yet been exported to monitor.
1058: */
1059: if (get_hostkey_index(hostkey, 1, ssh) == -1)
1060: fatal_f("hostbound hostkey does not match");
1061: sshkey_free(hostkey);
1062: }
1.1 provos 1063: return (fail == 0);
1064: }
1065:
1066: static int
1.203 djm 1067: monitor_valid_hostbasedblob(const u_char *data, u_int datalen,
1068: const char *cuser, const char *chost)
1.1 provos 1069: {
1.183 markus 1070: struct sshbuf *b;
1071: const u_char *p;
1072: char *cp, *userstyle;
1073: size_t len;
1074: int r, fail = 0;
1075: u_char type;
1.1 provos 1076:
1.203 djm 1077: if ((b = sshbuf_from(data, datalen)) == NULL)
1.217 djm 1078: fatal_f("sshbuf_new");
1.203 djm 1079: if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1.217 djm 1080: fatal_fr(r, "parse sessionid");
1.3 markus 1081:
1.13 markus 1082: if ((session_id2 == NULL) ||
1083: (len != session_id2_len) ||
1.108 djm 1084: (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1.1 provos 1085: fail++;
1.13 markus 1086:
1.183 markus 1087: if ((r = sshbuf_get_u8(b, &type)) != 0)
1.217 djm 1088: fatal_fr(r, "parse type");
1.183 markus 1089: if (type != SSH2_MSG_USERAUTH_REQUEST)
1.1 provos 1090: fail++;
1.183 markus 1091: if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1.217 djm 1092: fatal_fr(r, "parse userstyle");
1.121 djm 1093: xasprintf(&userstyle, "%s%s%s", authctxt->user,
1094: authctxt->style ? ":" : "",
1095: authctxt->style ? authctxt->style : "");
1.183 markus 1096: if (strcmp(userstyle, cp) != 0) {
1097: logit("wrong user name passed to monitor: "
1098: "expected %s != %.100s", userstyle, cp);
1.1 provos 1099: fail++;
1100: }
1.121 djm 1101: free(userstyle);
1.183 markus 1102: free(cp);
1103: if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1104: (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1.217 djm 1105: fatal_fr(r, "parse method");
1.183 markus 1106: if (strcmp(cp, "hostbased") != 0)
1.1 provos 1107: fail++;
1.183 markus 1108: free(cp);
1109: if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1110: (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1.217 djm 1111: fatal_fr(r, "parse pk");
1.1 provos 1112:
1113: /* verify client host, strip trailing dot if necessary */
1.183 markus 1114: if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1.217 djm 1115: fatal_fr(r, "parse host");
1.183 markus 1116: if (((len = strlen(cp)) > 0) && cp[len - 1] == '.')
1117: cp[len - 1] = '\0';
1118: if (strcmp(cp, chost) != 0)
1.1 provos 1119: fail++;
1.183 markus 1120: free(cp);
1.1 provos 1121:
1122: /* verify client user */
1.183 markus 1123: if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1.217 djm 1124: fatal_fr(r, "parse ruser");
1.183 markus 1125: if (strcmp(cp, cuser) != 0)
1.1 provos 1126: fail++;
1.183 markus 1127: free(cp);
1.1 provos 1128:
1.183 markus 1129: if (sshbuf_len(b) != 0)
1.1 provos 1130: fail++;
1.183 markus 1131: sshbuf_free(b);
1.1 provos 1132: return (fail == 0);
1133: }
1134:
1135: int
1.191 djm 1136: mm_answer_keyverify(struct ssh *ssh, int sock, struct sshbuf *m)
1.1 provos 1137: {
1.168 markus 1138: struct sshkey *key;
1.203 djm 1139: const u_char *signature, *data, *blob;
1140: char *sigalg = NULL, *fp = NULL;
1.169 markus 1141: size_t signaturelen, datalen, bloblen;
1.214 djm 1142: int r, ret, req_presence = 0, req_verify = 0, valid_data = 0;
1143: int encoded_ret;
1.202 djm 1144: struct sshkey_sig_details *sig_details = NULL;
1.1 provos 1145:
1.203 djm 1146: if ((r = sshbuf_get_string_direct(m, &blob, &bloblen)) != 0 ||
1147: (r = sshbuf_get_string_direct(m, &signature, &signaturelen)) != 0 ||
1148: (r = sshbuf_get_string_direct(m, &data, &datalen)) != 0 ||
1.176 djm 1149: (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
1.217 djm 1150: fatal_fr(r, "parse");
1.1 provos 1151:
1152: if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1.8 mouring 1153: !monitor_allowed_key(blob, bloblen))
1.217 djm 1154: fatal_f("bad key, not previously allowed");
1.1 provos 1155:
1.177 djm 1156: /* Empty signature algorithm means NULL. */
1157: if (*sigalg == '\0') {
1158: free(sigalg);
1159: sigalg = NULL;
1160: }
1161:
1.169 markus 1162: /* XXX use sshkey_froms here; need to change key_blob, etc. */
1163: if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1.217 djm 1164: fatal_fr(r, "parse key");
1.1 provos 1165:
1166: switch (key_blobtype) {
1167: case MM_USERKEY:
1.222 djm 1168: valid_data = monitor_valid_userblob(ssh, data, datalen);
1.172 djm 1169: auth_method = "publickey";
1.1 provos 1170: break;
1171: case MM_HOSTKEY:
1172: valid_data = monitor_valid_hostbasedblob(data, datalen,
1173: hostbased_cuser, hostbased_chost);
1.172 djm 1174: auth_method = "hostbased";
1.1 provos 1175: break;
1176: default:
1177: valid_data = 0;
1178: break;
1179: }
1180: if (!valid_data)
1.223 djm 1181: fatal_f("bad %s signature data blob",
1182: key_blobtype == MM_USERKEY ? "userkey" :
1183: (key_blobtype == MM_HOSTKEY ? "hostkey" : "unknown"));
1.1 provos 1184:
1.203 djm 1185: if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
1186: SSH_FP_DEFAULT)) == NULL)
1.217 djm 1187: fatal_f("sshkey_fingerprint failed");
1.203 djm 1188:
1.169 markus 1189: ret = sshkey_verify(key, signature, signaturelen, data, datalen,
1.202 djm 1190: sigalg, ssh->compat, &sig_details);
1.230 djm 1191: debug3_f("%s %s signature using %s %s%s%s", auth_method,
1192: sshkey_type(key), sigalg == NULL ? "default" : sigalg,
1.201 djm 1193: (ret == 0) ? "verified" : "unverified",
1194: (ret != 0) ? ": " : "", (ret != 0) ? ssh_err(ret) : "");
1.203 djm 1195:
1196: if (ret == 0 && key_blobtype == MM_USERKEY && sig_details != NULL) {
1197: req_presence = (options.pubkey_auth_options &
1.204 djm 1198: PUBKEYAUTH_TOUCH_REQUIRED) ||
1199: !key_opts->no_require_user_presence;
1.203 djm 1200: if (req_presence &&
1201: (sig_details->sk_flags & SSH_SK_USER_PRESENCE_REQD) == 0) {
1202: error("public key %s %s signature for %s%s from %.128s "
1.208 naddy 1203: "port %d rejected: user presence "
1204: "(authenticator touch) requirement not met ",
1205: sshkey_type(key), fp,
1.214 djm 1206: authctxt->valid ? "" : "invalid user ",
1207: authctxt->user, ssh_remote_ipaddr(ssh),
1208: ssh_remote_port(ssh));
1209: ret = SSH_ERR_SIGNATURE_INVALID;
1210: }
1211: req_verify = (options.pubkey_auth_options &
1212: PUBKEYAUTH_VERIFY_REQUIRED) || key_opts->require_verify;
1213: if (req_verify &&
1214: (sig_details->sk_flags & SSH_SK_USER_VERIFICATION_REQD) == 0) {
1215: error("public key %s %s signature for %s%s from %.128s "
1216: "port %d rejected: user verification requirement "
1217: "not met ", sshkey_type(key), fp,
1.203 djm 1218: authctxt->valid ? "" : "invalid user ",
1219: authctxt->user, ssh_remote_ipaddr(ssh),
1220: ssh_remote_port(ssh));
1221: ret = SSH_ERR_SIGNATURE_INVALID;
1222: }
1223: }
1.172 djm 1224: auth2_record_key(authctxt, ret == 0, key);
1.136 djm 1225:
1.180 djm 1226: if (key_blobtype == MM_USERKEY)
1227: auth_activate_options(ssh, key_opts);
1.1 provos 1228: monitor_reset_key_state();
1.3 markus 1229:
1.169 markus 1230: sshbuf_reset(m);
1231:
1232: /* encode ret != 0 as positive integer, since we're sending u32 */
1233: encoded_ret = (ret != 0);
1.202 djm 1234: if ((r = sshbuf_put_u32(m, encoded_ret)) != 0 ||
1.205 djm 1235: (r = sshbuf_put_u8(m, sig_details != NULL)) != 0)
1.217 djm 1236: fatal_fr(r, "assemble");
1.202 djm 1237: if (sig_details != NULL) {
1238: if ((r = sshbuf_put_u32(m, sig_details->sk_counter)) != 0 ||
1239: (r = sshbuf_put_u8(m, sig_details->sk_flags)) != 0)
1.217 djm 1240: fatal_fr(r, "assemble sk");
1.202 djm 1241: }
1242: sshkey_sig_details_free(sig_details);
1.59 avsm 1243: mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1.203 djm 1244:
1245: free(sigalg);
1246: free(fp);
1247: sshkey_free(key);
1.1 provos 1248:
1.169 markus 1249: return ret == 0;
1.1 provos 1250: }
1251:
1.2 markus 1252: static void
1.191 djm 1253: mm_record_login(struct ssh *ssh, Session *s, struct passwd *pw)
1.1 provos 1254: {
1255: socklen_t fromlen;
1256: struct sockaddr_storage from;
1257:
1258: /*
1259: * Get IP address of client. If the connection is not a socket, let
1260: * the address be 0.0.0.0.
1261: */
1262: memset(&from, 0, sizeof(from));
1.24 stevesk 1263: fromlen = sizeof(from);
1.191 djm 1264: if (ssh_packet_connection_is_on_socket(ssh)) {
1265: if (getpeername(ssh_packet_get_connection_in(ssh),
1.198 deraadt 1266: (struct sockaddr *)&from, &fromlen) == -1) {
1.1 provos 1267: debug("getpeername: %.100s", strerror(errno));
1.50 markus 1268: cleanup_exit(255);
1.1 provos 1269: }
1270: }
1271: /* Record that there was a login on that tty from the remote host. */
1272: record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1.158 djm 1273: session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
1.24 stevesk 1274: (struct sockaddr *)&from, fromlen);
1.1 provos 1275: }
1276:
1277: static void
1278: mm_session_close(Session *s)
1279: {
1.217 djm 1280: debug3_f("session %d pid %ld", s->self, (long)s->pid);
1.1 provos 1281: if (s->ttyfd != -1) {
1.217 djm 1282: debug3_f("tty %s ptyfd %d", s->tty, s->ptyfd);
1.1 provos 1283: session_pty_cleanup2(s);
1284: }
1.96 djm 1285: session_unused(s->self);
1.1 provos 1286: }
1287:
1288: int
1.191 djm 1289: mm_answer_pty(struct ssh *ssh, int sock, struct sshbuf *m)
1.1 provos 1290: {
1.11 mouring 1291: extern struct monitor *pmonitor;
1.1 provos 1292: Session *s;
1.183 markus 1293: int r, res, fd0;
1.1 provos 1294:
1.217 djm 1295: debug3_f("entering");
1.1 provos 1296:
1.183 markus 1297: sshbuf_reset(m);
1.1 provos 1298: s = session_new();
1299: if (s == NULL)
1300: goto error;
1301: s->authctxt = authctxt;
1302: s->pw = authctxt->pw;
1.11 mouring 1303: s->pid = pmonitor->m_pid;
1.1 provos 1304: res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1305: if (res == 0)
1306: goto error;
1307: pty_setowner(authctxt->pw, s->tty);
1308:
1.183 markus 1309: if ((r = sshbuf_put_u32(m, 1)) != 0 ||
1310: (r = sshbuf_put_cstring(m, s->tty)) != 0)
1.217 djm 1311: fatal_fr(r, "assemble");
1.1 provos 1312:
1313: /* We need to trick ttyslot */
1314: if (dup2(s->ttyfd, 0) == -1)
1.217 djm 1315: fatal_f("dup2");
1.1 provos 1316:
1.191 djm 1317: mm_record_login(ssh, s, authctxt->pw);
1.1 provos 1318:
1319: /* Now we can close the file descriptor again */
1320: close(0);
1.61 dtucker 1321:
1322: /* send messages generated by record_login */
1.183 markus 1323: if ((r = sshbuf_put_stringb(m, loginmsg)) != 0)
1.217 djm 1324: fatal_fr(r, "assemble loginmsg");
1.183 markus 1325: sshbuf_reset(loginmsg);
1.61 dtucker 1326:
1327: mm_request_send(sock, MONITOR_ANS_PTY, m);
1328:
1.92 djm 1329: if (mm_send_fd(sock, s->ptyfd) == -1 ||
1330: mm_send_fd(sock, s->ttyfd) == -1)
1.217 djm 1331: fatal_f("send fds failed");
1.1 provos 1332:
1333: /* make sure nothing uses fd 0 */
1.198 deraadt 1334: if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1.217 djm 1335: fatal_f("open(/dev/null): %s", strerror(errno));
1.1 provos 1336: if (fd0 != 0)
1.217 djm 1337: error_f("fd0 %d != 0", fd0);
1.1 provos 1338:
1.212 deraadt 1339: /* slave side of pty is not needed */
1.1 provos 1340: close(s->ttyfd);
1341: s->ttyfd = s->ptyfd;
1342: /* no need to dup() because nobody closes ptyfd */
1343: s->ptymaster = s->ptyfd;
1344:
1.217 djm 1345: debug3_f("tty %s ptyfd %d", s->tty, s->ttyfd);
1.1 provos 1346:
1347: return (0);
1348:
1349: error:
1350: if (s != NULL)
1351: mm_session_close(s);
1.183 markus 1352: if ((r = sshbuf_put_u32(m, 0)) != 0)
1.217 djm 1353: fatal_fr(r, "assemble 0");
1.59 avsm 1354: mm_request_send(sock, MONITOR_ANS_PTY, m);
1.1 provos 1355: return (0);
1356: }
1357:
1358: int
1.191 djm 1359: mm_answer_pty_cleanup(struct ssh *ssh, int sock, struct sshbuf *m)
1.1 provos 1360: {
1361: Session *s;
1362: char *tty;
1.183 markus 1363: int r;
1.1 provos 1364:
1.217 djm 1365: debug3_f("entering");
1.1 provos 1366:
1.183 markus 1367: if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
1.217 djm 1368: fatal_fr(r, "parse tty");
1.1 provos 1369: if ((s = session_by_tty(tty)) != NULL)
1370: mm_session_close(s);
1.183 markus 1371: sshbuf_reset(m);
1.124 djm 1372: free(tty);
1.1 provos 1373: return (0);
1374: }
1375:
1376: int
1.191 djm 1377: mm_answer_term(struct ssh *ssh, int sock, struct sshbuf *req)
1.1 provos 1378: {
1.11 mouring 1379: extern struct monitor *pmonitor;
1.1 provos 1380: int res, status;
1381:
1.217 djm 1382: debug3_f("tearing down sessions");
1.1 provos 1383:
1384: /* The child is terminating */
1.173 djm 1385: session_destroy_all(ssh, &mm_session_close);
1.1 provos 1386:
1.11 mouring 1387: while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1.9 markus 1388: if (errno != EINTR)
1389: exit(1);
1.1 provos 1390:
1391: res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1392:
1393: /* Terminate process */
1.57 deraadt 1394: exit(res);
1.170 markus 1395: }
1396:
1397: void
1.191 djm 1398: monitor_clear_keystate(struct ssh *ssh, struct monitor *pmonitor)
1.170 markus 1399: {
1400: ssh_clear_newkeys(ssh, MODE_IN);
1401: ssh_clear_newkeys(ssh, MODE_OUT);
1402: sshbuf_free(child_state);
1403: child_state = NULL;
1.1 provos 1404: }
1405:
1406: void
1.191 djm 1407: monitor_apply_keystate(struct ssh *ssh, struct monitor *pmonitor)
1.1 provos 1408: {
1.139 markus 1409: struct kex *kex;
1410: int r;
1411:
1.217 djm 1412: debug3_f("packet_set_state");
1.139 markus 1413: if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1.217 djm 1414: fatal_fr(r, "packet_set_state");
1.139 markus 1415: sshbuf_free(child_state);
1416: child_state = NULL;
1.223 djm 1417: if ((kex = ssh->kex) == NULL)
1418: fatal_f("internal error: ssh->kex == NULL");
1419: if (session_id2_len != sshbuf_len(ssh->kex->session_id)) {
1420: fatal_f("incorrect session id length %zu (expected %u)",
1421: sshbuf_len(ssh->kex->session_id), session_id2_len);
1422: }
1423: if (memcmp(sshbuf_ptr(ssh->kex->session_id), session_id2,
1424: session_id2_len) != 0)
1425: fatal_f("session ID mismatch");
1426: /* XXX set callbacks */
1.147 djm 1427: #ifdef WITH_OPENSSL
1.223 djm 1428: kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_server;
1429: kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_server;
1430: kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_server;
1431: kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_server;
1432: kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_server;
1433: kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1434: kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1435: kex->kex[KEX_ECDH_SHA2] = kex_gen_server;
1.147 djm 1436: #endif
1.223 djm 1437: kex->kex[KEX_C25519_SHA256] = kex_gen_server;
1438: kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_server;
1439: kex->load_host_public_key=&get_hostkey_public_by_type;
1440: kex->load_host_private_key=&get_hostkey_private_by_type;
1441: kex->host_key_index=&get_hostkey_index;
1442: kex->sign = sshd_hostkey_sign;
1.1 provos 1443: }
1444:
1.210 djm 1445: /* This function requires careful sanity checking */
1.1 provos 1446:
1447: void
1.192 djm 1448: mm_get_keystate(struct ssh *ssh, struct monitor *pmonitor)
1.1 provos 1449: {
1.217 djm 1450: debug3_f("Waiting for new keys");
1.1 provos 1451:
1.139 markus 1452: if ((child_state = sshbuf_new()) == NULL)
1.217 djm 1453: fatal_f("sshbuf_new failed");
1.139 markus 1454: mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1455: child_state);
1.217 djm 1456: debug3_f("GOT new keys");
1.1 provos 1457: }
1458:
1459:
1460: /* XXX */
1461:
1462: #define FD_CLOSEONEXEC(x) do { \
1.111 djm 1463: if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
1.1 provos 1464: fatal("fcntl(%d, F_SETFD)", x); \
1465: } while (0)
1466:
1.2 markus 1467: static void
1.114 djm 1468: monitor_openfds(struct monitor *mon, int do_logfds)
1.3 markus 1469: {
1.114 djm 1470: int pair[2];
1.171 markus 1471: #ifdef SO_ZEROIZE
1472: int on = 1;
1473: #endif
1.114 djm 1474:
1.1 provos 1475: if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1.217 djm 1476: fatal_f("socketpair: %s", strerror(errno));
1.171 markus 1477: #ifdef SO_ZEROIZE
1.198 deraadt 1478: if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) == -1)
1.171 markus 1479: error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
1.198 deraadt 1480: if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) == -1)
1.171 markus 1481: error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
1482: #endif
1.1 provos 1483: FD_CLOSEONEXEC(pair[0]);
1484: FD_CLOSEONEXEC(pair[1]);
1.114 djm 1485: mon->m_recvfd = pair[0];
1486: mon->m_sendfd = pair[1];
1487:
1488: if (do_logfds) {
1489: if (pipe(pair) == -1)
1.217 djm 1490: fatal_f("pipe: %s", strerror(errno));
1.114 djm 1491: FD_CLOSEONEXEC(pair[0]);
1492: FD_CLOSEONEXEC(pair[1]);
1493: mon->m_log_recvfd = pair[0];
1494: mon->m_log_sendfd = pair[1];
1495: } else
1496: mon->m_log_recvfd = mon->m_log_sendfd = -1;
1.1 provos 1497: }
1498:
1499: #define MM_MEMSIZE 65536
1500:
1501: struct monitor *
1502: monitor_init(void)
1503: {
1504: struct monitor *mon;
1505:
1.75 djm 1506: mon = xcalloc(1, sizeof(*mon));
1.114 djm 1507: monitor_openfds(mon, 1);
1.1 provos 1508:
1509: return mon;
1510: }
1511:
1512: void
1513: monitor_reinit(struct monitor *mon)
1514: {
1.114 djm 1515: monitor_openfds(mon, 0);
1.1 provos 1516: }
1.46 markus 1517:
1518: #ifdef GSSAPI
1519: int
1.191 djm 1520: mm_answer_gss_setup_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
1.46 markus 1521: {
1.59 avsm 1522: gss_OID_desc goid;
1.46 markus 1523: OM_uint32 major;
1.183 markus 1524: size_t len;
1.184 djm 1525: u_char *p;
1.183 markus 1526: int r;
1.46 markus 1527:
1.164 djm 1528: if (!options.gss_authentication)
1.217 djm 1529: fatal_f("GSSAPI authentication not enabled");
1.164 djm 1530:
1.184 djm 1531: if ((r = sshbuf_get_string(m, &p, &len)) != 0)
1.217 djm 1532: fatal_fr(r, "parse");
1.184 djm 1533: goid.elements = p;
1.59 avsm 1534: goid.length = len;
1.46 markus 1535:
1.59 avsm 1536: major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1.46 markus 1537:
1.124 djm 1538: free(goid.elements);
1.46 markus 1539:
1.183 markus 1540: sshbuf_reset(m);
1541: if ((r = sshbuf_put_u32(m, major)) != 0)
1.217 djm 1542: fatal_fr(r, "assemble");
1.46 markus 1543:
1.64 stevesk 1544: mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1.46 markus 1545:
1546: /* Now we have a context, enable the step */
1547: monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1548:
1549: return (0);
1550: }
1551:
1552: int
1.191 djm 1553: mm_answer_gss_accept_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
1.46 markus 1554: {
1555: gss_buffer_desc in;
1556: gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1.64 stevesk 1557: OM_uint32 major, minor;
1.46 markus 1558: OM_uint32 flags = 0; /* GSI needs this */
1.183 markus 1559: int r;
1.46 markus 1560:
1.164 djm 1561: if (!options.gss_authentication)
1.217 djm 1562: fatal_f("GSSAPI authentication not enabled");
1.164 djm 1563:
1.184 djm 1564: if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0)
1.217 djm 1565: fatal_fr(r, "ssh_gssapi_get_buffer_desc");
1.46 markus 1566: major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1.124 djm 1567: free(in.value);
1.46 markus 1568:
1.183 markus 1569: sshbuf_reset(m);
1570: if ((r = sshbuf_put_u32(m, major)) != 0 ||
1571: (r = sshbuf_put_string(m, out.value, out.length)) != 0 ||
1572: (r = sshbuf_put_u32(m, flags)) != 0)
1.217 djm 1573: fatal_fr(r, "assemble");
1.59 avsm 1574: mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1.46 markus 1575:
1576: gss_release_buffer(&minor, &out);
1577:
1.64 stevesk 1578: if (major == GSS_S_COMPLETE) {
1.46 markus 1579: monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1580: monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1.52 markus 1581: monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1.46 markus 1582: }
1583: return (0);
1584: }
1585:
1586: int
1.191 djm 1587: mm_answer_gss_checkmic(struct ssh *ssh, int sock, struct sshbuf *m)
1.52 markus 1588: {
1589: gss_buffer_desc gssbuf, mic;
1590: OM_uint32 ret;
1.184 djm 1591: int r;
1.54 djm 1592:
1.164 djm 1593: if (!options.gss_authentication)
1.217 djm 1594: fatal_f("GSSAPI authentication not enabled");
1.164 djm 1595:
1.184 djm 1596: if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 ||
1597: (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0)
1.217 djm 1598: fatal_fr(r, "ssh_gssapi_get_buffer_desc");
1.54 djm 1599:
1.52 markus 1600: ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1.54 djm 1601:
1.124 djm 1602: free(gssbuf.value);
1603: free(mic.value);
1.54 djm 1604:
1.183 markus 1605: sshbuf_reset(m);
1606: if ((r = sshbuf_put_u32(m, ret)) != 0)
1.217 djm 1607: fatal_fr(r, "assemble");
1.54 djm 1608:
1.59 avsm 1609: mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1.54 djm 1610:
1.52 markus 1611: if (!GSS_ERROR(ret))
1612: monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1.54 djm 1613:
1.52 markus 1614: return (0);
1615: }
1616:
1617: int
1.191 djm 1618: mm_answer_gss_userok(struct ssh *ssh, int sock, struct sshbuf *m)
1.46 markus 1619: {
1.184 djm 1620: int r, authenticated;
1.172 djm 1621: const char *displayname;
1.164 djm 1622:
1623: if (!options.gss_authentication)
1.217 djm 1624: fatal_f("GSSAPI authentication not enabled");
1.46 markus 1625:
1626: authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1627:
1.183 markus 1628: sshbuf_reset(m);
1629: if ((r = sshbuf_put_u32(m, authenticated)) != 0)
1.217 djm 1630: fatal_fr(r, "assemble");
1.46 markus 1631:
1.217 djm 1632: debug3_f("sending result %d", authenticated);
1.59 avsm 1633: mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1.46 markus 1634:
1.64 stevesk 1635: auth_method = "gssapi-with-mic";
1.172 djm 1636:
1637: if ((displayname = ssh_gssapi_displayname()) != NULL)
1638: auth2_record_info(authctxt, "%s", displayname);
1.46 markus 1639:
1640: /* Monitor loop will terminate if authenticated */
1641: return (authenticated);
1642: }
1643: #endif /* GSSAPI */
1.100 djm 1644: