1 /* $OpenBSD: monitor_wrap.c,v 1.68 2009/06/22 05:39:28 dtucker 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"
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
;
94 * m_pid is only set in the privileged part, and
95 * points to the unprivileged child.
97 return (pmonitor
&& pmonitor
->m_pid
> 0);
101 mm_request_send(int sock
, enum monitor_reqtype type
, Buffer
*m
)
103 u_int mlen
= buffer_len(m
);
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
));
117 mm_request_receive(int sock
, Buffer
*m
)
122 debug3("%s entering", __func__
);
124 if (atomicio(read
, sock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
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
);
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
));
139 mm_request_receive_expect(int sock
, enum monitor_reqtype type
, Buffer
*m
)
143 debug3("%s entering: type %d", __func__
, type
);
145 mm_request_receive(sock
, m
);
146 rtype
= buffer_get_char(m
);
148 fatal("%s: read: rtype %d != type %d", __func__
,
153 mm_choose_dh(int min
, int nbits
, int max
)
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
);
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
));
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
;
192 debug3("%s entering", __func__
);
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
);
209 mm_getpwnamallow(const char *username
)
214 ServerOptions
*newopts
;
216 debug3("%s entering", __func__
);
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) {
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
);
239 pw
->pw_dir
= buffer_get_string(&m
, NULL
);
240 pw
->pw_shell
= buffer_get_string(&m
, NULL
);
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);
258 mm_auth2_read_banner(void)
263 debug3("%s entering", __func__
);
266 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTH2_READ_BANNER
, &m
);
269 mm_request_receive_expect(pmonitor
->m_recvfd
,
270 MONITOR_ANS_AUTH2_READ_BANNER
, &m
);
271 banner
= buffer_get_string(&m
, NULL
);
274 /* treat empty banner as missing banner */
275 if (strlen(banner
) == 0) {
282 /* Inform the privileged process about service and style */
285 mm_inform_authserv(char *service
, char *style
)
289 debug3("%s entering", __func__
);
292 buffer_put_cstring(&m
, service
);
293 buffer_put_cstring(&m
, style
? style
: "");
295 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTHSERV
, &m
);
300 /* Do the password authentication */
302 mm_auth_password(Authctxt
*authctxt
, char *password
)
305 int authenticated
= 0;
307 debug3("%s entering", __func__
);
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
);
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
,
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
)
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
;
351 mm_send_debug(Buffer
*m
)
355 while (buffer_len(m
)) {
356 msg
= buffer_get_string(m
, NULL
);
357 debug3("%s: Sending debug: %s", __func__
, msg
);
358 packet_send_debug("%s", msg
);
364 mm_key_allowed(enum mm_keytype type
, char *user
, char *host
, Key
*key
)
369 int allowed
= 0, have_forced
= 0;
371 debug3("%s entering", __func__
);
373 /* Convert the key to a blob and the pass it over */
374 if (!key_to_blob(key
, &blob
, &len
))
378 buffer_put_int(&m
, type
);
379 buffer_put_cstring(&m
, user
? user
: "");
380 buffer_put_cstring(&m
, host
? host
: "");
381 buffer_put_string(&m
, blob
, len
);
384 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_KEYALLOWED
, &m
);
386 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__
);
387 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_KEYALLOWED
, &m
);
389 allowed
= buffer_get_int(&m
);
391 /* fake forced command */
392 auth_clear_options();
393 have_forced
= buffer_get_int(&m
);
394 forced_command
= have_forced
? xstrdup("true") : NULL
;
396 /* Send potential debug messages */
405 * This key verify needs to send the key type along, because the
406 * privileged parent makes the decision if the key is allowed
407 * for authentication.
411 mm_key_verify(Key
*key
, u_char
*sig
, u_int siglen
, u_char
*data
, u_int datalen
)
418 debug3("%s entering", __func__
);
420 /* Convert the key to a blob and the pass it over */
421 if (!key_to_blob(key
, &blob
, &len
))
425 buffer_put_string(&m
, blob
, len
);
426 buffer_put_string(&m
, sig
, siglen
);
427 buffer_put_string(&m
, data
, datalen
);
430 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_KEYVERIFY
, &m
);
432 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__
);
433 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_KEYVERIFY
, &m
);
435 verified
= buffer_get_int(&m
);
442 /* Export key state after authentication */
444 mm_newkeys_from_blob(u_char
*blob
, int blen
)
448 Newkeys
*newkey
= NULL
;
453 debug3("%s: %p(%d)", __func__
, blob
, blen
);
455 dump_base64(stderr
, blob
, blen
);
458 buffer_append(&b
, blob
, blen
);
460 newkey
= xmalloc(sizeof(*newkey
));
463 comp
= &newkey
->comp
;
466 enc
->name
= buffer_get_string(&b
, NULL
);
467 buffer_get(&b
, &enc
->cipher
, sizeof(enc
->cipher
));
468 enc
->enabled
= buffer_get_int(&b
);
469 enc
->block_size
= buffer_get_int(&b
);
470 enc
->key
= buffer_get_string(&b
, &enc
->key_len
);
471 enc
->iv
= buffer_get_string(&b
, &len
);
472 if (len
!= enc
->block_size
)
473 fatal("%s: bad ivlen: expected %u != %u", __func__
,
474 enc
->block_size
, len
);
476 if (enc
->name
== NULL
|| cipher_by_name(enc
->name
) != enc
->cipher
)
477 fatal("%s: bad cipher name %s or pointer %p", __func__
,
478 enc
->name
, enc
->cipher
);
481 mac
->name
= buffer_get_string(&b
, NULL
);
482 if (mac
->name
== NULL
|| mac_setup(mac
, mac
->name
) == -1)
483 fatal("%s: can not setup mac %s", __func__
, mac
->name
);
484 mac
->enabled
= buffer_get_int(&b
);
485 mac
->key
= buffer_get_string(&b
, &len
);
486 if (len
> mac
->key_len
)
487 fatal("%s: bad mac key length: %u > %d", __func__
, len
,
492 comp
->type
= buffer_get_int(&b
);
493 comp
->enabled
= buffer_get_int(&b
);
494 comp
->name
= buffer_get_string(&b
, NULL
);
496 len
= buffer_len(&b
);
498 error("newkeys_from_blob: remaining bytes in blob %u", len
);
504 mm_newkeys_to_blob(int mode
, u_char
**blobp
, u_int
*lenp
)
511 Newkeys
*newkey
= (Newkeys
*)packet_get_newkeys(mode
);
513 debug3("%s: converting %p", __func__
, newkey
);
515 if (newkey
== NULL
) {
516 error("%s: newkey == NULL", __func__
);
521 comp
= &newkey
->comp
;
525 buffer_put_cstring(&b
, enc
->name
);
526 /* The cipher struct is constant and shared, you export pointer */
527 buffer_append(&b
, &enc
->cipher
, sizeof(enc
->cipher
));
528 buffer_put_int(&b
, enc
->enabled
);
529 buffer_put_int(&b
, enc
->block_size
);
530 buffer_put_string(&b
, enc
->key
, enc
->key_len
);
531 packet_get_keyiv(mode
, enc
->iv
, enc
->block_size
);
532 buffer_put_string(&b
, enc
->iv
, enc
->block_size
);
535 buffer_put_cstring(&b
, mac
->name
);
536 buffer_put_int(&b
, mac
->enabled
);
537 buffer_put_string(&b
, mac
->key
, mac
->key_len
);
540 buffer_put_int(&b
, comp
->type
);
541 buffer_put_int(&b
, comp
->enabled
);
542 buffer_put_cstring(&b
, comp
->name
);
544 len
= buffer_len(&b
);
548 *blobp
= xmalloc(len
);
549 memcpy(*blobp
, buffer_ptr(&b
), len
);
551 memset(buffer_ptr(&b
), 0, len
);
557 mm_send_kex(Buffer
*m
, Kex
*kex
)
559 buffer_put_string(m
, kex
->session_id
, kex
->session_id_len
);
560 buffer_put_int(m
, kex
->we_need
);
561 buffer_put_int(m
, kex
->hostkey_type
);
562 buffer_put_int(m
, kex
->kex_type
);
563 buffer_put_string(m
, buffer_ptr(&kex
->my
), buffer_len(&kex
->my
));
564 buffer_put_string(m
, buffer_ptr(&kex
->peer
), buffer_len(&kex
->peer
));
565 buffer_put_int(m
, kex
->flags
);
566 buffer_put_cstring(m
, kex
->client_version_string
);
567 buffer_put_cstring(m
, kex
->server_version_string
);
571 mm_send_keystate(struct monitor
*monitor
)
573 Buffer m
, *input
, *output
;
576 u_int32_t seqnr
, packets
;
577 u_int64_t blocks
, bytes
;
586 buffer_put_int(&m
, packet_get_protocol_flags());
588 buffer_put_int(&m
, packet_get_ssh1_cipher());
590 debug3("%s: Sending ssh1 KEY+IV", __func__
);
591 keylen
= packet_get_encryption_key(NULL
);
592 key
= xmalloc(keylen
+1); /* add 1 if keylen == 0 */
593 keylen
= packet_get_encryption_key(key
);
594 buffer_put_string(&m
, key
, keylen
);
595 memset(key
, 0, keylen
);
598 ivlen
= packet_get_keyiv_len(MODE_OUT
);
599 packet_get_keyiv(MODE_OUT
, iv
, ivlen
);
600 buffer_put_string(&m
, iv
, ivlen
);
601 ivlen
= packet_get_keyiv_len(MODE_OUT
);
602 packet_get_keyiv(MODE_IN
, iv
, ivlen
);
603 buffer_put_string(&m
, iv
, ivlen
);
606 /* Kex for rekeying */
607 mm_send_kex(&m
, *monitor
->m_pkex
);
610 debug3("%s: Sending new keys: %p %p",
611 __func__
, packet_get_newkeys(MODE_OUT
),
612 packet_get_newkeys(MODE_IN
));
615 if (!mm_newkeys_to_blob(MODE_OUT
, &blob
, &bloblen
))
616 fatal("%s: conversion of newkeys failed", __func__
);
618 buffer_put_string(&m
, blob
, bloblen
);
621 if (!mm_newkeys_to_blob(MODE_IN
, &blob
, &bloblen
))
622 fatal("%s: conversion of newkeys failed", __func__
);
624 buffer_put_string(&m
, blob
, bloblen
);
627 packet_get_state(MODE_OUT
, &seqnr
, &blocks
, &packets
, &bytes
);
628 buffer_put_int(&m
, seqnr
);
629 buffer_put_int64(&m
, blocks
);
630 buffer_put_int(&m
, packets
);
631 buffer_put_int64(&m
, bytes
);
632 packet_get_state(MODE_IN
, &seqnr
, &blocks
, &packets
, &bytes
);
633 buffer_put_int(&m
, seqnr
);
634 buffer_put_int64(&m
, blocks
);
635 buffer_put_int(&m
, packets
);
636 buffer_put_int64(&m
, bytes
);
638 debug3("%s: New keys have been sent", __func__
);
640 /* More key context */
641 plen
= packet_get_keycontext(MODE_OUT
, NULL
);
643 packet_get_keycontext(MODE_OUT
, p
);
644 buffer_put_string(&m
, p
, plen
);
647 plen
= packet_get_keycontext(MODE_IN
, NULL
);
649 packet_get_keycontext(MODE_IN
, p
);
650 buffer_put_string(&m
, p
, plen
);
653 /* Compression state */
654 debug3("%s: Sending compression state", __func__
);
655 buffer_put_string(&m
, &outgoing_stream
, sizeof(outgoing_stream
));
656 buffer_put_string(&m
, &incoming_stream
, sizeof(incoming_stream
));
658 /* Network I/O buffers */
659 input
= (Buffer
*)packet_get_input();
660 output
= (Buffer
*)packet_get_output();
661 buffer_put_string(&m
, buffer_ptr(input
), buffer_len(input
));
662 buffer_put_string(&m
, buffer_ptr(output
), buffer_len(output
));
666 buffer_put_int64(&m
, get_sent_bytes());
667 buffer_put_int64(&m
, get_recv_bytes());
670 mm_request_send(monitor
->m_recvfd
, MONITOR_REQ_KEYEXPORT
, &m
);
671 debug3("%s: Finished sending state", __func__
);
677 mm_pty_allocate(int *ptyfd
, int *ttyfd
, char *namebuf
, size_t namebuflen
)
681 int success
= 0, tmp1
= -1, tmp2
= -1;
683 /* Kludge: ensure there are fds free to receive the pty/tty */
684 if ((tmp1
= dup(pmonitor
->m_recvfd
)) == -1 ||
685 (tmp2
= dup(pmonitor
->m_recvfd
)) == -1) {
686 error("%s: cannot allocate fds for pty", __func__
);
697 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PTY
, &m
);
699 debug3("%s: waiting for MONITOR_ANS_PTY", __func__
);
700 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PTY
, &m
);
702 success
= buffer_get_int(&m
);
704 debug3("%s: pty alloc failed", __func__
);
708 p
= buffer_get_string(&m
, NULL
);
709 msg
= buffer_get_string(&m
, NULL
);
712 strlcpy(namebuf
, p
, namebuflen
); /* Possible truncation */
715 buffer_append(&loginmsg
, msg
, strlen(msg
));
718 if ((*ptyfd
= mm_receive_fd(pmonitor
->m_recvfd
)) == -1 ||
719 (*ttyfd
= mm_receive_fd(pmonitor
->m_recvfd
)) == -1)
720 fatal("%s: receive fds failed", __func__
);
727 mm_session_pty_cleanup2(Session
*s
)
734 buffer_put_cstring(&m
, s
->tty
);
735 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PTYCLEANUP
, &m
);
738 /* closed dup'ed master */
739 if (s
->ptymaster
!= -1 && close(s
->ptymaster
) < 0)
740 error("close(s->ptymaster/%d): %s",
741 s
->ptymaster
, strerror(errno
));
743 /* unlink pty from session */
749 mm_start_pam(Authctxt
*authctxt
)
753 debug3("%s entering", __func__
);
754 if (!options
.use_pam
)
755 fatal("UsePAM=no, but ended up in %s anyway", __func__
);
758 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_START
, &m
);
764 mm_do_pam_account(void)
770 debug3("%s entering", __func__
);
771 if (!options
.use_pam
)
772 fatal("UsePAM=no, but ended up in %s anyway", __func__
);
775 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_ACCOUNT
, &m
);
777 mm_request_receive_expect(pmonitor
->m_recvfd
,
778 MONITOR_ANS_PAM_ACCOUNT
, &m
);
779 ret
= buffer_get_int(&m
);
780 msg
= buffer_get_string(&m
, NULL
);
781 buffer_append(&loginmsg
, msg
, strlen(msg
));
786 debug3("%s returning %d", __func__
, ret
);
792 mm_sshpam_init_ctx(Authctxt
*authctxt
)
797 debug3("%s", __func__
);
799 buffer_put_cstring(&m
, authctxt
->user
);
800 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_INIT_CTX
, &m
);
801 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__
);
802 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_INIT_CTX
, &m
);
803 success
= buffer_get_int(&m
);
805 debug3("%s: pam_init_ctx failed", __func__
);
814 mm_sshpam_query(void *ctx
, char **name
, char **info
,
815 u_int
*num
, char ***prompts
, u_int
**echo_on
)
821 debug3("%s", __func__
);
823 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_QUERY
, &m
);
824 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__
);
825 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_QUERY
, &m
);
826 ret
= buffer_get_int(&m
);
827 debug3("%s: pam_query returned %d", __func__
, ret
);
828 *name
= buffer_get_string(&m
, NULL
);
829 *info
= buffer_get_string(&m
, NULL
);
830 *num
= buffer_get_int(&m
);
831 if (*num
> PAM_MAX_NUM_MSG
)
832 fatal("%s: recieved %u PAM messages, expected <= %u",
833 __func__
, *num
, PAM_MAX_NUM_MSG
);
834 *prompts
= xcalloc((*num
+ 1), sizeof(char *));
835 *echo_on
= xcalloc((*num
+ 1), sizeof(u_int
));
836 for (i
= 0; i
< *num
; ++i
) {
837 (*prompts
)[i
] = buffer_get_string(&m
, NULL
);
838 (*echo_on
)[i
] = buffer_get_int(&m
);
845 mm_sshpam_respond(void *ctx
, u_int num
, char **resp
)
851 debug3("%s", __func__
);
853 buffer_put_int(&m
, num
);
854 for (i
= 0; i
< num
; ++i
)
855 buffer_put_cstring(&m
, resp
[i
]);
856 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_RESPOND
, &m
);
857 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__
);
858 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_RESPOND
, &m
);
859 ret
= buffer_get_int(&m
);
860 debug3("%s: pam_respond returned %d", __func__
, ret
);
866 mm_sshpam_free_ctx(void *ctxtp
)
870 debug3("%s", __func__
);
872 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_FREE_CTX
, &m
);
873 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__
);
874 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_FREE_CTX
, &m
);
879 /* Request process termination */
887 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_TERM
, &m
);
892 mm_ssh1_session_key(BIGNUM
*num
)
898 buffer_put_bignum2(&m
, num
);
899 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SESSKEY
, &m
);
901 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SESSKEY
, &m
);
903 rsafail
= buffer_get_int(&m
);
904 buffer_get_bignum2(&m
, num
);
912 mm_chall_setup(char **name
, char **infotxt
, u_int
*numprompts
,
913 char ***prompts
, u_int
**echo_on
)
916 *infotxt
= xstrdup("");
918 *prompts
= xcalloc(*numprompts
, sizeof(char *));
919 *echo_on
= xcalloc(*numprompts
, sizeof(u_int
));
924 mm_bsdauth_query(void *ctx
, char **name
, char **infotxt
,
925 u_int
*numprompts
, char ***prompts
, u_int
**echo_on
)
931 debug3("%s: entering", __func__
);
934 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_BSDAUTHQUERY
, &m
);
936 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_BSDAUTHQUERY
,
938 success
= buffer_get_int(&m
);
940 debug3("%s: no challenge", __func__
);
945 /* Get the challenge, and format the response */
946 challenge
= buffer_get_string(&m
, NULL
);
949 mm_chall_setup(name
, infotxt
, numprompts
, prompts
, echo_on
);
950 (*prompts
)[0] = challenge
;
952 debug3("%s: received challenge: %s", __func__
, challenge
);
958 mm_bsdauth_respond(void *ctx
, u_int numresponses
, char **responses
)
963 debug3("%s: entering", __func__
);
964 if (numresponses
!= 1)
968 buffer_put_cstring(&m
, responses
[0]);
969 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_BSDAUTHRESPOND
, &m
);
971 mm_request_receive_expect(pmonitor
->m_recvfd
,
972 MONITOR_ANS_BSDAUTHRESPOND
, &m
);
974 authok
= buffer_get_int(&m
);
977 return ((authok
== 0) ? -1 : 0);
982 mm_skey_query(void *ctx
, char **name
, char **infotxt
,
983 u_int
*numprompts
, char ***prompts
, u_int
**echo_on
)
989 debug3("%s: entering", __func__
);
992 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SKEYQUERY
, &m
);
994 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SKEYQUERY
,
996 success
= buffer_get_int(&m
);
998 debug3("%s: no challenge", __func__
);
1003 /* Get the challenge, and format the response */
1004 challenge
= buffer_get_string(&m
, NULL
);
1007 debug3("%s: received challenge: %s", __func__
, challenge
);
1009 mm_chall_setup(name
, infotxt
, numprompts
, prompts
, echo_on
);
1011 xasprintf(*prompts
, "%s%s", challenge
, SKEY_PROMPT
);
1018 mm_skey_respond(void *ctx
, u_int numresponses
, char **responses
)
1023 debug3("%s: entering", __func__
);
1024 if (numresponses
!= 1)
1028 buffer_put_cstring(&m
, responses
[0]);
1029 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SKEYRESPOND
, &m
);
1031 mm_request_receive_expect(pmonitor
->m_recvfd
,
1032 MONITOR_ANS_SKEYRESPOND
, &m
);
1034 authok
= buffer_get_int(&m
);
1037 return ((authok
== 0) ? -1 : 0);
1042 mm_ssh1_session_id(u_char session_id
[16])
1047 debug3("%s entering", __func__
);
1050 for (i
= 0; i
< 16; i
++)
1051 buffer_put_char(&m
, session_id
[i
]);
1053 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SESSID
, &m
);
1058 mm_auth_rsa_key_allowed(struct passwd
*pw
, BIGNUM
*client_n
, Key
**rkey
)
1064 int allowed
= 0, have_forced
= 0;
1066 debug3("%s entering", __func__
);
1069 buffer_put_bignum2(&m
, client_n
);
1071 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSAKEYALLOWED
, &m
);
1072 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSAKEYALLOWED
, &m
);
1074 allowed
= buffer_get_int(&m
);
1076 /* fake forced command */
1077 auth_clear_options();
1078 have_forced
= buffer_get_int(&m
);
1079 forced_command
= have_forced
? xstrdup("true") : NULL
;
1081 if (allowed
&& rkey
!= NULL
) {
1082 blob
= buffer_get_string(&m
, &blen
);
1083 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1084 fatal("%s: key_from_blob failed", __func__
);
1095 mm_auth_rsa_generate_challenge(Key
*key
)
1102 debug3("%s entering", __func__
);
1104 if ((challenge
= BN_new()) == NULL
)
1105 fatal("%s: BN_new failed", __func__
);
1107 key
->type
= KEY_RSA
; /* XXX cheat for key_to_blob */
1108 if (key_to_blob(key
, &blob
, &blen
) == 0)
1109 fatal("%s: key_to_blob failed", __func__
);
1110 key
->type
= KEY_RSA1
;
1113 buffer_put_string(&m
, blob
, blen
);
1116 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSACHALLENGE
, &m
);
1117 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSACHALLENGE
, &m
);
1119 buffer_get_bignum2(&m
, challenge
);
1126 mm_auth_rsa_verify_response(Key
*key
, BIGNUM
*p
, u_char response
[16])
1133 debug3("%s entering", __func__
);
1135 key
->type
= KEY_RSA
; /* XXX cheat for key_to_blob */
1136 if (key_to_blob(key
, &blob
, &blen
) == 0)
1137 fatal("%s: key_to_blob failed", __func__
);
1138 key
->type
= KEY_RSA1
;
1141 buffer_put_string(&m
, blob
, blen
);
1142 buffer_put_string(&m
, response
, 16);
1145 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSARESPONSE
, &m
);
1146 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSARESPONSE
, &m
);
1148 success
= buffer_get_int(&m
);
1154 #ifdef SSH_AUDIT_EVENTS
1156 mm_audit_event(ssh_audit_event_t event
)
1160 debug3("%s entering", __func__
);
1163 buffer_put_int(&m
, event
);
1165 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUDIT_EVENT
, &m
);
1170 mm_audit_run_command(const char *command
)
1174 debug3("%s entering command %s", __func__
, command
);
1177 buffer_put_cstring(&m
, command
);
1179 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUDIT_COMMAND
, &m
);
1182 #endif /* SSH_AUDIT_EVENTS */
1186 mm_ssh_gssapi_server_ctx(Gssctxt
**ctx
, gss_OID goid
)
1191 /* Client doesn't get to see the context */
1195 buffer_put_string(&m
, goid
->elements
, goid
->length
);
1197 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSSETUP
, &m
);
1198 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSSETUP
, &m
);
1200 major
= buffer_get_int(&m
);
1207 mm_ssh_gssapi_accept_ctx(Gssctxt
*ctx
, gss_buffer_desc
*in
,
1208 gss_buffer_desc
*out
, OM_uint32
*flags
)
1215 buffer_put_string(&m
, in
->value
, in
->length
);
1217 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSSTEP
, &m
);
1218 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSSTEP
, &m
);
1220 major
= buffer_get_int(&m
);
1221 out
->value
= buffer_get_string(&m
, &len
);
1224 *flags
= buffer_get_int(&m
);
1232 mm_ssh_gssapi_checkmic(Gssctxt
*ctx
, gss_buffer_t gssbuf
, gss_buffer_t gssmic
)
1238 buffer_put_string(&m
, gssbuf
->value
, gssbuf
->length
);
1239 buffer_put_string(&m
, gssmic
->value
, gssmic
->length
);
1241 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSCHECKMIC
, &m
);
1242 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSCHECKMIC
,
1245 major
= buffer_get_int(&m
);
1251 mm_ssh_gssapi_userok(char *user
)
1254 int authenticated
= 0;
1258 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSUSEROK
, &m
);
1259 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSUSEROK
,
1262 authenticated
= buffer_get_int(&m
);
1265 debug3("%s: user %sauthenticated",__func__
, authenticated
? "" : "not ");
1266 return (authenticated
);
1272 mm_auth2_jpake_get_pwdata(Authctxt
*authctxt
, BIGNUM
**s
,
1273 char **hash_scheme
, char **salt
)
1277 debug3("%s entering", __func__
);
1280 mm_request_send(pmonitor
->m_recvfd
,
1281 MONITOR_REQ_JPAKE_GET_PWDATA
, &m
);
1283 debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__
);
1284 mm_request_receive_expect(pmonitor
->m_recvfd
,
1285 MONITOR_ANS_JPAKE_GET_PWDATA
, &m
);
1287 *hash_scheme
= buffer_get_string(&m
, NULL
);
1288 *salt
= buffer_get_string(&m
, NULL
);
1294 mm_jpake_step1(struct modp_group
*grp
,
1295 u_char
**id
, u_int
*id_len
,
1296 BIGNUM
**priv1
, BIGNUM
**priv2
, BIGNUM
**g_priv1
, BIGNUM
**g_priv2
,
1297 u_char
**priv1_proof
, u_int
*priv1_proof_len
,
1298 u_char
**priv2_proof
, u_int
*priv2_proof_len
)
1302 debug3("%s entering", __func__
);
1305 mm_request_send(pmonitor
->m_recvfd
,
1306 MONITOR_REQ_JPAKE_STEP1
, &m
);
1308 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__
);
1309 mm_request_receive_expect(pmonitor
->m_recvfd
,
1310 MONITOR_ANS_JPAKE_STEP1
, &m
);
1312 if ((*priv1
= BN_new()) == NULL
||
1313 (*priv2
= BN_new()) == NULL
||
1314 (*g_priv1
= BN_new()) == NULL
||
1315 (*g_priv2
= BN_new()) == NULL
)
1316 fatal("%s: BN_new", __func__
);
1318 *id
= buffer_get_string(&m
, id_len
);
1319 /* priv1 and priv2 are, well, private */
1320 buffer_get_bignum2(&m
, *g_priv1
);
1321 buffer_get_bignum2(&m
, *g_priv2
);
1322 *priv1_proof
= buffer_get_string(&m
, priv1_proof_len
);
1323 *priv2_proof
= buffer_get_string(&m
, priv2_proof_len
);
1329 mm_jpake_step2(struct modp_group
*grp
, BIGNUM
*s
,
1330 BIGNUM
*mypub1
, BIGNUM
*theirpub1
, BIGNUM
*theirpub2
, BIGNUM
*mypriv2
,
1331 const u_char
*theirid
, u_int theirid_len
,
1332 const u_char
*myid
, u_int myid_len
,
1333 const u_char
*theirpub1_proof
, u_int theirpub1_proof_len
,
1334 const u_char
*theirpub2_proof
, u_int theirpub2_proof_len
,
1336 u_char
**newpub_exponent_proof
, u_int
*newpub_exponent_proof_len
)
1340 debug3("%s entering", __func__
);
1343 /* monitor already has all bignums except theirpub1, theirpub2 */
1344 buffer_put_bignum2(&m
, theirpub1
);
1345 buffer_put_bignum2(&m
, theirpub2
);
1346 /* monitor already knows our id */
1347 buffer_put_string(&m
, theirid
, theirid_len
);
1348 buffer_put_string(&m
, theirpub1_proof
, theirpub1_proof_len
);
1349 buffer_put_string(&m
, theirpub2_proof
, theirpub2_proof_len
);
1351 mm_request_send(pmonitor
->m_recvfd
,
1352 MONITOR_REQ_JPAKE_STEP2
, &m
);
1354 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__
);
1355 mm_request_receive_expect(pmonitor
->m_recvfd
,
1356 MONITOR_ANS_JPAKE_STEP2
, &m
);
1358 if ((*newpub
= BN_new()) == NULL
)
1359 fatal("%s: BN_new", __func__
);
1361 buffer_get_bignum2(&m
, *newpub
);
1362 *newpub_exponent_proof
= buffer_get_string(&m
,
1363 newpub_exponent_proof_len
);
1369 mm_jpake_key_confirm(struct modp_group
*grp
, BIGNUM
*s
, BIGNUM
*step2_val
,
1370 BIGNUM
*mypriv2
, BIGNUM
*mypub1
, BIGNUM
*mypub2
,
1371 BIGNUM
*theirpub1
, BIGNUM
*theirpub2
,
1372 const u_char
*my_id
, u_int my_id_len
,
1373 const u_char
*their_id
, u_int their_id_len
,
1374 const u_char
*sess_id
, u_int sess_id_len
,
1375 const u_char
*theirpriv2_s_proof
, u_int theirpriv2_s_proof_len
,
1377 u_char
**confirm_hash
, u_int
*confirm_hash_len
)
1381 debug3("%s entering", __func__
);
1384 /* monitor already has all bignums except step2_val */
1385 buffer_put_bignum2(&m
, step2_val
);
1386 /* monitor already knows all the ids */
1387 buffer_put_string(&m
, theirpriv2_s_proof
, theirpriv2_s_proof_len
);
1389 mm_request_send(pmonitor
->m_recvfd
,
1390 MONITOR_REQ_JPAKE_KEY_CONFIRM
, &m
);
1392 debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__
);
1393 mm_request_receive_expect(pmonitor
->m_recvfd
,
1394 MONITOR_ANS_JPAKE_KEY_CONFIRM
, &m
);
1396 /* 'k' is sensitive and stays in the monitor */
1397 *confirm_hash
= buffer_get_string(&m
, confirm_hash_len
);
1403 mm_jpake_check_confirm(const BIGNUM
*k
,
1404 const u_char
*peer_id
, u_int peer_id_len
,
1405 const u_char
*sess_id
, u_int sess_id_len
,
1406 const u_char
*peer_confirm_hash
, u_int peer_confirm_hash_len
)
1411 debug3("%s entering", __func__
);
1414 /* k is dummy in slave, ignored */
1415 /* monitor knows all the ids */
1416 buffer_put_string(&m
, peer_confirm_hash
, peer_confirm_hash_len
);
1417 mm_request_send(pmonitor
->m_recvfd
,
1418 MONITOR_REQ_JPAKE_CHECK_CONFIRM
, &m
);
1420 debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__
);
1421 mm_request_receive_expect(pmonitor
->m_recvfd
,
1422 MONITOR_ANS_JPAKE_CHECK_CONFIRM
, &m
);
1424 success
= buffer_get_int(&m
);
1427 debug3("%s: success = %d", __func__
, success
);