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