1 /* $OpenBSD: monitor_wrap.c,v 1.47 2006/07/11 20:07:25 stevesk 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>
32 #include <openssl/bn.h>
33 #include <openssl/dh.h>
42 #include "auth-options.h"
48 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
51 #define TARGET_OS_MAC 1
56 #include "monitor_wrap.h"
59 #include "monitor_fdpass.h"
73 extern Newkeys
*newkeys
[];
74 extern z_stream incoming_stream
;
75 extern z_stream outgoing_stream
;
76 extern struct monitor
*pmonitor
;
77 extern Buffer input
, output
;
78 extern Buffer loginmsg
;
79 extern ServerOptions options
;
85 * m_pid is only set in the privileged part, and
86 * points to the unprivileged child.
88 return (pmonitor
&& pmonitor
->m_pid
> 0);
92 mm_request_send(int sock
, enum monitor_reqtype type
, Buffer
*m
)
94 u_int mlen
= buffer_len(m
);
97 debug3("%s entering: type %d", __func__
, type
);
99 put_u32(buf
, mlen
+ 1);
100 buf
[4] = (u_char
) type
; /* 1st byte of payload is mesg-type */
101 if (atomicio(vwrite
, sock
, buf
, sizeof(buf
)) != sizeof(buf
))
102 fatal("%s: write: %s", __func__
, strerror(errno
));
103 if (atomicio(vwrite
, sock
, buffer_ptr(m
), mlen
) != mlen
)
104 fatal("%s: write: %s", __func__
, strerror(errno
));
108 mm_request_receive(int sock
, Buffer
*m
)
113 debug3("%s entering", __func__
);
115 if (atomicio(read
, sock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
118 fatal("%s: read: %s", __func__
, strerror(errno
));
120 msg_len
= get_u32(buf
);
121 if (msg_len
> 256 * 1024)
122 fatal("%s: read: bad msg_len %d", __func__
, msg_len
);
124 buffer_append_space(m
, msg_len
);
125 if (atomicio(read
, sock
, buffer_ptr(m
), msg_len
) != msg_len
)
126 fatal("%s: read: %s", __func__
, strerror(errno
));
130 mm_request_receive_expect(int sock
, enum monitor_reqtype type
, Buffer
*m
)
134 debug3("%s entering: type %d", __func__
, type
);
136 mm_request_receive(sock
, m
);
137 rtype
= buffer_get_char(m
);
139 fatal("%s: read: rtype %d != type %d", __func__
,
144 mm_choose_dh(int min
, int nbits
, int max
)
151 buffer_put_int(&m
, min
);
152 buffer_put_int(&m
, nbits
);
153 buffer_put_int(&m
, max
);
155 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_MODULI
, &m
);
157 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__
);
158 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_MODULI
, &m
);
160 success
= buffer_get_char(&m
);
162 fatal("%s: MONITOR_ANS_MODULI failed", __func__
);
164 if ((p
= BN_new()) == NULL
)
165 fatal("%s: BN_new failed", __func__
);
166 if ((g
= BN_new()) == NULL
)
167 fatal("%s: BN_new failed", __func__
);
168 buffer_get_bignum2(&m
, p
);
169 buffer_get_bignum2(&m
, g
);
171 debug3("%s: remaining %d", __func__
, buffer_len(&m
));
174 return (dh_new_group(g
, p
));
178 mm_key_sign(Key
*key
, u_char
**sigp
, u_int
*lenp
, u_char
*data
, u_int datalen
)
180 Kex
*kex
= *pmonitor
->m_pkex
;
183 debug3("%s entering", __func__
);
186 buffer_put_int(&m
, kex
->host_key_index(key
));
187 buffer_put_string(&m
, data
, datalen
);
189 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SIGN
, &m
);
191 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__
);
192 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SIGN
, &m
);
193 *sigp
= buffer_get_string(&m
, lenp
);
200 mm_getpwnamallow(const char *username
)
206 debug3("%s entering", __func__
);
209 buffer_put_cstring(&m
, username
);
211 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PWNAM
, &m
);
213 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__
);
214 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PWNAM
, &m
);
216 if (buffer_get_char(&m
) == 0) {
220 pw
= buffer_get_string(&m
, &pwlen
);
221 if (pwlen
!= sizeof(struct passwd
))
222 fatal("%s: struct passwd size mismatch", __func__
);
223 pw
->pw_name
= buffer_get_string(&m
, NULL
);
224 pw
->pw_passwd
= buffer_get_string(&m
, NULL
);
225 pw
->pw_gecos
= buffer_get_string(&m
, NULL
);
226 #ifdef HAVE_PW_CLASS_IN_PASSWD
227 pw
->pw_class
= buffer_get_string(&m
, NULL
);
229 pw
->pw_dir
= buffer_get_string(&m
, NULL
);
230 pw
->pw_shell
= buffer_get_string(&m
, NULL
);
237 mm_auth2_read_banner(void)
242 debug3("%s entering", __func__
);
245 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTH2_READ_BANNER
, &m
);
248 mm_request_receive_expect(pmonitor
->m_recvfd
,
249 MONITOR_ANS_AUTH2_READ_BANNER
, &m
);
250 banner
= buffer_get_string(&m
, NULL
);
253 /* treat empty banner as missing banner */
254 if (strlen(banner
) == 0) {
261 /* Inform the privileged process about service and style */
264 mm_inform_authserv(char *service
, char *style
)
268 debug3("%s entering", __func__
);
271 buffer_put_cstring(&m
, service
);
272 buffer_put_cstring(&m
, style
? style
: "");
274 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTHSERV
, &m
);
279 /* Do the password authentication */
281 mm_auth_password(Authctxt
*authctxt
, char *password
)
284 int authenticated
= 0;
286 debug3("%s entering", __func__
);
289 buffer_put_cstring(&m
, password
);
290 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUTHPASSWORD
, &m
);
292 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__
);
293 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_AUTHPASSWORD
, &m
);
295 authenticated
= buffer_get_int(&m
);
299 debug3("%s: user %sauthenticated",
300 __func__
, authenticated
? "" : "not ");
301 return (authenticated
);
305 mm_user_key_allowed(struct passwd
*pw
, Key
*key
)
307 return (mm_key_allowed(MM_USERKEY
, NULL
, NULL
, key
));
311 mm_hostbased_key_allowed(struct passwd
*pw
, char *user
, char *host
,
314 return (mm_key_allowed(MM_HOSTKEY
, user
, host
, key
));
318 mm_auth_rhosts_rsa_key_allowed(struct passwd
*pw
, char *user
,
319 char *host
, Key
*key
)
323 key
->type
= KEY_RSA
; /* XXX hack for key_to_blob */
324 ret
= mm_key_allowed(MM_RSAHOSTKEY
, user
, host
, key
);
325 key
->type
= KEY_RSA1
;
330 mm_send_debug(Buffer
*m
)
334 while (buffer_len(m
)) {
335 msg
= buffer_get_string(m
, NULL
);
336 debug3("%s: Sending debug: %s", __func__
, msg
);
337 packet_send_debug("%s", msg
);
343 mm_key_allowed(enum mm_keytype type
, char *user
, char *host
, Key
*key
)
348 int allowed
= 0, have_forced
= 0;
350 debug3("%s entering", __func__
);
352 /* Convert the key to a blob and the pass it over */
353 if (!key_to_blob(key
, &blob
, &len
))
357 buffer_put_int(&m
, type
);
358 buffer_put_cstring(&m
, user
? user
: "");
359 buffer_put_cstring(&m
, host
? host
: "");
360 buffer_put_string(&m
, blob
, len
);
363 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_KEYALLOWED
, &m
);
365 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__
);
366 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_KEYALLOWED
, &m
);
368 allowed
= buffer_get_int(&m
);
370 /* fake forced command */
371 auth_clear_options();
372 have_forced
= buffer_get_int(&m
);
373 forced_command
= have_forced
? xstrdup("true") : NULL
;
375 /* Send potential debug messages */
384 * This key verify needs to send the key type along, because the
385 * privileged parent makes the decision if the key is allowed
386 * for authentication.
390 mm_key_verify(Key
*key
, u_char
*sig
, u_int siglen
, u_char
*data
, u_int datalen
)
397 debug3("%s entering", __func__
);
399 /* Convert the key to a blob and the pass it over */
400 if (!key_to_blob(key
, &blob
, &len
))
404 buffer_put_string(&m
, blob
, len
);
405 buffer_put_string(&m
, sig
, siglen
);
406 buffer_put_string(&m
, data
, datalen
);
409 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_KEYVERIFY
, &m
);
411 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__
);
412 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_KEYVERIFY
, &m
);
414 verified
= buffer_get_int(&m
);
421 /* Export key state after authentication */
423 mm_newkeys_from_blob(u_char
*blob
, int blen
)
427 Newkeys
*newkey
= NULL
;
432 debug3("%s: %p(%d)", __func__
, blob
, blen
);
434 dump_base64(stderr
, blob
, blen
);
437 buffer_append(&b
, blob
, blen
);
439 newkey
= xmalloc(sizeof(*newkey
));
442 comp
= &newkey
->comp
;
445 enc
->name
= buffer_get_string(&b
, NULL
);
446 buffer_get(&b
, &enc
->cipher
, sizeof(enc
->cipher
));
447 enc
->enabled
= buffer_get_int(&b
);
448 enc
->block_size
= buffer_get_int(&b
);
449 enc
->key
= buffer_get_string(&b
, &enc
->key_len
);
450 enc
->iv
= buffer_get_string(&b
, &len
);
451 if (len
!= enc
->block_size
)
452 fatal("%s: bad ivlen: expected %u != %u", __func__
,
453 enc
->block_size
, len
);
455 if (enc
->name
== NULL
|| cipher_by_name(enc
->name
) != enc
->cipher
)
456 fatal("%s: bad cipher name %s or pointer %p", __func__
,
457 enc
->name
, enc
->cipher
);
460 mac
->name
= buffer_get_string(&b
, NULL
);
461 if (mac
->name
== NULL
|| mac_init(mac
, mac
->name
) == -1)
462 fatal("%s: can not init mac %s", __func__
, mac
->name
);
463 mac
->enabled
= buffer_get_int(&b
);
464 mac
->key
= buffer_get_string(&b
, &len
);
465 if (len
> mac
->key_len
)
466 fatal("%s: bad mac key length: %u > %d", __func__
, len
,
471 comp
->type
= buffer_get_int(&b
);
472 comp
->enabled
= buffer_get_int(&b
);
473 comp
->name
= buffer_get_string(&b
, NULL
);
475 len
= buffer_len(&b
);
477 error("newkeys_from_blob: remaining bytes in blob %u", len
);
483 mm_newkeys_to_blob(int mode
, u_char
**blobp
, u_int
*lenp
)
490 Newkeys
*newkey
= newkeys
[mode
];
492 debug3("%s: converting %p", __func__
, newkey
);
494 if (newkey
== NULL
) {
495 error("%s: newkey == NULL", __func__
);
500 comp
= &newkey
->comp
;
504 buffer_put_cstring(&b
, enc
->name
);
505 /* The cipher struct is constant and shared, you export pointer */
506 buffer_append(&b
, &enc
->cipher
, sizeof(enc
->cipher
));
507 buffer_put_int(&b
, enc
->enabled
);
508 buffer_put_int(&b
, enc
->block_size
);
509 buffer_put_string(&b
, enc
->key
, enc
->key_len
);
510 packet_get_keyiv(mode
, enc
->iv
, enc
->block_size
);
511 buffer_put_string(&b
, enc
->iv
, enc
->block_size
);
514 buffer_put_cstring(&b
, mac
->name
);
515 buffer_put_int(&b
, mac
->enabled
);
516 buffer_put_string(&b
, mac
->key
, mac
->key_len
);
519 buffer_put_int(&b
, comp
->type
);
520 buffer_put_int(&b
, comp
->enabled
);
521 buffer_put_cstring(&b
, comp
->name
);
523 len
= buffer_len(&b
);
527 *blobp
= xmalloc(len
);
528 memcpy(*blobp
, buffer_ptr(&b
), len
);
530 memset(buffer_ptr(&b
), 0, len
);
536 mm_send_kex(Buffer
*m
, Kex
*kex
)
538 buffer_put_string(m
, kex
->session_id
, kex
->session_id_len
);
539 buffer_put_int(m
, kex
->we_need
);
540 buffer_put_int(m
, kex
->hostkey_type
);
541 buffer_put_int(m
, kex
->kex_type
);
542 buffer_put_string(m
, buffer_ptr(&kex
->my
), buffer_len(&kex
->my
));
543 buffer_put_string(m
, buffer_ptr(&kex
->peer
), buffer_len(&kex
->peer
));
544 buffer_put_int(m
, kex
->flags
);
545 buffer_put_cstring(m
, kex
->client_version_string
);
546 buffer_put_cstring(m
, kex
->server_version_string
);
550 mm_send_keystate(struct monitor
*monitor
)
555 u_int32_t seqnr
, packets
;
565 buffer_put_int(&m
, packet_get_protocol_flags());
567 buffer_put_int(&m
, packet_get_ssh1_cipher());
569 debug3("%s: Sending ssh1 KEY+IV", __func__
);
570 keylen
= packet_get_encryption_key(NULL
);
571 key
= xmalloc(keylen
+1); /* add 1 if keylen == 0 */
572 keylen
= packet_get_encryption_key(key
);
573 buffer_put_string(&m
, key
, keylen
);
574 memset(key
, 0, keylen
);
577 ivlen
= packet_get_keyiv_len(MODE_OUT
);
578 packet_get_keyiv(MODE_OUT
, iv
, ivlen
);
579 buffer_put_string(&m
, iv
, ivlen
);
580 ivlen
= packet_get_keyiv_len(MODE_OUT
);
581 packet_get_keyiv(MODE_IN
, iv
, ivlen
);
582 buffer_put_string(&m
, iv
, ivlen
);
585 /* Kex for rekeying */
586 mm_send_kex(&m
, *monitor
->m_pkex
);
589 debug3("%s: Sending new keys: %p %p",
590 __func__
, newkeys
[MODE_OUT
], newkeys
[MODE_IN
]);
593 if (!mm_newkeys_to_blob(MODE_OUT
, &blob
, &bloblen
))
594 fatal("%s: conversion of newkeys failed", __func__
);
596 buffer_put_string(&m
, blob
, bloblen
);
599 if (!mm_newkeys_to_blob(MODE_IN
, &blob
, &bloblen
))
600 fatal("%s: conversion of newkeys failed", __func__
);
602 buffer_put_string(&m
, blob
, bloblen
);
605 packet_get_state(MODE_OUT
, &seqnr
, &blocks
, &packets
);
606 buffer_put_int(&m
, seqnr
);
607 buffer_put_int64(&m
, blocks
);
608 buffer_put_int(&m
, packets
);
609 packet_get_state(MODE_IN
, &seqnr
, &blocks
, &packets
);
610 buffer_put_int(&m
, seqnr
);
611 buffer_put_int64(&m
, blocks
);
612 buffer_put_int(&m
, packets
);
614 debug3("%s: New keys have been sent", __func__
);
616 /* More key context */
617 plen
= packet_get_keycontext(MODE_OUT
, NULL
);
619 packet_get_keycontext(MODE_OUT
, p
);
620 buffer_put_string(&m
, p
, plen
);
623 plen
= packet_get_keycontext(MODE_IN
, NULL
);
625 packet_get_keycontext(MODE_IN
, p
);
626 buffer_put_string(&m
, p
, plen
);
629 /* Compression state */
630 debug3("%s: Sending compression state", __func__
);
631 buffer_put_string(&m
, &outgoing_stream
, sizeof(outgoing_stream
));
632 buffer_put_string(&m
, &incoming_stream
, sizeof(incoming_stream
));
634 /* Network I/O buffers */
635 buffer_put_string(&m
, buffer_ptr(&input
), buffer_len(&input
));
636 buffer_put_string(&m
, buffer_ptr(&output
), buffer_len(&output
));
638 mm_request_send(monitor
->m_recvfd
, MONITOR_REQ_KEYEXPORT
, &m
);
639 debug3("%s: Finished sending state", __func__
);
645 mm_pty_allocate(int *ptyfd
, int *ttyfd
, char *namebuf
, size_t namebuflen
)
652 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PTY
, &m
);
654 debug3("%s: waiting for MONITOR_ANS_PTY", __func__
);
655 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PTY
, &m
);
657 success
= buffer_get_int(&m
);
659 debug3("%s: pty alloc failed", __func__
);
663 p
= buffer_get_string(&m
, NULL
);
664 msg
= buffer_get_string(&m
, NULL
);
667 strlcpy(namebuf
, p
, namebuflen
); /* Possible truncation */
670 buffer_append(&loginmsg
, msg
, strlen(msg
));
673 *ptyfd
= mm_receive_fd(pmonitor
->m_recvfd
);
674 *ttyfd
= mm_receive_fd(pmonitor
->m_recvfd
);
681 mm_session_pty_cleanup2(Session
*s
)
688 buffer_put_cstring(&m
, s
->tty
);
689 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PTYCLEANUP
, &m
);
692 /* closed dup'ed master */
693 if (close(s
->ptymaster
) < 0)
694 error("close(s->ptymaster): %s", strerror(errno
));
696 /* unlink pty from session */
702 mm_start_pam(Authctxt
*authctxt
)
706 debug3("%s entering", __func__
);
707 if (!options
.use_pam
)
708 fatal("UsePAM=no, but ended up in %s anyway", __func__
);
711 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_START
, &m
);
717 mm_do_pam_account(void)
723 debug3("%s entering", __func__
);
724 if (!options
.use_pam
)
725 fatal("UsePAM=no, but ended up in %s anyway", __func__
);
728 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_ACCOUNT
, &m
);
730 mm_request_receive_expect(pmonitor
->m_recvfd
,
731 MONITOR_ANS_PAM_ACCOUNT
, &m
);
732 ret
= buffer_get_int(&m
);
733 msg
= buffer_get_string(&m
, NULL
);
734 buffer_append(&loginmsg
, msg
, strlen(msg
));
739 debug3("%s returning %d", __func__
, ret
);
745 mm_sshpam_init_ctx(Authctxt
*authctxt
)
750 debug3("%s", __func__
);
752 buffer_put_cstring(&m
, authctxt
->user
);
753 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_INIT_CTX
, &m
);
754 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__
);
755 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_INIT_CTX
, &m
);
756 success
= buffer_get_int(&m
);
758 debug3("%s: pam_init_ctx failed", __func__
);
767 mm_sshpam_query(void *ctx
, char **name
, char **info
,
768 u_int
*num
, char ***prompts
, u_int
**echo_on
)
774 debug3("%s", __func__
);
776 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_QUERY
, &m
);
777 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__
);
778 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_QUERY
, &m
);
779 ret
= buffer_get_int(&m
);
780 debug3("%s: pam_query returned %d", __func__
, ret
);
781 *name
= buffer_get_string(&m
, NULL
);
782 *info
= buffer_get_string(&m
, NULL
);
783 *num
= buffer_get_int(&m
);
784 if (*num
> PAM_MAX_NUM_MSG
)
785 fatal("%s: recieved %u PAM messages, expected <= %u",
786 __func__
, *num
, PAM_MAX_NUM_MSG
);
787 *prompts
= xcalloc((*num
+ 1), sizeof(char *));
788 *echo_on
= xcalloc((*num
+ 1), sizeof(u_int
));
789 for (i
= 0; i
< *num
; ++i
) {
790 (*prompts
)[i
] = buffer_get_string(&m
, NULL
);
791 (*echo_on
)[i
] = buffer_get_int(&m
);
798 mm_sshpam_respond(void *ctx
, u_int num
, char **resp
)
804 debug3("%s", __func__
);
806 buffer_put_int(&m
, num
);
807 for (i
= 0; i
< num
; ++i
)
808 buffer_put_cstring(&m
, resp
[i
]);
809 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_RESPOND
, &m
);
810 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__
);
811 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_RESPOND
, &m
);
812 ret
= buffer_get_int(&m
);
813 debug3("%s: pam_respond returned %d", __func__
, ret
);
819 mm_sshpam_free_ctx(void *ctxtp
)
823 debug3("%s", __func__
);
825 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_PAM_FREE_CTX
, &m
);
826 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__
);
827 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_PAM_FREE_CTX
, &m
);
832 /* Request process termination */
840 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_TERM
, &m
);
845 mm_ssh1_session_key(BIGNUM
*num
)
851 buffer_put_bignum2(&m
, num
);
852 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SESSKEY
, &m
);
854 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SESSKEY
, &m
);
856 rsafail
= buffer_get_int(&m
);
857 buffer_get_bignum2(&m
, num
);
865 mm_chall_setup(char **name
, char **infotxt
, u_int
*numprompts
,
866 char ***prompts
, u_int
**echo_on
)
869 *infotxt
= xstrdup("");
871 *prompts
= xcalloc(*numprompts
, sizeof(char *));
872 *echo_on
= xcalloc(*numprompts
, sizeof(u_int
));
877 mm_bsdauth_query(void *ctx
, char **name
, char **infotxt
,
878 u_int
*numprompts
, char ***prompts
, u_int
**echo_on
)
884 debug3("%s: entering", __func__
);
887 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_BSDAUTHQUERY
, &m
);
889 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_BSDAUTHQUERY
,
891 success
= buffer_get_int(&m
);
893 debug3("%s: no challenge", __func__
);
898 /* Get the challenge, and format the response */
899 challenge
= buffer_get_string(&m
, NULL
);
902 mm_chall_setup(name
, infotxt
, numprompts
, prompts
, echo_on
);
903 (*prompts
)[0] = challenge
;
905 debug3("%s: received challenge: %s", __func__
, challenge
);
911 mm_bsdauth_respond(void *ctx
, u_int numresponses
, char **responses
)
916 debug3("%s: entering", __func__
);
917 if (numresponses
!= 1)
921 buffer_put_cstring(&m
, responses
[0]);
922 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_BSDAUTHRESPOND
, &m
);
924 mm_request_receive_expect(pmonitor
->m_recvfd
,
925 MONITOR_ANS_BSDAUTHRESPOND
, &m
);
927 authok
= buffer_get_int(&m
);
930 return ((authok
== 0) ? -1 : 0);
935 mm_skey_query(void *ctx
, char **name
, char **infotxt
,
936 u_int
*numprompts
, char ***prompts
, u_int
**echo_on
)
943 debug3("%s: entering", __func__
);
946 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SKEYQUERY
, &m
);
948 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_SKEYQUERY
,
950 success
= buffer_get_int(&m
);
952 debug3("%s: no challenge", __func__
);
957 /* Get the challenge, and format the response */
958 challenge
= buffer_get_string(&m
, NULL
);
961 debug3("%s: received challenge: %s", __func__
, challenge
);
963 mm_chall_setup(name
, infotxt
, numprompts
, prompts
, echo_on
);
965 xasprintf(*prompts
, "%s%s", challenge
, SKEY_PROMPT
);
972 mm_skey_respond(void *ctx
, u_int numresponses
, char **responses
)
977 debug3("%s: entering", __func__
);
978 if (numresponses
!= 1)
982 buffer_put_cstring(&m
, responses
[0]);
983 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SKEYRESPOND
, &m
);
985 mm_request_receive_expect(pmonitor
->m_recvfd
,
986 MONITOR_ANS_SKEYRESPOND
, &m
);
988 authok
= buffer_get_int(&m
);
991 return ((authok
== 0) ? -1 : 0);
996 mm_ssh1_session_id(u_char session_id
[16])
1001 debug3("%s entering", __func__
);
1004 for (i
= 0; i
< 16; i
++)
1005 buffer_put_char(&m
, session_id
[i
]);
1007 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_SESSID
, &m
);
1012 mm_auth_rsa_key_allowed(struct passwd
*pw
, BIGNUM
*client_n
, Key
**rkey
)
1018 int allowed
= 0, have_forced
= 0;
1020 debug3("%s entering", __func__
);
1023 buffer_put_bignum2(&m
, client_n
);
1025 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSAKEYALLOWED
, &m
);
1026 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSAKEYALLOWED
, &m
);
1028 allowed
= buffer_get_int(&m
);
1030 /* fake forced command */
1031 auth_clear_options();
1032 have_forced
= buffer_get_int(&m
);
1033 forced_command
= have_forced
? xstrdup("true") : NULL
;
1035 if (allowed
&& rkey
!= NULL
) {
1036 blob
= buffer_get_string(&m
, &blen
);
1037 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1038 fatal("%s: key_from_blob failed", __func__
);
1049 mm_auth_rsa_generate_challenge(Key
*key
)
1056 debug3("%s entering", __func__
);
1058 if ((challenge
= BN_new()) == NULL
)
1059 fatal("%s: BN_new failed", __func__
);
1061 key
->type
= KEY_RSA
; /* XXX cheat for key_to_blob */
1062 if (key_to_blob(key
, &blob
, &blen
) == 0)
1063 fatal("%s: key_to_blob failed", __func__
);
1064 key
->type
= KEY_RSA1
;
1067 buffer_put_string(&m
, blob
, blen
);
1070 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSACHALLENGE
, &m
);
1071 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSACHALLENGE
, &m
);
1073 buffer_get_bignum2(&m
, challenge
);
1080 mm_auth_rsa_verify_response(Key
*key
, BIGNUM
*p
, u_char response
[16])
1087 debug3("%s entering", __func__
);
1089 key
->type
= KEY_RSA
; /* XXX cheat for key_to_blob */
1090 if (key_to_blob(key
, &blob
, &blen
) == 0)
1091 fatal("%s: key_to_blob failed", __func__
);
1092 key
->type
= KEY_RSA1
;
1095 buffer_put_string(&m
, blob
, blen
);
1096 buffer_put_string(&m
, response
, 16);
1099 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_RSARESPONSE
, &m
);
1100 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_RSARESPONSE
, &m
);
1102 success
= buffer_get_int(&m
);
1108 #ifdef SSH_AUDIT_EVENTS
1110 mm_audit_event(ssh_audit_event_t event
)
1114 debug3("%s entering", __func__
);
1117 buffer_put_int(&m
, event
);
1119 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUDIT_EVENT
, &m
);
1124 mm_audit_run_command(const char *command
)
1128 debug3("%s entering command %s", __func__
, command
);
1131 buffer_put_cstring(&m
, command
);
1133 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_AUDIT_COMMAND
, &m
);
1136 #endif /* SSH_AUDIT_EVENTS */
1140 mm_ssh_gssapi_server_ctx(Gssctxt
**ctx
, gss_OID goid
)
1145 /* Client doesn't get to see the context */
1149 buffer_put_string(&m
, goid
->elements
, goid
->length
);
1151 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSSETUP
, &m
);
1152 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSSETUP
, &m
);
1154 major
= buffer_get_int(&m
);
1161 mm_ssh_gssapi_accept_ctx(Gssctxt
*ctx
, gss_buffer_desc
*in
,
1162 gss_buffer_desc
*out
, OM_uint32
*flags
)
1169 buffer_put_string(&m
, in
->value
, in
->length
);
1171 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSSTEP
, &m
);
1172 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSSTEP
, &m
);
1174 major
= buffer_get_int(&m
);
1175 out
->value
= buffer_get_string(&m
, &len
);
1178 *flags
= buffer_get_int(&m
);
1186 mm_ssh_gssapi_checkmic(Gssctxt
*ctx
, gss_buffer_t gssbuf
, gss_buffer_t gssmic
)
1192 buffer_put_string(&m
, gssbuf
->value
, gssbuf
->length
);
1193 buffer_put_string(&m
, gssmic
->value
, gssmic
->length
);
1195 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSCHECKMIC
, &m
);
1196 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSCHECKMIC
,
1199 major
= buffer_get_int(&m
);
1205 mm_ssh_gssapi_userok(char *user
)
1208 int authenticated
= 0;
1212 mm_request_send(pmonitor
->m_recvfd
, MONITOR_REQ_GSSUSEROK
, &m
);
1213 mm_request_receive_expect(pmonitor
->m_recvfd
, MONITOR_ANS_GSSUSEROK
,
1216 authenticated
= buffer_get_int(&m
);
1219 debug3("%s: user %sauthenticated",__func__
, authenticated
? "" : "not ");
1220 return (authenticated
);