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