1 /* $OpenBSD: monitor_wrap.c,v 1.54 2006/08/12 20:46:46 miod 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"
79 extern Newkeys
*newkeys
[];
80 extern z_stream incoming_stream
;
81 extern z_stream outgoing_stream
;
82 extern struct monitor
*pmonitor
;
83 extern Buffer input
, output
;
84 extern Buffer loginmsg
;
85 extern ServerOptions options
;
91 * m_pid is only set in the privileged part, and
92 * points to the unprivileged child.
94 return (pmonitor
&& pmonitor
->m_pid
> 0);
98 mm_request_send(int sock
, enum monitor_reqtype type
, Buffer
*m
)
100 u_int mlen
= buffer_len(m
);
103 debug3("%s entering: type %d", __func__
, type
);
105 put_u32(buf
, mlen
+ 1);
106 buf
[4] = (u_char
) type
; /* 1st byte of payload is mesg-type */
107 if (atomicio(vwrite
, sock
, buf
, sizeof(buf
)) != sizeof(buf
))
108 fatal("%s: write: %s", __func__
, strerror(errno
));
109 if (atomicio(vwrite
, sock
, buffer_ptr(m
), mlen
) != mlen
)
110 fatal("%s: write: %s", __func__
, strerror(errno
));
114 mm_request_receive(int sock
, Buffer
*m
)
119 debug3("%s entering", __func__
);
121 if (atomicio(read
, sock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
124 fatal("%s: read: %s", __func__
, strerror(errno
));
126 msg_len
= get_u32(buf
);
127 if (msg_len
> 256 * 1024)
128 fatal("%s: read: bad msg_len %d", __func__
, msg_len
);
130 buffer_append_space(m
, msg_len
);
131 if (atomicio(read
, sock
, buffer_ptr(m
), msg_len
) != msg_len
)
132 fatal("%s: read: %s", __func__
, strerror(errno
));
136 mm_request_receive_expect(int sock
, enum monitor_reqtype type
, Buffer
*m
)
140 debug3("%s entering: type %d", __func__
, type
);
142 mm_request_receive(sock
, m
);
143 rtype
= buffer_get_char(m
);
145 fatal("%s: read: rtype %d != type %d", __func__
,
150 mm_choose_dh(int min
, int nbits
, int max
)
157 buffer_put_int(&m
, min
);
158 buffer_put_int(&m
, nbits
);
159 buffer_put_int(&m
, max
);
161 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_MODULI
, &m
);
163 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__
);
164 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_MODULI
, &m
);
166 success
= buffer_get_char(&m
);
168 fatal("%s: MONITOR_ANS_MODULI failed", __func__
);
170 if ((p
= BN_new()) == NULL
)
171 fatal("%s: BN_new failed", __func__
);
172 if ((g
= BN_new()) == NULL
)
173 fatal("%s: BN_new failed", __func__
);
174 buffer_get_bignum2(&m
, p
);
175 buffer_get_bignum2(&m
, g
);
177 debug3("%s: remaining %d", __func__
, buffer_len(&m
));
180 return (dh_new_group(g
, p
));
184 mm_key_sign(Key
*key
, u_char
**sigp
, u_int
*lenp
, u_char
*data
, u_int datalen
)
186 Kex
*kex
= *pmonitor
->m_pkex
;
189 debug3("%s entering", __func__
);
192 buffer_put_int(&m
, kex
->host_key_index(key
));
193 buffer_put_string(&m
, data
, datalen
);
195 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SIGN
, &m
);
197 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__
);
198 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SIGN
, &m
);
199 *sigp
= buffer_get_string(&m
, lenp
);
206 mm_getpwnamallow(const char *username
)
212 debug3("%s entering", __func__
);
215 buffer_put_cstring(&m
, username
);
217 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PWNAM
, &m
);
219 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__
);
220 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PWNAM
, &m
);
222 if (buffer_get_char(&m
) == 0) {
226 pw
= buffer_get_string(&m
, &pwlen
);
227 if (pwlen
!= sizeof(struct passwd
))
228 fatal("%s: struct passwd size mismatch", __func__
);
229 pw
->pw_name
= buffer_get_string(&m
, NULL
);
230 pw
->pw_passwd
= buffer_get_string(&m
, NULL
);
231 pw
->pw_gecos
= buffer_get_string(&m
, NULL
);
232 #ifdef HAVE_PW_CLASS_IN_PASSWD
233 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
);
243 mm_auth2_read_banner(void)
248 debug3("%s entering", __func__
);
251 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTH2_READ_BANNER
, &m
);
254 mm_request_receive_expect(pmonitor
->m_recvfd
,
255 MONITOR_ANS_AUTH2_READ_BANNER
, &m
);
256 banner
= buffer_get_string(&m
, NULL
);
259 /* treat empty banner as missing banner */
260 if (strlen(banner
) == 0) {
267 /* Inform the privileged process about service and style */
270 mm_inform_authserv(char *service
, char *style
)
274 debug3("%s entering", __func__
);
277 buffer_put_cstring(&m
, service
);
278 buffer_put_cstring(&m
, style
? style
: "");
280 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTHSERV
, &m
);
285 /* Do the password authentication */
287 mm_auth_password(Authctxt
*authctxt
, char *password
)
290 int authenticated
= 0;
292 debug3("%s entering", __func__
);
295 buffer_put_cstring(&m
, password
);
296 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTHPASSWORD
, &m
);
298 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__
);
299 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_AUTHPASSWORD
, &m
);
301 authenticated
= buffer_get_int(&m
);
305 debug3("%s: user %sauthenticated",
306 __func__
, authenticated
? "" : "not ");
307 return (authenticated
);
311 mm_user_key_allowed(struct passwd
*pw
, Key
*key
)
313 return (mm_key_allowed(MM_USERKEY
, NULL
, NULL
, key
));
317 mm_hostbased_key_allowed(struct passwd
*pw
, char *user
, char *host
,
320 return (mm_key_allowed(MM_HOSTKEY
, user
, host
, key
));
324 mm_auth_rhosts_rsa_key_allowed(struct passwd
*pw
, char *user
,
325 char *host
, Key
*key
)
329 key
->type
= KEY_RSA
; /* XXX hack for key_to_blob */
330 ret
= mm_key_allowed(MM_RSAHOSTKEY
, user
, host
, key
);
331 key
->type
= KEY_RSA1
;
336 mm_send_debug(Buffer
*m
)
340 while (buffer_len(m
)) {
341 msg
= buffer_get_string(m
, NULL
);
342 debug3("%s: Sending debug: %s", __func__
, msg
);
343 packet_send_debug("%s", msg
);
349 mm_key_allowed(enum mm_keytype type
, char *user
, char *host
, Key
*key
)
354 int allowed
= 0, have_forced
= 0;
356 debug3("%s entering", __func__
);
358 /* Convert the key to a blob and the pass it over */
359 if (!key_to_blob(key
, &blob
, &len
))
363 buffer_put_int(&m
, type
);
364 buffer_put_cstring(&m
, user
? user
: "");
365 buffer_put_cstring(&m
, host
? host
: "");
366 buffer_put_string(&m
, blob
, len
);
369 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_KEYALLOWED
, &m
);
371 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__
);
372 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_KEYALLOWED
, &m
);
374 allowed
= buffer_get_int(&m
);
376 /* fake forced command */
377 auth_clear_options();
378 have_forced
= buffer_get_int(&m
);
379 forced_command
= have_forced
? xstrdup("true") : NULL
;
381 /* Send potential debug messages */
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_init(mac
, mac
->name
) == -1)
468 fatal("%s: can not init 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
[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
)
561 u_int32_t seqnr
, packets
;
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__
, newkeys
[MODE_OUT
], 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
);
612 buffer_put_int(&m
, seqnr
);
613 buffer_put_int64(&m
, blocks
);
614 buffer_put_int(&m
, packets
);
615 packet_get_state(MODE_IN
, &seqnr
, &blocks
, &packets
);
616 buffer_put_int(&m
, seqnr
);
617 buffer_put_int64(&m
, blocks
);
618 buffer_put_int(&m
, packets
);
620 debug3("%s: New keys have been sent", __func__
);
622 /* More key context */
623 plen
= packet_get_keycontext(MODE_OUT
, NULL
);
625 packet_get_keycontext(MODE_OUT
, p
);
626 buffer_put_string(&m
, p
, plen
);
629 plen
= packet_get_keycontext(MODE_IN
, NULL
);
631 packet_get_keycontext(MODE_IN
, p
);
632 buffer_put_string(&m
, p
, plen
);
635 /* Compression state */
636 debug3("%s: Sending compression state", __func__
);
637 buffer_put_string(&m
, &outgoing_stream
, sizeof(outgoing_stream
));
638 buffer_put_string(&m
, &incoming_stream
, sizeof(incoming_stream
));
640 /* Network I/O buffers */
641 buffer_put_string(&m
, buffer_ptr(&input
), buffer_len(&input
));
642 buffer_put_string(&m
, buffer_ptr(&output
), buffer_len(&output
));
644 mm_request_send(monitor
->m_recvfd
, MONITOR_REQ_KEYEXPORT
, &m
);
645 debug3("%s: Finished sending state", __func__
);
651 mm_pty_allocate(int *ptyfd
, int *ttyfd
, char *namebuf
, size_t namebuflen
)
658 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PTY
, &m
);
660 debug3("%s: waiting for MONITOR_ANS_PTY", __func__
);
661 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PTY
, &m
);
663 success
= buffer_get_int(&m
);
665 debug3("%s: pty alloc failed", __func__
);
669 p
= buffer_get_string(&m
, NULL
);
670 msg
= buffer_get_string(&m
, NULL
);
673 strlcpy(namebuf
, p
, namebuflen
); /* Possible truncation */
676 buffer_append(&loginmsg
, msg
, strlen(msg
));
679 *ptyfd
= mm_receive_fd(pmonitor
->m_recvfd
);
680 *ttyfd
= mm_receive_fd(pmonitor
->m_recvfd
);
687 mm_session_pty_cleanup2(Session
*s
)
694 buffer_put_cstring(&m
, s
->tty
);
695 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PTYCLEANUP
, &m
);
698 /* closed dup'ed master */
699 if (close(s
->ptymaster
) < 0)
700 error("close(s->ptymaster): %s", strerror(errno
));
702 /* unlink pty from session */
708 mm_start_pam(Authctxt
*authctxt
)
712 debug3("%s entering", __func__
);
713 if (!options
.use_pam
)
714 fatal("UsePAM=no, but ended up in %s anyway", __func__
);
717 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_START
, &m
);
723 mm_do_pam_account(void)
729 debug3("%s entering", __func__
);
730 if (!options
.use_pam
)
731 fatal("UsePAM=no, but ended up in %s anyway", __func__
);
734 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_ACCOUNT
, &m
);
736 mm_request_receive_expect(pmonitor
->m_recvfd
,
737 MONITOR_ANS_PAM_ACCOUNT
, &m
);
738 ret
= buffer_get_int(&m
);
739 msg
= buffer_get_string(&m
, NULL
);
740 buffer_append(&loginmsg
, msg
, strlen(msg
));
745 debug3("%s returning %d", __func__
, ret
);
751 mm_sshpam_init_ctx(Authctxt
*authctxt
)
756 debug3("%s", __func__
);
758 buffer_put_cstring(&m
, authctxt
->user
);
759 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_INIT_CTX
, &m
);
760 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__
);
761 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_INIT_CTX
, &m
);
762 success
= buffer_get_int(&m
);
764 debug3("%s: pam_init_ctx failed", __func__
);
773 mm_sshpam_query(void *ctx
, char **name
, char **info
,
774 u_int
*num
, char ***prompts
, u_int
**echo_on
)
780 debug3("%s", __func__
);
782 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_QUERY
, &m
);
783 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__
);
784 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_QUERY
, &m
);
785 ret
= buffer_get_int(&m
);
786 debug3("%s: pam_query returned %d", __func__
, ret
);
787 *name
= buffer_get_string(&m
, NULL
);
788 *info
= buffer_get_string(&m
, NULL
);
789 *num
= buffer_get_int(&m
);
790 if (*num
> PAM_MAX_NUM_MSG
)
791 fatal("%s: recieved %u PAM messages, expected <= %u",
792 __func__
, *num
, PAM_MAX_NUM_MSG
);
793 *prompts
= xcalloc((*num
+ 1), sizeof(char *));
794 *echo_on
= xcalloc((*num
+ 1), sizeof(u_int
));
795 for (i
= 0; i
< *num
; ++i
) {
796 (*prompts
)[i
] = buffer_get_string(&m
, NULL
);
797 (*echo_on
)[i
] = buffer_get_int(&m
);
804 mm_sshpam_respond(void *ctx
, u_int num
, char **resp
)
810 debug3("%s", __func__
);
812 buffer_put_int(&m
, num
);
813 for (i
= 0; i
< num
; ++i
)
814 buffer_put_cstring(&m
, resp
[i
]);
815 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_RESPOND
, &m
);
816 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__
);
817 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_RESPOND
, &m
);
818 ret
= buffer_get_int(&m
);
819 debug3("%s: pam_respond returned %d", __func__
, ret
);
825 mm_sshpam_free_ctx(void *ctxtp
)
829 debug3("%s", __func__
);
831 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_FREE_CTX
, &m
);
832 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__
);
833 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_FREE_CTX
, &m
);
838 /* Request process termination */
846 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_TERM
, &m
);
851 mm_ssh1_session_key(BIGNUM
*num
)
857 buffer_put_bignum2(&m
, num
);
858 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SESSKEY
, &m
);
860 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SESSKEY
, &m
);
862 rsafail
= buffer_get_int(&m
);
863 buffer_get_bignum2(&m
, num
);
871 mm_chall_setup(char **name
, char **infotxt
, u_int
*numprompts
,
872 char ***prompts
, u_int
**echo_on
)
875 *infotxt
= xstrdup("");
877 *prompts
= xcalloc(*numprompts
, sizeof(char *));
878 *echo_on
= xcalloc(*numprompts
, sizeof(u_int
));
883 mm_bsdauth_query(void *ctx
, char **name
, char **infotxt
,
884 u_int
*numprompts
, char ***prompts
, u_int
**echo_on
)
890 debug3("%s: entering", __func__
);
893 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_BSDAUTHQUERY
, &m
);
895 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_BSDAUTHQUERY
,
897 success
= buffer_get_int(&m
);
899 debug3("%s: no challenge", __func__
);
904 /* Get the challenge, and format the response */
905 challenge
= buffer_get_string(&m
, NULL
);
908 mm_chall_setup(name
, infotxt
, numprompts
, prompts
, echo_on
);
909 (*prompts
)[0] = challenge
;
911 debug3("%s: received challenge: %s", __func__
, challenge
);
917 mm_bsdauth_respond(void *ctx
, u_int numresponses
, char **responses
)
922 debug3("%s: entering", __func__
);
923 if (numresponses
!= 1)
927 buffer_put_cstring(&m
, responses
[0]);
928 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_BSDAUTHRESPOND
, &m
);
930 mm_request_receive_expect(pmonitor
->m_recvfd
,
931 MONITOR_ANS_BSDAUTHRESPOND
, &m
);
933 authok
= buffer_get_int(&m
);
936 return ((authok
== 0) ? -1 : 0);
941 mm_skey_query(void *ctx
, char **name
, char **infotxt
,
942 u_int
*numprompts
, char ***prompts
, u_int
**echo_on
)
948 debug3("%s: entering", __func__
);
951 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SKEYQUERY
, &m
);
953 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SKEYQUERY
,
955 success
= buffer_get_int(&m
);
957 debug3("%s: no challenge", __func__
);
962 /* Get the challenge, and format the response */
963 challenge
= buffer_get_string(&m
, NULL
);
966 debug3("%s: received challenge: %s", __func__
, challenge
);
968 mm_chall_setup(name
, infotxt
, numprompts
, prompts
, echo_on
);
970 xasprintf(*prompts
, "%s%s", challenge
, SKEY_PROMPT
);
977 mm_skey_respond(void *ctx
, u_int numresponses
, char **responses
)
982 debug3("%s: entering", __func__
);
983 if (numresponses
!= 1)
987 buffer_put_cstring(&m
, responses
[0]);
988 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SKEYRESPOND
, &m
);
990 mm_request_receive_expect(pmonitor
->m_recvfd
,
991 MONITOR_ANS_SKEYRESPOND
, &m
);
993 authok
= buffer_get_int(&m
);
996 return ((authok
== 0) ? -1 : 0);
1001 mm_ssh1_session_id(u_char session_id
[16])
1006 debug3("%s entering", __func__
);
1009 for (i
= 0; i
< 16; i
++)
1010 buffer_put_char(&m
, session_id
[i
]);
1012 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SESSID
, &m
);
1017 mm_auth_rsa_key_allowed(struct passwd
*pw
, BIGNUM
*client_n
, Key
**rkey
)
1023 int allowed
= 0, have_forced
= 0;
1025 debug3("%s entering", __func__
);
1028 buffer_put_bignum2(&m
, client_n
);
1030 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSAKEYALLOWED
, &m
);
1031 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSAKEYALLOWED
, &m
);
1033 allowed
= buffer_get_int(&m
);
1035 /* fake forced command */
1036 auth_clear_options();
1037 have_forced
= buffer_get_int(&m
);
1038 forced_command
= have_forced
? xstrdup("true") : NULL
;
1040 if (allowed
&& rkey
!= NULL
) {
1041 blob
= buffer_get_string(&m
, &blen
);
1042 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1043 fatal("%s: key_from_blob failed", __func__
);
1054 mm_auth_rsa_generate_challenge(Key
*key
)
1061 debug3("%s entering", __func__
);
1063 if ((challenge
= BN_new()) == NULL
)
1064 fatal("%s: BN_new failed", __func__
);
1066 key
->type
= KEY_RSA
; /* XXX cheat for key_to_blob */
1067 if (key_to_blob(key
, &blob
, &blen
) == 0)
1068 fatal("%s: key_to_blob failed", __func__
);
1069 key
->type
= KEY_RSA1
;
1072 buffer_put_string(&m
, blob
, blen
);
1075 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSACHALLENGE
, &m
);
1076 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSACHALLENGE
, &m
);
1078 buffer_get_bignum2(&m
, challenge
);
1085 mm_auth_rsa_verify_response(Key
*key
, BIGNUM
*p
, u_char response
[16])
1092 debug3("%s entering", __func__
);
1094 key
->type
= KEY_RSA
; /* XXX cheat for key_to_blob */
1095 if (key_to_blob(key
, &blob
, &blen
) == 0)
1096 fatal("%s: key_to_blob failed", __func__
);
1097 key
->type
= KEY_RSA1
;
1100 buffer_put_string(&m
, blob
, blen
);
1101 buffer_put_string(&m
, response
, 16);
1104 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSARESPONSE
, &m
);
1105 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSARESPONSE
, &m
);
1107 success
= buffer_get_int(&m
);
1113 #ifdef SSH_AUDIT_EVENTS
1115 mm_audit_event(ssh_audit_event_t event
)
1119 debug3("%s entering", __func__
);
1122 buffer_put_int(&m
, event
);
1124 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUDIT_EVENT
, &m
);
1129 mm_audit_run_command(const char *command
)
1133 debug3("%s entering command %s", __func__
, command
);
1136 buffer_put_cstring(&m
, command
);
1138 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUDIT_COMMAND
, &m
);
1141 #endif /* SSH_AUDIT_EVENTS */
1145 mm_ssh_gssapi_server_ctx(Gssctxt
**ctx
, gss_OID goid
)
1150 /* Client doesn't get to see the context */
1154 buffer_put_string(&m
, goid
->elements
, goid
->length
);
1156 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSSETUP
, &m
);
1157 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSSETUP
, &m
);
1159 major
= buffer_get_int(&m
);
1166 mm_ssh_gssapi_accept_ctx(Gssctxt
*ctx
, gss_buffer_desc
*in
,
1167 gss_buffer_desc
*out
, OM_uint32
*flags
)
1174 buffer_put_string(&m
, in
->value
, in
->length
);
1176 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSSTEP
, &m
);
1177 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSSTEP
, &m
);
1179 major
= buffer_get_int(&m
);
1180 out
->value
= buffer_get_string(&m
, &len
);
1183 *flags
= buffer_get_int(&m
);
1191 mm_ssh_gssapi_checkmic(Gssctxt
*ctx
, gss_buffer_t gssbuf
, gss_buffer_t gssmic
)
1197 buffer_put_string(&m
, gssbuf
->value
, gssbuf
->length
);
1198 buffer_put_string(&m
, gssmic
->value
, gssmic
->length
);
1200 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSCHECKMIC
, &m
);
1201 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSCHECKMIC
,
1204 major
= buffer_get_int(&m
);
1210 mm_ssh_gssapi_userok(char *user
)
1213 int authenticated
= 0;
1217 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSUSEROK
, &m
);
1218 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSUSEROK
,
1221 authenticated
= buffer_get_int(&m
);
1224 debug3("%s: user %sauthenticated",__func__
, authenticated
? "" : "not ");
1225 return (authenticated
);