1 /* $OpenBSD: monitor.c,v 1.150 2015/06/22 23:42:16 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"
53 # ifdef HAVE_SYS_POLL_H
54 # include <sys/poll.h>
63 #include <openssl/dh.h>
66 #include "openbsd-compat/sys-queue.h"
77 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
80 #define TARGET_OS_MAC 1
85 #include "auth-options.h"
95 #include "monitor_mm.h"
99 #include "monitor_wrap.h"
100 #include "monitor_fdpass.h"
109 static Gssctxt *gsscontext = NULL;
113 extern ServerOptions options;
114 extern u_int utmp_len;
115 extern u_char session_id[];
116 extern Buffer auth_debug;
117 extern int auth_debug_init;
118 extern Buffer loginmsg;
120 /* State exported from the child */
121 static struct sshbuf *child_state;
123 /* Functions on the monitor that answer unprivileged requests */
125 int mm_answer_moduli(int, Buffer *);
126 int mm_answer_sign(int, Buffer *);
127 int mm_answer_pwnamallow(int, Buffer *);
128 int mm_answer_auth2_read_banner(int, Buffer *);
129 int mm_answer_authserv(int, Buffer *);
130 int mm_answer_authpassword(int, Buffer *);
131 int mm_answer_bsdauthquery(int, Buffer *);
132 int mm_answer_bsdauthrespond(int, Buffer *);
133 int mm_answer_skeyquery(int, Buffer *);
134 int mm_answer_skeyrespond(int, Buffer *);
135 int mm_answer_keyallowed(int, Buffer *);
136 int mm_answer_keyverify(int, Buffer *);
137 int mm_answer_pty(int, Buffer *);
138 int mm_answer_pty_cleanup(int, Buffer *);
139 int mm_answer_term(int, Buffer *);
140 int mm_answer_rsa_keyallowed(int, Buffer *);
141 int mm_answer_rsa_challenge(int, Buffer *);
142 int mm_answer_rsa_response(int, Buffer *);
143 int mm_answer_sesskey(int, Buffer *);
144 int mm_answer_sessid(int, Buffer *);
147 int mm_answer_pam_start(int, Buffer *);
148 int mm_answer_pam_account(int, Buffer *);
149 int mm_answer_pam_init_ctx(int, Buffer *);
150 int mm_answer_pam_query(int, Buffer *);
151 int mm_answer_pam_respond(int, Buffer *);
152 int mm_answer_pam_free_ctx(int, Buffer *);
156 int mm_answer_gss_setup_ctx(int, Buffer *);
157 int mm_answer_gss_accept_ctx(int, Buffer *);
158 int mm_answer_gss_userok(int, Buffer *);
159 int mm_answer_gss_checkmic(int, Buffer *);
162 #ifdef SSH_AUDIT_EVENTS
163 int mm_answer_audit_event(int, Buffer *);
164 int mm_answer_audit_command(int, Buffer *);
167 static int monitor_read_log(struct monitor *);
169 static Authctxt *authctxt;
172 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
175 /* local state for key verify */
176 static u_char *key_blob = NULL;
177 static u_int key_bloblen = 0;
178 static int key_blobtype = MM_NOKEY;
179 static char *hostbased_cuser = NULL;
180 static char *hostbased_chost = NULL;
181 static char *auth_method = "unknown";
182 static char *auth_submethod = NULL;
183 static u_int session_id2_len = 0;
184 static u_char *session_id2 = NULL;
185 static pid_t monitor_child_pid;
188 enum monitor_reqtype type;
190 int (*f)(int, Buffer *);
193 #define MON_ISAUTH 0x0004 /* Required for Authentication */
194 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
195 #define MON_ONCE 0x0010 /* Disable after calling */
196 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
198 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
200 #define MON_PERMIT 0x1000 /* Request is permitted */
202 struct mon_table mon_dispatch_proto20[] = {
204 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
206 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
207 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
208 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
209 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
210 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
212 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
213 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
214 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
215 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
216 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
217 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
219 #ifdef SSH_AUDIT_EVENTS
220 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
223 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
224 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
227 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
228 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
230 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
231 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
233 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
234 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
235 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
236 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
241 struct mon_table mon_dispatch_postauth20[] = {
243 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
245 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
246 {MONITOR_REQ_PTY, 0, mm_answer_pty},
247 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
248 {MONITOR_REQ_TERM, 0, mm_answer_term},
249 #ifdef SSH_AUDIT_EVENTS
250 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
251 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
256 struct mon_table mon_dispatch_proto15[] = {
258 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
259 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
260 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
261 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
262 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
263 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
264 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
265 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
267 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
268 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
271 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
272 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
275 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
276 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
277 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
278 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
279 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
280 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
282 #ifdef SSH_AUDIT_EVENTS
283 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
285 #endif /* WITH_SSH1 */
289 struct mon_table mon_dispatch_postauth15[] = {
291 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
292 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
293 {MONITOR_REQ_TERM, 0, mm_answer_term},
294 #ifdef SSH_AUDIT_EVENTS
295 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
296 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
298 #endif /* WITH_SSH1 */
302 struct mon_table *mon_dispatch;
304 /* Specifies if a certain message is allowed at the moment */
307 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
309 while (ent->f != NULL) {
310 if (ent->type == type) {
311 ent->flags &= ~MON_PERMIT;
312 ent->flags |= permit ? MON_PERMIT : 0;
320 monitor_permit_authentications(int permit)
322 struct mon_table *ent = mon_dispatch;
324 while (ent->f != NULL) {
325 if (ent->flags & MON_AUTH) {
326 ent->flags &= ~MON_PERMIT;
327 ent->flags |= permit ? MON_PERMIT : 0;
334 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
336 struct mon_table *ent;
337 int authenticated = 0, partial = 0;
339 debug3("preauth child monitor started");
341 close(pmonitor->m_recvfd);
342 close(pmonitor->m_log_sendfd);
343 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
345 authctxt = _authctxt;
346 memset(authctxt, 0, sizeof(*authctxt));
348 authctxt->loginmsg = &loginmsg;
351 mon_dispatch = mon_dispatch_proto20;
353 /* Permit requests for moduli and signatures */
354 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
355 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
357 mon_dispatch = mon_dispatch_proto15;
359 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
362 /* The first few requests do not require asynchronous access */
363 while (!authenticated) {
365 auth_method = "unknown";
366 auth_submethod = NULL;
367 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
369 /* Special handling for multiple required authentications */
370 if (options.num_auth_methods != 0) {
372 fatal("AuthenticationMethods is not supported"
373 "with SSH protocol 1");
375 !auth2_update_methods_lists(authctxt,
376 auth_method, auth_submethod)) {
377 debug3("%s: method %s: partial", __func__,
385 if (!(ent->flags & MON_AUTHDECIDE))
386 fatal("%s: unexpected authentication from %d",
387 __func__, ent->type);
388 if (authctxt->pw->pw_uid == 0 &&
389 !auth_root_allowed(auth_method))
392 /* PAM needs to perform account checks after auth */
393 if (options.use_pam && authenticated) {
397 mm_request_receive_expect(pmonitor->m_sendfd,
398 MONITOR_REQ_PAM_ACCOUNT, &m);
399 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
404 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
405 auth_log(authctxt, authenticated, partial,
406 auth_method, auth_submethod);
407 if (!partial && !authenticated)
408 authctxt->failures++;
412 if (!authctxt->valid)
413 fatal("%s: authenticated invalid user", __func__);
414 if (strcmp(auth_method, "unknown") == 0)
415 fatal("%s: authentication method name unknown", __func__);
417 debug("%s: %s has been authenticated by privileged process",
418 __func__, authctxt->user);
420 mm_get_keystate(pmonitor);
422 /* Drain any buffered messages from the child */
423 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
426 close(pmonitor->m_sendfd);
427 close(pmonitor->m_log_recvfd);
428 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
432 monitor_set_child_handler(pid_t pid)
434 monitor_child_pid = pid;
438 monitor_child_handler(int sig)
440 kill(monitor_child_pid, sig);
444 monitor_child_postauth(struct monitor *pmonitor)
446 close(pmonitor->m_recvfd);
447 pmonitor->m_recvfd = -1;
449 monitor_set_child_handler(pmonitor->m_pid);
450 signal(SIGHUP, &monitor_child_handler);
451 signal(SIGTERM, &monitor_child_handler);
452 signal(SIGINT, &monitor_child_handler);
454 signal(SIGXFSZ, SIG_IGN);
458 mon_dispatch = mon_dispatch_postauth20;
460 /* Permit requests for moduli and signatures */
461 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
462 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
463 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
465 mon_dispatch = mon_dispatch_postauth15;
466 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
469 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
470 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
474 monitor_read(pmonitor, mon_dispatch, NULL);
478 monitor_sync(struct monitor *pmonitor)
480 if (options.compression) {
481 /* The member allocation is not visible, so sync it */
482 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
486 /* Allocation functions for zlib */
488 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
490 size_t len = (size_t) size * ncount;
493 if (len == 0 || ncount > SIZE_MAX / size)
494 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
496 address = mm_malloc(mm, len);
502 mm_zfree(struct mm_master *mm, void *address)
504 mm_free(mm, address);
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);
688 mm_answer_sign(int sock, Buffer *m)
690 struct ssh *ssh = active_state; /* XXX */
691 extern int auth_sock; /* XXX move to state struct? */
693 struct sshbuf *sigbuf;
696 size_t datlen, siglen;
697 int r, keyid, is_proof = 0;
698 const char proof_req[] = "hostkeys-prove-00@openssh.com";
700 debug3("%s", __func__);
702 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
703 (r = sshbuf_get_string(m, &p, &datlen)) != 0)
704 fatal("%s: buffer error: %s", __func__, ssh_err(r));
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,
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, 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));
774 mm_request_send(sock, MONITOR_ANS_SIGN, m);
776 /* Turn on permissions for getpwnam */
777 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
782 /* Retrieves the password entry and also checks if the user is permitted */
785 mm_answer_pwnamallow(int sock, Buffer *m)
788 struct passwd *pwent;
792 debug3("%s", __func__);
794 if (authctxt->attempt++ != 0)
795 fatal("%s: multiple attempts for getpwnam", __func__);
797 username = buffer_get_string(m, NULL);
799 pwent = getpwnamallow(username);
801 authctxt->user = xstrdup(username);
802 setproctitle("%s [priv]", pwent ? username : "unknown");
808 buffer_put_char(m, 0);
809 authctxt->pw = fakepw();
814 authctxt->pw = pwent;
817 buffer_put_char(m, 1);
818 buffer_put_string(m, pwent, sizeof(struct passwd));
819 buffer_put_cstring(m, pwent->pw_name);
820 buffer_put_cstring(m, "*");
821 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
822 buffer_put_cstring(m, pwent->pw_gecos);
824 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
825 buffer_put_cstring(m, pwent->pw_class);
827 buffer_put_cstring(m, pwent->pw_dir);
828 buffer_put_cstring(m, pwent->pw_shell);
831 buffer_put_string(m, &options, sizeof(options));
833 #define M_CP_STROPT(x) do { \
834 if (options.x != NULL) \
835 buffer_put_cstring(m, options.x); \
837 #define M_CP_STRARRAYOPT(x, nx) do { \
838 for (i = 0; i < options.nx; i++) \
839 buffer_put_cstring(m, options.x[i]); \
841 /* See comment in servconf.h */
842 COPY_MATCH_STRING_OPTS();
844 #undef M_CP_STRARRAYOPT
846 /* Create valid auth method lists */
847 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
849 * The monitor will continue long enough to let the child
850 * run to it's packet_disconnect(), but it must not allow any
851 * authentication to succeed.
853 debug("%s: no valid authentication method lists", __func__);
856 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
857 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
859 /* For SSHv1 allow authentication now */
861 monitor_permit_authentications(1);
863 /* Allow service/style information on the auth context */
864 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
865 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
869 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
875 int mm_answer_auth2_read_banner(int sock, Buffer *m)
880 banner = auth2_read_banner();
881 buffer_put_cstring(m, banner != NULL ? banner : "");
882 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
889 mm_answer_authserv(int sock, Buffer *m)
891 monitor_permit_authentications(1);
893 authctxt->service = buffer_get_string(m, NULL);
894 authctxt->style = buffer_get_string(m, NULL);
895 debug3("%s: service=%s, style=%s",
896 __func__, authctxt->service, authctxt->style);
898 if (strlen(authctxt->style) == 0) {
899 free(authctxt->style);
900 authctxt->style = NULL;
907 mm_answer_authpassword(int sock, Buffer *m)
909 static int call_count;
914 passwd = buffer_get_string(m, &plen);
915 /* Only authenticate if the context is valid */
916 authenticated = options.password_authentication &&
917 auth_password(authctxt, passwd);
918 explicit_bzero(passwd, strlen(passwd));
922 buffer_put_int(m, authenticated);
924 debug3("%s: sending result %d", __func__, authenticated);
925 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
928 if (plen == 0 && call_count == 1)
929 auth_method = "none";
931 auth_method = "password";
933 /* Causes monitor loop to terminate if authenticated */
934 return (authenticated);
939 mm_answer_bsdauthquery(int sock, Buffer *m)
941 char *name, *infotxt;
947 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
948 &prompts, &echo_on) < 0 ? 0 : 1;
951 buffer_put_int(m, success);
953 buffer_put_cstring(m, prompts[0]);
955 debug3("%s: sending challenge success: %u", __func__, success);
956 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
969 mm_answer_bsdauthrespond(int sock, Buffer *m)
974 if (authctxt->as == 0)
975 fatal("%s: no bsd auth session", __func__);
977 response = buffer_get_string(m, NULL);
978 authok = options.challenge_response_authentication &&
979 auth_userresponse(authctxt->as, response, 0);
981 debug3("%s: <%s> = <%d>", __func__, response, authok);
985 buffer_put_int(m, authok);
987 debug3("%s: sending authenticated: %d", __func__, authok);
988 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
991 auth_method = "keyboard-interactive";
992 auth_submethod = "bsdauth";
994 auth_method = "bsdauth";
996 return (authok != 0);
1002 mm_answer_skeyquery(int sock, Buffer *m)
1005 char challenge[1024];
1008 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
1009 sizeof(challenge)) < 0 ? 0 : 1;
1012 buffer_put_int(m, success);
1014 buffer_put_cstring(m, challenge);
1016 debug3("%s: sending challenge success: %u", __func__, success);
1017 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
1023 mm_answer_skeyrespond(int sock, Buffer *m)
1028 response = buffer_get_string(m, NULL);
1030 authok = (options.challenge_response_authentication &&
1032 skey_haskey(authctxt->pw->pw_name) == 0 &&
1033 skey_passcheck(authctxt->pw->pw_name, response) != -1);
1038 buffer_put_int(m, authok);
1040 debug3("%s: sending authenticated: %d", __func__, authok);
1041 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
1043 auth_method = "skey";
1045 return (authok != 0);
1051 mm_answer_pam_start(int sock, Buffer *m)
1053 if (!options.use_pam)
1054 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1056 start_pam(authctxt);
1058 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1064 mm_answer_pam_account(int sock, Buffer *m)
1068 if (!options.use_pam)
1069 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1071 ret = do_pam_account();
1073 buffer_put_int(m, ret);
1074 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1076 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
1081 static void *sshpam_ctxt, *sshpam_authok;
1082 extern KbdintDevice sshpam_device;
1085 mm_answer_pam_init_ctx(int sock, Buffer *m)
1087 debug3("%s", __func__);
1088 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1089 sshpam_authok = NULL;
1091 if (sshpam_ctxt != NULL) {
1092 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1093 buffer_put_int(m, 1);
1095 buffer_put_int(m, 0);
1097 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
1102 mm_answer_pam_query(int sock, Buffer *m)
1104 char *name = NULL, *info = NULL, **prompts = NULL;
1105 u_int i, num = 0, *echo_on = 0;
1108 debug3("%s", __func__);
1109 sshpam_authok = NULL;
1110 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1111 if (ret == 0 && num == 0)
1112 sshpam_authok = sshpam_ctxt;
1113 if (num > 1 || name == NULL || info == NULL)
1116 buffer_put_int(m, ret);
1117 buffer_put_cstring(m, name);
1119 buffer_put_cstring(m, info);
1121 buffer_put_int(m, num);
1122 for (i = 0; i < num; ++i) {
1123 buffer_put_cstring(m, prompts[i]);
1125 buffer_put_int(m, echo_on[i]);
1129 auth_method = "keyboard-interactive";
1130 auth_submethod = "pam";
1131 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
1136 mm_answer_pam_respond(int sock, Buffer *m)
1142 debug3("%s", __func__);
1143 sshpam_authok = NULL;
1144 num = buffer_get_int(m);
1146 resp = xcalloc(num, sizeof(char *));
1147 for (i = 0; i < num; ++i)
1148 resp[i] = buffer_get_string(m, NULL);
1149 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1150 for (i = 0; i < num; ++i)
1154 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1157 buffer_put_int(m, ret);
1158 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
1159 auth_method = "keyboard-interactive";
1160 auth_submethod = "pam";
1162 sshpam_authok = sshpam_ctxt;
1167 mm_answer_pam_free_ctx(int sock, Buffer *m)
1169 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
1171 debug3("%s", __func__);
1172 (sshpam_device.free_ctx)(sshpam_ctxt);
1173 sshpam_ctxt = sshpam_authok = NULL;
1175 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
1176 auth_method = "keyboard-interactive";
1177 auth_submethod = "pam";
1183 mm_answer_keyallowed(int sock, Buffer *m)
1186 char *cuser, *chost;
1188 u_int bloblen, pubkey_auth_attempt;
1189 enum mm_keytype type = 0;
1192 debug3("%s entering", __func__);
1194 type = buffer_get_int(m);
1195 cuser = buffer_get_string(m, NULL);
1196 chost = buffer_get_string(m, NULL);
1197 blob = buffer_get_string(m, &bloblen);
1198 pubkey_auth_attempt = buffer_get_int(m);
1200 key = key_from_blob(blob, bloblen);
1202 if ((compat20 && type == MM_RSAHOSTKEY) ||
1203 (!compat20 && type != MM_RSAHOSTKEY))
1204 fatal("%s: key type and protocol mismatch", __func__);
1206 debug3("%s: key_from_blob: %p", __func__, key);
1208 if (key != NULL && authctxt->valid) {
1209 /* These should not make it past the privsep child */
1210 if (key_type_plain(key->type) == KEY_RSA &&
1211 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1212 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1216 allowed = options.pubkey_authentication &&
1217 !auth2_userkey_already_used(authctxt, key) &&
1218 match_pattern_list(sshkey_ssh_name(key),
1219 options.pubkey_key_types, 0) == 1 &&
1220 user_key_allowed(authctxt->pw, key,
1221 pubkey_auth_attempt);
1222 pubkey_auth_info(authctxt, key, NULL);
1223 auth_method = "publickey";
1224 if (options.pubkey_authentication &&
1225 (!pubkey_auth_attempt || allowed != 1))
1226 auth_clear_options();
1229 allowed = options.hostbased_authentication &&
1230 match_pattern_list(sshkey_ssh_name(key),
1231 options.hostbased_key_types, 0) == 1 &&
1232 hostbased_key_allowed(authctxt->pw,
1234 pubkey_auth_info(authctxt, key,
1235 "client user \"%.100s\", client host \"%.100s\"",
1237 auth_method = "hostbased";
1241 key->type = KEY_RSA1; /* XXX */
1242 allowed = options.rhosts_rsa_authentication &&
1243 auth_rhosts_rsa_key_allowed(authctxt->pw,
1245 if (options.rhosts_rsa_authentication && allowed != 1)
1246 auth_clear_options();
1247 auth_method = "rsa";
1251 fatal("%s: unknown key type %d", __func__, type);
1258 /* clear temporarily storage (used by verify) */
1259 monitor_reset_key_state();
1262 /* Save temporarily for comparison in verify */
1264 key_bloblen = bloblen;
1265 key_blobtype = type;
1266 hostbased_cuser = cuser;
1267 hostbased_chost = chost;
1269 /* Log failed attempt */
1270 auth_log(authctxt, 0, 0, auth_method, NULL);
1276 debug3("%s: key %p is %s",
1277 __func__, key, allowed ? "allowed" : "not allowed");
1280 buffer_put_int(m, allowed);
1281 buffer_put_int(m, forced_command != NULL);
1283 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1285 if (type == MM_RSAHOSTKEY)
1286 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1292 monitor_valid_userblob(u_char *data, u_int datalen)
1295 char *p, *userstyle;
1300 buffer_append(&b, data, datalen);
1302 if (datafellows & SSH_OLD_SESSIONID) {
1304 len = buffer_len(&b);
1305 if ((session_id2 == NULL) ||
1306 (len < session_id2_len) ||
1307 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1309 buffer_consume(&b, session_id2_len);
1311 p = buffer_get_string(&b, &len);
1312 if ((session_id2 == NULL) ||
1313 (len != session_id2_len) ||
1314 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1318 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1320 p = buffer_get_cstring(&b, NULL);
1321 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1322 authctxt->style ? ":" : "",
1323 authctxt->style ? authctxt->style : "");
1324 if (strcmp(userstyle, p) != 0) {
1325 logit("wrong user name passed to monitor: expected %s != %.100s",
1331 buffer_skip_string(&b);
1332 if (datafellows & SSH_BUG_PKAUTH) {
1333 if (!buffer_get_char(&b))
1336 p = buffer_get_cstring(&b, NULL);
1337 if (strcmp("publickey", p) != 0)
1340 if (!buffer_get_char(&b))
1342 buffer_skip_string(&b);
1344 buffer_skip_string(&b);
1345 if (buffer_len(&b) != 0)
1352 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1356 char *p, *userstyle;
1361 buffer_append(&b, data, datalen);
1363 p = buffer_get_string(&b, &len);
1364 if ((session_id2 == NULL) ||
1365 (len != session_id2_len) ||
1366 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1370 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1372 p = buffer_get_cstring(&b, NULL);
1373 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1374 authctxt->style ? ":" : "",
1375 authctxt->style ? authctxt->style : "");
1376 if (strcmp(userstyle, p) != 0) {
1377 logit("wrong user name passed to monitor: expected %s != %.100s",
1383 buffer_skip_string(&b); /* service */
1384 p = buffer_get_cstring(&b, NULL);
1385 if (strcmp(p, "hostbased") != 0)
1388 buffer_skip_string(&b); /* pkalg */
1389 buffer_skip_string(&b); /* pkblob */
1391 /* verify client host, strip trailing dot if necessary */
1392 p = buffer_get_string(&b, NULL);
1393 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1395 if (strcmp(p, chost) != 0)
1399 /* verify client user */
1400 p = buffer_get_string(&b, NULL);
1401 if (strcmp(p, cuser) != 0)
1405 if (buffer_len(&b) != 0)
1412 mm_answer_keyverify(int sock, Buffer *m)
1415 u_char *signature, *data, *blob;
1416 u_int signaturelen, datalen, bloblen;
1420 blob = buffer_get_string(m, &bloblen);
1421 signature = buffer_get_string(m, &signaturelen);
1422 data = buffer_get_string(m, &datalen);
1424 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1425 !monitor_allowed_key(blob, bloblen))
1426 fatal("%s: bad key, not previously allowed", __func__);
1428 key = key_from_blob(blob, bloblen);
1430 fatal("%s: bad public key blob", __func__);
1432 switch (key_blobtype) {
1434 valid_data = monitor_valid_userblob(data, datalen);
1437 valid_data = monitor_valid_hostbasedblob(data, datalen,
1438 hostbased_cuser, hostbased_chost);
1445 fatal("%s: bad signature data blob", __func__);
1447 verified = key_verify(key, signature, signaturelen, data, datalen);
1448 debug3("%s: key %p signature %s",
1449 __func__, key, (verified == 1) ? "verified" : "unverified");
1451 /* If auth was successful then record key to ensure it isn't reused */
1453 auth2_record_userkey(authctxt, key);
1461 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1463 monitor_reset_key_state();
1466 buffer_put_int(m, verified);
1467 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1469 return (verified == 1);
1473 mm_record_login(Session *s, struct passwd *pw)
1476 struct sockaddr_storage from;
1478 if (options.use_login)
1482 * Get IP address of client. If the connection is not a socket, let
1483 * the address be 0.0.0.0.
1485 memset(&from, 0, sizeof(from));
1486 fromlen = sizeof(from);
1487 if (packet_connection_is_on_socket()) {
1488 if (getpeername(packet_get_connection_in(),
1489 (struct sockaddr *)&from, &fromlen) < 0) {
1490 debug("getpeername: %.100s", strerror(errno));
1494 /* Record that there was a login on that tty from the remote host. */
1495 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1496 get_remote_name_or_ip(utmp_len, options.use_dns),
1497 (struct sockaddr *)&from, fromlen);
1501 mm_session_close(Session *s)
1503 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1504 if (s->ttyfd != -1) {
1505 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1506 session_pty_cleanup2(s);
1508 session_unused(s->self);
1512 mm_answer_pty(int sock, Buffer *m)
1514 extern struct monitor *pmonitor;
1518 debug3("%s entering", __func__);
1524 s->authctxt = authctxt;
1525 s->pw = authctxt->pw;
1526 s->pid = pmonitor->m_pid;
1527 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1530 pty_setowner(authctxt->pw, s->tty);
1532 buffer_put_int(m, 1);
1533 buffer_put_cstring(m, s->tty);
1535 /* We need to trick ttyslot */
1536 if (dup2(s->ttyfd, 0) == -1)
1537 fatal("%s: dup2", __func__);
1539 mm_record_login(s, authctxt->pw);
1541 /* Now we can close the file descriptor again */
1544 /* send messages generated by record_login */
1545 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1546 buffer_clear(&loginmsg);
1548 mm_request_send(sock, MONITOR_ANS_PTY, m);
1550 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1551 mm_send_fd(sock, s->ttyfd) == -1)
1552 fatal("%s: send fds failed", __func__);
1554 /* make sure nothing uses fd 0 */
1555 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1556 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1558 error("%s: fd0 %d != 0", __func__, fd0);
1560 /* slave is not needed */
1562 s->ttyfd = s->ptyfd;
1563 /* no need to dup() because nobody closes ptyfd */
1564 s->ptymaster = s->ptyfd;
1566 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1572 mm_session_close(s);
1573 buffer_put_int(m, 0);
1574 mm_request_send(sock, MONITOR_ANS_PTY, m);
1579 mm_answer_pty_cleanup(int sock, Buffer *m)
1584 debug3("%s entering", __func__);
1586 tty = buffer_get_string(m, NULL);
1587 if ((s = session_by_tty(tty)) != NULL)
1588 mm_session_close(s);
1596 mm_answer_sesskey(int sock, Buffer *m)
1601 /* Turn off permissions */
1602 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1604 if ((p = BN_new()) == NULL)
1605 fatal("%s: BN_new", __func__);
1607 buffer_get_bignum2(m, p);
1609 rsafail = ssh1_session_key(p);
1612 buffer_put_int(m, rsafail);
1613 buffer_put_bignum2(m, p);
1617 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1619 /* Turn on permissions for sessid passing */
1620 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1626 mm_answer_sessid(int sock, Buffer *m)
1630 debug3("%s entering", __func__);
1632 if (buffer_len(m) != 16)
1633 fatal("%s: bad ssh1 session id", __func__);
1634 for (i = 0; i < 16; i++)
1635 session_id[i] = buffer_get_char(m);
1637 /* Turn on permissions for getpwnam */
1638 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1644 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1648 u_char *blob = NULL;
1652 debug3("%s entering", __func__);
1654 auth_method = "rsa";
1655 if (options.rsa_authentication && authctxt->valid) {
1656 if ((client_n = BN_new()) == NULL)
1657 fatal("%s: BN_new", __func__);
1658 buffer_get_bignum2(m, client_n);
1659 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1660 BN_clear_free(client_n);
1663 buffer_put_int(m, allowed);
1664 buffer_put_int(m, forced_command != NULL);
1666 /* clear temporarily storage (used by generate challenge) */
1667 monitor_reset_key_state();
1669 if (allowed && key != NULL) {
1670 key->type = KEY_RSA; /* cheat for key_to_blob */
1671 if (key_to_blob(key, &blob, &blen) == 0)
1672 fatal("%s: key_to_blob failed", __func__);
1673 buffer_put_string(m, blob, blen);
1675 /* Save temporarily for comparison in verify */
1678 key_blobtype = MM_RSAUSERKEY;
1683 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1685 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1686 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1691 mm_answer_rsa_challenge(int sock, Buffer *m)
1697 debug3("%s entering", __func__);
1699 if (!authctxt->valid)
1700 fatal("%s: authctxt not valid", __func__);
1701 blob = buffer_get_string(m, &blen);
1702 if (!monitor_allowed_key(blob, blen))
1703 fatal("%s: bad key, not previously allowed", __func__);
1704 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1705 fatal("%s: key type mismatch", __func__);
1706 if ((key = key_from_blob(blob, blen)) == NULL)
1707 fatal("%s: received bad key", __func__);
1708 if (key->type != KEY_RSA)
1709 fatal("%s: received bad key type %d", __func__, key->type);
1710 key->type = KEY_RSA1;
1712 BN_clear_free(ssh1_challenge);
1713 ssh1_challenge = auth_rsa_generate_challenge(key);
1716 buffer_put_bignum2(m, ssh1_challenge);
1718 debug3("%s sending reply", __func__);
1719 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1721 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1729 mm_answer_rsa_response(int sock, Buffer *m)
1732 u_char *blob, *response;
1736 debug3("%s entering", __func__);
1738 if (!authctxt->valid)
1739 fatal("%s: authctxt not valid", __func__);
1740 if (ssh1_challenge == NULL)
1741 fatal("%s: no ssh1_challenge", __func__);
1743 blob = buffer_get_string(m, &blen);
1744 if (!monitor_allowed_key(blob, blen))
1745 fatal("%s: bad key, not previously allowed", __func__);
1746 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1747 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1748 if ((key = key_from_blob(blob, blen)) == NULL)
1749 fatal("%s: received bad key", __func__);
1750 response = buffer_get_string(m, &len);
1752 fatal("%s: received bad response to challenge", __func__);
1753 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1759 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1762 BN_clear_free(ssh1_challenge);
1763 ssh1_challenge = NULL;
1764 monitor_reset_key_state();
1767 buffer_put_int(m, success);
1768 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1775 mm_answer_term(int sock, Buffer *req)
1777 extern struct monitor *pmonitor;
1780 debug3("%s: tearing down sessions", __func__);
1782 /* The child is terminating */
1783 session_destroy_all(&mm_session_close);
1786 if (options.use_pam)
1790 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1794 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1796 /* Terminate process */
1800 #ifdef SSH_AUDIT_EVENTS
1801 /* Report that an audit event occurred */
1803 mm_answer_audit_event(int socket, Buffer *m)
1805 ssh_audit_event_t event;
1807 debug3("%s entering", __func__);
1809 event = buffer_get_int(m);
1811 case SSH_AUTH_FAIL_PUBKEY:
1812 case SSH_AUTH_FAIL_HOSTBASED:
1813 case SSH_AUTH_FAIL_GSSAPI:
1814 case SSH_LOGIN_EXCEED_MAXTRIES:
1815 case SSH_LOGIN_ROOT_DENIED:
1816 case SSH_CONNECTION_CLOSE:
1817 case SSH_INVALID_USER:
1821 fatal("Audit event type %d not permitted", event);
1828 mm_answer_audit_command(int socket, Buffer *m)
1833 debug3("%s entering", __func__);
1834 cmd = buffer_get_string(m, &len);
1835 /* sanity check command, if so how? */
1836 audit_run_command(cmd);
1840 #endif /* SSH_AUDIT_EVENTS */
1843 monitor_apply_keystate(struct monitor *pmonitor)
1845 struct ssh *ssh = active_state; /* XXX */
1849 debug3("%s: packet_set_state", __func__);
1850 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1851 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1852 sshbuf_free(child_state);
1855 if ((kex = ssh->kex) != 0) {
1856 /* XXX set callbacks */
1858 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1859 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1860 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1861 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1862 # ifdef OPENSSL_HAS_ECC
1863 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
1865 #endif /* WITH_OPENSSL */
1866 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1867 kex->load_host_public_key=&get_hostkey_public_by_type;
1868 kex->load_host_private_key=&get_hostkey_private_by_type;
1869 kex->host_key_index=&get_hostkey_index;
1870 kex->sign = sshd_hostkey_sign;
1873 /* Update with new address */
1874 if (options.compression) {
1875 ssh_packet_set_compress_hooks(ssh, pmonitor->m_zlib,
1876 (ssh_packet_comp_alloc_func *)mm_zalloc,
1877 (ssh_packet_comp_free_func *)mm_zfree);
1881 /* This function requries careful sanity checking */
1884 mm_get_keystate(struct monitor *pmonitor)
1886 debug3("%s: Waiting for new keys", __func__);
1888 if ((child_state = sshbuf_new()) == NULL)
1889 fatal("%s: sshbuf_new failed", __func__);
1890 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1892 debug3("%s: GOT new keys", __func__);
1898 #define FD_CLOSEONEXEC(x) do { \
1899 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
1900 fatal("fcntl(%d, F_SETFD)", x); \
1904 monitor_openfds(struct monitor *mon, int do_logfds)
1908 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1909 fatal("%s: socketpair: %s", __func__, strerror(errno));
1910 FD_CLOSEONEXEC(pair[0]);
1911 FD_CLOSEONEXEC(pair[1]);
1912 mon->m_recvfd = pair[0];
1913 mon->m_sendfd = pair[1];
1916 if (pipe(pair) == -1)
1917 fatal("%s: pipe: %s", __func__, strerror(errno));
1918 FD_CLOSEONEXEC(pair[0]);
1919 FD_CLOSEONEXEC(pair[1]);
1920 mon->m_log_recvfd = pair[0];
1921 mon->m_log_sendfd = pair[1];
1923 mon->m_log_recvfd = mon->m_log_sendfd = -1;
1926 #define MM_MEMSIZE 65536
1931 struct ssh *ssh = active_state; /* XXX */
1932 struct monitor *mon;
1934 mon = xcalloc(1, sizeof(*mon));
1936 monitor_openfds(mon, 1);
1938 /* Used to share zlib space across processes */
1939 if (options.compression) {
1940 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1941 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1943 /* Compression needs to share state across borders */
1944 ssh_packet_set_compress_hooks(ssh, mon->m_zlib,
1945 (ssh_packet_comp_alloc_func *)mm_zalloc,
1946 (ssh_packet_comp_free_func *)mm_zfree);
1953 monitor_reinit(struct monitor *mon)
1955 monitor_openfds(mon, 0);
1960 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1966 goid.elements = buffer_get_string(m, &len);
1969 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1971 free(goid.elements);
1974 buffer_put_int(m, major);
1976 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1978 /* Now we have a context, enable the step */
1979 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1985 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1988 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1989 OM_uint32 major, minor;
1990 OM_uint32 flags = 0; /* GSI needs this */
1993 in.value = buffer_get_string(m, &len);
1995 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1999 buffer_put_int(m, major);
2000 buffer_put_string(m, out.value, out.length);
2001 buffer_put_int(m, flags);
2002 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
2004 gss_release_buffer(&minor, &out);
2006 if (major == GSS_S_COMPLETE) {
2007 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2008 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2009 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
2015 mm_answer_gss_checkmic(int sock, Buffer *m)
2017 gss_buffer_desc gssbuf, mic;
2021 gssbuf.value = buffer_get_string(m, &len);
2022 gssbuf.length = len;
2023 mic.value = buffer_get_string(m, &len);
2026 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
2032 buffer_put_int(m, ret);
2034 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
2036 if (!GSS_ERROR(ret))
2037 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2043 mm_answer_gss_userok(int sock, Buffer *m)
2047 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2050 buffer_put_int(m, authenticated);
2052 debug3("%s: sending result %d", __func__, authenticated);
2053 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
2055 auth_method = "gssapi-with-mic";
2057 /* Monitor loop will terminate if authenticated */
2058 return (authenticated);