1 /* $OpenBSD: monitor.c,v 1.157 2016/02/15 23:32:37 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"
108 static Gssctxt *gsscontext = NULL;
112 extern ServerOptions options;
113 extern u_int utmp_len;
114 extern u_char session_id[];
115 extern Buffer auth_debug;
116 extern int auth_debug_init;
117 extern Buffer loginmsg;
119 /* State exported from the child */
120 static struct sshbuf *child_state;
122 /* Functions on the monitor that answer unprivileged requests */
124 int mm_answer_moduli(int, Buffer *);
125 int mm_answer_sign(int, Buffer *);
126 int mm_answer_pwnamallow(int, Buffer *);
127 int mm_answer_auth2_read_banner(int, Buffer *);
128 int mm_answer_authserv(int, Buffer *);
129 int mm_answer_authpassword(int, Buffer *);
130 int mm_answer_bsdauthquery(int, Buffer *);
131 int mm_answer_bsdauthrespond(int, Buffer *);
132 int mm_answer_skeyquery(int, Buffer *);
133 int mm_answer_skeyrespond(int, Buffer *);
134 int mm_answer_keyallowed(int, Buffer *);
135 int mm_answer_keyverify(int, Buffer *);
136 int mm_answer_pty(int, Buffer *);
137 int mm_answer_pty_cleanup(int, Buffer *);
138 int mm_answer_term(int, Buffer *);
139 int mm_answer_rsa_keyallowed(int, Buffer *);
140 int mm_answer_rsa_challenge(int, Buffer *);
141 int mm_answer_rsa_response(int, Buffer *);
142 int mm_answer_sesskey(int, Buffer *);
143 int mm_answer_sessid(int, Buffer *);
146 int mm_answer_pam_start(int, Buffer *);
147 int mm_answer_pam_account(int, Buffer *);
148 int mm_answer_pam_init_ctx(int, Buffer *);
149 int mm_answer_pam_query(int, Buffer *);
150 int mm_answer_pam_respond(int, Buffer *);
151 int mm_answer_pam_free_ctx(int, Buffer *);
155 int mm_answer_gss_setup_ctx(int, Buffer *);
156 int mm_answer_gss_accept_ctx(int, Buffer *);
157 int mm_answer_gss_userok(int, Buffer *);
158 int mm_answer_gss_checkmic(int, Buffer *);
161 #ifdef SSH_AUDIT_EVENTS
162 int mm_answer_audit_event(int, Buffer *);
163 int mm_answer_audit_command(int, Buffer *);
166 static int monitor_read_log(struct monitor *);
168 static Authctxt *authctxt;
171 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
174 /* local state for key verify */
175 static u_char *key_blob = NULL;
176 static u_int key_bloblen = 0;
177 static int key_blobtype = MM_NOKEY;
178 static char *hostbased_cuser = NULL;
179 static char *hostbased_chost = NULL;
180 static char *auth_method = "unknown";
181 static char *auth_submethod = NULL;
182 static u_int session_id2_len = 0;
183 static u_char *session_id2 = NULL;
184 static pid_t monitor_child_pid;
187 enum monitor_reqtype type;
189 int (*f)(int, Buffer *);
192 #define MON_ISAUTH 0x0004 /* Required for Authentication */
193 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
194 #define MON_ONCE 0x0010 /* Disable after calling */
195 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
197 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
199 #define MON_PERMIT 0x1000 /* Request is permitted */
201 struct mon_table mon_dispatch_proto20[] = {
203 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
205 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
206 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
207 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
208 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
209 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
211 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
212 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
213 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
214 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
215 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
216 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
218 #ifdef SSH_AUDIT_EVENTS
219 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
222 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
223 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
226 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
227 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
229 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
230 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
232 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
233 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
234 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
235 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
240 struct mon_table mon_dispatch_postauth20[] = {
242 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
244 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
245 {MONITOR_REQ_PTY, 0, mm_answer_pty},
246 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
247 {MONITOR_REQ_TERM, 0, mm_answer_term},
248 #ifdef SSH_AUDIT_EVENTS
249 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
250 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
255 struct mon_table mon_dispatch_proto15[] = {
257 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
258 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
259 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
260 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
261 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
262 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
263 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
264 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
266 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
267 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
270 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
271 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
274 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
275 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
276 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
277 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
278 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
279 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
281 #ifdef SSH_AUDIT_EVENTS
282 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
284 #endif /* WITH_SSH1 */
288 struct mon_table mon_dispatch_postauth15[] = {
290 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
291 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
292 {MONITOR_REQ_TERM, 0, mm_answer_term},
293 #ifdef SSH_AUDIT_EVENTS
294 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
295 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
297 #endif /* WITH_SSH1 */
301 struct mon_table *mon_dispatch;
303 /* Specifies if a certain message is allowed at the moment */
306 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
308 while (ent->f != NULL) {
309 if (ent->type == type) {
310 ent->flags &= ~MON_PERMIT;
311 ent->flags |= permit ? MON_PERMIT : 0;
319 monitor_permit_authentications(int permit)
321 struct mon_table *ent = mon_dispatch;
323 while (ent->f != NULL) {
324 if (ent->flags & MON_AUTH) {
325 ent->flags &= ~MON_PERMIT;
326 ent->flags |= permit ? MON_PERMIT : 0;
333 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
335 struct mon_table *ent;
336 int authenticated = 0, partial = 0;
338 debug3("preauth child monitor started");
340 close(pmonitor->m_recvfd);
341 close(pmonitor->m_log_sendfd);
342 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
344 authctxt = _authctxt;
345 memset(authctxt, 0, sizeof(*authctxt));
347 authctxt->loginmsg = &loginmsg;
350 mon_dispatch = mon_dispatch_proto20;
352 /* Permit requests for moduli and signatures */
353 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
354 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
356 mon_dispatch = mon_dispatch_proto15;
358 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
361 /* The first few requests do not require asynchronous access */
362 while (!authenticated) {
364 auth_method = "unknown";
365 auth_submethod = NULL;
366 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
368 /* Special handling for multiple required authentications */
369 if (options.num_auth_methods != 0) {
371 fatal("AuthenticationMethods is not supported"
372 "with SSH protocol 1");
374 !auth2_update_methods_lists(authctxt,
375 auth_method, auth_submethod)) {
376 debug3("%s: method %s: partial", __func__,
384 if (!(ent->flags & MON_AUTHDECIDE))
385 fatal("%s: unexpected authentication from %d",
386 __func__, ent->type);
387 if (authctxt->pw->pw_uid == 0 &&
388 !auth_root_allowed(auth_method))
391 /* PAM needs to perform account checks after auth */
392 if (options.use_pam && authenticated) {
396 mm_request_receive_expect(pmonitor->m_sendfd,
397 MONITOR_REQ_PAM_ACCOUNT, &m);
398 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
403 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
404 auth_log(authctxt, authenticated, partial,
405 auth_method, auth_submethod);
406 if (!partial && !authenticated)
407 authctxt->failures++;
411 if (!authctxt->valid)
412 fatal("%s: authenticated invalid user", __func__);
413 if (strcmp(auth_method, "unknown") == 0)
414 fatal("%s: authentication method name unknown", __func__);
416 debug("%s: %s has been authenticated by privileged process",
417 __func__, authctxt->user);
419 mm_get_keystate(pmonitor);
421 /* Drain any buffered messages from the child */
422 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
425 close(pmonitor->m_sendfd);
426 close(pmonitor->m_log_recvfd);
427 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
431 monitor_set_child_handler(pid_t pid)
433 monitor_child_pid = pid;
437 monitor_child_handler(int sig)
439 kill(monitor_child_pid, sig);
443 monitor_child_postauth(struct monitor *pmonitor)
445 close(pmonitor->m_recvfd);
446 pmonitor->m_recvfd = -1;
448 monitor_set_child_handler(pmonitor->m_pid);
449 signal(SIGHUP, &monitor_child_handler);
450 signal(SIGTERM, &monitor_child_handler);
451 signal(SIGINT, &monitor_child_handler);
453 signal(SIGXFSZ, SIG_IGN);
457 mon_dispatch = mon_dispatch_postauth20;
459 /* Permit requests for moduli and signatures */
460 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
461 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
462 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
464 mon_dispatch = mon_dispatch_postauth15;
465 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
468 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
469 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
473 monitor_read(pmonitor, mon_dispatch, NULL);
477 monitor_sync(struct monitor *pmonitor)
479 if (options.compression) {
480 /* The member allocation is not visible, so sync it */
481 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
485 /* Allocation functions for zlib */
487 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
489 if (size == 0 || ncount == 0 || ncount > SIZE_MAX / size)
490 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
492 return mm_malloc(mm, size * ncount);
496 mm_zfree(struct mm_master *mm, void *address)
498 mm_free(mm, address);
502 monitor_read_log(struct monitor *pmonitor)
508 buffer_init(&logmsg);
511 buffer_append_space(&logmsg, 4);
512 if (atomicio(read, pmonitor->m_log_recvfd,
513 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
514 if (errno == EPIPE) {
515 buffer_free(&logmsg);
516 debug("%s: child log fd closed", __func__);
517 close(pmonitor->m_log_recvfd);
518 pmonitor->m_log_recvfd = -1;
521 fatal("%s: log fd read: %s", __func__, strerror(errno));
523 len = buffer_get_int(&logmsg);
524 if (len <= 4 || len > 8192)
525 fatal("%s: invalid log message length %u", __func__, len);
527 /* Read severity, message */
528 buffer_clear(&logmsg);
529 buffer_append_space(&logmsg, len);
530 if (atomicio(read, pmonitor->m_log_recvfd,
531 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
532 fatal("%s: log fd read: %s", __func__, strerror(errno));
535 level = buffer_get_int(&logmsg);
536 msg = buffer_get_string(&logmsg, NULL);
537 if (log_level_name(level) == NULL)
538 fatal("%s: invalid log level %u (corrupted message?)",
540 do_log2(level, "%s [preauth]", msg);
542 buffer_free(&logmsg);
549 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
550 struct mon_table **pent)
555 struct pollfd pfd[2];
558 memset(&pfd, 0, sizeof(pfd));
559 pfd[0].fd = pmonitor->m_sendfd;
560 pfd[0].events = POLLIN;
561 pfd[1].fd = pmonitor->m_log_recvfd;
562 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
563 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
564 if (errno == EINTR || errno == EAGAIN)
566 fatal("%s: poll: %s", __func__, strerror(errno));
568 if (pfd[1].revents) {
570 * Drain all log messages before processing next
573 monitor_read_log(pmonitor);
577 break; /* Continues below */
582 mm_request_receive(pmonitor->m_sendfd, &m);
583 type = buffer_get_char(&m);
585 debug3("%s: checking request %d", __func__, type);
587 while (ent->f != NULL) {
588 if (ent->type == type)
593 if (ent->f != NULL) {
594 if (!(ent->flags & MON_PERMIT))
595 fatal("%s: unpermitted request %d", __func__,
597 ret = (*ent->f)(pmonitor->m_sendfd, &m);
600 /* The child may use this request only once, disable it */
601 if (ent->flags & MON_ONCE) {
602 debug2("%s: %d used once, disabling now", __func__,
604 ent->flags &= ~MON_PERMIT;
613 fatal("%s: unsupported request: %d", __func__, type);
619 /* allowed key state */
621 monitor_allowed_key(u_char *blob, u_int bloblen)
623 /* make sure key is allowed */
624 if (key_blob == NULL || key_bloblen != bloblen ||
625 timingsafe_bcmp(key_blob, blob, key_bloblen))
631 monitor_reset_key_state(void)
635 free(hostbased_cuser);
636 free(hostbased_chost);
639 key_blobtype = MM_NOKEY;
640 hostbased_cuser = NULL;
641 hostbased_chost = NULL;
646 mm_answer_moduli(int sock, Buffer *m)
651 min = buffer_get_int(m);
652 want = buffer_get_int(m);
653 max = buffer_get_int(m);
655 debug3("%s: got parameters: %d %d %d",
656 __func__, min, want, max);
657 /* We need to check here, too, in case the child got corrupted */
658 if (max < min || want < min || max < want)
659 fatal("%s: bad parameters: %d %d %d",
660 __func__, min, want, max);
664 dh = choose_dh(min, want, max);
666 buffer_put_char(m, 0);
669 /* Send first bignum */
670 buffer_put_char(m, 1);
671 buffer_put_bignum2(m, dh->p);
672 buffer_put_bignum2(m, dh->g);
676 mm_request_send(sock, MONITOR_ANS_MODULI, m);
682 mm_answer_sign(int sock, Buffer *m)
684 struct ssh *ssh = active_state; /* XXX */
685 extern int auth_sock; /* XXX move to state struct? */
687 struct sshbuf *sigbuf = NULL;
688 u_char *p = NULL, *signature = NULL;
690 size_t datlen, siglen, alglen;
691 int r, keyid, is_proof = 0;
692 const char proof_req[] = "hostkeys-prove-00@openssh.com";
694 debug3("%s", __func__);
696 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
697 (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
698 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0)
699 fatal("%s: buffer error: %s", __func__, ssh_err(r));
702 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
703 * SHA384 (48 bytes) and SHA512 (64 bytes).
705 * Otherwise, verify the signature request is for a hostkey
708 * XXX perform similar check for KEX signature requests too?
709 * it's not trivial, since what is signed is the hash, rather
710 * than the full kex structure...
712 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
714 * Construct expected hostkey proof and compare it to what
715 * the client sent us.
717 if (session_id2_len == 0) /* hostkeys is never first */
718 fatal("%s: bad data length: %zu", __func__, datlen);
719 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
720 fatal("%s: no hostkey for index %d", __func__, keyid);
721 if ((sigbuf = sshbuf_new()) == NULL)
722 fatal("%s: sshbuf_new", __func__);
723 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
724 (r = sshbuf_put_string(sigbuf, session_id2,
725 session_id2_len)) != 0 ||
726 (r = sshkey_puts(key, sigbuf)) != 0)
727 fatal("%s: couldn't prepare private key "
728 "proof buffer: %s", __func__, ssh_err(r));
729 if (datlen != sshbuf_len(sigbuf) ||
730 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
731 fatal("%s: bad data length: %zu, hostkey proof len %zu",
732 __func__, datlen, sshbuf_len(sigbuf));
737 /* save session id, it will be passed on the first call */
738 if (session_id2_len == 0) {
739 session_id2_len = datlen;
740 session_id2 = xmalloc(session_id2_len);
741 memcpy(session_id2, p, session_id2_len);
744 if ((key = get_hostkey_by_index(keyid)) != NULL) {
745 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
747 fatal("%s: sshkey_sign failed: %s",
748 __func__, ssh_err(r));
749 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
751 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
752 p, datlen, alg, datafellows)) != 0) {
753 fatal("%s: ssh_agent_sign failed: %s",
754 __func__, ssh_err(r));
757 fatal("%s: no hostkey from index %d", __func__, keyid);
759 debug3("%s: %s signature %p(%zu)", __func__,
760 is_proof ? "KEX" : "hostkey proof", signature, siglen);
763 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
764 fatal("%s: buffer error: %s", __func__, ssh_err(r));
770 mm_request_send(sock, MONITOR_ANS_SIGN, m);
772 /* Turn on permissions for getpwnam */
773 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
778 /* Retrieves the password entry and also checks if the user is permitted */
781 mm_answer_pwnamallow(int sock, Buffer *m)
784 struct passwd *pwent;
788 debug3("%s", __func__);
790 if (authctxt->attempt++ != 0)
791 fatal("%s: multiple attempts for getpwnam", __func__);
793 username = buffer_get_string(m, NULL);
795 pwent = getpwnamallow(username);
797 authctxt->user = xstrdup(username);
798 setproctitle("%s [priv]", pwent ? username : "unknown");
804 buffer_put_char(m, 0);
805 authctxt->pw = fakepw();
810 authctxt->pw = pwent;
813 buffer_put_char(m, 1);
814 buffer_put_string(m, pwent, sizeof(struct passwd));
815 buffer_put_cstring(m, pwent->pw_name);
816 buffer_put_cstring(m, "*");
817 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
818 buffer_put_cstring(m, pwent->pw_gecos);
820 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
821 buffer_put_cstring(m, pwent->pw_class);
823 buffer_put_cstring(m, pwent->pw_dir);
824 buffer_put_cstring(m, pwent->pw_shell);
827 buffer_put_string(m, &options, sizeof(options));
829 #define M_CP_STROPT(x) do { \
830 if (options.x != NULL) \
831 buffer_put_cstring(m, options.x); \
833 #define M_CP_STRARRAYOPT(x, nx) do { \
834 for (i = 0; i < options.nx; i++) \
835 buffer_put_cstring(m, options.x[i]); \
837 /* See comment in servconf.h */
838 COPY_MATCH_STRING_OPTS();
840 #undef M_CP_STRARRAYOPT
842 /* Create valid auth method lists */
843 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
845 * The monitor will continue long enough to let the child
846 * run to it's packet_disconnect(), but it must not allow any
847 * authentication to succeed.
849 debug("%s: no valid authentication method lists", __func__);
852 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
853 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
855 /* For SSHv1 allow authentication now */
857 monitor_permit_authentications(1);
859 /* Allow service/style information on the auth context */
860 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
861 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
865 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
871 int mm_answer_auth2_read_banner(int sock, Buffer *m)
876 banner = auth2_read_banner();
877 buffer_put_cstring(m, banner != NULL ? banner : "");
878 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
885 mm_answer_authserv(int sock, Buffer *m)
887 monitor_permit_authentications(1);
889 authctxt->service = buffer_get_string(m, NULL);
890 authctxt->style = buffer_get_string(m, NULL);
891 debug3("%s: service=%s, style=%s",
892 __func__, authctxt->service, authctxt->style);
894 if (strlen(authctxt->style) == 0) {
895 free(authctxt->style);
896 authctxt->style = NULL;
903 mm_answer_authpassword(int sock, Buffer *m)
905 static int call_count;
910 passwd = buffer_get_string(m, &plen);
911 /* Only authenticate if the context is valid */
912 authenticated = options.password_authentication &&
913 auth_password(authctxt, passwd);
914 explicit_bzero(passwd, strlen(passwd));
918 buffer_put_int(m, authenticated);
920 debug3("%s: sending result %d", __func__, authenticated);
921 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
924 if (plen == 0 && call_count == 1)
925 auth_method = "none";
927 auth_method = "password";
929 /* Causes monitor loop to terminate if authenticated */
930 return (authenticated);
935 mm_answer_bsdauthquery(int sock, Buffer *m)
937 char *name, *infotxt;
943 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
944 &prompts, &echo_on) < 0 ? 0 : 1;
947 buffer_put_int(m, success);
949 buffer_put_cstring(m, prompts[0]);
951 debug3("%s: sending challenge success: %u", __func__, success);
952 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
965 mm_answer_bsdauthrespond(int sock, Buffer *m)
970 if (authctxt->as == NULL)
971 fatal("%s: no bsd auth session", __func__);
973 response = buffer_get_string(m, NULL);
974 authok = options.challenge_response_authentication &&
975 auth_userresponse(authctxt->as, response, 0);
977 debug3("%s: <%s> = <%d>", __func__, response, authok);
981 buffer_put_int(m, authok);
983 debug3("%s: sending authenticated: %d", __func__, authok);
984 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
987 auth_method = "keyboard-interactive";
988 auth_submethod = "bsdauth";
990 auth_method = "bsdauth";
992 return (authok != 0);
998 mm_answer_skeyquery(int sock, Buffer *m)
1001 char challenge[1024];
1004 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
1005 sizeof(challenge)) < 0 ? 0 : 1;
1008 buffer_put_int(m, success);
1010 buffer_put_cstring(m, challenge);
1012 debug3("%s: sending challenge success: %u", __func__, success);
1013 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
1019 mm_answer_skeyrespond(int sock, Buffer *m)
1024 response = buffer_get_string(m, NULL);
1026 authok = (options.challenge_response_authentication &&
1028 skey_haskey(authctxt->pw->pw_name) == 0 &&
1029 skey_passcheck(authctxt->pw->pw_name, response) != -1);
1034 buffer_put_int(m, authok);
1036 debug3("%s: sending authenticated: %d", __func__, authok);
1037 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
1039 auth_method = "keyboard-interactive";
1040 auth_submethod = "skey";
1042 return (authok != 0);
1048 mm_answer_pam_start(int sock, Buffer *m)
1050 if (!options.use_pam)
1051 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1053 start_pam(authctxt);
1055 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1061 mm_answer_pam_account(int sock, Buffer *m)
1065 if (!options.use_pam)
1066 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1068 ret = do_pam_account();
1070 buffer_put_int(m, ret);
1071 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1073 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
1078 static void *sshpam_ctxt, *sshpam_authok;
1079 extern KbdintDevice sshpam_device;
1082 mm_answer_pam_init_ctx(int sock, Buffer *m)
1084 debug3("%s", __func__);
1085 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1086 sshpam_authok = NULL;
1088 if (sshpam_ctxt != NULL) {
1089 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1090 buffer_put_int(m, 1);
1092 buffer_put_int(m, 0);
1094 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
1099 mm_answer_pam_query(int sock, Buffer *m)
1101 char *name = NULL, *info = NULL, **prompts = NULL;
1102 u_int i, num = 0, *echo_on = 0;
1105 debug3("%s", __func__);
1106 sshpam_authok = NULL;
1107 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1108 if (ret == 0 && num == 0)
1109 sshpam_authok = sshpam_ctxt;
1110 if (num > 1 || name == NULL || info == NULL)
1113 buffer_put_int(m, ret);
1114 buffer_put_cstring(m, name);
1116 buffer_put_cstring(m, info);
1118 buffer_put_int(m, num);
1119 for (i = 0; i < num; ++i) {
1120 buffer_put_cstring(m, prompts[i]);
1122 buffer_put_int(m, echo_on[i]);
1126 auth_method = "keyboard-interactive";
1127 auth_submethod = "pam";
1128 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
1133 mm_answer_pam_respond(int sock, Buffer *m)
1139 debug3("%s", __func__);
1140 sshpam_authok = NULL;
1141 num = buffer_get_int(m);
1143 resp = xcalloc(num, sizeof(char *));
1144 for (i = 0; i < num; ++i)
1145 resp[i] = buffer_get_string(m, NULL);
1146 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1147 for (i = 0; i < num; ++i)
1151 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1154 buffer_put_int(m, ret);
1155 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
1156 auth_method = "keyboard-interactive";
1157 auth_submethod = "pam";
1159 sshpam_authok = sshpam_ctxt;
1164 mm_answer_pam_free_ctx(int sock, Buffer *m)
1166 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
1168 debug3("%s", __func__);
1169 (sshpam_device.free_ctx)(sshpam_ctxt);
1170 sshpam_ctxt = sshpam_authok = NULL;
1172 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
1173 auth_method = "keyboard-interactive";
1174 auth_submethod = "pam";
1180 mm_answer_keyallowed(int sock, Buffer *m)
1183 char *cuser, *chost;
1185 u_int bloblen, pubkey_auth_attempt;
1186 enum mm_keytype type = 0;
1189 debug3("%s entering", __func__);
1191 type = buffer_get_int(m);
1192 cuser = buffer_get_string(m, NULL);
1193 chost = buffer_get_string(m, NULL);
1194 blob = buffer_get_string(m, &bloblen);
1195 pubkey_auth_attempt = buffer_get_int(m);
1197 key = key_from_blob(blob, bloblen);
1199 if ((compat20 && type == MM_RSAHOSTKEY) ||
1200 (!compat20 && type != MM_RSAHOSTKEY))
1201 fatal("%s: key type and protocol mismatch", __func__);
1203 debug3("%s: key_from_blob: %p", __func__, key);
1205 if (key != NULL && authctxt->valid) {
1206 /* These should not make it past the privsep child */
1207 if (key_type_plain(key->type) == KEY_RSA &&
1208 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1209 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1213 allowed = options.pubkey_authentication &&
1214 !auth2_userkey_already_used(authctxt, key) &&
1215 match_pattern_list(sshkey_ssh_name(key),
1216 options.pubkey_key_types, 0) == 1 &&
1217 user_key_allowed(authctxt->pw, key,
1218 pubkey_auth_attempt);
1219 pubkey_auth_info(authctxt, key, NULL);
1220 auth_method = "publickey";
1221 if (options.pubkey_authentication &&
1222 (!pubkey_auth_attempt || allowed != 1))
1223 auth_clear_options();
1226 allowed = options.hostbased_authentication &&
1227 match_pattern_list(sshkey_ssh_name(key),
1228 options.hostbased_key_types, 0) == 1 &&
1229 hostbased_key_allowed(authctxt->pw,
1231 pubkey_auth_info(authctxt, key,
1232 "client user \"%.100s\", client host \"%.100s\"",
1234 auth_method = "hostbased";
1238 key->type = KEY_RSA1; /* XXX */
1239 allowed = options.rhosts_rsa_authentication &&
1240 auth_rhosts_rsa_key_allowed(authctxt->pw,
1242 if (options.rhosts_rsa_authentication && allowed != 1)
1243 auth_clear_options();
1244 auth_method = "rsa";
1248 fatal("%s: unknown key type %d", __func__, type);
1255 /* clear temporarily storage (used by verify) */
1256 monitor_reset_key_state();
1259 /* Save temporarily for comparison in verify */
1261 key_bloblen = bloblen;
1262 key_blobtype = type;
1263 hostbased_cuser = cuser;
1264 hostbased_chost = chost;
1266 /* Log failed attempt */
1267 auth_log(authctxt, 0, 0, auth_method, NULL);
1273 debug3("%s: key %p is %s",
1274 __func__, key, allowed ? "allowed" : "not allowed");
1277 buffer_put_int(m, allowed);
1278 buffer_put_int(m, forced_command != NULL);
1280 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1282 if (type == MM_RSAHOSTKEY)
1283 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1289 monitor_valid_userblob(u_char *data, u_int datalen)
1292 char *p, *userstyle;
1297 buffer_append(&b, data, datalen);
1299 if (datafellows & SSH_OLD_SESSIONID) {
1301 len = buffer_len(&b);
1302 if ((session_id2 == NULL) ||
1303 (len < session_id2_len) ||
1304 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1306 buffer_consume(&b, session_id2_len);
1308 p = buffer_get_string(&b, &len);
1309 if ((session_id2 == NULL) ||
1310 (len != session_id2_len) ||
1311 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1315 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1317 p = buffer_get_cstring(&b, NULL);
1318 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1319 authctxt->style ? ":" : "",
1320 authctxt->style ? authctxt->style : "");
1321 if (strcmp(userstyle, p) != 0) {
1322 logit("wrong user name passed to monitor: expected %s != %.100s",
1328 buffer_skip_string(&b);
1329 if (datafellows & SSH_BUG_PKAUTH) {
1330 if (!buffer_get_char(&b))
1333 p = buffer_get_cstring(&b, NULL);
1334 if (strcmp("publickey", p) != 0)
1337 if (!buffer_get_char(&b))
1339 buffer_skip_string(&b);
1341 buffer_skip_string(&b);
1342 if (buffer_len(&b) != 0)
1349 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1353 char *p, *userstyle;
1358 buffer_append(&b, data, datalen);
1360 p = buffer_get_string(&b, &len);
1361 if ((session_id2 == NULL) ||
1362 (len != session_id2_len) ||
1363 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1367 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1369 p = buffer_get_cstring(&b, NULL);
1370 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1371 authctxt->style ? ":" : "",
1372 authctxt->style ? authctxt->style : "");
1373 if (strcmp(userstyle, p) != 0) {
1374 logit("wrong user name passed to monitor: expected %s != %.100s",
1380 buffer_skip_string(&b); /* service */
1381 p = buffer_get_cstring(&b, NULL);
1382 if (strcmp(p, "hostbased") != 0)
1385 buffer_skip_string(&b); /* pkalg */
1386 buffer_skip_string(&b); /* pkblob */
1388 /* verify client host, strip trailing dot if necessary */
1389 p = buffer_get_string(&b, NULL);
1390 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1392 if (strcmp(p, chost) != 0)
1396 /* verify client user */
1397 p = buffer_get_string(&b, NULL);
1398 if (strcmp(p, cuser) != 0)
1402 if (buffer_len(&b) != 0)
1409 mm_answer_keyverify(int sock, Buffer *m)
1412 u_char *signature, *data, *blob;
1413 u_int signaturelen, datalen, bloblen;
1417 blob = buffer_get_string(m, &bloblen);
1418 signature = buffer_get_string(m, &signaturelen);
1419 data = buffer_get_string(m, &datalen);
1421 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1422 !monitor_allowed_key(blob, bloblen))
1423 fatal("%s: bad key, not previously allowed", __func__);
1425 key = key_from_blob(blob, bloblen);
1427 fatal("%s: bad public key blob", __func__);
1429 switch (key_blobtype) {
1431 valid_data = monitor_valid_userblob(data, datalen);
1434 valid_data = monitor_valid_hostbasedblob(data, datalen,
1435 hostbased_cuser, hostbased_chost);
1442 fatal("%s: bad signature data blob", __func__);
1444 verified = key_verify(key, signature, signaturelen, data, datalen);
1445 debug3("%s: key %p signature %s",
1446 __func__, key, (verified == 1) ? "verified" : "unverified");
1448 /* If auth was successful then record key to ensure it isn't reused */
1449 if (verified == 1 && key_blobtype == MM_USERKEY)
1450 auth2_record_userkey(authctxt, key);
1458 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1460 monitor_reset_key_state();
1463 buffer_put_int(m, verified);
1464 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1466 return (verified == 1);
1470 mm_record_login(Session *s, struct passwd *pw)
1473 struct sockaddr_storage from;
1475 if (options.use_login)
1479 * Get IP address of client. If the connection is not a socket, let
1480 * the address be 0.0.0.0.
1482 memset(&from, 0, sizeof(from));
1483 fromlen = sizeof(from);
1484 if (packet_connection_is_on_socket()) {
1485 if (getpeername(packet_get_connection_in(),
1486 (struct sockaddr *)&from, &fromlen) < 0) {
1487 debug("getpeername: %.100s", strerror(errno));
1491 /* Record that there was a login on that tty from the remote host. */
1492 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1493 get_remote_name_or_ip(utmp_len, options.use_dns),
1494 (struct sockaddr *)&from, fromlen);
1498 mm_session_close(Session *s)
1500 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1501 if (s->ttyfd != -1) {
1502 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1503 session_pty_cleanup2(s);
1505 session_unused(s->self);
1509 mm_answer_pty(int sock, Buffer *m)
1511 extern struct monitor *pmonitor;
1515 debug3("%s entering", __func__);
1521 s->authctxt = authctxt;
1522 s->pw = authctxt->pw;
1523 s->pid = pmonitor->m_pid;
1524 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1527 pty_setowner(authctxt->pw, s->tty);
1529 buffer_put_int(m, 1);
1530 buffer_put_cstring(m, s->tty);
1532 /* We need to trick ttyslot */
1533 if (dup2(s->ttyfd, 0) == -1)
1534 fatal("%s: dup2", __func__);
1536 mm_record_login(s, authctxt->pw);
1538 /* Now we can close the file descriptor again */
1541 /* send messages generated by record_login */
1542 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1543 buffer_clear(&loginmsg);
1545 mm_request_send(sock, MONITOR_ANS_PTY, m);
1547 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1548 mm_send_fd(sock, s->ttyfd) == -1)
1549 fatal("%s: send fds failed", __func__);
1551 /* make sure nothing uses fd 0 */
1552 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1553 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1555 error("%s: fd0 %d != 0", __func__, fd0);
1557 /* slave is not needed */
1559 s->ttyfd = s->ptyfd;
1560 /* no need to dup() because nobody closes ptyfd */
1561 s->ptymaster = s->ptyfd;
1563 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1569 mm_session_close(s);
1570 buffer_put_int(m, 0);
1571 mm_request_send(sock, MONITOR_ANS_PTY, m);
1576 mm_answer_pty_cleanup(int sock, Buffer *m)
1581 debug3("%s entering", __func__);
1583 tty = buffer_get_string(m, NULL);
1584 if ((s = session_by_tty(tty)) != NULL)
1585 mm_session_close(s);
1593 mm_answer_sesskey(int sock, Buffer *m)
1598 /* Turn off permissions */
1599 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1601 if ((p = BN_new()) == NULL)
1602 fatal("%s: BN_new", __func__);
1604 buffer_get_bignum2(m, p);
1606 rsafail = ssh1_session_key(p);
1609 buffer_put_int(m, rsafail);
1610 buffer_put_bignum2(m, p);
1614 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1616 /* Turn on permissions for sessid passing */
1617 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1623 mm_answer_sessid(int sock, Buffer *m)
1627 debug3("%s entering", __func__);
1629 if (buffer_len(m) != 16)
1630 fatal("%s: bad ssh1 session id", __func__);
1631 for (i = 0; i < 16; i++)
1632 session_id[i] = buffer_get_char(m);
1634 /* Turn on permissions for getpwnam */
1635 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1641 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1645 u_char *blob = NULL;
1649 debug3("%s entering", __func__);
1651 auth_method = "rsa";
1652 if (options.rsa_authentication && authctxt->valid) {
1653 if ((client_n = BN_new()) == NULL)
1654 fatal("%s: BN_new", __func__);
1655 buffer_get_bignum2(m, client_n);
1656 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1657 BN_clear_free(client_n);
1660 buffer_put_int(m, allowed);
1661 buffer_put_int(m, forced_command != NULL);
1663 /* clear temporarily storage (used by generate challenge) */
1664 monitor_reset_key_state();
1666 if (allowed && key != NULL) {
1667 key->type = KEY_RSA; /* cheat for key_to_blob */
1668 if (key_to_blob(key, &blob, &blen) == 0)
1669 fatal("%s: key_to_blob failed", __func__);
1670 buffer_put_string(m, blob, blen);
1672 /* Save temporarily for comparison in verify */
1675 key_blobtype = MM_RSAUSERKEY;
1680 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1682 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1683 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1688 mm_answer_rsa_challenge(int sock, Buffer *m)
1694 debug3("%s entering", __func__);
1696 if (!authctxt->valid)
1697 fatal("%s: authctxt not valid", __func__);
1698 blob = buffer_get_string(m, &blen);
1699 if (!monitor_allowed_key(blob, blen))
1700 fatal("%s: bad key, not previously allowed", __func__);
1701 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1702 fatal("%s: key type mismatch", __func__);
1703 if ((key = key_from_blob(blob, blen)) == NULL)
1704 fatal("%s: received bad key", __func__);
1705 if (key->type != KEY_RSA)
1706 fatal("%s: received bad key type %d", __func__, key->type);
1707 key->type = KEY_RSA1;
1709 BN_clear_free(ssh1_challenge);
1710 ssh1_challenge = auth_rsa_generate_challenge(key);
1713 buffer_put_bignum2(m, ssh1_challenge);
1715 debug3("%s sending reply", __func__);
1716 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1718 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1726 mm_answer_rsa_response(int sock, Buffer *m)
1729 u_char *blob, *response;
1733 debug3("%s entering", __func__);
1735 if (!authctxt->valid)
1736 fatal("%s: authctxt not valid", __func__);
1737 if (ssh1_challenge == NULL)
1738 fatal("%s: no ssh1_challenge", __func__);
1740 blob = buffer_get_string(m, &blen);
1741 if (!monitor_allowed_key(blob, blen))
1742 fatal("%s: bad key, not previously allowed", __func__);
1743 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1744 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1745 if ((key = key_from_blob(blob, blen)) == NULL)
1746 fatal("%s: received bad key", __func__);
1747 response = buffer_get_string(m, &len);
1749 fatal("%s: received bad response to challenge", __func__);
1750 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1756 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1759 BN_clear_free(ssh1_challenge);
1760 ssh1_challenge = NULL;
1761 monitor_reset_key_state();
1764 buffer_put_int(m, success);
1765 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1772 mm_answer_term(int sock, Buffer *req)
1774 extern struct monitor *pmonitor;
1777 debug3("%s: tearing down sessions", __func__);
1779 /* The child is terminating */
1780 session_destroy_all(&mm_session_close);
1783 if (options.use_pam)
1787 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1791 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1793 /* Terminate process */
1797 #ifdef SSH_AUDIT_EVENTS
1798 /* Report that an audit event occurred */
1800 mm_answer_audit_event(int socket, Buffer *m)
1802 ssh_audit_event_t event;
1804 debug3("%s entering", __func__);
1806 event = buffer_get_int(m);
1808 case SSH_AUTH_FAIL_PUBKEY:
1809 case SSH_AUTH_FAIL_HOSTBASED:
1810 case SSH_AUTH_FAIL_GSSAPI:
1811 case SSH_LOGIN_EXCEED_MAXTRIES:
1812 case SSH_LOGIN_ROOT_DENIED:
1813 case SSH_CONNECTION_CLOSE:
1814 case SSH_INVALID_USER:
1818 fatal("Audit event type %d not permitted", event);
1825 mm_answer_audit_command(int socket, Buffer *m)
1830 debug3("%s entering", __func__);
1831 cmd = buffer_get_string(m, &len);
1832 /* sanity check command, if so how? */
1833 audit_run_command(cmd);
1837 #endif /* SSH_AUDIT_EVENTS */
1840 monitor_apply_keystate(struct monitor *pmonitor)
1842 struct ssh *ssh = active_state; /* XXX */
1846 debug3("%s: packet_set_state", __func__);
1847 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1848 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1849 sshbuf_free(child_state);
1852 if ((kex = ssh->kex) != NULL) {
1853 /* XXX set callbacks */
1855 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1856 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1857 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1858 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1859 # ifdef OPENSSL_HAS_ECC
1860 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
1862 #endif /* WITH_OPENSSL */
1863 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1864 kex->load_host_public_key=&get_hostkey_public_by_type;
1865 kex->load_host_private_key=&get_hostkey_private_by_type;
1866 kex->host_key_index=&get_hostkey_index;
1867 kex->sign = sshd_hostkey_sign;
1870 /* Update with new address */
1871 if (options.compression) {
1872 ssh_packet_set_compress_hooks(ssh, pmonitor->m_zlib,
1873 (ssh_packet_comp_alloc_func *)mm_zalloc,
1874 (ssh_packet_comp_free_func *)mm_zfree);
1878 /* This function requries careful sanity checking */
1881 mm_get_keystate(struct monitor *pmonitor)
1883 debug3("%s: Waiting for new keys", __func__);
1885 if ((child_state = sshbuf_new()) == NULL)
1886 fatal("%s: sshbuf_new failed", __func__);
1887 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1889 debug3("%s: GOT new keys", __func__);
1895 #define FD_CLOSEONEXEC(x) do { \
1896 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
1897 fatal("fcntl(%d, F_SETFD)", x); \
1901 monitor_openfds(struct monitor *mon, int do_logfds)
1905 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1906 fatal("%s: socketpair: %s", __func__, strerror(errno));
1907 FD_CLOSEONEXEC(pair[0]);
1908 FD_CLOSEONEXEC(pair[1]);
1909 mon->m_recvfd = pair[0];
1910 mon->m_sendfd = pair[1];
1913 if (pipe(pair) == -1)
1914 fatal("%s: pipe: %s", __func__, strerror(errno));
1915 FD_CLOSEONEXEC(pair[0]);
1916 FD_CLOSEONEXEC(pair[1]);
1917 mon->m_log_recvfd = pair[0];
1918 mon->m_log_sendfd = pair[1];
1920 mon->m_log_recvfd = mon->m_log_sendfd = -1;
1923 #define MM_MEMSIZE 65536
1928 struct ssh *ssh = active_state; /* XXX */
1929 struct monitor *mon;
1931 mon = xcalloc(1, sizeof(*mon));
1933 monitor_openfds(mon, 1);
1935 /* Used to share zlib space across processes */
1936 if (options.compression) {
1937 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1938 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1940 /* Compression needs to share state across borders */
1941 ssh_packet_set_compress_hooks(ssh, mon->m_zlib,
1942 (ssh_packet_comp_alloc_func *)mm_zalloc,
1943 (ssh_packet_comp_free_func *)mm_zfree);
1950 monitor_reinit(struct monitor *mon)
1952 monitor_openfds(mon, 0);
1957 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1963 goid.elements = buffer_get_string(m, &len);
1966 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1968 free(goid.elements);
1971 buffer_put_int(m, major);
1973 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1975 /* Now we have a context, enable the step */
1976 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1982 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1985 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1986 OM_uint32 major, minor;
1987 OM_uint32 flags = 0; /* GSI needs this */
1990 in.value = buffer_get_string(m, &len);
1992 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1996 buffer_put_int(m, major);
1997 buffer_put_string(m, out.value, out.length);
1998 buffer_put_int(m, flags);
1999 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
2001 gss_release_buffer(&minor, &out);
2003 if (major == GSS_S_COMPLETE) {
2004 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2005 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2006 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
2012 mm_answer_gss_checkmic(int sock, Buffer *m)
2014 gss_buffer_desc gssbuf, mic;
2018 gssbuf.value = buffer_get_string(m, &len);
2019 gssbuf.length = len;
2020 mic.value = buffer_get_string(m, &len);
2023 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
2029 buffer_put_int(m, ret);
2031 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
2033 if (!GSS_ERROR(ret))
2034 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2040 mm_answer_gss_userok(int sock, Buffer *m)
2044 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2047 buffer_put_int(m, authenticated);
2049 debug3("%s: sending result %d", __func__, authenticated);
2050 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
2052 auth_method = "gssapi-with-mic";
2054 /* Monitor loop will terminate if authenticated */
2055 return (authenticated);