1 /* $OpenBSD: monitor.c,v 1.236 2023/05/10 10:04:20 dtucker 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"
101 static Gssctxt *gsscontext = NULL;
105 extern ServerOptions options;
106 extern u_int utmp_len;
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_keyallowed(struct ssh *, int, struct sshbuf *);
124 int mm_answer_keyverify(struct ssh *, int, struct sshbuf *);
125 int mm_answer_pty(struct ssh *, int, struct sshbuf *);
126 int mm_answer_pty_cleanup(struct ssh *, int, struct sshbuf *);
127 int mm_answer_term(struct ssh *, int, struct sshbuf *);
128 int mm_answer_sesskey(struct ssh *, int, struct sshbuf *);
129 int mm_answer_sessid(struct ssh *, int, struct sshbuf *);
132 int mm_answer_pam_start(struct ssh *, int, struct sshbuf *);
133 int mm_answer_pam_account(struct ssh *, int, struct sshbuf *);
134 int mm_answer_pam_init_ctx(struct ssh *, int, struct sshbuf *);
135 int mm_answer_pam_query(struct ssh *, int, struct sshbuf *);
136 int mm_answer_pam_respond(struct ssh *, int, struct sshbuf *);
137 int mm_answer_pam_free_ctx(struct ssh *, int, struct sshbuf *);
141 int mm_answer_gss_setup_ctx(struct ssh *, int, struct sshbuf *);
142 int mm_answer_gss_accept_ctx(struct ssh *, int, struct sshbuf *);
143 int mm_answer_gss_userok(struct ssh *, int, struct sshbuf *);
144 int mm_answer_gss_checkmic(struct ssh *, int, struct sshbuf *);
147 #ifdef SSH_AUDIT_EVENTS
148 int mm_answer_audit_event(struct ssh *, int, struct sshbuf *);
149 int mm_answer_audit_command(struct ssh *, int, struct sshbuf *);
152 static Authctxt *authctxt;
154 /* local state for key verify */
155 static u_char *key_blob = NULL;
156 static size_t key_bloblen = 0;
157 static u_int key_blobtype = MM_NOKEY;
158 static struct sshauthopt *key_opts = NULL;
159 static char *hostbased_cuser = NULL;
160 static char *hostbased_chost = NULL;
161 static char *auth_method = "unknown";
162 static char *auth_submethod = NULL;
163 static u_int session_id2_len = 0;
164 static u_char *session_id2 = NULL;
165 static pid_t monitor_child_pid;
168 enum monitor_reqtype type;
170 int (*f)(struct ssh *, int, struct sshbuf *);
173 #define MON_ISAUTH 0x0004 /* Required for Authentication */
174 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
175 #define MON_ONCE 0x0010 /* Disable after calling */
176 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
178 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
180 #define MON_PERMIT 0x1000 /* Request is permitted */
182 static int monitor_read(struct ssh *, struct monitor *, struct mon_table *,
183 struct mon_table **);
184 static int monitor_read_log(struct monitor *);
186 struct mon_table mon_dispatch_proto20[] = {
188 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
190 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
191 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
192 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
193 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
194 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
196 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
197 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
198 {MONITOR_REQ_PAM_INIT_CTX, MON_ONCE, mm_answer_pam_init_ctx},
199 {MONITOR_REQ_PAM_QUERY, 0, mm_answer_pam_query},
200 {MONITOR_REQ_PAM_RESPOND, MON_ONCE, mm_answer_pam_respond},
201 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
203 #ifdef SSH_AUDIT_EVENTS
204 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
207 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
208 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
210 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
211 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
213 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
214 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
215 {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
216 {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
221 struct mon_table mon_dispatch_postauth20[] = {
223 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
225 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
226 {MONITOR_REQ_PTY, 0, mm_answer_pty},
227 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
228 {MONITOR_REQ_TERM, 0, mm_answer_term},
229 #ifdef SSH_AUDIT_EVENTS
230 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
231 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
236 struct mon_table *mon_dispatch;
238 /* Specifies if a certain message is allowed at the moment */
240 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
242 while (ent->f != NULL) {
243 if (ent->type == type) {
244 ent->flags &= ~MON_PERMIT;
245 ent->flags |= permit ? MON_PERMIT : 0;
253 monitor_permit_authentications(int permit)
255 struct mon_table *ent = mon_dispatch;
257 while (ent->f != NULL) {
258 if (ent->flags & MON_AUTH) {
259 ent->flags &= ~MON_PERMIT;
260 ent->flags |= permit ? MON_PERMIT : 0;
267 monitor_child_preauth(struct ssh *ssh, struct monitor *pmonitor)
269 struct mon_table *ent;
270 int authenticated = 0, partial = 0;
272 debug3("preauth child monitor started");
274 if (pmonitor->m_recvfd >= 0)
275 close(pmonitor->m_recvfd);
276 if (pmonitor->m_log_sendfd >= 0)
277 close(pmonitor->m_log_sendfd);
278 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
280 authctxt = (Authctxt *)ssh->authctxt;
281 memset(authctxt, 0, sizeof(*authctxt));
282 ssh->authctxt = authctxt;
284 authctxt->loginmsg = loginmsg;
286 mon_dispatch = mon_dispatch_proto20;
287 /* Permit requests for moduli and signatures */
288 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
289 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
291 /* The first few requests do not require asynchronous access */
292 while (!authenticated) {
294 auth_method = "unknown";
295 auth_submethod = NULL;
296 auth2_authctxt_reset_info(authctxt);
298 authenticated = (monitor_read(ssh, pmonitor,
299 mon_dispatch, &ent) == 1);
301 /* Special handling for multiple required authentications */
302 if (options.num_auth_methods != 0) {
304 !auth2_update_methods_lists(authctxt,
305 auth_method, auth_submethod)) {
306 debug3_f("method %s: partial", auth_method);
313 if (!(ent->flags & MON_AUTHDECIDE))
314 fatal_f("unexpected authentication from %d",
316 if (authctxt->pw->pw_uid == 0 &&
317 !auth_root_allowed(ssh, auth_method))
320 /* PAM needs to perform account checks after auth */
321 if (options.use_pam && authenticated) {
324 if ((m = sshbuf_new()) == NULL)
325 fatal("%s: sshbuf_new failed",
327 mm_request_receive_expect(pmonitor->m_sendfd,
328 MONITOR_REQ_PAM_ACCOUNT, m);
329 authenticated = mm_answer_pam_account(
330 ssh, pmonitor->m_sendfd, m);
335 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
336 auth_log(ssh, authenticated, partial,
337 auth_method, auth_submethod);
338 if (!partial && !authenticated)
339 authctxt->failures++;
340 if (authenticated || partial) {
341 auth2_update_session_info(authctxt,
342 auth_method, auth_submethod);
347 if (!authctxt->valid)
348 fatal_f("authenticated invalid user");
349 if (strcmp(auth_method, "unknown") == 0)
350 fatal_f("authentication method name unknown");
352 debug_f("user %s authenticated by privileged process", authctxt->user);
353 ssh->authctxt = NULL;
354 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
356 mm_get_keystate(ssh, pmonitor);
358 /* Drain any buffered messages from the child */
359 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
362 if (pmonitor->m_recvfd >= 0)
363 close(pmonitor->m_recvfd);
364 if (pmonitor->m_log_sendfd >= 0)
365 close(pmonitor->m_log_sendfd);
366 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
370 monitor_set_child_handler(pid_t pid)
372 monitor_child_pid = pid;
376 monitor_child_handler(int sig)
378 kill(monitor_child_pid, sig);
382 monitor_child_postauth(struct ssh *ssh, struct monitor *pmonitor)
384 close(pmonitor->m_recvfd);
385 pmonitor->m_recvfd = -1;
387 monitor_set_child_handler(pmonitor->m_pid);
388 ssh_signal(SIGHUP, &monitor_child_handler);
389 ssh_signal(SIGTERM, &monitor_child_handler);
390 ssh_signal(SIGINT, &monitor_child_handler);
392 ssh_signal(SIGXFSZ, SIG_IGN);
395 mon_dispatch = mon_dispatch_postauth20;
397 /* Permit requests for moduli and signatures */
398 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
399 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
400 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
402 if (auth_opts->permit_pty_flag) {
403 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
404 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
408 monitor_read(ssh, pmonitor, mon_dispatch, NULL);
412 monitor_read_log(struct monitor *pmonitor)
414 struct sshbuf *logmsg;
415 u_int len, level, forced;
420 if ((logmsg = sshbuf_new()) == NULL)
421 fatal_f("sshbuf_new");
424 if ((r = sshbuf_reserve(logmsg, 4, &p)) != 0)
425 fatal_fr(r, "reserve len");
426 if (atomicio(read, pmonitor->m_log_recvfd, p, 4) != 4) {
427 if (errno == EPIPE) {
429 debug_f("child log fd closed");
430 close(pmonitor->m_log_recvfd);
431 pmonitor->m_log_recvfd = -1;
434 fatal_f("log fd read: %s", strerror(errno));
436 if ((r = sshbuf_get_u32(logmsg, &len)) != 0)
437 fatal_fr(r, "parse len");
438 if (len <= 4 || len > 8192)
439 fatal_f("invalid log message length %u", len);
441 /* Read severity, message */
442 sshbuf_reset(logmsg);
443 if ((r = sshbuf_reserve(logmsg, len, &p)) != 0)
444 fatal_fr(r, "reserve msg");
445 if (atomicio(read, pmonitor->m_log_recvfd, p, len) != len)
446 fatal_f("log fd read: %s", strerror(errno));
447 if ((r = sshbuf_get_u32(logmsg, &level)) != 0 ||
448 (r = sshbuf_get_u32(logmsg, &forced)) != 0 ||
449 (r = sshbuf_get_cstring(logmsg, &msg, NULL)) != 0)
450 fatal_fr(r, "parse");
453 if (log_level_name(level) == NULL)
454 fatal_f("invalid log level %u (corrupted message?)", level);
455 sshlogdirect(level, forced, "%s [preauth]", msg);
464 monitor_read(struct ssh *ssh, struct monitor *pmonitor, struct mon_table *ent,
465 struct mon_table **pent)
470 struct pollfd pfd[2];
473 memset(&pfd, 0, sizeof(pfd));
474 pfd[0].fd = pmonitor->m_sendfd;
475 pfd[0].events = POLLIN;
476 pfd[1].fd = pmonitor->m_log_recvfd;
477 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
478 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
479 if (errno == EINTR || errno == EAGAIN)
481 fatal_f("poll: %s", strerror(errno));
483 if (pfd[1].revents) {
485 * Drain all log messages before processing next
488 monitor_read_log(pmonitor);
492 break; /* Continues below */
495 if ((m = sshbuf_new()) == NULL)
496 fatal_f("sshbuf_new");
498 mm_request_receive(pmonitor->m_sendfd, m);
499 if ((r = sshbuf_get_u8(m, &type)) != 0)
500 fatal_fr(r, "parse type");
502 debug3_f("checking request %d", type);
504 while (ent->f != NULL) {
505 if (ent->type == type)
510 if (ent->f != NULL) {
511 if (!(ent->flags & MON_PERMIT))
512 fatal_f("unpermitted request %d", type);
513 ret = (*ent->f)(ssh, pmonitor->m_sendfd, m);
516 /* The child may use this request only once, disable it */
517 if (ent->flags & MON_ONCE) {
518 debug2_f("%d used once, disabling now", type);
519 ent->flags &= ~MON_PERMIT;
528 fatal_f("unsupported request: %d", type);
534 /* allowed key state */
536 monitor_allowed_key(const u_char *blob, u_int bloblen)
538 /* make sure key is allowed */
539 if (key_blob == NULL || key_bloblen != bloblen ||
540 timingsafe_bcmp(key_blob, blob, key_bloblen))
546 monitor_reset_key_state(void)
550 free(hostbased_cuser);
551 free(hostbased_chost);
552 sshauthopt_free(key_opts);
555 key_blobtype = MM_NOKEY;
557 hostbased_cuser = NULL;
558 hostbased_chost = NULL;
563 mm_answer_moduli(struct ssh *ssh, int sock, struct sshbuf *m)
566 const BIGNUM *dh_p, *dh_g;
568 u_int min, want, max;
570 if ((r = sshbuf_get_u32(m, &min)) != 0 ||
571 (r = sshbuf_get_u32(m, &want)) != 0 ||
572 (r = sshbuf_get_u32(m, &max)) != 0)
573 fatal_fr(r, "parse");
575 debug3_f("got parameters: %d %d %d", min, want, max);
576 /* We need to check here, too, in case the child got corrupted */
577 if (max < min || want < min || max < want)
578 fatal_f("bad parameters: %d %d %d", min, want, max);
582 dh = choose_dh(min, want, max);
584 if ((r = sshbuf_put_u8(m, 0)) != 0)
585 fatal_fr(r, "assemble empty");
588 /* Send first bignum */
589 DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
590 if ((r = sshbuf_put_u8(m, 1)) != 0 ||
591 (r = sshbuf_put_bignum2(m, dh_p)) != 0 ||
592 (r = sshbuf_put_bignum2(m, dh_g)) != 0)
593 fatal_fr(r, "assemble");
597 mm_request_send(sock, MONITOR_ANS_MODULI, m);
603 mm_answer_sign(struct ssh *ssh, int sock, struct sshbuf *m)
605 extern int auth_sock; /* XXX move to state struct? */
607 struct sshbuf *sigbuf = NULL;
608 u_char *p = NULL, *signature = NULL;
610 size_t datlen, siglen, alglen;
613 const char proof_req[] = "hostkeys-prove-00@openssh.com";
615 debug3_f("entering");
617 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
618 (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
619 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0 ||
620 (r = sshbuf_get_u32(m, &compat)) != 0)
621 fatal_fr(r, "parse");
623 fatal_f("invalid key ID");
626 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
627 * SHA384 (48 bytes) and SHA512 (64 bytes).
629 * Otherwise, verify the signature request is for a hostkey
632 * XXX perform similar check for KEX signature requests too?
633 * it's not trivial, since what is signed is the hash, rather
634 * than the full kex structure...
636 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
638 * Construct expected hostkey proof and compare it to what
639 * the client sent us.
641 if (session_id2_len == 0) /* hostkeys is never first */
642 fatal_f("bad data length: %zu", datlen);
643 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
644 fatal_f("no hostkey for index %d", keyid);
645 if ((sigbuf = sshbuf_new()) == NULL)
646 fatal_f("sshbuf_new");
647 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
648 (r = sshbuf_put_string(sigbuf, session_id2,
649 session_id2_len)) != 0 ||
650 (r = sshkey_puts(key, sigbuf)) != 0)
651 fatal_fr(r, "assemble private key proof");
652 if (datlen != sshbuf_len(sigbuf) ||
653 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
654 fatal_f("bad data length: %zu, hostkey proof len %zu",
655 datlen, sshbuf_len(sigbuf));
660 /* save session id, it will be passed on the first call */
661 if (session_id2_len == 0) {
662 session_id2_len = datlen;
663 session_id2 = xmalloc(session_id2_len);
664 memcpy(session_id2, p, session_id2_len);
667 if ((key = get_hostkey_by_index(keyid)) != NULL) {
668 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
669 options.sk_provider, NULL, compat)) != 0)
671 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
673 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
674 p, datlen, alg, compat)) != 0)
675 fatal_fr(r, "agent sign");
677 fatal_f("no hostkey from index %d", keyid);
679 debug3_f("%s %s signature len=%zu", alg,
680 is_proof ? "hostkey proof" : "KEX", siglen);
683 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
684 fatal_fr(r, "assemble");
690 mm_request_send(sock, MONITOR_ANS_SIGN, m);
692 /* Turn on permissions for getpwnam */
693 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
698 #define PUTPW(b, id) \
700 if ((r = sshbuf_put_string(b, \
701 &pwent->id, sizeof(pwent->id))) != 0) \
702 fatal_fr(r, "assemble %s", #id); \
705 /* Retrieves the password entry and also checks if the user is permitted */
707 mm_answer_pwnamallow(struct ssh *ssh, int sock, struct sshbuf *m)
709 struct passwd *pwent;
713 debug3_f("entering");
715 if (authctxt->attempt++ != 0)
716 fatal_f("multiple attempts for getpwnam");
718 if ((r = sshbuf_get_cstring(m, &authctxt->user, NULL)) != 0)
719 fatal_fr(r, "parse");
721 pwent = getpwnamallow(ssh, authctxt->user);
723 setproctitle("%s [priv]", pwent ? authctxt->user : "unknown");
728 if ((r = sshbuf_put_u8(m, 0)) != 0)
729 fatal_fr(r, "assemble fakepw");
730 authctxt->pw = fakepw();
735 authctxt->pw = pwent;
738 /* XXX send fake class/dir/shell, etc. */
739 if ((r = sshbuf_put_u8(m, 1)) != 0)
740 fatal_fr(r, "assemble ok");
743 #ifdef HAVE_STRUCT_PASSWD_PW_CHANGE
746 #ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE
749 if ((r = sshbuf_put_cstring(m, pwent->pw_name)) != 0 ||
750 (r = sshbuf_put_cstring(m, "*")) != 0 ||
751 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
752 (r = sshbuf_put_cstring(m, pwent->pw_gecos)) != 0 ||
754 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
755 (r = sshbuf_put_cstring(m, pwent->pw_class)) != 0 ||
757 (r = sshbuf_put_cstring(m, pwent->pw_dir)) != 0 ||
758 (r = sshbuf_put_cstring(m, pwent->pw_shell)) != 0)
759 fatal_fr(r, "assemble pw");
762 ssh_packet_set_log_preamble(ssh, "%suser %s",
763 authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
764 if ((r = sshbuf_put_string(m, &options, sizeof(options))) != 0)
765 fatal_fr(r, "assemble options");
767 #define M_CP_STROPT(x) do { \
768 if (options.x != NULL && \
769 (r = sshbuf_put_cstring(m, options.x)) != 0) \
770 fatal_fr(r, "assemble %s", #x); \
772 #define M_CP_STRARRAYOPT(x, nx) do { \
773 for (i = 0; i < options.nx; i++) { \
774 if ((r = sshbuf_put_cstring(m, options.x[i])) != 0) \
775 fatal_fr(r, "assemble %s", #x); \
778 /* See comment in servconf.h */
779 COPY_MATCH_STRING_OPTS();
781 #undef M_CP_STRARRAYOPT
783 /* Create valid auth method lists */
784 if (auth2_setup_methods_lists(authctxt) != 0) {
786 * The monitor will continue long enough to let the child
787 * run to its packet_disconnect(), but it must not allow any
788 * authentication to succeed.
790 debug_f("no valid authentication method lists");
793 debug3_f("sending MONITOR_ANS_PWNAM: %d", allowed);
794 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
796 /* Allow service/style information on the auth context */
797 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
798 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
802 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
808 int mm_answer_auth2_read_banner(struct ssh *ssh, int sock, struct sshbuf *m)
814 banner = auth2_read_banner();
815 if ((r = sshbuf_put_cstring(m, banner != NULL ? banner : "")) != 0)
816 fatal_fr(r, "assemble");
817 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
824 mm_answer_authserv(struct ssh *ssh, int sock, struct sshbuf *m)
828 monitor_permit_authentications(1);
830 if ((r = sshbuf_get_cstring(m, &authctxt->service, NULL)) != 0 ||
831 (r = sshbuf_get_cstring(m, &authctxt->style, NULL)) != 0)
832 fatal_fr(r, "parse");
833 debug3_f("service=%s, style=%s", authctxt->service, authctxt->style);
835 if (strlen(authctxt->style) == 0) {
836 free(authctxt->style);
837 authctxt->style = NULL;
844 * Check that the key type appears in the supplied pattern list, ignoring
845 * mismatches in the signature algorithm. (Signature algorithm checks are
846 * performed in the unprivileged authentication code).
847 * Returns 1 on success, 0 otherwise.
850 key_base_type_match(const char *method, const struct sshkey *key,
853 char *s, *l, *ol = xstrdup(list);
857 for ((s = strsep(&l, ",")); s && *s != '\0'; (s = strsep(&l, ","))) {
858 if (sshkey_type_from_name(s) == key->type) {
864 error("%s key type %s is not in permitted list %s", method,
865 sshkey_ssh_name(key), list);
873 mm_answer_authpassword(struct ssh *ssh, int sock, struct sshbuf *m)
875 static int call_count;
877 int r, authenticated;
880 if (!options.password_authentication)
881 fatal_f("password authentication not enabled");
882 if ((r = sshbuf_get_cstring(m, &passwd, &plen)) != 0)
883 fatal_fr(r, "parse");
884 /* Only authenticate if the context is valid */
885 authenticated = options.password_authentication &&
886 auth_password(ssh, passwd);
887 freezero(passwd, plen);
890 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
891 fatal_fr(r, "assemble");
893 if ((r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0)
894 fatal_fr(r, "assemble PAM");
897 debug3("%s: sending result %d", __func__, authenticated);
898 debug3_f("sending result %d", authenticated);
899 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
902 if (plen == 0 && call_count == 1)
903 auth_method = "none";
905 auth_method = "password";
907 /* Causes monitor loop to terminate if authenticated */
908 return (authenticated);
913 mm_answer_bsdauthquery(struct ssh *ssh, int sock, struct sshbuf *m)
915 char *name, *infotxt;
916 u_int numprompts, *echo_on, success;
920 if (!options.kbd_interactive_authentication)
921 fatal_f("kbd-int authentication not enabled");
922 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
923 &prompts, &echo_on) < 0 ? 0 : 1;
926 if ((r = sshbuf_put_u32(m, success)) != 0)
927 fatal_fr(r, "assemble");
929 if ((r = sshbuf_put_cstring(m, prompts[0])) != 0)
930 fatal_fr(r, "assemble prompt");
933 debug3_f("sending challenge success: %u", success);
934 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
947 mm_answer_bsdauthrespond(struct ssh *ssh, int sock, struct sshbuf *m)
952 if (!options.kbd_interactive_authentication)
953 fatal_f("kbd-int authentication not enabled");
954 if (authctxt->as == NULL)
955 fatal_f("no bsd auth session");
957 if ((r = sshbuf_get_cstring(m, &response, NULL)) != 0)
958 fatal_fr(r, "parse");
959 authok = options.kbd_interactive_authentication &&
960 auth_userresponse(authctxt->as, response, 0);
962 debug3_f("<%s> = <%d>", response, authok);
966 if ((r = sshbuf_put_u32(m, authok)) != 0)
967 fatal_fr(r, "assemble");
969 debug3_f("sending authenticated: %d", authok);
970 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
972 auth_method = "keyboard-interactive";
973 auth_submethod = "bsdauth";
975 return (authok != 0);
981 mm_answer_pam_start(struct ssh *ssh, int sock, struct sshbuf *m)
983 if (!options.use_pam)
984 fatal("UsePAM not set, but ended up in %s anyway", __func__);
988 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
989 if (options.kbd_interactive_authentication)
990 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
996 mm_answer_pam_account(struct ssh *ssh, int sock, struct sshbuf *m)
1001 if (!options.use_pam)
1002 fatal("%s: PAM not enabled", __func__);
1004 ret = do_pam_account();
1006 if ((r = sshbuf_put_u32(m, ret)) != 0 ||
1007 (r = sshbuf_put_stringb(m, loginmsg)) != 0)
1008 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1010 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
1015 static void *sshpam_ctxt, *sshpam_authok;
1016 extern KbdintDevice sshpam_device;
1019 mm_answer_pam_init_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
1024 debug3("%s", __func__);
1025 if (!options.kbd_interactive_authentication)
1026 fatal("%s: kbd-int authentication not enabled", __func__);
1027 if (sshpam_ctxt != NULL)
1028 fatal("%s: already called", __func__);
1029 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1030 sshpam_authok = NULL;
1032 if (sshpam_ctxt != NULL) {
1033 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1034 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
1037 if ((r = sshbuf_put_u32(m, ok)) != 0)
1038 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1039 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
1044 mm_answer_pam_query(struct ssh *ssh, int sock, struct sshbuf *m)
1046 char *name = NULL, *info = NULL, **prompts = NULL;
1047 u_int i, num = 0, *echo_on = 0;
1050 debug3("%s", __func__);
1051 sshpam_authok = NULL;
1052 if (sshpam_ctxt == NULL)
1053 fatal("%s: no context", __func__);
1054 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info,
1055 &num, &prompts, &echo_on);
1056 if (ret == 0 && num == 0)
1057 sshpam_authok = sshpam_ctxt;
1058 if (num > 1 || name == NULL || info == NULL)
1059 fatal("sshpam_device.query failed");
1060 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
1062 if ((r = sshbuf_put_u32(m, ret)) != 0 ||
1063 (r = sshbuf_put_cstring(m, name)) != 0 ||
1064 (r = sshbuf_put_cstring(m, info)) != 0 ||
1065 (r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0 ||
1066 (r = sshbuf_put_u32(m, num)) != 0)
1067 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1070 for (i = 0; i < num; ++i) {
1071 if ((r = sshbuf_put_cstring(m, prompts[i])) != 0 ||
1072 (r = sshbuf_put_u32(m, echo_on[i])) != 0)
1073 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1078 auth_method = "keyboard-interactive";
1079 auth_submethod = "pam";
1080 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
1085 mm_answer_pam_respond(struct ssh *ssh, int sock, struct sshbuf *m)
1091 debug3("%s", __func__);
1092 if (sshpam_ctxt == NULL)
1093 fatal("%s: no context", __func__);
1094 sshpam_authok = NULL;
1095 if ((r = sshbuf_get_u32(m, &num)) != 0)
1096 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1097 if (num > PAM_MAX_NUM_MSG) {
1098 fatal_f("Too many PAM messages, got %u, expected <= %u",
1099 num, (unsigned)PAM_MAX_NUM_MSG);
1102 resp = xcalloc(num, sizeof(char *));
1103 for (i = 0; i < num; ++i) {
1104 if ((r = sshbuf_get_cstring(m, &(resp[i]), NULL)) != 0)
1105 fatal("%s: buffer error: %s",
1106 __func__, ssh_err(r));
1108 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1109 for (i = 0; i < num; ++i)
1113 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1116 if ((r = sshbuf_put_u32(m, ret)) != 0)
1117 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1118 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
1119 auth_method = "keyboard-interactive";
1120 auth_submethod = "pam";
1122 sshpam_authok = sshpam_ctxt;
1127 mm_answer_pam_free_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
1129 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
1131 debug3("%s", __func__);
1132 if (sshpam_ctxt == NULL)
1133 fatal("%s: no context", __func__);
1134 (sshpam_device.free_ctx)(sshpam_ctxt);
1135 sshpam_ctxt = sshpam_authok = NULL;
1137 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
1138 /* Allow another attempt */
1139 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
1140 auth_method = "keyboard-interactive";
1141 auth_submethod = "pam";
1147 mm_answer_keyallowed(struct ssh *ssh, int sock, struct sshbuf *m)
1149 struct sshkey *key = NULL;
1150 char *cuser, *chost;
1151 u_int pubkey_auth_attempt;
1154 struct sshauthopt *opts = NULL;
1156 debug3_f("entering");
1157 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1158 (r = sshbuf_get_cstring(m, &cuser, NULL)) != 0 ||
1159 (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
1160 (r = sshkey_froms(m, &key)) != 0 ||
1161 (r = sshbuf_get_u32(m, &pubkey_auth_attempt)) != 0)
1162 fatal_fr(r, "parse");
1164 if (key != NULL && authctxt->valid) {
1167 auth_method = "publickey";
1168 if (!options.pubkey_authentication)
1170 if (auth2_key_already_used(authctxt, key))
1172 if (!key_base_type_match(auth_method, key,
1173 options.pubkey_accepted_algos))
1175 allowed = user_key_allowed(ssh, authctxt->pw, key,
1176 pubkey_auth_attempt, &opts);
1179 auth_method = "hostbased";
1180 if (!options.hostbased_authentication)
1182 if (auth2_key_already_used(authctxt, key))
1184 if (!key_base_type_match(auth_method, key,
1185 options.hostbased_accepted_algos))
1187 allowed = hostbased_key_allowed(ssh, authctxt->pw,
1189 auth2_record_info(authctxt,
1190 "client user \"%.100s\", client host \"%.100s\"",
1194 fatal_f("unknown key type %u", type);
1199 debug3_f("%s authentication%s: %s key is %s", auth_method,
1200 pubkey_auth_attempt ? "" : " test",
1201 (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key),
1202 allowed ? "allowed" : "not allowed");
1204 auth2_record_key(authctxt, 0, key);
1206 /* clear temporarily storage (used by verify) */
1207 monitor_reset_key_state();
1210 /* Save temporarily for comparison in verify */
1211 if ((r = sshkey_to_blob(key, &key_blob, &key_bloblen)) != 0)
1212 fatal_fr(r, "sshkey_to_blob");
1213 key_blobtype = type;
1215 hostbased_cuser = cuser;
1216 hostbased_chost = chost;
1218 /* Log failed attempt */
1219 auth_log(ssh, 0, 0, auth_method, NULL);
1226 if ((r = sshbuf_put_u32(m, allowed)) != 0)
1227 fatal_fr(r, "assemble");
1228 if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0)
1229 fatal_fr(r, "sshauthopt_serialise");
1230 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1233 sshauthopt_free(opts);
1239 monitor_valid_userblob(struct ssh *ssh, const u_char *data, u_int datalen)
1242 struct sshkey *hostkey = NULL;
1244 char *userstyle, *cp;
1247 int hostbound = 0, r, fail = 0;
1249 if ((b = sshbuf_from(data, datalen)) == NULL)
1250 fatal_f("sshbuf_from");
1252 if (ssh->compat & SSH_OLD_SESSIONID) {
1254 len = sshbuf_len(b);
1255 if ((session_id2 == NULL) ||
1256 (len < session_id2_len) ||
1257 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1259 if ((r = sshbuf_consume(b, session_id2_len)) != 0)
1260 fatal_fr(r, "consume");
1262 if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1263 fatal_fr(r, "parse sessionid");
1264 if ((session_id2 == NULL) ||
1265 (len != session_id2_len) ||
1266 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1269 if ((r = sshbuf_get_u8(b, &type)) != 0)
1270 fatal_fr(r, "parse type");
1271 if (type != SSH2_MSG_USERAUTH_REQUEST)
1273 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1274 fatal_fr(r, "parse userstyle");
1275 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1276 authctxt->style ? ":" : "",
1277 authctxt->style ? authctxt->style : "");
1278 if (strcmp(userstyle, cp) != 0) {
1279 logit("wrong user name passed to monitor: "
1280 "expected %s != %.100s", userstyle, cp);
1285 if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1286 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1287 fatal_fr(r, "parse method");
1288 if (strcmp("publickey", cp) != 0) {
1289 if (strcmp("publickey-hostbound-v00@openssh.com", cp) == 0)
1295 if ((r = sshbuf_get_u8(b, &type)) != 0)
1296 fatal_fr(r, "parse pktype");
1299 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1300 (r = sshbuf_skip_string(b)) != 0 || /* pkblob */
1301 (hostbound && (r = sshkey_froms(b, &hostkey)) != 0))
1302 fatal_fr(r, "parse pk");
1303 if (sshbuf_len(b) != 0)
1306 if (hostkey != NULL) {
1308 * Ensure this is actually one of our hostkeys; unfortunately
1309 * can't check ssh->kex->initial_hostkey directly at this point
1310 * as packet state has not yet been exported to monitor.
1312 if (get_hostkey_index(hostkey, 1, ssh) == -1)
1313 fatal_f("hostbound hostkey does not match");
1314 sshkey_free(hostkey);
1320 monitor_valid_hostbasedblob(const u_char *data, u_int datalen,
1321 const char *cuser, const char *chost)
1325 char *cp, *userstyle;
1330 if ((b = sshbuf_from(data, datalen)) == NULL)
1331 fatal_f("sshbuf_new");
1332 if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1333 fatal_fr(r, "parse sessionid");
1335 if ((session_id2 == NULL) ||
1336 (len != session_id2_len) ||
1337 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1340 if ((r = sshbuf_get_u8(b, &type)) != 0)
1341 fatal_fr(r, "parse type");
1342 if (type != SSH2_MSG_USERAUTH_REQUEST)
1344 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1345 fatal_fr(r, "parse userstyle");
1346 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1347 authctxt->style ? ":" : "",
1348 authctxt->style ? authctxt->style : "");
1349 if (strcmp(userstyle, cp) != 0) {
1350 logit("wrong user name passed to monitor: "
1351 "expected %s != %.100s", userstyle, cp);
1356 if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1357 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1358 fatal_fr(r, "parse method");
1359 if (strcmp(cp, "hostbased") != 0)
1362 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1363 (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1364 fatal_fr(r, "parse pk");
1366 /* verify client host, strip trailing dot if necessary */
1367 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1368 fatal_fr(r, "parse host");
1369 if (((len = strlen(cp)) > 0) && cp[len - 1] == '.')
1371 if (strcmp(cp, chost) != 0)
1375 /* verify client user */
1376 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1377 fatal_fr(r, "parse ruser");
1378 if (strcmp(cp, cuser) != 0)
1382 if (sshbuf_len(b) != 0)
1389 mm_answer_keyverify(struct ssh *ssh, int sock, struct sshbuf *m)
1392 const u_char *signature, *data, *blob;
1393 char *sigalg = NULL, *fp = NULL;
1394 size_t signaturelen, datalen, bloblen;
1395 int r, ret, req_presence = 0, req_verify = 0, valid_data = 0;
1397 struct sshkey_sig_details *sig_details = NULL;
1399 if ((r = sshbuf_get_string_direct(m, &blob, &bloblen)) != 0 ||
1400 (r = sshbuf_get_string_direct(m, &signature, &signaturelen)) != 0 ||
1401 (r = sshbuf_get_string_direct(m, &data, &datalen)) != 0 ||
1402 (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
1403 fatal_fr(r, "parse");
1405 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1406 !monitor_allowed_key(blob, bloblen))
1407 fatal_f("bad key, not previously allowed");
1409 /* Empty signature algorithm means NULL. */
1410 if (*sigalg == '\0') {
1415 /* XXX use sshkey_froms here; need to change key_blob, etc. */
1416 if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1417 fatal_fr(r, "parse key");
1419 switch (key_blobtype) {
1421 valid_data = monitor_valid_userblob(ssh, data, datalen);
1422 auth_method = "publickey";
1425 valid_data = monitor_valid_hostbasedblob(data, datalen,
1426 hostbased_cuser, hostbased_chost);
1427 auth_method = "hostbased";
1434 fatal_f("bad %s signature data blob",
1435 key_blobtype == MM_USERKEY ? "userkey" :
1436 (key_blobtype == MM_HOSTKEY ? "hostkey" : "unknown"));
1438 if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
1439 SSH_FP_DEFAULT)) == NULL)
1440 fatal_f("sshkey_fingerprint failed");
1442 ret = sshkey_verify(key, signature, signaturelen, data, datalen,
1443 sigalg, ssh->compat, &sig_details);
1444 debug3_f("%s %s signature using %s %s%s%s", auth_method,
1445 sshkey_type(key), sigalg == NULL ? "default" : sigalg,
1446 (ret == 0) ? "verified" : "unverified",
1447 (ret != 0) ? ": " : "", (ret != 0) ? ssh_err(ret) : "");
1449 if (ret == 0 && key_blobtype == MM_USERKEY && sig_details != NULL) {
1450 req_presence = (options.pubkey_auth_options &
1451 PUBKEYAUTH_TOUCH_REQUIRED) ||
1452 !key_opts->no_require_user_presence;
1454 (sig_details->sk_flags & SSH_SK_USER_PRESENCE_REQD) == 0) {
1455 error("public key %s %s signature for %s%s from %.128s "
1456 "port %d rejected: user presence "
1457 "(authenticator touch) requirement not met ",
1458 sshkey_type(key), fp,
1459 authctxt->valid ? "" : "invalid user ",
1460 authctxt->user, ssh_remote_ipaddr(ssh),
1461 ssh_remote_port(ssh));
1462 ret = SSH_ERR_SIGNATURE_INVALID;
1464 req_verify = (options.pubkey_auth_options &
1465 PUBKEYAUTH_VERIFY_REQUIRED) || key_opts->require_verify;
1467 (sig_details->sk_flags & SSH_SK_USER_VERIFICATION_REQD) == 0) {
1468 error("public key %s %s signature for %s%s from %.128s "
1469 "port %d rejected: user verification requirement "
1470 "not met ", sshkey_type(key), fp,
1471 authctxt->valid ? "" : "invalid user ",
1472 authctxt->user, ssh_remote_ipaddr(ssh),
1473 ssh_remote_port(ssh));
1474 ret = SSH_ERR_SIGNATURE_INVALID;
1477 auth2_record_key(authctxt, ret == 0, key);
1479 if (key_blobtype == MM_USERKEY)
1480 auth_activate_options(ssh, key_opts);
1481 monitor_reset_key_state();
1485 /* encode ret != 0 as positive integer, since we're sending u32 */
1486 encoded_ret = (ret != 0);
1487 if ((r = sshbuf_put_u32(m, encoded_ret)) != 0 ||
1488 (r = sshbuf_put_u8(m, sig_details != NULL)) != 0)
1489 fatal_fr(r, "assemble");
1490 if (sig_details != NULL) {
1491 if ((r = sshbuf_put_u32(m, sig_details->sk_counter)) != 0 ||
1492 (r = sshbuf_put_u8(m, sig_details->sk_flags)) != 0)
1493 fatal_fr(r, "assemble sk");
1495 sshkey_sig_details_free(sig_details);
1496 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1506 mm_record_login(struct ssh *ssh, Session *s, struct passwd *pw)
1509 struct sockaddr_storage from;
1512 * Get IP address of client. If the connection is not a socket, let
1513 * the address be 0.0.0.0.
1515 memset(&from, 0, sizeof(from));
1516 fromlen = sizeof(from);
1517 if (ssh_packet_connection_is_on_socket(ssh)) {
1518 if (getpeername(ssh_packet_get_connection_in(ssh),
1519 (struct sockaddr *)&from, &fromlen) == -1) {
1520 debug("getpeername: %.100s", strerror(errno));
1524 /* Record that there was a login on that tty from the remote host. */
1525 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1526 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
1527 (struct sockaddr *)&from, fromlen);
1531 mm_session_close(Session *s)
1533 debug3_f("session %d pid %ld", s->self, (long)s->pid);
1534 if (s->ttyfd != -1) {
1535 debug3_f("tty %s ptyfd %d", s->tty, s->ptyfd);
1536 session_pty_cleanup2(s);
1538 session_unused(s->self);
1542 mm_answer_pty(struct ssh *ssh, int sock, struct sshbuf *m)
1544 extern struct monitor *pmonitor;
1548 debug3_f("entering");
1554 s->authctxt = authctxt;
1555 s->pw = authctxt->pw;
1556 s->pid = pmonitor->m_pid;
1557 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1560 pty_setowner(authctxt->pw, s->tty);
1562 if ((r = sshbuf_put_u32(m, 1)) != 0 ||
1563 (r = sshbuf_put_cstring(m, s->tty)) != 0)
1564 fatal_fr(r, "assemble");
1566 /* We need to trick ttyslot */
1567 if (dup2(s->ttyfd, 0) == -1)
1570 mm_record_login(ssh, s, authctxt->pw);
1572 /* Now we can close the file descriptor again */
1575 /* send messages generated by record_login */
1576 if ((r = sshbuf_put_stringb(m, loginmsg)) != 0)
1577 fatal_fr(r, "assemble loginmsg");
1578 sshbuf_reset(loginmsg);
1580 mm_request_send(sock, MONITOR_ANS_PTY, m);
1582 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1583 mm_send_fd(sock, s->ttyfd) == -1)
1584 fatal_f("send fds failed");
1586 /* make sure nothing uses fd 0 */
1587 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1588 fatal_f("open(/dev/null): %s", strerror(errno));
1590 error_f("fd0 %d != 0", fd0);
1592 /* slave side of pty is not needed */
1594 s->ttyfd = s->ptyfd;
1595 /* no need to dup() because nobody closes ptyfd */
1596 s->ptymaster = s->ptyfd;
1598 debug3_f("tty %s ptyfd %d", s->tty, s->ttyfd);
1604 mm_session_close(s);
1605 if ((r = sshbuf_put_u32(m, 0)) != 0)
1606 fatal_fr(r, "assemble 0");
1607 mm_request_send(sock, MONITOR_ANS_PTY, m);
1612 mm_answer_pty_cleanup(struct ssh *ssh, int sock, struct sshbuf *m)
1618 debug3_f("entering");
1620 if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
1621 fatal_fr(r, "parse tty");
1622 if ((s = session_by_tty(tty)) != NULL)
1623 mm_session_close(s);
1630 mm_answer_term(struct ssh *ssh, int sock, struct sshbuf *req)
1632 extern struct monitor *pmonitor;
1635 debug3_f("tearing down sessions");
1637 /* The child is terminating */
1638 session_destroy_all(ssh, &mm_session_close);
1641 if (options.use_pam)
1645 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1649 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1651 /* Terminate process */
1655 #ifdef SSH_AUDIT_EVENTS
1656 /* Report that an audit event occurred */
1658 mm_answer_audit_event(struct ssh *ssh, int socket, struct sshbuf *m)
1661 ssh_audit_event_t event;
1664 debug3("%s entering", __func__);
1666 if ((r = sshbuf_get_u32(m, &n)) != 0)
1667 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1668 event = (ssh_audit_event_t)n;
1670 case SSH_AUTH_FAIL_PUBKEY:
1671 case SSH_AUTH_FAIL_HOSTBASED:
1672 case SSH_AUTH_FAIL_GSSAPI:
1673 case SSH_LOGIN_EXCEED_MAXTRIES:
1674 case SSH_LOGIN_ROOT_DENIED:
1675 case SSH_CONNECTION_CLOSE:
1676 case SSH_INVALID_USER:
1677 audit_event(ssh, event);
1680 fatal("Audit event type %d not permitted", event);
1687 mm_answer_audit_command(struct ssh *ssh, int socket, struct sshbuf *m)
1692 debug3("%s entering", __func__);
1693 if ((r = sshbuf_get_cstring(m, &cmd, NULL)) != 0)
1694 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1695 /* sanity check command, if so how? */
1696 audit_run_command(cmd);
1700 #endif /* SSH_AUDIT_EVENTS */
1703 monitor_clear_keystate(struct ssh *ssh, struct monitor *pmonitor)
1705 ssh_clear_newkeys(ssh, MODE_IN);
1706 ssh_clear_newkeys(ssh, MODE_OUT);
1707 sshbuf_free(child_state);
1712 monitor_apply_keystate(struct ssh *ssh, struct monitor *pmonitor)
1717 debug3_f("packet_set_state");
1718 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1719 fatal_fr(r, "packet_set_state");
1720 sshbuf_free(child_state);
1722 if ((kex = ssh->kex) == NULL)
1723 fatal_f("internal error: ssh->kex == NULL");
1724 if (session_id2_len != sshbuf_len(ssh->kex->session_id)) {
1725 fatal_f("incorrect session id length %zu (expected %u)",
1726 sshbuf_len(ssh->kex->session_id), session_id2_len);
1728 if (memcmp(sshbuf_ptr(ssh->kex->session_id), session_id2,
1729 session_id2_len) != 0)
1730 fatal_f("session ID mismatch");
1731 /* XXX set callbacks */
1733 kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_server;
1734 kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_server;
1735 kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_server;
1736 kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_server;
1737 kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_server;
1738 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1739 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1740 # ifdef OPENSSL_HAS_ECC
1741 kex->kex[KEX_ECDH_SHA2] = kex_gen_server;
1743 #endif /* WITH_OPENSSL */
1744 kex->kex[KEX_C25519_SHA256] = kex_gen_server;
1745 kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_server;
1746 kex->load_host_public_key=&get_hostkey_public_by_type;
1747 kex->load_host_private_key=&get_hostkey_private_by_type;
1748 kex->host_key_index=&get_hostkey_index;
1749 kex->sign = sshd_hostkey_sign;
1752 /* This function requires careful sanity checking */
1755 mm_get_keystate(struct ssh *ssh, struct monitor *pmonitor)
1757 debug3_f("Waiting for new keys");
1759 if ((child_state = sshbuf_new()) == NULL)
1760 fatal_f("sshbuf_new failed");
1761 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1763 debug3_f("GOT new keys");
1769 #define FD_CLOSEONEXEC(x) do { \
1770 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
1771 fatal("fcntl(%d, F_SETFD)", x); \
1775 monitor_openfds(struct monitor *mon, int do_logfds)
1782 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1783 fatal_f("socketpair: %s", strerror(errno));
1785 if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) == -1)
1786 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
1787 if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) == -1)
1788 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
1790 FD_CLOSEONEXEC(pair[0]);
1791 FD_CLOSEONEXEC(pair[1]);
1792 mon->m_recvfd = pair[0];
1793 mon->m_sendfd = pair[1];
1796 if (pipe(pair) == -1)
1797 fatal_f("pipe: %s", strerror(errno));
1798 FD_CLOSEONEXEC(pair[0]);
1799 FD_CLOSEONEXEC(pair[1]);
1800 mon->m_log_recvfd = pair[0];
1801 mon->m_log_sendfd = pair[1];
1803 mon->m_log_recvfd = mon->m_log_sendfd = -1;
1806 #define MM_MEMSIZE 65536
1811 struct monitor *mon;
1813 mon = xcalloc(1, sizeof(*mon));
1814 monitor_openfds(mon, 1);
1820 monitor_reinit(struct monitor *mon)
1822 monitor_openfds(mon, 0);
1827 mm_answer_gss_setup_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
1835 if (!options.gss_authentication)
1836 fatal_f("GSSAPI authentication not enabled");
1838 if ((r = sshbuf_get_string(m, &p, &len)) != 0)
1839 fatal_fr(r, "parse");
1843 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1845 free(goid.elements);
1848 if ((r = sshbuf_put_u32(m, major)) != 0)
1849 fatal_fr(r, "assemble");
1851 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1853 /* Now we have a context, enable the step */
1854 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1860 mm_answer_gss_accept_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
1863 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1864 OM_uint32 major, minor;
1865 OM_uint32 flags = 0; /* GSI needs this */
1868 if (!options.gss_authentication)
1869 fatal_f("GSSAPI authentication not enabled");
1871 if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0)
1872 fatal_fr(r, "ssh_gssapi_get_buffer_desc");
1873 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1877 if ((r = sshbuf_put_u32(m, major)) != 0 ||
1878 (r = sshbuf_put_string(m, out.value, out.length)) != 0 ||
1879 (r = sshbuf_put_u32(m, flags)) != 0)
1880 fatal_fr(r, "assemble");
1881 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1883 gss_release_buffer(&minor, &out);
1885 if (major == GSS_S_COMPLETE) {
1886 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1887 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1888 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1894 mm_answer_gss_checkmic(struct ssh *ssh, int sock, struct sshbuf *m)
1896 gss_buffer_desc gssbuf, mic;
1900 if (!options.gss_authentication)
1901 fatal_f("GSSAPI authentication not enabled");
1903 if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 ||
1904 (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0)
1905 fatal_fr(r, "ssh_gssapi_get_buffer_desc");
1907 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1913 if ((r = sshbuf_put_u32(m, ret)) != 0)
1914 fatal_fr(r, "assemble");
1916 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1918 if (!GSS_ERROR(ret))
1919 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1925 mm_answer_gss_userok(struct ssh *ssh, int sock, struct sshbuf *m)
1927 int r, authenticated;
1928 const char *displayname;
1930 if (!options.gss_authentication)
1931 fatal_f("GSSAPI authentication not enabled");
1933 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1936 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
1937 fatal_fr(r, "assemble");
1939 debug3_f("sending result %d", authenticated);
1940 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1942 auth_method = "gssapi-with-mic";
1944 if ((displayname = ssh_gssapi_displayname()) != NULL)
1945 auth2_record_info(authctxt, "%s", displayname);
1947 /* Monitor loop will terminate if authenticated */
1948 return (authenticated);