1 /* $OpenBSD: monitor_wrap.c,v 1.77 2013/11/06 16:52:11 markus Exp $ */
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
30 #include <sys/types.h>
41 #include <openssl/bn.h>
42 #include <openssl/dh.h>
43 #include <openssl/evp.h>
45 #include "openbsd-compat/sys-queue.h"
55 #include "auth-options.h"
59 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
62 #define TARGET_OS_MAC 1
70 #include "monitor_wrap.h"
72 #include "monitor_fdpass.h"
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;
92 mm_log_handler(LogLevel level, const char *msg, void *ctx)
95 struct monitor *mon = (struct monitor *)ctx;
97 if (mon->m_log_sendfd == -1)
98 fatal("%s: no log channel", __func__);
100 buffer_init(&log_msg);
102 * Placeholder for packet length. Will be filled in with the actual
103 * packet length once the packet has been constucted. This saves
106 buffer_put_int(&log_msg, 0);
108 buffer_put_int(&log_msg, level);
109 buffer_put_cstring(&log_msg, msg);
110 put_u32(buffer_ptr(&log_msg), buffer_len(&log_msg) - 4);
111 if (atomicio(vwrite, mon->m_log_sendfd, buffer_ptr(&log_msg),
112 buffer_len(&log_msg)) != buffer_len(&log_msg))
113 fatal("%s: write: %s", __func__, strerror(errno));
114 buffer_free(&log_msg);
121 * m_pid is only set in the privileged part, and
122 * points to the unprivileged child.
124 return (pmonitor && pmonitor->m_pid > 0);
128 mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
130 u_int mlen = buffer_len(m);
133 debug3("%s entering: type %d", __func__, type);
135 put_u32(buf, mlen + 1);
136 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
137 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
138 fatal("%s: write: %s", __func__, strerror(errno));
139 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
140 fatal("%s: write: %s", __func__, strerror(errno));
144 mm_request_receive(int sock, Buffer *m)
149 debug3("%s entering", __func__);
151 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
154 fatal("%s: read: %s", __func__, strerror(errno));
156 msg_len = get_u32(buf);
157 if (msg_len > 256 * 1024)
158 fatal("%s: read: bad msg_len %d", __func__, msg_len);
160 buffer_append_space(m, msg_len);
161 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
162 fatal("%s: read: %s", __func__, strerror(errno));
166 mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
170 debug3("%s entering: type %d", __func__, type);
172 mm_request_receive(sock, m);
173 rtype = buffer_get_char(m);
175 fatal("%s: read: rtype %d != type %d", __func__,
180 mm_choose_dh(int min, int nbits, int max)
187 buffer_put_int(&m, min);
188 buffer_put_int(&m, nbits);
189 buffer_put_int(&m, max);
191 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
193 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
194 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
196 success = buffer_get_char(&m);
198 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
200 if ((p = BN_new()) == NULL)
201 fatal("%s: BN_new failed", __func__);
202 if ((g = BN_new()) == NULL)
203 fatal("%s: BN_new failed", __func__);
204 buffer_get_bignum2(&m, p);
205 buffer_get_bignum2(&m, g);
207 debug3("%s: remaining %d", __func__, buffer_len(&m));
210 return (dh_new_group(g, p));
214 mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
216 Kex *kex = *pmonitor->m_pkex;
219 debug3("%s entering", __func__);
222 buffer_put_int(&m, kex->host_key_index(key));
223 buffer_put_string(&m, data, datalen);
225 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
227 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
228 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
229 *sigp = buffer_get_string(&m, lenp);
236 mm_getpwnamallow(const char *username)
241 ServerOptions *newopts;
243 debug3("%s entering", __func__);
246 buffer_put_cstring(&m, username);
248 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
250 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
251 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
253 if (buffer_get_char(&m) == 0) {
257 pw = buffer_get_string(&m, &len);
258 if (len != sizeof(struct passwd))
259 fatal("%s: struct passwd size mismatch", __func__);
260 pw->pw_name = buffer_get_string(&m, NULL);
261 pw->pw_passwd = buffer_get_string(&m, NULL);
262 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
263 pw->pw_gecos = buffer_get_string(&m, NULL);
265 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
266 pw->pw_class = buffer_get_string(&m, NULL);
268 pw->pw_dir = buffer_get_string(&m, NULL);
269 pw->pw_shell = buffer_get_string(&m, NULL);
272 /* copy options block as a Match directive may have changed some */
273 newopts = buffer_get_string(&m, &len);
274 if (len != sizeof(*newopts))
275 fatal("%s: option block size mismatch", __func__);
277 #define M_CP_STROPT(x) do { \
278 if (newopts->x != NULL) \
279 newopts->x = buffer_get_string(&m, NULL); \
281 #define M_CP_STRARRAYOPT(x, nx) do { \
282 for (i = 0; i < newopts->nx; i++) \
283 newopts->x[i] = buffer_get_string(&m, NULL); \
285 /* See comment in servconf.h */
286 COPY_MATCH_STRING_OPTS();
288 #undef M_CP_STRARRAYOPT
290 copy_set_server_options(&options, newopts, 1);
299 mm_auth2_read_banner(void)
304 debug3("%s entering", __func__);
307 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
310 mm_request_receive_expect(pmonitor->m_recvfd,
311 MONITOR_ANS_AUTH2_READ_BANNER, &m);
312 banner = buffer_get_string(&m, NULL);
315 /* treat empty banner as missing banner */
316 if (strlen(banner) == 0) {
323 /* Inform the privileged process about service and style */
326 mm_inform_authserv(char *service, char *style)
330 debug3("%s entering", __func__);
333 buffer_put_cstring(&m, service);
334 buffer_put_cstring(&m, style ? style : "");
336 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
341 /* Do the password authentication */
343 mm_auth_password(Authctxt *authctxt, char *password)
346 int authenticated = 0;
348 debug3("%s entering", __func__);
351 buffer_put_cstring(&m, password);
352 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
354 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
355 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
357 authenticated = buffer_get_int(&m);
361 debug3("%s: user %sauthenticated",
362 __func__, authenticated ? "" : "not ");
363 return (authenticated);
367 mm_user_key_allowed(struct passwd *pw, Key *key)
369 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
373 mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
376 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
380 mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
381 char *host, Key *key)
385 key->type = KEY_RSA; /* XXX hack for key_to_blob */
386 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
387 key->type = KEY_RSA1;
392 mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
397 int allowed = 0, have_forced = 0;
399 debug3("%s entering", __func__);
401 /* Convert the key to a blob and the pass it over */
402 if (!key_to_blob(key, &blob, &len))
406 buffer_put_int(&m, type);
407 buffer_put_cstring(&m, user ? user : "");
408 buffer_put_cstring(&m, host ? host : "");
409 buffer_put_string(&m, blob, len);
412 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
414 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
415 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
417 allowed = buffer_get_int(&m);
419 /* fake forced command */
420 auth_clear_options();
421 have_forced = buffer_get_int(&m);
422 forced_command = have_forced ? xstrdup("true") : NULL;
430 * This key verify needs to send the key type along, because the
431 * privileged parent makes the decision if the key is allowed
432 * for authentication.
436 mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
443 debug3("%s entering", __func__);
445 /* Convert the key to a blob and the pass it over */
446 if (!key_to_blob(key, &blob, &len))
450 buffer_put_string(&m, blob, len);
451 buffer_put_string(&m, sig, siglen);
452 buffer_put_string(&m, data, datalen);
455 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
457 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
458 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
460 verified = buffer_get_int(&m);
467 /* Export key state after authentication */
469 mm_newkeys_from_blob(u_char *blob, int blen)
473 Newkeys *newkey = NULL;
478 debug3("%s: %p(%d)", __func__, blob, blen);
480 dump_base64(stderr, blob, blen);
483 buffer_append(&b, blob, blen);
485 newkey = xcalloc(1, sizeof(*newkey));
488 comp = &newkey->comp;
491 enc->name = buffer_get_string(&b, NULL);
492 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
493 enc->enabled = buffer_get_int(&b);
494 enc->block_size = buffer_get_int(&b);
495 enc->key = buffer_get_string(&b, &enc->key_len);
496 enc->iv = buffer_get_string(&b, &enc->iv_len);
498 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
499 fatal("%s: bad cipher name %s or pointer %p", __func__,
500 enc->name, enc->cipher);
503 if (cipher_authlen(enc->cipher) == 0) {
504 mac->name = buffer_get_string(&b, NULL);
505 if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
506 fatal("%s: can not setup mac %s", __func__, mac->name);
507 mac->enabled = buffer_get_int(&b);
508 mac->key = buffer_get_string(&b, &len);
509 if (len > mac->key_len)
510 fatal("%s: bad mac key length: %u > %d", __func__, len,
516 comp->type = buffer_get_int(&b);
517 comp->enabled = buffer_get_int(&b);
518 comp->name = buffer_get_string(&b, NULL);
520 len = buffer_len(&b);
522 error("newkeys_from_blob: remaining bytes in blob %u", len);
528 mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
535 Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode);
537 debug3("%s: converting %p", __func__, newkey);
539 if (newkey == NULL) {
540 error("%s: newkey == NULL", __func__);
545 comp = &newkey->comp;
549 buffer_put_cstring(&b, enc->name);
550 /* The cipher struct is constant and shared, you export pointer */
551 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
552 buffer_put_int(&b, enc->enabled);
553 buffer_put_int(&b, enc->block_size);
554 buffer_put_string(&b, enc->key, enc->key_len);
555 packet_get_keyiv(mode, enc->iv, enc->iv_len);
556 buffer_put_string(&b, enc->iv, enc->iv_len);
559 if (cipher_authlen(enc->cipher) == 0) {
560 buffer_put_cstring(&b, mac->name);
561 buffer_put_int(&b, mac->enabled);
562 buffer_put_string(&b, mac->key, mac->key_len);
566 buffer_put_int(&b, comp->type);
567 buffer_put_int(&b, comp->enabled);
568 buffer_put_cstring(&b, comp->name);
570 len = buffer_len(&b);
574 *blobp = xmalloc(len);
575 memcpy(*blobp, buffer_ptr(&b), len);
577 memset(buffer_ptr(&b), 0, len);
583 mm_send_kex(Buffer *m, Kex *kex)
585 buffer_put_string(m, kex->session_id, kex->session_id_len);
586 buffer_put_int(m, kex->we_need);
587 buffer_put_int(m, kex->hostkey_type);
588 buffer_put_int(m, kex->kex_type);
589 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
590 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
591 buffer_put_int(m, kex->flags);
592 buffer_put_cstring(m, kex->client_version_string);
593 buffer_put_cstring(m, kex->server_version_string);
597 mm_send_keystate(struct monitor *monitor)
599 Buffer m, *input, *output;
602 u_int32_t seqnr, packets;
603 u_int64_t blocks, bytes;
612 buffer_put_int(&m, packet_get_protocol_flags());
614 buffer_put_int(&m, packet_get_ssh1_cipher());
616 debug3("%s: Sending ssh1 KEY+IV", __func__);
617 keylen = packet_get_encryption_key(NULL);
618 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
619 keylen = packet_get_encryption_key(key);
620 buffer_put_string(&m, key, keylen);
621 memset(key, 0, keylen);
624 ivlen = packet_get_keyiv_len(MODE_OUT);
625 packet_get_keyiv(MODE_OUT, iv, ivlen);
626 buffer_put_string(&m, iv, ivlen);
627 ivlen = packet_get_keyiv_len(MODE_IN);
628 packet_get_keyiv(MODE_IN, iv, ivlen);
629 buffer_put_string(&m, iv, ivlen);
632 /* Kex for rekeying */
633 mm_send_kex(&m, *monitor->m_pkex);
636 debug3("%s: Sending new keys: %p %p",
637 __func__, packet_get_newkeys(MODE_OUT),
638 packet_get_newkeys(MODE_IN));
641 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
642 fatal("%s: conversion of newkeys failed", __func__);
644 buffer_put_string(&m, blob, bloblen);
647 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
648 fatal("%s: conversion of newkeys failed", __func__);
650 buffer_put_string(&m, blob, bloblen);
653 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
654 buffer_put_int(&m, seqnr);
655 buffer_put_int64(&m, blocks);
656 buffer_put_int(&m, packets);
657 buffer_put_int64(&m, bytes);
658 packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
659 buffer_put_int(&m, seqnr);
660 buffer_put_int64(&m, blocks);
661 buffer_put_int(&m, packets);
662 buffer_put_int64(&m, bytes);
664 debug3("%s: New keys have been sent", __func__);
666 /* More key context */
667 plen = packet_get_keycontext(MODE_OUT, NULL);
669 packet_get_keycontext(MODE_OUT, p);
670 buffer_put_string(&m, p, plen);
673 plen = packet_get_keycontext(MODE_IN, NULL);
675 packet_get_keycontext(MODE_IN, p);
676 buffer_put_string(&m, p, plen);
679 /* Compression state */
680 debug3("%s: Sending compression state", __func__);
681 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
682 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
684 /* Network I/O buffers */
685 input = (Buffer *)packet_get_input();
686 output = (Buffer *)packet_get_output();
687 buffer_put_string(&m, buffer_ptr(input), buffer_len(input));
688 buffer_put_string(&m, buffer_ptr(output), buffer_len(output));
692 buffer_put_int64(&m, get_sent_bytes());
693 buffer_put_int64(&m, get_recv_bytes());
696 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
697 debug3("%s: Finished sending state", __func__);
703 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
707 int success = 0, tmp1 = -1, tmp2 = -1;
709 /* Kludge: ensure there are fds free to receive the pty/tty */
710 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
711 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
712 error("%s: cannot allocate fds for pty", __func__);
723 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
725 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
726 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
728 success = buffer_get_int(&m);
730 debug3("%s: pty alloc failed", __func__);
734 p = buffer_get_string(&m, NULL);
735 msg = buffer_get_string(&m, NULL);
738 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
741 buffer_append(&loginmsg, msg, strlen(msg));
744 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
745 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
746 fatal("%s: receive fds failed", __func__);
753 mm_session_pty_cleanup2(Session *s)
760 buffer_put_cstring(&m, s->tty);
761 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
764 /* closed dup'ed master */
765 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
766 error("close(s->ptymaster/%d): %s",
767 s->ptymaster, strerror(errno));
769 /* unlink pty from session */
775 mm_start_pam(Authctxt *authctxt)
779 debug3("%s entering", __func__);
780 if (!options.use_pam)
781 fatal("UsePAM=no, but ended up in %s anyway", __func__);
784 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
790 mm_do_pam_account(void)
796 debug3("%s entering", __func__);
797 if (!options.use_pam)
798 fatal("UsePAM=no, but ended up in %s anyway", __func__);
801 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
803 mm_request_receive_expect(pmonitor->m_recvfd,
804 MONITOR_ANS_PAM_ACCOUNT, &m);
805 ret = buffer_get_int(&m);
806 msg = buffer_get_string(&m, NULL);
807 buffer_append(&loginmsg, msg, strlen(msg));
812 debug3("%s returning %d", __func__, ret);
818 mm_sshpam_init_ctx(Authctxt *authctxt)
823 debug3("%s", __func__);
825 buffer_put_cstring(&m, authctxt->user);
826 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
827 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
828 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
829 success = buffer_get_int(&m);
831 debug3("%s: pam_init_ctx failed", __func__);
840 mm_sshpam_query(void *ctx, char **name, char **info,
841 u_int *num, char ***prompts, u_int **echo_on)
847 debug3("%s", __func__);
849 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
850 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
851 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
852 ret = buffer_get_int(&m);
853 debug3("%s: pam_query returned %d", __func__, ret);
854 *name = buffer_get_string(&m, NULL);
855 *info = buffer_get_string(&m, NULL);
856 *num = buffer_get_int(&m);
857 if (*num > PAM_MAX_NUM_MSG)
858 fatal("%s: recieved %u PAM messages, expected <= %u",
859 __func__, *num, PAM_MAX_NUM_MSG);
860 *prompts = xcalloc((*num + 1), sizeof(char *));
861 *echo_on = xcalloc((*num + 1), sizeof(u_int));
862 for (i = 0; i < *num; ++i) {
863 (*prompts)[i] = buffer_get_string(&m, NULL);
864 (*echo_on)[i] = buffer_get_int(&m);
871 mm_sshpam_respond(void *ctx, u_int num, char **resp)
877 debug3("%s", __func__);
879 buffer_put_int(&m, num);
880 for (i = 0; i < num; ++i)
881 buffer_put_cstring(&m, resp[i]);
882 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
883 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
884 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
885 ret = buffer_get_int(&m);
886 debug3("%s: pam_respond returned %d", __func__, ret);
892 mm_sshpam_free_ctx(void *ctxtp)
896 debug3("%s", __func__);
898 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
899 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
900 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
905 /* Request process termination */
913 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
918 mm_ssh1_session_key(BIGNUM *num)
924 buffer_put_bignum2(&m, num);
925 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
927 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
929 rsafail = buffer_get_int(&m);
930 buffer_get_bignum2(&m, num);
938 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
939 char ***prompts, u_int **echo_on)
942 *infotxt = xstrdup("");
944 *prompts = xcalloc(*numprompts, sizeof(char *));
945 *echo_on = xcalloc(*numprompts, sizeof(u_int));
950 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
951 u_int *numprompts, char ***prompts, u_int **echo_on)
957 debug3("%s: entering", __func__);
960 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
962 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
964 success = buffer_get_int(&m);
966 debug3("%s: no challenge", __func__);
971 /* Get the challenge, and format the response */
972 challenge = buffer_get_string(&m, NULL);
975 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
976 (*prompts)[0] = challenge;
978 debug3("%s: received challenge: %s", __func__, challenge);
984 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
989 debug3("%s: entering", __func__);
990 if (numresponses != 1)
994 buffer_put_cstring(&m, responses[0]);
995 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
997 mm_request_receive_expect(pmonitor->m_recvfd,
998 MONITOR_ANS_BSDAUTHRESPOND, &m);
1000 authok = buffer_get_int(&m);
1003 return ((authok == 0) ? -1 : 0);
1008 mm_skey_query(void *ctx, char **name, char **infotxt,
1009 u_int *numprompts, char ***prompts, u_int **echo_on)
1015 debug3("%s: entering", __func__);
1018 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
1020 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
1022 success = buffer_get_int(&m);
1024 debug3("%s: no challenge", __func__);
1029 /* Get the challenge, and format the response */
1030 challenge = buffer_get_string(&m, NULL);
1033 debug3("%s: received challenge: %s", __func__, challenge);
1035 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
1037 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
1044 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
1049 debug3("%s: entering", __func__);
1050 if (numresponses != 1)
1054 buffer_put_cstring(&m, responses[0]);
1055 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
1057 mm_request_receive_expect(pmonitor->m_recvfd,
1058 MONITOR_ANS_SKEYRESPOND, &m);
1060 authok = buffer_get_int(&m);
1063 return ((authok == 0) ? -1 : 0);
1068 mm_ssh1_session_id(u_char session_id[16])
1073 debug3("%s entering", __func__);
1076 for (i = 0; i < 16; i++)
1077 buffer_put_char(&m, session_id[i]);
1079 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1084 mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1090 int allowed = 0, have_forced = 0;
1092 debug3("%s entering", __func__);
1095 buffer_put_bignum2(&m, client_n);
1097 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1098 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1100 allowed = buffer_get_int(&m);
1102 /* fake forced command */
1103 auth_clear_options();
1104 have_forced = buffer_get_int(&m);
1105 forced_command = have_forced ? xstrdup("true") : NULL;
1107 if (allowed && rkey != NULL) {
1108 blob = buffer_get_string(&m, &blen);
1109 if ((key = key_from_blob(blob, blen)) == NULL)
1110 fatal("%s: key_from_blob failed", __func__);
1120 mm_auth_rsa_generate_challenge(Key *key)
1127 debug3("%s entering", __func__);
1129 if ((challenge = BN_new()) == NULL)
1130 fatal("%s: BN_new failed", __func__);
1132 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1133 if (key_to_blob(key, &blob, &blen) == 0)
1134 fatal("%s: key_to_blob failed", __func__);
1135 key->type = KEY_RSA1;
1138 buffer_put_string(&m, blob, blen);
1141 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1142 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1144 buffer_get_bignum2(&m, challenge);
1151 mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1158 debug3("%s entering", __func__);
1160 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1161 if (key_to_blob(key, &blob, &blen) == 0)
1162 fatal("%s: key_to_blob failed", __func__);
1163 key->type = KEY_RSA1;
1166 buffer_put_string(&m, blob, blen);
1167 buffer_put_string(&m, response, 16);
1170 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1171 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1173 success = buffer_get_int(&m);
1179 #ifdef SSH_AUDIT_EVENTS
1181 mm_audit_event(ssh_audit_event_t event)
1185 debug3("%s entering", __func__);
1188 buffer_put_int(&m, event);
1190 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1195 mm_audit_run_command(const char *command)
1199 debug3("%s entering command %s", __func__, command);
1202 buffer_put_cstring(&m, command);
1204 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1207 #endif /* SSH_AUDIT_EVENTS */
1211 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1216 /* Client doesn't get to see the context */
1220 buffer_put_string(&m, goid->elements, goid->length);
1222 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1223 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1225 major = buffer_get_int(&m);
1232 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1233 gss_buffer_desc *out, OM_uint32 *flags)
1240 buffer_put_string(&m, in->value, in->length);
1242 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1243 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1245 major = buffer_get_int(&m);
1246 out->value = buffer_get_string(&m, &len);
1249 *flags = buffer_get_int(&m);
1257 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1263 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1264 buffer_put_string(&m, gssmic->value, gssmic->length);
1266 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1267 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1270 major = buffer_get_int(&m);
1276 mm_ssh_gssapi_userok(char *user)
1279 int authenticated = 0;
1283 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1284 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1287 authenticated = buffer_get_int(&m);
1290 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1291 return (authenticated);
1297 mm_auth2_jpake_get_pwdata(Authctxt *authctxt, BIGNUM **s,
1298 char **hash_scheme, char **salt)
1302 debug3("%s entering", __func__);
1305 mm_request_send(pmonitor->m_recvfd,
1306 MONITOR_REQ_JPAKE_GET_PWDATA, &m);
1308 debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__);
1309 mm_request_receive_expect(pmonitor->m_recvfd,
1310 MONITOR_ANS_JPAKE_GET_PWDATA, &m);
1312 *hash_scheme = buffer_get_string(&m, NULL);
1313 *salt = buffer_get_string(&m, NULL);
1319 mm_jpake_step1(struct modp_group *grp,
1320 u_char **id, u_int *id_len,
1321 BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2,
1322 u_char **priv1_proof, u_int *priv1_proof_len,
1323 u_char **priv2_proof, u_int *priv2_proof_len)
1327 debug3("%s entering", __func__);
1330 mm_request_send(pmonitor->m_recvfd,
1331 MONITOR_REQ_JPAKE_STEP1, &m);
1333 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__);
1334 mm_request_receive_expect(pmonitor->m_recvfd,
1335 MONITOR_ANS_JPAKE_STEP1, &m);
1337 if ((*priv1 = BN_new()) == NULL ||
1338 (*priv2 = BN_new()) == NULL ||
1339 (*g_priv1 = BN_new()) == NULL ||
1340 (*g_priv2 = BN_new()) == NULL)
1341 fatal("%s: BN_new", __func__);
1343 *id = buffer_get_string(&m, id_len);
1344 /* priv1 and priv2 are, well, private */
1345 buffer_get_bignum2(&m, *g_priv1);
1346 buffer_get_bignum2(&m, *g_priv2);
1347 *priv1_proof = buffer_get_string(&m, priv1_proof_len);
1348 *priv2_proof = buffer_get_string(&m, priv2_proof_len);
1354 mm_jpake_step2(struct modp_group *grp, BIGNUM *s,
1355 BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2,
1356 const u_char *theirid, u_int theirid_len,
1357 const u_char *myid, u_int myid_len,
1358 const u_char *theirpub1_proof, u_int theirpub1_proof_len,
1359 const u_char *theirpub2_proof, u_int theirpub2_proof_len,
1361 u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len)
1365 debug3("%s entering", __func__);
1368 /* monitor already has all bignums except theirpub1, theirpub2 */
1369 buffer_put_bignum2(&m, theirpub1);
1370 buffer_put_bignum2(&m, theirpub2);
1371 /* monitor already knows our id */
1372 buffer_put_string(&m, theirid, theirid_len);
1373 buffer_put_string(&m, theirpub1_proof, theirpub1_proof_len);
1374 buffer_put_string(&m, theirpub2_proof, theirpub2_proof_len);
1376 mm_request_send(pmonitor->m_recvfd,
1377 MONITOR_REQ_JPAKE_STEP2, &m);
1379 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__);
1380 mm_request_receive_expect(pmonitor->m_recvfd,
1381 MONITOR_ANS_JPAKE_STEP2, &m);
1383 if ((*newpub = BN_new()) == NULL)
1384 fatal("%s: BN_new", __func__);
1386 buffer_get_bignum2(&m, *newpub);
1387 *newpub_exponent_proof = buffer_get_string(&m,
1388 newpub_exponent_proof_len);
1394 mm_jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val,
1395 BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2,
1396 BIGNUM *theirpub1, BIGNUM *theirpub2,
1397 const u_char *my_id, u_int my_id_len,
1398 const u_char *their_id, u_int their_id_len,
1399 const u_char *sess_id, u_int sess_id_len,
1400 const u_char *theirpriv2_s_proof, u_int theirpriv2_s_proof_len,
1402 u_char **confirm_hash, u_int *confirm_hash_len)
1406 debug3("%s entering", __func__);
1409 /* monitor already has all bignums except step2_val */
1410 buffer_put_bignum2(&m, step2_val);
1411 /* monitor already knows all the ids */
1412 buffer_put_string(&m, theirpriv2_s_proof, theirpriv2_s_proof_len);
1414 mm_request_send(pmonitor->m_recvfd,
1415 MONITOR_REQ_JPAKE_KEY_CONFIRM, &m);
1417 debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__);
1418 mm_request_receive_expect(pmonitor->m_recvfd,
1419 MONITOR_ANS_JPAKE_KEY_CONFIRM, &m);
1421 /* 'k' is sensitive and stays in the monitor */
1422 *confirm_hash = buffer_get_string(&m, confirm_hash_len);
1428 mm_jpake_check_confirm(const BIGNUM *k,
1429 const u_char *peer_id, u_int peer_id_len,
1430 const u_char *sess_id, u_int sess_id_len,
1431 const u_char *peer_confirm_hash, u_int peer_confirm_hash_len)
1436 debug3("%s entering", __func__);
1439 /* k is dummy in slave, ignored */
1440 /* monitor knows all the ids */
1441 buffer_put_string(&m, peer_confirm_hash, peer_confirm_hash_len);
1442 mm_request_send(pmonitor->m_recvfd,
1443 MONITOR_REQ_JPAKE_CHECK_CONFIRM, &m);
1445 debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__);
1446 mm_request_receive_expect(pmonitor->m_recvfd,
1447 MONITOR_ANS_JPAKE_CHECK_CONFIRM, &m);
1449 success = buffer_get_int(&m);
1452 debug3("%s: success = %d", __func__, success);