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>
40 #include <openssl/bn.h>
41 #include <openssl/dh.h>
52 #include "auth-options.h"
56 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
59 #define TARGET_OS_MAC 1
67 #include "monitor_wrap.h"
69 #include "monitor_fdpass.h"
78 extern Newkeys
*newkeys
[];
79 extern z_stream incoming_stream
;
80 extern z_stream outgoing_stream
;
81 extern struct monitor
*pmonitor
;
82 extern Buffer input
, output
;
83 extern Buffer loginmsg
;
84 extern ServerOptions options
;
90 * m_pid is only set in the privileged part, and
91 * points to the unprivileged child.
93 return (pmonitor
&& pmonitor
->m_pid
> 0);
97 mm_request_send(int sock
, enum monitor_reqtype type
, Buffer
*m
)
99 u_int mlen
= buffer_len(m
);
102 debug3("%s entering: type %d", __func__
, type
);
104 put_u32(buf
, mlen
+ 1);
105 buf
[4] = (u_char
) type
; /* 1st byte of payload is mesg-type */
106 if (atomicio(vwrite
, sock
, buf
, sizeof(buf
)) != sizeof(buf
))
107 fatal("%s: write: %s", __func__
, strerror(errno
));
108 if (atomicio(vwrite
, sock
, buffer_ptr(m
), mlen
) != mlen
)
109 fatal("%s: write: %s", __func__
, strerror(errno
));
113 mm_request_receive(int sock
, Buffer
*m
)
118 debug3("%s entering", __func__
);
120 if (atomicio(read
, sock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
123 fatal("%s: read: %s", __func__
, strerror(errno
));
125 msg_len
= get_u32(buf
);
126 if (msg_len
> 256 * 1024)
127 fatal("%s: read: bad msg_len %d", __func__
, msg_len
);
129 buffer_append_space(m
, msg_len
);
130 if (atomicio(read
, sock
, buffer_ptr(m
), msg_len
) != msg_len
)
131 fatal("%s: read: %s", __func__
, strerror(errno
));
135 mm_request_receive_expect(int sock
, enum monitor_reqtype type
, Buffer
*m
)
139 debug3("%s entering: type %d", __func__
, type
);
141 mm_request_receive(sock
, m
);
142 rtype
= buffer_get_char(m
);
144 fatal("%s: read: rtype %d != type %d", __func__
,
149 mm_choose_dh(int min
, int nbits
, int max
)
156 buffer_put_int(&m
, min
);
157 buffer_put_int(&m
, nbits
);
158 buffer_put_int(&m
, max
);
160 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_MODULI
, &m
);
162 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__
);
163 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_MODULI
, &m
);
165 success
= buffer_get_char(&m
);
167 fatal("%s: MONITOR_ANS_MODULI failed", __func__
);
169 if ((p
= BN_new()) == NULL
)
170 fatal("%s: BN_new failed", __func__
);
171 if ((g
= BN_new()) == NULL
)
172 fatal("%s: BN_new failed", __func__
);
173 buffer_get_bignum2(&m
, p
);
174 buffer_get_bignum2(&m
, g
);
176 debug3("%s: remaining %d", __func__
, buffer_len(&m
));
179 return (dh_new_group(g
, p
));
183 mm_key_sign(Key
*key
, u_char
**sigp
, u_int
*lenp
, u_char
*data
, u_int datalen
)
185 Kex
*kex
= *pmonitor
->m_pkex
;
188 debug3("%s entering", __func__
);
191 buffer_put_int(&m
, kex
->host_key_index(key
));
192 buffer_put_string(&m
, data
, datalen
);
194 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SIGN
, &m
);
196 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__
);
197 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SIGN
, &m
);
198 *sigp
= buffer_get_string(&m
, lenp
);
205 mm_getpwnamallow(const char *username
)
211 debug3("%s entering", __func__
);
214 buffer_put_cstring(&m
, username
);
216 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PWNAM
, &m
);
218 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__
);
219 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PWNAM
, &m
);
221 if (buffer_get_char(&m
) == 0) {
225 pw
= buffer_get_string(&m
, &pwlen
);
226 if (pwlen
!= sizeof(struct passwd
))
227 fatal("%s: struct passwd size mismatch", __func__
);
228 pw
->pw_name
= buffer_get_string(&m
, NULL
);
229 pw
->pw_passwd
= buffer_get_string(&m
, NULL
);
230 pw
->pw_gecos
= buffer_get_string(&m
, NULL
);
231 #ifdef HAVE_PW_CLASS_IN_PASSWD
232 pw
->pw_class
= buffer_get_string(&m
, NULL
);
234 pw
->pw_dir
= buffer_get_string(&m
, NULL
);
235 pw
->pw_shell
= buffer_get_string(&m
, NULL
);
242 mm_auth2_read_banner(void)
247 debug3("%s entering", __func__
);
250 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTH2_READ_BANNER
, &m
);
253 mm_request_receive_expect(pmonitor
->m_recvfd
,
254 MONITOR_ANS_AUTH2_READ_BANNER
, &m
);
255 banner
= buffer_get_string(&m
, NULL
);
258 /* treat empty banner as missing banner */
259 if (strlen(banner
) == 0) {
266 /* Inform the privileged process about service and style */
269 mm_inform_authserv(char *service
, char *style
)
273 debug3("%s entering", __func__
);
276 buffer_put_cstring(&m
, service
);
277 buffer_put_cstring(&m
, style
? style
: "");
279 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTHSERV
, &m
);
284 /* Do the password authentication */
286 mm_auth_password(Authctxt
*authctxt
, char *password
)
289 int authenticated
= 0;
291 debug3("%s entering", __func__
);
294 buffer_put_cstring(&m
, password
);
295 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTHPASSWORD
, &m
);
297 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__
);
298 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_AUTHPASSWORD
, &m
);
300 authenticated
= buffer_get_int(&m
);
304 debug3("%s: user %sauthenticated",
305 __func__
, authenticated
? "" : "not ");
306 return (authenticated
);
310 mm_user_key_allowed(struct passwd
*pw
, Key
*key
)
312 return (mm_key_allowed(MM_USERKEY
, NULL
, NULL
, key
));
316 mm_hostbased_key_allowed(struct passwd
*pw
, char *user
, char *host
,
319 return (mm_key_allowed(MM_HOSTKEY
, user
, host
, key
));
323 mm_auth_rhosts_rsa_key_allowed(struct passwd
*pw
, char *user
,
324 char *host
, Key
*key
)
328 key
->type
= KEY_RSA
; /* XXX hack for key_to_blob */
329 ret
= mm_key_allowed(MM_RSAHOSTKEY
, user
, host
, key
);
330 key
->type
= KEY_RSA1
;
335 mm_send_debug(Buffer
*m
)
339 while (buffer_len(m
)) {
340 msg
= buffer_get_string(m
, NULL
);
341 debug3("%s: Sending debug: %s", __func__
, msg
);
342 packet_send_debug("%s", msg
);
348 mm_key_allowed(enum mm_keytype type
, char *user
, char *host
, Key
*key
)
353 int allowed
= 0, have_forced
= 0;
355 debug3("%s entering", __func__
);
357 /* Convert the key to a blob and the pass it over */
358 if (!key_to_blob(key
, &blob
, &len
))
362 buffer_put_int(&m
, type
);
363 buffer_put_cstring(&m
, user
? user
: "");
364 buffer_put_cstring(&m
, host
? host
: "");
365 buffer_put_string(&m
, blob
, len
);
368 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_KEYALLOWED
, &m
);
370 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__
);
371 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_KEYALLOWED
, &m
);
373 allowed
= buffer_get_int(&m
);
375 /* fake forced command */
376 auth_clear_options();
377 have_forced
= buffer_get_int(&m
);
378 forced_command
= have_forced
? xstrdup("true") : NULL
;
380 /* Send potential debug messages */
389 * This key verify needs to send the key type along, because the
390 * privileged parent makes the decision if the key is allowed
391 * for authentication.
395 mm_key_verify(Key
*key
, u_char
*sig
, u_int siglen
, u_char
*data
, u_int datalen
)
402 debug3("%s entering", __func__
);
404 /* Convert the key to a blob and the pass it over */
405 if (!key_to_blob(key
, &blob
, &len
))
409 buffer_put_string(&m
, blob
, len
);
410 buffer_put_string(&m
, sig
, siglen
);
411 buffer_put_string(&m
, data
, datalen
);
414 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_KEYVERIFY
, &m
);
416 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__
);
417 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_KEYVERIFY
, &m
);
419 verified
= buffer_get_int(&m
);
426 /* Export key state after authentication */
428 mm_newkeys_from_blob(u_char
*blob
, int blen
)
432 Newkeys
*newkey
= NULL
;
437 debug3("%s: %p(%d)", __func__
, blob
, blen
);
439 dump_base64(stderr
, blob
, blen
);
442 buffer_append(&b
, blob
, blen
);
444 newkey
= xmalloc(sizeof(*newkey
));
447 comp
= &newkey
->comp
;
450 enc
->name
= buffer_get_string(&b
, NULL
);
451 buffer_get(&b
, &enc
->cipher
, sizeof(enc
->cipher
));
452 enc
->enabled
= buffer_get_int(&b
);
453 enc
->block_size
= buffer_get_int(&b
);
454 enc
->key
= buffer_get_string(&b
, &enc
->key_len
);
455 enc
->iv
= buffer_get_string(&b
, &len
);
456 if (len
!= enc
->block_size
)
457 fatal("%s: bad ivlen: expected %u != %u", __func__
,
458 enc
->block_size
, len
);
460 if (enc
->name
== NULL
|| cipher_by_name(enc
->name
) != enc
->cipher
)
461 fatal("%s: bad cipher name %s or pointer %p", __func__
,
462 enc
->name
, enc
->cipher
);
465 mac
->name
= buffer_get_string(&b
, NULL
);
466 if (mac
->name
== NULL
|| mac_init(mac
, mac
->name
) == -1)
467 fatal("%s: can not init mac %s", __func__
, mac
->name
);
468 mac
->enabled
= buffer_get_int(&b
);
469 mac
->key
= buffer_get_string(&b
, &len
);
470 if (len
> mac
->key_len
)
471 fatal("%s: bad mac key length: %u > %d", __func__
, len
,
476 comp
->type
= buffer_get_int(&b
);
477 comp
->enabled
= buffer_get_int(&b
);
478 comp
->name
= buffer_get_string(&b
, NULL
);
480 len
= buffer_len(&b
);
482 error("newkeys_from_blob: remaining bytes in blob %u", len
);
488 mm_newkeys_to_blob(int mode
, u_char
**blobp
, u_int
*lenp
)
495 Newkeys
*newkey
= newkeys
[mode
];
497 debug3("%s: converting %p", __func__
, newkey
);
499 if (newkey
== NULL
) {
500 error("%s: newkey == NULL", __func__
);
505 comp
= &newkey
->comp
;
509 buffer_put_cstring(&b
, enc
->name
);
510 /* The cipher struct is constant and shared, you export pointer */
511 buffer_append(&b
, &enc
->cipher
, sizeof(enc
->cipher
));
512 buffer_put_int(&b
, enc
->enabled
);
513 buffer_put_int(&b
, enc
->block_size
);
514 buffer_put_string(&b
, enc
->key
, enc
->key_len
);
515 packet_get_keyiv(mode
, enc
->iv
, enc
->block_size
);
516 buffer_put_string(&b
, enc
->iv
, enc
->block_size
);
519 buffer_put_cstring(&b
, mac
->name
);
520 buffer_put_int(&b
, mac
->enabled
);
521 buffer_put_string(&b
, mac
->key
, mac
->key_len
);
524 buffer_put_int(&b
, comp
->type
);
525 buffer_put_int(&b
, comp
->enabled
);
526 buffer_put_cstring(&b
, comp
->name
);
528 len
= buffer_len(&b
);
532 *blobp
= xmalloc(len
);
533 memcpy(*blobp
, buffer_ptr(&b
), len
);
535 memset(buffer_ptr(&b
), 0, len
);
541 mm_send_kex(Buffer
*m
, Kex
*kex
)
543 buffer_put_string(m
, kex
->session_id
, kex
->session_id_len
);
544 buffer_put_int(m
, kex
->we_need
);
545 buffer_put_int(m
, kex
->hostkey_type
);
546 buffer_put_int(m
, kex
->kex_type
);
547 buffer_put_string(m
, buffer_ptr(&kex
->my
), buffer_len(&kex
->my
));
548 buffer_put_string(m
, buffer_ptr(&kex
->peer
), buffer_len(&kex
->peer
));
549 buffer_put_int(m
, kex
->flags
);
550 buffer_put_cstring(m
, kex
->client_version_string
);
551 buffer_put_cstring(m
, kex
->server_version_string
);
555 mm_send_keystate(struct monitor
*monitor
)
560 u_int32_t seqnr
, packets
;
570 buffer_put_int(&m
, packet_get_protocol_flags());
572 buffer_put_int(&m
, packet_get_ssh1_cipher());
574 debug3("%s: Sending ssh1 KEY+IV", __func__
);
575 keylen
= packet_get_encryption_key(NULL
);
576 key
= xmalloc(keylen
+1); /* add 1 if keylen == 0 */
577 keylen
= packet_get_encryption_key(key
);
578 buffer_put_string(&m
, key
, keylen
);
579 memset(key
, 0, keylen
);
582 ivlen
= packet_get_keyiv_len(MODE_OUT
);
583 packet_get_keyiv(MODE_OUT
, iv
, ivlen
);
584 buffer_put_string(&m
, iv
, ivlen
);
585 ivlen
= packet_get_keyiv_len(MODE_OUT
);
586 packet_get_keyiv(MODE_IN
, iv
, ivlen
);
587 buffer_put_string(&m
, iv
, ivlen
);
590 /* Kex for rekeying */
591 mm_send_kex(&m
, *monitor
->m_pkex
);
594 debug3("%s: Sending new keys: %p %p",
595 __func__
, newkeys
[MODE_OUT
], newkeys
[MODE_IN
]);
598 if (!mm_newkeys_to_blob(MODE_OUT
, &blob
, &bloblen
))
599 fatal("%s: conversion of newkeys failed", __func__
);
601 buffer_put_string(&m
, blob
, bloblen
);
604 if (!mm_newkeys_to_blob(MODE_IN
, &blob
, &bloblen
))
605 fatal("%s: conversion of newkeys failed", __func__
);
607 buffer_put_string(&m
, blob
, bloblen
);
610 packet_get_state(MODE_OUT
, &seqnr
, &blocks
, &packets
);
611 buffer_put_int(&m
, seqnr
);
612 buffer_put_int64(&m
, blocks
);
613 buffer_put_int(&m
, packets
);
614 packet_get_state(MODE_IN
, &seqnr
, &blocks
, &packets
);
615 buffer_put_int(&m
, seqnr
);
616 buffer_put_int64(&m
, blocks
);
617 buffer_put_int(&m
, packets
);
619 debug3("%s: New keys have been sent", __func__
);
621 /* More key context */
622 plen
= packet_get_keycontext(MODE_OUT
, NULL
);
624 packet_get_keycontext(MODE_OUT
, p
);
625 buffer_put_string(&m
, p
, plen
);
628 plen
= packet_get_keycontext(MODE_IN
, NULL
);
630 packet_get_keycontext(MODE_IN
, p
);
631 buffer_put_string(&m
, p
, plen
);
634 /* Compression state */
635 debug3("%s: Sending compression state", __func__
);
636 buffer_put_string(&m
, &outgoing_stream
, sizeof(outgoing_stream
));
637 buffer_put_string(&m
, &incoming_stream
, sizeof(incoming_stream
));
639 /* Network I/O buffers */
640 buffer_put_string(&m
, buffer_ptr(&input
), buffer_len(&input
));
641 buffer_put_string(&m
, buffer_ptr(&output
), buffer_len(&output
));
643 mm_request_send(monitor
->m_recvfd
, MONITOR_REQ_KEYEXPORT
, &m
);
644 debug3("%s: Finished sending state", __func__
);
650 mm_pty_allocate(int *ptyfd
, int *ttyfd
, char *namebuf
, size_t namebuflen
)
657 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PTY
, &m
);
659 debug3("%s: waiting for MONITOR_ANS_PTY", __func__
);
660 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PTY
, &m
);
662 success
= buffer_get_int(&m
);
664 debug3("%s: pty alloc failed", __func__
);
668 p
= buffer_get_string(&m
, NULL
);
669 msg
= buffer_get_string(&m
, NULL
);
672 strlcpy(namebuf
, p
, namebuflen
); /* Possible truncation */
675 buffer_append(&loginmsg
, msg
, strlen(msg
));
678 *ptyfd
= mm_receive_fd(pmonitor
->m_recvfd
);
679 *ttyfd
= mm_receive_fd(pmonitor
->m_recvfd
);
686 mm_session_pty_cleanup2(Session
*s
)
693 buffer_put_cstring(&m
, s
->tty
);
694 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PTYCLEANUP
, &m
);
697 /* closed dup'ed master */
698 if (close(s
->ptymaster
) < 0)
699 error("close(s->ptymaster): %s", strerror(errno
));
701 /* unlink pty from session */
707 mm_start_pam(Authctxt
*authctxt
)
711 debug3("%s entering", __func__
);
712 if (!options
.use_pam
)
713 fatal("UsePAM=no, but ended up in %s anyway", __func__
);
716 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_START
, &m
);
722 mm_do_pam_account(void)
728 debug3("%s entering", __func__
);
729 if (!options
.use_pam
)
730 fatal("UsePAM=no, but ended up in %s anyway", __func__
);
733 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_ACCOUNT
, &m
);
735 mm_request_receive_expect(pmonitor
->m_recvfd
,
736 MONITOR_ANS_PAM_ACCOUNT
, &m
);
737 ret
= buffer_get_int(&m
);
738 msg
= buffer_get_string(&m
, NULL
);
739 buffer_append(&loginmsg
, msg
, strlen(msg
));
744 debug3("%s returning %d", __func__
, ret
);
750 mm_sshpam_init_ctx(Authctxt
*authctxt
)
755 debug3("%s", __func__
);
757 buffer_put_cstring(&m
, authctxt
->user
);
758 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_INIT_CTX
, &m
);
759 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__
);
760 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_INIT_CTX
, &m
);
761 success
= buffer_get_int(&m
);
763 debug3("%s: pam_init_ctx failed", __func__
);
772 mm_sshpam_query(void *ctx
, char **name
, char **info
,
773 u_int
*num
, char ***prompts
, u_int
**echo_on
)
779 debug3("%s", __func__
);
781 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_QUERY
, &m
);
782 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__
);
783 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_QUERY
, &m
);
784 ret
= buffer_get_int(&m
);
785 debug3("%s: pam_query returned %d", __func__
, ret
);
786 *name
= buffer_get_string(&m
, NULL
);
787 *info
= buffer_get_string(&m
, NULL
);
788 *num
= buffer_get_int(&m
);
789 if (*num
> PAM_MAX_NUM_MSG
)
790 fatal("%s: recieved %u PAM messages, expected <= %u",
791 __func__
, *num
, PAM_MAX_NUM_MSG
);
792 *prompts
= xcalloc((*num
+ 1), sizeof(char *));
793 *echo_on
= xcalloc((*num
+ 1), sizeof(u_int
));
794 for (i
= 0; i
< *num
; ++i
) {
795 (*prompts
)[i
] = buffer_get_string(&m
, NULL
);
796 (*echo_on
)[i
] = buffer_get_int(&m
);
803 mm_sshpam_respond(void *ctx
, u_int num
, char **resp
)
809 debug3("%s", __func__
);
811 buffer_put_int(&m
, num
);
812 for (i
= 0; i
< num
; ++i
)
813 buffer_put_cstring(&m
, resp
[i
]);
814 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_RESPOND
, &m
);
815 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__
);
816 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_RESPOND
, &m
);
817 ret
= buffer_get_int(&m
);
818 debug3("%s: pam_respond returned %d", __func__
, ret
);
824 mm_sshpam_free_ctx(void *ctxtp
)
828 debug3("%s", __func__
);
830 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_FREE_CTX
, &m
);
831 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__
);
832 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_FREE_CTX
, &m
);
837 /* Request process termination */
845 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_TERM
, &m
);
850 mm_ssh1_session_key(BIGNUM
*num
)
856 buffer_put_bignum2(&m
, num
);
857 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SESSKEY
, &m
);
859 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SESSKEY
, &m
);
861 rsafail
= buffer_get_int(&m
);
862 buffer_get_bignum2(&m
, num
);
870 mm_chall_setup(char **name
, char **infotxt
, u_int
*numprompts
,
871 char ***prompts
, u_int
**echo_on
)
874 *infotxt
= xstrdup("");
876 *prompts
= xcalloc(*numprompts
, sizeof(char *));
877 *echo_on
= xcalloc(*numprompts
, sizeof(u_int
));
882 mm_bsdauth_query(void *ctx
, char **name
, char **infotxt
,
883 u_int
*numprompts
, char ***prompts
, u_int
**echo_on
)
889 debug3("%s: entering", __func__
);
892 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_BSDAUTHQUERY
, &m
);
894 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_BSDAUTHQUERY
,
896 success
= buffer_get_int(&m
);
898 debug3("%s: no challenge", __func__
);
903 /* Get the challenge, and format the response */
904 challenge
= buffer_get_string(&m
, NULL
);
907 mm_chall_setup(name
, infotxt
, numprompts
, prompts
, echo_on
);
908 (*prompts
)[0] = challenge
;
910 debug3("%s: received challenge: %s", __func__
, challenge
);
916 mm_bsdauth_respond(void *ctx
, u_int numresponses
, char **responses
)
921 debug3("%s: entering", __func__
);
922 if (numresponses
!= 1)
926 buffer_put_cstring(&m
, responses
[0]);
927 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_BSDAUTHRESPOND
, &m
);
929 mm_request_receive_expect(pmonitor
->m_recvfd
,
930 MONITOR_ANS_BSDAUTHRESPOND
, &m
);
932 authok
= buffer_get_int(&m
);
935 return ((authok
== 0) ? -1 : 0);
940 mm_skey_query(void *ctx
, char **name
, char **infotxt
,
941 u_int
*numprompts
, char ***prompts
, u_int
**echo_on
)
947 debug3("%s: entering", __func__
);
950 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SKEYQUERY
, &m
);
952 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SKEYQUERY
,
954 success
= buffer_get_int(&m
);
956 debug3("%s: no challenge", __func__
);
961 /* Get the challenge, and format the response */
962 challenge
= buffer_get_string(&m
, NULL
);
965 debug3("%s: received challenge: %s", __func__
, challenge
);
967 mm_chall_setup(name
, infotxt
, numprompts
, prompts
, echo_on
);
969 xasprintf(*prompts
, "%s%s", challenge
, SKEY_PROMPT
);
976 mm_skey_respond(void *ctx
, u_int numresponses
, char **responses
)
981 debug3("%s: entering", __func__
);
982 if (numresponses
!= 1)
986 buffer_put_cstring(&m
, responses
[0]);
987 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SKEYRESPOND
, &m
);
989 mm_request_receive_expect(pmonitor
->m_recvfd
,
990 MONITOR_ANS_SKEYRESPOND
, &m
);
992 authok
= buffer_get_int(&m
);
995 return ((authok
== 0) ? -1 : 0);
1000 mm_ssh1_session_id(u_char session_id
[16])
1005 debug3("%s entering", __func__
);
1008 for (i
= 0; i
< 16; i
++)
1009 buffer_put_char(&m
, session_id
[i
]);
1011 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SESSID
, &m
);
1016 mm_auth_rsa_key_allowed(struct passwd
*pw
, BIGNUM
*client_n
, Key
**rkey
)
1022 int allowed
= 0, have_forced
= 0;
1024 debug3("%s entering", __func__
);
1027 buffer_put_bignum2(&m
, client_n
);
1029 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSAKEYALLOWED
, &m
);
1030 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSAKEYALLOWED
, &m
);
1032 allowed
= buffer_get_int(&m
);
1034 /* fake forced command */
1035 auth_clear_options();
1036 have_forced
= buffer_get_int(&m
);
1037 forced_command
= have_forced
? xstrdup("true") : NULL
;
1039 if (allowed
&& rkey
!= NULL
) {
1040 blob
= buffer_get_string(&m
, &blen
);
1041 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1042 fatal("%s: key_from_blob failed", __func__
);
1053 mm_auth_rsa_generate_challenge(Key
*key
)
1060 debug3("%s entering", __func__
);
1062 if ((challenge
= BN_new()) == NULL
)
1063 fatal("%s: BN_new failed", __func__
);
1065 key
->type
= KEY_RSA
; /* XXX cheat for key_to_blob */
1066 if (key_to_blob(key
, &blob
, &blen
) == 0)
1067 fatal("%s: key_to_blob failed", __func__
);
1068 key
->type
= KEY_RSA1
;
1071 buffer_put_string(&m
, blob
, blen
);
1074 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSACHALLENGE
, &m
);
1075 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSACHALLENGE
, &m
);
1077 buffer_get_bignum2(&m
, challenge
);
1084 mm_auth_rsa_verify_response(Key
*key
, BIGNUM
*p
, u_char response
[16])
1091 debug3("%s entering", __func__
);
1093 key
->type
= KEY_RSA
; /* XXX cheat for key_to_blob */
1094 if (key_to_blob(key
, &blob
, &blen
) == 0)
1095 fatal("%s: key_to_blob failed", __func__
);
1096 key
->type
= KEY_RSA1
;
1099 buffer_put_string(&m
, blob
, blen
);
1100 buffer_put_string(&m
, response
, 16);
1103 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSARESPONSE
, &m
);
1104 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSARESPONSE
, &m
);
1106 success
= buffer_get_int(&m
);
1112 #ifdef SSH_AUDIT_EVENTS
1114 mm_audit_event(ssh_audit_event_t event
)
1118 debug3("%s entering", __func__
);
1121 buffer_put_int(&m
, event
);
1123 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUDIT_EVENT
, &m
);
1128 mm_audit_run_command(const char *command
)
1132 debug3("%s entering command %s", __func__
, command
);
1135 buffer_put_cstring(&m
, command
);
1137 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUDIT_COMMAND
, &m
);
1140 #endif /* SSH_AUDIT_EVENTS */
1144 mm_ssh_gssapi_server_ctx(Gssctxt
**ctx
, gss_OID goid
)
1149 /* Client doesn't get to see the context */
1153 buffer_put_string(&m
, goid
->elements
, goid
->length
);
1155 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSSETUP
, &m
);
1156 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSSETUP
, &m
);
1158 major
= buffer_get_int(&m
);
1165 mm_ssh_gssapi_accept_ctx(Gssctxt
*ctx
, gss_buffer_desc
*in
,
1166 gss_buffer_desc
*out
, OM_uint32
*flags
)
1173 buffer_put_string(&m
, in
->value
, in
->length
);
1175 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSSTEP
, &m
);
1176 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSSTEP
, &m
);
1178 major
= buffer_get_int(&m
);
1179 out
->value
= buffer_get_string(&m
, &len
);
1182 *flags
= buffer_get_int(&m
);
1190 mm_ssh_gssapi_checkmic(Gssctxt
*ctx
, gss_buffer_t gssbuf
, gss_buffer_t gssmic
)
1196 buffer_put_string(&m
, gssbuf
->value
, gssbuf
->length
);
1197 buffer_put_string(&m
, gssmic
->value
, gssmic
->length
);
1199 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSCHECKMIC
, &m
);
1200 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSCHECKMIC
,
1203 major
= buffer_get_int(&m
);
1209 mm_ssh_gssapi_userok(char *user
)
1212 int authenticated
= 0;
1216 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSUSEROK
, &m
);
1217 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSUSEROK
,
1220 authenticated
= buffer_get_int(&m
);
1223 debug3("%s: user %sauthenticated",__func__
, authenticated
? "" : "not ");
1224 return (authenticated
);