1 /* $OpenBSD: monitor.c,v 1.135 2014/07/15 15:54:14 millert 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>
31 #include <sys/param.h>
32 #include <sys/socket.h>
33 #include "openbsd-compat/sys-tree.h"
51 # ifdef HAVE_SYS_POLL_H
52 # include <sys/poll.h>
61 #include <openssl/dh.h>
64 #include "openbsd-compat/sys-queue.h"
75 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
78 #define TARGET_OS_MAC 1
83 #include "auth-options.h"
93 #include "monitor_mm.h"
97 #include "monitor_wrap.h"
98 #include "monitor_fdpass.h"
105 static Gssctxt
*gsscontext
= NULL
;
109 extern ServerOptions options
;
110 extern u_int utmp_len
;
111 extern Newkeys
*current_keys
[];
112 extern z_stream incoming_stream
;
113 extern z_stream outgoing_stream
;
114 extern u_char session_id
[];
115 extern Buffer auth_debug
;
116 extern int auth_debug_init
;
117 extern Buffer loginmsg
;
119 /* State exported from the child */
140 u_int64_t sent_bytes
;
141 u_int64_t recv_bytes
;
144 /* Functions on the monitor that answer unprivileged requests */
146 int mm_answer_moduli(int, Buffer
*);
147 int mm_answer_sign(int, Buffer
*);
148 int mm_answer_pwnamallow(int, Buffer
*);
149 int mm_answer_auth2_read_banner(int, Buffer
*);
150 int mm_answer_authserv(int, Buffer
*);
151 int mm_answer_authpassword(int, Buffer
*);
152 int mm_answer_bsdauthquery(int, Buffer
*);
153 int mm_answer_bsdauthrespond(int, Buffer
*);
154 int mm_answer_skeyquery(int, Buffer
*);
155 int mm_answer_skeyrespond(int, Buffer
*);
156 int mm_answer_keyallowed(int, Buffer
*);
157 int mm_answer_keyverify(int, Buffer
*);
158 int mm_answer_pty(int, Buffer
*);
159 int mm_answer_pty_cleanup(int, Buffer
*);
160 int mm_answer_term(int, Buffer
*);
161 int mm_answer_rsa_keyallowed(int, Buffer
*);
162 int mm_answer_rsa_challenge(int, Buffer
*);
163 int mm_answer_rsa_response(int, Buffer
*);
164 int mm_answer_sesskey(int, Buffer
*);
165 int mm_answer_sessid(int, Buffer
*);
168 int mm_answer_pam_start(int, Buffer
*);
169 int mm_answer_pam_account(int, Buffer
*);
170 int mm_answer_pam_init_ctx(int, Buffer
*);
171 int mm_answer_pam_query(int, Buffer
*);
172 int mm_answer_pam_respond(int, Buffer
*);
173 int mm_answer_pam_free_ctx(int, Buffer
*);
177 int mm_answer_gss_setup_ctx(int, Buffer
*);
178 int mm_answer_gss_accept_ctx(int, Buffer
*);
179 int mm_answer_gss_userok(int, Buffer
*);
180 int mm_answer_gss_checkmic(int, Buffer
*);
183 #ifdef SSH_AUDIT_EVENTS
184 int mm_answer_audit_event(int, Buffer
*);
185 int mm_answer_audit_command(int, Buffer
*);
188 static int monitor_read_log(struct monitor
*);
190 static Authctxt
*authctxt
;
193 static BIGNUM
*ssh1_challenge
= NULL
; /* used for ssh1 rsa auth */
196 /* local state for key verify */
197 static u_char
*key_blob
= NULL
;
198 static u_int key_bloblen
= 0;
199 static int key_blobtype
= MM_NOKEY
;
200 static char *hostbased_cuser
= NULL
;
201 static char *hostbased_chost
= NULL
;
202 static char *auth_method
= "unknown";
203 static char *auth_submethod
= NULL
;
204 static u_int session_id2_len
= 0;
205 static u_char
*session_id2
= NULL
;
206 static pid_t monitor_child_pid
;
209 enum monitor_reqtype type
;
211 int (*f
)(int, Buffer
*);
214 #define MON_ISAUTH 0x0004 /* Required for Authentication */
215 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
216 #define MON_ONCE 0x0010 /* Disable after calling */
217 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
219 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
221 #define MON_PERMIT 0x1000 /* Request is permitted */
223 struct mon_table mon_dispatch_proto20
[] = {
225 {MONITOR_REQ_MODULI
, MON_ONCE
, mm_answer_moduli
},
227 {MONITOR_REQ_SIGN
, MON_ONCE
, mm_answer_sign
},
228 {MONITOR_REQ_PWNAM
, MON_ONCE
, mm_answer_pwnamallow
},
229 {MONITOR_REQ_AUTHSERV
, MON_ONCE
, mm_answer_authserv
},
230 {MONITOR_REQ_AUTH2_READ_BANNER
, MON_ONCE
, mm_answer_auth2_read_banner
},
231 {MONITOR_REQ_AUTHPASSWORD
, MON_AUTH
, mm_answer_authpassword
},
233 {MONITOR_REQ_PAM_START
, MON_ONCE
, mm_answer_pam_start
},
234 {MONITOR_REQ_PAM_ACCOUNT
, 0, mm_answer_pam_account
},
235 {MONITOR_REQ_PAM_INIT_CTX
, MON_ISAUTH
, mm_answer_pam_init_ctx
},
236 {MONITOR_REQ_PAM_QUERY
, MON_ISAUTH
, mm_answer_pam_query
},
237 {MONITOR_REQ_PAM_RESPOND
, MON_ISAUTH
, mm_answer_pam_respond
},
238 {MONITOR_REQ_PAM_FREE_CTX
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_pam_free_ctx
},
240 #ifdef SSH_AUDIT_EVENTS
241 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
244 {MONITOR_REQ_BSDAUTHQUERY
, MON_ISAUTH
, mm_answer_bsdauthquery
},
245 {MONITOR_REQ_BSDAUTHRESPOND
, MON_AUTH
, mm_answer_bsdauthrespond
},
248 {MONITOR_REQ_SKEYQUERY
, MON_ISAUTH
, mm_answer_skeyquery
},
249 {MONITOR_REQ_SKEYRESPOND
, MON_AUTH
, mm_answer_skeyrespond
},
251 {MONITOR_REQ_KEYALLOWED
, MON_ISAUTH
, mm_answer_keyallowed
},
252 {MONITOR_REQ_KEYVERIFY
, MON_AUTH
, mm_answer_keyverify
},
254 {MONITOR_REQ_GSSSETUP
, MON_ISAUTH
, mm_answer_gss_setup_ctx
},
255 {MONITOR_REQ_GSSSTEP
, MON_ISAUTH
, mm_answer_gss_accept_ctx
},
256 {MONITOR_REQ_GSSUSEROK
, MON_AUTH
, mm_answer_gss_userok
},
257 {MONITOR_REQ_GSSCHECKMIC
, MON_ISAUTH
, mm_answer_gss_checkmic
},
262 struct mon_table mon_dispatch_postauth20
[] = {
264 {MONITOR_REQ_MODULI
, 0, mm_answer_moduli
},
266 {MONITOR_REQ_SIGN
, 0, mm_answer_sign
},
267 {MONITOR_REQ_PTY
, 0, mm_answer_pty
},
268 {MONITOR_REQ_PTYCLEANUP
, 0, mm_answer_pty_cleanup
},
269 {MONITOR_REQ_TERM
, 0, mm_answer_term
},
270 #ifdef SSH_AUDIT_EVENTS
271 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
272 {MONITOR_REQ_AUDIT_COMMAND
, MON_PERMIT
, mm_answer_audit_command
},
277 struct mon_table mon_dispatch_proto15
[] = {
279 {MONITOR_REQ_PWNAM
, MON_ONCE
, mm_answer_pwnamallow
},
280 {MONITOR_REQ_SESSKEY
, MON_ONCE
, mm_answer_sesskey
},
281 {MONITOR_REQ_SESSID
, MON_ONCE
, mm_answer_sessid
},
282 {MONITOR_REQ_AUTHPASSWORD
, MON_AUTH
, mm_answer_authpassword
},
283 {MONITOR_REQ_RSAKEYALLOWED
, MON_ISAUTH
|MON_ALOG
, mm_answer_rsa_keyallowed
},
284 {MONITOR_REQ_KEYALLOWED
, MON_ISAUTH
|MON_ALOG
, mm_answer_keyallowed
},
285 {MONITOR_REQ_RSACHALLENGE
, MON_ONCE
, mm_answer_rsa_challenge
},
286 {MONITOR_REQ_RSARESPONSE
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_rsa_response
},
288 {MONITOR_REQ_BSDAUTHQUERY
, MON_ISAUTH
, mm_answer_bsdauthquery
},
289 {MONITOR_REQ_BSDAUTHRESPOND
, MON_AUTH
, mm_answer_bsdauthrespond
},
292 {MONITOR_REQ_SKEYQUERY
, MON_ISAUTH
, mm_answer_skeyquery
},
293 {MONITOR_REQ_SKEYRESPOND
, MON_AUTH
, mm_answer_skeyrespond
},
296 {MONITOR_REQ_PAM_START
, MON_ONCE
, mm_answer_pam_start
},
297 {MONITOR_REQ_PAM_ACCOUNT
, 0, mm_answer_pam_account
},
298 {MONITOR_REQ_PAM_INIT_CTX
, MON_ISAUTH
, mm_answer_pam_init_ctx
},
299 {MONITOR_REQ_PAM_QUERY
, MON_ISAUTH
, mm_answer_pam_query
},
300 {MONITOR_REQ_PAM_RESPOND
, MON_ISAUTH
, mm_answer_pam_respond
},
301 {MONITOR_REQ_PAM_FREE_CTX
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_pam_free_ctx
},
303 #ifdef SSH_AUDIT_EVENTS
304 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
306 #endif /* WITH_SSH1 */
310 struct mon_table mon_dispatch_postauth15
[] = {
312 {MONITOR_REQ_PTY
, MON_ONCE
, mm_answer_pty
},
313 {MONITOR_REQ_PTYCLEANUP
, MON_ONCE
, mm_answer_pty_cleanup
},
314 {MONITOR_REQ_TERM
, 0, mm_answer_term
},
315 #ifdef SSH_AUDIT_EVENTS
316 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
317 {MONITOR_REQ_AUDIT_COMMAND
, MON_PERMIT
|MON_ONCE
, mm_answer_audit_command
},
319 #endif /* WITH_SSH1 */
323 struct mon_table
*mon_dispatch
;
325 /* Specifies if a certain message is allowed at the moment */
328 monitor_permit(struct mon_table
*ent
, enum monitor_reqtype type
, int permit
)
330 while (ent
->f
!= NULL
) {
331 if (ent
->type
== type
) {
332 ent
->flags
&= ~MON_PERMIT
;
333 ent
->flags
|= permit
? MON_PERMIT
: 0;
341 monitor_permit_authentications(int permit
)
343 struct mon_table
*ent
= mon_dispatch
;
345 while (ent
->f
!= NULL
) {
346 if (ent
->flags
& MON_AUTH
) {
347 ent
->flags
&= ~MON_PERMIT
;
348 ent
->flags
|= permit
? MON_PERMIT
: 0;
355 monitor_child_preauth(Authctxt
*_authctxt
, struct monitor
*pmonitor
)
357 struct mon_table
*ent
;
358 int authenticated
= 0, partial
= 0;
360 debug3("preauth child monitor started");
362 close(pmonitor
->m_recvfd
);
363 close(pmonitor
->m_log_sendfd
);
364 pmonitor
->m_log_sendfd
= pmonitor
->m_recvfd
= -1;
366 authctxt
= _authctxt
;
367 memset(authctxt
, 0, sizeof(*authctxt
));
369 authctxt
->loginmsg
= &loginmsg
;
372 mon_dispatch
= mon_dispatch_proto20
;
374 /* Permit requests for moduli and signatures */
375 monitor_permit(mon_dispatch
, MONITOR_REQ_MODULI
, 1);
376 monitor_permit(mon_dispatch
, MONITOR_REQ_SIGN
, 1);
378 mon_dispatch
= mon_dispatch_proto15
;
380 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSKEY
, 1);
383 /* The first few requests do not require asynchronous access */
384 while (!authenticated
) {
386 auth_method
= "unknown";
387 auth_submethod
= NULL
;
388 authenticated
= (monitor_read(pmonitor
, mon_dispatch
, &ent
) == 1);
390 /* Special handling for multiple required authentications */
391 if (options
.num_auth_methods
!= 0) {
393 fatal("AuthenticationMethods is not supported"
394 "with SSH protocol 1");
396 !auth2_update_methods_lists(authctxt
,
397 auth_method
, auth_submethod
)) {
398 debug3("%s: method %s: partial", __func__
,
406 if (!(ent
->flags
& MON_AUTHDECIDE
))
407 fatal("%s: unexpected authentication from %d",
408 __func__
, ent
->type
);
409 if (authctxt
->pw
->pw_uid
== 0 &&
410 !auth_root_allowed(auth_method
))
413 /* PAM needs to perform account checks after auth */
414 if (options
.use_pam
&& authenticated
) {
418 mm_request_receive_expect(pmonitor
->m_sendfd
,
419 MONITOR_REQ_PAM_ACCOUNT
, &m
);
420 authenticated
= mm_answer_pam_account(pmonitor
->m_sendfd
, &m
);
425 if (ent
->flags
& (MON_AUTHDECIDE
|MON_ALOG
)) {
426 auth_log(authctxt
, authenticated
, partial
,
427 auth_method
, auth_submethod
);
429 authctxt
->failures
++;
433 if (!authctxt
->valid
)
434 fatal("%s: authenticated invalid user", __func__
);
435 if (strcmp(auth_method
, "unknown") == 0)
436 fatal("%s: authentication method name unknown", __func__
);
438 debug("%s: %s has been authenticated by privileged process",
439 __func__
, authctxt
->user
);
441 mm_get_keystate(pmonitor
);
443 /* Drain any buffered messages from the child */
444 while (pmonitor
->m_log_recvfd
!= -1 && monitor_read_log(pmonitor
) == 0)
447 close(pmonitor
->m_sendfd
);
448 close(pmonitor
->m_log_recvfd
);
449 pmonitor
->m_sendfd
= pmonitor
->m_log_recvfd
= -1;
453 monitor_set_child_handler(pid_t pid
)
455 monitor_child_pid
= pid
;
459 monitor_child_handler(int sig
)
461 kill(monitor_child_pid
, sig
);
465 monitor_child_postauth(struct monitor
*pmonitor
)
467 close(pmonitor
->m_recvfd
);
468 pmonitor
->m_recvfd
= -1;
470 monitor_set_child_handler(pmonitor
->m_pid
);
471 signal(SIGHUP
, &monitor_child_handler
);
472 signal(SIGTERM
, &monitor_child_handler
);
473 signal(SIGINT
, &monitor_child_handler
);
475 signal(SIGXFSZ
, SIG_IGN
);
479 mon_dispatch
= mon_dispatch_postauth20
;
481 /* Permit requests for moduli and signatures */
482 monitor_permit(mon_dispatch
, MONITOR_REQ_MODULI
, 1);
483 monitor_permit(mon_dispatch
, MONITOR_REQ_SIGN
, 1);
484 monitor_permit(mon_dispatch
, MONITOR_REQ_TERM
, 1);
486 mon_dispatch
= mon_dispatch_postauth15
;
487 monitor_permit(mon_dispatch
, MONITOR_REQ_TERM
, 1);
490 monitor_permit(mon_dispatch
, MONITOR_REQ_PTY
, 1);
491 monitor_permit(mon_dispatch
, MONITOR_REQ_PTYCLEANUP
, 1);
495 monitor_read(pmonitor
, mon_dispatch
, NULL
);
499 monitor_sync(struct monitor
*pmonitor
)
501 if (options
.compression
) {
502 /* The member allocation is not visible, so sync it */
503 mm_share_sync(&pmonitor
->m_zlib
, &pmonitor
->m_zback
);
508 monitor_read_log(struct monitor
*pmonitor
)
514 buffer_init(&logmsg
);
517 buffer_append_space(&logmsg
, 4);
518 if (atomicio(read
, pmonitor
->m_log_recvfd
,
519 buffer_ptr(&logmsg
), buffer_len(&logmsg
)) != buffer_len(&logmsg
)) {
520 if (errno
== EPIPE
) {
521 buffer_free(&logmsg
);
522 debug("%s: child log fd closed", __func__
);
523 close(pmonitor
->m_log_recvfd
);
524 pmonitor
->m_log_recvfd
= -1;
527 fatal("%s: log fd read: %s", __func__
, strerror(errno
));
529 len
= buffer_get_int(&logmsg
);
530 if (len
<= 4 || len
> 8192)
531 fatal("%s: invalid log message length %u", __func__
, len
);
533 /* Read severity, message */
534 buffer_clear(&logmsg
);
535 buffer_append_space(&logmsg
, len
);
536 if (atomicio(read
, pmonitor
->m_log_recvfd
,
537 buffer_ptr(&logmsg
), buffer_len(&logmsg
)) != buffer_len(&logmsg
))
538 fatal("%s: log fd read: %s", __func__
, strerror(errno
));
541 level
= buffer_get_int(&logmsg
);
542 msg
= buffer_get_string(&logmsg
, NULL
);
543 if (log_level_name(level
) == NULL
)
544 fatal("%s: invalid log level %u (corrupted message?)",
546 do_log2(level
, "%s [preauth]", msg
);
548 buffer_free(&logmsg
);
555 monitor_read(struct monitor
*pmonitor
, struct mon_table
*ent
,
556 struct mon_table
**pent
)
561 struct pollfd pfd
[2];
564 memset(&pfd
, 0, sizeof(pfd
));
565 pfd
[0].fd
= pmonitor
->m_sendfd
;
566 pfd
[0].events
= POLLIN
;
567 pfd
[1].fd
= pmonitor
->m_log_recvfd
;
568 pfd
[1].events
= pfd
[1].fd
== -1 ? 0 : POLLIN
;
569 if (poll(pfd
, pfd
[1].fd
== -1 ? 1 : 2, -1) == -1) {
570 if (errno
== EINTR
|| errno
== EAGAIN
)
572 fatal("%s: poll: %s", __func__
, strerror(errno
));
574 if (pfd
[1].revents
) {
576 * Drain all log messages before processing next
579 monitor_read_log(pmonitor
);
583 break; /* Continues below */
588 mm_request_receive(pmonitor
->m_sendfd
, &m
);
589 type
= buffer_get_char(&m
);
591 debug3("%s: checking request %d", __func__
, type
);
593 while (ent
->f
!= NULL
) {
594 if (ent
->type
== type
)
599 if (ent
->f
!= NULL
) {
600 if (!(ent
->flags
& MON_PERMIT
))
601 fatal("%s: unpermitted request %d", __func__
,
603 ret
= (*ent
->f
)(pmonitor
->m_sendfd
, &m
);
606 /* The child may use this request only once, disable it */
607 if (ent
->flags
& MON_ONCE
) {
608 debug2("%s: %d used once, disabling now", __func__
,
610 ent
->flags
&= ~MON_PERMIT
;
619 fatal("%s: unsupported request: %d", __func__
, type
);
625 /* allowed key state */
627 monitor_allowed_key(u_char
*blob
, u_int bloblen
)
629 /* make sure key is allowed */
630 if (key_blob
== NULL
|| key_bloblen
!= bloblen
||
631 timingsafe_bcmp(key_blob
, blob
, key_bloblen
))
637 monitor_reset_key_state(void)
641 free(hostbased_cuser
);
642 free(hostbased_chost
);
645 key_blobtype
= MM_NOKEY
;
646 hostbased_cuser
= NULL
;
647 hostbased_chost
= NULL
;
652 mm_answer_moduli(int sock
, Buffer
*m
)
657 min
= buffer_get_int(m
);
658 want
= buffer_get_int(m
);
659 max
= buffer_get_int(m
);
661 debug3("%s: got parameters: %d %d %d",
662 __func__
, min
, want
, max
);
663 /* We need to check here, too, in case the child got corrupted */
664 if (max
< min
|| want
< min
|| max
< want
)
665 fatal("%s: bad parameters: %d %d %d",
666 __func__
, min
, want
, max
);
670 dh
= choose_dh(min
, want
, max
);
672 buffer_put_char(m
, 0);
675 /* Send first bignum */
676 buffer_put_char(m
, 1);
677 buffer_put_bignum2(m
, dh
->p
);
678 buffer_put_bignum2(m
, dh
->g
);
682 mm_request_send(sock
, MONITOR_ANS_MODULI
, m
);
687 extern AuthenticationConnection
*auth_conn
;
690 mm_answer_sign(int sock
, Buffer
*m
)
695 u_int siglen
, datlen
;
698 debug3("%s", __func__
);
700 keyid
= buffer_get_int(m
);
701 p
= buffer_get_string(m
, &datlen
);
704 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
705 * SHA384 (48 bytes) and SHA512 (64 bytes).
707 if (datlen
!= 20 && datlen
!= 32 && datlen
!= 48 && datlen
!= 64)
708 fatal("%s: data length incorrect: %u", __func__
, datlen
);
710 /* save session id, it will be passed on the first call */
711 if (session_id2_len
== 0) {
712 session_id2_len
= datlen
;
713 session_id2
= xmalloc(session_id2_len
);
714 memcpy(session_id2
, p
, session_id2_len
);
717 if ((key
= get_hostkey_by_index(keyid
)) != NULL
) {
718 if (key_sign(key
, &signature
, &siglen
, p
, datlen
) < 0)
719 fatal("%s: key_sign failed", __func__
);
720 } else if ((key
= get_hostkey_public_by_index(keyid
)) != NULL
&&
722 if (ssh_agent_sign(auth_conn
, key
, &signature
, &siglen
, p
,
724 fatal("%s: ssh_agent_sign failed", __func__
);
726 fatal("%s: no hostkey from index %d", __func__
, keyid
);
728 debug3("%s: signature %p(%u)", __func__
, signature
, siglen
);
731 buffer_put_string(m
, signature
, siglen
);
736 mm_request_send(sock
, MONITOR_ANS_SIGN
, m
);
738 /* Turn on permissions for getpwnam */
739 monitor_permit(mon_dispatch
, MONITOR_REQ_PWNAM
, 1);
744 /* Retrieves the password entry and also checks if the user is permitted */
747 mm_answer_pwnamallow(int sock
, Buffer
*m
)
750 struct passwd
*pwent
;
754 debug3("%s", __func__
);
756 if (authctxt
->attempt
++ != 0)
757 fatal("%s: multiple attempts for getpwnam", __func__
);
759 username
= buffer_get_string(m
, NULL
);
761 pwent
= getpwnamallow(username
);
763 authctxt
->user
= xstrdup(username
);
764 setproctitle("%s [priv]", pwent
? username
: "unknown");
770 buffer_put_char(m
, 0);
771 authctxt
->pw
= fakepw();
776 authctxt
->pw
= pwent
;
779 buffer_put_char(m
, 1);
780 buffer_put_string(m
, pwent
, sizeof(struct passwd
));
781 buffer_put_cstring(m
, pwent
->pw_name
);
782 buffer_put_cstring(m
, "*");
783 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
784 buffer_put_cstring(m
, pwent
->pw_gecos
);
786 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
787 buffer_put_cstring(m
, pwent
->pw_class
);
789 buffer_put_cstring(m
, pwent
->pw_dir
);
790 buffer_put_cstring(m
, pwent
->pw_shell
);
793 buffer_put_string(m
, &options
, sizeof(options
));
795 #define M_CP_STROPT(x) do { \
796 if (options.x != NULL) \
797 buffer_put_cstring(m, options.x); \
799 #define M_CP_STRARRAYOPT(x, nx) do { \
800 for (i = 0; i < options.nx; i++) \
801 buffer_put_cstring(m, options.x[i]); \
803 /* See comment in servconf.h */
804 COPY_MATCH_STRING_OPTS();
806 #undef M_CP_STRARRAYOPT
808 /* Create valid auth method lists */
809 if (compat20
&& auth2_setup_methods_lists(authctxt
) != 0) {
811 * The monitor will continue long enough to let the child
812 * run to it's packet_disconnect(), but it must not allow any
813 * authentication to succeed.
815 debug("%s: no valid authentication method lists", __func__
);
818 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__
, allowed
);
819 mm_request_send(sock
, MONITOR_ANS_PWNAM
, m
);
821 /* For SSHv1 allow authentication now */
823 monitor_permit_authentications(1);
825 /* Allow service/style information on the auth context */
826 monitor_permit(mon_dispatch
, MONITOR_REQ_AUTHSERV
, 1);
827 monitor_permit(mon_dispatch
, MONITOR_REQ_AUTH2_READ_BANNER
, 1);
831 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_START
, 1);
837 int mm_answer_auth2_read_banner(int sock
, Buffer
*m
)
842 banner
= auth2_read_banner();
843 buffer_put_cstring(m
, banner
!= NULL
? banner
: "");
844 mm_request_send(sock
, MONITOR_ANS_AUTH2_READ_BANNER
, m
);
851 mm_answer_authserv(int sock
, Buffer
*m
)
853 monitor_permit_authentications(1);
855 authctxt
->service
= buffer_get_string(m
, NULL
);
856 authctxt
->style
= buffer_get_string(m
, NULL
);
857 debug3("%s: service=%s, style=%s",
858 __func__
, authctxt
->service
, authctxt
->style
);
860 if (strlen(authctxt
->style
) == 0) {
861 free(authctxt
->style
);
862 authctxt
->style
= NULL
;
869 mm_answer_authpassword(int sock
, Buffer
*m
)
871 static int call_count
;
876 passwd
= buffer_get_string(m
, &plen
);
877 /* Only authenticate if the context is valid */
878 authenticated
= options
.password_authentication
&&
879 auth_password(authctxt
, passwd
);
880 explicit_bzero(passwd
, strlen(passwd
));
884 buffer_put_int(m
, authenticated
);
886 debug3("%s: sending result %d", __func__
, authenticated
);
887 mm_request_send(sock
, MONITOR_ANS_AUTHPASSWORD
, m
);
890 if (plen
== 0 && call_count
== 1)
891 auth_method
= "none";
893 auth_method
= "password";
895 /* Causes monitor loop to terminate if authenticated */
896 return (authenticated
);
901 mm_answer_bsdauthquery(int sock
, Buffer
*m
)
903 char *name
, *infotxt
;
909 success
= bsdauth_query(authctxt
, &name
, &infotxt
, &numprompts
,
910 &prompts
, &echo_on
) < 0 ? 0 : 1;
913 buffer_put_int(m
, success
);
915 buffer_put_cstring(m
, prompts
[0]);
917 debug3("%s: sending challenge success: %u", __func__
, success
);
918 mm_request_send(sock
, MONITOR_ANS_BSDAUTHQUERY
, m
);
931 mm_answer_bsdauthrespond(int sock
, Buffer
*m
)
936 if (authctxt
->as
== 0)
937 fatal("%s: no bsd auth session", __func__
);
939 response
= buffer_get_string(m
, NULL
);
940 authok
= options
.challenge_response_authentication
&&
941 auth_userresponse(authctxt
->as
, response
, 0);
943 debug3("%s: <%s> = <%d>", __func__
, response
, authok
);
947 buffer_put_int(m
, authok
);
949 debug3("%s: sending authenticated: %d", __func__
, authok
);
950 mm_request_send(sock
, MONITOR_ANS_BSDAUTHRESPOND
, m
);
953 auth_method
= "keyboard-interactive";
954 auth_submethod
= "bsdauth";
956 auth_method
= "bsdauth";
958 return (authok
!= 0);
964 mm_answer_skeyquery(int sock
, Buffer
*m
)
967 char challenge
[1024];
970 success
= _compat_skeychallenge(&skey
, authctxt
->user
, challenge
,
971 sizeof(challenge
)) < 0 ? 0 : 1;
974 buffer_put_int(m
, success
);
976 buffer_put_cstring(m
, challenge
);
978 debug3("%s: sending challenge success: %u", __func__
, success
);
979 mm_request_send(sock
, MONITOR_ANS_SKEYQUERY
, m
);
985 mm_answer_skeyrespond(int sock
, Buffer
*m
)
990 response
= buffer_get_string(m
, NULL
);
992 authok
= (options
.challenge_response_authentication
&&
994 skey_haskey(authctxt
->pw
->pw_name
) == 0 &&
995 skey_passcheck(authctxt
->pw
->pw_name
, response
) != -1);
1000 buffer_put_int(m
, authok
);
1002 debug3("%s: sending authenticated: %d", __func__
, authok
);
1003 mm_request_send(sock
, MONITOR_ANS_SKEYRESPOND
, m
);
1005 auth_method
= "skey";
1007 return (authok
!= 0);
1013 mm_answer_pam_start(int sock
, Buffer
*m
)
1015 if (!options
.use_pam
)
1016 fatal("UsePAM not set, but ended up in %s anyway", __func__
);
1018 start_pam(authctxt
);
1020 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_ACCOUNT
, 1);
1026 mm_answer_pam_account(int sock
, Buffer
*m
)
1030 if (!options
.use_pam
)
1031 fatal("UsePAM not set, but ended up in %s anyway", __func__
);
1033 ret
= do_pam_account();
1035 buffer_put_int(m
, ret
);
1036 buffer_put_string(m
, buffer_ptr(&loginmsg
), buffer_len(&loginmsg
));
1038 mm_request_send(sock
, MONITOR_ANS_PAM_ACCOUNT
, m
);
1043 static void *sshpam_ctxt
, *sshpam_authok
;
1044 extern KbdintDevice sshpam_device
;
1047 mm_answer_pam_init_ctx(int sock
, Buffer
*m
)
1050 debug3("%s", __func__
);
1051 authctxt
->user
= buffer_get_string(m
, NULL
);
1052 sshpam_ctxt
= (sshpam_device
.init_ctx
)(authctxt
);
1053 sshpam_authok
= NULL
;
1055 if (sshpam_ctxt
!= NULL
) {
1056 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_FREE_CTX
, 1);
1057 buffer_put_int(m
, 1);
1059 buffer_put_int(m
, 0);
1061 mm_request_send(sock
, MONITOR_ANS_PAM_INIT_CTX
, m
);
1066 mm_answer_pam_query(int sock
, Buffer
*m
)
1068 char *name
= NULL
, *info
= NULL
, **prompts
= NULL
;
1069 u_int i
, num
= 0, *echo_on
= 0;
1072 debug3("%s", __func__
);
1073 sshpam_authok
= NULL
;
1074 ret
= (sshpam_device
.query
)(sshpam_ctxt
, &name
, &info
, &num
, &prompts
, &echo_on
);
1075 if (ret
== 0 && num
== 0)
1076 sshpam_authok
= sshpam_ctxt
;
1077 if (num
> 1 || name
== NULL
|| info
== NULL
)
1080 buffer_put_int(m
, ret
);
1081 buffer_put_cstring(m
, name
);
1083 buffer_put_cstring(m
, info
);
1085 buffer_put_int(m
, num
);
1086 for (i
= 0; i
< num
; ++i
) {
1087 buffer_put_cstring(m
, prompts
[i
]);
1089 buffer_put_int(m
, echo_on
[i
]);
1093 auth_method
= "keyboard-interactive";
1094 auth_submethod
= "pam";
1095 mm_request_send(sock
, MONITOR_ANS_PAM_QUERY
, m
);
1100 mm_answer_pam_respond(int sock
, Buffer
*m
)
1106 debug3("%s", __func__
);
1107 sshpam_authok
= NULL
;
1108 num
= buffer_get_int(m
);
1110 resp
= xcalloc(num
, sizeof(char *));
1111 for (i
= 0; i
< num
; ++i
)
1112 resp
[i
] = buffer_get_string(m
, NULL
);
1113 ret
= (sshpam_device
.respond
)(sshpam_ctxt
, num
, resp
);
1114 for (i
= 0; i
< num
; ++i
)
1118 ret
= (sshpam_device
.respond
)(sshpam_ctxt
, num
, NULL
);
1121 buffer_put_int(m
, ret
);
1122 mm_request_send(sock
, MONITOR_ANS_PAM_RESPOND
, m
);
1123 auth_method
= "keyboard-interactive";
1124 auth_submethod
= "pam";
1126 sshpam_authok
= sshpam_ctxt
;
1131 mm_answer_pam_free_ctx(int sock
, Buffer
*m
)
1134 debug3("%s", __func__
);
1135 (sshpam_device
.free_ctx
)(sshpam_ctxt
);
1137 mm_request_send(sock
, MONITOR_ANS_PAM_FREE_CTX
, m
);
1138 auth_method
= "keyboard-interactive";
1139 auth_submethod
= "pam";
1140 return (sshpam_authok
== sshpam_ctxt
);
1145 mm_answer_keyallowed(int sock
, Buffer
*m
)
1148 char *cuser
, *chost
;
1151 enum mm_keytype type
= 0;
1154 debug3("%s entering", __func__
);
1156 type
= buffer_get_int(m
);
1157 cuser
= buffer_get_string(m
, NULL
);
1158 chost
= buffer_get_string(m
, NULL
);
1159 blob
= buffer_get_string(m
, &bloblen
);
1161 key
= key_from_blob(blob
, bloblen
);
1163 if ((compat20
&& type
== MM_RSAHOSTKEY
) ||
1164 (!compat20
&& type
!= MM_RSAHOSTKEY
))
1165 fatal("%s: key type and protocol mismatch", __func__
);
1167 debug3("%s: key_from_blob: %p", __func__
, key
);
1169 if (key
!= NULL
&& authctxt
->valid
) {
1172 allowed
= options
.pubkey_authentication
&&
1173 user_key_allowed(authctxt
->pw
, key
);
1174 pubkey_auth_info(authctxt
, key
, NULL
);
1175 auth_method
= "publickey";
1176 if (options
.pubkey_authentication
&& allowed
!= 1)
1177 auth_clear_options();
1180 allowed
= options
.hostbased_authentication
&&
1181 hostbased_key_allowed(authctxt
->pw
,
1183 pubkey_auth_info(authctxt
, key
,
1184 "client user \"%.100s\", client host \"%.100s\"",
1186 auth_method
= "hostbased";
1190 key
->type
= KEY_RSA1
; /* XXX */
1191 allowed
= options
.rhosts_rsa_authentication
&&
1192 auth_rhosts_rsa_key_allowed(authctxt
->pw
,
1194 if (options
.rhosts_rsa_authentication
&& allowed
!= 1)
1195 auth_clear_options();
1196 auth_method
= "rsa";
1200 fatal("%s: unknown key type %d", __func__
, type
);
1207 /* clear temporarily storage (used by verify) */
1208 monitor_reset_key_state();
1211 /* Save temporarily for comparison in verify */
1213 key_bloblen
= bloblen
;
1214 key_blobtype
= type
;
1215 hostbased_cuser
= cuser
;
1216 hostbased_chost
= chost
;
1218 /* Log failed attempt */
1219 auth_log(authctxt
, 0, 0, auth_method
, NULL
);
1225 debug3("%s: key %p is %s",
1226 __func__
, key
, allowed
? "allowed" : "not allowed");
1229 buffer_put_int(m
, allowed
);
1230 buffer_put_int(m
, forced_command
!= NULL
);
1232 mm_request_send(sock
, MONITOR_ANS_KEYALLOWED
, m
);
1234 if (type
== MM_RSAHOSTKEY
)
1235 monitor_permit(mon_dispatch
, MONITOR_REQ_RSACHALLENGE
, allowed
);
1241 monitor_valid_userblob(u_char
*data
, u_int datalen
)
1244 char *p
, *userstyle
;
1249 buffer_append(&b
, data
, datalen
);
1251 if (datafellows
& SSH_OLD_SESSIONID
) {
1253 len
= buffer_len(&b
);
1254 if ((session_id2
== NULL
) ||
1255 (len
< session_id2_len
) ||
1256 (timingsafe_bcmp(p
, session_id2
, session_id2_len
) != 0))
1258 buffer_consume(&b
, session_id2_len
);
1260 p
= buffer_get_string(&b
, &len
);
1261 if ((session_id2
== NULL
) ||
1262 (len
!= session_id2_len
) ||
1263 (timingsafe_bcmp(p
, session_id2
, session_id2_len
) != 0))
1267 if (buffer_get_char(&b
) != SSH2_MSG_USERAUTH_REQUEST
)
1269 p
= buffer_get_cstring(&b
, NULL
);
1270 xasprintf(&userstyle
, "%s%s%s", authctxt
->user
,
1271 authctxt
->style
? ":" : "",
1272 authctxt
->style
? authctxt
->style
: "");
1273 if (strcmp(userstyle
, p
) != 0) {
1274 logit("wrong user name passed to monitor: expected %s != %.100s",
1280 buffer_skip_string(&b
);
1281 if (datafellows
& SSH_BUG_PKAUTH
) {
1282 if (!buffer_get_char(&b
))
1285 p
= buffer_get_cstring(&b
, NULL
);
1286 if (strcmp("publickey", p
) != 0)
1289 if (!buffer_get_char(&b
))
1291 buffer_skip_string(&b
);
1293 buffer_skip_string(&b
);
1294 if (buffer_len(&b
) != 0)
1301 monitor_valid_hostbasedblob(u_char
*data
, u_int datalen
, char *cuser
,
1305 char *p
, *userstyle
;
1310 buffer_append(&b
, data
, datalen
);
1312 p
= buffer_get_string(&b
, &len
);
1313 if ((session_id2
== NULL
) ||
1314 (len
!= session_id2_len
) ||
1315 (timingsafe_bcmp(p
, session_id2
, session_id2_len
) != 0))
1319 if (buffer_get_char(&b
) != SSH2_MSG_USERAUTH_REQUEST
)
1321 p
= buffer_get_cstring(&b
, NULL
);
1322 xasprintf(&userstyle
, "%s%s%s", authctxt
->user
,
1323 authctxt
->style
? ":" : "",
1324 authctxt
->style
? authctxt
->style
: "");
1325 if (strcmp(userstyle
, p
) != 0) {
1326 logit("wrong user name passed to monitor: expected %s != %.100s",
1332 buffer_skip_string(&b
); /* service */
1333 p
= buffer_get_cstring(&b
, NULL
);
1334 if (strcmp(p
, "hostbased") != 0)
1337 buffer_skip_string(&b
); /* pkalg */
1338 buffer_skip_string(&b
); /* pkblob */
1340 /* verify client host, strip trailing dot if necessary */
1341 p
= buffer_get_string(&b
, NULL
);
1342 if (((len
= strlen(p
)) > 0) && p
[len
- 1] == '.')
1344 if (strcmp(p
, chost
) != 0)
1348 /* verify client user */
1349 p
= buffer_get_string(&b
, NULL
);
1350 if (strcmp(p
, cuser
) != 0)
1354 if (buffer_len(&b
) != 0)
1361 mm_answer_keyverify(int sock
, Buffer
*m
)
1364 u_char
*signature
, *data
, *blob
;
1365 u_int signaturelen
, datalen
, bloblen
;
1369 blob
= buffer_get_string(m
, &bloblen
);
1370 signature
= buffer_get_string(m
, &signaturelen
);
1371 data
= buffer_get_string(m
, &datalen
);
1373 if (hostbased_cuser
== NULL
|| hostbased_chost
== NULL
||
1374 !monitor_allowed_key(blob
, bloblen
))
1375 fatal("%s: bad key, not previously allowed", __func__
);
1377 key
= key_from_blob(blob
, bloblen
);
1379 fatal("%s: bad public key blob", __func__
);
1381 switch (key_blobtype
) {
1383 valid_data
= monitor_valid_userblob(data
, datalen
);
1386 valid_data
= monitor_valid_hostbasedblob(data
, datalen
,
1387 hostbased_cuser
, hostbased_chost
);
1394 fatal("%s: bad signature data blob", __func__
);
1396 verified
= key_verify(key
, signature
, signaturelen
, data
, datalen
);
1397 debug3("%s: key %p signature %s",
1398 __func__
, key
, (verified
== 1) ? "verified" : "unverified");
1405 auth_method
= key_blobtype
== MM_USERKEY
? "publickey" : "hostbased";
1407 monitor_reset_key_state();
1410 buffer_put_int(m
, verified
);
1411 mm_request_send(sock
, MONITOR_ANS_KEYVERIFY
, m
);
1413 return (verified
== 1);
1417 mm_record_login(Session
*s
, struct passwd
*pw
)
1420 struct sockaddr_storage from
;
1423 * Get IP address of client. If the connection is not a socket, let
1424 * the address be 0.0.0.0.
1426 memset(&from
, 0, sizeof(from
));
1427 fromlen
= sizeof(from
);
1428 if (packet_connection_is_on_socket()) {
1429 if (getpeername(packet_get_connection_in(),
1430 (struct sockaddr
*)&from
, &fromlen
) < 0) {
1431 debug("getpeername: %.100s", strerror(errno
));
1435 /* Record that there was a login on that tty from the remote host. */
1436 record_login(s
->pid
, s
->tty
, pw
->pw_name
, pw
->pw_uid
,
1437 get_remote_name_or_ip(utmp_len
, options
.use_dns
),
1438 (struct sockaddr
*)&from
, fromlen
);
1442 mm_session_close(Session
*s
)
1444 debug3("%s: session %d pid %ld", __func__
, s
->self
, (long)s
->pid
);
1445 if (s
->ttyfd
!= -1) {
1446 debug3("%s: tty %s ptyfd %d", __func__
, s
->tty
, s
->ptyfd
);
1447 session_pty_cleanup2(s
);
1449 session_unused(s
->self
);
1453 mm_answer_pty(int sock
, Buffer
*m
)
1455 extern struct monitor
*pmonitor
;
1459 debug3("%s entering", __func__
);
1465 s
->authctxt
= authctxt
;
1466 s
->pw
= authctxt
->pw
;
1467 s
->pid
= pmonitor
->m_pid
;
1468 res
= pty_allocate(&s
->ptyfd
, &s
->ttyfd
, s
->tty
, sizeof(s
->tty
));
1471 pty_setowner(authctxt
->pw
, s
->tty
);
1473 buffer_put_int(m
, 1);
1474 buffer_put_cstring(m
, s
->tty
);
1476 /* We need to trick ttyslot */
1477 if (dup2(s
->ttyfd
, 0) == -1)
1478 fatal("%s: dup2", __func__
);
1480 mm_record_login(s
, authctxt
->pw
);
1482 /* Now we can close the file descriptor again */
1485 /* send messages generated by record_login */
1486 buffer_put_string(m
, buffer_ptr(&loginmsg
), buffer_len(&loginmsg
));
1487 buffer_clear(&loginmsg
);
1489 mm_request_send(sock
, MONITOR_ANS_PTY
, m
);
1491 if (mm_send_fd(sock
, s
->ptyfd
) == -1 ||
1492 mm_send_fd(sock
, s
->ttyfd
) == -1)
1493 fatal("%s: send fds failed", __func__
);
1495 /* make sure nothing uses fd 0 */
1496 if ((fd0
= open(_PATH_DEVNULL
, O_RDONLY
)) < 0)
1497 fatal("%s: open(/dev/null): %s", __func__
, strerror(errno
));
1499 error("%s: fd0 %d != 0", __func__
, fd0
);
1501 /* slave is not needed */
1503 s
->ttyfd
= s
->ptyfd
;
1504 /* no need to dup() because nobody closes ptyfd */
1505 s
->ptymaster
= s
->ptyfd
;
1507 debug3("%s: tty %s ptyfd %d", __func__
, s
->tty
, s
->ttyfd
);
1513 mm_session_close(s
);
1514 buffer_put_int(m
, 0);
1515 mm_request_send(sock
, MONITOR_ANS_PTY
, m
);
1520 mm_answer_pty_cleanup(int sock
, Buffer
*m
)
1525 debug3("%s entering", __func__
);
1527 tty
= buffer_get_string(m
, NULL
);
1528 if ((s
= session_by_tty(tty
)) != NULL
)
1529 mm_session_close(s
);
1537 mm_answer_sesskey(int sock
, Buffer
*m
)
1542 /* Turn off permissions */
1543 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSKEY
, 0);
1545 if ((p
= BN_new()) == NULL
)
1546 fatal("%s: BN_new", __func__
);
1548 buffer_get_bignum2(m
, p
);
1550 rsafail
= ssh1_session_key(p
);
1553 buffer_put_int(m
, rsafail
);
1554 buffer_put_bignum2(m
, p
);
1558 mm_request_send(sock
, MONITOR_ANS_SESSKEY
, m
);
1560 /* Turn on permissions for sessid passing */
1561 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSID
, 1);
1567 mm_answer_sessid(int sock
, Buffer
*m
)
1571 debug3("%s entering", __func__
);
1573 if (buffer_len(m
) != 16)
1574 fatal("%s: bad ssh1 session id", __func__
);
1575 for (i
= 0; i
< 16; i
++)
1576 session_id
[i
] = buffer_get_char(m
);
1578 /* Turn on permissions for getpwnam */
1579 monitor_permit(mon_dispatch
, MONITOR_REQ_PWNAM
, 1);
1585 mm_answer_rsa_keyallowed(int sock
, Buffer
*m
)
1589 u_char
*blob
= NULL
;
1593 debug3("%s entering", __func__
);
1595 auth_method
= "rsa";
1596 if (options
.rsa_authentication
&& authctxt
->valid
) {
1597 if ((client_n
= BN_new()) == NULL
)
1598 fatal("%s: BN_new", __func__
);
1599 buffer_get_bignum2(m
, client_n
);
1600 allowed
= auth_rsa_key_allowed(authctxt
->pw
, client_n
, &key
);
1601 BN_clear_free(client_n
);
1604 buffer_put_int(m
, allowed
);
1605 buffer_put_int(m
, forced_command
!= NULL
);
1607 /* clear temporarily storage (used by generate challenge) */
1608 monitor_reset_key_state();
1610 if (allowed
&& key
!= NULL
) {
1611 key
->type
= KEY_RSA
; /* cheat for key_to_blob */
1612 if (key_to_blob(key
, &blob
, &blen
) == 0)
1613 fatal("%s: key_to_blob failed", __func__
);
1614 buffer_put_string(m
, blob
, blen
);
1616 /* Save temporarily for comparison in verify */
1619 key_blobtype
= MM_RSAUSERKEY
;
1624 mm_request_send(sock
, MONITOR_ANS_RSAKEYALLOWED
, m
);
1626 monitor_permit(mon_dispatch
, MONITOR_REQ_RSACHALLENGE
, allowed
);
1627 monitor_permit(mon_dispatch
, MONITOR_REQ_RSARESPONSE
, 0);
1632 mm_answer_rsa_challenge(int sock
, Buffer
*m
)
1638 debug3("%s entering", __func__
);
1640 if (!authctxt
->valid
)
1641 fatal("%s: authctxt not valid", __func__
);
1642 blob
= buffer_get_string(m
, &blen
);
1643 if (!monitor_allowed_key(blob
, blen
))
1644 fatal("%s: bad key, not previously allowed", __func__
);
1645 if (key_blobtype
!= MM_RSAUSERKEY
&& key_blobtype
!= MM_RSAHOSTKEY
)
1646 fatal("%s: key type mismatch", __func__
);
1647 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1648 fatal("%s: received bad key", __func__
);
1649 if (key
->type
!= KEY_RSA
)
1650 fatal("%s: received bad key type %d", __func__
, key
->type
);
1651 key
->type
= KEY_RSA1
;
1653 BN_clear_free(ssh1_challenge
);
1654 ssh1_challenge
= auth_rsa_generate_challenge(key
);
1657 buffer_put_bignum2(m
, ssh1_challenge
);
1659 debug3("%s sending reply", __func__
);
1660 mm_request_send(sock
, MONITOR_ANS_RSACHALLENGE
, m
);
1662 monitor_permit(mon_dispatch
, MONITOR_REQ_RSARESPONSE
, 1);
1670 mm_answer_rsa_response(int sock
, Buffer
*m
)
1673 u_char
*blob
, *response
;
1677 debug3("%s entering", __func__
);
1679 if (!authctxt
->valid
)
1680 fatal("%s: authctxt not valid", __func__
);
1681 if (ssh1_challenge
== NULL
)
1682 fatal("%s: no ssh1_challenge", __func__
);
1684 blob
= buffer_get_string(m
, &blen
);
1685 if (!monitor_allowed_key(blob
, blen
))
1686 fatal("%s: bad key, not previously allowed", __func__
);
1687 if (key_blobtype
!= MM_RSAUSERKEY
&& key_blobtype
!= MM_RSAHOSTKEY
)
1688 fatal("%s: key type mismatch: %d", __func__
, key_blobtype
);
1689 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1690 fatal("%s: received bad key", __func__
);
1691 response
= buffer_get_string(m
, &len
);
1693 fatal("%s: received bad response to challenge", __func__
);
1694 success
= auth_rsa_verify_response(key
, ssh1_challenge
, response
);
1700 auth_method
= key_blobtype
== MM_RSAUSERKEY
? "rsa" : "rhosts-rsa";
1703 BN_clear_free(ssh1_challenge
);
1704 ssh1_challenge
= NULL
;
1705 monitor_reset_key_state();
1708 buffer_put_int(m
, success
);
1709 mm_request_send(sock
, MONITOR_ANS_RSARESPONSE
, m
);
1716 mm_answer_term(int sock
, Buffer
*req
)
1718 extern struct monitor
*pmonitor
;
1721 debug3("%s: tearing down sessions", __func__
);
1723 /* The child is terminating */
1724 session_destroy_all(&mm_session_close
);
1727 if (options
.use_pam
)
1731 while (waitpid(pmonitor
->m_pid
, &status
, 0) == -1)
1735 res
= WIFEXITED(status
) ? WEXITSTATUS(status
) : 1;
1737 /* Terminate process */
1741 #ifdef SSH_AUDIT_EVENTS
1742 /* Report that an audit event occurred */
1744 mm_answer_audit_event(int socket
, Buffer
*m
)
1746 ssh_audit_event_t event
;
1748 debug3("%s entering", __func__
);
1750 event
= buffer_get_int(m
);
1752 case SSH_AUTH_FAIL_PUBKEY
:
1753 case SSH_AUTH_FAIL_HOSTBASED
:
1754 case SSH_AUTH_FAIL_GSSAPI
:
1755 case SSH_LOGIN_EXCEED_MAXTRIES
:
1756 case SSH_LOGIN_ROOT_DENIED
:
1757 case SSH_CONNECTION_CLOSE
:
1758 case SSH_INVALID_USER
:
1762 fatal("Audit event type %d not permitted", event
);
1769 mm_answer_audit_command(int socket
, Buffer
*m
)
1774 debug3("%s entering", __func__
);
1775 cmd
= buffer_get_string(m
, &len
);
1776 /* sanity check command, if so how? */
1777 audit_run_command(cmd
);
1781 #endif /* SSH_AUDIT_EVENTS */
1784 monitor_apply_keystate(struct monitor
*pmonitor
)
1787 set_newkeys(MODE_IN
);
1788 set_newkeys(MODE_OUT
);
1790 packet_set_protocol_flags(child_state
.ssh1protoflags
);
1791 packet_set_encryption_key(child_state
.ssh1key
,
1792 child_state
.ssh1keylen
, child_state
.ssh1cipher
);
1793 free(child_state
.ssh1key
);
1796 /* for rc4 and other stateful ciphers */
1797 packet_set_keycontext(MODE_OUT
, child_state
.keyout
);
1798 free(child_state
.keyout
);
1799 packet_set_keycontext(MODE_IN
, child_state
.keyin
);
1800 free(child_state
.keyin
);
1803 packet_set_iv(MODE_OUT
, child_state
.ivout
);
1804 free(child_state
.ivout
);
1805 packet_set_iv(MODE_IN
, child_state
.ivin
);
1806 free(child_state
.ivin
);
1809 memcpy(&incoming_stream
, &child_state
.incoming
,
1810 sizeof(incoming_stream
));
1811 memcpy(&outgoing_stream
, &child_state
.outgoing
,
1812 sizeof(outgoing_stream
));
1814 /* Update with new address */
1815 if (options
.compression
)
1816 mm_init_compression(pmonitor
->m_zlib
);
1818 packet_set_postauth();
1820 if (options
.rekey_limit
|| options
.rekey_interval
)
1821 packet_set_rekey_limits((u_int32_t
)options
.rekey_limit
,
1822 (time_t)options
.rekey_interval
);
1824 /* Network I/O buffers */
1825 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1826 buffer_clear(packet_get_input());
1827 buffer_append(packet_get_input(), child_state
.input
, child_state
.ilen
);
1828 explicit_bzero(child_state
.input
, child_state
.ilen
);
1829 free(child_state
.input
);
1831 buffer_clear(packet_get_output());
1832 buffer_append(packet_get_output(), child_state
.output
,
1834 explicit_bzero(child_state
.output
, child_state
.olen
);
1835 free(child_state
.output
);
1839 roam_set_bytes(child_state
.sent_bytes
, child_state
.recv_bytes
);
1843 mm_get_kex(Buffer
*m
)
1849 kex
= xcalloc(1, sizeof(*kex
));
1850 kex
->session_id
= buffer_get_string(m
, &kex
->session_id_len
);
1851 if (session_id2
== NULL
||
1852 kex
->session_id_len
!= session_id2_len
||
1853 timingsafe_bcmp(kex
->session_id
, session_id2
, session_id2_len
) != 0)
1854 fatal("mm_get_get: internal error: bad session id");
1855 kex
->we_need
= buffer_get_int(m
);
1857 kex
->kex
[KEX_DH_GRP1_SHA1
] = kexdh_server
;
1858 kex
->kex
[KEX_DH_GRP14_SHA1
] = kexdh_server
;
1859 kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_server
;
1860 kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_server
;
1861 kex
->kex
[KEX_ECDH_SHA2
] = kexecdh_server
;
1863 kex
->kex
[KEX_C25519_SHA256
] = kexc25519_server
;
1865 kex
->hostkey_type
= buffer_get_int(m
);
1866 kex
->kex_type
= buffer_get_int(m
);
1867 blob
= buffer_get_string(m
, &bloblen
);
1868 buffer_init(&kex
->my
);
1869 buffer_append(&kex
->my
, blob
, bloblen
);
1871 blob
= buffer_get_string(m
, &bloblen
);
1872 buffer_init(&kex
->peer
);
1873 buffer_append(&kex
->peer
, blob
, bloblen
);
1876 kex
->flags
= buffer_get_int(m
);
1877 kex
->client_version_string
= buffer_get_string(m
, NULL
);
1878 kex
->server_version_string
= buffer_get_string(m
, NULL
);
1879 kex
->load_host_public_key
=&get_hostkey_public_by_type
;
1880 kex
->load_host_private_key
=&get_hostkey_private_by_type
;
1881 kex
->host_key_index
=&get_hostkey_index
;
1882 kex
->sign
= sshd_hostkey_sign
;
1887 /* This function requries careful sanity checking */
1890 mm_get_keystate(struct monitor
*pmonitor
)
1894 u_int bloblen
, plen
;
1895 u_int32_t seqnr
, packets
;
1896 u_int64_t blocks
, bytes
;
1898 debug3("%s: Waiting for new keys", __func__
);
1901 mm_request_receive_expect(pmonitor
->m_sendfd
, MONITOR_REQ_KEYEXPORT
, &m
);
1903 child_state
.ssh1protoflags
= buffer_get_int(&m
);
1904 child_state
.ssh1cipher
= buffer_get_int(&m
);
1905 child_state
.ssh1key
= buffer_get_string(&m
,
1906 &child_state
.ssh1keylen
);
1907 child_state
.ivout
= buffer_get_string(&m
,
1908 &child_state
.ivoutlen
);
1909 child_state
.ivin
= buffer_get_string(&m
, &child_state
.ivinlen
);
1912 /* Get the Kex for rekeying */
1913 *pmonitor
->m_pkex
= mm_get_kex(&m
);
1916 blob
= buffer_get_string(&m
, &bloblen
);
1917 current_keys
[MODE_OUT
] = mm_newkeys_from_blob(blob
, bloblen
);
1920 debug3("%s: Waiting for second key", __func__
);
1921 blob
= buffer_get_string(&m
, &bloblen
);
1922 current_keys
[MODE_IN
] = mm_newkeys_from_blob(blob
, bloblen
);
1925 /* Now get sequence numbers for the packets */
1926 seqnr
= buffer_get_int(&m
);
1927 blocks
= buffer_get_int64(&m
);
1928 packets
= buffer_get_int(&m
);
1929 bytes
= buffer_get_int64(&m
);
1930 packet_set_state(MODE_OUT
, seqnr
, blocks
, packets
, bytes
);
1931 seqnr
= buffer_get_int(&m
);
1932 blocks
= buffer_get_int64(&m
);
1933 packets
= buffer_get_int(&m
);
1934 bytes
= buffer_get_int64(&m
);
1935 packet_set_state(MODE_IN
, seqnr
, blocks
, packets
, bytes
);
1938 /* Get the key context */
1939 child_state
.keyout
= buffer_get_string(&m
, &child_state
.keyoutlen
);
1940 child_state
.keyin
= buffer_get_string(&m
, &child_state
.keyinlen
);
1942 debug3("%s: Getting compression state", __func__
);
1943 /* Get compression state */
1944 p
= buffer_get_string(&m
, &plen
);
1945 if (plen
!= sizeof(child_state
.outgoing
))
1946 fatal("%s: bad request size", __func__
);
1947 memcpy(&child_state
.outgoing
, p
, sizeof(child_state
.outgoing
));
1950 p
= buffer_get_string(&m
, &plen
);
1951 if (plen
!= sizeof(child_state
.incoming
))
1952 fatal("%s: bad request size", __func__
);
1953 memcpy(&child_state
.incoming
, p
, sizeof(child_state
.incoming
));
1956 /* Network I/O buffers */
1957 debug3("%s: Getting Network I/O buffers", __func__
);
1958 child_state
.input
= buffer_get_string(&m
, &child_state
.ilen
);
1959 child_state
.output
= buffer_get_string(&m
, &child_state
.olen
);
1963 child_state
.sent_bytes
= buffer_get_int64(&m
);
1964 child_state
.recv_bytes
= buffer_get_int64(&m
);
1971 /* Allocation functions for zlib */
1973 mm_zalloc(struct mm_master
*mm
, u_int ncount
, u_int size
)
1975 size_t len
= (size_t) size
* ncount
;
1978 if (len
== 0 || ncount
> SIZE_T_MAX
/ size
)
1979 fatal("%s: mm_zalloc(%u, %u)", __func__
, ncount
, size
);
1981 address
= mm_malloc(mm
, len
);
1987 mm_zfree(struct mm_master
*mm
, void *address
)
1989 mm_free(mm
, address
);
1993 mm_init_compression(struct mm_master
*mm
)
1995 outgoing_stream
.zalloc
= (alloc_func
)mm_zalloc
;
1996 outgoing_stream
.zfree
= (free_func
)mm_zfree
;
1997 outgoing_stream
.opaque
= mm
;
1999 incoming_stream
.zalloc
= (alloc_func
)mm_zalloc
;
2000 incoming_stream
.zfree
= (free_func
)mm_zfree
;
2001 incoming_stream
.opaque
= mm
;
2006 #define FD_CLOSEONEXEC(x) do { \
2007 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
2008 fatal("fcntl(%d, F_SETFD)", x); \
2012 monitor_openfds(struct monitor
*mon
, int do_logfds
)
2016 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, pair
) == -1)
2017 fatal("%s: socketpair: %s", __func__
, strerror(errno
));
2018 FD_CLOSEONEXEC(pair
[0]);
2019 FD_CLOSEONEXEC(pair
[1]);
2020 mon
->m_recvfd
= pair
[0];
2021 mon
->m_sendfd
= pair
[1];
2024 if (pipe(pair
) == -1)
2025 fatal("%s: pipe: %s", __func__
, strerror(errno
));
2026 FD_CLOSEONEXEC(pair
[0]);
2027 FD_CLOSEONEXEC(pair
[1]);
2028 mon
->m_log_recvfd
= pair
[0];
2029 mon
->m_log_sendfd
= pair
[1];
2031 mon
->m_log_recvfd
= mon
->m_log_sendfd
= -1;
2034 #define MM_MEMSIZE 65536
2039 struct monitor
*mon
;
2041 mon
= xcalloc(1, sizeof(*mon
));
2043 monitor_openfds(mon
, 1);
2045 /* Used to share zlib space across processes */
2046 if (options
.compression
) {
2047 mon
->m_zback
= mm_create(NULL
, MM_MEMSIZE
);
2048 mon
->m_zlib
= mm_create(mon
->m_zback
, 20 * MM_MEMSIZE
);
2050 /* Compression needs to share state across borders */
2051 mm_init_compression(mon
->m_zlib
);
2058 monitor_reinit(struct monitor
*mon
)
2060 monitor_openfds(mon
, 0);
2065 mm_answer_gss_setup_ctx(int sock
, Buffer
*m
)
2071 goid
.elements
= buffer_get_string(m
, &len
);
2074 major
= ssh_gssapi_server_ctx(&gsscontext
, &goid
);
2076 free(goid
.elements
);
2079 buffer_put_int(m
, major
);
2081 mm_request_send(sock
, MONITOR_ANS_GSSSETUP
, m
);
2083 /* Now we have a context, enable the step */
2084 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSSTEP
, 1);
2090 mm_answer_gss_accept_ctx(int sock
, Buffer
*m
)
2093 gss_buffer_desc out
= GSS_C_EMPTY_BUFFER
;
2094 OM_uint32 major
, minor
;
2095 OM_uint32 flags
= 0; /* GSI needs this */
2098 in
.value
= buffer_get_string(m
, &len
);
2100 major
= ssh_gssapi_accept_ctx(gsscontext
, &in
, &out
, &flags
);
2104 buffer_put_int(m
, major
);
2105 buffer_put_string(m
, out
.value
, out
.length
);
2106 buffer_put_int(m
, flags
);
2107 mm_request_send(sock
, MONITOR_ANS_GSSSTEP
, m
);
2109 gss_release_buffer(&minor
, &out
);
2111 if (major
== GSS_S_COMPLETE
) {
2112 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSSTEP
, 0);
2113 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSUSEROK
, 1);
2114 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSCHECKMIC
, 1);
2120 mm_answer_gss_checkmic(int sock
, Buffer
*m
)
2122 gss_buffer_desc gssbuf
, mic
;
2126 gssbuf
.value
= buffer_get_string(m
, &len
);
2127 gssbuf
.length
= len
;
2128 mic
.value
= buffer_get_string(m
, &len
);
2131 ret
= ssh_gssapi_checkmic(gsscontext
, &gssbuf
, &mic
);
2137 buffer_put_int(m
, ret
);
2139 mm_request_send(sock
, MONITOR_ANS_GSSCHECKMIC
, m
);
2141 if (!GSS_ERROR(ret
))
2142 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSUSEROK
, 1);
2148 mm_answer_gss_userok(int sock
, Buffer
*m
)
2152 authenticated
= authctxt
->valid
&& ssh_gssapi_userok(authctxt
->user
);
2155 buffer_put_int(m
, authenticated
);
2157 debug3("%s: sending result %d", __func__
, authenticated
);
2158 mm_request_send(sock
, MONITOR_ANS_GSSUSEROK
, m
);
2160 auth_method
= "gssapi-with-mic";
2162 /* Monitor loop will terminate if authenticated */
2163 return (authenticated
);