1 /* $NetBSD: monitor_wrap.c,v 1.1.1.2 2009/12/27 01:06:59 christos Exp $ */
2 /* $OpenBSD: monitor_wrap.c,v 1.68 2009/06/22 05:39:28 dtucker Exp $ */
4 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
5 * Copyright 2002 Markus Friedl <markus@openbsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 __RCSID("$NetBSD: monitor_wrap.c,v 1.2 2009/06/07 22:38:46 christos Exp $");
31 #include <sys/types.h>
33 #include <sys/queue.h>
42 #include <openssl/bn.h>
43 #include <openssl/dh.h>
44 #include <openssl/evp.h>
55 #include "auth-options.h"
64 #include "monitor_wrap.h"
66 #include "monitor_fdpass.h"
69 #include <security/pam_appl.h>
82 extern z_stream incoming_stream
;
83 extern z_stream outgoing_stream
;
84 extern struct monitor
*pmonitor
;
85 extern Buffer loginmsg
;
86 extern ServerOptions options
;
92 * m_pid is only set in the privileged part, and
93 * points to the unprivileged child.
95 return (pmonitor
&& pmonitor
->m_pid
> 0);
99 mm_request_send(int sock
, enum monitor_reqtype type
, Buffer
*m
)
101 u_int mlen
= buffer_len(m
);
104 debug3("%s entering: type %d", __func__
, type
);
106 put_u32(buf
, mlen
+ 1);
107 buf
[4] = (u_char
) type
; /* 1st byte of payload is mesg-type */
108 if (atomicio(vwrite
, sock
, buf
, sizeof(buf
)) != sizeof(buf
))
109 fatal("%s: write: %s", __func__
, strerror(errno
));
110 if (atomicio(vwrite
, sock
, buffer_ptr(m
), mlen
) != mlen
)
111 fatal("%s: write: %s", __func__
, strerror(errno
));
115 mm_request_receive(int sock
, Buffer
*m
)
120 debug3("%s entering", __func__
);
122 if (atomicio(read
, sock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
125 fatal("%s: read: %s", __func__
, strerror(errno
));
127 msg_len
= get_u32(buf
);
128 if (msg_len
> 256 * 1024)
129 fatal("%s: read: bad msg_len %d", __func__
, msg_len
);
131 buffer_append_space(m
, msg_len
);
132 if (atomicio(read
, sock
, buffer_ptr(m
), msg_len
) != msg_len
)
133 fatal("%s: read: %s", __func__
, strerror(errno
));
137 mm_request_receive_expect(int sock
, enum monitor_reqtype type
, Buffer
*m
)
141 debug3("%s entering: type %d", __func__
, type
);
143 mm_request_receive(sock
, m
);
144 rtype
= buffer_get_char(m
);
146 fatal("%s: read: rtype %d != type %d", __func__
,
151 mm_choose_dh(int min
, int nbits
, int max
)
158 buffer_put_int(&m
, min
);
159 buffer_put_int(&m
, nbits
);
160 buffer_put_int(&m
, max
);
162 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_MODULI
, &m
);
164 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__
);
165 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_MODULI
, &m
);
167 success
= buffer_get_char(&m
);
169 fatal("%s: MONITOR_ANS_MODULI failed", __func__
);
171 if ((p
= BN_new()) == NULL
)
172 fatal("%s: BN_new failed", __func__
);
173 if ((g
= BN_new()) == NULL
)
174 fatal("%s: BN_new failed", __func__
);
175 buffer_get_bignum2(&m
, p
);
176 buffer_get_bignum2(&m
, g
);
178 debug3("%s: remaining %d", __func__
, buffer_len(&m
));
181 return (dh_new_group(g
, p
));
185 mm_key_sign(Key
*key
, u_char
**sigp
, u_int
*lenp
, u_char
*data
, u_int datalen
)
187 Kex
*kex
= *pmonitor
->m_pkex
;
190 debug3("%s entering", __func__
);
193 buffer_put_int(&m
, kex
->host_key_index(key
));
194 buffer_put_string(&m
, data
, datalen
);
196 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SIGN
, &m
);
198 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__
);
199 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SIGN
, &m
);
200 *sigp
= buffer_get_string(&m
, lenp
);
207 mm_getpwnamallow(const char *username
)
212 ServerOptions
*newopts
;
214 debug3("%s entering", __func__
);
217 buffer_put_cstring(&m
, username
);
219 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PWNAM
, &m
);
221 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__
);
222 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PWNAM
, &m
);
224 if (buffer_get_char(&m
) == 0) {
228 pw
= buffer_get_string(&m
, &len
);
229 if (len
!= sizeof(struct passwd
))
230 fatal("%s: struct passwd size mismatch", __func__
);
231 pw
->pw_name
= buffer_get_string(&m
, NULL
);
232 pw
->pw_passwd
= buffer_get_string(&m
, NULL
);
233 pw
->pw_gecos
= buffer_get_string(&m
, NULL
);
234 pw
->pw_class
= buffer_get_string(&m
, NULL
);
235 pw
->pw_dir
= buffer_get_string(&m
, NULL
);
236 pw
->pw_shell
= buffer_get_string(&m
, NULL
);
239 /* copy options block as a Match directive may have changed some */
240 newopts
= buffer_get_string(&m
, &len
);
241 if (len
!= sizeof(*newopts
))
242 fatal("%s: option block size mismatch", __func__
);
243 if (newopts
->banner
!= NULL
)
244 newopts
->banner
= buffer_get_string(&m
, NULL
);
245 copy_set_server_options(&options
, newopts
, 1);
254 mm_auth2_read_banner(void)
259 debug3("%s entering", __func__
);
262 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTH2_READ_BANNER
, &m
);
265 mm_request_receive_expect(pmonitor
->m_recvfd
,
266 MONITOR_ANS_AUTH2_READ_BANNER
, &m
);
267 banner
= buffer_get_string(&m
, NULL
);
270 /* treat empty banner as missing banner */
271 if (strlen(banner
) == 0) {
278 /* Inform the privileged process about service and style */
281 mm_inform_authserv(char *service
, char *style
)
285 debug3("%s entering", __func__
);
288 buffer_put_cstring(&m
, service
);
289 buffer_put_cstring(&m
, style
? style
: "");
291 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTHSERV
, &m
);
296 /* Do the password authentication */
298 mm_auth_password(Authctxt
*authctxt
, char *password
)
301 int authenticated
= 0;
303 debug3("%s entering", __func__
);
306 buffer_put_cstring(&m
, password
);
307 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTHPASSWORD
, &m
);
309 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__
);
310 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_AUTHPASSWORD
, &m
);
312 authenticated
= buffer_get_int(&m
);
316 debug3("%s: user %sauthenticated",
317 __func__
, authenticated
? "" : "not ");
318 return (authenticated
);
322 mm_user_key_allowed(struct passwd
*pw
, Key
*key
)
324 return (mm_key_allowed(MM_USERKEY
, NULL
, NULL
, key
));
328 mm_hostbased_key_allowed(struct passwd
*pw
, char *user
, char *host
,
331 return (mm_key_allowed(MM_HOSTKEY
, user
, host
, key
));
335 mm_auth_rhosts_rsa_key_allowed(struct passwd
*pw
, char *user
,
336 char *host
, Key
*key
)
340 key
->type
= KEY_RSA
; /* XXX hack for key_to_blob */
341 ret
= mm_key_allowed(MM_RSAHOSTKEY
, user
, host
, key
);
342 key
->type
= KEY_RSA1
;
347 mm_send_debug(Buffer
*m
)
351 while (buffer_len(m
)) {
352 msg
= buffer_get_string(m
, NULL
);
353 debug3("%s: Sending debug: %s", __func__
, msg
);
354 packet_send_debug("%s", msg
);
360 mm_key_allowed(enum mm_keytype type
, char *user
, char *host
, Key
*key
)
365 int allowed
= 0, have_forced
= 0;
367 debug3("%s entering", __func__
);
369 /* Convert the key to a blob and the pass it over */
370 if (!key_to_blob(key
, &blob
, &len
))
374 buffer_put_int(&m
, type
);
375 buffer_put_cstring(&m
, user
? user
: "");
376 buffer_put_cstring(&m
, host
? host
: "");
377 buffer_put_string(&m
, blob
, len
);
380 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_KEYALLOWED
, &m
);
382 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__
);
383 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_KEYALLOWED
, &m
);
385 allowed
= buffer_get_int(&m
);
387 /* fake forced command */
388 auth_clear_options();
389 have_forced
= buffer_get_int(&m
);
390 forced_command
= have_forced
? xstrdup("true") : NULL
;
392 /* Send potential debug messages */
401 * This key verify needs to send the key type along, because the
402 * privileged parent makes the decision if the key is allowed
403 * for authentication.
407 mm_key_verify(Key
*key
, u_char
*sig
, u_int siglen
, u_char
*data
, u_int datalen
)
414 debug3("%s entering", __func__
);
416 /* Convert the key to a blob and the pass it over */
417 if (!key_to_blob(key
, &blob
, &len
))
421 buffer_put_string(&m
, blob
, len
);
422 buffer_put_string(&m
, sig
, siglen
);
423 buffer_put_string(&m
, data
, datalen
);
426 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_KEYVERIFY
, &m
);
428 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__
);
429 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_KEYVERIFY
, &m
);
431 verified
= buffer_get_int(&m
);
438 /* Export key state after authentication */
440 mm_newkeys_from_blob(u_char
*blob
, int blen
)
444 Newkeys
*newkey
= NULL
;
449 debug3("%s: %p(%d)", __func__
, blob
, blen
);
451 dump_base64(stderr
, blob
, blen
);
454 buffer_append(&b
, blob
, blen
);
456 newkey
= xmalloc(sizeof(*newkey
));
459 comp
= &newkey
->comp
;
462 enc
->name
= buffer_get_string(&b
, NULL
);
463 buffer_get(&b
, &enc
->cipher
, sizeof(enc
->cipher
));
464 enc
->enabled
= buffer_get_int(&b
);
465 enc
->block_size
= buffer_get_int(&b
);
466 enc
->key
= buffer_get_string(&b
, &enc
->key_len
);
467 enc
->iv
= buffer_get_string(&b
, &len
);
468 if (len
!= enc
->block_size
)
469 fatal("%s: bad ivlen: expected %u != %u", __func__
,
470 enc
->block_size
, len
);
472 if (enc
->name
== NULL
|| cipher_by_name(enc
->name
) != enc
->cipher
)
473 fatal("%s: bad cipher name %s or pointer %p", __func__
,
474 enc
->name
, enc
->cipher
);
477 mac
->name
= buffer_get_string(&b
, NULL
);
478 if (mac
->name
== NULL
|| mac_setup(mac
, mac
->name
) == -1)
479 fatal("%s: can not setup mac %s", __func__
, mac
->name
);
480 mac
->enabled
= buffer_get_int(&b
);
481 mac
->key
= buffer_get_string(&b
, &len
);
482 if (len
> mac
->key_len
)
483 fatal("%s: bad mac key length: %u > %d", __func__
, len
,
488 comp
->type
= buffer_get_int(&b
);
489 comp
->enabled
= buffer_get_int(&b
);
490 comp
->name
= buffer_get_string(&b
, NULL
);
492 len
= buffer_len(&b
);
494 error("newkeys_from_blob: remaining bytes in blob %u", len
);
500 mm_newkeys_to_blob(int mode
, u_char
**blobp
, u_int
*lenp
)
507 Newkeys
*newkey
= (Newkeys
*)packet_get_newkeys(mode
);
509 debug3("%s: converting %p", __func__
, newkey
);
511 if (newkey
== NULL
) {
512 error("%s: newkey == NULL", __func__
);
517 comp
= &newkey
->comp
;
521 buffer_put_cstring(&b
, enc
->name
);
522 /* The cipher struct is constant and shared, you export pointer */
523 buffer_append(&b
, &enc
->cipher
, sizeof(enc
->cipher
));
524 buffer_put_int(&b
, enc
->enabled
);
525 buffer_put_int(&b
, enc
->block_size
);
526 buffer_put_string(&b
, enc
->key
, enc
->key_len
);
527 packet_get_keyiv(mode
, enc
->iv
, enc
->block_size
);
528 buffer_put_string(&b
, enc
->iv
, enc
->block_size
);
531 buffer_put_cstring(&b
, mac
->name
);
532 buffer_put_int(&b
, mac
->enabled
);
533 buffer_put_string(&b
, mac
->key
, mac
->key_len
);
536 buffer_put_int(&b
, comp
->type
);
537 buffer_put_int(&b
, comp
->enabled
);
538 buffer_put_cstring(&b
, comp
->name
);
540 len
= buffer_len(&b
);
544 *blobp
= xmalloc(len
);
545 memcpy(*blobp
, buffer_ptr(&b
), len
);
547 memset(buffer_ptr(&b
), 0, len
);
553 mm_send_kex(Buffer
*m
, Kex
*kex
)
555 buffer_put_string(m
, kex
->session_id
, kex
->session_id_len
);
556 buffer_put_int(m
, kex
->we_need
);
557 buffer_put_int(m
, kex
->hostkey_type
);
558 buffer_put_int(m
, kex
->kex_type
);
559 buffer_put_string(m
, buffer_ptr(&kex
->my
), buffer_len(&kex
->my
));
560 buffer_put_string(m
, buffer_ptr(&kex
->peer
), buffer_len(&kex
->peer
));
561 buffer_put_int(m
, kex
->flags
);
562 buffer_put_cstring(m
, kex
->client_version_string
);
563 buffer_put_cstring(m
, kex
->server_version_string
);
567 mm_send_keystate(struct monitor
*monitor
)
569 Buffer m
, *input
, *output
;
572 u_int32_t seqnr
, packets
;
573 u_int64_t blocks
, bytes
;
582 buffer_put_int(&m
, packet_get_protocol_flags());
584 buffer_put_int(&m
, packet_get_ssh1_cipher());
586 debug3("%s: Sending ssh1 KEY+IV", __func__
);
587 keylen
= packet_get_encryption_key(NULL
);
588 key
= xmalloc(keylen
+1); /* add 1 if keylen == 0 */
589 keylen
= packet_get_encryption_key(key
);
590 buffer_put_string(&m
, key
, keylen
);
591 memset(key
, 0, keylen
);
594 ivlen
= packet_get_keyiv_len(MODE_OUT
);
595 packet_get_keyiv(MODE_OUT
, iv
, ivlen
);
596 buffer_put_string(&m
, iv
, ivlen
);
597 ivlen
= packet_get_keyiv_len(MODE_OUT
);
598 packet_get_keyiv(MODE_IN
, iv
, ivlen
);
599 buffer_put_string(&m
, iv
, ivlen
);
602 /* Kex for rekeying */
603 mm_send_kex(&m
, *monitor
->m_pkex
);
606 debug3("%s: Sending new keys: %p %p",
607 __func__
, packet_get_newkeys(MODE_OUT
),
608 packet_get_newkeys(MODE_IN
));
611 if (!mm_newkeys_to_blob(MODE_OUT
, &blob
, &bloblen
))
612 fatal("%s: conversion of newkeys failed", __func__
);
614 buffer_put_string(&m
, blob
, bloblen
);
617 if (!mm_newkeys_to_blob(MODE_IN
, &blob
, &bloblen
))
618 fatal("%s: conversion of newkeys failed", __func__
);
620 buffer_put_string(&m
, blob
, bloblen
);
623 packet_get_state(MODE_OUT
, &seqnr
, &blocks
, &packets
, &bytes
);
624 buffer_put_int(&m
, seqnr
);
625 buffer_put_int64(&m
, blocks
);
626 buffer_put_int(&m
, packets
);
627 buffer_put_int64(&m
, bytes
);
628 packet_get_state(MODE_IN
, &seqnr
, &blocks
, &packets
, &bytes
);
629 buffer_put_int(&m
, seqnr
);
630 buffer_put_int64(&m
, blocks
);
631 buffer_put_int(&m
, packets
);
632 buffer_put_int64(&m
, bytes
);
634 debug3("%s: New keys have been sent", __func__
);
636 /* More key context */
637 plen
= packet_get_keycontext(MODE_OUT
, NULL
);
639 packet_get_keycontext(MODE_OUT
, p
);
640 buffer_put_string(&m
, p
, plen
);
643 plen
= packet_get_keycontext(MODE_IN
, NULL
);
645 packet_get_keycontext(MODE_IN
, p
);
646 buffer_put_string(&m
, p
, plen
);
649 /* Compression state */
650 debug3("%s: Sending compression state", __func__
);
651 buffer_put_string(&m
, &outgoing_stream
, sizeof(outgoing_stream
));
652 buffer_put_string(&m
, &incoming_stream
, sizeof(incoming_stream
));
654 /* Network I/O buffers */
655 input
= (Buffer
*)packet_get_input();
656 output
= (Buffer
*)packet_get_output();
657 buffer_put_string(&m
, buffer_ptr(input
), buffer_len(input
));
658 buffer_put_string(&m
, buffer_ptr(output
), buffer_len(output
));
662 buffer_put_int64(&m
, get_sent_bytes());
663 buffer_put_int64(&m
, get_recv_bytes());
666 mm_request_send(monitor
->m_recvfd
, MONITOR_REQ_KEYEXPORT
, &m
);
667 debug3("%s: Finished sending state", __func__
);
673 mm_pty_allocate(int *ptyfd
, int *ttyfd
, char *namebuf
, size_t namebuflen
)
677 int success
= 0, tmp1
= -1, tmp2
= -1;
679 /* Kludge: ensure there are fds free to receive the pty/tty */
680 if ((tmp1
= dup(pmonitor
->m_recvfd
)) == -1 ||
681 (tmp2
= dup(pmonitor
->m_recvfd
)) == -1) {
682 error("%s: cannot allocate fds for pty", __func__
);
693 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PTY
, &m
);
695 debug3("%s: waiting for MONITOR_ANS_PTY", __func__
);
696 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PTY
, &m
);
698 success
= buffer_get_int(&m
);
700 debug3("%s: pty alloc failed", __func__
);
704 p
= buffer_get_string(&m
, NULL
);
705 msg
= buffer_get_string(&m
, NULL
);
708 strlcpy(namebuf
, p
, namebuflen
); /* Possible truncation */
711 buffer_append(&loginmsg
, msg
, strlen(msg
));
714 if ((*ptyfd
= mm_receive_fd(pmonitor
->m_recvfd
)) == -1 ||
715 (*ttyfd
= mm_receive_fd(pmonitor
->m_recvfd
)) == -1)
716 fatal("%s: receive fds failed", __func__
);
723 mm_session_pty_cleanup2(Session
*s
)
730 buffer_put_cstring(&m
, s
->tty
);
731 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PTYCLEANUP
, &m
);
734 /* closed dup'ed master */
735 if (s
->ptymaster
!= -1 && close(s
->ptymaster
) < 0)
736 error("close(s->ptymaster/%d): %s",
737 s
->ptymaster
, strerror(errno
));
739 /* unlink pty from session */
745 mm_start_pam(Authctxt
*authctxt
)
749 debug3("%s entering", __func__
);
750 if (!options
.use_pam
)
751 fatal("UsePAM=no, but ended up in %s anyway", __func__
);
754 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_START
, &m
);
760 mm_do_pam_account(void)
766 debug3("%s entering", __func__
);
767 if (!options
.use_pam
)
768 fatal("UsePAM=no, but ended up in %s anyway", __func__
);
771 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_ACCOUNT
, &m
);
773 mm_request_receive_expect(pmonitor
->m_recvfd
,
774 MONITOR_ANS_PAM_ACCOUNT
, &m
);
775 ret
= buffer_get_int(&m
);
776 msg
= buffer_get_string(&m
, NULL
);
777 buffer_append(&loginmsg
, msg
, strlen(msg
));
782 debug3("%s returning %d", __func__
, ret
);
788 mm_sshpam_init_ctx(Authctxt
*authctxt
)
793 debug3("%s", __func__
);
795 buffer_put_cstring(&m
, authctxt
->user
);
796 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_INIT_CTX
, &m
);
797 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__
);
798 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_INIT_CTX
, &m
);
799 success
= buffer_get_int(&m
);
801 debug3("%s: pam_init_ctx failed", __func__
);
810 mm_sshpam_query(void *ctx
, char **name
, char **info
,
811 u_int
*num
, char ***prompts
, u_int
**echo_on
)
817 debug3("%s", __func__
);
819 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_QUERY
, &m
);
820 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__
);
821 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_QUERY
, &m
);
822 ret
= buffer_get_int(&m
);
823 debug3("%s: pam_query returned %d", __func__
, ret
);
824 *name
= buffer_get_string(&m
, NULL
);
825 *info
= buffer_get_string(&m
, NULL
);
826 *num
= buffer_get_int(&m
);
827 if (*num
> PAM_MAX_NUM_MSG
)
828 fatal("%s: received %u PAM messages, expected <= %u",
829 __func__
, *num
, PAM_MAX_NUM_MSG
);
830 *prompts
= xcalloc((*num
+ 1), sizeof(char *));
831 *echo_on
= xcalloc((*num
+ 1), sizeof(u_int
));
832 for (i
= 0; i
< *num
; ++i
) {
833 (*prompts
)[i
] = buffer_get_string(&m
, NULL
);
834 (*echo_on
)[i
] = buffer_get_int(&m
);
841 mm_sshpam_respond(void *ctx
, u_int num
, char **resp
)
847 debug3("%s", __func__
);
849 buffer_put_int(&m
, num
);
850 for (i
= 0; i
< num
; ++i
)
851 buffer_put_cstring(&m
, resp
[i
]);
852 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_RESPOND
, &m
);
853 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__
);
854 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_RESPOND
, &m
);
855 ret
= buffer_get_int(&m
);
856 debug3("%s: pam_respond returned %d", __func__
, ret
);
862 mm_sshpam_free_ctx(void *ctxtp
)
866 debug3("%s", __func__
);
868 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_FREE_CTX
, &m
);
869 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__
);
870 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_FREE_CTX
, &m
);
875 /* Request process termination */
883 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_TERM
, &m
);
888 mm_ssh1_session_key(BIGNUM
*num
)
894 buffer_put_bignum2(&m
, num
);
895 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SESSKEY
, &m
);
897 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SESSKEY
, &m
);
899 rsafail
= buffer_get_int(&m
);
900 buffer_get_bignum2(&m
, num
);
907 #if defined(BSD_AUTH) || defined(SKEY)
909 mm_chall_setup(char **name
, char **infotxt
, u_int
*numprompts
,
910 char ***prompts
, u_int
**echo_on
)
913 *infotxt
= xstrdup("");
915 *prompts
= xcalloc(*numprompts
, sizeof(char *));
916 *echo_on
= xcalloc(*numprompts
, sizeof(u_int
));
923 mm_bsdauth_query(void *ctx
, char **name
, char **infotxt
,
924 u_int
*numprompts
, char ***prompts
, u_int
**echo_on
)
930 debug3("%s: entering", __func__
);
933 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_BSDAUTHQUERY
, &m
);
935 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_BSDAUTHQUERY
,
937 success
= buffer_get_int(&m
);
939 debug3("%s: no challenge", __func__
);
944 /* Get the challenge, and format the response */
945 challenge
= buffer_get_string(&m
, NULL
);
948 mm_chall_setup(name
, infotxt
, numprompts
, prompts
, echo_on
);
949 (*prompts
)[0] = challenge
;
951 debug3("%s: received challenge: %s", __func__
, challenge
);
957 mm_bsdauth_respond(void *ctx
, u_int numresponses
, char **responses
)
962 debug3("%s: entering", __func__
);
963 if (numresponses
!= 1)
967 buffer_put_cstring(&m
, responses
[0]);
968 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_BSDAUTHRESPOND
, &m
);
970 mm_request_receive_expect(pmonitor
->m_recvfd
,
971 MONITOR_ANS_BSDAUTHRESPOND
, &m
);
973 authok
= buffer_get_int(&m
);
976 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
);
1156 mm_ssh_gssapi_server_ctx(Gssctxt
**ctx
, gss_OID goid
)
1161 /* Client doesn't get to see the context */
1165 buffer_put_string(&m
, goid
->elements
, goid
->length
);
1167 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSSETUP
, &m
);
1168 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSSETUP
, &m
);
1170 major
= buffer_get_int(&m
);
1177 mm_ssh_gssapi_accept_ctx(Gssctxt
*ctx
, gss_buffer_desc
*in
,
1178 gss_buffer_desc
*out
, OM_uint32
*flags
)
1185 buffer_put_string(&m
, in
->value
, in
->length
);
1187 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSSTEP
, &m
);
1188 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSSTEP
, &m
);
1190 major
= buffer_get_int(&m
);
1191 out
->value
= buffer_get_string(&m
, &len
);
1194 *flags
= buffer_get_int(&m
);
1202 mm_ssh_gssapi_checkmic(Gssctxt
*ctx
, gss_buffer_t gssbuf
, gss_buffer_t gssmic
)
1208 buffer_put_string(&m
, gssbuf
->value
, gssbuf
->length
);
1209 buffer_put_string(&m
, gssmic
->value
, gssmic
->length
);
1211 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSCHECKMIC
, &m
);
1212 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSCHECKMIC
,
1215 major
= buffer_get_int(&m
);
1221 mm_ssh_gssapi_userok(char *user
)
1224 int authenticated
= 0;
1228 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSUSEROK
, &m
);
1229 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSUSEROK
,
1232 authenticated
= buffer_get_int(&m
);
1235 debug3("%s: user %sauthenticated",__func__
, authenticated
? "" : "not ");
1236 return (authenticated
);
1242 mm_auth2_jpake_get_pwdata(Authctxt
*authctxt
, BIGNUM
**s
,
1243 char **hash_scheme
, char **salt
)
1247 debug3("%s entering", __func__
);
1250 mm_request_send(pmonitor
->m_recvfd
,
1251 MONITOR_REQ_JPAKE_GET_PWDATA
, &m
);
1253 debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__
);
1254 mm_request_receive_expect(pmonitor
->m_recvfd
,
1255 MONITOR_ANS_JPAKE_GET_PWDATA
, &m
);
1257 *hash_scheme
= buffer_get_string(&m
, NULL
);
1258 *salt
= buffer_get_string(&m
, NULL
);
1264 mm_jpake_step1(struct modp_group
*grp
,
1265 u_char
**id
, u_int
*id_len
,
1266 BIGNUM
**priv1
, BIGNUM
**priv2
, BIGNUM
**g_priv1
, BIGNUM
**g_priv2
,
1267 u_char
**priv1_proof
, u_int
*priv1_proof_len
,
1268 u_char
**priv2_proof
, u_int
*priv2_proof_len
)
1272 debug3("%s entering", __func__
);
1275 mm_request_send(pmonitor
->m_recvfd
,
1276 MONITOR_REQ_JPAKE_STEP1
, &m
);
1278 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__
);
1279 mm_request_receive_expect(pmonitor
->m_recvfd
,
1280 MONITOR_ANS_JPAKE_STEP1
, &m
);
1282 if ((*priv1
= BN_new()) == NULL
||
1283 (*priv2
= BN_new()) == NULL
||
1284 (*g_priv1
= BN_new()) == NULL
||
1285 (*g_priv2
= BN_new()) == NULL
)
1286 fatal("%s: BN_new", __func__
);
1288 *id
= buffer_get_string(&m
, id_len
);
1289 /* priv1 and priv2 are, well, private */
1290 buffer_get_bignum2(&m
, *g_priv1
);
1291 buffer_get_bignum2(&m
, *g_priv2
);
1292 *priv1_proof
= buffer_get_string(&m
, priv1_proof_len
);
1293 *priv2_proof
= buffer_get_string(&m
, priv2_proof_len
);
1299 mm_jpake_step2(struct modp_group
*grp
, BIGNUM
*s
,
1300 BIGNUM
*mypub1
, BIGNUM
*theirpub1
, BIGNUM
*theirpub2
, BIGNUM
*mypriv2
,
1301 const u_char
*theirid
, u_int theirid_len
,
1302 const u_char
*myid
, u_int myid_len
,
1303 const u_char
*theirpub1_proof
, u_int theirpub1_proof_len
,
1304 const u_char
*theirpub2_proof
, u_int theirpub2_proof_len
,
1306 u_char
**newpub_exponent_proof
, u_int
*newpub_exponent_proof_len
)
1310 debug3("%s entering", __func__
);
1313 /* monitor already has all bignums except theirpub1, theirpub2 */
1314 buffer_put_bignum2(&m
, theirpub1
);
1315 buffer_put_bignum2(&m
, theirpub2
);
1316 /* monitor already knows our id */
1317 buffer_put_string(&m
, theirid
, theirid_len
);
1318 buffer_put_string(&m
, theirpub1_proof
, theirpub1_proof_len
);
1319 buffer_put_string(&m
, theirpub2_proof
, theirpub2_proof_len
);
1321 mm_request_send(pmonitor
->m_recvfd
,
1322 MONITOR_REQ_JPAKE_STEP2
, &m
);
1324 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__
);
1325 mm_request_receive_expect(pmonitor
->m_recvfd
,
1326 MONITOR_ANS_JPAKE_STEP2
, &m
);
1328 if ((*newpub
= BN_new()) == NULL
)
1329 fatal("%s: BN_new", __func__
);
1331 buffer_get_bignum2(&m
, *newpub
);
1332 *newpub_exponent_proof
= buffer_get_string(&m
,
1333 newpub_exponent_proof_len
);
1339 mm_jpake_key_confirm(struct modp_group
*grp
, BIGNUM
*s
, BIGNUM
*step2_val
,
1340 BIGNUM
*mypriv2
, BIGNUM
*mypub1
, BIGNUM
*mypub2
,
1341 BIGNUM
*theirpub1
, BIGNUM
*theirpub2
,
1342 const u_char
*my_id
, u_int my_id_len
,
1343 const u_char
*their_id
, u_int their_id_len
,
1344 const u_char
*sess_id
, u_int sess_id_len
,
1345 const u_char
*theirpriv2_s_proof
, u_int theirpriv2_s_proof_len
,
1347 u_char
**confirm_hash
, u_int
*confirm_hash_len
)
1351 debug3("%s entering", __func__
);
1354 /* monitor already has all bignums except step2_val */
1355 buffer_put_bignum2(&m
, step2_val
);
1356 /* monitor already knows all the ids */
1357 buffer_put_string(&m
, theirpriv2_s_proof
, theirpriv2_s_proof_len
);
1359 mm_request_send(pmonitor
->m_recvfd
,
1360 MONITOR_REQ_JPAKE_KEY_CONFIRM
, &m
);
1362 debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__
);
1363 mm_request_receive_expect(pmonitor
->m_recvfd
,
1364 MONITOR_ANS_JPAKE_KEY_CONFIRM
, &m
);
1366 /* 'k' is sensitive and stays in the monitor */
1367 *confirm_hash
= buffer_get_string(&m
, confirm_hash_len
);
1373 mm_jpake_check_confirm(const BIGNUM
*k
,
1374 const u_char
*peer_id
, u_int peer_id_len
,
1375 const u_char
*sess_id
, u_int sess_id_len
,
1376 const u_char
*peer_confirm_hash
, u_int peer_confirm_hash_len
)
1381 debug3("%s entering", __func__
);
1384 /* k is dummy in slave, ignored */
1385 /* monitor knows all the ids */
1386 buffer_put_string(&m
, peer_confirm_hash
, peer_confirm_hash_len
);
1387 mm_request_send(pmonitor
->m_recvfd
,
1388 MONITOR_REQ_JPAKE_CHECK_CONFIRM
, &m
);
1390 debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__
);
1391 mm_request_receive_expect(pmonitor
->m_recvfd
,
1392 MONITOR_ANS_JPAKE_CHECK_CONFIRM
, &m
);
1394 success
= buffer_get_int(&m
);
1397 debug3("%s: success = %d", __func__
, success
);
1404 mm_auth_krb4(Authctxt
*authctxt
, void *_auth
, char **client
, void *_reply
)
1412 debug3("%s entering", __func__
);
1417 buffer_put_string(&m
, auth
->dat
, auth
->length
);
1419 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_KRB4
, &m
);
1420 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_KRB4
, &m
);
1422 success
= buffer_get_int(&m
);
1424 *client
= buffer_get_string(&m
, NULL
);
1425 p
= buffer_get_string(&m
, &rlen
);
1426 if (rlen
>= MAX_KTXT_LEN
)
1427 fatal("%s: reply from monitor too large", __func__
);
1428 reply
->length
= rlen
;
1429 memcpy(reply
->dat
, p
, rlen
);
1440 mm_auth_krb5(void *ctx
, void *argp
, char **userp
, void *resp
)
1442 krb5_data
*tkt
, *reply
;
1446 debug3("%s entering", __func__
);
1447 tkt
= (krb5_data
*) argp
;
1448 reply
= (krb5_data
*) resp
;
1451 buffer_put_string(&m
, tkt
->data
, tkt
->length
);
1453 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_KRB5
, &m
);
1454 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_KRB5
, &m
);
1456 success
= buffer_get_int(&m
);
1460 *userp
= buffer_get_string(&m
, NULL
);
1461 reply
->data
= buffer_get_string(&m
, &len
);
1462 reply
->length
= len
;
1464 memset(reply
, 0, sizeof(*reply
));