- (dtucker) [openbsd-compat/port-linux.c] Check is_selinux_enabled for exact
[openssh-git.git] / monitor_wrap.c
blob1a5dda56158e5c9c9f053a12a379959c8bb3c799
1 /* $OpenBSD: monitor_wrap.c,v 1.70 2010/08/31 11:54:45 djm Exp $ */
2 /*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
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.
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.
28 #include "includes.h"
30 #include <sys/types.h>
31 #include <sys/uio.h>
33 #include <errno.h>
34 #include <pwd.h>
35 #include <signal.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <unistd.h>
41 #include <openssl/bn.h>
42 #include <openssl/dh.h>
43 #include <openssl/evp.h>
45 #include "openbsd-compat/sys-queue.h"
46 #include "xmalloc.h"
47 #include "ssh.h"
48 #include "dh.h"
49 #include "buffer.h"
50 #include "key.h"
51 #include "cipher.h"
52 #include "kex.h"
53 #include "hostfile.h"
54 #include "auth.h"
55 #include "auth-options.h"
56 #include "packet.h"
57 #include "mac.h"
58 #include "log.h"
59 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
60 #undef TARGET_OS_MAC
61 #include "zlib.h"
62 #define TARGET_OS_MAC 1
63 #else
64 #include "zlib.h"
65 #endif
66 #include "monitor.h"
67 #ifdef GSSAPI
68 #include "ssh-gss.h"
69 #endif
70 #include "monitor_wrap.h"
71 #include "atomicio.h"
72 #include "monitor_fdpass.h"
73 #include "misc.h"
74 #include "schnorr.h"
75 #include "jpake.h"
76 #include "uuencode.h"
78 #include "channels.h"
79 #include "session.h"
80 #include "servconf.h"
81 #include "roaming.h"
83 /* Imports */
84 extern int compat20;
85 extern z_stream incoming_stream;
86 extern z_stream outgoing_stream;
87 extern struct monitor *pmonitor;
88 extern Buffer loginmsg;
89 extern ServerOptions options;
91 int
92 mm_is_monitor(void)
95 * m_pid is only set in the privileged part, and
96 * points to the unprivileged child.
98 return (pmonitor && pmonitor->m_pid > 0);
101 void
102 mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
104 u_int mlen = buffer_len(m);
105 u_char buf[5];
107 debug3("%s entering: type %d", __func__, type);
109 put_u32(buf, mlen + 1);
110 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
111 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
112 fatal("%s: write: %s", __func__, strerror(errno));
113 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
114 fatal("%s: write: %s", __func__, strerror(errno));
117 void
118 mm_request_receive(int sock, Buffer *m)
120 u_char buf[4];
121 u_int msg_len;
123 debug3("%s entering", __func__);
125 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
126 if (errno == EPIPE)
127 cleanup_exit(255);
128 fatal("%s: read: %s", __func__, strerror(errno));
130 msg_len = get_u32(buf);
131 if (msg_len > 256 * 1024)
132 fatal("%s: read: bad msg_len %d", __func__, msg_len);
133 buffer_clear(m);
134 buffer_append_space(m, msg_len);
135 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
136 fatal("%s: read: %s", __func__, strerror(errno));
139 void
140 mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
142 u_char rtype;
144 debug3("%s entering: type %d", __func__, type);
146 mm_request_receive(sock, m);
147 rtype = buffer_get_char(m);
148 if (rtype != type)
149 fatal("%s: read: rtype %d != type %d", __func__,
150 rtype, type);
153 DH *
154 mm_choose_dh(int min, int nbits, int max)
156 BIGNUM *p, *g;
157 int success = 0;
158 Buffer m;
160 buffer_init(&m);
161 buffer_put_int(&m, min);
162 buffer_put_int(&m, nbits);
163 buffer_put_int(&m, max);
165 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
167 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
168 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
170 success = buffer_get_char(&m);
171 if (success == 0)
172 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
174 if ((p = BN_new()) == NULL)
175 fatal("%s: BN_new failed", __func__);
176 if ((g = BN_new()) == NULL)
177 fatal("%s: BN_new failed", __func__);
178 buffer_get_bignum2(&m, p);
179 buffer_get_bignum2(&m, g);
181 debug3("%s: remaining %d", __func__, buffer_len(&m));
182 buffer_free(&m);
184 return (dh_new_group(g, p));
188 mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
190 Kex *kex = *pmonitor->m_pkex;
191 Buffer m;
193 debug3("%s entering", __func__);
195 buffer_init(&m);
196 buffer_put_int(&m, kex->host_key_index(key));
197 buffer_put_string(&m, data, datalen);
199 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
201 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
202 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
203 *sigp = buffer_get_string(&m, lenp);
204 buffer_free(&m);
206 return (0);
209 struct passwd *
210 mm_getpwnamallow(const char *username)
212 Buffer m;
213 struct passwd *pw;
214 u_int len;
215 ServerOptions *newopts;
217 debug3("%s entering", __func__);
219 buffer_init(&m);
220 buffer_put_cstring(&m, username);
222 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
224 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
225 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
227 if (buffer_get_char(&m) == 0) {
228 pw = NULL;
229 goto out;
231 pw = buffer_get_string(&m, &len);
232 if (len != sizeof(struct passwd))
233 fatal("%s: struct passwd size mismatch", __func__);
234 pw->pw_name = buffer_get_string(&m, NULL);
235 pw->pw_passwd = buffer_get_string(&m, NULL);
236 pw->pw_gecos = buffer_get_string(&m, NULL);
237 #ifdef HAVE_PW_CLASS_IN_PASSWD
238 pw->pw_class = buffer_get_string(&m, NULL);
239 #endif
240 pw->pw_dir = buffer_get_string(&m, NULL);
241 pw->pw_shell = buffer_get_string(&m, NULL);
243 out:
244 /* copy options block as a Match directive may have changed some */
245 newopts = buffer_get_string(&m, &len);
246 if (len != sizeof(*newopts))
247 fatal("%s: option block size mismatch", __func__);
248 if (newopts->banner != NULL)
249 newopts->banner = buffer_get_string(&m, NULL);
250 copy_set_server_options(&options, newopts, 1);
251 xfree(newopts);
253 buffer_free(&m);
255 return (pw);
258 char *
259 mm_auth2_read_banner(void)
261 Buffer m;
262 char *banner;
264 debug3("%s entering", __func__);
266 buffer_init(&m);
267 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
268 buffer_clear(&m);
270 mm_request_receive_expect(pmonitor->m_recvfd,
271 MONITOR_ANS_AUTH2_READ_BANNER, &m);
272 banner = buffer_get_string(&m, NULL);
273 buffer_free(&m);
275 /* treat empty banner as missing banner */
276 if (strlen(banner) == 0) {
277 xfree(banner);
278 banner = NULL;
280 return (banner);
283 /* Inform the privileged process about service and style */
285 void
286 mm_inform_authserv(char *service, char *style)
288 Buffer m;
290 debug3("%s entering", __func__);
292 buffer_init(&m);
293 buffer_put_cstring(&m, service);
294 buffer_put_cstring(&m, style ? style : "");
296 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
298 buffer_free(&m);
301 /* Do the password authentication */
303 mm_auth_password(Authctxt *authctxt, char *password)
305 Buffer m;
306 int authenticated = 0;
308 debug3("%s entering", __func__);
310 buffer_init(&m);
311 buffer_put_cstring(&m, password);
312 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
314 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
315 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
317 authenticated = buffer_get_int(&m);
319 buffer_free(&m);
321 debug3("%s: user %sauthenticated",
322 __func__, authenticated ? "" : "not ");
323 return (authenticated);
327 mm_user_key_allowed(struct passwd *pw, Key *key)
329 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
333 mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
334 Key *key)
336 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
340 mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
341 char *host, Key *key)
343 int ret;
345 key->type = KEY_RSA; /* XXX hack for key_to_blob */
346 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
347 key->type = KEY_RSA1;
348 return (ret);
352 mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
354 Buffer m;
355 u_char *blob;
356 u_int len;
357 int allowed = 0, have_forced = 0;
359 debug3("%s entering", __func__);
361 /* Convert the key to a blob and the pass it over */
362 if (!key_to_blob(key, &blob, &len))
363 return (0);
365 buffer_init(&m);
366 buffer_put_int(&m, type);
367 buffer_put_cstring(&m, user ? user : "");
368 buffer_put_cstring(&m, host ? host : "");
369 buffer_put_string(&m, blob, len);
370 xfree(blob);
372 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
374 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
375 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
377 allowed = buffer_get_int(&m);
379 /* fake forced command */
380 auth_clear_options();
381 have_forced = buffer_get_int(&m);
382 forced_command = have_forced ? xstrdup("true") : NULL;
384 buffer_free(&m);
386 return (allowed);
390 * This key verify needs to send the key type along, because the
391 * privileged parent makes the decision if the key is allowed
392 * for authentication.
396 mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
398 Buffer m;
399 u_char *blob;
400 u_int len;
401 int verified = 0;
403 debug3("%s entering", __func__);
405 /* Convert the key to a blob and the pass it over */
406 if (!key_to_blob(key, &blob, &len))
407 return (0);
409 buffer_init(&m);
410 buffer_put_string(&m, blob, len);
411 buffer_put_string(&m, sig, siglen);
412 buffer_put_string(&m, data, datalen);
413 xfree(blob);
415 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
417 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
418 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
420 verified = buffer_get_int(&m);
422 buffer_free(&m);
424 return (verified);
427 /* Export key state after authentication */
428 Newkeys *
429 mm_newkeys_from_blob(u_char *blob, int blen)
431 Buffer b;
432 u_int len;
433 Newkeys *newkey = NULL;
434 Enc *enc;
435 Mac *mac;
436 Comp *comp;
438 debug3("%s: %p(%d)", __func__, blob, blen);
439 #ifdef DEBUG_PK
440 dump_base64(stderr, blob, blen);
441 #endif
442 buffer_init(&b);
443 buffer_append(&b, blob, blen);
445 newkey = xmalloc(sizeof(*newkey));
446 enc = &newkey->enc;
447 mac = &newkey->mac;
448 comp = &newkey->comp;
450 /* Enc structure */
451 enc->name = buffer_get_string(&b, NULL);
452 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
453 enc->enabled = buffer_get_int(&b);
454 enc->block_size = buffer_get_int(&b);
455 enc->key = buffer_get_string(&b, &enc->key_len);
456 enc->iv = buffer_get_string(&b, &len);
457 if (len != enc->block_size)
458 fatal("%s: bad ivlen: expected %u != %u", __func__,
459 enc->block_size, len);
461 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
462 fatal("%s: bad cipher name %s or pointer %p", __func__,
463 enc->name, enc->cipher);
465 /* Mac structure */
466 mac->name = buffer_get_string(&b, NULL);
467 if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
468 fatal("%s: can not setup mac %s", __func__, mac->name);
469 mac->enabled = buffer_get_int(&b);
470 mac->key = buffer_get_string(&b, &len);
471 if (len > mac->key_len)
472 fatal("%s: bad mac key length: %u > %d", __func__, len,
473 mac->key_len);
474 mac->key_len = len;
476 /* Comp structure */
477 comp->type = buffer_get_int(&b);
478 comp->enabled = buffer_get_int(&b);
479 comp->name = buffer_get_string(&b, NULL);
481 len = buffer_len(&b);
482 if (len != 0)
483 error("newkeys_from_blob: remaining bytes in blob %u", len);
484 buffer_free(&b);
485 return (newkey);
489 mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
491 Buffer b;
492 int len;
493 Enc *enc;
494 Mac *mac;
495 Comp *comp;
496 Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode);
498 debug3("%s: converting %p", __func__, newkey);
500 if (newkey == NULL) {
501 error("%s: newkey == NULL", __func__);
502 return 0;
504 enc = &newkey->enc;
505 mac = &newkey->mac;
506 comp = &newkey->comp;
508 buffer_init(&b);
509 /* Enc structure */
510 buffer_put_cstring(&b, enc->name);
511 /* The cipher struct is constant and shared, you export pointer */
512 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
513 buffer_put_int(&b, enc->enabled);
514 buffer_put_int(&b, enc->block_size);
515 buffer_put_string(&b, enc->key, enc->key_len);
516 packet_get_keyiv(mode, enc->iv, enc->block_size);
517 buffer_put_string(&b, enc->iv, enc->block_size);
519 /* Mac structure */
520 buffer_put_cstring(&b, mac->name);
521 buffer_put_int(&b, mac->enabled);
522 buffer_put_string(&b, mac->key, mac->key_len);
524 /* Comp structure */
525 buffer_put_int(&b, comp->type);
526 buffer_put_int(&b, comp->enabled);
527 buffer_put_cstring(&b, comp->name);
529 len = buffer_len(&b);
530 if (lenp != NULL)
531 *lenp = len;
532 if (blobp != NULL) {
533 *blobp = xmalloc(len);
534 memcpy(*blobp, buffer_ptr(&b), len);
536 memset(buffer_ptr(&b), 0, len);
537 buffer_free(&b);
538 return len;
541 static void
542 mm_send_kex(Buffer *m, Kex *kex)
544 buffer_put_string(m, kex->session_id, kex->session_id_len);
545 buffer_put_int(m, kex->we_need);
546 buffer_put_int(m, kex->hostkey_type);
547 buffer_put_int(m, kex->kex_type);
548 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
549 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
550 buffer_put_int(m, kex->flags);
551 buffer_put_cstring(m, kex->client_version_string);
552 buffer_put_cstring(m, kex->server_version_string);
555 void
556 mm_send_keystate(struct monitor *monitor)
558 Buffer m, *input, *output;
559 u_char *blob, *p;
560 u_int bloblen, plen;
561 u_int32_t seqnr, packets;
562 u_int64_t blocks, bytes;
564 buffer_init(&m);
566 if (!compat20) {
567 u_char iv[24];
568 u_char *key;
569 u_int ivlen, keylen;
571 buffer_put_int(&m, packet_get_protocol_flags());
573 buffer_put_int(&m, packet_get_ssh1_cipher());
575 debug3("%s: Sending ssh1 KEY+IV", __func__);
576 keylen = packet_get_encryption_key(NULL);
577 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
578 keylen = packet_get_encryption_key(key);
579 buffer_put_string(&m, key, keylen);
580 memset(key, 0, keylen);
581 xfree(key);
583 ivlen = packet_get_keyiv_len(MODE_OUT);
584 packet_get_keyiv(MODE_OUT, iv, ivlen);
585 buffer_put_string(&m, iv, ivlen);
586 ivlen = packet_get_keyiv_len(MODE_OUT);
587 packet_get_keyiv(MODE_IN, iv, ivlen);
588 buffer_put_string(&m, iv, ivlen);
589 goto skip;
590 } else {
591 /* Kex for rekeying */
592 mm_send_kex(&m, *monitor->m_pkex);
595 debug3("%s: Sending new keys: %p %p",
596 __func__, packet_get_newkeys(MODE_OUT),
597 packet_get_newkeys(MODE_IN));
599 /* Keys from Kex */
600 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
601 fatal("%s: conversion of newkeys failed", __func__);
603 buffer_put_string(&m, blob, bloblen);
604 xfree(blob);
606 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
607 fatal("%s: conversion of newkeys failed", __func__);
609 buffer_put_string(&m, blob, bloblen);
610 xfree(blob);
612 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
613 buffer_put_int(&m, seqnr);
614 buffer_put_int64(&m, blocks);
615 buffer_put_int(&m, packets);
616 buffer_put_int64(&m, bytes);
617 packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
618 buffer_put_int(&m, seqnr);
619 buffer_put_int64(&m, blocks);
620 buffer_put_int(&m, packets);
621 buffer_put_int64(&m, bytes);
623 debug3("%s: New keys have been sent", __func__);
624 skip:
625 /* More key context */
626 plen = packet_get_keycontext(MODE_OUT, NULL);
627 p = xmalloc(plen+1);
628 packet_get_keycontext(MODE_OUT, p);
629 buffer_put_string(&m, p, plen);
630 xfree(p);
632 plen = packet_get_keycontext(MODE_IN, NULL);
633 p = xmalloc(plen+1);
634 packet_get_keycontext(MODE_IN, p);
635 buffer_put_string(&m, p, plen);
636 xfree(p);
638 /* Compression state */
639 debug3("%s: Sending compression state", __func__);
640 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
641 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
643 /* Network I/O buffers */
644 input = (Buffer *)packet_get_input();
645 output = (Buffer *)packet_get_output();
646 buffer_put_string(&m, buffer_ptr(input), buffer_len(input));
647 buffer_put_string(&m, buffer_ptr(output), buffer_len(output));
649 /* Roaming */
650 if (compat20) {
651 buffer_put_int64(&m, get_sent_bytes());
652 buffer_put_int64(&m, get_recv_bytes());
655 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
656 debug3("%s: Finished sending state", __func__);
658 buffer_free(&m);
662 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
664 Buffer m;
665 char *p, *msg;
666 int success = 0, tmp1 = -1, tmp2 = -1;
668 /* Kludge: ensure there are fds free to receive the pty/tty */
669 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
670 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
671 error("%s: cannot allocate fds for pty", __func__);
672 if (tmp1 > 0)
673 close(tmp1);
674 if (tmp2 > 0)
675 close(tmp2);
676 return 0;
678 close(tmp1);
679 close(tmp2);
681 buffer_init(&m);
682 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
684 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
685 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
687 success = buffer_get_int(&m);
688 if (success == 0) {
689 debug3("%s: pty alloc failed", __func__);
690 buffer_free(&m);
691 return (0);
693 p = buffer_get_string(&m, NULL);
694 msg = buffer_get_string(&m, NULL);
695 buffer_free(&m);
697 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
698 xfree(p);
700 buffer_append(&loginmsg, msg, strlen(msg));
701 xfree(msg);
703 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
704 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
705 fatal("%s: receive fds failed", __func__);
707 /* Success */
708 return (1);
711 void
712 mm_session_pty_cleanup2(Session *s)
714 Buffer m;
716 if (s->ttyfd == -1)
717 return;
718 buffer_init(&m);
719 buffer_put_cstring(&m, s->tty);
720 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
721 buffer_free(&m);
723 /* closed dup'ed master */
724 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
725 error("close(s->ptymaster/%d): %s",
726 s->ptymaster, strerror(errno));
728 /* unlink pty from session */
729 s->ttyfd = -1;
732 #ifdef USE_PAM
733 void
734 mm_start_pam(Authctxt *authctxt)
736 Buffer m;
738 debug3("%s entering", __func__);
739 if (!options.use_pam)
740 fatal("UsePAM=no, but ended up in %s anyway", __func__);
742 buffer_init(&m);
743 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
745 buffer_free(&m);
748 u_int
749 mm_do_pam_account(void)
751 Buffer m;
752 u_int ret;
753 char *msg;
755 debug3("%s entering", __func__);
756 if (!options.use_pam)
757 fatal("UsePAM=no, but ended up in %s anyway", __func__);
759 buffer_init(&m);
760 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
762 mm_request_receive_expect(pmonitor->m_recvfd,
763 MONITOR_ANS_PAM_ACCOUNT, &m);
764 ret = buffer_get_int(&m);
765 msg = buffer_get_string(&m, NULL);
766 buffer_append(&loginmsg, msg, strlen(msg));
767 xfree(msg);
769 buffer_free(&m);
771 debug3("%s returning %d", __func__, ret);
773 return (ret);
776 void *
777 mm_sshpam_init_ctx(Authctxt *authctxt)
779 Buffer m;
780 int success;
782 debug3("%s", __func__);
783 buffer_init(&m);
784 buffer_put_cstring(&m, authctxt->user);
785 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
786 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
787 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
788 success = buffer_get_int(&m);
789 if (success == 0) {
790 debug3("%s: pam_init_ctx failed", __func__);
791 buffer_free(&m);
792 return (NULL);
794 buffer_free(&m);
795 return (authctxt);
799 mm_sshpam_query(void *ctx, char **name, char **info,
800 u_int *num, char ***prompts, u_int **echo_on)
802 Buffer m;
803 u_int i;
804 int ret;
806 debug3("%s", __func__);
807 buffer_init(&m);
808 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
809 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
810 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
811 ret = buffer_get_int(&m);
812 debug3("%s: pam_query returned %d", __func__, ret);
813 *name = buffer_get_string(&m, NULL);
814 *info = buffer_get_string(&m, NULL);
815 *num = buffer_get_int(&m);
816 if (*num > PAM_MAX_NUM_MSG)
817 fatal("%s: recieved %u PAM messages, expected <= %u",
818 __func__, *num, PAM_MAX_NUM_MSG);
819 *prompts = xcalloc((*num + 1), sizeof(char *));
820 *echo_on = xcalloc((*num + 1), sizeof(u_int));
821 for (i = 0; i < *num; ++i) {
822 (*prompts)[i] = buffer_get_string(&m, NULL);
823 (*echo_on)[i] = buffer_get_int(&m);
825 buffer_free(&m);
826 return (ret);
830 mm_sshpam_respond(void *ctx, u_int num, char **resp)
832 Buffer m;
833 u_int i;
834 int ret;
836 debug3("%s", __func__);
837 buffer_init(&m);
838 buffer_put_int(&m, num);
839 for (i = 0; i < num; ++i)
840 buffer_put_cstring(&m, resp[i]);
841 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
842 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
843 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
844 ret = buffer_get_int(&m);
845 debug3("%s: pam_respond returned %d", __func__, ret);
846 buffer_free(&m);
847 return (ret);
850 void
851 mm_sshpam_free_ctx(void *ctxtp)
853 Buffer m;
855 debug3("%s", __func__);
856 buffer_init(&m);
857 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
858 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
859 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
860 buffer_free(&m);
862 #endif /* USE_PAM */
864 /* Request process termination */
866 void
867 mm_terminate(void)
869 Buffer m;
871 buffer_init(&m);
872 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
873 buffer_free(&m);
877 mm_ssh1_session_key(BIGNUM *num)
879 int rsafail;
880 Buffer m;
882 buffer_init(&m);
883 buffer_put_bignum2(&m, num);
884 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
886 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
888 rsafail = buffer_get_int(&m);
889 buffer_get_bignum2(&m, num);
891 buffer_free(&m);
893 return (rsafail);
896 static void
897 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
898 char ***prompts, u_int **echo_on)
900 *name = xstrdup("");
901 *infotxt = xstrdup("");
902 *numprompts = 1;
903 *prompts = xcalloc(*numprompts, sizeof(char *));
904 *echo_on = xcalloc(*numprompts, sizeof(u_int));
905 (*echo_on)[0] = 0;
909 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
910 u_int *numprompts, char ***prompts, u_int **echo_on)
912 Buffer m;
913 u_int success;
914 char *challenge;
916 debug3("%s: entering", __func__);
918 buffer_init(&m);
919 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
921 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
922 &m);
923 success = buffer_get_int(&m);
924 if (success == 0) {
925 debug3("%s: no challenge", __func__);
926 buffer_free(&m);
927 return (-1);
930 /* Get the challenge, and format the response */
931 challenge = buffer_get_string(&m, NULL);
932 buffer_free(&m);
934 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
935 (*prompts)[0] = challenge;
937 debug3("%s: received challenge: %s", __func__, challenge);
939 return (0);
943 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
945 Buffer m;
946 int authok;
948 debug3("%s: entering", __func__);
949 if (numresponses != 1)
950 return (-1);
952 buffer_init(&m);
953 buffer_put_cstring(&m, responses[0]);
954 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
956 mm_request_receive_expect(pmonitor->m_recvfd,
957 MONITOR_ANS_BSDAUTHRESPOND, &m);
959 authok = buffer_get_int(&m);
960 buffer_free(&m);
962 return ((authok == 0) ? -1 : 0);
965 #ifdef SKEY
967 mm_skey_query(void *ctx, char **name, char **infotxt,
968 u_int *numprompts, char ***prompts, u_int **echo_on)
970 Buffer m;
971 u_int success;
972 char *challenge;
974 debug3("%s: entering", __func__);
976 buffer_init(&m);
977 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
979 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
980 &m);
981 success = buffer_get_int(&m);
982 if (success == 0) {
983 debug3("%s: no challenge", __func__);
984 buffer_free(&m);
985 return (-1);
988 /* Get the challenge, and format the response */
989 challenge = buffer_get_string(&m, NULL);
990 buffer_free(&m);
992 debug3("%s: received challenge: %s", __func__, challenge);
994 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
996 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
997 xfree(challenge);
999 return (0);
1003 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
1005 Buffer m;
1006 int authok;
1008 debug3("%s: entering", __func__);
1009 if (numresponses != 1)
1010 return (-1);
1012 buffer_init(&m);
1013 buffer_put_cstring(&m, responses[0]);
1014 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
1016 mm_request_receive_expect(pmonitor->m_recvfd,
1017 MONITOR_ANS_SKEYRESPOND, &m);
1019 authok = buffer_get_int(&m);
1020 buffer_free(&m);
1022 return ((authok == 0) ? -1 : 0);
1024 #endif /* SKEY */
1026 void
1027 mm_ssh1_session_id(u_char session_id[16])
1029 Buffer m;
1030 int i;
1032 debug3("%s entering", __func__);
1034 buffer_init(&m);
1035 for (i = 0; i < 16; i++)
1036 buffer_put_char(&m, session_id[i]);
1038 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1039 buffer_free(&m);
1043 mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1045 Buffer m;
1046 Key *key;
1047 u_char *blob;
1048 u_int blen;
1049 int allowed = 0, have_forced = 0;
1051 debug3("%s entering", __func__);
1053 buffer_init(&m);
1054 buffer_put_bignum2(&m, client_n);
1056 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1057 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1059 allowed = buffer_get_int(&m);
1061 /* fake forced command */
1062 auth_clear_options();
1063 have_forced = buffer_get_int(&m);
1064 forced_command = have_forced ? xstrdup("true") : NULL;
1066 if (allowed && rkey != NULL) {
1067 blob = buffer_get_string(&m, &blen);
1068 if ((key = key_from_blob(blob, blen)) == NULL)
1069 fatal("%s: key_from_blob failed", __func__);
1070 *rkey = key;
1071 xfree(blob);
1073 buffer_free(&m);
1075 return (allowed);
1078 BIGNUM *
1079 mm_auth_rsa_generate_challenge(Key *key)
1081 Buffer m;
1082 BIGNUM *challenge;
1083 u_char *blob;
1084 u_int blen;
1086 debug3("%s entering", __func__);
1088 if ((challenge = BN_new()) == NULL)
1089 fatal("%s: BN_new failed", __func__);
1091 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1092 if (key_to_blob(key, &blob, &blen) == 0)
1093 fatal("%s: key_to_blob failed", __func__);
1094 key->type = KEY_RSA1;
1096 buffer_init(&m);
1097 buffer_put_string(&m, blob, blen);
1098 xfree(blob);
1100 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1101 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1103 buffer_get_bignum2(&m, challenge);
1104 buffer_free(&m);
1106 return (challenge);
1110 mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1112 Buffer m;
1113 u_char *blob;
1114 u_int blen;
1115 int success = 0;
1117 debug3("%s entering", __func__);
1119 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1120 if (key_to_blob(key, &blob, &blen) == 0)
1121 fatal("%s: key_to_blob failed", __func__);
1122 key->type = KEY_RSA1;
1124 buffer_init(&m);
1125 buffer_put_string(&m, blob, blen);
1126 buffer_put_string(&m, response, 16);
1127 xfree(blob);
1129 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1130 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1132 success = buffer_get_int(&m);
1133 buffer_free(&m);
1135 return (success);
1138 #ifdef SSH_AUDIT_EVENTS
1139 void
1140 mm_audit_event(ssh_audit_event_t event)
1142 Buffer m;
1144 debug3("%s entering", __func__);
1146 buffer_init(&m);
1147 buffer_put_int(&m, event);
1149 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1150 buffer_free(&m);
1153 void
1154 mm_audit_run_command(const char *command)
1156 Buffer m;
1158 debug3("%s entering command %s", __func__, command);
1160 buffer_init(&m);
1161 buffer_put_cstring(&m, command);
1163 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1164 buffer_free(&m);
1166 #endif /* SSH_AUDIT_EVENTS */
1168 #ifdef GSSAPI
1169 OM_uint32
1170 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1172 Buffer m;
1173 OM_uint32 major;
1175 /* Client doesn't get to see the context */
1176 *ctx = NULL;
1178 buffer_init(&m);
1179 buffer_put_string(&m, goid->elements, goid->length);
1181 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1182 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1184 major = buffer_get_int(&m);
1186 buffer_free(&m);
1187 return (major);
1190 OM_uint32
1191 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1192 gss_buffer_desc *out, OM_uint32 *flags)
1194 Buffer m;
1195 OM_uint32 major;
1196 u_int len;
1198 buffer_init(&m);
1199 buffer_put_string(&m, in->value, in->length);
1201 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1202 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1204 major = buffer_get_int(&m);
1205 out->value = buffer_get_string(&m, &len);
1206 out->length = len;
1207 if (flags)
1208 *flags = buffer_get_int(&m);
1210 buffer_free(&m);
1212 return (major);
1215 OM_uint32
1216 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1218 Buffer m;
1219 OM_uint32 major;
1221 buffer_init(&m);
1222 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1223 buffer_put_string(&m, gssmic->value, gssmic->length);
1225 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1226 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1227 &m);
1229 major = buffer_get_int(&m);
1230 buffer_free(&m);
1231 return(major);
1235 mm_ssh_gssapi_userok(char *user)
1237 Buffer m;
1238 int authenticated = 0;
1240 buffer_init(&m);
1242 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1243 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1244 &m);
1246 authenticated = buffer_get_int(&m);
1248 buffer_free(&m);
1249 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1250 return (authenticated);
1252 #endif /* GSSAPI */
1254 #ifdef JPAKE
1255 void
1256 mm_auth2_jpake_get_pwdata(Authctxt *authctxt, BIGNUM **s,
1257 char **hash_scheme, char **salt)
1259 Buffer m;
1261 debug3("%s entering", __func__);
1263 buffer_init(&m);
1264 mm_request_send(pmonitor->m_recvfd,
1265 MONITOR_REQ_JPAKE_GET_PWDATA, &m);
1267 debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__);
1268 mm_request_receive_expect(pmonitor->m_recvfd,
1269 MONITOR_ANS_JPAKE_GET_PWDATA, &m);
1271 *hash_scheme = buffer_get_string(&m, NULL);
1272 *salt = buffer_get_string(&m, NULL);
1274 buffer_free(&m);
1277 void
1278 mm_jpake_step1(struct modp_group *grp,
1279 u_char **id, u_int *id_len,
1280 BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2,
1281 u_char **priv1_proof, u_int *priv1_proof_len,
1282 u_char **priv2_proof, u_int *priv2_proof_len)
1284 Buffer m;
1286 debug3("%s entering", __func__);
1288 buffer_init(&m);
1289 mm_request_send(pmonitor->m_recvfd,
1290 MONITOR_REQ_JPAKE_STEP1, &m);
1292 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__);
1293 mm_request_receive_expect(pmonitor->m_recvfd,
1294 MONITOR_ANS_JPAKE_STEP1, &m);
1296 if ((*priv1 = BN_new()) == NULL ||
1297 (*priv2 = BN_new()) == NULL ||
1298 (*g_priv1 = BN_new()) == NULL ||
1299 (*g_priv2 = BN_new()) == NULL)
1300 fatal("%s: BN_new", __func__);
1302 *id = buffer_get_string(&m, id_len);
1303 /* priv1 and priv2 are, well, private */
1304 buffer_get_bignum2(&m, *g_priv1);
1305 buffer_get_bignum2(&m, *g_priv2);
1306 *priv1_proof = buffer_get_string(&m, priv1_proof_len);
1307 *priv2_proof = buffer_get_string(&m, priv2_proof_len);
1309 buffer_free(&m);
1312 void
1313 mm_jpake_step2(struct modp_group *grp, BIGNUM *s,
1314 BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2,
1315 const u_char *theirid, u_int theirid_len,
1316 const u_char *myid, u_int myid_len,
1317 const u_char *theirpub1_proof, u_int theirpub1_proof_len,
1318 const u_char *theirpub2_proof, u_int theirpub2_proof_len,
1319 BIGNUM **newpub,
1320 u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len)
1322 Buffer m;
1324 debug3("%s entering", __func__);
1326 buffer_init(&m);
1327 /* monitor already has all bignums except theirpub1, theirpub2 */
1328 buffer_put_bignum2(&m, theirpub1);
1329 buffer_put_bignum2(&m, theirpub2);
1330 /* monitor already knows our id */
1331 buffer_put_string(&m, theirid, theirid_len);
1332 buffer_put_string(&m, theirpub1_proof, theirpub1_proof_len);
1333 buffer_put_string(&m, theirpub2_proof, theirpub2_proof_len);
1335 mm_request_send(pmonitor->m_recvfd,
1336 MONITOR_REQ_JPAKE_STEP2, &m);
1338 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__);
1339 mm_request_receive_expect(pmonitor->m_recvfd,
1340 MONITOR_ANS_JPAKE_STEP2, &m);
1342 if ((*newpub = BN_new()) == NULL)
1343 fatal("%s: BN_new", __func__);
1345 buffer_get_bignum2(&m, *newpub);
1346 *newpub_exponent_proof = buffer_get_string(&m,
1347 newpub_exponent_proof_len);
1349 buffer_free(&m);
1352 void
1353 mm_jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val,
1354 BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2,
1355 BIGNUM *theirpub1, BIGNUM *theirpub2,
1356 const u_char *my_id, u_int my_id_len,
1357 const u_char *their_id, u_int their_id_len,
1358 const u_char *sess_id, u_int sess_id_len,
1359 const u_char *theirpriv2_s_proof, u_int theirpriv2_s_proof_len,
1360 BIGNUM **k,
1361 u_char **confirm_hash, u_int *confirm_hash_len)
1363 Buffer m;
1365 debug3("%s entering", __func__);
1367 buffer_init(&m);
1368 /* monitor already has all bignums except step2_val */
1369 buffer_put_bignum2(&m, step2_val);
1370 /* monitor already knows all the ids */
1371 buffer_put_string(&m, theirpriv2_s_proof, theirpriv2_s_proof_len);
1373 mm_request_send(pmonitor->m_recvfd,
1374 MONITOR_REQ_JPAKE_KEY_CONFIRM, &m);
1376 debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__);
1377 mm_request_receive_expect(pmonitor->m_recvfd,
1378 MONITOR_ANS_JPAKE_KEY_CONFIRM, &m);
1380 /* 'k' is sensitive and stays in the monitor */
1381 *confirm_hash = buffer_get_string(&m, confirm_hash_len);
1383 buffer_free(&m);
1387 mm_jpake_check_confirm(const BIGNUM *k,
1388 const u_char *peer_id, u_int peer_id_len,
1389 const u_char *sess_id, u_int sess_id_len,
1390 const u_char *peer_confirm_hash, u_int peer_confirm_hash_len)
1392 Buffer m;
1393 int success = 0;
1395 debug3("%s entering", __func__);
1397 buffer_init(&m);
1398 /* k is dummy in slave, ignored */
1399 /* monitor knows all the ids */
1400 buffer_put_string(&m, peer_confirm_hash, peer_confirm_hash_len);
1401 mm_request_send(pmonitor->m_recvfd,
1402 MONITOR_REQ_JPAKE_CHECK_CONFIRM, &m);
1404 debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__);
1405 mm_request_receive_expect(pmonitor->m_recvfd,
1406 MONITOR_ANS_JPAKE_CHECK_CONFIRM, &m);
1408 success = buffer_get_int(&m);
1409 buffer_free(&m);
1411 debug3("%s: success = %d", __func__, success);
1412 return success;
1414 #endif /* JPAKE */