1 /* $OpenBSD: monitor_wrap.c,v 1.70 2010/08/31 11:54:45 djm 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
;
95 * m_pid is only set in the privileged part, and
96 * points to the unprivileged child.
98 return (pmonitor
&& pmonitor
->m_pid
> 0);
102 mm_request_send(int sock
, enum monitor_reqtype type
, Buffer
*m
)
104 u_int mlen
= buffer_len(m
);
107 debug3("%s entering: type %d", __func__
, type
);
109 put_u32(buf
, mlen
+ 1);
110 buf
[4] = (u_char
) type
; /* 1st byte of payload is mesg-type */
111 if (atomicio(vwrite
, sock
, buf
, sizeof(buf
)) != sizeof(buf
))
112 fatal("%s: write: %s", __func__
, strerror(errno
));
113 if (atomicio(vwrite
, sock
, buffer_ptr(m
), mlen
) != mlen
)
114 fatal("%s: write: %s", __func__
, strerror(errno
));
118 mm_request_receive(int sock
, Buffer
*m
)
123 debug3("%s entering", __func__
);
125 if (atomicio(read
, sock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
128 fatal("%s: read: %s", __func__
, strerror(errno
));
130 msg_len
= get_u32(buf
);
131 if (msg_len
> 256 * 1024)
132 fatal("%s: read: bad msg_len %d", __func__
, msg_len
);
134 buffer_append_space(m
, msg_len
);
135 if (atomicio(read
, sock
, buffer_ptr(m
), msg_len
) != msg_len
)
136 fatal("%s: read: %s", __func__
, strerror(errno
));
140 mm_request_receive_expect(int sock
, enum monitor_reqtype type
, Buffer
*m
)
144 debug3("%s entering: type %d", __func__
, type
);
146 mm_request_receive(sock
, m
);
147 rtype
= buffer_get_char(m
);
149 fatal("%s: read: rtype %d != type %d", __func__
,
154 mm_choose_dh(int min
, int nbits
, int max
)
161 buffer_put_int(&m
, min
);
162 buffer_put_int(&m
, nbits
);
163 buffer_put_int(&m
, max
);
165 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_MODULI
, &m
);
167 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__
);
168 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_MODULI
, &m
);
170 success
= buffer_get_char(&m
);
172 fatal("%s: MONITOR_ANS_MODULI failed", __func__
);
174 if ((p
= BN_new()) == NULL
)
175 fatal("%s: BN_new failed", __func__
);
176 if ((g
= BN_new()) == NULL
)
177 fatal("%s: BN_new failed", __func__
);
178 buffer_get_bignum2(&m
, p
);
179 buffer_get_bignum2(&m
, g
);
181 debug3("%s: remaining %d", __func__
, buffer_len(&m
));
184 return (dh_new_group(g
, p
));
188 mm_key_sign(Key
*key
, u_char
**sigp
, u_int
*lenp
, u_char
*data
, u_int datalen
)
190 Kex
*kex
= *pmonitor
->m_pkex
;
193 debug3("%s entering", __func__
);
196 buffer_put_int(&m
, kex
->host_key_index(key
));
197 buffer_put_string(&m
, data
, datalen
);
199 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SIGN
, &m
);
201 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__
);
202 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SIGN
, &m
);
203 *sigp
= buffer_get_string(&m
, lenp
);
210 mm_getpwnamallow(const char *username
)
215 ServerOptions
*newopts
;
217 debug3("%s entering", __func__
);
220 buffer_put_cstring(&m
, username
);
222 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PWNAM
, &m
);
224 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__
);
225 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PWNAM
, &m
);
227 if (buffer_get_char(&m
) == 0) {
231 pw
= buffer_get_string(&m
, &len
);
232 if (len
!= sizeof(struct passwd
))
233 fatal("%s: struct passwd size mismatch", __func__
);
234 pw
->pw_name
= buffer_get_string(&m
, NULL
);
235 pw
->pw_passwd
= buffer_get_string(&m
, NULL
);
236 pw
->pw_gecos
= buffer_get_string(&m
, NULL
);
237 #ifdef HAVE_PW_CLASS_IN_PASSWD
238 pw
->pw_class
= buffer_get_string(&m
, NULL
);
240 pw
->pw_dir
= buffer_get_string(&m
, NULL
);
241 pw
->pw_shell
= buffer_get_string(&m
, NULL
);
244 /* copy options block as a Match directive may have changed some */
245 newopts
= buffer_get_string(&m
, &len
);
246 if (len
!= sizeof(*newopts
))
247 fatal("%s: option block size mismatch", __func__
);
248 if (newopts
->banner
!= NULL
)
249 newopts
->banner
= buffer_get_string(&m
, NULL
);
250 copy_set_server_options(&options
, newopts
, 1);
259 mm_auth2_read_banner(void)
264 debug3("%s entering", __func__
);
267 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTH2_READ_BANNER
, &m
);
270 mm_request_receive_expect(pmonitor
->m_recvfd
,
271 MONITOR_ANS_AUTH2_READ_BANNER
, &m
);
272 banner
= buffer_get_string(&m
, NULL
);
275 /* treat empty banner as missing banner */
276 if (strlen(banner
) == 0) {
283 /* Inform the privileged process about service and style */
286 mm_inform_authserv(char *service
, char *style
)
290 debug3("%s entering", __func__
);
293 buffer_put_cstring(&m
, service
);
294 buffer_put_cstring(&m
, style
? style
: "");
296 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTHSERV
, &m
);
301 /* Do the password authentication */
303 mm_auth_password(Authctxt
*authctxt
, char *password
)
306 int authenticated
= 0;
308 debug3("%s entering", __func__
);
311 buffer_put_cstring(&m
, password
);
312 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTHPASSWORD
, &m
);
314 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__
);
315 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_AUTHPASSWORD
, &m
);
317 authenticated
= buffer_get_int(&m
);
321 debug3("%s: user %sauthenticated",
322 __func__
, authenticated
? "" : "not ");
323 return (authenticated
);
327 mm_user_key_allowed(struct passwd
*pw
, Key
*key
)
329 return (mm_key_allowed(MM_USERKEY
, NULL
, NULL
, key
));
333 mm_hostbased_key_allowed(struct passwd
*pw
, char *user
, char *host
,
336 return (mm_key_allowed(MM_HOSTKEY
, user
, host
, key
));
340 mm_auth_rhosts_rsa_key_allowed(struct passwd
*pw
, char *user
,
341 char *host
, Key
*key
)
345 key
->type
= KEY_RSA
; /* XXX hack for key_to_blob */
346 ret
= mm_key_allowed(MM_RSAHOSTKEY
, user
, host
, key
);
347 key
->type
= KEY_RSA1
;
352 mm_key_allowed(enum mm_keytype type
, char *user
, char *host
, Key
*key
)
357 int allowed
= 0, have_forced
= 0;
359 debug3("%s entering", __func__
);
361 /* Convert the key to a blob and the pass it over */
362 if (!key_to_blob(key
, &blob
, &len
))
366 buffer_put_int(&m
, type
);
367 buffer_put_cstring(&m
, user
? user
: "");
368 buffer_put_cstring(&m
, host
? host
: "");
369 buffer_put_string(&m
, blob
, len
);
372 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_KEYALLOWED
, &m
);
374 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__
);
375 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_KEYALLOWED
, &m
);
377 allowed
= buffer_get_int(&m
);
379 /* fake forced command */
380 auth_clear_options();
381 have_forced
= buffer_get_int(&m
);
382 forced_command
= have_forced
? xstrdup("true") : NULL
;
390 * This key verify needs to send the key type along, because the
391 * privileged parent makes the decision if the key is allowed
392 * for authentication.
396 mm_key_verify(Key
*key
, u_char
*sig
, u_int siglen
, u_char
*data
, u_int datalen
)
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_string(&m
, blob
, len
);
411 buffer_put_string(&m
, sig
, siglen
);
412 buffer_put_string(&m
, data
, datalen
);
415 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_KEYVERIFY
, &m
);
417 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__
);
418 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_KEYVERIFY
, &m
);
420 verified
= buffer_get_int(&m
);
427 /* Export key state after authentication */
429 mm_newkeys_from_blob(u_char
*blob
, int blen
)
433 Newkeys
*newkey
= NULL
;
438 debug3("%s: %p(%d)", __func__
, blob
, blen
);
440 dump_base64(stderr
, blob
, blen
);
443 buffer_append(&b
, blob
, blen
);
445 newkey
= xmalloc(sizeof(*newkey
));
448 comp
= &newkey
->comp
;
451 enc
->name
= buffer_get_string(&b
, NULL
);
452 buffer_get(&b
, &enc
->cipher
, sizeof(enc
->cipher
));
453 enc
->enabled
= buffer_get_int(&b
);
454 enc
->block_size
= buffer_get_int(&b
);
455 enc
->key
= buffer_get_string(&b
, &enc
->key_len
);
456 enc
->iv
= buffer_get_string(&b
, &len
);
457 if (len
!= enc
->block_size
)
458 fatal("%s: bad ivlen: expected %u != %u", __func__
,
459 enc
->block_size
, len
);
461 if (enc
->name
== NULL
|| cipher_by_name(enc
->name
) != enc
->cipher
)
462 fatal("%s: bad cipher name %s or pointer %p", __func__
,
463 enc
->name
, enc
->cipher
);
466 mac
->name
= buffer_get_string(&b
, NULL
);
467 if (mac
->name
== NULL
|| mac_setup(mac
, mac
->name
) == -1)
468 fatal("%s: can not setup mac %s", __func__
, mac
->name
);
469 mac
->enabled
= buffer_get_int(&b
);
470 mac
->key
= buffer_get_string(&b
, &len
);
471 if (len
> mac
->key_len
)
472 fatal("%s: bad mac key length: %u > %d", __func__
, len
,
477 comp
->type
= buffer_get_int(&b
);
478 comp
->enabled
= buffer_get_int(&b
);
479 comp
->name
= buffer_get_string(&b
, NULL
);
481 len
= buffer_len(&b
);
483 error("newkeys_from_blob: remaining bytes in blob %u", len
);
489 mm_newkeys_to_blob(int mode
, u_char
**blobp
, u_int
*lenp
)
496 Newkeys
*newkey
= (Newkeys
*)packet_get_newkeys(mode
);
498 debug3("%s: converting %p", __func__
, newkey
);
500 if (newkey
== NULL
) {
501 error("%s: newkey == NULL", __func__
);
506 comp
= &newkey
->comp
;
510 buffer_put_cstring(&b
, enc
->name
);
511 /* The cipher struct is constant and shared, you export pointer */
512 buffer_append(&b
, &enc
->cipher
, sizeof(enc
->cipher
));
513 buffer_put_int(&b
, enc
->enabled
);
514 buffer_put_int(&b
, enc
->block_size
);
515 buffer_put_string(&b
, enc
->key
, enc
->key_len
);
516 packet_get_keyiv(mode
, enc
->iv
, enc
->block_size
);
517 buffer_put_string(&b
, enc
->iv
, enc
->block_size
);
520 buffer_put_cstring(&b
, mac
->name
);
521 buffer_put_int(&b
, mac
->enabled
);
522 buffer_put_string(&b
, mac
->key
, mac
->key_len
);
525 buffer_put_int(&b
, comp
->type
);
526 buffer_put_int(&b
, comp
->enabled
);
527 buffer_put_cstring(&b
, comp
->name
);
529 len
= buffer_len(&b
);
533 *blobp
= xmalloc(len
);
534 memcpy(*blobp
, buffer_ptr(&b
), len
);
536 memset(buffer_ptr(&b
), 0, len
);
542 mm_send_kex(Buffer
*m
, Kex
*kex
)
544 buffer_put_string(m
, kex
->session_id
, kex
->session_id_len
);
545 buffer_put_int(m
, kex
->we_need
);
546 buffer_put_int(m
, kex
->hostkey_type
);
547 buffer_put_int(m
, kex
->kex_type
);
548 buffer_put_string(m
, buffer_ptr(&kex
->my
), buffer_len(&kex
->my
));
549 buffer_put_string(m
, buffer_ptr(&kex
->peer
), buffer_len(&kex
->peer
));
550 buffer_put_int(m
, kex
->flags
);
551 buffer_put_cstring(m
, kex
->client_version_string
);
552 buffer_put_cstring(m
, kex
->server_version_string
);
556 mm_send_keystate(struct monitor
*monitor
)
558 Buffer m
, *input
, *output
;
561 u_int32_t seqnr
, packets
;
562 u_int64_t blocks
, bytes
;
571 buffer_put_int(&m
, packet_get_protocol_flags());
573 buffer_put_int(&m
, packet_get_ssh1_cipher());
575 debug3("%s: Sending ssh1 KEY+IV", __func__
);
576 keylen
= packet_get_encryption_key(NULL
);
577 key
= xmalloc(keylen
+1); /* add 1 if keylen == 0 */
578 keylen
= packet_get_encryption_key(key
);
579 buffer_put_string(&m
, key
, keylen
);
580 memset(key
, 0, keylen
);
583 ivlen
= packet_get_keyiv_len(MODE_OUT
);
584 packet_get_keyiv(MODE_OUT
, iv
, ivlen
);
585 buffer_put_string(&m
, iv
, ivlen
);
586 ivlen
= packet_get_keyiv_len(MODE_OUT
);
587 packet_get_keyiv(MODE_IN
, iv
, ivlen
);
588 buffer_put_string(&m
, iv
, ivlen
);
591 /* Kex for rekeying */
592 mm_send_kex(&m
, *monitor
->m_pkex
);
595 debug3("%s: Sending new keys: %p %p",
596 __func__
, packet_get_newkeys(MODE_OUT
),
597 packet_get_newkeys(MODE_IN
));
600 if (!mm_newkeys_to_blob(MODE_OUT
, &blob
, &bloblen
))
601 fatal("%s: conversion of newkeys failed", __func__
);
603 buffer_put_string(&m
, blob
, bloblen
);
606 if (!mm_newkeys_to_blob(MODE_IN
, &blob
, &bloblen
))
607 fatal("%s: conversion of newkeys failed", __func__
);
609 buffer_put_string(&m
, blob
, bloblen
);
612 packet_get_state(MODE_OUT
, &seqnr
, &blocks
, &packets
, &bytes
);
613 buffer_put_int(&m
, seqnr
);
614 buffer_put_int64(&m
, blocks
);
615 buffer_put_int(&m
, packets
);
616 buffer_put_int64(&m
, bytes
);
617 packet_get_state(MODE_IN
, &seqnr
, &blocks
, &packets
, &bytes
);
618 buffer_put_int(&m
, seqnr
);
619 buffer_put_int64(&m
, blocks
);
620 buffer_put_int(&m
, packets
);
621 buffer_put_int64(&m
, bytes
);
623 debug3("%s: New keys have been sent", __func__
);
625 /* More key context */
626 plen
= packet_get_keycontext(MODE_OUT
, NULL
);
628 packet_get_keycontext(MODE_OUT
, p
);
629 buffer_put_string(&m
, p
, plen
);
632 plen
= packet_get_keycontext(MODE_IN
, NULL
);
634 packet_get_keycontext(MODE_IN
, p
);
635 buffer_put_string(&m
, p
, plen
);
638 /* Compression state */
639 debug3("%s: Sending compression state", __func__
);
640 buffer_put_string(&m
, &outgoing_stream
, sizeof(outgoing_stream
));
641 buffer_put_string(&m
, &incoming_stream
, sizeof(incoming_stream
));
643 /* Network I/O buffers */
644 input
= (Buffer
*)packet_get_input();
645 output
= (Buffer
*)packet_get_output();
646 buffer_put_string(&m
, buffer_ptr(input
), buffer_len(input
));
647 buffer_put_string(&m
, buffer_ptr(output
), buffer_len(output
));
651 buffer_put_int64(&m
, get_sent_bytes());
652 buffer_put_int64(&m
, get_recv_bytes());
655 mm_request_send(monitor
->m_recvfd
, MONITOR_REQ_KEYEXPORT
, &m
);
656 debug3("%s: Finished sending state", __func__
);
662 mm_pty_allocate(int *ptyfd
, int *ttyfd
, char *namebuf
, size_t namebuflen
)
666 int success
= 0, tmp1
= -1, tmp2
= -1;
668 /* Kludge: ensure there are fds free to receive the pty/tty */
669 if ((tmp1
= dup(pmonitor
->m_recvfd
)) == -1 ||
670 (tmp2
= dup(pmonitor
->m_recvfd
)) == -1) {
671 error("%s: cannot allocate fds for pty", __func__
);
682 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PTY
, &m
);
684 debug3("%s: waiting for MONITOR_ANS_PTY", __func__
);
685 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PTY
, &m
);
687 success
= buffer_get_int(&m
);
689 debug3("%s: pty alloc failed", __func__
);
693 p
= buffer_get_string(&m
, NULL
);
694 msg
= buffer_get_string(&m
, NULL
);
697 strlcpy(namebuf
, p
, namebuflen
); /* Possible truncation */
700 buffer_append(&loginmsg
, msg
, strlen(msg
));
703 if ((*ptyfd
= mm_receive_fd(pmonitor
->m_recvfd
)) == -1 ||
704 (*ttyfd
= mm_receive_fd(pmonitor
->m_recvfd
)) == -1)
705 fatal("%s: receive fds failed", __func__
);
712 mm_session_pty_cleanup2(Session
*s
)
719 buffer_put_cstring(&m
, s
->tty
);
720 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PTYCLEANUP
, &m
);
723 /* closed dup'ed master */
724 if (s
->ptymaster
!= -1 && close(s
->ptymaster
) < 0)
725 error("close(s->ptymaster/%d): %s",
726 s
->ptymaster
, strerror(errno
));
728 /* unlink pty from session */
734 mm_start_pam(Authctxt
*authctxt
)
738 debug3("%s entering", __func__
);
739 if (!options
.use_pam
)
740 fatal("UsePAM=no, but ended up in %s anyway", __func__
);
743 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_START
, &m
);
749 mm_do_pam_account(void)
755 debug3("%s entering", __func__
);
756 if (!options
.use_pam
)
757 fatal("UsePAM=no, but ended up in %s anyway", __func__
);
760 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_ACCOUNT
, &m
);
762 mm_request_receive_expect(pmonitor
->m_recvfd
,
763 MONITOR_ANS_PAM_ACCOUNT
, &m
);
764 ret
= buffer_get_int(&m
);
765 msg
= buffer_get_string(&m
, NULL
);
766 buffer_append(&loginmsg
, msg
, strlen(msg
));
771 debug3("%s returning %d", __func__
, ret
);
777 mm_sshpam_init_ctx(Authctxt
*authctxt
)
782 debug3("%s", __func__
);
784 buffer_put_cstring(&m
, authctxt
->user
);
785 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_INIT_CTX
, &m
);
786 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__
);
787 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_INIT_CTX
, &m
);
788 success
= buffer_get_int(&m
);
790 debug3("%s: pam_init_ctx failed", __func__
);
799 mm_sshpam_query(void *ctx
, char **name
, char **info
,
800 u_int
*num
, char ***prompts
, u_int
**echo_on
)
806 debug3("%s", __func__
);
808 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_QUERY
, &m
);
809 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__
);
810 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_QUERY
, &m
);
811 ret
= buffer_get_int(&m
);
812 debug3("%s: pam_query returned %d", __func__
, ret
);
813 *name
= buffer_get_string(&m
, NULL
);
814 *info
= buffer_get_string(&m
, NULL
);
815 *num
= buffer_get_int(&m
);
816 if (*num
> PAM_MAX_NUM_MSG
)
817 fatal("%s: recieved %u PAM messages, expected <= %u",
818 __func__
, *num
, PAM_MAX_NUM_MSG
);
819 *prompts
= xcalloc((*num
+ 1), sizeof(char *));
820 *echo_on
= xcalloc((*num
+ 1), sizeof(u_int
));
821 for (i
= 0; i
< *num
; ++i
) {
822 (*prompts
)[i
] = buffer_get_string(&m
, NULL
);
823 (*echo_on
)[i
] = buffer_get_int(&m
);
830 mm_sshpam_respond(void *ctx
, u_int num
, char **resp
)
836 debug3("%s", __func__
);
838 buffer_put_int(&m
, num
);
839 for (i
= 0; i
< num
; ++i
)
840 buffer_put_cstring(&m
, resp
[i
]);
841 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_RESPOND
, &m
);
842 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__
);
843 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_RESPOND
, &m
);
844 ret
= buffer_get_int(&m
);
845 debug3("%s: pam_respond returned %d", __func__
, ret
);
851 mm_sshpam_free_ctx(void *ctxtp
)
855 debug3("%s", __func__
);
857 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_FREE_CTX
, &m
);
858 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__
);
859 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_FREE_CTX
, &m
);
864 /* Request process termination */
872 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_TERM
, &m
);
877 mm_ssh1_session_key(BIGNUM
*num
)
883 buffer_put_bignum2(&m
, num
);
884 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SESSKEY
, &m
);
886 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SESSKEY
, &m
);
888 rsafail
= buffer_get_int(&m
);
889 buffer_get_bignum2(&m
, num
);
897 mm_chall_setup(char **name
, char **infotxt
, u_int
*numprompts
,
898 char ***prompts
, u_int
**echo_on
)
901 *infotxt
= xstrdup("");
903 *prompts
= xcalloc(*numprompts
, sizeof(char *));
904 *echo_on
= xcalloc(*numprompts
, sizeof(u_int
));
909 mm_bsdauth_query(void *ctx
, char **name
, char **infotxt
,
910 u_int
*numprompts
, char ***prompts
, u_int
**echo_on
)
916 debug3("%s: entering", __func__
);
919 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_BSDAUTHQUERY
, &m
);
921 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_BSDAUTHQUERY
,
923 success
= buffer_get_int(&m
);
925 debug3("%s: no challenge", __func__
);
930 /* Get the challenge, and format the response */
931 challenge
= buffer_get_string(&m
, NULL
);
934 mm_chall_setup(name
, infotxt
, numprompts
, prompts
, echo_on
);
935 (*prompts
)[0] = challenge
;
937 debug3("%s: received challenge: %s", __func__
, challenge
);
943 mm_bsdauth_respond(void *ctx
, u_int numresponses
, char **responses
)
948 debug3("%s: entering", __func__
);
949 if (numresponses
!= 1)
953 buffer_put_cstring(&m
, responses
[0]);
954 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_BSDAUTHRESPOND
, &m
);
956 mm_request_receive_expect(pmonitor
->m_recvfd
,
957 MONITOR_ANS_BSDAUTHRESPOND
, &m
);
959 authok
= buffer_get_int(&m
);
962 return ((authok
== 0) ? -1 : 0);
967 mm_skey_query(void *ctx
, char **name
, char **infotxt
,
968 u_int
*numprompts
, char ***prompts
, u_int
**echo_on
)
974 debug3("%s: entering", __func__
);
977 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SKEYQUERY
, &m
);
979 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SKEYQUERY
,
981 success
= buffer_get_int(&m
);
983 debug3("%s: no challenge", __func__
);
988 /* Get the challenge, and format the response */
989 challenge
= buffer_get_string(&m
, NULL
);
992 debug3("%s: received challenge: %s", __func__
, challenge
);
994 mm_chall_setup(name
, infotxt
, numprompts
, prompts
, echo_on
);
996 xasprintf(*prompts
, "%s%s", challenge
, SKEY_PROMPT
);
1003 mm_skey_respond(void *ctx
, u_int numresponses
, char **responses
)
1008 debug3("%s: entering", __func__
);
1009 if (numresponses
!= 1)
1013 buffer_put_cstring(&m
, responses
[0]);
1014 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SKEYRESPOND
, &m
);
1016 mm_request_receive_expect(pmonitor
->m_recvfd
,
1017 MONITOR_ANS_SKEYRESPOND
, &m
);
1019 authok
= buffer_get_int(&m
);
1022 return ((authok
== 0) ? -1 : 0);
1027 mm_ssh1_session_id(u_char session_id
[16])
1032 debug3("%s entering", __func__
);
1035 for (i
= 0; i
< 16; i
++)
1036 buffer_put_char(&m
, session_id
[i
]);
1038 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SESSID
, &m
);
1043 mm_auth_rsa_key_allowed(struct passwd
*pw
, BIGNUM
*client_n
, Key
**rkey
)
1049 int allowed
= 0, have_forced
= 0;
1051 debug3("%s entering", __func__
);
1054 buffer_put_bignum2(&m
, client_n
);
1056 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSAKEYALLOWED
, &m
);
1057 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSAKEYALLOWED
, &m
);
1059 allowed
= buffer_get_int(&m
);
1061 /* fake forced command */
1062 auth_clear_options();
1063 have_forced
= buffer_get_int(&m
);
1064 forced_command
= have_forced
? xstrdup("true") : NULL
;
1066 if (allowed
&& rkey
!= NULL
) {
1067 blob
= buffer_get_string(&m
, &blen
);
1068 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1069 fatal("%s: key_from_blob failed", __func__
);
1079 mm_auth_rsa_generate_challenge(Key
*key
)
1086 debug3("%s entering", __func__
);
1088 if ((challenge
= BN_new()) == NULL
)
1089 fatal("%s: BN_new failed", __func__
);
1091 key
->type
= KEY_RSA
; /* XXX cheat for key_to_blob */
1092 if (key_to_blob(key
, &blob
, &blen
) == 0)
1093 fatal("%s: key_to_blob failed", __func__
);
1094 key
->type
= KEY_RSA1
;
1097 buffer_put_string(&m
, blob
, blen
);
1100 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSACHALLENGE
, &m
);
1101 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSACHALLENGE
, &m
);
1103 buffer_get_bignum2(&m
, challenge
);
1110 mm_auth_rsa_verify_response(Key
*key
, BIGNUM
*p
, u_char response
[16])
1117 debug3("%s entering", __func__
);
1119 key
->type
= KEY_RSA
; /* XXX cheat for key_to_blob */
1120 if (key_to_blob(key
, &blob
, &blen
) == 0)
1121 fatal("%s: key_to_blob failed", __func__
);
1122 key
->type
= KEY_RSA1
;
1125 buffer_put_string(&m
, blob
, blen
);
1126 buffer_put_string(&m
, response
, 16);
1129 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSARESPONSE
, &m
);
1130 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSARESPONSE
, &m
);
1132 success
= buffer_get_int(&m
);
1138 #ifdef SSH_AUDIT_EVENTS
1140 mm_audit_event(ssh_audit_event_t event
)
1144 debug3("%s entering", __func__
);
1147 buffer_put_int(&m
, event
);
1149 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUDIT_EVENT
, &m
);
1154 mm_audit_run_command(const char *command
)
1158 debug3("%s entering command %s", __func__
, command
);
1161 buffer_put_cstring(&m
, command
);
1163 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUDIT_COMMAND
, &m
);
1166 #endif /* SSH_AUDIT_EVENTS */
1170 mm_ssh_gssapi_server_ctx(Gssctxt
**ctx
, gss_OID goid
)
1175 /* Client doesn't get to see the context */
1179 buffer_put_string(&m
, goid
->elements
, goid
->length
);
1181 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSSETUP
, &m
);
1182 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSSETUP
, &m
);
1184 major
= buffer_get_int(&m
);
1191 mm_ssh_gssapi_accept_ctx(Gssctxt
*ctx
, gss_buffer_desc
*in
,
1192 gss_buffer_desc
*out
, OM_uint32
*flags
)
1199 buffer_put_string(&m
, in
->value
, in
->length
);
1201 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSSTEP
, &m
);
1202 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSSTEP
, &m
);
1204 major
= buffer_get_int(&m
);
1205 out
->value
= buffer_get_string(&m
, &len
);
1208 *flags
= buffer_get_int(&m
);
1216 mm_ssh_gssapi_checkmic(Gssctxt
*ctx
, gss_buffer_t gssbuf
, gss_buffer_t gssmic
)
1222 buffer_put_string(&m
, gssbuf
->value
, gssbuf
->length
);
1223 buffer_put_string(&m
, gssmic
->value
, gssmic
->length
);
1225 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSCHECKMIC
, &m
);
1226 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSCHECKMIC
,
1229 major
= buffer_get_int(&m
);
1235 mm_ssh_gssapi_userok(char *user
)
1238 int authenticated
= 0;
1242 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSUSEROK
, &m
);
1243 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSUSEROK
,
1246 authenticated
= buffer_get_int(&m
);
1249 debug3("%s: user %sauthenticated",__func__
, authenticated
? "" : "not ");
1250 return (authenticated
);
1256 mm_auth2_jpake_get_pwdata(Authctxt
*authctxt
, BIGNUM
**s
,
1257 char **hash_scheme
, char **salt
)
1261 debug3("%s entering", __func__
);
1264 mm_request_send(pmonitor
->m_recvfd
,
1265 MONITOR_REQ_JPAKE_GET_PWDATA
, &m
);
1267 debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__
);
1268 mm_request_receive_expect(pmonitor
->m_recvfd
,
1269 MONITOR_ANS_JPAKE_GET_PWDATA
, &m
);
1271 *hash_scheme
= buffer_get_string(&m
, NULL
);
1272 *salt
= buffer_get_string(&m
, NULL
);
1278 mm_jpake_step1(struct modp_group
*grp
,
1279 u_char
**id
, u_int
*id_len
,
1280 BIGNUM
**priv1
, BIGNUM
**priv2
, BIGNUM
**g_priv1
, BIGNUM
**g_priv2
,
1281 u_char
**priv1_proof
, u_int
*priv1_proof_len
,
1282 u_char
**priv2_proof
, u_int
*priv2_proof_len
)
1286 debug3("%s entering", __func__
);
1289 mm_request_send(pmonitor
->m_recvfd
,
1290 MONITOR_REQ_JPAKE_STEP1
, &m
);
1292 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__
);
1293 mm_request_receive_expect(pmonitor
->m_recvfd
,
1294 MONITOR_ANS_JPAKE_STEP1
, &m
);
1296 if ((*priv1
= BN_new()) == NULL
||
1297 (*priv2
= BN_new()) == NULL
||
1298 (*g_priv1
= BN_new()) == NULL
||
1299 (*g_priv2
= BN_new()) == NULL
)
1300 fatal("%s: BN_new", __func__
);
1302 *id
= buffer_get_string(&m
, id_len
);
1303 /* priv1 and priv2 are, well, private */
1304 buffer_get_bignum2(&m
, *g_priv1
);
1305 buffer_get_bignum2(&m
, *g_priv2
);
1306 *priv1_proof
= buffer_get_string(&m
, priv1_proof_len
);
1307 *priv2_proof
= buffer_get_string(&m
, priv2_proof_len
);
1313 mm_jpake_step2(struct modp_group
*grp
, BIGNUM
*s
,
1314 BIGNUM
*mypub1
, BIGNUM
*theirpub1
, BIGNUM
*theirpub2
, BIGNUM
*mypriv2
,
1315 const u_char
*theirid
, u_int theirid_len
,
1316 const u_char
*myid
, u_int myid_len
,
1317 const u_char
*theirpub1_proof
, u_int theirpub1_proof_len
,
1318 const u_char
*theirpub2_proof
, u_int theirpub2_proof_len
,
1320 u_char
**newpub_exponent_proof
, u_int
*newpub_exponent_proof_len
)
1324 debug3("%s entering", __func__
);
1327 /* monitor already has all bignums except theirpub1, theirpub2 */
1328 buffer_put_bignum2(&m
, theirpub1
);
1329 buffer_put_bignum2(&m
, theirpub2
);
1330 /* monitor already knows our id */
1331 buffer_put_string(&m
, theirid
, theirid_len
);
1332 buffer_put_string(&m
, theirpub1_proof
, theirpub1_proof_len
);
1333 buffer_put_string(&m
, theirpub2_proof
, theirpub2_proof_len
);
1335 mm_request_send(pmonitor
->m_recvfd
,
1336 MONITOR_REQ_JPAKE_STEP2
, &m
);
1338 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__
);
1339 mm_request_receive_expect(pmonitor
->m_recvfd
,
1340 MONITOR_ANS_JPAKE_STEP2
, &m
);
1342 if ((*newpub
= BN_new()) == NULL
)
1343 fatal("%s: BN_new", __func__
);
1345 buffer_get_bignum2(&m
, *newpub
);
1346 *newpub_exponent_proof
= buffer_get_string(&m
,
1347 newpub_exponent_proof_len
);
1353 mm_jpake_key_confirm(struct modp_group
*grp
, BIGNUM
*s
, BIGNUM
*step2_val
,
1354 BIGNUM
*mypriv2
, BIGNUM
*mypub1
, BIGNUM
*mypub2
,
1355 BIGNUM
*theirpub1
, BIGNUM
*theirpub2
,
1356 const u_char
*my_id
, u_int my_id_len
,
1357 const u_char
*their_id
, u_int their_id_len
,
1358 const u_char
*sess_id
, u_int sess_id_len
,
1359 const u_char
*theirpriv2_s_proof
, u_int theirpriv2_s_proof_len
,
1361 u_char
**confirm_hash
, u_int
*confirm_hash_len
)
1365 debug3("%s entering", __func__
);
1368 /* monitor already has all bignums except step2_val */
1369 buffer_put_bignum2(&m
, step2_val
);
1370 /* monitor already knows all the ids */
1371 buffer_put_string(&m
, theirpriv2_s_proof
, theirpriv2_s_proof_len
);
1373 mm_request_send(pmonitor
->m_recvfd
,
1374 MONITOR_REQ_JPAKE_KEY_CONFIRM
, &m
);
1376 debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__
);
1377 mm_request_receive_expect(pmonitor
->m_recvfd
,
1378 MONITOR_ANS_JPAKE_KEY_CONFIRM
, &m
);
1380 /* 'k' is sensitive and stays in the monitor */
1381 *confirm_hash
= buffer_get_string(&m
, confirm_hash_len
);
1387 mm_jpake_check_confirm(const BIGNUM
*k
,
1388 const u_char
*peer_id
, u_int peer_id_len
,
1389 const u_char
*sess_id
, u_int sess_id_len
,
1390 const u_char
*peer_confirm_hash
, u_int peer_confirm_hash_len
)
1395 debug3("%s entering", __func__
);
1398 /* k is dummy in slave, ignored */
1399 /* monitor knows all the ids */
1400 buffer_put_string(&m
, peer_confirm_hash
, peer_confirm_hash_len
);
1401 mm_request_send(pmonitor
->m_recvfd
,
1402 MONITOR_REQ_JPAKE_CHECK_CONFIRM
, &m
);
1404 debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__
);
1405 mm_request_receive_expect(pmonitor
->m_recvfd
,
1406 MONITOR_ANS_JPAKE_CHECK_CONFIRM
, &m
);
1408 success
= buffer_get_int(&m
);
1411 debug3("%s: success = %d", __func__
, success
);