1 /* $OpenBSD: monitor_wrap.c,v 1.80 2014/04/29 18:01:49 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>
42 #include <openssl/bn.h>
43 #include <openssl/dh.h>
44 #include <openssl/evp.h>
47 #include "openbsd-compat/sys-queue.h"
59 #include "auth-options.h"
63 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
66 #define TARGET_OS_MAC 1
74 #include "monitor_wrap.h"
76 #include "monitor_fdpass.h"
87 extern z_stream incoming_stream
;
88 extern z_stream outgoing_stream
;
89 extern struct monitor
*pmonitor
;
90 extern Buffer loginmsg
;
91 extern ServerOptions options
;
94 mm_log_handler(LogLevel level
, const char *msg
, void *ctx
)
97 struct monitor
*mon
= (struct monitor
*)ctx
;
99 if (mon
->m_log_sendfd
== -1)
100 fatal("%s: no log channel", __func__
);
102 buffer_init(&log_msg
);
104 * Placeholder for packet length. Will be filled in with the actual
105 * packet length once the packet has been constucted. This saves
108 buffer_put_int(&log_msg
, 0);
110 buffer_put_int(&log_msg
, level
);
111 buffer_put_cstring(&log_msg
, msg
);
112 put_u32(buffer_ptr(&log_msg
), buffer_len(&log_msg
) - 4);
113 if (atomicio(vwrite
, mon
->m_log_sendfd
, buffer_ptr(&log_msg
),
114 buffer_len(&log_msg
)) != buffer_len(&log_msg
))
115 fatal("%s: write: %s", __func__
, strerror(errno
));
116 buffer_free(&log_msg
);
123 * m_pid is only set in the privileged part, and
124 * points to the unprivileged child.
126 return (pmonitor
&& pmonitor
->m_pid
> 0);
130 mm_request_send(int sock
, enum monitor_reqtype type
, Buffer
*m
)
132 u_int mlen
= buffer_len(m
);
135 debug3("%s entering: type %d", __func__
, type
);
137 put_u32(buf
, mlen
+ 1);
138 buf
[4] = (u_char
) type
; /* 1st byte of payload is mesg-type */
139 if (atomicio(vwrite
, sock
, buf
, sizeof(buf
)) != sizeof(buf
))
140 fatal("%s: write: %s", __func__
, strerror(errno
));
141 if (atomicio(vwrite
, sock
, buffer_ptr(m
), mlen
) != mlen
)
142 fatal("%s: write: %s", __func__
, strerror(errno
));
146 mm_request_receive(int sock
, Buffer
*m
)
151 debug3("%s entering", __func__
);
153 if (atomicio(read
, sock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
156 fatal("%s: read: %s", __func__
, strerror(errno
));
158 msg_len
= get_u32(buf
);
159 if (msg_len
> 256 * 1024)
160 fatal("%s: read: bad msg_len %d", __func__
, msg_len
);
162 buffer_append_space(m
, msg_len
);
163 if (atomicio(read
, sock
, buffer_ptr(m
), msg_len
) != msg_len
)
164 fatal("%s: read: %s", __func__
, strerror(errno
));
168 mm_request_receive_expect(int sock
, enum monitor_reqtype type
, Buffer
*m
)
172 debug3("%s entering: type %d", __func__
, type
);
174 mm_request_receive(sock
, m
);
175 rtype
= buffer_get_char(m
);
177 fatal("%s: read: rtype %d != type %d", __func__
,
183 mm_choose_dh(int min
, int nbits
, int max
)
190 buffer_put_int(&m
, min
);
191 buffer_put_int(&m
, nbits
);
192 buffer_put_int(&m
, max
);
194 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_MODULI
, &m
);
196 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__
);
197 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_MODULI
, &m
);
199 success
= buffer_get_char(&m
);
201 fatal("%s: MONITOR_ANS_MODULI failed", __func__
);
203 if ((p
= BN_new()) == NULL
)
204 fatal("%s: BN_new failed", __func__
);
205 if ((g
= BN_new()) == NULL
)
206 fatal("%s: BN_new failed", __func__
);
207 buffer_get_bignum2(&m
, p
);
208 buffer_get_bignum2(&m
, g
);
210 debug3("%s: remaining %d", __func__
, buffer_len(&m
));
213 return (dh_new_group(g
, p
));
218 mm_key_sign(Key
*key
, u_char
**sigp
, u_int
*lenp
, u_char
*data
, u_int datalen
)
220 Kex
*kex
= *pmonitor
->m_pkex
;
223 debug3("%s entering", __func__
);
226 buffer_put_int(&m
, kex
->host_key_index(key
));
227 buffer_put_string(&m
, data
, datalen
);
229 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SIGN
, &m
);
231 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__
);
232 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SIGN
, &m
);
233 *sigp
= buffer_get_string(&m
, lenp
);
240 mm_getpwnamallow(const char *username
)
245 ServerOptions
*newopts
;
247 debug3("%s entering", __func__
);
250 buffer_put_cstring(&m
, username
);
252 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PWNAM
, &m
);
254 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__
);
255 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PWNAM
, &m
);
257 if (buffer_get_char(&m
) == 0) {
261 pw
= buffer_get_string(&m
, &len
);
262 if (len
!= sizeof(struct passwd
))
263 fatal("%s: struct passwd size mismatch", __func__
);
264 pw
->pw_name
= buffer_get_string(&m
, NULL
);
265 pw
->pw_passwd
= buffer_get_string(&m
, NULL
);
266 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
267 pw
->pw_gecos
= buffer_get_string(&m
, NULL
);
269 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
270 pw
->pw_class
= buffer_get_string(&m
, NULL
);
272 pw
->pw_dir
= buffer_get_string(&m
, NULL
);
273 pw
->pw_shell
= buffer_get_string(&m
, NULL
);
276 /* copy options block as a Match directive may have changed some */
277 newopts
= buffer_get_string(&m
, &len
);
278 if (len
!= sizeof(*newopts
))
279 fatal("%s: option block size mismatch", __func__
);
281 #define M_CP_STROPT(x) do { \
282 if (newopts->x != NULL) \
283 newopts->x = buffer_get_string(&m, NULL); \
285 #define M_CP_STRARRAYOPT(x, nx) do { \
286 for (i = 0; i < newopts->nx; i++) \
287 newopts->x[i] = buffer_get_string(&m, NULL); \
289 /* See comment in servconf.h */
290 COPY_MATCH_STRING_OPTS();
292 #undef M_CP_STRARRAYOPT
294 copy_set_server_options(&options
, newopts
, 1);
303 mm_auth2_read_banner(void)
308 debug3("%s entering", __func__
);
311 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTH2_READ_BANNER
, &m
);
314 mm_request_receive_expect(pmonitor
->m_recvfd
,
315 MONITOR_ANS_AUTH2_READ_BANNER
, &m
);
316 banner
= buffer_get_string(&m
, NULL
);
319 /* treat empty banner as missing banner */
320 if (strlen(banner
) == 0) {
327 /* Inform the privileged process about service and style */
330 mm_inform_authserv(char *service
, char *style
)
334 debug3("%s entering", __func__
);
337 buffer_put_cstring(&m
, service
);
338 buffer_put_cstring(&m
, style
? style
: "");
340 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTHSERV
, &m
);
345 /* Do the password authentication */
347 mm_auth_password(Authctxt
*authctxt
, char *password
)
350 int authenticated
= 0;
352 debug3("%s entering", __func__
);
355 buffer_put_cstring(&m
, password
);
356 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTHPASSWORD
, &m
);
358 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__
);
359 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_AUTHPASSWORD
, &m
);
361 authenticated
= buffer_get_int(&m
);
365 debug3("%s: user %sauthenticated",
366 __func__
, authenticated
? "" : "not ");
367 return (authenticated
);
371 mm_user_key_allowed(struct passwd
*pw
, Key
*key
)
373 return (mm_key_allowed(MM_USERKEY
, NULL
, NULL
, key
));
377 mm_hostbased_key_allowed(struct passwd
*pw
, char *user
, char *host
,
380 return (mm_key_allowed(MM_HOSTKEY
, user
, host
, key
));
384 mm_auth_rhosts_rsa_key_allowed(struct passwd
*pw
, char *user
,
385 char *host
, Key
*key
)
389 key
->type
= KEY_RSA
; /* XXX hack for key_to_blob */
390 ret
= mm_key_allowed(MM_RSAHOSTKEY
, user
, host
, key
);
391 key
->type
= KEY_RSA1
;
396 mm_key_allowed(enum mm_keytype type
, char *user
, char *host
, Key
*key
)
401 int allowed
= 0, have_forced
= 0;
403 debug3("%s entering", __func__
);
405 /* Convert the key to a blob and the pass it over */
406 if (!key_to_blob(key
, &blob
, &len
))
410 buffer_put_int(&m
, type
);
411 buffer_put_cstring(&m
, user
? user
: "");
412 buffer_put_cstring(&m
, host
? host
: "");
413 buffer_put_string(&m
, blob
, len
);
416 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_KEYALLOWED
, &m
);
418 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__
);
419 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_KEYALLOWED
, &m
);
421 allowed
= buffer_get_int(&m
);
423 /* fake forced command */
424 auth_clear_options();
425 have_forced
= buffer_get_int(&m
);
426 forced_command
= have_forced
? xstrdup("true") : NULL
;
434 * This key verify needs to send the key type along, because the
435 * privileged parent makes the decision if the key is allowed
436 * for authentication.
440 mm_key_verify(Key
*key
, u_char
*sig
, u_int siglen
, u_char
*data
, u_int datalen
)
447 debug3("%s entering", __func__
);
449 /* Convert the key to a blob and the pass it over */
450 if (!key_to_blob(key
, &blob
, &len
))
454 buffer_put_string(&m
, blob
, len
);
455 buffer_put_string(&m
, sig
, siglen
);
456 buffer_put_string(&m
, data
, datalen
);
459 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_KEYVERIFY
, &m
);
461 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__
);
462 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_KEYVERIFY
, &m
);
464 verified
= buffer_get_int(&m
);
471 /* Export key state after authentication */
473 mm_newkeys_from_blob(u_char
*blob
, int blen
)
477 Newkeys
*newkey
= NULL
;
482 debug3("%s: %p(%d)", __func__
, blob
, blen
);
484 dump_base64(stderr
, blob
, blen
);
487 buffer_append(&b
, blob
, blen
);
489 newkey
= xcalloc(1, sizeof(*newkey
));
492 comp
= &newkey
->comp
;
495 enc
->name
= buffer_get_string(&b
, NULL
);
496 buffer_get(&b
, &enc
->cipher
, sizeof(enc
->cipher
));
497 enc
->enabled
= buffer_get_int(&b
);
498 enc
->block_size
= buffer_get_int(&b
);
499 enc
->key
= buffer_get_string(&b
, &enc
->key_len
);
500 enc
->iv
= buffer_get_string(&b
, &enc
->iv_len
);
502 if (enc
->name
== NULL
|| cipher_by_name(enc
->name
) != enc
->cipher
)
503 fatal("%s: bad cipher name %s or pointer %p", __func__
,
504 enc
->name
, enc
->cipher
);
507 if (cipher_authlen(enc
->cipher
) == 0) {
508 mac
->name
= buffer_get_string(&b
, NULL
);
509 if (mac
->name
== NULL
|| mac_setup(mac
, mac
->name
) == -1)
510 fatal("%s: can not setup mac %s", __func__
, mac
->name
);
511 mac
->enabled
= buffer_get_int(&b
);
512 mac
->key
= buffer_get_string(&b
, &len
);
513 if (len
> mac
->key_len
)
514 fatal("%s: bad mac key length: %u > %d", __func__
, len
,
520 comp
->type
= buffer_get_int(&b
);
521 comp
->enabled
= buffer_get_int(&b
);
522 comp
->name
= buffer_get_string(&b
, NULL
);
524 len
= buffer_len(&b
);
526 error("newkeys_from_blob: remaining bytes in blob %u", len
);
532 mm_newkeys_to_blob(int mode
, u_char
**blobp
, u_int
*lenp
)
539 Newkeys
*newkey
= (Newkeys
*)packet_get_newkeys(mode
);
541 debug3("%s: converting %p", __func__
, newkey
);
543 if (newkey
== NULL
) {
544 error("%s: newkey == NULL", __func__
);
549 comp
= &newkey
->comp
;
553 buffer_put_cstring(&b
, enc
->name
);
554 /* The cipher struct is constant and shared, you export pointer */
555 buffer_append(&b
, &enc
->cipher
, sizeof(enc
->cipher
));
556 buffer_put_int(&b
, enc
->enabled
);
557 buffer_put_int(&b
, enc
->block_size
);
558 buffer_put_string(&b
, enc
->key
, enc
->key_len
);
559 packet_get_keyiv(mode
, enc
->iv
, enc
->iv_len
);
560 buffer_put_string(&b
, enc
->iv
, enc
->iv_len
);
563 if (cipher_authlen(enc
->cipher
) == 0) {
564 buffer_put_cstring(&b
, mac
->name
);
565 buffer_put_int(&b
, mac
->enabled
);
566 buffer_put_string(&b
, mac
->key
, mac
->key_len
);
570 buffer_put_int(&b
, comp
->type
);
571 buffer_put_int(&b
, comp
->enabled
);
572 buffer_put_cstring(&b
, comp
->name
);
574 len
= buffer_len(&b
);
578 *blobp
= xmalloc(len
);
579 memcpy(*blobp
, buffer_ptr(&b
), len
);
581 explicit_bzero(buffer_ptr(&b
), len
);
587 mm_send_kex(Buffer
*m
, Kex
*kex
)
589 buffer_put_string(m
, kex
->session_id
, kex
->session_id_len
);
590 buffer_put_int(m
, kex
->we_need
);
591 buffer_put_int(m
, kex
->hostkey_type
);
592 buffer_put_int(m
, kex
->kex_type
);
593 buffer_put_string(m
, buffer_ptr(&kex
->my
), buffer_len(&kex
->my
));
594 buffer_put_string(m
, buffer_ptr(&kex
->peer
), buffer_len(&kex
->peer
));
595 buffer_put_int(m
, kex
->flags
);
596 buffer_put_cstring(m
, kex
->client_version_string
);
597 buffer_put_cstring(m
, kex
->server_version_string
);
601 mm_send_keystate(struct monitor
*monitor
)
603 Buffer m
, *input
, *output
;
606 u_int32_t seqnr
, packets
;
607 u_int64_t blocks
, bytes
;
616 buffer_put_int(&m
, packet_get_protocol_flags());
618 buffer_put_int(&m
, packet_get_ssh1_cipher());
620 debug3("%s: Sending ssh1 KEY+IV", __func__
);
621 keylen
= packet_get_encryption_key(NULL
);
622 key
= xmalloc(keylen
+1); /* add 1 if keylen == 0 */
623 keylen
= packet_get_encryption_key(key
);
624 buffer_put_string(&m
, key
, keylen
);
625 explicit_bzero(key
, keylen
);
628 ivlen
= packet_get_keyiv_len(MODE_OUT
);
629 packet_get_keyiv(MODE_OUT
, iv
, ivlen
);
630 buffer_put_string(&m
, iv
, ivlen
);
631 ivlen
= packet_get_keyiv_len(MODE_IN
);
632 packet_get_keyiv(MODE_IN
, iv
, ivlen
);
633 buffer_put_string(&m
, iv
, ivlen
);
636 /* Kex for rekeying */
637 mm_send_kex(&m
, *monitor
->m_pkex
);
640 debug3("%s: Sending new keys: %p %p",
641 __func__
, packet_get_newkeys(MODE_OUT
),
642 packet_get_newkeys(MODE_IN
));
645 if (!mm_newkeys_to_blob(MODE_OUT
, &blob
, &bloblen
))
646 fatal("%s: conversion of newkeys failed", __func__
);
648 buffer_put_string(&m
, blob
, bloblen
);
651 if (!mm_newkeys_to_blob(MODE_IN
, &blob
, &bloblen
))
652 fatal("%s: conversion of newkeys failed", __func__
);
654 buffer_put_string(&m
, blob
, bloblen
);
657 packet_get_state(MODE_OUT
, &seqnr
, &blocks
, &packets
, &bytes
);
658 buffer_put_int(&m
, seqnr
);
659 buffer_put_int64(&m
, blocks
);
660 buffer_put_int(&m
, packets
);
661 buffer_put_int64(&m
, bytes
);
662 packet_get_state(MODE_IN
, &seqnr
, &blocks
, &packets
, &bytes
);
663 buffer_put_int(&m
, seqnr
);
664 buffer_put_int64(&m
, blocks
);
665 buffer_put_int(&m
, packets
);
666 buffer_put_int64(&m
, bytes
);
668 debug3("%s: New keys have been sent", __func__
);
670 /* More key context */
671 plen
= packet_get_keycontext(MODE_OUT
, NULL
);
673 packet_get_keycontext(MODE_OUT
, p
);
674 buffer_put_string(&m
, p
, plen
);
677 plen
= packet_get_keycontext(MODE_IN
, NULL
);
679 packet_get_keycontext(MODE_IN
, p
);
680 buffer_put_string(&m
, p
, plen
);
683 /* Compression state */
684 debug3("%s: Sending compression state", __func__
);
685 buffer_put_string(&m
, &outgoing_stream
, sizeof(outgoing_stream
));
686 buffer_put_string(&m
, &incoming_stream
, sizeof(incoming_stream
));
688 /* Network I/O buffers */
689 input
= (Buffer
*)packet_get_input();
690 output
= (Buffer
*)packet_get_output();
691 buffer_put_string(&m
, buffer_ptr(input
), buffer_len(input
));
692 buffer_put_string(&m
, buffer_ptr(output
), buffer_len(output
));
696 buffer_put_int64(&m
, get_sent_bytes());
697 buffer_put_int64(&m
, get_recv_bytes());
700 mm_request_send(monitor
->m_recvfd
, MONITOR_REQ_KEYEXPORT
, &m
);
701 debug3("%s: Finished sending state", __func__
);
707 mm_pty_allocate(int *ptyfd
, int *ttyfd
, char *namebuf
, size_t namebuflen
)
711 int success
= 0, tmp1
= -1, tmp2
= -1;
713 /* Kludge: ensure there are fds free to receive the pty/tty */
714 if ((tmp1
= dup(pmonitor
->m_recvfd
)) == -1 ||
715 (tmp2
= dup(pmonitor
->m_recvfd
)) == -1) {
716 error("%s: cannot allocate fds for pty", __func__
);
727 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PTY
, &m
);
729 debug3("%s: waiting for MONITOR_ANS_PTY", __func__
);
730 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PTY
, &m
);
732 success
= buffer_get_int(&m
);
734 debug3("%s: pty alloc failed", __func__
);
738 p
= buffer_get_string(&m
, NULL
);
739 msg
= buffer_get_string(&m
, NULL
);
742 strlcpy(namebuf
, p
, namebuflen
); /* Possible truncation */
745 buffer_append(&loginmsg
, msg
, strlen(msg
));
748 if ((*ptyfd
= mm_receive_fd(pmonitor
->m_recvfd
)) == -1 ||
749 (*ttyfd
= mm_receive_fd(pmonitor
->m_recvfd
)) == -1)
750 fatal("%s: receive fds failed", __func__
);
757 mm_session_pty_cleanup2(Session
*s
)
764 buffer_put_cstring(&m
, s
->tty
);
765 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PTYCLEANUP
, &m
);
768 /* closed dup'ed master */
769 if (s
->ptymaster
!= -1 && close(s
->ptymaster
) < 0)
770 error("close(s->ptymaster/%d): %s",
771 s
->ptymaster
, strerror(errno
));
773 /* unlink pty from session */
779 mm_start_pam(Authctxt
*authctxt
)
783 debug3("%s entering", __func__
);
784 if (!options
.use_pam
)
785 fatal("UsePAM=no, but ended up in %s anyway", __func__
);
788 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_START
, &m
);
794 mm_do_pam_account(void)
800 debug3("%s entering", __func__
);
801 if (!options
.use_pam
)
802 fatal("UsePAM=no, but ended up in %s anyway", __func__
);
805 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_ACCOUNT
, &m
);
807 mm_request_receive_expect(pmonitor
->m_recvfd
,
808 MONITOR_ANS_PAM_ACCOUNT
, &m
);
809 ret
= buffer_get_int(&m
);
810 msg
= buffer_get_string(&m
, NULL
);
811 buffer_append(&loginmsg
, msg
, strlen(msg
));
816 debug3("%s returning %d", __func__
, ret
);
822 mm_sshpam_init_ctx(Authctxt
*authctxt
)
827 debug3("%s", __func__
);
829 buffer_put_cstring(&m
, authctxt
->user
);
830 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_INIT_CTX
, &m
);
831 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__
);
832 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_INIT_CTX
, &m
);
833 success
= buffer_get_int(&m
);
835 debug3("%s: pam_init_ctx failed", __func__
);
844 mm_sshpam_query(void *ctx
, char **name
, char **info
,
845 u_int
*num
, char ***prompts
, u_int
**echo_on
)
851 debug3("%s", __func__
);
853 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_QUERY
, &m
);
854 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__
);
855 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_QUERY
, &m
);
856 ret
= buffer_get_int(&m
);
857 debug3("%s: pam_query returned %d", __func__
, ret
);
858 *name
= buffer_get_string(&m
, NULL
);
859 *info
= buffer_get_string(&m
, NULL
);
860 *num
= buffer_get_int(&m
);
861 if (*num
> PAM_MAX_NUM_MSG
)
862 fatal("%s: recieved %u PAM messages, expected <= %u",
863 __func__
, *num
, PAM_MAX_NUM_MSG
);
864 *prompts
= xcalloc((*num
+ 1), sizeof(char *));
865 *echo_on
= xcalloc((*num
+ 1), sizeof(u_int
));
866 for (i
= 0; i
< *num
; ++i
) {
867 (*prompts
)[i
] = buffer_get_string(&m
, NULL
);
868 (*echo_on
)[i
] = buffer_get_int(&m
);
875 mm_sshpam_respond(void *ctx
, u_int num
, char **resp
)
881 debug3("%s", __func__
);
883 buffer_put_int(&m
, num
);
884 for (i
= 0; i
< num
; ++i
)
885 buffer_put_cstring(&m
, resp
[i
]);
886 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_RESPOND
, &m
);
887 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__
);
888 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_RESPOND
, &m
);
889 ret
= buffer_get_int(&m
);
890 debug3("%s: pam_respond returned %d", __func__
, ret
);
896 mm_sshpam_free_ctx(void *ctxtp
)
900 debug3("%s", __func__
);
902 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_FREE_CTX
, &m
);
903 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__
);
904 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_FREE_CTX
, &m
);
909 /* Request process termination */
917 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_TERM
, &m
);
923 mm_ssh1_session_key(BIGNUM
*num
)
929 buffer_put_bignum2(&m
, num
);
930 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SESSKEY
, &m
);
932 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SESSKEY
, &m
);
934 rsafail
= buffer_get_int(&m
);
935 buffer_get_bignum2(&m
, num
);
944 mm_chall_setup(char **name
, char **infotxt
, u_int
*numprompts
,
945 char ***prompts
, u_int
**echo_on
)
948 *infotxt
= xstrdup("");
950 *prompts
= xcalloc(*numprompts
, sizeof(char *));
951 *echo_on
= xcalloc(*numprompts
, sizeof(u_int
));
956 mm_bsdauth_query(void *ctx
, char **name
, char **infotxt
,
957 u_int
*numprompts
, char ***prompts
, u_int
**echo_on
)
963 debug3("%s: entering", __func__
);
966 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_BSDAUTHQUERY
, &m
);
968 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_BSDAUTHQUERY
,
970 success
= buffer_get_int(&m
);
972 debug3("%s: no challenge", __func__
);
977 /* Get the challenge, and format the response */
978 challenge
= buffer_get_string(&m
, NULL
);
981 mm_chall_setup(name
, infotxt
, numprompts
, prompts
, echo_on
);
982 (*prompts
)[0] = challenge
;
984 debug3("%s: received challenge: %s", __func__
, challenge
);
990 mm_bsdauth_respond(void *ctx
, u_int numresponses
, char **responses
)
995 debug3("%s: entering", __func__
);
996 if (numresponses
!= 1)
1000 buffer_put_cstring(&m
, responses
[0]);
1001 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_BSDAUTHRESPOND
, &m
);
1003 mm_request_receive_expect(pmonitor
->m_recvfd
,
1004 MONITOR_ANS_BSDAUTHRESPOND
, &m
);
1006 authok
= buffer_get_int(&m
);
1009 return ((authok
== 0) ? -1 : 0);
1014 mm_skey_query(void *ctx
, char **name
, char **infotxt
,
1015 u_int
*numprompts
, char ***prompts
, u_int
**echo_on
)
1021 debug3("%s: entering", __func__
);
1024 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SKEYQUERY
, &m
);
1026 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SKEYQUERY
,
1028 success
= buffer_get_int(&m
);
1030 debug3("%s: no challenge", __func__
);
1035 /* Get the challenge, and format the response */
1036 challenge
= buffer_get_string(&m
, NULL
);
1039 debug3("%s: received challenge: %s", __func__
, challenge
);
1041 mm_chall_setup(name
, infotxt
, numprompts
, prompts
, echo_on
);
1043 xasprintf(*prompts
, "%s%s", challenge
, SKEY_PROMPT
);
1050 mm_skey_respond(void *ctx
, u_int numresponses
, char **responses
)
1055 debug3("%s: entering", __func__
);
1056 if (numresponses
!= 1)
1060 buffer_put_cstring(&m
, responses
[0]);
1061 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SKEYRESPOND
, &m
);
1063 mm_request_receive_expect(pmonitor
->m_recvfd
,
1064 MONITOR_ANS_SKEYRESPOND
, &m
);
1066 authok
= buffer_get_int(&m
);
1069 return ((authok
== 0) ? -1 : 0);
1074 mm_ssh1_session_id(u_char session_id
[16])
1079 debug3("%s entering", __func__
);
1082 for (i
= 0; i
< 16; i
++)
1083 buffer_put_char(&m
, session_id
[i
]);
1085 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SESSID
, &m
);
1091 mm_auth_rsa_key_allowed(struct passwd
*pw
, BIGNUM
*client_n
, Key
**rkey
)
1097 int allowed
= 0, have_forced
= 0;
1099 debug3("%s entering", __func__
);
1102 buffer_put_bignum2(&m
, client_n
);
1104 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSAKEYALLOWED
, &m
);
1105 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSAKEYALLOWED
, &m
);
1107 allowed
= buffer_get_int(&m
);
1109 /* fake forced command */
1110 auth_clear_options();
1111 have_forced
= buffer_get_int(&m
);
1112 forced_command
= have_forced
? xstrdup("true") : NULL
;
1114 if (allowed
&& rkey
!= NULL
) {
1115 blob
= buffer_get_string(&m
, &blen
);
1116 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1117 fatal("%s: key_from_blob failed", __func__
);
1127 mm_auth_rsa_generate_challenge(Key
*key
)
1134 debug3("%s entering", __func__
);
1136 if ((challenge
= BN_new()) == NULL
)
1137 fatal("%s: BN_new failed", __func__
);
1139 key
->type
= KEY_RSA
; /* XXX cheat for key_to_blob */
1140 if (key_to_blob(key
, &blob
, &blen
) == 0)
1141 fatal("%s: key_to_blob failed", __func__
);
1142 key
->type
= KEY_RSA1
;
1145 buffer_put_string(&m
, blob
, blen
);
1148 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSACHALLENGE
, &m
);
1149 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSACHALLENGE
, &m
);
1151 buffer_get_bignum2(&m
, challenge
);
1158 mm_auth_rsa_verify_response(Key
*key
, BIGNUM
*p
, u_char response
[16])
1165 debug3("%s entering", __func__
);
1167 key
->type
= KEY_RSA
; /* XXX cheat for key_to_blob */
1168 if (key_to_blob(key
, &blob
, &blen
) == 0)
1169 fatal("%s: key_to_blob failed", __func__
);
1170 key
->type
= KEY_RSA1
;
1173 buffer_put_string(&m
, blob
, blen
);
1174 buffer_put_string(&m
, response
, 16);
1177 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSARESPONSE
, &m
);
1178 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSARESPONSE
, &m
);
1180 success
= buffer_get_int(&m
);
1187 #ifdef SSH_AUDIT_EVENTS
1189 mm_audit_event(ssh_audit_event_t event
)
1193 debug3("%s entering", __func__
);
1196 buffer_put_int(&m
, event
);
1198 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUDIT_EVENT
, &m
);
1203 mm_audit_run_command(const char *command
)
1207 debug3("%s entering command %s", __func__
, command
);
1210 buffer_put_cstring(&m
, command
);
1212 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUDIT_COMMAND
, &m
);
1215 #endif /* SSH_AUDIT_EVENTS */
1219 mm_ssh_gssapi_server_ctx(Gssctxt
**ctx
, gss_OID goid
)
1224 /* Client doesn't get to see the context */
1228 buffer_put_string(&m
, goid
->elements
, goid
->length
);
1230 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSSETUP
, &m
);
1231 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSSETUP
, &m
);
1233 major
= buffer_get_int(&m
);
1240 mm_ssh_gssapi_accept_ctx(Gssctxt
*ctx
, gss_buffer_desc
*in
,
1241 gss_buffer_desc
*out
, OM_uint32
*flags
)
1248 buffer_put_string(&m
, in
->value
, in
->length
);
1250 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSSTEP
, &m
);
1251 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSSTEP
, &m
);
1253 major
= buffer_get_int(&m
);
1254 out
->value
= buffer_get_string(&m
, &len
);
1257 *flags
= buffer_get_int(&m
);
1265 mm_ssh_gssapi_checkmic(Gssctxt
*ctx
, gss_buffer_t gssbuf
, gss_buffer_t gssmic
)
1271 buffer_put_string(&m
, gssbuf
->value
, gssbuf
->length
);
1272 buffer_put_string(&m
, gssmic
->value
, gssmic
->length
);
1274 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSCHECKMIC
, &m
);
1275 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSCHECKMIC
,
1278 major
= buffer_get_int(&m
);
1284 mm_ssh_gssapi_userok(char *user
)
1287 int authenticated
= 0;
1291 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSUSEROK
, &m
);
1292 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSUSEROK
,
1295 authenticated
= buffer_get_int(&m
);
1298 debug3("%s: user %sauthenticated",__func__
, authenticated
? "" : "not ");
1299 return (authenticated
);