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