1 /* $OpenBSD: monitor.c,v 1.161 2016/07/22 03:39:13 djm 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/socket.h>
32 #include "openbsd-compat/sys-tree.h"
54 # ifdef HAVE_SYS_POLL_H
55 # include <sys/poll.h>
64 #include <openssl/dh.h>
67 #include "openbsd-compat/sys-queue.h"
79 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
82 #define TARGET_OS_MAC 1
87 #include "auth-options.h"
97 #include "monitor_mm.h"
101 #include "monitor_wrap.h"
102 #include "monitor_fdpass.h"
110 static Gssctxt
*gsscontext
= NULL
;
114 extern ServerOptions options
;
115 extern u_int utmp_len
;
116 extern u_char session_id
[];
117 extern Buffer auth_debug
;
118 extern int auth_debug_init
;
119 extern Buffer loginmsg
;
121 /* State exported from the child */
122 static struct sshbuf
*child_state
;
124 /* Functions on the monitor that answer unprivileged requests */
126 int mm_answer_moduli(int, Buffer
*);
127 int mm_answer_sign(int, Buffer
*);
128 int mm_answer_pwnamallow(int, Buffer
*);
129 int mm_answer_auth2_read_banner(int, Buffer
*);
130 int mm_answer_authserv(int, Buffer
*);
131 int mm_answer_authpassword(int, Buffer
*);
132 int mm_answer_bsdauthquery(int, Buffer
*);
133 int mm_answer_bsdauthrespond(int, Buffer
*);
134 int mm_answer_skeyquery(int, Buffer
*);
135 int mm_answer_skeyrespond(int, Buffer
*);
136 int mm_answer_keyallowed(int, Buffer
*);
137 int mm_answer_keyverify(int, Buffer
*);
138 int mm_answer_pty(int, Buffer
*);
139 int mm_answer_pty_cleanup(int, Buffer
*);
140 int mm_answer_term(int, Buffer
*);
141 int mm_answer_rsa_keyallowed(int, Buffer
*);
142 int mm_answer_rsa_challenge(int, Buffer
*);
143 int mm_answer_rsa_response(int, Buffer
*);
144 int mm_answer_sesskey(int, Buffer
*);
145 int mm_answer_sessid(int, Buffer
*);
148 int mm_answer_pam_start(int, Buffer
*);
149 int mm_answer_pam_account(int, Buffer
*);
150 int mm_answer_pam_init_ctx(int, Buffer
*);
151 int mm_answer_pam_query(int, Buffer
*);
152 int mm_answer_pam_respond(int, Buffer
*);
153 int mm_answer_pam_free_ctx(int, Buffer
*);
157 int mm_answer_gss_setup_ctx(int, Buffer
*);
158 int mm_answer_gss_accept_ctx(int, Buffer
*);
159 int mm_answer_gss_userok(int, Buffer
*);
160 int mm_answer_gss_checkmic(int, Buffer
*);
163 #ifdef SSH_AUDIT_EVENTS
164 int mm_answer_audit_event(int, Buffer
*);
165 int mm_answer_audit_command(int, Buffer
*);
168 static int monitor_read_log(struct monitor
*);
170 static Authctxt
*authctxt
;
173 static BIGNUM
*ssh1_challenge
= NULL
; /* used for ssh1 rsa auth */
176 /* local state for key verify */
177 static u_char
*key_blob
= NULL
;
178 static u_int key_bloblen
= 0;
179 static int key_blobtype
= MM_NOKEY
;
180 static char *hostbased_cuser
= NULL
;
181 static char *hostbased_chost
= NULL
;
182 static char *auth_method
= "unknown";
183 static char *auth_submethod
= NULL
;
184 static u_int session_id2_len
= 0;
185 static u_char
*session_id2
= NULL
;
186 static pid_t monitor_child_pid
;
189 enum monitor_reqtype type
;
191 int (*f
)(int, Buffer
*);
194 #define MON_ISAUTH 0x0004 /* Required for Authentication */
195 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
196 #define MON_ONCE 0x0010 /* Disable after calling */
197 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
199 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
201 #define MON_PERMIT 0x1000 /* Request is permitted */
203 struct mon_table mon_dispatch_proto20
[] = {
205 {MONITOR_REQ_MODULI
, MON_ONCE
, mm_answer_moduli
},
207 {MONITOR_REQ_SIGN
, MON_ONCE
, mm_answer_sign
},
208 {MONITOR_REQ_PWNAM
, MON_ONCE
, mm_answer_pwnamallow
},
209 {MONITOR_REQ_AUTHSERV
, MON_ONCE
, mm_answer_authserv
},
210 {MONITOR_REQ_AUTH2_READ_BANNER
, MON_ONCE
, mm_answer_auth2_read_banner
},
211 {MONITOR_REQ_AUTHPASSWORD
, MON_AUTH
, mm_answer_authpassword
},
213 {MONITOR_REQ_PAM_START
, MON_ONCE
, mm_answer_pam_start
},
214 {MONITOR_REQ_PAM_ACCOUNT
, 0, mm_answer_pam_account
},
215 {MONITOR_REQ_PAM_INIT_CTX
, MON_ISAUTH
, mm_answer_pam_init_ctx
},
216 {MONITOR_REQ_PAM_QUERY
, MON_ISAUTH
, mm_answer_pam_query
},
217 {MONITOR_REQ_PAM_RESPOND
, MON_ISAUTH
, mm_answer_pam_respond
},
218 {MONITOR_REQ_PAM_FREE_CTX
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_pam_free_ctx
},
220 #ifdef SSH_AUDIT_EVENTS
221 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
224 {MONITOR_REQ_BSDAUTHQUERY
, MON_ISAUTH
, mm_answer_bsdauthquery
},
225 {MONITOR_REQ_BSDAUTHRESPOND
, MON_AUTH
, mm_answer_bsdauthrespond
},
228 {MONITOR_REQ_SKEYQUERY
, MON_ISAUTH
, mm_answer_skeyquery
},
229 {MONITOR_REQ_SKEYRESPOND
, MON_AUTH
, mm_answer_skeyrespond
},
231 {MONITOR_REQ_KEYALLOWED
, MON_ISAUTH
, mm_answer_keyallowed
},
232 {MONITOR_REQ_KEYVERIFY
, MON_AUTH
, mm_answer_keyverify
},
234 {MONITOR_REQ_GSSSETUP
, MON_ISAUTH
, mm_answer_gss_setup_ctx
},
235 {MONITOR_REQ_GSSSTEP
, MON_ISAUTH
, mm_answer_gss_accept_ctx
},
236 {MONITOR_REQ_GSSUSEROK
, MON_AUTH
, mm_answer_gss_userok
},
237 {MONITOR_REQ_GSSCHECKMIC
, MON_ISAUTH
, mm_answer_gss_checkmic
},
242 struct mon_table mon_dispatch_postauth20
[] = {
244 {MONITOR_REQ_MODULI
, 0, mm_answer_moduli
},
246 {MONITOR_REQ_SIGN
, 0, mm_answer_sign
},
247 {MONITOR_REQ_PTY
, 0, mm_answer_pty
},
248 {MONITOR_REQ_PTYCLEANUP
, 0, mm_answer_pty_cleanup
},
249 {MONITOR_REQ_TERM
, 0, mm_answer_term
},
250 #ifdef SSH_AUDIT_EVENTS
251 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
252 {MONITOR_REQ_AUDIT_COMMAND
, MON_PERMIT
, mm_answer_audit_command
},
257 struct mon_table mon_dispatch_proto15
[] = {
259 {MONITOR_REQ_PWNAM
, MON_ONCE
, mm_answer_pwnamallow
},
260 {MONITOR_REQ_SESSKEY
, MON_ONCE
, mm_answer_sesskey
},
261 {MONITOR_REQ_SESSID
, MON_ONCE
, mm_answer_sessid
},
262 {MONITOR_REQ_AUTHPASSWORD
, MON_AUTH
, mm_answer_authpassword
},
263 {MONITOR_REQ_RSAKEYALLOWED
, MON_ISAUTH
|MON_ALOG
, mm_answer_rsa_keyallowed
},
264 {MONITOR_REQ_KEYALLOWED
, MON_ISAUTH
|MON_ALOG
, mm_answer_keyallowed
},
265 {MONITOR_REQ_RSACHALLENGE
, MON_ONCE
, mm_answer_rsa_challenge
},
266 {MONITOR_REQ_RSARESPONSE
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_rsa_response
},
268 {MONITOR_REQ_BSDAUTHQUERY
, MON_ISAUTH
, mm_answer_bsdauthquery
},
269 {MONITOR_REQ_BSDAUTHRESPOND
, MON_AUTH
, mm_answer_bsdauthrespond
},
272 {MONITOR_REQ_SKEYQUERY
, MON_ISAUTH
, mm_answer_skeyquery
},
273 {MONITOR_REQ_SKEYRESPOND
, MON_AUTH
, mm_answer_skeyrespond
},
276 {MONITOR_REQ_PAM_START
, MON_ONCE
, mm_answer_pam_start
},
277 {MONITOR_REQ_PAM_ACCOUNT
, 0, mm_answer_pam_account
},
278 {MONITOR_REQ_PAM_INIT_CTX
, MON_ISAUTH
, mm_answer_pam_init_ctx
},
279 {MONITOR_REQ_PAM_QUERY
, MON_ISAUTH
, mm_answer_pam_query
},
280 {MONITOR_REQ_PAM_RESPOND
, MON_ISAUTH
, mm_answer_pam_respond
},
281 {MONITOR_REQ_PAM_FREE_CTX
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_pam_free_ctx
},
283 #ifdef SSH_AUDIT_EVENTS
284 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
286 #endif /* WITH_SSH1 */
290 struct mon_table mon_dispatch_postauth15
[] = {
292 {MONITOR_REQ_PTY
, MON_ONCE
, mm_answer_pty
},
293 {MONITOR_REQ_PTYCLEANUP
, MON_ONCE
, mm_answer_pty_cleanup
},
294 {MONITOR_REQ_TERM
, 0, mm_answer_term
},
295 #ifdef SSH_AUDIT_EVENTS
296 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
297 {MONITOR_REQ_AUDIT_COMMAND
, MON_PERMIT
|MON_ONCE
, mm_answer_audit_command
},
299 #endif /* WITH_SSH1 */
303 struct mon_table
*mon_dispatch
;
305 /* Specifies if a certain message is allowed at the moment */
308 monitor_permit(struct mon_table
*ent
, enum monitor_reqtype type
, int permit
)
310 while (ent
->f
!= NULL
) {
311 if (ent
->type
== type
) {
312 ent
->flags
&= ~MON_PERMIT
;
313 ent
->flags
|= permit
? MON_PERMIT
: 0;
321 monitor_permit_authentications(int permit
)
323 struct mon_table
*ent
= mon_dispatch
;
325 while (ent
->f
!= NULL
) {
326 if (ent
->flags
& MON_AUTH
) {
327 ent
->flags
&= ~MON_PERMIT
;
328 ent
->flags
|= permit
? MON_PERMIT
: 0;
335 monitor_child_preauth(Authctxt
*_authctxt
, struct monitor
*pmonitor
)
337 struct mon_table
*ent
;
338 int authenticated
= 0, partial
= 0;
340 debug3("preauth child monitor started");
342 close(pmonitor
->m_recvfd
);
343 close(pmonitor
->m_log_sendfd
);
344 pmonitor
->m_log_sendfd
= pmonitor
->m_recvfd
= -1;
346 authctxt
= _authctxt
;
347 memset(authctxt
, 0, sizeof(*authctxt
));
349 authctxt
->loginmsg
= &loginmsg
;
352 mon_dispatch
= mon_dispatch_proto20
;
354 /* Permit requests for moduli and signatures */
355 monitor_permit(mon_dispatch
, MONITOR_REQ_MODULI
, 1);
356 monitor_permit(mon_dispatch
, MONITOR_REQ_SIGN
, 1);
358 mon_dispatch
= mon_dispatch_proto15
;
360 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSKEY
, 1);
363 /* The first few requests do not require asynchronous access */
364 while (!authenticated
) {
366 auth_method
= "unknown";
367 auth_submethod
= NULL
;
368 authenticated
= (monitor_read(pmonitor
, mon_dispatch
, &ent
) == 1);
370 /* Special handling for multiple required authentications */
371 if (options
.num_auth_methods
!= 0) {
373 fatal("AuthenticationMethods is not supported"
374 "with SSH protocol 1");
376 !auth2_update_methods_lists(authctxt
,
377 auth_method
, auth_submethod
)) {
378 debug3("%s: method %s: partial", __func__
,
386 if (!(ent
->flags
& MON_AUTHDECIDE
))
387 fatal("%s: unexpected authentication from %d",
388 __func__
, ent
->type
);
389 if (authctxt
->pw
->pw_uid
== 0 &&
390 !auth_root_allowed(auth_method
))
393 /* PAM needs to perform account checks after auth */
394 if (options
.use_pam
&& authenticated
) {
398 mm_request_receive_expect(pmonitor
->m_sendfd
,
399 MONITOR_REQ_PAM_ACCOUNT
, &m
);
400 authenticated
= mm_answer_pam_account(pmonitor
->m_sendfd
, &m
);
405 if (ent
->flags
& (MON_AUTHDECIDE
|MON_ALOG
)) {
406 auth_log(authctxt
, authenticated
, partial
,
407 auth_method
, auth_submethod
);
408 if (!partial
&& !authenticated
)
409 authctxt
->failures
++;
413 if (!authctxt
->valid
)
414 fatal("%s: authenticated invalid user", __func__
);
415 if (strcmp(auth_method
, "unknown") == 0)
416 fatal("%s: authentication method name unknown", __func__
);
418 debug("%s: %s has been authenticated by privileged process",
419 __func__
, authctxt
->user
);
421 mm_get_keystate(pmonitor
);
423 /* Drain any buffered messages from the child */
424 while (pmonitor
->m_log_recvfd
!= -1 && monitor_read_log(pmonitor
) == 0)
427 close(pmonitor
->m_sendfd
);
428 close(pmonitor
->m_log_recvfd
);
429 pmonitor
->m_sendfd
= pmonitor
->m_log_recvfd
= -1;
433 monitor_set_child_handler(pid_t pid
)
435 monitor_child_pid
= pid
;
439 monitor_child_handler(int sig
)
441 kill(monitor_child_pid
, sig
);
445 monitor_child_postauth(struct monitor
*pmonitor
)
447 close(pmonitor
->m_recvfd
);
448 pmonitor
->m_recvfd
= -1;
450 monitor_set_child_handler(pmonitor
->m_pid
);
451 signal(SIGHUP
, &monitor_child_handler
);
452 signal(SIGTERM
, &monitor_child_handler
);
453 signal(SIGINT
, &monitor_child_handler
);
455 signal(SIGXFSZ
, SIG_IGN
);
459 mon_dispatch
= mon_dispatch_postauth20
;
461 /* Permit requests for moduli and signatures */
462 monitor_permit(mon_dispatch
, MONITOR_REQ_MODULI
, 1);
463 monitor_permit(mon_dispatch
, MONITOR_REQ_SIGN
, 1);
464 monitor_permit(mon_dispatch
, MONITOR_REQ_TERM
, 1);
466 mon_dispatch
= mon_dispatch_postauth15
;
467 monitor_permit(mon_dispatch
, MONITOR_REQ_TERM
, 1);
470 monitor_permit(mon_dispatch
, MONITOR_REQ_PTY
, 1);
471 monitor_permit(mon_dispatch
, MONITOR_REQ_PTYCLEANUP
, 1);
475 monitor_read(pmonitor
, mon_dispatch
, NULL
);
479 monitor_sync(struct monitor
*pmonitor
)
481 if (options
.compression
) {
482 /* The member allocation is not visible, so sync it */
483 mm_share_sync(&pmonitor
->m_zlib
, &pmonitor
->m_zback
);
487 /* Allocation functions for zlib */
489 mm_zalloc(struct mm_master
*mm
, u_int ncount
, u_int size
)
491 if (size
== 0 || ncount
== 0 || ncount
> SIZE_MAX
/ size
)
492 fatal("%s: mm_zalloc(%u, %u)", __func__
, ncount
, size
);
494 return mm_malloc(mm
, size
* ncount
);
498 mm_zfree(struct mm_master
*mm
, void *address
)
500 mm_free(mm
, address
);
504 monitor_read_log(struct monitor
*pmonitor
)
510 buffer_init(&logmsg
);
513 buffer_append_space(&logmsg
, 4);
514 if (atomicio(read
, pmonitor
->m_log_recvfd
,
515 buffer_ptr(&logmsg
), buffer_len(&logmsg
)) != buffer_len(&logmsg
)) {
516 if (errno
== EPIPE
) {
517 buffer_free(&logmsg
);
518 debug("%s: child log fd closed", __func__
);
519 close(pmonitor
->m_log_recvfd
);
520 pmonitor
->m_log_recvfd
= -1;
523 fatal("%s: log fd read: %s", __func__
, strerror(errno
));
525 len
= buffer_get_int(&logmsg
);
526 if (len
<= 4 || len
> 8192)
527 fatal("%s: invalid log message length %u", __func__
, len
);
529 /* Read severity, message */
530 buffer_clear(&logmsg
);
531 buffer_append_space(&logmsg
, len
);
532 if (atomicio(read
, pmonitor
->m_log_recvfd
,
533 buffer_ptr(&logmsg
), buffer_len(&logmsg
)) != buffer_len(&logmsg
))
534 fatal("%s: log fd read: %s", __func__
, strerror(errno
));
537 level
= buffer_get_int(&logmsg
);
538 msg
= buffer_get_string(&logmsg
, NULL
);
539 if (log_level_name(level
) == NULL
)
540 fatal("%s: invalid log level %u (corrupted message?)",
542 do_log2(level
, "%s [preauth]", msg
);
544 buffer_free(&logmsg
);
551 monitor_read(struct monitor
*pmonitor
, struct mon_table
*ent
,
552 struct mon_table
**pent
)
557 struct pollfd pfd
[2];
560 memset(&pfd
, 0, sizeof(pfd
));
561 pfd
[0].fd
= pmonitor
->m_sendfd
;
562 pfd
[0].events
= POLLIN
;
563 pfd
[1].fd
= pmonitor
->m_log_recvfd
;
564 pfd
[1].events
= pfd
[1].fd
== -1 ? 0 : POLLIN
;
565 if (poll(pfd
, pfd
[1].fd
== -1 ? 1 : 2, -1) == -1) {
566 if (errno
== EINTR
|| errno
== EAGAIN
)
568 fatal("%s: poll: %s", __func__
, strerror(errno
));
570 if (pfd
[1].revents
) {
572 * Drain all log messages before processing next
575 monitor_read_log(pmonitor
);
579 break; /* Continues below */
584 mm_request_receive(pmonitor
->m_sendfd
, &m
);
585 type
= buffer_get_char(&m
);
587 debug3("%s: checking request %d", __func__
, type
);
589 while (ent
->f
!= NULL
) {
590 if (ent
->type
== type
)
595 if (ent
->f
!= NULL
) {
596 if (!(ent
->flags
& MON_PERMIT
))
597 fatal("%s: unpermitted request %d", __func__
,
599 ret
= (*ent
->f
)(pmonitor
->m_sendfd
, &m
);
602 /* The child may use this request only once, disable it */
603 if (ent
->flags
& MON_ONCE
) {
604 debug2("%s: %d used once, disabling now", __func__
,
606 ent
->flags
&= ~MON_PERMIT
;
615 fatal("%s: unsupported request: %d", __func__
, type
);
621 /* allowed key state */
623 monitor_allowed_key(u_char
*blob
, u_int bloblen
)
625 /* make sure key is allowed */
626 if (key_blob
== NULL
|| key_bloblen
!= bloblen
||
627 timingsafe_bcmp(key_blob
, blob
, key_bloblen
))
633 monitor_reset_key_state(void)
637 free(hostbased_cuser
);
638 free(hostbased_chost
);
641 key_blobtype
= MM_NOKEY
;
642 hostbased_cuser
= NULL
;
643 hostbased_chost
= NULL
;
648 mm_answer_moduli(int sock
, Buffer
*m
)
653 min
= buffer_get_int(m
);
654 want
= buffer_get_int(m
);
655 max
= buffer_get_int(m
);
657 debug3("%s: got parameters: %d %d %d",
658 __func__
, min
, want
, max
);
659 /* We need to check here, too, in case the child got corrupted */
660 if (max
< min
|| want
< min
|| max
< want
)
661 fatal("%s: bad parameters: %d %d %d",
662 __func__
, min
, want
, max
);
666 dh
= choose_dh(min
, want
, max
);
668 buffer_put_char(m
, 0);
671 /* Send first bignum */
672 buffer_put_char(m
, 1);
673 buffer_put_bignum2(m
, dh
->p
);
674 buffer_put_bignum2(m
, dh
->g
);
678 mm_request_send(sock
, MONITOR_ANS_MODULI
, m
);
684 mm_answer_sign(int sock
, Buffer
*m
)
686 struct ssh
*ssh
= active_state
; /* XXX */
687 extern int auth_sock
; /* XXX move to state struct? */
689 struct sshbuf
*sigbuf
= NULL
;
690 u_char
*p
= NULL
, *signature
= NULL
;
692 size_t datlen
, siglen
, alglen
;
695 const char proof_req
[] = "hostkeys-prove-00@openssh.com";
697 debug3("%s", __func__
);
699 if ((r
= sshbuf_get_u32(m
, &keyid
)) != 0 ||
700 (r
= sshbuf_get_string(m
, &p
, &datlen
)) != 0 ||
701 (r
= sshbuf_get_cstring(m
, &alg
, &alglen
)) != 0)
702 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
704 fatal("%s: invalid key ID", __func__
);
707 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
708 * SHA384 (48 bytes) and SHA512 (64 bytes).
710 * Otherwise, verify the signature request is for a hostkey
713 * XXX perform similar check for KEX signature requests too?
714 * it's not trivial, since what is signed is the hash, rather
715 * than the full kex structure...
717 if (datlen
!= 20 && datlen
!= 32 && datlen
!= 48 && datlen
!= 64) {
719 * Construct expected hostkey proof and compare it to what
720 * the client sent us.
722 if (session_id2_len
== 0) /* hostkeys is never first */
723 fatal("%s: bad data length: %zu", __func__
, datlen
);
724 if ((key
= get_hostkey_public_by_index(keyid
, ssh
)) == NULL
)
725 fatal("%s: no hostkey for index %d", __func__
, keyid
);
726 if ((sigbuf
= sshbuf_new()) == NULL
)
727 fatal("%s: sshbuf_new", __func__
);
728 if ((r
= sshbuf_put_cstring(sigbuf
, proof_req
)) != 0 ||
729 (r
= sshbuf_put_string(sigbuf
, session_id2
,
730 session_id2_len
)) != 0 ||
731 (r
= sshkey_puts(key
, sigbuf
)) != 0)
732 fatal("%s: couldn't prepare private key "
733 "proof buffer: %s", __func__
, ssh_err(r
));
734 if (datlen
!= sshbuf_len(sigbuf
) ||
735 memcmp(p
, sshbuf_ptr(sigbuf
), sshbuf_len(sigbuf
)) != 0)
736 fatal("%s: bad data length: %zu, hostkey proof len %zu",
737 __func__
, datlen
, sshbuf_len(sigbuf
));
742 /* save session id, it will be passed on the first call */
743 if (session_id2_len
== 0) {
744 session_id2_len
= datlen
;
745 session_id2
= xmalloc(session_id2_len
);
746 memcpy(session_id2
, p
, session_id2_len
);
749 if ((key
= get_hostkey_by_index(keyid
)) != NULL
) {
750 if ((r
= sshkey_sign(key
, &signature
, &siglen
, p
, datlen
, alg
,
752 fatal("%s: sshkey_sign failed: %s",
753 __func__
, ssh_err(r
));
754 } else if ((key
= get_hostkey_public_by_index(keyid
, ssh
)) != NULL
&&
756 if ((r
= ssh_agent_sign(auth_sock
, key
, &signature
, &siglen
,
757 p
, datlen
, alg
, datafellows
)) != 0) {
758 fatal("%s: ssh_agent_sign failed: %s",
759 __func__
, ssh_err(r
));
762 fatal("%s: no hostkey from index %d", __func__
, keyid
);
764 debug3("%s: %s signature %p(%zu)", __func__
,
765 is_proof
? "KEX" : "hostkey proof", signature
, siglen
);
768 if ((r
= sshbuf_put_string(m
, signature
, siglen
)) != 0)
769 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
775 mm_request_send(sock
, MONITOR_ANS_SIGN
, m
);
777 /* Turn on permissions for getpwnam */
778 monitor_permit(mon_dispatch
, MONITOR_REQ_PWNAM
, 1);
783 /* Retrieves the password entry and also checks if the user is permitted */
786 mm_answer_pwnamallow(int sock
, Buffer
*m
)
789 struct passwd
*pwent
;
793 debug3("%s", __func__
);
795 if (authctxt
->attempt
++ != 0)
796 fatal("%s: multiple attempts for getpwnam", __func__
);
798 username
= buffer_get_string(m
, NULL
);
800 pwent
= getpwnamallow(username
);
802 authctxt
->user
= xstrdup(username
);
803 setproctitle("%s [priv]", pwent
? username
: "unknown");
809 buffer_put_char(m
, 0);
810 authctxt
->pw
= fakepw();
815 authctxt
->pw
= pwent
;
818 buffer_put_char(m
, 1);
819 buffer_put_string(m
, pwent
, sizeof(struct passwd
));
820 buffer_put_cstring(m
, pwent
->pw_name
);
821 buffer_put_cstring(m
, "*");
822 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
823 buffer_put_cstring(m
, pwent
->pw_gecos
);
825 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
826 buffer_put_cstring(m
, pwent
->pw_class
);
828 buffer_put_cstring(m
, pwent
->pw_dir
);
829 buffer_put_cstring(m
, pwent
->pw_shell
);
832 buffer_put_string(m
, &options
, sizeof(options
));
834 #define M_CP_STROPT(x) do { \
835 if (options.x != NULL) \
836 buffer_put_cstring(m, options.x); \
838 #define M_CP_STRARRAYOPT(x, nx) do { \
839 for (i = 0; i < options.nx; i++) \
840 buffer_put_cstring(m, options.x[i]); \
842 /* See comment in servconf.h */
843 COPY_MATCH_STRING_OPTS();
845 #undef M_CP_STRARRAYOPT
847 /* Create valid auth method lists */
848 if (compat20
&& auth2_setup_methods_lists(authctxt
) != 0) {
850 * The monitor will continue long enough to let the child
851 * run to it's packet_disconnect(), but it must not allow any
852 * authentication to succeed.
854 debug("%s: no valid authentication method lists", __func__
);
857 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__
, allowed
);
858 mm_request_send(sock
, MONITOR_ANS_PWNAM
, m
);
860 /* For SSHv1 allow authentication now */
862 monitor_permit_authentications(1);
864 /* Allow service/style information on the auth context */
865 monitor_permit(mon_dispatch
, MONITOR_REQ_AUTHSERV
, 1);
866 monitor_permit(mon_dispatch
, MONITOR_REQ_AUTH2_READ_BANNER
, 1);
870 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_START
, 1);
876 int mm_answer_auth2_read_banner(int sock
, Buffer
*m
)
881 banner
= auth2_read_banner();
882 buffer_put_cstring(m
, banner
!= NULL
? banner
: "");
883 mm_request_send(sock
, MONITOR_ANS_AUTH2_READ_BANNER
, m
);
890 mm_answer_authserv(int sock
, Buffer
*m
)
892 monitor_permit_authentications(1);
894 authctxt
->service
= buffer_get_string(m
, NULL
);
895 authctxt
->style
= buffer_get_string(m
, NULL
);
896 debug3("%s: service=%s, style=%s",
897 __func__
, authctxt
->service
, authctxt
->style
);
899 if (strlen(authctxt
->style
) == 0) {
900 free(authctxt
->style
);
901 authctxt
->style
= NULL
;
908 mm_answer_authpassword(int sock
, Buffer
*m
)
910 static int call_count
;
915 passwd
= buffer_get_string(m
, &plen
);
916 /* Only authenticate if the context is valid */
917 authenticated
= options
.password_authentication
&&
918 auth_password(authctxt
, passwd
);
919 explicit_bzero(passwd
, strlen(passwd
));
923 buffer_put_int(m
, authenticated
);
925 buffer_put_int(m
, sshpam_get_maxtries_reached());
928 debug3("%s: sending result %d", __func__
, authenticated
);
929 mm_request_send(sock
, MONITOR_ANS_AUTHPASSWORD
, m
);
932 if (plen
== 0 && call_count
== 1)
933 auth_method
= "none";
935 auth_method
= "password";
937 /* Causes monitor loop to terminate if authenticated */
938 return (authenticated
);
943 mm_answer_bsdauthquery(int sock
, Buffer
*m
)
945 char *name
, *infotxt
;
951 success
= bsdauth_query(authctxt
, &name
, &infotxt
, &numprompts
,
952 &prompts
, &echo_on
) < 0 ? 0 : 1;
955 buffer_put_int(m
, success
);
957 buffer_put_cstring(m
, prompts
[0]);
959 debug3("%s: sending challenge success: %u", __func__
, success
);
960 mm_request_send(sock
, MONITOR_ANS_BSDAUTHQUERY
, m
);
973 mm_answer_bsdauthrespond(int sock
, Buffer
*m
)
978 if (authctxt
->as
== NULL
)
979 fatal("%s: no bsd auth session", __func__
);
981 response
= buffer_get_string(m
, NULL
);
982 authok
= options
.challenge_response_authentication
&&
983 auth_userresponse(authctxt
->as
, response
, 0);
985 debug3("%s: <%s> = <%d>", __func__
, response
, authok
);
989 buffer_put_int(m
, authok
);
991 debug3("%s: sending authenticated: %d", __func__
, authok
);
992 mm_request_send(sock
, MONITOR_ANS_BSDAUTHRESPOND
, m
);
995 auth_method
= "keyboard-interactive";
996 auth_submethod
= "bsdauth";
998 auth_method
= "bsdauth";
1000 return (authok
!= 0);
1006 mm_answer_skeyquery(int sock
, Buffer
*m
)
1009 char challenge
[1024];
1012 success
= _compat_skeychallenge(&skey
, authctxt
->user
, challenge
,
1013 sizeof(challenge
)) < 0 ? 0 : 1;
1016 buffer_put_int(m
, success
);
1018 buffer_put_cstring(m
, challenge
);
1020 debug3("%s: sending challenge success: %u", __func__
, success
);
1021 mm_request_send(sock
, MONITOR_ANS_SKEYQUERY
, m
);
1027 mm_answer_skeyrespond(int sock
, Buffer
*m
)
1032 response
= buffer_get_string(m
, NULL
);
1034 authok
= (options
.challenge_response_authentication
&&
1036 skey_haskey(authctxt
->pw
->pw_name
) == 0 &&
1037 skey_passcheck(authctxt
->pw
->pw_name
, response
) != -1);
1042 buffer_put_int(m
, authok
);
1044 debug3("%s: sending authenticated: %d", __func__
, authok
);
1045 mm_request_send(sock
, MONITOR_ANS_SKEYRESPOND
, m
);
1047 auth_method
= "keyboard-interactive";
1048 auth_submethod
= "skey";
1050 return (authok
!= 0);
1056 mm_answer_pam_start(int sock
, Buffer
*m
)
1058 if (!options
.use_pam
)
1059 fatal("UsePAM not set, but ended up in %s anyway", __func__
);
1061 start_pam(authctxt
);
1063 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_ACCOUNT
, 1);
1069 mm_answer_pam_account(int sock
, Buffer
*m
)
1073 if (!options
.use_pam
)
1074 fatal("UsePAM not set, but ended up in %s anyway", __func__
);
1076 ret
= do_pam_account();
1078 buffer_put_int(m
, ret
);
1079 buffer_put_string(m
, buffer_ptr(&loginmsg
), buffer_len(&loginmsg
));
1081 mm_request_send(sock
, MONITOR_ANS_PAM_ACCOUNT
, m
);
1086 static void *sshpam_ctxt
, *sshpam_authok
;
1087 extern KbdintDevice sshpam_device
;
1090 mm_answer_pam_init_ctx(int sock
, Buffer
*m
)
1092 debug3("%s", __func__
);
1093 sshpam_ctxt
= (sshpam_device
.init_ctx
)(authctxt
);
1094 sshpam_authok
= NULL
;
1096 if (sshpam_ctxt
!= NULL
) {
1097 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_FREE_CTX
, 1);
1098 buffer_put_int(m
, 1);
1100 buffer_put_int(m
, 0);
1102 mm_request_send(sock
, MONITOR_ANS_PAM_INIT_CTX
, m
);
1107 mm_answer_pam_query(int sock
, Buffer
*m
)
1109 char *name
= NULL
, *info
= NULL
, **prompts
= NULL
;
1110 u_int i
, num
= 0, *echo_on
= 0;
1113 debug3("%s", __func__
);
1114 sshpam_authok
= NULL
;
1115 ret
= (sshpam_device
.query
)(sshpam_ctxt
, &name
, &info
, &num
, &prompts
, &echo_on
);
1116 if (ret
== 0 && num
== 0)
1117 sshpam_authok
= sshpam_ctxt
;
1118 if (num
> 1 || name
== NULL
|| info
== NULL
)
1121 buffer_put_int(m
, ret
);
1122 buffer_put_cstring(m
, name
);
1124 buffer_put_cstring(m
, info
);
1126 buffer_put_int(m
, sshpam_get_maxtries_reached());
1127 buffer_put_int(m
, num
);
1128 for (i
= 0; i
< num
; ++i
) {
1129 buffer_put_cstring(m
, prompts
[i
]);
1131 buffer_put_int(m
, echo_on
[i
]);
1135 auth_method
= "keyboard-interactive";
1136 auth_submethod
= "pam";
1137 mm_request_send(sock
, MONITOR_ANS_PAM_QUERY
, m
);
1142 mm_answer_pam_respond(int sock
, Buffer
*m
)
1148 debug3("%s", __func__
);
1149 sshpam_authok
= NULL
;
1150 num
= buffer_get_int(m
);
1152 resp
= xcalloc(num
, sizeof(char *));
1153 for (i
= 0; i
< num
; ++i
)
1154 resp
[i
] = buffer_get_string(m
, NULL
);
1155 ret
= (sshpam_device
.respond
)(sshpam_ctxt
, num
, resp
);
1156 for (i
= 0; i
< num
; ++i
)
1160 ret
= (sshpam_device
.respond
)(sshpam_ctxt
, num
, NULL
);
1163 buffer_put_int(m
, ret
);
1164 mm_request_send(sock
, MONITOR_ANS_PAM_RESPOND
, m
);
1165 auth_method
= "keyboard-interactive";
1166 auth_submethod
= "pam";
1168 sshpam_authok
= sshpam_ctxt
;
1173 mm_answer_pam_free_ctx(int sock
, Buffer
*m
)
1175 int r
= sshpam_authok
!= NULL
&& sshpam_authok
== sshpam_ctxt
;
1177 debug3("%s", __func__
);
1178 (sshpam_device
.free_ctx
)(sshpam_ctxt
);
1179 sshpam_ctxt
= sshpam_authok
= NULL
;
1181 mm_request_send(sock
, MONITOR_ANS_PAM_FREE_CTX
, m
);
1182 auth_method
= "keyboard-interactive";
1183 auth_submethod
= "pam";
1189 mm_answer_keyallowed(int sock
, Buffer
*m
)
1192 char *cuser
, *chost
;
1194 u_int bloblen
, pubkey_auth_attempt
;
1195 enum mm_keytype type
= 0;
1198 debug3("%s entering", __func__
);
1200 type
= buffer_get_int(m
);
1201 cuser
= buffer_get_string(m
, NULL
);
1202 chost
= buffer_get_string(m
, NULL
);
1203 blob
= buffer_get_string(m
, &bloblen
);
1204 pubkey_auth_attempt
= buffer_get_int(m
);
1206 key
= key_from_blob(blob
, bloblen
);
1208 if ((compat20
&& type
== MM_RSAHOSTKEY
) ||
1209 (!compat20
&& type
!= MM_RSAHOSTKEY
))
1210 fatal("%s: key type and protocol mismatch", __func__
);
1212 debug3("%s: key_from_blob: %p", __func__
, key
);
1214 if (key
!= NULL
&& authctxt
->valid
) {
1215 /* These should not make it past the privsep child */
1216 if (key_type_plain(key
->type
) == KEY_RSA
&&
1217 (datafellows
& SSH_BUG_RSASIGMD5
) != 0)
1218 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__
);
1222 allowed
= options
.pubkey_authentication
&&
1223 !auth2_userkey_already_used(authctxt
, key
) &&
1224 match_pattern_list(sshkey_ssh_name(key
),
1225 options
.pubkey_key_types
, 0) == 1 &&
1226 user_key_allowed(authctxt
->pw
, key
,
1227 pubkey_auth_attempt
);
1228 pubkey_auth_info(authctxt
, key
, NULL
);
1229 auth_method
= "publickey";
1230 if (options
.pubkey_authentication
&&
1231 (!pubkey_auth_attempt
|| allowed
!= 1))
1232 auth_clear_options();
1235 allowed
= options
.hostbased_authentication
&&
1236 match_pattern_list(sshkey_ssh_name(key
),
1237 options
.hostbased_key_types
, 0) == 1 &&
1238 hostbased_key_allowed(authctxt
->pw
,
1240 pubkey_auth_info(authctxt
, key
,
1241 "client user \"%.100s\", client host \"%.100s\"",
1243 auth_method
= "hostbased";
1247 key
->type
= KEY_RSA1
; /* XXX */
1248 allowed
= options
.rhosts_rsa_authentication
&&
1249 auth_rhosts_rsa_key_allowed(authctxt
->pw
,
1251 if (options
.rhosts_rsa_authentication
&& allowed
!= 1)
1252 auth_clear_options();
1253 auth_method
= "rsa";
1257 fatal("%s: unknown key type %d", __func__
, type
);
1262 debug3("%s: key %p is %s",
1263 __func__
, key
, allowed
? "allowed" : "not allowed");
1268 /* clear temporarily storage (used by verify) */
1269 monitor_reset_key_state();
1272 /* Save temporarily for comparison in verify */
1274 key_bloblen
= bloblen
;
1275 key_blobtype
= type
;
1276 hostbased_cuser
= cuser
;
1277 hostbased_chost
= chost
;
1279 /* Log failed attempt */
1280 auth_log(authctxt
, 0, 0, auth_method
, NULL
);
1287 buffer_put_int(m
, allowed
);
1288 buffer_put_int(m
, forced_command
!= NULL
);
1290 mm_request_send(sock
, MONITOR_ANS_KEYALLOWED
, m
);
1292 if (type
== MM_RSAHOSTKEY
)
1293 monitor_permit(mon_dispatch
, MONITOR_REQ_RSACHALLENGE
, allowed
);
1299 monitor_valid_userblob(u_char
*data
, u_int datalen
)
1303 char *userstyle
, *cp
;
1308 buffer_append(&b
, data
, datalen
);
1310 if (datafellows
& SSH_OLD_SESSIONID
) {
1312 len
= buffer_len(&b
);
1313 if ((session_id2
== NULL
) ||
1314 (len
< session_id2_len
) ||
1315 (timingsafe_bcmp(p
, session_id2
, session_id2_len
) != 0))
1317 buffer_consume(&b
, session_id2_len
);
1319 p
= buffer_get_string(&b
, &len
);
1320 if ((session_id2
== NULL
) ||
1321 (len
!= session_id2_len
) ||
1322 (timingsafe_bcmp(p
, session_id2
, session_id2_len
) != 0))
1326 if (buffer_get_char(&b
) != SSH2_MSG_USERAUTH_REQUEST
)
1328 cp
= buffer_get_cstring(&b
, NULL
);
1329 xasprintf(&userstyle
, "%s%s%s", authctxt
->user
,
1330 authctxt
->style
? ":" : "",
1331 authctxt
->style
? authctxt
->style
: "");
1332 if (strcmp(userstyle
, cp
) != 0) {
1333 logit("wrong user name passed to monitor: "
1334 "expected %s != %.100s", userstyle
, cp
);
1339 buffer_skip_string(&b
);
1340 if (datafellows
& SSH_BUG_PKAUTH
) {
1341 if (!buffer_get_char(&b
))
1344 cp
= buffer_get_cstring(&b
, NULL
);
1345 if (strcmp("publickey", cp
) != 0)
1348 if (!buffer_get_char(&b
))
1350 buffer_skip_string(&b
);
1352 buffer_skip_string(&b
);
1353 if (buffer_len(&b
) != 0)
1360 monitor_valid_hostbasedblob(u_char
*data
, u_int datalen
, char *cuser
,
1364 char *p
, *userstyle
;
1369 buffer_append(&b
, data
, datalen
);
1371 p
= buffer_get_string(&b
, &len
);
1372 if ((session_id2
== NULL
) ||
1373 (len
!= session_id2_len
) ||
1374 (timingsafe_bcmp(p
, session_id2
, session_id2_len
) != 0))
1378 if (buffer_get_char(&b
) != SSH2_MSG_USERAUTH_REQUEST
)
1380 p
= buffer_get_cstring(&b
, NULL
);
1381 xasprintf(&userstyle
, "%s%s%s", authctxt
->user
,
1382 authctxt
->style
? ":" : "",
1383 authctxt
->style
? authctxt
->style
: "");
1384 if (strcmp(userstyle
, p
) != 0) {
1385 logit("wrong user name passed to monitor: expected %s != %.100s",
1391 buffer_skip_string(&b
); /* service */
1392 p
= buffer_get_cstring(&b
, NULL
);
1393 if (strcmp(p
, "hostbased") != 0)
1396 buffer_skip_string(&b
); /* pkalg */
1397 buffer_skip_string(&b
); /* pkblob */
1399 /* verify client host, strip trailing dot if necessary */
1400 p
= buffer_get_string(&b
, NULL
);
1401 if (((len
= strlen(p
)) > 0) && p
[len
- 1] == '.')
1403 if (strcmp(p
, chost
) != 0)
1407 /* verify client user */
1408 p
= buffer_get_string(&b
, NULL
);
1409 if (strcmp(p
, cuser
) != 0)
1413 if (buffer_len(&b
) != 0)
1420 mm_answer_keyverify(int sock
, Buffer
*m
)
1423 u_char
*signature
, *data
, *blob
;
1424 u_int signaturelen
, datalen
, bloblen
;
1428 blob
= buffer_get_string(m
, &bloblen
);
1429 signature
= buffer_get_string(m
, &signaturelen
);
1430 data
= buffer_get_string(m
, &datalen
);
1432 if (hostbased_cuser
== NULL
|| hostbased_chost
== NULL
||
1433 !monitor_allowed_key(blob
, bloblen
))
1434 fatal("%s: bad key, not previously allowed", __func__
);
1436 key
= key_from_blob(blob
, bloblen
);
1438 fatal("%s: bad public key blob", __func__
);
1440 switch (key_blobtype
) {
1442 valid_data
= monitor_valid_userblob(data
, datalen
);
1445 valid_data
= monitor_valid_hostbasedblob(data
, datalen
,
1446 hostbased_cuser
, hostbased_chost
);
1453 fatal("%s: bad signature data blob", __func__
);
1455 verified
= key_verify(key
, signature
, signaturelen
, data
, datalen
);
1456 debug3("%s: key %p signature %s",
1457 __func__
, key
, (verified
== 1) ? "verified" : "unverified");
1459 /* If auth was successful then record key to ensure it isn't reused */
1460 if (verified
== 1 && key_blobtype
== MM_USERKEY
)
1461 auth2_record_userkey(authctxt
, key
);
1469 auth_method
= key_blobtype
== MM_USERKEY
? "publickey" : "hostbased";
1471 monitor_reset_key_state();
1474 buffer_put_int(m
, verified
);
1475 mm_request_send(sock
, MONITOR_ANS_KEYVERIFY
, m
);
1477 return (verified
== 1);
1481 mm_record_login(Session
*s
, struct passwd
*pw
)
1483 struct ssh
*ssh
= active_state
; /* XXX */
1485 struct sockaddr_storage from
;
1487 if (options
.use_login
)
1491 * Get IP address of client. If the connection is not a socket, let
1492 * the address be 0.0.0.0.
1494 memset(&from
, 0, sizeof(from
));
1495 fromlen
= sizeof(from
);
1496 if (packet_connection_is_on_socket()) {
1497 if (getpeername(packet_get_connection_in(),
1498 (struct sockaddr
*)&from
, &fromlen
) < 0) {
1499 debug("getpeername: %.100s", strerror(errno
));
1503 /* Record that there was a login on that tty from the remote host. */
1504 record_login(s
->pid
, s
->tty
, pw
->pw_name
, pw
->pw_uid
,
1505 session_get_remote_name_or_ip(ssh
, utmp_len
, options
.use_dns
),
1506 (struct sockaddr
*)&from
, fromlen
);
1510 mm_session_close(Session
*s
)
1512 debug3("%s: session %d pid %ld", __func__
, s
->self
, (long)s
->pid
);
1513 if (s
->ttyfd
!= -1) {
1514 debug3("%s: tty %s ptyfd %d", __func__
, s
->tty
, s
->ptyfd
);
1515 session_pty_cleanup2(s
);
1517 session_unused(s
->self
);
1521 mm_answer_pty(int sock
, Buffer
*m
)
1523 extern struct monitor
*pmonitor
;
1527 debug3("%s entering", __func__
);
1533 s
->authctxt
= authctxt
;
1534 s
->pw
= authctxt
->pw
;
1535 s
->pid
= pmonitor
->m_pid
;
1536 res
= pty_allocate(&s
->ptyfd
, &s
->ttyfd
, s
->tty
, sizeof(s
->tty
));
1539 pty_setowner(authctxt
->pw
, s
->tty
);
1541 buffer_put_int(m
, 1);
1542 buffer_put_cstring(m
, s
->tty
);
1544 /* We need to trick ttyslot */
1545 if (dup2(s
->ttyfd
, 0) == -1)
1546 fatal("%s: dup2", __func__
);
1548 mm_record_login(s
, authctxt
->pw
);
1550 /* Now we can close the file descriptor again */
1553 /* send messages generated by record_login */
1554 buffer_put_string(m
, buffer_ptr(&loginmsg
), buffer_len(&loginmsg
));
1555 buffer_clear(&loginmsg
);
1557 mm_request_send(sock
, MONITOR_ANS_PTY
, m
);
1559 if (mm_send_fd(sock
, s
->ptyfd
) == -1 ||
1560 mm_send_fd(sock
, s
->ttyfd
) == -1)
1561 fatal("%s: send fds failed", __func__
);
1563 /* make sure nothing uses fd 0 */
1564 if ((fd0
= open(_PATH_DEVNULL
, O_RDONLY
)) < 0)
1565 fatal("%s: open(/dev/null): %s", __func__
, strerror(errno
));
1567 error("%s: fd0 %d != 0", __func__
, fd0
);
1569 /* slave is not needed */
1571 s
->ttyfd
= s
->ptyfd
;
1572 /* no need to dup() because nobody closes ptyfd */
1573 s
->ptymaster
= s
->ptyfd
;
1575 debug3("%s: tty %s ptyfd %d", __func__
, s
->tty
, s
->ttyfd
);
1581 mm_session_close(s
);
1582 buffer_put_int(m
, 0);
1583 mm_request_send(sock
, MONITOR_ANS_PTY
, m
);
1588 mm_answer_pty_cleanup(int sock
, Buffer
*m
)
1593 debug3("%s entering", __func__
);
1595 tty
= buffer_get_string(m
, NULL
);
1596 if ((s
= session_by_tty(tty
)) != NULL
)
1597 mm_session_close(s
);
1605 mm_answer_sesskey(int sock
, Buffer
*m
)
1610 /* Turn off permissions */
1611 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSKEY
, 0);
1613 if ((p
= BN_new()) == NULL
)
1614 fatal("%s: BN_new", __func__
);
1616 buffer_get_bignum2(m
, p
);
1618 rsafail
= ssh1_session_key(p
);
1621 buffer_put_int(m
, rsafail
);
1622 buffer_put_bignum2(m
, p
);
1626 mm_request_send(sock
, MONITOR_ANS_SESSKEY
, m
);
1628 /* Turn on permissions for sessid passing */
1629 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSID
, 1);
1635 mm_answer_sessid(int sock
, Buffer
*m
)
1639 debug3("%s entering", __func__
);
1641 if (buffer_len(m
) != 16)
1642 fatal("%s: bad ssh1 session id", __func__
);
1643 for (i
= 0; i
< 16; i
++)
1644 session_id
[i
] = buffer_get_char(m
);
1646 /* Turn on permissions for getpwnam */
1647 monitor_permit(mon_dispatch
, MONITOR_REQ_PWNAM
, 1);
1653 mm_answer_rsa_keyallowed(int sock
, Buffer
*m
)
1657 u_char
*blob
= NULL
;
1661 debug3("%s entering", __func__
);
1663 auth_method
= "rsa";
1664 if (options
.rsa_authentication
&& authctxt
->valid
) {
1665 if ((client_n
= BN_new()) == NULL
)
1666 fatal("%s: BN_new", __func__
);
1667 buffer_get_bignum2(m
, client_n
);
1668 allowed
= auth_rsa_key_allowed(authctxt
->pw
, client_n
, &key
);
1669 BN_clear_free(client_n
);
1672 buffer_put_int(m
, allowed
);
1673 buffer_put_int(m
, forced_command
!= NULL
);
1675 /* clear temporarily storage (used by generate challenge) */
1676 monitor_reset_key_state();
1678 if (allowed
&& key
!= NULL
) {
1679 key
->type
= KEY_RSA
; /* cheat for key_to_blob */
1680 if (key_to_blob(key
, &blob
, &blen
) == 0)
1681 fatal("%s: key_to_blob failed", __func__
);
1682 buffer_put_string(m
, blob
, blen
);
1684 /* Save temporarily for comparison in verify */
1687 key_blobtype
= MM_RSAUSERKEY
;
1692 mm_request_send(sock
, MONITOR_ANS_RSAKEYALLOWED
, m
);
1694 monitor_permit(mon_dispatch
, MONITOR_REQ_RSACHALLENGE
, allowed
);
1695 monitor_permit(mon_dispatch
, MONITOR_REQ_RSARESPONSE
, 0);
1700 mm_answer_rsa_challenge(int sock
, Buffer
*m
)
1706 debug3("%s entering", __func__
);
1708 if (!authctxt
->valid
)
1709 fatal("%s: authctxt not valid", __func__
);
1710 blob
= buffer_get_string(m
, &blen
);
1711 if (!monitor_allowed_key(blob
, blen
))
1712 fatal("%s: bad key, not previously allowed", __func__
);
1713 if (key_blobtype
!= MM_RSAUSERKEY
&& key_blobtype
!= MM_RSAHOSTKEY
)
1714 fatal("%s: key type mismatch", __func__
);
1715 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1716 fatal("%s: received bad key", __func__
);
1717 if (key
->type
!= KEY_RSA
)
1718 fatal("%s: received bad key type %d", __func__
, key
->type
);
1719 key
->type
= KEY_RSA1
;
1721 BN_clear_free(ssh1_challenge
);
1722 ssh1_challenge
= auth_rsa_generate_challenge(key
);
1725 buffer_put_bignum2(m
, ssh1_challenge
);
1727 debug3("%s sending reply", __func__
);
1728 mm_request_send(sock
, MONITOR_ANS_RSACHALLENGE
, m
);
1730 monitor_permit(mon_dispatch
, MONITOR_REQ_RSARESPONSE
, 1);
1738 mm_answer_rsa_response(int sock
, Buffer
*m
)
1741 u_char
*blob
, *response
;
1745 debug3("%s entering", __func__
);
1747 if (!authctxt
->valid
)
1748 fatal("%s: authctxt not valid", __func__
);
1749 if (ssh1_challenge
== NULL
)
1750 fatal("%s: no ssh1_challenge", __func__
);
1752 blob
= buffer_get_string(m
, &blen
);
1753 if (!monitor_allowed_key(blob
, blen
))
1754 fatal("%s: bad key, not previously allowed", __func__
);
1755 if (key_blobtype
!= MM_RSAUSERKEY
&& key_blobtype
!= MM_RSAHOSTKEY
)
1756 fatal("%s: key type mismatch: %d", __func__
, key_blobtype
);
1757 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1758 fatal("%s: received bad key", __func__
);
1759 response
= buffer_get_string(m
, &len
);
1761 fatal("%s: received bad response to challenge", __func__
);
1762 success
= auth_rsa_verify_response(key
, ssh1_challenge
, response
);
1768 auth_method
= key_blobtype
== MM_RSAUSERKEY
? "rsa" : "rhosts-rsa";
1771 BN_clear_free(ssh1_challenge
);
1772 ssh1_challenge
= NULL
;
1773 monitor_reset_key_state();
1776 buffer_put_int(m
, success
);
1777 mm_request_send(sock
, MONITOR_ANS_RSARESPONSE
, m
);
1784 mm_answer_term(int sock
, Buffer
*req
)
1786 extern struct monitor
*pmonitor
;
1789 debug3("%s: tearing down sessions", __func__
);
1791 /* The child is terminating */
1792 session_destroy_all(&mm_session_close
);
1795 if (options
.use_pam
)
1799 while (waitpid(pmonitor
->m_pid
, &status
, 0) == -1)
1803 res
= WIFEXITED(status
) ? WEXITSTATUS(status
) : 1;
1805 /* Terminate process */
1809 #ifdef SSH_AUDIT_EVENTS
1810 /* Report that an audit event occurred */
1812 mm_answer_audit_event(int socket
, Buffer
*m
)
1814 ssh_audit_event_t event
;
1816 debug3("%s entering", __func__
);
1818 event
= buffer_get_int(m
);
1820 case SSH_AUTH_FAIL_PUBKEY
:
1821 case SSH_AUTH_FAIL_HOSTBASED
:
1822 case SSH_AUTH_FAIL_GSSAPI
:
1823 case SSH_LOGIN_EXCEED_MAXTRIES
:
1824 case SSH_LOGIN_ROOT_DENIED
:
1825 case SSH_CONNECTION_CLOSE
:
1826 case SSH_INVALID_USER
:
1830 fatal("Audit event type %d not permitted", event
);
1837 mm_answer_audit_command(int socket
, Buffer
*m
)
1842 debug3("%s entering", __func__
);
1843 cmd
= buffer_get_string(m
, &len
);
1844 /* sanity check command, if so how? */
1845 audit_run_command(cmd
);
1849 #endif /* SSH_AUDIT_EVENTS */
1852 monitor_apply_keystate(struct monitor
*pmonitor
)
1854 struct ssh
*ssh
= active_state
; /* XXX */
1858 debug3("%s: packet_set_state", __func__
);
1859 if ((r
= ssh_packet_set_state(ssh
, child_state
)) != 0)
1860 fatal("%s: packet_set_state: %s", __func__
, ssh_err(r
));
1861 sshbuf_free(child_state
);
1864 if ((kex
= ssh
->kex
) != NULL
) {
1865 /* XXX set callbacks */
1867 kex
->kex
[KEX_DH_GRP1_SHA1
] = kexdh_server
;
1868 kex
->kex
[KEX_DH_GRP14_SHA1
] = kexdh_server
;
1869 kex
->kex
[KEX_DH_GRP14_SHA256
] = kexdh_server
;
1870 kex
->kex
[KEX_DH_GRP16_SHA512
] = kexdh_server
;
1871 kex
->kex
[KEX_DH_GRP18_SHA512
] = kexdh_server
;
1872 kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_server
;
1873 kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_server
;
1874 # ifdef OPENSSL_HAS_ECC
1875 kex
->kex
[KEX_ECDH_SHA2
] = kexecdh_server
;
1877 #endif /* WITH_OPENSSL */
1878 kex
->kex
[KEX_C25519_SHA256
] = kexc25519_server
;
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
;
1885 /* Update with new address */
1886 if (options
.compression
) {
1887 ssh_packet_set_compress_hooks(ssh
, pmonitor
->m_zlib
,
1888 (ssh_packet_comp_alloc_func
*)mm_zalloc
,
1889 (ssh_packet_comp_free_func
*)mm_zfree
);
1893 /* This function requries careful sanity checking */
1896 mm_get_keystate(struct monitor
*pmonitor
)
1898 debug3("%s: Waiting for new keys", __func__
);
1900 if ((child_state
= sshbuf_new()) == NULL
)
1901 fatal("%s: sshbuf_new failed", __func__
);
1902 mm_request_receive_expect(pmonitor
->m_sendfd
, MONITOR_REQ_KEYEXPORT
,
1904 debug3("%s: GOT new keys", __func__
);
1910 #define FD_CLOSEONEXEC(x) do { \
1911 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
1912 fatal("fcntl(%d, F_SETFD)", x); \
1916 monitor_openfds(struct monitor
*mon
, int do_logfds
)
1920 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, pair
) == -1)
1921 fatal("%s: socketpair: %s", __func__
, strerror(errno
));
1922 FD_CLOSEONEXEC(pair
[0]);
1923 FD_CLOSEONEXEC(pair
[1]);
1924 mon
->m_recvfd
= pair
[0];
1925 mon
->m_sendfd
= pair
[1];
1928 if (pipe(pair
) == -1)
1929 fatal("%s: pipe: %s", __func__
, strerror(errno
));
1930 FD_CLOSEONEXEC(pair
[0]);
1931 FD_CLOSEONEXEC(pair
[1]);
1932 mon
->m_log_recvfd
= pair
[0];
1933 mon
->m_log_sendfd
= pair
[1];
1935 mon
->m_log_recvfd
= mon
->m_log_sendfd
= -1;
1938 #define MM_MEMSIZE 65536
1943 struct ssh
*ssh
= active_state
; /* XXX */
1944 struct monitor
*mon
;
1946 mon
= xcalloc(1, sizeof(*mon
));
1948 monitor_openfds(mon
, 1);
1950 /* Used to share zlib space across processes */
1951 if (options
.compression
) {
1952 mon
->m_zback
= mm_create(NULL
, MM_MEMSIZE
);
1953 mon
->m_zlib
= mm_create(mon
->m_zback
, 20 * MM_MEMSIZE
);
1955 /* Compression needs to share state across borders */
1956 ssh_packet_set_compress_hooks(ssh
, mon
->m_zlib
,
1957 (ssh_packet_comp_alloc_func
*)mm_zalloc
,
1958 (ssh_packet_comp_free_func
*)mm_zfree
);
1965 monitor_reinit(struct monitor
*mon
)
1967 monitor_openfds(mon
, 0);
1972 mm_answer_gss_setup_ctx(int sock
, Buffer
*m
)
1978 goid
.elements
= buffer_get_string(m
, &len
);
1981 major
= ssh_gssapi_server_ctx(&gsscontext
, &goid
);
1983 free(goid
.elements
);
1986 buffer_put_int(m
, major
);
1988 mm_request_send(sock
, MONITOR_ANS_GSSSETUP
, m
);
1990 /* Now we have a context, enable the step */
1991 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSSTEP
, 1);
1997 mm_answer_gss_accept_ctx(int sock
, Buffer
*m
)
2000 gss_buffer_desc out
= GSS_C_EMPTY_BUFFER
;
2001 OM_uint32 major
, minor
;
2002 OM_uint32 flags
= 0; /* GSI needs this */
2005 in
.value
= buffer_get_string(m
, &len
);
2007 major
= ssh_gssapi_accept_ctx(gsscontext
, &in
, &out
, &flags
);
2011 buffer_put_int(m
, major
);
2012 buffer_put_string(m
, out
.value
, out
.length
);
2013 buffer_put_int(m
, flags
);
2014 mm_request_send(sock
, MONITOR_ANS_GSSSTEP
, m
);
2016 gss_release_buffer(&minor
, &out
);
2018 if (major
== GSS_S_COMPLETE
) {
2019 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSSTEP
, 0);
2020 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSUSEROK
, 1);
2021 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSCHECKMIC
, 1);
2027 mm_answer_gss_checkmic(int sock
, Buffer
*m
)
2029 gss_buffer_desc gssbuf
, mic
;
2033 gssbuf
.value
= buffer_get_string(m
, &len
);
2034 gssbuf
.length
= len
;
2035 mic
.value
= buffer_get_string(m
, &len
);
2038 ret
= ssh_gssapi_checkmic(gsscontext
, &gssbuf
, &mic
);
2044 buffer_put_int(m
, ret
);
2046 mm_request_send(sock
, MONITOR_ANS_GSSCHECKMIC
, m
);
2048 if (!GSS_ERROR(ret
))
2049 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSUSEROK
, 1);
2055 mm_answer_gss_userok(int sock
, Buffer
*m
)
2059 authenticated
= authctxt
->valid
&& ssh_gssapi_userok(authctxt
->user
);
2062 buffer_put_int(m
, authenticated
);
2064 debug3("%s: sending result %d", __func__
, authenticated
);
2065 mm_request_send(sock
, MONITOR_ANS_GSSUSEROK
, m
);
2067 auth_method
= "gssapi-with-mic";
2069 /* Monitor loop will terminate if authenticated */
2070 return (authenticated
);