1 /* $OpenBSD: monitor_wrap.c,v 1.69 2010/03/07 11:57:13 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_key_allowed(enum mm_keytype type
, char *user
, char *host
, Key
*key
)
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
))
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
);
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
;
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
)
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
))
409 buffer_put_string(&m
, blob
, len
);
410 buffer_put_string(&m
, sig
, siglen
);
411 buffer_put_string(&m
, data
, datalen
);
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
);
426 /* Export key state after authentication */
428 mm_newkeys_from_blob(u_char
*blob
, int blen
)
432 Newkeys
*newkey
= NULL
;
437 debug3("%s: %p(%d)", __func__
, blob
, blen
);
439 dump_base64(stderr
, blob
, blen
);
442 buffer_append(&b
, blob
, blen
);
444 newkey
= xmalloc(sizeof(*newkey
));
447 comp
= &newkey
->comp
;
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
);
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
,
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
);
482 error("newkeys_from_blob: remaining bytes in blob %u", len
);
488 mm_newkeys_to_blob(int mode
, u_char
**blobp
, u_int
*lenp
)
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__
);
505 comp
= &newkey
->comp
;
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
);
519 buffer_put_cstring(&b
, mac
->name
);
520 buffer_put_int(&b
, mac
->enabled
);
521 buffer_put_string(&b
, mac
->key
, mac
->key_len
);
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
);
532 *blobp
= xmalloc(len
);
533 memcpy(*blobp
, buffer_ptr(&b
), len
);
535 memset(buffer_ptr(&b
), 0, len
);
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
);
555 mm_send_keystate(struct monitor
*monitor
)
557 Buffer m
, *input
, *output
;
560 u_int32_t seqnr
, packets
;
561 u_int64_t blocks
, bytes
;
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
);
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
);
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
));
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
);
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
);
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__
);
624 /* More key context */
625 plen
= packet_get_keycontext(MODE_OUT
, NULL
);
627 packet_get_keycontext(MODE_OUT
, p
);
628 buffer_put_string(&m
, p
, plen
);
631 plen
= packet_get_keycontext(MODE_IN
, NULL
);
633 packet_get_keycontext(MODE_IN
, p
);
634 buffer_put_string(&m
, p
, plen
);
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
));
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__
);
661 mm_pty_allocate(int *ptyfd
, int *ttyfd
, char *namebuf
, size_t namebuflen
)
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__
);
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
);
688 debug3("%s: pty alloc failed", __func__
);
692 p
= buffer_get_string(&m
, NULL
);
693 msg
= buffer_get_string(&m
, NULL
);
696 strlcpy(namebuf
, p
, namebuflen
); /* Possible truncation */
699 buffer_append(&loginmsg
, msg
, strlen(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__
);
711 mm_session_pty_cleanup2(Session
*s
)
718 buffer_put_cstring(&m
, s
->tty
);
719 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PTYCLEANUP
, &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 */
733 mm_start_pam(Authctxt
*authctxt
)
737 debug3("%s entering", __func__
);
738 if (!options
.use_pam
)
739 fatal("UsePAM=no, but ended up in %s anyway", __func__
);
742 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_START
, &m
);
748 mm_do_pam_account(void)
754 debug3("%s entering", __func__
);
755 if (!options
.use_pam
)
756 fatal("UsePAM=no, but ended up in %s anyway", __func__
);
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
));
770 debug3("%s returning %d", __func__
, ret
);
776 mm_sshpam_init_ctx(Authctxt
*authctxt
)
781 debug3("%s", __func__
);
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
);
789 debug3("%s: pam_init_ctx failed", __func__
);
798 mm_sshpam_query(void *ctx
, char **name
, char **info
,
799 u_int
*num
, char ***prompts
, u_int
**echo_on
)
805 debug3("%s", __func__
);
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
);
829 mm_sshpam_respond(void *ctx
, u_int num
, char **resp
)
835 debug3("%s", __func__
);
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
);
850 mm_sshpam_free_ctx(void *ctxtp
)
854 debug3("%s", __func__
);
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
);
863 /* Request process termination */
871 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_TERM
, &m
);
876 mm_ssh1_session_key(BIGNUM
*num
)
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
);
896 mm_chall_setup(char **name
, char **infotxt
, u_int
*numprompts
,
897 char ***prompts
, u_int
**echo_on
)
900 *infotxt
= xstrdup("");
902 *prompts
= xcalloc(*numprompts
, sizeof(char *));
903 *echo_on
= xcalloc(*numprompts
, sizeof(u_int
));
908 mm_bsdauth_query(void *ctx
, char **name
, char **infotxt
,
909 u_int
*numprompts
, char ***prompts
, u_int
**echo_on
)
915 debug3("%s: entering", __func__
);
918 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_BSDAUTHQUERY
, &m
);
920 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_BSDAUTHQUERY
,
922 success
= buffer_get_int(&m
);
924 debug3("%s: no challenge", __func__
);
929 /* Get the challenge, and format the response */
930 challenge
= buffer_get_string(&m
, NULL
);
933 mm_chall_setup(name
, infotxt
, numprompts
, prompts
, echo_on
);
934 (*prompts
)[0] = challenge
;
936 debug3("%s: received challenge: %s", __func__
, challenge
);
942 mm_bsdauth_respond(void *ctx
, u_int numresponses
, char **responses
)
947 debug3("%s: entering", __func__
);
948 if (numresponses
!= 1)
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
);
961 return ((authok
== 0) ? -1 : 0);
966 mm_skey_query(void *ctx
, char **name
, char **infotxt
,
967 u_int
*numprompts
, char ***prompts
, u_int
**echo_on
)
973 debug3("%s: entering", __func__
);
976 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SKEYQUERY
, &m
);
978 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SKEYQUERY
,
980 success
= buffer_get_int(&m
);
982 debug3("%s: no challenge", __func__
);
987 /* Get the challenge, and format the response */
988 challenge
= buffer_get_string(&m
, NULL
);
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
);
1002 mm_skey_respond(void *ctx
, u_int numresponses
, char **responses
)
1007 debug3("%s: entering", __func__
);
1008 if (numresponses
!= 1)
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
);
1021 return ((authok
== 0) ? -1 : 0);
1026 mm_ssh1_session_id(u_char session_id
[16])
1031 debug3("%s entering", __func__
);
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
);
1042 mm_auth_rsa_key_allowed(struct passwd
*pw
, BIGNUM
*client_n
, Key
**rkey
)
1048 int allowed
= 0, have_forced
= 0;
1050 debug3("%s entering", __func__
);
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__
);
1078 mm_auth_rsa_generate_challenge(Key
*key
)
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
;
1096 buffer_put_string(&m
, blob
, blen
);
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
);
1109 mm_auth_rsa_verify_response(Key
*key
, BIGNUM
*p
, u_char response
[16])
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
;
1124 buffer_put_string(&m
, blob
, blen
);
1125 buffer_put_string(&m
, response
, 16);
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
);
1137 #ifdef SSH_AUDIT_EVENTS
1139 mm_audit_event(ssh_audit_event_t event
)
1143 debug3("%s entering", __func__
);
1146 buffer_put_int(&m
, event
);
1148 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUDIT_EVENT
, &m
);
1153 mm_audit_run_command(const char *command
)
1157 debug3("%s entering command %s", __func__
, command
);
1160 buffer_put_cstring(&m
, command
);
1162 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUDIT_COMMAND
, &m
);
1165 #endif /* SSH_AUDIT_EVENTS */
1169 mm_ssh_gssapi_server_ctx(Gssctxt
**ctx
, gss_OID goid
)
1174 /* Client doesn't get to see the context */
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
);
1190 mm_ssh_gssapi_accept_ctx(Gssctxt
*ctx
, gss_buffer_desc
*in
,
1191 gss_buffer_desc
*out
, OM_uint32
*flags
)
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
);
1207 *flags
= buffer_get_int(&m
);
1215 mm_ssh_gssapi_checkmic(Gssctxt
*ctx
, gss_buffer_t gssbuf
, gss_buffer_t gssmic
)
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
,
1228 major
= buffer_get_int(&m
);
1234 mm_ssh_gssapi_userok(char *user
)
1237 int authenticated
= 0;
1241 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSUSEROK
, &m
);
1242 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSUSEROK
,
1245 authenticated
= buffer_get_int(&m
);
1248 debug3("%s: user %sauthenticated",__func__
, authenticated
? "" : "not ");
1249 return (authenticated
);
1255 mm_auth2_jpake_get_pwdata(Authctxt
*authctxt
, BIGNUM
**s
,
1256 char **hash_scheme
, char **salt
)
1260 debug3("%s entering", __func__
);
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
);
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
)
1285 debug3("%s entering", __func__
);
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
);
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
,
1319 u_char
**newpub_exponent_proof
, u_int
*newpub_exponent_proof_len
)
1323 debug3("%s entering", __func__
);
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
);
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
,
1360 u_char
**confirm_hash
, u_int
*confirm_hash_len
)
1364 debug3("%s entering", __func__
);
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
);
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
)
1394 debug3("%s entering", __func__
);
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
);
1410 debug3("%s: success = %d", __func__
, success
);