- dtucker@cvs.openbsd.org 2010/03/07 11:57:13
[openssh-git.git] / monitor_wrap.c
blobfaeb02cfa35942fd5bc9644d798b9c9cc683d560
1 /* $OpenBSD: monitor_wrap.c,v 1.69 2010/03/07 11:57:13 dtucker 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"
77 #include "channels.h"
78 #include "session.h"
79 #include "servconf.h"
80 #include "roaming.h"
82 /* Imports */
83 extern int compat20;
84 extern z_stream incoming_stream;
85 extern z_stream outgoing_stream;
86 extern struct monitor *pmonitor;
87 extern Buffer loginmsg;
88 extern ServerOptions options;
90 int
91 mm_is_monitor(void)
94 * m_pid is only set in the privileged part, and
95 * points to the unprivileged child.
97 return (pmonitor && pmonitor->m_pid > 0);
100 void
101 mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
103 u_int mlen = buffer_len(m);
104 u_char buf[5];
106 debug3("%s entering: type %d", __func__, type);
108 put_u32(buf, mlen + 1);
109 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
110 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
111 fatal("%s: write: %s", __func__, strerror(errno));
112 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
113 fatal("%s: write: %s", __func__, strerror(errno));
116 void
117 mm_request_receive(int sock, Buffer *m)
119 u_char buf[4];
120 u_int msg_len;
122 debug3("%s entering", __func__);
124 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
125 if (errno == EPIPE)
126 cleanup_exit(255);
127 fatal("%s: read: %s", __func__, strerror(errno));
129 msg_len = get_u32(buf);
130 if (msg_len > 256 * 1024)
131 fatal("%s: read: bad msg_len %d", __func__, msg_len);
132 buffer_clear(m);
133 buffer_append_space(m, msg_len);
134 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
135 fatal("%s: read: %s", __func__, strerror(errno));
138 void
139 mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
141 u_char rtype;
143 debug3("%s entering: type %d", __func__, type);
145 mm_request_receive(sock, m);
146 rtype = buffer_get_char(m);
147 if (rtype != type)
148 fatal("%s: read: rtype %d != type %d", __func__,
149 rtype, type);
152 DH *
153 mm_choose_dh(int min, int nbits, int max)
155 BIGNUM *p, *g;
156 int success = 0;
157 Buffer m;
159 buffer_init(&m);
160 buffer_put_int(&m, min);
161 buffer_put_int(&m, nbits);
162 buffer_put_int(&m, max);
164 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
166 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
167 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
169 success = buffer_get_char(&m);
170 if (success == 0)
171 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
173 if ((p = BN_new()) == NULL)
174 fatal("%s: BN_new failed", __func__);
175 if ((g = BN_new()) == NULL)
176 fatal("%s: BN_new failed", __func__);
177 buffer_get_bignum2(&m, p);
178 buffer_get_bignum2(&m, g);
180 debug3("%s: remaining %d", __func__, buffer_len(&m));
181 buffer_free(&m);
183 return (dh_new_group(g, p));
187 mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
189 Kex *kex = *pmonitor->m_pkex;
190 Buffer m;
192 debug3("%s entering", __func__);
194 buffer_init(&m);
195 buffer_put_int(&m, kex->host_key_index(key));
196 buffer_put_string(&m, data, datalen);
198 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
200 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
201 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
202 *sigp = buffer_get_string(&m, lenp);
203 buffer_free(&m);
205 return (0);
208 struct passwd *
209 mm_getpwnamallow(const char *username)
211 Buffer m;
212 struct passwd *pw;
213 u_int len;
214 ServerOptions *newopts;
216 debug3("%s entering", __func__);
218 buffer_init(&m);
219 buffer_put_cstring(&m, username);
221 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
223 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
224 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
226 if (buffer_get_char(&m) == 0) {
227 pw = NULL;
228 goto out;
230 pw = buffer_get_string(&m, &len);
231 if (len != sizeof(struct passwd))
232 fatal("%s: struct passwd size mismatch", __func__);
233 pw->pw_name = buffer_get_string(&m, NULL);
234 pw->pw_passwd = buffer_get_string(&m, NULL);
235 pw->pw_gecos = buffer_get_string(&m, NULL);
236 #ifdef HAVE_PW_CLASS_IN_PASSWD
237 pw->pw_class = buffer_get_string(&m, NULL);
238 #endif
239 pw->pw_dir = buffer_get_string(&m, NULL);
240 pw->pw_shell = buffer_get_string(&m, NULL);
242 out:
243 /* copy options block as a Match directive may have changed some */
244 newopts = buffer_get_string(&m, &len);
245 if (len != sizeof(*newopts))
246 fatal("%s: option block size mismatch", __func__);
247 if (newopts->banner != NULL)
248 newopts->banner = buffer_get_string(&m, NULL);
249 copy_set_server_options(&options, newopts, 1);
250 xfree(newopts);
252 buffer_free(&m);
254 return (pw);
257 char *
258 mm_auth2_read_banner(void)
260 Buffer m;
261 char *banner;
263 debug3("%s entering", __func__);
265 buffer_init(&m);
266 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
267 buffer_clear(&m);
269 mm_request_receive_expect(pmonitor->m_recvfd,
270 MONITOR_ANS_AUTH2_READ_BANNER, &m);
271 banner = buffer_get_string(&m, NULL);
272 buffer_free(&m);
274 /* treat empty banner as missing banner */
275 if (strlen(banner) == 0) {
276 xfree(banner);
277 banner = NULL;
279 return (banner);
282 /* Inform the privileged process about service and style */
284 void
285 mm_inform_authserv(char *service, char *style)
287 Buffer m;
289 debug3("%s entering", __func__);
291 buffer_init(&m);
292 buffer_put_cstring(&m, service);
293 buffer_put_cstring(&m, style ? style : "");
295 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
297 buffer_free(&m);
300 /* Do the password authentication */
302 mm_auth_password(Authctxt *authctxt, char *password)
304 Buffer m;
305 int authenticated = 0;
307 debug3("%s entering", __func__);
309 buffer_init(&m);
310 buffer_put_cstring(&m, password);
311 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
313 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
314 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
316 authenticated = buffer_get_int(&m);
318 buffer_free(&m);
320 debug3("%s: user %sauthenticated",
321 __func__, authenticated ? "" : "not ");
322 return (authenticated);
326 mm_user_key_allowed(struct passwd *pw, Key *key)
328 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
332 mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
333 Key *key)
335 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
339 mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
340 char *host, Key *key)
342 int ret;
344 key->type = KEY_RSA; /* XXX hack for key_to_blob */
345 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
346 key->type = KEY_RSA1;
347 return (ret);
351 mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
353 Buffer m;
354 u_char *blob;
355 u_int len;
356 int allowed = 0, have_forced = 0;
358 debug3("%s entering", __func__);
360 /* Convert the key to a blob and the pass it over */
361 if (!key_to_blob(key, &blob, &len))
362 return (0);
364 buffer_init(&m);
365 buffer_put_int(&m, type);
366 buffer_put_cstring(&m, user ? user : "");
367 buffer_put_cstring(&m, host ? host : "");
368 buffer_put_string(&m, blob, len);
369 xfree(blob);
371 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
373 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
374 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
376 allowed = buffer_get_int(&m);
378 /* fake forced command */
379 auth_clear_options();
380 have_forced = buffer_get_int(&m);
381 forced_command = have_forced ? xstrdup("true") : NULL;
383 buffer_free(&m);
385 return (allowed);
389 * This key verify needs to send the key type along, because the
390 * privileged parent makes the decision if the key is allowed
391 * for authentication.
395 mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
397 Buffer m;
398 u_char *blob;
399 u_int len;
400 int verified = 0;
402 debug3("%s entering", __func__);
404 /* Convert the key to a blob and the pass it over */
405 if (!key_to_blob(key, &blob, &len))
406 return (0);
408 buffer_init(&m);
409 buffer_put_string(&m, blob, len);
410 buffer_put_string(&m, sig, siglen);
411 buffer_put_string(&m, data, datalen);
412 xfree(blob);
414 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
416 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
417 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
419 verified = buffer_get_int(&m);
421 buffer_free(&m);
423 return (verified);
426 /* Export key state after authentication */
427 Newkeys *
428 mm_newkeys_from_blob(u_char *blob, int blen)
430 Buffer b;
431 u_int len;
432 Newkeys *newkey = NULL;
433 Enc *enc;
434 Mac *mac;
435 Comp *comp;
437 debug3("%s: %p(%d)", __func__, blob, blen);
438 #ifdef DEBUG_PK
439 dump_base64(stderr, blob, blen);
440 #endif
441 buffer_init(&b);
442 buffer_append(&b, blob, blen);
444 newkey = xmalloc(sizeof(*newkey));
445 enc = &newkey->enc;
446 mac = &newkey->mac;
447 comp = &newkey->comp;
449 /* Enc structure */
450 enc->name = buffer_get_string(&b, NULL);
451 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
452 enc->enabled = buffer_get_int(&b);
453 enc->block_size = buffer_get_int(&b);
454 enc->key = buffer_get_string(&b, &enc->key_len);
455 enc->iv = buffer_get_string(&b, &len);
456 if (len != enc->block_size)
457 fatal("%s: bad ivlen: expected %u != %u", __func__,
458 enc->block_size, len);
460 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
461 fatal("%s: bad cipher name %s or pointer %p", __func__,
462 enc->name, enc->cipher);
464 /* Mac structure */
465 mac->name = buffer_get_string(&b, NULL);
466 if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
467 fatal("%s: can not setup mac %s", __func__, mac->name);
468 mac->enabled = buffer_get_int(&b);
469 mac->key = buffer_get_string(&b, &len);
470 if (len > mac->key_len)
471 fatal("%s: bad mac key length: %u > %d", __func__, len,
472 mac->key_len);
473 mac->key_len = len;
475 /* Comp structure */
476 comp->type = buffer_get_int(&b);
477 comp->enabled = buffer_get_int(&b);
478 comp->name = buffer_get_string(&b, NULL);
480 len = buffer_len(&b);
481 if (len != 0)
482 error("newkeys_from_blob: remaining bytes in blob %u", len);
483 buffer_free(&b);
484 return (newkey);
488 mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
490 Buffer b;
491 int len;
492 Enc *enc;
493 Mac *mac;
494 Comp *comp;
495 Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode);
497 debug3("%s: converting %p", __func__, newkey);
499 if (newkey == NULL) {
500 error("%s: newkey == NULL", __func__);
501 return 0;
503 enc = &newkey->enc;
504 mac = &newkey->mac;
505 comp = &newkey->comp;
507 buffer_init(&b);
508 /* Enc structure */
509 buffer_put_cstring(&b, enc->name);
510 /* The cipher struct is constant and shared, you export pointer */
511 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
512 buffer_put_int(&b, enc->enabled);
513 buffer_put_int(&b, enc->block_size);
514 buffer_put_string(&b, enc->key, enc->key_len);
515 packet_get_keyiv(mode, enc->iv, enc->block_size);
516 buffer_put_string(&b, enc->iv, enc->block_size);
518 /* Mac structure */
519 buffer_put_cstring(&b, mac->name);
520 buffer_put_int(&b, mac->enabled);
521 buffer_put_string(&b, mac->key, mac->key_len);
523 /* Comp structure */
524 buffer_put_int(&b, comp->type);
525 buffer_put_int(&b, comp->enabled);
526 buffer_put_cstring(&b, comp->name);
528 len = buffer_len(&b);
529 if (lenp != NULL)
530 *lenp = len;
531 if (blobp != NULL) {
532 *blobp = xmalloc(len);
533 memcpy(*blobp, buffer_ptr(&b), len);
535 memset(buffer_ptr(&b), 0, len);
536 buffer_free(&b);
537 return len;
540 static void
541 mm_send_kex(Buffer *m, Kex *kex)
543 buffer_put_string(m, kex->session_id, kex->session_id_len);
544 buffer_put_int(m, kex->we_need);
545 buffer_put_int(m, kex->hostkey_type);
546 buffer_put_int(m, kex->kex_type);
547 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
548 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
549 buffer_put_int(m, kex->flags);
550 buffer_put_cstring(m, kex->client_version_string);
551 buffer_put_cstring(m, kex->server_version_string);
554 void
555 mm_send_keystate(struct monitor *monitor)
557 Buffer m, *input, *output;
558 u_char *blob, *p;
559 u_int bloblen, plen;
560 u_int32_t seqnr, packets;
561 u_int64_t blocks, bytes;
563 buffer_init(&m);
565 if (!compat20) {
566 u_char iv[24];
567 u_char *key;
568 u_int ivlen, keylen;
570 buffer_put_int(&m, packet_get_protocol_flags());
572 buffer_put_int(&m, packet_get_ssh1_cipher());
574 debug3("%s: Sending ssh1 KEY+IV", __func__);
575 keylen = packet_get_encryption_key(NULL);
576 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
577 keylen = packet_get_encryption_key(key);
578 buffer_put_string(&m, key, keylen);
579 memset(key, 0, keylen);
580 xfree(key);
582 ivlen = packet_get_keyiv_len(MODE_OUT);
583 packet_get_keyiv(MODE_OUT, iv, ivlen);
584 buffer_put_string(&m, iv, ivlen);
585 ivlen = packet_get_keyiv_len(MODE_OUT);
586 packet_get_keyiv(MODE_IN, iv, ivlen);
587 buffer_put_string(&m, iv, ivlen);
588 goto skip;
589 } else {
590 /* Kex for rekeying */
591 mm_send_kex(&m, *monitor->m_pkex);
594 debug3("%s: Sending new keys: %p %p",
595 __func__, packet_get_newkeys(MODE_OUT),
596 packet_get_newkeys(MODE_IN));
598 /* Keys from Kex */
599 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
600 fatal("%s: conversion of newkeys failed", __func__);
602 buffer_put_string(&m, blob, bloblen);
603 xfree(blob);
605 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
606 fatal("%s: conversion of newkeys failed", __func__);
608 buffer_put_string(&m, blob, bloblen);
609 xfree(blob);
611 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
612 buffer_put_int(&m, seqnr);
613 buffer_put_int64(&m, blocks);
614 buffer_put_int(&m, packets);
615 buffer_put_int64(&m, bytes);
616 packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
617 buffer_put_int(&m, seqnr);
618 buffer_put_int64(&m, blocks);
619 buffer_put_int(&m, packets);
620 buffer_put_int64(&m, bytes);
622 debug3("%s: New keys have been sent", __func__);
623 skip:
624 /* More key context */
625 plen = packet_get_keycontext(MODE_OUT, NULL);
626 p = xmalloc(plen+1);
627 packet_get_keycontext(MODE_OUT, p);
628 buffer_put_string(&m, p, plen);
629 xfree(p);
631 plen = packet_get_keycontext(MODE_IN, NULL);
632 p = xmalloc(plen+1);
633 packet_get_keycontext(MODE_IN, p);
634 buffer_put_string(&m, p, plen);
635 xfree(p);
637 /* Compression state */
638 debug3("%s: Sending compression state", __func__);
639 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
640 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
642 /* Network I/O buffers */
643 input = (Buffer *)packet_get_input();
644 output = (Buffer *)packet_get_output();
645 buffer_put_string(&m, buffer_ptr(input), buffer_len(input));
646 buffer_put_string(&m, buffer_ptr(output), buffer_len(output));
648 /* Roaming */
649 if (compat20) {
650 buffer_put_int64(&m, get_sent_bytes());
651 buffer_put_int64(&m, get_recv_bytes());
654 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
655 debug3("%s: Finished sending state", __func__);
657 buffer_free(&m);
661 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
663 Buffer m;
664 char *p, *msg;
665 int success = 0, tmp1 = -1, tmp2 = -1;
667 /* Kludge: ensure there are fds free to receive the pty/tty */
668 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
669 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
670 error("%s: cannot allocate fds for pty", __func__);
671 if (tmp1 > 0)
672 close(tmp1);
673 if (tmp2 > 0)
674 close(tmp2);
675 return 0;
677 close(tmp1);
678 close(tmp2);
680 buffer_init(&m);
681 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
683 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
684 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
686 success = buffer_get_int(&m);
687 if (success == 0) {
688 debug3("%s: pty alloc failed", __func__);
689 buffer_free(&m);
690 return (0);
692 p = buffer_get_string(&m, NULL);
693 msg = buffer_get_string(&m, NULL);
694 buffer_free(&m);
696 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
697 xfree(p);
699 buffer_append(&loginmsg, msg, strlen(msg));
700 xfree(msg);
702 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
703 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
704 fatal("%s: receive fds failed", __func__);
706 /* Success */
707 return (1);
710 void
711 mm_session_pty_cleanup2(Session *s)
713 Buffer m;
715 if (s->ttyfd == -1)
716 return;
717 buffer_init(&m);
718 buffer_put_cstring(&m, s->tty);
719 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
720 buffer_free(&m);
722 /* closed dup'ed master */
723 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
724 error("close(s->ptymaster/%d): %s",
725 s->ptymaster, strerror(errno));
727 /* unlink pty from session */
728 s->ttyfd = -1;
731 #ifdef USE_PAM
732 void
733 mm_start_pam(Authctxt *authctxt)
735 Buffer m;
737 debug3("%s entering", __func__);
738 if (!options.use_pam)
739 fatal("UsePAM=no, but ended up in %s anyway", __func__);
741 buffer_init(&m);
742 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
744 buffer_free(&m);
747 u_int
748 mm_do_pam_account(void)
750 Buffer m;
751 u_int ret;
752 char *msg;
754 debug3("%s entering", __func__);
755 if (!options.use_pam)
756 fatal("UsePAM=no, but ended up in %s anyway", __func__);
758 buffer_init(&m);
759 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
761 mm_request_receive_expect(pmonitor->m_recvfd,
762 MONITOR_ANS_PAM_ACCOUNT, &m);
763 ret = buffer_get_int(&m);
764 msg = buffer_get_string(&m, NULL);
765 buffer_append(&loginmsg, msg, strlen(msg));
766 xfree(msg);
768 buffer_free(&m);
770 debug3("%s returning %d", __func__, ret);
772 return (ret);
775 void *
776 mm_sshpam_init_ctx(Authctxt *authctxt)
778 Buffer m;
779 int success;
781 debug3("%s", __func__);
782 buffer_init(&m);
783 buffer_put_cstring(&m, authctxt->user);
784 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
785 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
786 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
787 success = buffer_get_int(&m);
788 if (success == 0) {
789 debug3("%s: pam_init_ctx failed", __func__);
790 buffer_free(&m);
791 return (NULL);
793 buffer_free(&m);
794 return (authctxt);
798 mm_sshpam_query(void *ctx, char **name, char **info,
799 u_int *num, char ***prompts, u_int **echo_on)
801 Buffer m;
802 u_int i;
803 int ret;
805 debug3("%s", __func__);
806 buffer_init(&m);
807 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
808 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
809 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
810 ret = buffer_get_int(&m);
811 debug3("%s: pam_query returned %d", __func__, ret);
812 *name = buffer_get_string(&m, NULL);
813 *info = buffer_get_string(&m, NULL);
814 *num = buffer_get_int(&m);
815 if (*num > PAM_MAX_NUM_MSG)
816 fatal("%s: recieved %u PAM messages, expected <= %u",
817 __func__, *num, PAM_MAX_NUM_MSG);
818 *prompts = xcalloc((*num + 1), sizeof(char *));
819 *echo_on = xcalloc((*num + 1), sizeof(u_int));
820 for (i = 0; i < *num; ++i) {
821 (*prompts)[i] = buffer_get_string(&m, NULL);
822 (*echo_on)[i] = buffer_get_int(&m);
824 buffer_free(&m);
825 return (ret);
829 mm_sshpam_respond(void *ctx, u_int num, char **resp)
831 Buffer m;
832 u_int i;
833 int ret;
835 debug3("%s", __func__);
836 buffer_init(&m);
837 buffer_put_int(&m, num);
838 for (i = 0; i < num; ++i)
839 buffer_put_cstring(&m, resp[i]);
840 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
841 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
842 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
843 ret = buffer_get_int(&m);
844 debug3("%s: pam_respond returned %d", __func__, ret);
845 buffer_free(&m);
846 return (ret);
849 void
850 mm_sshpam_free_ctx(void *ctxtp)
852 Buffer m;
854 debug3("%s", __func__);
855 buffer_init(&m);
856 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
857 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
858 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
859 buffer_free(&m);
861 #endif /* USE_PAM */
863 /* Request process termination */
865 void
866 mm_terminate(void)
868 Buffer m;
870 buffer_init(&m);
871 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
872 buffer_free(&m);
876 mm_ssh1_session_key(BIGNUM *num)
878 int rsafail;
879 Buffer m;
881 buffer_init(&m);
882 buffer_put_bignum2(&m, num);
883 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
885 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
887 rsafail = buffer_get_int(&m);
888 buffer_get_bignum2(&m, num);
890 buffer_free(&m);
892 return (rsafail);
895 static void
896 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
897 char ***prompts, u_int **echo_on)
899 *name = xstrdup("");
900 *infotxt = xstrdup("");
901 *numprompts = 1;
902 *prompts = xcalloc(*numprompts, sizeof(char *));
903 *echo_on = xcalloc(*numprompts, sizeof(u_int));
904 (*echo_on)[0] = 0;
908 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
909 u_int *numprompts, char ***prompts, u_int **echo_on)
911 Buffer m;
912 u_int success;
913 char *challenge;
915 debug3("%s: entering", __func__);
917 buffer_init(&m);
918 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
920 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
921 &m);
922 success = buffer_get_int(&m);
923 if (success == 0) {
924 debug3("%s: no challenge", __func__);
925 buffer_free(&m);
926 return (-1);
929 /* Get the challenge, and format the response */
930 challenge = buffer_get_string(&m, NULL);
931 buffer_free(&m);
933 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
934 (*prompts)[0] = challenge;
936 debug3("%s: received challenge: %s", __func__, challenge);
938 return (0);
942 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
944 Buffer m;
945 int authok;
947 debug3("%s: entering", __func__);
948 if (numresponses != 1)
949 return (-1);
951 buffer_init(&m);
952 buffer_put_cstring(&m, responses[0]);
953 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
955 mm_request_receive_expect(pmonitor->m_recvfd,
956 MONITOR_ANS_BSDAUTHRESPOND, &m);
958 authok = buffer_get_int(&m);
959 buffer_free(&m);
961 return ((authok == 0) ? -1 : 0);
964 #ifdef SKEY
966 mm_skey_query(void *ctx, char **name, char **infotxt,
967 u_int *numprompts, char ***prompts, u_int **echo_on)
969 Buffer m;
970 u_int success;
971 char *challenge;
973 debug3("%s: entering", __func__);
975 buffer_init(&m);
976 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
978 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
979 &m);
980 success = buffer_get_int(&m);
981 if (success == 0) {
982 debug3("%s: no challenge", __func__);
983 buffer_free(&m);
984 return (-1);
987 /* Get the challenge, and format the response */
988 challenge = buffer_get_string(&m, NULL);
989 buffer_free(&m);
991 debug3("%s: received challenge: %s", __func__, challenge);
993 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
995 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
996 xfree(challenge);
998 return (0);
1002 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
1004 Buffer m;
1005 int authok;
1007 debug3("%s: entering", __func__);
1008 if (numresponses != 1)
1009 return (-1);
1011 buffer_init(&m);
1012 buffer_put_cstring(&m, responses[0]);
1013 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
1015 mm_request_receive_expect(pmonitor->m_recvfd,
1016 MONITOR_ANS_SKEYRESPOND, &m);
1018 authok = buffer_get_int(&m);
1019 buffer_free(&m);
1021 return ((authok == 0) ? -1 : 0);
1023 #endif /* SKEY */
1025 void
1026 mm_ssh1_session_id(u_char session_id[16])
1028 Buffer m;
1029 int i;
1031 debug3("%s entering", __func__);
1033 buffer_init(&m);
1034 for (i = 0; i < 16; i++)
1035 buffer_put_char(&m, session_id[i]);
1037 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1038 buffer_free(&m);
1042 mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1044 Buffer m;
1045 Key *key;
1046 u_char *blob;
1047 u_int blen;
1048 int allowed = 0, have_forced = 0;
1050 debug3("%s entering", __func__);
1052 buffer_init(&m);
1053 buffer_put_bignum2(&m, client_n);
1055 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1056 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1058 allowed = buffer_get_int(&m);
1060 /* fake forced command */
1061 auth_clear_options();
1062 have_forced = buffer_get_int(&m);
1063 forced_command = have_forced ? xstrdup("true") : NULL;
1065 if (allowed && rkey != NULL) {
1066 blob = buffer_get_string(&m, &blen);
1067 if ((key = key_from_blob(blob, blen)) == NULL)
1068 fatal("%s: key_from_blob failed", __func__);
1069 *rkey = key;
1070 xfree(blob);
1072 buffer_free(&m);
1074 return (allowed);
1077 BIGNUM *
1078 mm_auth_rsa_generate_challenge(Key *key)
1080 Buffer m;
1081 BIGNUM *challenge;
1082 u_char *blob;
1083 u_int blen;
1085 debug3("%s entering", __func__);
1087 if ((challenge = BN_new()) == NULL)
1088 fatal("%s: BN_new failed", __func__);
1090 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1091 if (key_to_blob(key, &blob, &blen) == 0)
1092 fatal("%s: key_to_blob failed", __func__);
1093 key->type = KEY_RSA1;
1095 buffer_init(&m);
1096 buffer_put_string(&m, blob, blen);
1097 xfree(blob);
1099 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1100 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1102 buffer_get_bignum2(&m, challenge);
1103 buffer_free(&m);
1105 return (challenge);
1109 mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1111 Buffer m;
1112 u_char *blob;
1113 u_int blen;
1114 int success = 0;
1116 debug3("%s entering", __func__);
1118 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1119 if (key_to_blob(key, &blob, &blen) == 0)
1120 fatal("%s: key_to_blob failed", __func__);
1121 key->type = KEY_RSA1;
1123 buffer_init(&m);
1124 buffer_put_string(&m, blob, blen);
1125 buffer_put_string(&m, response, 16);
1126 xfree(blob);
1128 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1129 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1131 success = buffer_get_int(&m);
1132 buffer_free(&m);
1134 return (success);
1137 #ifdef SSH_AUDIT_EVENTS
1138 void
1139 mm_audit_event(ssh_audit_event_t event)
1141 Buffer m;
1143 debug3("%s entering", __func__);
1145 buffer_init(&m);
1146 buffer_put_int(&m, event);
1148 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1149 buffer_free(&m);
1152 void
1153 mm_audit_run_command(const char *command)
1155 Buffer m;
1157 debug3("%s entering command %s", __func__, command);
1159 buffer_init(&m);
1160 buffer_put_cstring(&m, command);
1162 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1163 buffer_free(&m);
1165 #endif /* SSH_AUDIT_EVENTS */
1167 #ifdef GSSAPI
1168 OM_uint32
1169 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1171 Buffer m;
1172 OM_uint32 major;
1174 /* Client doesn't get to see the context */
1175 *ctx = NULL;
1177 buffer_init(&m);
1178 buffer_put_string(&m, goid->elements, goid->length);
1180 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1181 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1183 major = buffer_get_int(&m);
1185 buffer_free(&m);
1186 return (major);
1189 OM_uint32
1190 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1191 gss_buffer_desc *out, OM_uint32 *flags)
1193 Buffer m;
1194 OM_uint32 major;
1195 u_int len;
1197 buffer_init(&m);
1198 buffer_put_string(&m, in->value, in->length);
1200 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1201 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1203 major = buffer_get_int(&m);
1204 out->value = buffer_get_string(&m, &len);
1205 out->length = len;
1206 if (flags)
1207 *flags = buffer_get_int(&m);
1209 buffer_free(&m);
1211 return (major);
1214 OM_uint32
1215 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1217 Buffer m;
1218 OM_uint32 major;
1220 buffer_init(&m);
1221 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1222 buffer_put_string(&m, gssmic->value, gssmic->length);
1224 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1225 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1226 &m);
1228 major = buffer_get_int(&m);
1229 buffer_free(&m);
1230 return(major);
1234 mm_ssh_gssapi_userok(char *user)
1236 Buffer m;
1237 int authenticated = 0;
1239 buffer_init(&m);
1241 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1242 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1243 &m);
1245 authenticated = buffer_get_int(&m);
1247 buffer_free(&m);
1248 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1249 return (authenticated);
1251 #endif /* GSSAPI */
1253 #ifdef JPAKE
1254 void
1255 mm_auth2_jpake_get_pwdata(Authctxt *authctxt, BIGNUM **s,
1256 char **hash_scheme, char **salt)
1258 Buffer m;
1260 debug3("%s entering", __func__);
1262 buffer_init(&m);
1263 mm_request_send(pmonitor->m_recvfd,
1264 MONITOR_REQ_JPAKE_GET_PWDATA, &m);
1266 debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__);
1267 mm_request_receive_expect(pmonitor->m_recvfd,
1268 MONITOR_ANS_JPAKE_GET_PWDATA, &m);
1270 *hash_scheme = buffer_get_string(&m, NULL);
1271 *salt = buffer_get_string(&m, NULL);
1273 buffer_free(&m);
1276 void
1277 mm_jpake_step1(struct modp_group *grp,
1278 u_char **id, u_int *id_len,
1279 BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2,
1280 u_char **priv1_proof, u_int *priv1_proof_len,
1281 u_char **priv2_proof, u_int *priv2_proof_len)
1283 Buffer m;
1285 debug3("%s entering", __func__);
1287 buffer_init(&m);
1288 mm_request_send(pmonitor->m_recvfd,
1289 MONITOR_REQ_JPAKE_STEP1, &m);
1291 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__);
1292 mm_request_receive_expect(pmonitor->m_recvfd,
1293 MONITOR_ANS_JPAKE_STEP1, &m);
1295 if ((*priv1 = BN_new()) == NULL ||
1296 (*priv2 = BN_new()) == NULL ||
1297 (*g_priv1 = BN_new()) == NULL ||
1298 (*g_priv2 = BN_new()) == NULL)
1299 fatal("%s: BN_new", __func__);
1301 *id = buffer_get_string(&m, id_len);
1302 /* priv1 and priv2 are, well, private */
1303 buffer_get_bignum2(&m, *g_priv1);
1304 buffer_get_bignum2(&m, *g_priv2);
1305 *priv1_proof = buffer_get_string(&m, priv1_proof_len);
1306 *priv2_proof = buffer_get_string(&m, priv2_proof_len);
1308 buffer_free(&m);
1311 void
1312 mm_jpake_step2(struct modp_group *grp, BIGNUM *s,
1313 BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2,
1314 const u_char *theirid, u_int theirid_len,
1315 const u_char *myid, u_int myid_len,
1316 const u_char *theirpub1_proof, u_int theirpub1_proof_len,
1317 const u_char *theirpub2_proof, u_int theirpub2_proof_len,
1318 BIGNUM **newpub,
1319 u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len)
1321 Buffer m;
1323 debug3("%s entering", __func__);
1325 buffer_init(&m);
1326 /* monitor already has all bignums except theirpub1, theirpub2 */
1327 buffer_put_bignum2(&m, theirpub1);
1328 buffer_put_bignum2(&m, theirpub2);
1329 /* monitor already knows our id */
1330 buffer_put_string(&m, theirid, theirid_len);
1331 buffer_put_string(&m, theirpub1_proof, theirpub1_proof_len);
1332 buffer_put_string(&m, theirpub2_proof, theirpub2_proof_len);
1334 mm_request_send(pmonitor->m_recvfd,
1335 MONITOR_REQ_JPAKE_STEP2, &m);
1337 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__);
1338 mm_request_receive_expect(pmonitor->m_recvfd,
1339 MONITOR_ANS_JPAKE_STEP2, &m);
1341 if ((*newpub = BN_new()) == NULL)
1342 fatal("%s: BN_new", __func__);
1344 buffer_get_bignum2(&m, *newpub);
1345 *newpub_exponent_proof = buffer_get_string(&m,
1346 newpub_exponent_proof_len);
1348 buffer_free(&m);
1351 void
1352 mm_jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val,
1353 BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2,
1354 BIGNUM *theirpub1, BIGNUM *theirpub2,
1355 const u_char *my_id, u_int my_id_len,
1356 const u_char *their_id, u_int their_id_len,
1357 const u_char *sess_id, u_int sess_id_len,
1358 const u_char *theirpriv2_s_proof, u_int theirpriv2_s_proof_len,
1359 BIGNUM **k,
1360 u_char **confirm_hash, u_int *confirm_hash_len)
1362 Buffer m;
1364 debug3("%s entering", __func__);
1366 buffer_init(&m);
1367 /* monitor already has all bignums except step2_val */
1368 buffer_put_bignum2(&m, step2_val);
1369 /* monitor already knows all the ids */
1370 buffer_put_string(&m, theirpriv2_s_proof, theirpriv2_s_proof_len);
1372 mm_request_send(pmonitor->m_recvfd,
1373 MONITOR_REQ_JPAKE_KEY_CONFIRM, &m);
1375 debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__);
1376 mm_request_receive_expect(pmonitor->m_recvfd,
1377 MONITOR_ANS_JPAKE_KEY_CONFIRM, &m);
1379 /* 'k' is sensitive and stays in the monitor */
1380 *confirm_hash = buffer_get_string(&m, confirm_hash_len);
1382 buffer_free(&m);
1386 mm_jpake_check_confirm(const BIGNUM *k,
1387 const u_char *peer_id, u_int peer_id_len,
1388 const u_char *sess_id, u_int sess_id_len,
1389 const u_char *peer_confirm_hash, u_int peer_confirm_hash_len)
1391 Buffer m;
1392 int success = 0;
1394 debug3("%s entering", __func__);
1396 buffer_init(&m);
1397 /* k is dummy in slave, ignored */
1398 /* monitor knows all the ids */
1399 buffer_put_string(&m, peer_confirm_hash, peer_confirm_hash_len);
1400 mm_request_send(pmonitor->m_recvfd,
1401 MONITOR_REQ_JPAKE_CHECK_CONFIRM, &m);
1403 debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__);
1404 mm_request_receive_expect(pmonitor->m_recvfd,
1405 MONITOR_ANS_JPAKE_CHECK_CONFIRM, &m);
1407 success = buffer_get_int(&m);
1408 buffer_free(&m);
1410 debug3("%s: success = %d", __func__, success);
1411 return success;
1413 #endif /* JPAKE */