1 /* $OpenBSD: monitor.c,v 1.197 2019/01/21 10:38:54 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>
53 # ifdef HAVE_SYS_POLL_H
54 # include <sys/poll.h>
59 #include <openssl/dh.h>
62 #include "openbsd-compat/sys-tree.h"
63 #include "openbsd-compat/sys-queue.h"
64 #include "openbsd-compat/openssl-compat.h"
78 #include "auth-options.h"
91 #include "monitor_wrap.h"
92 #include "monitor_fdpass.h"
100 static Gssctxt *gsscontext = NULL;
104 extern ServerOptions options;
105 extern u_int utmp_len;
106 extern u_char session_id[];
107 extern struct sshbuf *loginmsg;
108 extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */
110 /* State exported from the child */
111 static struct sshbuf *child_state;
113 /* Functions on the monitor that answer unprivileged requests */
115 int mm_answer_moduli(struct ssh *, int, struct sshbuf *);
116 int mm_answer_sign(struct ssh *, int, struct sshbuf *);
117 int mm_answer_pwnamallow(struct ssh *, int, struct sshbuf *);
118 int mm_answer_auth2_read_banner(struct ssh *, int, struct sshbuf *);
119 int mm_answer_authserv(struct ssh *, int, struct sshbuf *);
120 int mm_answer_authpassword(struct ssh *, int, struct sshbuf *);
121 int mm_answer_bsdauthquery(struct ssh *, int, struct sshbuf *);
122 int mm_answer_bsdauthrespond(struct ssh *, int, struct sshbuf *);
123 int mm_answer_skeyquery(struct ssh *, int, struct sshbuf *);
124 int mm_answer_skeyrespond(struct ssh *, int, struct sshbuf *);
125 int mm_answer_keyallowed(struct ssh *, int, struct sshbuf *);
126 int mm_answer_keyverify(struct ssh *, int, struct sshbuf *);
127 int mm_answer_pty(struct ssh *, int, struct sshbuf *);
128 int mm_answer_pty_cleanup(struct ssh *, int, struct sshbuf *);
129 int mm_answer_term(struct ssh *, int, struct sshbuf *);
130 int mm_answer_rsa_keyallowed(struct ssh *, int, struct sshbuf *);
131 int mm_answer_rsa_challenge(struct ssh *, int, struct sshbuf *);
132 int mm_answer_rsa_response(struct ssh *, int, struct sshbuf *);
133 int mm_answer_sesskey(struct ssh *, int, struct sshbuf *);
134 int mm_answer_sessid(struct ssh *, int, struct sshbuf *);
137 int mm_answer_pam_start(struct ssh *, int, struct sshbuf *);
138 int mm_answer_pam_account(struct ssh *, int, struct sshbuf *);
139 int mm_answer_pam_init_ctx(struct ssh *, int, struct sshbuf *);
140 int mm_answer_pam_query(struct ssh *, int, struct sshbuf *);
141 int mm_answer_pam_respond(struct ssh *, int, struct sshbuf *);
142 int mm_answer_pam_free_ctx(struct ssh *, int, struct sshbuf *);
146 int mm_answer_gss_setup_ctx(struct ssh *, int, struct sshbuf *);
147 int mm_answer_gss_accept_ctx(struct ssh *, int, struct sshbuf *);
148 int mm_answer_gss_userok(struct ssh *, int, struct sshbuf *);
149 int mm_answer_gss_checkmic(struct ssh *, int, struct sshbuf *);
152 #ifdef SSH_AUDIT_EVENTS
153 int mm_answer_audit_event(struct ssh *, int, struct sshbuf *);
154 int mm_answer_audit_command(struct ssh *, int, struct sshbuf *);
157 static Authctxt *authctxt;
159 /* local state for key verify */
160 static u_char *key_blob = NULL;
161 static size_t key_bloblen = 0;
162 static int key_blobtype = MM_NOKEY;
163 static struct sshauthopt *key_opts = NULL;
164 static char *hostbased_cuser = NULL;
165 static char *hostbased_chost = NULL;
166 static char *auth_method = "unknown";
167 static char *auth_submethod = NULL;
168 static u_int session_id2_len = 0;
169 static u_char *session_id2 = NULL;
170 static pid_t monitor_child_pid;
173 enum monitor_reqtype type;
175 int (*f)(struct ssh *, int, struct sshbuf *);
178 #define MON_ISAUTH 0x0004 /* Required for Authentication */
179 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
180 #define MON_ONCE 0x0010 /* Disable after calling */
181 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
183 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
185 #define MON_PERMIT 0x1000 /* Request is permitted */
187 static int monitor_read(struct ssh *, struct monitor *, struct mon_table *,
188 struct mon_table **);
189 static int monitor_read_log(struct monitor *);
191 struct mon_table mon_dispatch_proto20[] = {
193 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
195 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
196 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
197 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
198 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
199 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
201 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
202 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
203 {MONITOR_REQ_PAM_INIT_CTX, MON_ONCE, mm_answer_pam_init_ctx},
204 {MONITOR_REQ_PAM_QUERY, 0, mm_answer_pam_query},
205 {MONITOR_REQ_PAM_RESPOND, MON_ONCE, mm_answer_pam_respond},
206 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
208 #ifdef SSH_AUDIT_EVENTS
209 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
212 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
213 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
215 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
216 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
218 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
219 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
220 {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
221 {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
226 struct mon_table mon_dispatch_postauth20[] = {
228 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
230 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
231 {MONITOR_REQ_PTY, 0, mm_answer_pty},
232 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
233 {MONITOR_REQ_TERM, 0, mm_answer_term},
234 #ifdef SSH_AUDIT_EVENTS
235 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
236 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
241 struct mon_table *mon_dispatch;
243 /* Specifies if a certain message is allowed at the moment */
245 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
247 while (ent->f != NULL) {
248 if (ent->type == type) {
249 ent->flags &= ~MON_PERMIT;
250 ent->flags |= permit ? MON_PERMIT : 0;
258 monitor_permit_authentications(int permit)
260 struct mon_table *ent = mon_dispatch;
262 while (ent->f != NULL) {
263 if (ent->flags & MON_AUTH) {
264 ent->flags &= ~MON_PERMIT;
265 ent->flags |= permit ? MON_PERMIT : 0;
272 monitor_child_preauth(struct ssh *ssh, struct monitor *pmonitor)
274 struct mon_table *ent;
275 int authenticated = 0, partial = 0;
277 debug3("preauth child monitor started");
279 if (pmonitor->m_recvfd >= 0)
280 close(pmonitor->m_recvfd);
281 if (pmonitor->m_log_sendfd >= 0)
282 close(pmonitor->m_log_sendfd);
283 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
285 authctxt = (Authctxt *)ssh->authctxt;
286 memset(authctxt, 0, sizeof(*authctxt));
287 ssh->authctxt = authctxt;
289 authctxt->loginmsg = loginmsg;
291 mon_dispatch = mon_dispatch_proto20;
292 /* Permit requests for moduli and signatures */
293 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
294 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
296 /* The first few requests do not require asynchronous access */
297 while (!authenticated) {
299 auth_method = "unknown";
300 auth_submethod = NULL;
301 auth2_authctxt_reset_info(authctxt);
303 authenticated = (monitor_read(ssh, pmonitor,
304 mon_dispatch, &ent) == 1);
306 /* Special handling for multiple required authentications */
307 if (options.num_auth_methods != 0) {
309 !auth2_update_methods_lists(authctxt,
310 auth_method, auth_submethod)) {
311 debug3("%s: method %s: partial", __func__,
319 if (!(ent->flags & MON_AUTHDECIDE))
320 fatal("%s: unexpected authentication from %d",
321 __func__, ent->type);
322 if (authctxt->pw->pw_uid == 0 &&
323 !auth_root_allowed(ssh, auth_method))
326 /* PAM needs to perform account checks after auth */
327 if (options.use_pam && authenticated) {
330 if ((m = sshbuf_new()) == NULL)
331 fatal("%s: sshbuf_new failed",
333 mm_request_receive_expect(pmonitor->m_sendfd,
334 MONITOR_REQ_PAM_ACCOUNT, m);
335 authenticated = mm_answer_pam_account(
336 ssh, pmonitor->m_sendfd, m);
341 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
342 auth_log(ssh, authenticated, partial,
343 auth_method, auth_submethod);
344 if (!partial && !authenticated)
345 authctxt->failures++;
346 if (authenticated || partial) {
347 auth2_update_session_info(authctxt,
348 auth_method, auth_submethod);
353 if (!authctxt->valid)
354 fatal("%s: authenticated invalid user", __func__);
355 if (strcmp(auth_method, "unknown") == 0)
356 fatal("%s: authentication method name unknown", __func__);
358 debug("%s: %s has been authenticated by privileged process",
359 __func__, authctxt->user);
360 ssh->authctxt = NULL;
361 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
363 mm_get_keystate(ssh, pmonitor);
365 /* Drain any buffered messages from the child */
366 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
369 if (pmonitor->m_recvfd >= 0)
370 close(pmonitor->m_recvfd);
371 if (pmonitor->m_log_sendfd >= 0)
372 close(pmonitor->m_log_sendfd);
373 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
377 monitor_set_child_handler(pid_t pid)
379 monitor_child_pid = pid;
383 monitor_child_handler(int sig)
385 kill(monitor_child_pid, sig);
389 monitor_child_postauth(struct ssh *ssh, struct monitor *pmonitor)
391 close(pmonitor->m_recvfd);
392 pmonitor->m_recvfd = -1;
394 monitor_set_child_handler(pmonitor->m_pid);
395 signal(SIGHUP, &monitor_child_handler);
396 signal(SIGTERM, &monitor_child_handler);
397 signal(SIGINT, &monitor_child_handler);
399 signal(SIGXFSZ, SIG_IGN);
402 mon_dispatch = mon_dispatch_postauth20;
404 /* Permit requests for moduli and signatures */
405 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
406 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
407 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
409 if (auth_opts->permit_pty_flag) {
410 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
411 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
415 monitor_read(ssh, pmonitor, mon_dispatch, NULL);
419 monitor_read_log(struct monitor *pmonitor)
421 struct sshbuf *logmsg;
427 if ((logmsg = sshbuf_new()) == NULL)
428 fatal("%s: sshbuf_new", __func__);
431 if ((r = sshbuf_reserve(logmsg, 4, &p)) != 0)
432 fatal("%s: reserve: %s", __func__, ssh_err(r));
433 if (atomicio(read, pmonitor->m_log_recvfd, p, 4) != 4) {
434 if (errno == EPIPE) {
436 debug("%s: child log fd closed", __func__);
437 close(pmonitor->m_log_recvfd);
438 pmonitor->m_log_recvfd = -1;
441 fatal("%s: log fd read: %s", __func__, strerror(errno));
443 if ((r = sshbuf_get_u32(logmsg, &len)) != 0)
444 fatal("%s: get len: %s", __func__, ssh_err(r));
445 if (len <= 4 || len > 8192)
446 fatal("%s: invalid log message length %u", __func__, len);
448 /* Read severity, message */
449 sshbuf_reset(logmsg);
450 if ((r = sshbuf_reserve(logmsg, len, &p)) != 0)
451 fatal("%s: reserve: %s", __func__, ssh_err(r));
452 if (atomicio(read, pmonitor->m_log_recvfd, p, len) != len)
453 fatal("%s: log fd read: %s", __func__, strerror(errno));
454 if ((r = sshbuf_get_u32(logmsg, &level)) != 0 ||
455 (r = sshbuf_get_cstring(logmsg, &msg, NULL)) != 0)
456 fatal("%s: decode: %s", __func__, ssh_err(r));
459 if (log_level_name(level) == NULL)
460 fatal("%s: invalid log level %u (corrupted message?)",
462 do_log2(level, "%s [preauth]", msg);
471 monitor_read(struct ssh *ssh, struct monitor *pmonitor, struct mon_table *ent,
472 struct mon_table **pent)
477 struct pollfd pfd[2];
480 memset(&pfd, 0, sizeof(pfd));
481 pfd[0].fd = pmonitor->m_sendfd;
482 pfd[0].events = POLLIN;
483 pfd[1].fd = pmonitor->m_log_recvfd;
484 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
485 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
486 if (errno == EINTR || errno == EAGAIN)
488 fatal("%s: poll: %s", __func__, strerror(errno));
490 if (pfd[1].revents) {
492 * Drain all log messages before processing next
495 monitor_read_log(pmonitor);
499 break; /* Continues below */
502 if ((m = sshbuf_new()) == NULL)
503 fatal("%s: sshbuf_new", __func__);
505 mm_request_receive(pmonitor->m_sendfd, m);
506 if ((r = sshbuf_get_u8(m, &type)) != 0)
507 fatal("%s: decode: %s", __func__, ssh_err(r));
509 debug3("%s: checking request %d", __func__, type);
511 while (ent->f != NULL) {
512 if (ent->type == type)
517 if (ent->f != NULL) {
518 if (!(ent->flags & MON_PERMIT))
519 fatal("%s: unpermitted request %d", __func__,
521 ret = (*ent->f)(ssh, pmonitor->m_sendfd, m);
524 /* The child may use this request only once, disable it */
525 if (ent->flags & MON_ONCE) {
526 debug2("%s: %d used once, disabling now", __func__,
528 ent->flags &= ~MON_PERMIT;
537 fatal("%s: unsupported request: %d", __func__, type);
543 /* allowed key state */
545 monitor_allowed_key(u_char *blob, u_int bloblen)
547 /* make sure key is allowed */
548 if (key_blob == NULL || key_bloblen != bloblen ||
549 timingsafe_bcmp(key_blob, blob, key_bloblen))
555 monitor_reset_key_state(void)
559 free(hostbased_cuser);
560 free(hostbased_chost);
561 sshauthopt_free(key_opts);
564 key_blobtype = MM_NOKEY;
566 hostbased_cuser = NULL;
567 hostbased_chost = NULL;
572 mm_answer_moduli(struct ssh *ssh, int sock, struct sshbuf *m)
575 const BIGNUM *dh_p, *dh_g;
577 u_int min, want, max;
579 if ((r = sshbuf_get_u32(m, &min)) != 0 ||
580 (r = sshbuf_get_u32(m, &want)) != 0 ||
581 (r = sshbuf_get_u32(m, &max)) != 0)
582 fatal("%s: buffer error: %s", __func__, ssh_err(r));
584 debug3("%s: got parameters: %d %d %d",
585 __func__, min, want, max);
586 /* We need to check here, too, in case the child got corrupted */
587 if (max < min || want < min || max < want)
588 fatal("%s: bad parameters: %d %d %d",
589 __func__, min, want, max);
593 dh = choose_dh(min, want, max);
595 if ((r = sshbuf_put_u8(m, 0)) != 0)
596 fatal("%s: buffer error: %s", __func__, ssh_err(r));
599 /* Send first bignum */
600 DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
601 if ((r = sshbuf_put_u8(m, 1)) != 0 ||
602 (r = sshbuf_put_bignum2(m, dh_p)) != 0 ||
603 (r = sshbuf_put_bignum2(m, dh_g)) != 0)
604 fatal("%s: buffer error: %s", __func__, ssh_err(r));
608 mm_request_send(sock, MONITOR_ANS_MODULI, m);
614 mm_answer_sign(struct ssh *ssh, int sock, struct sshbuf *m)
616 extern int auth_sock; /* XXX move to state struct? */
618 struct sshbuf *sigbuf = NULL;
619 u_char *p = NULL, *signature = NULL;
621 size_t datlen, siglen, alglen;
624 const char proof_req[] = "hostkeys-prove-00@openssh.com";
626 debug3("%s", __func__);
628 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
629 (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
630 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0 ||
631 (r = sshbuf_get_u32(m, &compat)) != 0)
632 fatal("%s: buffer error: %s", __func__, ssh_err(r));
634 fatal("%s: invalid key ID", __func__);
637 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
638 * SHA384 (48 bytes) and SHA512 (64 bytes).
640 * Otherwise, verify the signature request is for a hostkey
643 * XXX perform similar check for KEX signature requests too?
644 * it's not trivial, since what is signed is the hash, rather
645 * than the full kex structure...
647 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
649 * Construct expected hostkey proof and compare it to what
650 * the client sent us.
652 if (session_id2_len == 0) /* hostkeys is never first */
653 fatal("%s: bad data length: %zu", __func__, datlen);
654 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
655 fatal("%s: no hostkey for index %d", __func__, keyid);
656 if ((sigbuf = sshbuf_new()) == NULL)
657 fatal("%s: sshbuf_new", __func__);
658 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
659 (r = sshbuf_put_string(sigbuf, session_id2,
660 session_id2_len)) != 0 ||
661 (r = sshkey_puts(key, sigbuf)) != 0)
662 fatal("%s: couldn't prepare private key "
663 "proof buffer: %s", __func__, ssh_err(r));
664 if (datlen != sshbuf_len(sigbuf) ||
665 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
666 fatal("%s: bad data length: %zu, hostkey proof len %zu",
667 __func__, datlen, sshbuf_len(sigbuf));
672 /* save session id, it will be passed on the first call */
673 if (session_id2_len == 0) {
674 session_id2_len = datlen;
675 session_id2 = xmalloc(session_id2_len);
676 memcpy(session_id2, p, session_id2_len);
679 if ((key = get_hostkey_by_index(keyid)) != NULL) {
680 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
682 fatal("%s: sshkey_sign failed: %s",
683 __func__, ssh_err(r));
684 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
686 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
687 p, datlen, alg, compat)) != 0) {
688 fatal("%s: ssh_agent_sign failed: %s",
689 __func__, ssh_err(r));
692 fatal("%s: no hostkey from index %d", __func__, keyid);
694 debug3("%s: %s signature %p(%zu)", __func__,
695 is_proof ? "KEX" : "hostkey proof", signature, siglen);
698 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
699 fatal("%s: buffer error: %s", __func__, ssh_err(r));
705 mm_request_send(sock, MONITOR_ANS_SIGN, m);
707 /* Turn on permissions for getpwnam */
708 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
713 /* Retrieves the password entry and also checks if the user is permitted */
716 mm_answer_pwnamallow(struct ssh *ssh, int sock, struct sshbuf *m)
719 struct passwd *pwent;
723 debug3("%s", __func__);
725 if (authctxt->attempt++ != 0)
726 fatal("%s: multiple attempts for getpwnam", __func__);
728 if ((r = sshbuf_get_cstring(m, &username, NULL)) != 0)
729 fatal("%s: buffer error: %s", __func__, ssh_err(r));
731 pwent = getpwnamallow(ssh, username);
733 authctxt->user = xstrdup(username);
734 setproctitle("%s [priv]", pwent ? username : "unknown");
740 if ((r = sshbuf_put_u8(m, 0)) != 0)
741 fatal("%s: buffer error: %s", __func__, ssh_err(r));
742 authctxt->pw = fakepw();
747 authctxt->pw = pwent;
750 /* XXX don't sent pwent to unpriv; send fake class/dir/shell too */
751 if ((r = sshbuf_put_u8(m, 1)) != 0 ||
752 (r = sshbuf_put_string(m, pwent, sizeof(*pwent))) != 0 ||
753 (r = sshbuf_put_cstring(m, pwent->pw_name)) != 0 ||
754 (r = sshbuf_put_cstring(m, "*")) != 0 ||
755 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
756 (r = sshbuf_put_cstring(m, pwent->pw_gecos)) != 0 ||
758 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
759 (r = sshbuf_put_cstring(m, pwent->pw_class)) != 0 ||
761 (r = sshbuf_put_cstring(m, pwent->pw_dir)) != 0 ||
762 (r = sshbuf_put_cstring(m, pwent->pw_shell)) != 0)
763 fatal("%s: buffer error: %s", __func__, ssh_err(r));
766 ssh_packet_set_log_preamble(ssh, "%suser %s",
767 authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
768 if ((r = sshbuf_put_string(m, &options, sizeof(options))) != 0)
769 fatal("%s: buffer error: %s", __func__, ssh_err(r));
771 #define M_CP_STROPT(x) do { \
772 if (options.x != NULL) { \
773 if ((r = sshbuf_put_cstring(m, options.x)) != 0) \
774 fatal("%s: buffer error: %s", \
775 __func__, ssh_err(r)); \
778 #define M_CP_STRARRAYOPT(x, nx) do { \
779 for (i = 0; i < options.nx; i++) { \
780 if ((r = sshbuf_put_cstring(m, options.x[i])) != 0) \
781 fatal("%s: buffer error: %s", \
782 __func__, ssh_err(r)); \
785 /* See comment in servconf.h */
786 COPY_MATCH_STRING_OPTS();
788 #undef M_CP_STRARRAYOPT
790 /* Create valid auth method lists */
791 if (auth2_setup_methods_lists(authctxt) != 0) {
793 * The monitor will continue long enough to let the child
794 * run to it's packet_disconnect(), but it must not allow any
795 * authentication to succeed.
797 debug("%s: no valid authentication method lists", __func__);
800 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
801 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
803 /* Allow service/style information on the auth context */
804 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
805 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
809 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
815 int mm_answer_auth2_read_banner(struct ssh *ssh, int sock, struct sshbuf *m)
821 banner = auth2_read_banner();
822 if ((r = sshbuf_put_cstring(m, banner != NULL ? banner : "")) != 0)
823 fatal("%s: buffer error: %s", __func__, ssh_err(r));
824 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
831 mm_answer_authserv(struct ssh *ssh, int sock, struct sshbuf *m)
835 monitor_permit_authentications(1);
837 if ((r = sshbuf_get_cstring(m, &authctxt->service, NULL)) != 0 ||
838 (r = sshbuf_get_cstring(m, &authctxt->style, NULL)) != 0)
839 fatal("%s: buffer error: %s", __func__, ssh_err(r));
840 debug3("%s: service=%s, style=%s",
841 __func__, authctxt->service, authctxt->style);
843 if (strlen(authctxt->style) == 0) {
844 free(authctxt->style);
845 authctxt->style = NULL;
852 * Check that the key type appears in the supplied pattern list, ignoring
853 * mismatches in the signature algorithm. (Signature algorithm checks are
854 * performed in the unprivileged authentication code).
855 * Returns 1 on success, 0 otherwise.
858 key_base_type_match(const char *method, const struct sshkey *key,
861 char *s, *l, *ol = xstrdup(list);
865 for ((s = strsep(&l, ",")); s && *s != '\0'; (s = strsep(&l, ","))) {
866 if (sshkey_type_from_name(s) == key->type) {
872 error("%s key type %s is not in permitted list %s", method,
873 sshkey_ssh_name(key), list);
881 mm_answer_authpassword(struct ssh *ssh, int sock, struct sshbuf *m)
883 static int call_count;
885 int r, authenticated;
888 if (!options.password_authentication)
889 fatal("%s: password authentication not enabled", __func__);
890 if ((r = sshbuf_get_cstring(m, &passwd, &plen)) != 0)
891 fatal("%s: buffer error: %s", __func__, ssh_err(r));
892 /* Only authenticate if the context is valid */
893 authenticated = options.password_authentication &&
894 auth_password(ssh, passwd);
895 explicit_bzero(passwd, plen);
899 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
900 fatal("%s: buffer error: %s", __func__, ssh_err(r));
902 if ((r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0)
903 fatal("%s: buffer error: %s", __func__, ssh_err(r));
906 debug3("%s: sending result %d", __func__, authenticated);
907 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
910 if (plen == 0 && call_count == 1)
911 auth_method = "none";
913 auth_method = "password";
915 /* Causes monitor loop to terminate if authenticated */
916 return (authenticated);
921 mm_answer_bsdauthquery(struct ssh *ssh, int sock, struct sshbuf *m)
923 char *name, *infotxt;
924 u_int numprompts, *echo_on, success;
928 if (!options.kbd_interactive_authentication)
929 fatal("%s: kbd-int authentication not enabled", __func__);
930 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
931 &prompts, &echo_on) < 0 ? 0 : 1;
934 if ((r = sshbuf_put_u32(m, success)) != 0)
935 fatal("%s: buffer error: %s", __func__, ssh_err(r));
937 if ((r = sshbuf_put_cstring(m, prompts[0])) != 0)
938 fatal("%s: buffer error: %s", __func__, ssh_err(r));
941 debug3("%s: sending challenge success: %u", __func__, success);
942 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
955 mm_answer_bsdauthrespond(struct ssh *ssh, int sock, struct sshbuf *m)
960 if (!options.kbd_interactive_authentication)
961 fatal("%s: kbd-int authentication not enabled", __func__);
962 if (authctxt->as == NULL)
963 fatal("%s: no bsd auth session", __func__);
965 if ((r = sshbuf_get_cstring(m, &response, NULL)) != 0)
966 fatal("%s: buffer error: %s", __func__, ssh_err(r));
967 authok = options.challenge_response_authentication &&
968 auth_userresponse(authctxt->as, response, 0);
970 debug3("%s: <%s> = <%d>", __func__, response, authok);
974 if ((r = sshbuf_put_u32(m, authok)) != 0)
975 fatal("%s: buffer error: %s", __func__, ssh_err(r));
977 debug3("%s: sending authenticated: %d", __func__, authok);
978 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
980 auth_method = "keyboard-interactive";
981 auth_submethod = "bsdauth";
983 return (authok != 0);
989 mm_answer_pam_start(struct ssh *ssh, int sock, struct sshbuf *m)
991 if (!options.use_pam)
992 fatal("UsePAM not set, but ended up in %s anyway", __func__);
996 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
997 if (options.kbd_interactive_authentication)
998 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
1004 mm_answer_pam_account(struct ssh *ssh, int sock, struct sshbuf *m)
1009 if (!options.use_pam)
1010 fatal("%s: PAM not enabled", __func__);
1012 ret = do_pam_account();
1014 if ((r = sshbuf_put_u32(m, ret)) != 0 ||
1015 (r = sshbuf_put_stringb(m, loginmsg)) != 0)
1016 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1018 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
1023 static void *sshpam_ctxt, *sshpam_authok;
1024 extern KbdintDevice sshpam_device;
1027 mm_answer_pam_init_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
1032 debug3("%s", __func__);
1033 if (!options.kbd_interactive_authentication)
1034 fatal("%s: kbd-int authentication not enabled", __func__);
1035 if (sshpam_ctxt != NULL)
1036 fatal("%s: already called", __func__);
1037 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1038 sshpam_authok = NULL;
1040 if (sshpam_ctxt != NULL) {
1041 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1042 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
1045 if ((r = sshbuf_put_u32(m, ok)) != 0)
1046 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1047 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
1052 mm_answer_pam_query(struct ssh *ssh, int sock, struct sshbuf *m)
1054 char *name = NULL, *info = NULL, **prompts = NULL;
1055 u_int i, num = 0, *echo_on = 0;
1058 debug3("%s", __func__);
1059 sshpam_authok = NULL;
1060 if (sshpam_ctxt == NULL)
1061 fatal("%s: no context", __func__);
1062 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info,
1063 &num, &prompts, &echo_on);
1064 if (ret == 0 && num == 0)
1065 sshpam_authok = sshpam_ctxt;
1066 if (num > 1 || name == NULL || info == NULL)
1067 fatal("sshpam_device.query failed");
1068 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
1070 if ((r = sshbuf_put_u32(m, ret)) != 0 ||
1071 (r = sshbuf_put_cstring(m, name)) != 0 ||
1072 (r = sshbuf_put_cstring(m, info)) != 0 ||
1073 (r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0 ||
1074 (r = sshbuf_put_u32(m, num)) != 0)
1075 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1078 for (i = 0; i < num; ++i) {
1079 if ((r = sshbuf_put_cstring(m, prompts[i])) != 0 ||
1080 (r = sshbuf_put_u32(m, echo_on[i])) != 0)
1081 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1086 auth_method = "keyboard-interactive";
1087 auth_submethod = "pam";
1088 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
1093 mm_answer_pam_respond(struct ssh *ssh, int sock, struct sshbuf *m)
1099 debug3("%s", __func__);
1100 if (sshpam_ctxt == NULL)
1101 fatal("%s: no context", __func__);
1102 sshpam_authok = NULL;
1103 if ((r = sshbuf_get_u32(m, &num)) != 0)
1104 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1106 resp = xcalloc(num, sizeof(char *));
1107 for (i = 0; i < num; ++i) {
1108 if ((r = sshbuf_get_cstring(m, &(resp[i]), NULL)) != 0)
1109 fatal("%s: buffer error: %s",
1110 __func__, ssh_err(r));
1112 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1113 for (i = 0; i < num; ++i)
1117 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1120 if ((r = sshbuf_put_u32(m, ret)) != 0)
1121 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1122 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
1123 auth_method = "keyboard-interactive";
1124 auth_submethod = "pam";
1126 sshpam_authok = sshpam_ctxt;
1131 mm_answer_pam_free_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
1133 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
1135 debug3("%s", __func__);
1136 if (sshpam_ctxt == NULL)
1137 fatal("%s: no context", __func__);
1138 (sshpam_device.free_ctx)(sshpam_ctxt);
1139 sshpam_ctxt = sshpam_authok = NULL;
1141 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
1142 /* Allow another attempt */
1143 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
1144 auth_method = "keyboard-interactive";
1145 auth_submethod = "pam";
1151 mm_answer_keyallowed(struct ssh *ssh, int sock, struct sshbuf *m)
1153 struct sshkey *key = NULL;
1154 char *cuser, *chost;
1155 u_int pubkey_auth_attempt;
1156 enum mm_keytype type = 0;
1158 struct sshauthopt *opts = NULL;
1160 debug3("%s entering", __func__);
1161 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1162 (r = sshbuf_get_cstring(m, &cuser, NULL)) != 0 ||
1163 (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
1164 (r = sshkey_froms(m, &key)) != 0 ||
1165 (r = sshbuf_get_u32(m, &pubkey_auth_attempt)) != 0)
1166 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1168 debug3("%s: key_from_blob: %p", __func__, key);
1170 if (key != NULL && authctxt->valid) {
1171 /* These should not make it past the privsep child */
1172 if (sshkey_type_plain(key->type) == KEY_RSA &&
1173 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1174 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1178 auth_method = "publickey";
1179 if (!options.pubkey_authentication)
1181 if (auth2_key_already_used(authctxt, key))
1183 if (!key_base_type_match(auth_method, key,
1184 options.pubkey_key_types))
1186 allowed = user_key_allowed(ssh, authctxt->pw, key,
1187 pubkey_auth_attempt, &opts);
1190 auth_method = "hostbased";
1191 if (!options.hostbased_authentication)
1193 if (auth2_key_already_used(authctxt, key))
1195 if (!key_base_type_match(auth_method, key,
1196 options.hostbased_key_types))
1198 allowed = hostbased_key_allowed(ssh, authctxt->pw,
1200 auth2_record_info(authctxt,
1201 "client user \"%.100s\", client host \"%.100s\"",
1205 fatal("%s: unknown key type %d", __func__, type);
1210 debug3("%s: %s authentication%s: %s key is %s", __func__,
1211 auth_method, pubkey_auth_attempt ? "" : " test",
1212 (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key),
1213 allowed ? "allowed" : "not allowed");
1215 auth2_record_key(authctxt, 0, key);
1217 /* clear temporarily storage (used by verify) */
1218 monitor_reset_key_state();
1221 /* Save temporarily for comparison in verify */
1222 if ((r = sshkey_to_blob(key, &key_blob, &key_bloblen)) != 0)
1223 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1224 key_blobtype = type;
1226 hostbased_cuser = cuser;
1227 hostbased_chost = chost;
1229 /* Log failed attempt */
1230 auth_log(ssh, 0, 0, auth_method, NULL);
1237 if ((r = sshbuf_put_u32(m, allowed)) != 0)
1238 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1239 if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0)
1240 fatal("%s: sshauthopt_serialise: %s", __func__, ssh_err(r));
1241 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1244 sshauthopt_free(opts);
1250 monitor_valid_userblob(u_char *data, u_int datalen)
1254 char *userstyle, *cp;
1259 if ((b = sshbuf_new()) == NULL)
1260 fatal("%s: sshbuf_new", __func__);
1261 if ((r = sshbuf_put(b, data, datalen)) != 0)
1262 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1264 if (datafellows & SSH_OLD_SESSIONID) {
1266 len = sshbuf_len(b);
1267 if ((session_id2 == NULL) ||
1268 (len < session_id2_len) ||
1269 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1271 if ((r = sshbuf_consume(b, session_id2_len)) != 0)
1272 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1274 if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1275 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1276 if ((session_id2 == NULL) ||
1277 (len != session_id2_len) ||
1278 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1281 if ((r = sshbuf_get_u8(b, &type)) != 0)
1282 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1283 if (type != SSH2_MSG_USERAUTH_REQUEST)
1285 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1286 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1287 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1288 authctxt->style ? ":" : "",
1289 authctxt->style ? authctxt->style : "");
1290 if (strcmp(userstyle, cp) != 0) {
1291 logit("wrong user name passed to monitor: "
1292 "expected %s != %.100s", userstyle, cp);
1297 if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1298 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1299 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1300 if (strcmp("publickey", cp) != 0)
1303 if ((r = sshbuf_get_u8(b, &type)) != 0)
1304 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1307 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1308 (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1309 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1310 if (sshbuf_len(b) != 0)
1317 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1322 char *cp, *userstyle;
1327 if ((b = sshbuf_new()) == NULL)
1328 fatal("%s: sshbuf_new", __func__);
1329 if ((r = sshbuf_put(b, data, datalen)) != 0 ||
1330 (r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1331 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1333 if ((session_id2 == NULL) ||
1334 (len != session_id2_len) ||
1335 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1338 if ((r = sshbuf_get_u8(b, &type)) != 0)
1339 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1340 if (type != SSH2_MSG_USERAUTH_REQUEST)
1342 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1343 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1344 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1345 authctxt->style ? ":" : "",
1346 authctxt->style ? authctxt->style : "");
1347 if (strcmp(userstyle, cp) != 0) {
1348 logit("wrong user name passed to monitor: "
1349 "expected %s != %.100s", userstyle, cp);
1354 if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1355 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1356 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1357 if (strcmp(cp, "hostbased") != 0)
1360 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1361 (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1362 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1364 /* verify client host, strip trailing dot if necessary */
1365 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1366 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1367 if (((len = strlen(cp)) > 0) && cp[len - 1] == '.')
1369 if (strcmp(cp, chost) != 0)
1373 /* verify client user */
1374 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1375 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1376 if (strcmp(cp, cuser) != 0)
1380 if (sshbuf_len(b) != 0)
1387 mm_answer_keyverify(struct ssh *ssh, int sock, struct sshbuf *m)
1390 u_char *signature, *data, *blob;
1392 size_t signaturelen, datalen, bloblen;
1393 int r, ret, valid_data = 0, encoded_ret;
1395 if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
1396 (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 ||
1397 (r = sshbuf_get_string(m, &data, &datalen)) != 0 ||
1398 (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
1399 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1401 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1402 !monitor_allowed_key(blob, bloblen))
1403 fatal("%s: bad key, not previously allowed", __func__);
1405 /* Empty signature algorithm means NULL. */
1406 if (*sigalg == '\0') {
1411 /* XXX use sshkey_froms here; need to change key_blob, etc. */
1412 if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1413 fatal("%s: bad public key blob: %s", __func__, ssh_err(r));
1415 switch (key_blobtype) {
1417 valid_data = monitor_valid_userblob(data, datalen);
1418 auth_method = "publickey";
1421 valid_data = monitor_valid_hostbasedblob(data, datalen,
1422 hostbased_cuser, hostbased_chost);
1423 auth_method = "hostbased";
1430 fatal("%s: bad signature data blob", __func__);
1432 ret = sshkey_verify(key, signature, signaturelen, data, datalen,
1433 sigalg, ssh->compat);
1434 debug3("%s: %s %p signature %s", __func__, auth_method, key,
1435 (ret == 0) ? "verified" : "unverified");
1436 auth2_record_key(authctxt, ret == 0, key);
1443 if (key_blobtype == MM_USERKEY)
1444 auth_activate_options(ssh, key_opts);
1445 monitor_reset_key_state();
1450 /* encode ret != 0 as positive integer, since we're sending u32 */
1451 encoded_ret = (ret != 0);
1452 if ((r = sshbuf_put_u32(m, encoded_ret)) != 0)
1453 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1454 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1460 mm_record_login(struct ssh *ssh, Session *s, struct passwd *pw)
1463 struct sockaddr_storage from;
1466 * Get IP address of client. If the connection is not a socket, let
1467 * the address be 0.0.0.0.
1469 memset(&from, 0, sizeof(from));
1470 fromlen = sizeof(from);
1471 if (ssh_packet_connection_is_on_socket(ssh)) {
1472 if (getpeername(ssh_packet_get_connection_in(ssh),
1473 (struct sockaddr *)&from, &fromlen) < 0) {
1474 debug("getpeername: %.100s", strerror(errno));
1478 /* Record that there was a login on that tty from the remote host. */
1479 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1480 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
1481 (struct sockaddr *)&from, fromlen);
1485 mm_session_close(Session *s)
1487 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1488 if (s->ttyfd != -1) {
1489 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1490 session_pty_cleanup2(s);
1492 session_unused(s->self);
1496 mm_answer_pty(struct ssh *ssh, int sock, struct sshbuf *m)
1498 extern struct monitor *pmonitor;
1502 debug3("%s entering", __func__);
1508 s->authctxt = authctxt;
1509 s->pw = authctxt->pw;
1510 s->pid = pmonitor->m_pid;
1511 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1514 pty_setowner(authctxt->pw, s->tty);
1516 if ((r = sshbuf_put_u32(m, 1)) != 0 ||
1517 (r = sshbuf_put_cstring(m, s->tty)) != 0)
1518 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1520 /* We need to trick ttyslot */
1521 if (dup2(s->ttyfd, 0) == -1)
1522 fatal("%s: dup2", __func__);
1524 mm_record_login(ssh, s, authctxt->pw);
1526 /* Now we can close the file descriptor again */
1529 /* send messages generated by record_login */
1530 if ((r = sshbuf_put_stringb(m, loginmsg)) != 0)
1531 fatal("%s: put login message: %s", __func__, ssh_err(r));
1532 sshbuf_reset(loginmsg);
1534 mm_request_send(sock, MONITOR_ANS_PTY, m);
1536 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1537 mm_send_fd(sock, s->ttyfd) == -1)
1538 fatal("%s: send fds failed", __func__);
1540 /* make sure nothing uses fd 0 */
1541 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1542 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1544 error("%s: fd0 %d != 0", __func__, fd0);
1546 /* slave is not needed */
1548 s->ttyfd = s->ptyfd;
1549 /* no need to dup() because nobody closes ptyfd */
1550 s->ptymaster = s->ptyfd;
1552 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1558 mm_session_close(s);
1559 if ((r = sshbuf_put_u32(m, 0)) != 0)
1560 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1561 mm_request_send(sock, MONITOR_ANS_PTY, m);
1566 mm_answer_pty_cleanup(struct ssh *ssh, int sock, struct sshbuf *m)
1572 debug3("%s entering", __func__);
1574 if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
1575 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1576 if ((s = session_by_tty(tty)) != NULL)
1577 mm_session_close(s);
1584 mm_answer_term(struct ssh *ssh, int sock, struct sshbuf *req)
1586 extern struct monitor *pmonitor;
1589 debug3("%s: tearing down sessions", __func__);
1591 /* The child is terminating */
1592 session_destroy_all(ssh, &mm_session_close);
1595 if (options.use_pam)
1599 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1603 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1605 /* Terminate process */
1609 #ifdef SSH_AUDIT_EVENTS
1610 /* Report that an audit event occurred */
1612 mm_answer_audit_event(struct ssh *ssh, int socket, struct sshbuf *m)
1615 ssh_audit_event_t event;
1618 debug3("%s entering", __func__);
1620 if ((r = sshbuf_get_u32(m, &n)) != 0)
1621 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1622 event = (ssh_audit_event_t)n;
1624 case SSH_AUTH_FAIL_PUBKEY:
1625 case SSH_AUTH_FAIL_HOSTBASED:
1626 case SSH_AUTH_FAIL_GSSAPI:
1627 case SSH_LOGIN_EXCEED_MAXTRIES:
1628 case SSH_LOGIN_ROOT_DENIED:
1629 case SSH_CONNECTION_CLOSE:
1630 case SSH_INVALID_USER:
1631 audit_event(ssh, event);
1634 fatal("Audit event type %d not permitted", event);
1641 mm_answer_audit_command(struct ssh *ssh, int socket, struct sshbuf *m)
1646 debug3("%s entering", __func__);
1647 if ((r = sshbuf_get_cstring(m, &cmd, NULL)) != 0)
1648 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1649 /* sanity check command, if so how? */
1650 audit_run_command(cmd);
1654 #endif /* SSH_AUDIT_EVENTS */
1657 monitor_clear_keystate(struct ssh *ssh, struct monitor *pmonitor)
1659 ssh_clear_newkeys(ssh, MODE_IN);
1660 ssh_clear_newkeys(ssh, MODE_OUT);
1661 sshbuf_free(child_state);
1666 monitor_apply_keystate(struct ssh *ssh, struct monitor *pmonitor)
1671 debug3("%s: packet_set_state", __func__);
1672 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1673 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1674 sshbuf_free(child_state);
1677 if ((kex = ssh->kex) != NULL) {
1678 /* XXX set callbacks */
1680 kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_server;
1681 kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_server;
1682 kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_server;
1683 kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_server;
1684 kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_server;
1685 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1686 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1687 # ifdef OPENSSL_HAS_ECC
1688 kex->kex[KEX_ECDH_SHA2] = kex_gen_server;
1690 #endif /* WITH_OPENSSL */
1691 kex->kex[KEX_C25519_SHA256] = kex_gen_server;
1692 kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_server;
1693 kex->load_host_public_key=&get_hostkey_public_by_type;
1694 kex->load_host_private_key=&get_hostkey_private_by_type;
1695 kex->host_key_index=&get_hostkey_index;
1696 kex->sign = sshd_hostkey_sign;
1700 /* This function requries careful sanity checking */
1703 mm_get_keystate(struct ssh *ssh, struct monitor *pmonitor)
1705 debug3("%s: Waiting for new keys", __func__);
1707 if ((child_state = sshbuf_new()) == NULL)
1708 fatal("%s: sshbuf_new failed", __func__);
1709 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1711 debug3("%s: GOT new keys", __func__);
1717 #define FD_CLOSEONEXEC(x) do { \
1718 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
1719 fatal("fcntl(%d, F_SETFD)", x); \
1723 monitor_openfds(struct monitor *mon, int do_logfds)
1730 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1731 fatal("%s: socketpair: %s", __func__, strerror(errno));
1733 if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1734 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
1735 if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1736 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
1738 FD_CLOSEONEXEC(pair[0]);
1739 FD_CLOSEONEXEC(pair[1]);
1740 mon->m_recvfd = pair[0];
1741 mon->m_sendfd = pair[1];
1744 if (pipe(pair) == -1)
1745 fatal("%s: pipe: %s", __func__, strerror(errno));
1746 FD_CLOSEONEXEC(pair[0]);
1747 FD_CLOSEONEXEC(pair[1]);
1748 mon->m_log_recvfd = pair[0];
1749 mon->m_log_sendfd = pair[1];
1751 mon->m_log_recvfd = mon->m_log_sendfd = -1;
1754 #define MM_MEMSIZE 65536
1759 struct monitor *mon;
1761 mon = xcalloc(1, sizeof(*mon));
1762 monitor_openfds(mon, 1);
1768 monitor_reinit(struct monitor *mon)
1770 monitor_openfds(mon, 0);
1775 mm_answer_gss_setup_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
1783 if (!options.gss_authentication)
1784 fatal("%s: GSSAPI authentication not enabled", __func__);
1786 if ((r = sshbuf_get_string(m, &p, &len)) != 0)
1787 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1791 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1793 free(goid.elements);
1796 if ((r = sshbuf_put_u32(m, major)) != 0)
1797 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1799 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1801 /* Now we have a context, enable the step */
1802 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1808 mm_answer_gss_accept_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
1811 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1812 OM_uint32 major, minor;
1813 OM_uint32 flags = 0; /* GSI needs this */
1816 if (!options.gss_authentication)
1817 fatal("%s: GSSAPI authentication not enabled", __func__);
1819 if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0)
1820 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1821 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1825 if ((r = sshbuf_put_u32(m, major)) != 0 ||
1826 (r = sshbuf_put_string(m, out.value, out.length)) != 0 ||
1827 (r = sshbuf_put_u32(m, flags)) != 0)
1828 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1829 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1831 gss_release_buffer(&minor, &out);
1833 if (major == GSS_S_COMPLETE) {
1834 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1835 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1836 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1842 mm_answer_gss_checkmic(struct ssh *ssh, int sock, struct sshbuf *m)
1844 gss_buffer_desc gssbuf, mic;
1848 if (!options.gss_authentication)
1849 fatal("%s: GSSAPI authentication not enabled", __func__);
1851 if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 ||
1852 (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0)
1853 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1855 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1861 if ((r = sshbuf_put_u32(m, ret)) != 0)
1862 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1864 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1866 if (!GSS_ERROR(ret))
1867 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1873 mm_answer_gss_userok(struct ssh *ssh, int sock, struct sshbuf *m)
1875 int r, authenticated;
1876 const char *displayname;
1878 if (!options.gss_authentication)
1879 fatal("%s: GSSAPI authentication not enabled", __func__);
1881 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1884 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
1885 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1887 debug3("%s: sending result %d", __func__, authenticated);
1888 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1890 auth_method = "gssapi-with-mic";
1892 if ((displayname = ssh_gssapi_displayname()) != NULL)
1893 auth2_record_info(authctxt, "%s", displayname);
1895 /* Monitor loop will terminate if authenticated */
1896 return (authenticated);