1 /* $OpenBSD: monitor_wrap.c,v 1.60 2007/10/29 04:08:08 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>
53 #include "auth-options.h"
57 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
60 #define TARGET_OS_MAC 1
68 #include "monitor_wrap.h"
70 #include "monitor_fdpass.h"
80 extern Newkeys
*newkeys
[];
81 extern z_stream incoming_stream
;
82 extern z_stream outgoing_stream
;
83 extern struct monitor
*pmonitor
;
84 extern Buffer input
, output
;
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 #ifdef HAVE_PW_CLASS_IN_PASSWD
235 pw
->pw_class
= buffer_get_string(&m
, NULL
);
237 pw
->pw_dir
= buffer_get_string(&m
, NULL
);
238 pw
->pw_shell
= buffer_get_string(&m
, NULL
);
241 /* copy options block as a Match directive may have changed some */
242 newopts
= buffer_get_string(&m
, &len
);
243 if (len
!= sizeof(*newopts
))
244 fatal("%s: option block size mismatch", __func__
);
245 if (newopts
->banner
!= NULL
)
246 newopts
->banner
= buffer_get_string(&m
, NULL
);
247 copy_set_server_options(&options
, newopts
, 1);
256 mm_auth2_read_banner(void)
261 debug3("%s entering", __func__
);
264 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTH2_READ_BANNER
, &m
);
267 mm_request_receive_expect(pmonitor
->m_recvfd
,
268 MONITOR_ANS_AUTH2_READ_BANNER
, &m
);
269 banner
= buffer_get_string(&m
, NULL
);
272 /* treat empty banner as missing banner */
273 if (strlen(banner
) == 0) {
280 /* Inform the privileged process about service and style */
283 mm_inform_authserv(char *service
, char *style
)
287 debug3("%s entering", __func__
);
290 buffer_put_cstring(&m
, service
);
291 buffer_put_cstring(&m
, style
? style
: "");
293 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTHSERV
, &m
);
298 /* Do the password authentication */
300 mm_auth_password(Authctxt
*authctxt
, char *password
)
303 int authenticated
= 0;
305 debug3("%s entering", __func__
);
308 buffer_put_cstring(&m
, password
);
309 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTHPASSWORD
, &m
);
311 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__
);
312 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_AUTHPASSWORD
, &m
);
314 authenticated
= buffer_get_int(&m
);
318 debug3("%s: user %sauthenticated",
319 __func__
, authenticated
? "" : "not ");
320 return (authenticated
);
324 mm_user_key_allowed(struct passwd
*pw
, Key
*key
)
326 return (mm_key_allowed(MM_USERKEY
, NULL
, NULL
, key
));
330 mm_hostbased_key_allowed(struct passwd
*pw
, char *user
, char *host
,
333 return (mm_key_allowed(MM_HOSTKEY
, user
, host
, key
));
337 mm_auth_rhosts_rsa_key_allowed(struct passwd
*pw
, char *user
,
338 char *host
, Key
*key
)
342 key
->type
= KEY_RSA
; /* XXX hack for key_to_blob */
343 ret
= mm_key_allowed(MM_RSAHOSTKEY
, user
, host
, key
);
344 key
->type
= KEY_RSA1
;
349 mm_send_debug(Buffer
*m
)
353 while (buffer_len(m
)) {
354 msg
= buffer_get_string(m
, NULL
);
355 debug3("%s: Sending debug: %s", __func__
, msg
);
356 packet_send_debug("%s", msg
);
362 mm_key_allowed(enum mm_keytype type
, char *user
, char *host
, Key
*key
)
367 int allowed
= 0, have_forced
= 0;
369 debug3("%s entering", __func__
);
371 /* Convert the key to a blob and the pass it over */
372 if (!key_to_blob(key
, &blob
, &len
))
376 buffer_put_int(&m
, type
);
377 buffer_put_cstring(&m
, user
? user
: "");
378 buffer_put_cstring(&m
, host
? host
: "");
379 buffer_put_string(&m
, blob
, len
);
382 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_KEYALLOWED
, &m
);
384 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__
);
385 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_KEYALLOWED
, &m
);
387 allowed
= buffer_get_int(&m
);
389 /* fake forced command */
390 auth_clear_options();
391 have_forced
= buffer_get_int(&m
);
392 forced_command
= have_forced
? xstrdup("true") : NULL
;
394 /* Send potential debug messages */
403 * This key verify needs to send the key type along, because the
404 * privileged parent makes the decision if the key is allowed
405 * for authentication.
409 mm_key_verify(Key
*key
, u_char
*sig
, u_int siglen
, u_char
*data
, u_int datalen
)
416 debug3("%s entering", __func__
);
418 /* Convert the key to a blob and the pass it over */
419 if (!key_to_blob(key
, &blob
, &len
))
423 buffer_put_string(&m
, blob
, len
);
424 buffer_put_string(&m
, sig
, siglen
);
425 buffer_put_string(&m
, data
, datalen
);
428 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_KEYVERIFY
, &m
);
430 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__
);
431 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_KEYVERIFY
, &m
);
433 verified
= buffer_get_int(&m
);
440 /* Export key state after authentication */
442 mm_newkeys_from_blob(u_char
*blob
, int blen
)
446 Newkeys
*newkey
= NULL
;
451 debug3("%s: %p(%d)", __func__
, blob
, blen
);
453 dump_base64(stderr
, blob
, blen
);
456 buffer_append(&b
, blob
, blen
);
458 newkey
= xmalloc(sizeof(*newkey
));
461 comp
= &newkey
->comp
;
464 enc
->name
= buffer_get_string(&b
, NULL
);
465 buffer_get(&b
, &enc
->cipher
, sizeof(enc
->cipher
));
466 enc
->enabled
= buffer_get_int(&b
);
467 enc
->block_size
= buffer_get_int(&b
);
468 enc
->key
= buffer_get_string(&b
, &enc
->key_len
);
469 enc
->iv
= buffer_get_string(&b
, &len
);
470 if (len
!= enc
->block_size
)
471 fatal("%s: bad ivlen: expected %u != %u", __func__
,
472 enc
->block_size
, len
);
474 if (enc
->name
== NULL
|| cipher_by_name(enc
->name
) != enc
->cipher
)
475 fatal("%s: bad cipher name %s or pointer %p", __func__
,
476 enc
->name
, enc
->cipher
);
479 mac
->name
= buffer_get_string(&b
, NULL
);
480 if (mac
->name
== NULL
|| mac_setup(mac
, mac
->name
) == -1)
481 fatal("%s: can not setup mac %s", __func__
, mac
->name
);
482 mac
->enabled
= buffer_get_int(&b
);
483 mac
->key
= buffer_get_string(&b
, &len
);
484 if (len
> mac
->key_len
)
485 fatal("%s: bad mac key length: %u > %d", __func__
, len
,
490 comp
->type
= buffer_get_int(&b
);
491 comp
->enabled
= buffer_get_int(&b
);
492 comp
->name
= buffer_get_string(&b
, NULL
);
494 len
= buffer_len(&b
);
496 error("newkeys_from_blob: remaining bytes in blob %u", len
);
502 mm_newkeys_to_blob(int mode
, u_char
**blobp
, u_int
*lenp
)
509 Newkeys
*newkey
= newkeys
[mode
];
511 debug3("%s: converting %p", __func__
, newkey
);
513 if (newkey
== NULL
) {
514 error("%s: newkey == NULL", __func__
);
519 comp
= &newkey
->comp
;
523 buffer_put_cstring(&b
, enc
->name
);
524 /* The cipher struct is constant and shared, you export pointer */
525 buffer_append(&b
, &enc
->cipher
, sizeof(enc
->cipher
));
526 buffer_put_int(&b
, enc
->enabled
);
527 buffer_put_int(&b
, enc
->block_size
);
528 buffer_put_string(&b
, enc
->key
, enc
->key_len
);
529 packet_get_keyiv(mode
, enc
->iv
, enc
->block_size
);
530 buffer_put_string(&b
, enc
->iv
, enc
->block_size
);
533 buffer_put_cstring(&b
, mac
->name
);
534 buffer_put_int(&b
, mac
->enabled
);
535 buffer_put_string(&b
, mac
->key
, mac
->key_len
);
538 buffer_put_int(&b
, comp
->type
);
539 buffer_put_int(&b
, comp
->enabled
);
540 buffer_put_cstring(&b
, comp
->name
);
542 len
= buffer_len(&b
);
546 *blobp
= xmalloc(len
);
547 memcpy(*blobp
, buffer_ptr(&b
), len
);
549 memset(buffer_ptr(&b
), 0, len
);
555 mm_send_kex(Buffer
*m
, Kex
*kex
)
557 buffer_put_string(m
, kex
->session_id
, kex
->session_id_len
);
558 buffer_put_int(m
, kex
->we_need
);
559 buffer_put_int(m
, kex
->hostkey_type
);
560 buffer_put_int(m
, kex
->kex_type
);
561 buffer_put_string(m
, buffer_ptr(&kex
->my
), buffer_len(&kex
->my
));
562 buffer_put_string(m
, buffer_ptr(&kex
->peer
), buffer_len(&kex
->peer
));
563 buffer_put_int(m
, kex
->flags
);
564 buffer_put_cstring(m
, kex
->client_version_string
);
565 buffer_put_cstring(m
, kex
->server_version_string
);
569 mm_send_keystate(struct monitor
*monitor
)
574 u_int32_t seqnr
, packets
;
584 buffer_put_int(&m
, packet_get_protocol_flags());
586 buffer_put_int(&m
, packet_get_ssh1_cipher());
588 debug3("%s: Sending ssh1 KEY+IV", __func__
);
589 keylen
= packet_get_encryption_key(NULL
);
590 key
= xmalloc(keylen
+1); /* add 1 if keylen == 0 */
591 keylen
= packet_get_encryption_key(key
);
592 buffer_put_string(&m
, key
, keylen
);
593 memset(key
, 0, keylen
);
596 ivlen
= packet_get_keyiv_len(MODE_OUT
);
597 packet_get_keyiv(MODE_OUT
, iv
, ivlen
);
598 buffer_put_string(&m
, iv
, ivlen
);
599 ivlen
= packet_get_keyiv_len(MODE_OUT
);
600 packet_get_keyiv(MODE_IN
, iv
, ivlen
);
601 buffer_put_string(&m
, iv
, ivlen
);
604 /* Kex for rekeying */
605 mm_send_kex(&m
, *monitor
->m_pkex
);
608 debug3("%s: Sending new keys: %p %p",
609 __func__
, newkeys
[MODE_OUT
], newkeys
[MODE_IN
]);
612 if (!mm_newkeys_to_blob(MODE_OUT
, &blob
, &bloblen
))
613 fatal("%s: conversion of newkeys failed", __func__
);
615 buffer_put_string(&m
, blob
, bloblen
);
618 if (!mm_newkeys_to_blob(MODE_IN
, &blob
, &bloblen
))
619 fatal("%s: conversion of newkeys failed", __func__
);
621 buffer_put_string(&m
, blob
, bloblen
);
624 packet_get_state(MODE_OUT
, &seqnr
, &blocks
, &packets
);
625 buffer_put_int(&m
, seqnr
);
626 buffer_put_int64(&m
, blocks
);
627 buffer_put_int(&m
, packets
);
628 packet_get_state(MODE_IN
, &seqnr
, &blocks
, &packets
);
629 buffer_put_int(&m
, seqnr
);
630 buffer_put_int64(&m
, blocks
);
631 buffer_put_int(&m
, packets
);
633 debug3("%s: New keys have been sent", __func__
);
635 /* More key context */
636 plen
= packet_get_keycontext(MODE_OUT
, NULL
);
638 packet_get_keycontext(MODE_OUT
, p
);
639 buffer_put_string(&m
, p
, plen
);
642 plen
= packet_get_keycontext(MODE_IN
, NULL
);
644 packet_get_keycontext(MODE_IN
, p
);
645 buffer_put_string(&m
, p
, plen
);
648 /* Compression state */
649 debug3("%s: Sending compression state", __func__
);
650 buffer_put_string(&m
, &outgoing_stream
, sizeof(outgoing_stream
));
651 buffer_put_string(&m
, &incoming_stream
, sizeof(incoming_stream
));
653 /* Network I/O buffers */
654 buffer_put_string(&m
, buffer_ptr(&input
), buffer_len(&input
));
655 buffer_put_string(&m
, buffer_ptr(&output
), buffer_len(&output
));
657 mm_request_send(monitor
->m_recvfd
, MONITOR_REQ_KEYEXPORT
, &m
);
658 debug3("%s: Finished sending state", __func__
);
664 mm_pty_allocate(int *ptyfd
, int *ttyfd
, char *namebuf
, size_t namebuflen
)
671 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PTY
, &m
);
673 debug3("%s: waiting for MONITOR_ANS_PTY", __func__
);
674 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PTY
, &m
);
676 success
= buffer_get_int(&m
);
678 debug3("%s: pty alloc failed", __func__
);
682 p
= buffer_get_string(&m
, NULL
);
683 msg
= buffer_get_string(&m
, NULL
);
686 strlcpy(namebuf
, p
, namebuflen
); /* Possible truncation */
689 buffer_append(&loginmsg
, msg
, strlen(msg
));
692 if ((*ptyfd
= mm_receive_fd(pmonitor
->m_recvfd
)) == -1 ||
693 (*ttyfd
= mm_receive_fd(pmonitor
->m_recvfd
)) == -1)
694 fatal("%s: receive fds failed", __func__
);
701 mm_session_pty_cleanup2(Session
*s
)
708 buffer_put_cstring(&m
, s
->tty
);
709 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PTYCLEANUP
, &m
);
712 /* closed dup'ed master */
713 if (close(s
->ptymaster
) < 0)
714 error("close(s->ptymaster): %s", strerror(errno
));
716 /* unlink pty from session */
722 mm_start_pam(Authctxt
*authctxt
)
726 debug3("%s entering", __func__
);
727 if (!options
.use_pam
)
728 fatal("UsePAM=no, but ended up in %s anyway", __func__
);
731 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_START
, &m
);
737 mm_do_pam_account(void)
743 debug3("%s entering", __func__
);
744 if (!options
.use_pam
)
745 fatal("UsePAM=no, but ended up in %s anyway", __func__
);
748 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_ACCOUNT
, &m
);
750 mm_request_receive_expect(pmonitor
->m_recvfd
,
751 MONITOR_ANS_PAM_ACCOUNT
, &m
);
752 ret
= buffer_get_int(&m
);
753 msg
= buffer_get_string(&m
, NULL
);
754 buffer_append(&loginmsg
, msg
, strlen(msg
));
759 debug3("%s returning %d", __func__
, ret
);
765 mm_sshpam_init_ctx(Authctxt
*authctxt
)
770 debug3("%s", __func__
);
772 buffer_put_cstring(&m
, authctxt
->user
);
773 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_INIT_CTX
, &m
);
774 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__
);
775 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_INIT_CTX
, &m
);
776 success
= buffer_get_int(&m
);
778 debug3("%s: pam_init_ctx failed", __func__
);
787 mm_sshpam_query(void *ctx
, char **name
, char **info
,
788 u_int
*num
, char ***prompts
, u_int
**echo_on
)
794 debug3("%s", __func__
);
796 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_QUERY
, &m
);
797 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__
);
798 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_QUERY
, &m
);
799 ret
= buffer_get_int(&m
);
800 debug3("%s: pam_query returned %d", __func__
, ret
);
801 *name
= buffer_get_string(&m
, NULL
);
802 *info
= buffer_get_string(&m
, NULL
);
803 *num
= buffer_get_int(&m
);
804 if (*num
> PAM_MAX_NUM_MSG
)
805 fatal("%s: recieved %u PAM messages, expected <= %u",
806 __func__
, *num
, PAM_MAX_NUM_MSG
);
807 *prompts
= xcalloc((*num
+ 1), sizeof(char *));
808 *echo_on
= xcalloc((*num
+ 1), sizeof(u_int
));
809 for (i
= 0; i
< *num
; ++i
) {
810 (*prompts
)[i
] = buffer_get_string(&m
, NULL
);
811 (*echo_on
)[i
] = buffer_get_int(&m
);
818 mm_sshpam_respond(void *ctx
, u_int num
, char **resp
)
824 debug3("%s", __func__
);
826 buffer_put_int(&m
, num
);
827 for (i
= 0; i
< num
; ++i
)
828 buffer_put_cstring(&m
, resp
[i
]);
829 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_RESPOND
, &m
);
830 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__
);
831 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_RESPOND
, &m
);
832 ret
= buffer_get_int(&m
);
833 debug3("%s: pam_respond returned %d", __func__
, ret
);
839 mm_sshpam_free_ctx(void *ctxtp
)
843 debug3("%s", __func__
);
845 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_FREE_CTX
, &m
);
846 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__
);
847 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_FREE_CTX
, &m
);
852 /* Request process termination */
860 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_TERM
, &m
);
865 mm_ssh1_session_key(BIGNUM
*num
)
871 buffer_put_bignum2(&m
, num
);
872 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SESSKEY
, &m
);
874 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SESSKEY
, &m
);
876 rsafail
= buffer_get_int(&m
);
877 buffer_get_bignum2(&m
, num
);
885 mm_chall_setup(char **name
, char **infotxt
, u_int
*numprompts
,
886 char ***prompts
, u_int
**echo_on
)
889 *infotxt
= xstrdup("");
891 *prompts
= xcalloc(*numprompts
, sizeof(char *));
892 *echo_on
= xcalloc(*numprompts
, sizeof(u_int
));
897 mm_bsdauth_query(void *ctx
, char **name
, char **infotxt
,
898 u_int
*numprompts
, char ***prompts
, u_int
**echo_on
)
904 debug3("%s: entering", __func__
);
907 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_BSDAUTHQUERY
, &m
);
909 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_BSDAUTHQUERY
,
911 success
= buffer_get_int(&m
);
913 debug3("%s: no challenge", __func__
);
918 /* Get the challenge, and format the response */
919 challenge
= buffer_get_string(&m
, NULL
);
922 mm_chall_setup(name
, infotxt
, numprompts
, prompts
, echo_on
);
923 (*prompts
)[0] = challenge
;
925 debug3("%s: received challenge: %s", __func__
, challenge
);
931 mm_bsdauth_respond(void *ctx
, u_int numresponses
, char **responses
)
936 debug3("%s: entering", __func__
);
937 if (numresponses
!= 1)
941 buffer_put_cstring(&m
, responses
[0]);
942 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_BSDAUTHRESPOND
, &m
);
944 mm_request_receive_expect(pmonitor
->m_recvfd
,
945 MONITOR_ANS_BSDAUTHRESPOND
, &m
);
947 authok
= buffer_get_int(&m
);
950 return ((authok
== 0) ? -1 : 0);
955 mm_skey_query(void *ctx
, char **name
, char **infotxt
,
956 u_int
*numprompts
, char ***prompts
, u_int
**echo_on
)
962 debug3("%s: entering", __func__
);
965 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SKEYQUERY
, &m
);
967 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SKEYQUERY
,
969 success
= buffer_get_int(&m
);
971 debug3("%s: no challenge", __func__
);
976 /* Get the challenge, and format the response */
977 challenge
= buffer_get_string(&m
, NULL
);
980 debug3("%s: received challenge: %s", __func__
, challenge
);
982 mm_chall_setup(name
, infotxt
, numprompts
, prompts
, echo_on
);
984 xasprintf(*prompts
, "%s%s", challenge
, SKEY_PROMPT
);
991 mm_skey_respond(void *ctx
, u_int numresponses
, char **responses
)
996 debug3("%s: entering", __func__
);
997 if (numresponses
!= 1)
1001 buffer_put_cstring(&m
, responses
[0]);
1002 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SKEYRESPOND
, &m
);
1004 mm_request_receive_expect(pmonitor
->m_recvfd
,
1005 MONITOR_ANS_SKEYRESPOND
, &m
);
1007 authok
= buffer_get_int(&m
);
1010 return ((authok
== 0) ? -1 : 0);
1015 mm_ssh1_session_id(u_char session_id
[16])
1020 debug3("%s entering", __func__
);
1023 for (i
= 0; i
< 16; i
++)
1024 buffer_put_char(&m
, session_id
[i
]);
1026 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SESSID
, &m
);
1031 mm_auth_rsa_key_allowed(struct passwd
*pw
, BIGNUM
*client_n
, Key
**rkey
)
1037 int allowed
= 0, have_forced
= 0;
1039 debug3("%s entering", __func__
);
1042 buffer_put_bignum2(&m
, client_n
);
1044 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSAKEYALLOWED
, &m
);
1045 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSAKEYALLOWED
, &m
);
1047 allowed
= buffer_get_int(&m
);
1049 /* fake forced command */
1050 auth_clear_options();
1051 have_forced
= buffer_get_int(&m
);
1052 forced_command
= have_forced
? xstrdup("true") : NULL
;
1054 if (allowed
&& rkey
!= NULL
) {
1055 blob
= buffer_get_string(&m
, &blen
);
1056 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1057 fatal("%s: key_from_blob failed", __func__
);
1068 mm_auth_rsa_generate_challenge(Key
*key
)
1075 debug3("%s entering", __func__
);
1077 if ((challenge
= BN_new()) == NULL
)
1078 fatal("%s: BN_new failed", __func__
);
1080 key
->type
= KEY_RSA
; /* XXX cheat for key_to_blob */
1081 if (key_to_blob(key
, &blob
, &blen
) == 0)
1082 fatal("%s: key_to_blob failed", __func__
);
1083 key
->type
= KEY_RSA1
;
1086 buffer_put_string(&m
, blob
, blen
);
1089 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSACHALLENGE
, &m
);
1090 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSACHALLENGE
, &m
);
1092 buffer_get_bignum2(&m
, challenge
);
1099 mm_auth_rsa_verify_response(Key
*key
, BIGNUM
*p
, u_char response
[16])
1106 debug3("%s entering", __func__
);
1108 key
->type
= KEY_RSA
; /* XXX cheat for key_to_blob */
1109 if (key_to_blob(key
, &blob
, &blen
) == 0)
1110 fatal("%s: key_to_blob failed", __func__
);
1111 key
->type
= KEY_RSA1
;
1114 buffer_put_string(&m
, blob
, blen
);
1115 buffer_put_string(&m
, response
, 16);
1118 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSARESPONSE
, &m
);
1119 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSARESPONSE
, &m
);
1121 success
= buffer_get_int(&m
);
1127 #ifdef SSH_AUDIT_EVENTS
1129 mm_audit_event(ssh_audit_event_t event
)
1133 debug3("%s entering", __func__
);
1136 buffer_put_int(&m
, event
);
1138 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUDIT_EVENT
, &m
);
1143 mm_audit_run_command(const char *command
)
1147 debug3("%s entering command %s", __func__
, command
);
1150 buffer_put_cstring(&m
, command
);
1152 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUDIT_COMMAND
, &m
);
1155 #endif /* SSH_AUDIT_EVENTS */
1159 mm_ssh_gssapi_server_ctx(Gssctxt
**ctx
, gss_OID goid
)
1164 /* Client doesn't get to see the context */
1168 buffer_put_string(&m
, goid
->elements
, goid
->length
);
1170 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSSETUP
, &m
);
1171 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSSETUP
, &m
);
1173 major
= buffer_get_int(&m
);
1180 mm_ssh_gssapi_accept_ctx(Gssctxt
*ctx
, gss_buffer_desc
*in
,
1181 gss_buffer_desc
*out
, OM_uint32
*flags
)
1188 buffer_put_string(&m
, in
->value
, in
->length
);
1190 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSSTEP
, &m
);
1191 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSSTEP
, &m
);
1193 major
= buffer_get_int(&m
);
1194 out
->value
= buffer_get_string(&m
, &len
);
1197 *flags
= buffer_get_int(&m
);
1205 mm_ssh_gssapi_checkmic(Gssctxt
*ctx
, gss_buffer_t gssbuf
, gss_buffer_t gssmic
)
1211 buffer_put_string(&m
, gssbuf
->value
, gssbuf
->length
);
1212 buffer_put_string(&m
, gssmic
->value
, gssmic
->length
);
1214 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSCHECKMIC
, &m
);
1215 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSCHECKMIC
,
1218 major
= buffer_get_int(&m
);
1224 mm_ssh_gssapi_userok(char *user
)
1227 int authenticated
= 0;
1231 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSUSEROK
, &m
);
1232 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSUSEROK
,
1235 authenticated
= buffer_get_int(&m
);
1238 debug3("%s: user %sauthenticated",__func__
, authenticated
? "" : "not ");
1239 return (authenticated
);