1 /* $OpenBSD: monitor.c,v 1.186 2018/07/20 03:46:34 djm Exp $ */
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include "openbsd-compat/sys-tree.h"
54 # ifdef HAVE_SYS_POLL_H
55 # include <sys/poll.h>
60 #include <openssl/dh.h>
63 #include "openbsd-compat/sys-queue.h"
76 #include "auth-options.h"
89 #include "monitor_wrap.h"
90 #include "monitor_fdpass.h"
98 static Gssctxt *gsscontext = NULL;
102 extern ServerOptions options;
103 extern u_int utmp_len;
104 extern u_char session_id[];
105 extern struct sshbuf *loginmsg;
106 extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */
108 /* State exported from the child */
109 static struct sshbuf *child_state;
111 /* Functions on the monitor that answer unprivileged requests */
113 int mm_answer_moduli(int, struct sshbuf *);
114 int mm_answer_sign(int, struct sshbuf *);
115 int mm_answer_pwnamallow(int, struct sshbuf *);
116 int mm_answer_auth2_read_banner(int, struct sshbuf *);
117 int mm_answer_authserv(int, struct sshbuf *);
118 int mm_answer_authpassword(int, struct sshbuf *);
119 int mm_answer_bsdauthquery(int, struct sshbuf *);
120 int mm_answer_bsdauthrespond(int, struct sshbuf *);
121 int mm_answer_keyallowed(int, struct sshbuf *);
122 int mm_answer_keyverify(int, struct sshbuf *);
123 int mm_answer_pty(int, struct sshbuf *);
124 int mm_answer_pty_cleanup(int, struct sshbuf *);
125 int mm_answer_term(int, struct sshbuf *);
126 int mm_answer_rsa_keyallowed(int, struct sshbuf *);
127 int mm_answer_rsa_challenge(int, struct sshbuf *);
128 int mm_answer_rsa_response(int, struct sshbuf *);
129 int mm_answer_sesskey(int, struct sshbuf *);
130 int mm_answer_sessid(int, struct sshbuf *);
133 int mm_answer_pam_start(int, struct sshbuf *);
134 int mm_answer_pam_account(int, struct sshbuf *);
135 int mm_answer_pam_init_ctx(int, struct sshbuf *);
136 int mm_answer_pam_query(int, struct sshbuf *);
137 int mm_answer_pam_respond(int, struct sshbuf *);
138 int mm_answer_pam_free_ctx(int, struct sshbuf *);
142 int mm_answer_gss_setup_ctx(int, struct sshbuf *);
143 int mm_answer_gss_accept_ctx(int, struct sshbuf *);
144 int mm_answer_gss_userok(int, struct sshbuf *);
145 int mm_answer_gss_checkmic(int, struct sshbuf *);
148 #ifdef SSH_AUDIT_EVENTS
149 int mm_answer_audit_event(int, struct sshbuf *);
150 int mm_answer_audit_command(int, struct sshbuf *);
153 static int monitor_read_log(struct monitor *);
155 static Authctxt *authctxt;
157 /* local state for key verify */
158 static u_char *key_blob = NULL;
159 static size_t key_bloblen = 0;
160 static int key_blobtype = MM_NOKEY;
161 static struct sshauthopt *key_opts = NULL;
162 static char *hostbased_cuser = NULL;
163 static char *hostbased_chost = NULL;
164 static char *auth_method = "unknown";
165 static char *auth_submethod = NULL;
166 static u_int session_id2_len = 0;
167 static u_char *session_id2 = NULL;
168 static pid_t monitor_child_pid;
171 enum monitor_reqtype type;
173 int (*f)(int, struct sshbuf *);
176 #define MON_ISAUTH 0x0004 /* Required for Authentication */
177 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
178 #define MON_ONCE 0x0010 /* Disable after calling */
179 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
181 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
183 #define MON_PERMIT 0x1000 /* Request is permitted */
185 struct mon_table mon_dispatch_proto20[] = {
187 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
189 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
190 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
191 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
192 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
193 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
195 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
196 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
197 {MONITOR_REQ_PAM_INIT_CTX, MON_ONCE, mm_answer_pam_init_ctx},
198 {MONITOR_REQ_PAM_QUERY, 0, mm_answer_pam_query},
199 {MONITOR_REQ_PAM_RESPOND, MON_ONCE, mm_answer_pam_respond},
200 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
202 #ifdef SSH_AUDIT_EVENTS
203 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
206 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
207 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
209 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
210 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
212 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
213 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
214 {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
215 {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
220 struct mon_table mon_dispatch_postauth20[] = {
222 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
224 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
225 {MONITOR_REQ_PTY, 0, mm_answer_pty},
226 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
227 {MONITOR_REQ_TERM, 0, mm_answer_term},
228 #ifdef SSH_AUDIT_EVENTS
229 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
230 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
235 struct mon_table *mon_dispatch;
237 /* Specifies if a certain message is allowed at the moment */
239 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
241 while (ent->f != NULL) {
242 if (ent->type == type) {
243 ent->flags &= ~MON_PERMIT;
244 ent->flags |= permit ? MON_PERMIT : 0;
252 monitor_permit_authentications(int permit)
254 struct mon_table *ent = mon_dispatch;
256 while (ent->f != NULL) {
257 if (ent->flags & MON_AUTH) {
258 ent->flags &= ~MON_PERMIT;
259 ent->flags |= permit ? MON_PERMIT : 0;
266 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
268 struct ssh *ssh = active_state; /* XXX */
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;
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(pmonitor, mon_dispatch, &ent) == 1);
300 /* Special handling for multiple required authentications */
301 if (options.num_auth_methods != 0) {
303 !auth2_update_methods_lists(authctxt,
304 auth_method, auth_submethod)) {
305 debug3("%s: method %s: partial", __func__,
313 if (!(ent->flags & MON_AUTHDECIDE))
314 fatal("%s: unexpected authentication from %d",
315 __func__, ent->type);
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 pmonitor->m_sendfd, m);
335 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
336 auth_log(authctxt, 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("%s: authenticated invalid user", __func__);
349 if (strcmp(auth_method, "unknown") == 0)
350 fatal("%s: authentication method name unknown", __func__);
352 debug("%s: %s has been authenticated by privileged process",
353 __func__, authctxt->user);
354 ssh->authctxt = NULL;
355 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
357 mm_get_keystate(pmonitor);
359 /* Drain any buffered messages from the child */
360 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
363 if (pmonitor->m_recvfd >= 0)
364 close(pmonitor->m_recvfd);
365 if (pmonitor->m_log_sendfd >= 0)
366 close(pmonitor->m_log_sendfd);
367 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
371 monitor_set_child_handler(pid_t pid)
373 monitor_child_pid = pid;
377 monitor_child_handler(int sig)
379 kill(monitor_child_pid, sig);
383 monitor_child_postauth(struct monitor *pmonitor)
385 close(pmonitor->m_recvfd);
386 pmonitor->m_recvfd = -1;
388 monitor_set_child_handler(pmonitor->m_pid);
389 signal(SIGHUP, &monitor_child_handler);
390 signal(SIGTERM, &monitor_child_handler);
391 signal(SIGINT, &monitor_child_handler);
393 signal(SIGXFSZ, SIG_IGN);
396 mon_dispatch = mon_dispatch_postauth20;
398 /* Permit requests for moduli and signatures */
399 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
400 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
401 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
403 if (auth_opts->permit_pty_flag) {
404 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
405 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
409 monitor_read(pmonitor, mon_dispatch, NULL);
413 monitor_read_log(struct monitor *pmonitor)
415 struct sshbuf *logmsg;
421 if ((logmsg = sshbuf_new()) == NULL)
422 fatal("%s: sshbuf_new", __func__);
425 if ((r = sshbuf_reserve(logmsg, 4, &p)) != 0)
426 fatal("%s: reserve: %s", __func__, ssh_err(r));
427 if (atomicio(read, pmonitor->m_log_recvfd, p, 4) != 4) {
428 if (errno == EPIPE) {
430 debug("%s: child log fd closed", __func__);
431 close(pmonitor->m_log_recvfd);
432 pmonitor->m_log_recvfd = -1;
435 fatal("%s: log fd read: %s", __func__, strerror(errno));
437 if ((r = sshbuf_get_u32(logmsg, &len)) != 0)
438 fatal("%s: get len: %s", __func__, ssh_err(r));
439 if (len <= 4 || len > 8192)
440 fatal("%s: invalid log message length %u", __func__, len);
442 /* Read severity, message */
443 sshbuf_reset(logmsg);
444 if ((r = sshbuf_reserve(logmsg, len, &p)) != 0)
445 fatal("%s: reserve: %s", __func__, ssh_err(r));
446 if (atomicio(read, pmonitor->m_log_recvfd, p, len) != len)
447 fatal("%s: log fd read: %s", __func__, strerror(errno));
448 if ((r = sshbuf_get_u32(logmsg, &level)) != 0 ||
449 (r = sshbuf_get_cstring(logmsg, &msg, NULL)) != 0)
450 fatal("%s: decode: %s", __func__, ssh_err(r));
453 if (log_level_name(level) == NULL)
454 fatal("%s: invalid log level %u (corrupted message?)",
456 do_log2(level, "%s [preauth]", msg);
465 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
466 struct mon_table **pent)
471 struct pollfd pfd[2];
474 memset(&pfd, 0, sizeof(pfd));
475 pfd[0].fd = pmonitor->m_sendfd;
476 pfd[0].events = POLLIN;
477 pfd[1].fd = pmonitor->m_log_recvfd;
478 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
479 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
480 if (errno == EINTR || errno == EAGAIN)
482 fatal("%s: poll: %s", __func__, strerror(errno));
484 if (pfd[1].revents) {
486 * Drain all log messages before processing next
489 monitor_read_log(pmonitor);
493 break; /* Continues below */
496 if ((m = sshbuf_new()) == NULL)
497 fatal("%s: sshbuf_new", __func__);
499 mm_request_receive(pmonitor->m_sendfd, m);
500 if ((r = sshbuf_get_u8(m, &type)) != 0)
501 fatal("%s: decode: %s", __func__, ssh_err(r));
503 debug3("%s: checking request %d", __func__, type);
505 while (ent->f != NULL) {
506 if (ent->type == type)
511 if (ent->f != NULL) {
512 if (!(ent->flags & MON_PERMIT))
513 fatal("%s: unpermitted request %d", __func__,
515 ret = (*ent->f)(pmonitor->m_sendfd, m);
518 /* The child may use this request only once, disable it */
519 if (ent->flags & MON_ONCE) {
520 debug2("%s: %d used once, disabling now", __func__,
522 ent->flags &= ~MON_PERMIT;
531 fatal("%s: unsupported request: %d", __func__, type);
537 /* allowed key state */
539 monitor_allowed_key(u_char *blob, u_int bloblen)
541 /* make sure key is allowed */
542 if (key_blob == NULL || key_bloblen != bloblen ||
543 timingsafe_bcmp(key_blob, blob, key_bloblen))
549 monitor_reset_key_state(void)
553 free(hostbased_cuser);
554 free(hostbased_chost);
555 sshauthopt_free(key_opts);
558 key_blobtype = MM_NOKEY;
560 hostbased_cuser = NULL;
561 hostbased_chost = NULL;
566 mm_answer_moduli(int sock, struct sshbuf *m)
570 u_int min, want, max;
572 if ((r = sshbuf_get_u32(m, &min)) != 0 ||
573 (r = sshbuf_get_u32(m, &want)) != 0 ||
574 (r = sshbuf_get_u32(m, &max)) != 0)
575 fatal("%s: buffer error: %s", __func__, ssh_err(r));
577 debug3("%s: got parameters: %d %d %d",
578 __func__, min, want, max);
579 /* We need to check here, too, in case the child got corrupted */
580 if (max < min || want < min || max < want)
581 fatal("%s: bad parameters: %d %d %d",
582 __func__, min, want, max);
586 dh = choose_dh(min, want, max);
588 if ((r = sshbuf_put_u8(m, 0)) != 0)
589 fatal("%s: buffer error: %s", __func__, ssh_err(r));
592 /* Send first bignum */
593 if ((r = sshbuf_put_u8(m, 1)) != 0 ||
594 (r = sshbuf_put_bignum2(m, dh->p)) != 0 ||
595 (r = sshbuf_put_bignum2(m, dh->g)) != 0)
596 fatal("%s: buffer error: %s", __func__, ssh_err(r));
600 mm_request_send(sock, MONITOR_ANS_MODULI, m);
606 mm_answer_sign(int sock, struct sshbuf *m)
608 struct ssh *ssh = active_state; /* XXX */
609 extern int auth_sock; /* XXX move to state struct? */
611 struct sshbuf *sigbuf = NULL;
612 u_char *p = NULL, *signature = NULL;
614 size_t datlen, siglen, alglen;
617 const char proof_req[] = "hostkeys-prove-00@openssh.com";
619 debug3("%s", __func__);
621 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
622 (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
623 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0 ||
624 (r = sshbuf_get_u32(m, &compat)) != 0)
625 fatal("%s: buffer error: %s", __func__, ssh_err(r));
627 fatal("%s: invalid key ID", __func__);
630 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
631 * SHA384 (48 bytes) and SHA512 (64 bytes).
633 * Otherwise, verify the signature request is for a hostkey
636 * XXX perform similar check for KEX signature requests too?
637 * it's not trivial, since what is signed is the hash, rather
638 * than the full kex structure...
640 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
642 * Construct expected hostkey proof and compare it to what
643 * the client sent us.
645 if (session_id2_len == 0) /* hostkeys is never first */
646 fatal("%s: bad data length: %zu", __func__, datlen);
647 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
648 fatal("%s: no hostkey for index %d", __func__, keyid);
649 if ((sigbuf = sshbuf_new()) == NULL)
650 fatal("%s: sshbuf_new", __func__);
651 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
652 (r = sshbuf_put_string(sigbuf, session_id2,
653 session_id2_len)) != 0 ||
654 (r = sshkey_puts(key, sigbuf)) != 0)
655 fatal("%s: couldn't prepare private key "
656 "proof buffer: %s", __func__, ssh_err(r));
657 if (datlen != sshbuf_len(sigbuf) ||
658 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
659 fatal("%s: bad data length: %zu, hostkey proof len %zu",
660 __func__, datlen, sshbuf_len(sigbuf));
665 /* save session id, it will be passed on the first call */
666 if (session_id2_len == 0) {
667 session_id2_len = datlen;
668 session_id2 = xmalloc(session_id2_len);
669 memcpy(session_id2, p, session_id2_len);
672 if ((key = get_hostkey_by_index(keyid)) != NULL) {
673 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
675 fatal("%s: sshkey_sign failed: %s",
676 __func__, ssh_err(r));
677 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
679 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
680 p, datlen, alg, compat)) != 0) {
681 fatal("%s: ssh_agent_sign failed: %s",
682 __func__, ssh_err(r));
685 fatal("%s: no hostkey from index %d", __func__, keyid);
687 debug3("%s: %s signature %p(%zu)", __func__,
688 is_proof ? "KEX" : "hostkey proof", signature, siglen);
691 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
692 fatal("%s: buffer error: %s", __func__, ssh_err(r));
698 mm_request_send(sock, MONITOR_ANS_SIGN, m);
700 /* Turn on permissions for getpwnam */
701 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
706 /* Retrieves the password entry and also checks if the user is permitted */
709 mm_answer_pwnamallow(int sock, struct sshbuf *m)
711 struct ssh *ssh = active_state; /* XXX */
713 struct passwd *pwent;
717 debug3("%s", __func__);
719 if (authctxt->attempt++ != 0)
720 fatal("%s: multiple attempts for getpwnam", __func__);
722 if ((r = sshbuf_get_cstring(m, &username, NULL)) != 0)
723 fatal("%s: buffer error: %s", __func__, ssh_err(r));
725 pwent = getpwnamallow(username);
727 authctxt->user = xstrdup(username);
728 setproctitle("%s [priv]", pwent ? username : "unknown");
734 if ((r = sshbuf_put_u8(m, 0)) != 0)
735 fatal("%s: buffer error: %s", __func__, ssh_err(r));
736 authctxt->pw = fakepw();
741 authctxt->pw = pwent;
744 /* XXX don't sent pwent to unpriv; send fake class/dir/shell too */
745 if ((r = sshbuf_put_u8(m, 1)) != 0 ||
746 (r = sshbuf_put_string(m, pwent, sizeof(*pwent))) != 0 ||
747 (r = sshbuf_put_cstring(m, pwent->pw_name)) != 0 ||
748 (r = sshbuf_put_cstring(m, "*")) != 0 ||
749 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
750 (r = sshbuf_put_cstring(m, pwent->pw_gecos)) != 0 ||
752 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
753 (r = sshbuf_put_cstring(m, pwent->pw_class)) != 0 ||
755 (r = sshbuf_put_cstring(m, pwent->pw_dir)) != 0 ||
756 (r = sshbuf_put_cstring(m, pwent->pw_shell)) != 0)
757 fatal("%s: buffer error: %s", __func__, ssh_err(r));
760 ssh_packet_set_log_preamble(ssh, "%suser %s",
761 authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
762 if ((r = sshbuf_put_string(m, &options, sizeof(options))) != 0)
763 fatal("%s: buffer error: %s", __func__, ssh_err(r));
765 #define M_CP_STROPT(x) do { \
766 if (options.x != NULL) { \
767 if ((r = sshbuf_put_cstring(m, options.x)) != 0) \
768 fatal("%s: buffer error: %s", \
769 __func__, ssh_err(r)); \
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("%s: buffer error: %s", \
776 __func__, ssh_err(r)); \
779 /* See comment in servconf.h */
780 COPY_MATCH_STRING_OPTS();
782 #undef M_CP_STRARRAYOPT
784 /* Create valid auth method lists */
785 if (auth2_setup_methods_lists(authctxt) != 0) {
787 * The monitor will continue long enough to let the child
788 * run to it's packet_disconnect(), but it must not allow any
789 * authentication to succeed.
791 debug("%s: no valid authentication method lists", __func__);
794 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
795 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
797 /* Allow service/style information on the auth context */
798 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
799 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
803 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
809 int mm_answer_auth2_read_banner(int sock, struct sshbuf *m)
815 banner = auth2_read_banner();
816 if ((r = sshbuf_put_cstring(m, banner != NULL ? banner : "")) != 0)
817 fatal("%s: buffer error: %s", __func__, ssh_err(r));
818 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
825 mm_answer_authserv(int sock, struct sshbuf *m)
829 monitor_permit_authentications(1);
831 if ((r = sshbuf_get_cstring(m, &authctxt->service, NULL)) != 0 ||
832 (r = sshbuf_get_cstring(m, &authctxt->style, NULL)) != 0)
833 fatal("%s: buffer error: %s", __func__, ssh_err(r));
834 debug3("%s: service=%s, style=%s",
835 __func__, authctxt->service, authctxt->style);
837 if (strlen(authctxt->style) == 0) {
838 free(authctxt->style);
839 authctxt->style = NULL;
846 mm_answer_authpassword(int sock, struct sshbuf *m)
848 struct ssh *ssh = active_state; /* XXX */
849 static int call_count;
851 int r, authenticated;
854 if (!options.password_authentication)
855 fatal("%s: password authentication not enabled", __func__);
856 if ((r = sshbuf_get_cstring(m, &passwd, &plen)) != 0)
857 fatal("%s: buffer error: %s", __func__, ssh_err(r));
858 /* Only authenticate if the context is valid */
859 authenticated = options.password_authentication &&
860 auth_password(ssh, passwd);
861 explicit_bzero(passwd, plen);
865 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
866 fatal("%s: buffer error: %s", __func__, ssh_err(r));
868 if ((r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0)
869 fatal("%s: buffer error: %s", __func__, ssh_err(r));
872 debug3("%s: sending result %d", __func__, authenticated);
873 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
876 if (plen == 0 && call_count == 1)
877 auth_method = "none";
879 auth_method = "password";
881 /* Causes monitor loop to terminate if authenticated */
882 return (authenticated);
887 mm_answer_bsdauthquery(int sock, struct sshbuf *m)
889 char *name, *infotxt;
890 u_int numprompts, *echo_on, success;
894 if (!options.kbd_interactive_authentication)
895 fatal("%s: kbd-int authentication not enabled", __func__);
896 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
897 &prompts, &echo_on) < 0 ? 0 : 1;
900 if ((r = sshbuf_put_u32(m, success)) != 0)
901 fatal("%s: buffer error: %s", __func__, ssh_err(r));
903 if ((r = sshbuf_put_cstring(m, prompts[0])) != 0)
904 fatal("%s: buffer error: %s", __func__, ssh_err(r));
907 debug3("%s: sending challenge success: %u", __func__, success);
908 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
921 mm_answer_bsdauthrespond(int sock, struct sshbuf *m)
926 if (!options.kbd_interactive_authentication)
927 fatal("%s: kbd-int authentication not enabled", __func__);
928 if (authctxt->as == NULL)
929 fatal("%s: no bsd auth session", __func__);
931 if ((r = sshbuf_get_cstring(m, &response, NULL)) != 0)
932 fatal("%s: buffer error: %s", __func__, ssh_err(r));
933 authok = options.challenge_response_authentication &&
934 auth_userresponse(authctxt->as, response, 0);
936 debug3("%s: <%s> = <%d>", __func__, response, authok);
940 if ((r = sshbuf_put_u32(m, authok)) != 0)
941 fatal("%s: buffer error: %s", __func__, ssh_err(r));
943 debug3("%s: sending authenticated: %d", __func__, authok);
944 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
946 auth_method = "keyboard-interactive";
947 auth_submethod = "bsdauth";
949 return (authok != 0);
955 mm_answer_pam_start(int sock, struct sshbuf *m)
957 if (!options.use_pam)
958 fatal("UsePAM not set, but ended up in %s anyway", __func__);
962 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
963 if (options.kbd_interactive_authentication)
964 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
970 mm_answer_pam_account(int sock, struct sshbuf *m)
975 if (!options.use_pam)
976 fatal("%s: PAM not enabled", __func__);
978 ret = do_pam_account();
980 if ((r = sshbuf_put_u32(m, ret)) != 0 ||
981 (r = sshbuf_put_stringb(m, loginmsg)) != 0)
982 fatal("%s: buffer error: %s", __func__, ssh_err(r));
984 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
989 static void *sshpam_ctxt, *sshpam_authok;
990 extern KbdintDevice sshpam_device;
993 mm_answer_pam_init_ctx(int sock, struct sshbuf *m)
998 debug3("%s", __func__);
999 if (!options.kbd_interactive_authentication)
1000 fatal("%s: kbd-int authentication not enabled", __func__);
1001 if (sshpam_ctxt != NULL)
1002 fatal("%s: already called", __func__);
1003 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1004 sshpam_authok = NULL;
1006 if (sshpam_ctxt != NULL) {
1007 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1008 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
1011 if ((r = sshbuf_put_u32(m, ok)) != 0)
1012 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1013 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
1018 mm_answer_pam_query(int sock, struct sshbuf *m)
1020 char *name = NULL, *info = NULL, **prompts = NULL;
1021 u_int i, num = 0, *echo_on = 0;
1024 debug3("%s", __func__);
1025 sshpam_authok = NULL;
1026 if (sshpam_ctxt == NULL)
1027 fatal("%s: no context", __func__);
1028 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info,
1029 &num, &prompts, &echo_on);
1030 if (ret == 0 && num == 0)
1031 sshpam_authok = sshpam_ctxt;
1032 if (num > 1 || name == NULL || info == NULL)
1033 fatal("sshpam_device.query failed");
1034 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
1036 if ((r = sshbuf_put_u32(m, ret)) != 0 ||
1037 (r = sshbuf_put_cstring(m, name)) != 0 ||
1038 (r = sshbuf_put_cstring(m, info)) != 0 ||
1039 (r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0 ||
1040 (r = sshbuf_put_u32(m, num)) != 0)
1041 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1044 for (i = 0; i < num; ++i) {
1045 if ((r = sshbuf_put_cstring(m, prompts[i])) != 0 ||
1046 (r = sshbuf_put_u32(m, echo_on[i])) != 0)
1047 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1052 auth_method = "keyboard-interactive";
1053 auth_submethod = "pam";
1054 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
1059 mm_answer_pam_respond(int sock, struct sshbuf *m)
1065 debug3("%s", __func__);
1066 if (sshpam_ctxt == NULL)
1067 fatal("%s: no context", __func__);
1068 sshpam_authok = NULL;
1069 if ((r = sshbuf_get_u32(m, &num)) != 0)
1070 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1072 resp = xcalloc(num, sizeof(char *));
1073 for (i = 0; i < num; ++i) {
1074 if ((r = sshbuf_get_cstring(m, &(resp[i]), NULL)) != 0)
1075 fatal("%s: buffer error: %s",
1076 __func__, ssh_err(r));
1078 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1079 for (i = 0; i < num; ++i)
1083 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1086 if ((r = sshbuf_put_u32(m, ret)) != 0)
1087 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1088 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
1089 auth_method = "keyboard-interactive";
1090 auth_submethod = "pam";
1092 sshpam_authok = sshpam_ctxt;
1097 mm_answer_pam_free_ctx(int sock, struct sshbuf *m)
1099 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
1101 debug3("%s", __func__);
1102 if (sshpam_ctxt == NULL)
1103 fatal("%s: no context", __func__);
1104 (sshpam_device.free_ctx)(sshpam_ctxt);
1105 sshpam_ctxt = sshpam_authok = NULL;
1107 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
1108 /* Allow another attempt */
1109 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
1110 auth_method = "keyboard-interactive";
1111 auth_submethod = "pam";
1117 mm_answer_keyallowed(int sock, struct sshbuf *m)
1119 struct ssh *ssh = active_state; /* XXX */
1120 struct sshkey *key = NULL;
1121 char *cuser, *chost;
1122 u_int pubkey_auth_attempt;
1123 enum mm_keytype type = 0;
1125 struct sshauthopt *opts = NULL;
1127 debug3("%s entering", __func__);
1128 if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1129 (r = sshbuf_get_cstring(m, &cuser, NULL)) != 0 ||
1130 (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
1131 (r = sshkey_froms(m, &key)) != 0 ||
1132 (r = sshbuf_get_u32(m, &pubkey_auth_attempt)) != 0)
1133 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1135 debug3("%s: key_from_blob: %p", __func__, key);
1137 if (key != NULL && authctxt->valid) {
1138 /* These should not make it past the privsep child */
1139 if (sshkey_type_plain(key->type) == KEY_RSA &&
1140 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1141 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1145 auth_method = "publickey";
1146 if (!options.pubkey_authentication)
1148 if (auth2_key_already_used(authctxt, key))
1150 if (match_pattern_list(sshkey_ssh_name(key),
1151 options.pubkey_key_types, 0) != 1)
1153 allowed = user_key_allowed(ssh, authctxt->pw, key,
1154 pubkey_auth_attempt, &opts);
1157 auth_method = "hostbased";
1158 if (!options.hostbased_authentication)
1160 if (auth2_key_already_used(authctxt, key))
1162 if (match_pattern_list(sshkey_ssh_name(key),
1163 options.hostbased_key_types, 0) != 1)
1165 allowed = hostbased_key_allowed(authctxt->pw,
1167 auth2_record_info(authctxt,
1168 "client user \"%.100s\", client host \"%.100s\"",
1172 fatal("%s: unknown key type %d", __func__, type);
1177 debug3("%s: %s authentication%s: %s key is %s", __func__,
1178 auth_method, pubkey_auth_attempt ? "" : " test",
1179 (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key),
1180 allowed ? "allowed" : "not allowed");
1182 auth2_record_key(authctxt, 0, key);
1184 /* clear temporarily storage (used by verify) */
1185 monitor_reset_key_state();
1188 /* Save temporarily for comparison in verify */
1189 if ((r = sshkey_to_blob(key, &key_blob, &key_bloblen)) != 0)
1190 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1191 key_blobtype = type;
1193 hostbased_cuser = cuser;
1194 hostbased_chost = chost;
1196 /* Log failed attempt */
1197 auth_log(authctxt, 0, 0, auth_method, NULL);
1204 if ((r = sshbuf_put_u32(m, allowed)) != 0)
1205 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1206 if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0)
1207 fatal("%s: sshauthopt_serialise: %s", __func__, ssh_err(r));
1208 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1211 sshauthopt_free(opts);
1217 monitor_valid_userblob(u_char *data, u_int datalen)
1221 char *userstyle, *cp;
1226 if ((b = sshbuf_new()) == NULL)
1227 fatal("%s: sshbuf_new", __func__);
1228 if ((r = sshbuf_put(b, data, datalen)) != 0)
1229 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1231 if (datafellows & SSH_OLD_SESSIONID) {
1233 len = sshbuf_len(b);
1234 if ((session_id2 == NULL) ||
1235 (len < session_id2_len) ||
1236 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1238 if ((r = sshbuf_consume(b, session_id2_len)) != 0)
1239 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1241 if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1242 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1243 if ((session_id2 == NULL) ||
1244 (len != session_id2_len) ||
1245 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1248 if ((r = sshbuf_get_u8(b, &type)) != 0)
1249 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1250 if (type != SSH2_MSG_USERAUTH_REQUEST)
1252 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1253 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1254 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1255 authctxt->style ? ":" : "",
1256 authctxt->style ? authctxt->style : "");
1257 if (strcmp(userstyle, cp) != 0) {
1258 logit("wrong user name passed to monitor: "
1259 "expected %s != %.100s", userstyle, cp);
1264 if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1265 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1266 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1267 if (strcmp("publickey", cp) != 0)
1270 if ((r = sshbuf_get_u8(b, &type)) != 0)
1271 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1274 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1275 (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1276 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1277 if (sshbuf_len(b) != 0)
1284 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1289 char *cp, *userstyle;
1294 if ((b = sshbuf_new()) == NULL)
1295 fatal("%s: sshbuf_new", __func__);
1296 if ((r = sshbuf_put(b, data, datalen)) != 0 ||
1297 (r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1298 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1300 if ((session_id2 == NULL) ||
1301 (len != session_id2_len) ||
1302 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1305 if ((r = sshbuf_get_u8(b, &type)) != 0)
1306 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1307 if (type != SSH2_MSG_USERAUTH_REQUEST)
1309 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1310 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1311 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1312 authctxt->style ? ":" : "",
1313 authctxt->style ? authctxt->style : "");
1314 if (strcmp(userstyle, cp) != 0) {
1315 logit("wrong user name passed to monitor: "
1316 "expected %s != %.100s", userstyle, cp);
1321 if ((r = sshbuf_skip_string(b)) != 0 || /* service */
1322 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1323 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1324 if (strcmp(cp, "hostbased") != 0)
1327 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
1328 (r = sshbuf_skip_string(b)) != 0) /* pkblob */
1329 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1331 /* verify client host, strip trailing dot if necessary */
1332 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1333 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1334 if (((len = strlen(cp)) > 0) && cp[len - 1] == '.')
1336 if (strcmp(cp, chost) != 0)
1340 /* verify client user */
1341 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1342 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1343 if (strcmp(cp, cuser) != 0)
1347 if (sshbuf_len(b) != 0)
1354 mm_answer_keyverify(int sock, struct sshbuf *m)
1356 struct ssh *ssh = active_state; /* XXX */
1358 u_char *signature, *data, *blob;
1360 size_t signaturelen, datalen, bloblen;
1361 int r, ret, valid_data = 0, encoded_ret;
1363 if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
1364 (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 ||
1365 (r = sshbuf_get_string(m, &data, &datalen)) != 0 ||
1366 (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
1367 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1369 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1370 !monitor_allowed_key(blob, bloblen))
1371 fatal("%s: bad key, not previously allowed", __func__);
1373 /* Empty signature algorithm means NULL. */
1374 if (*sigalg == '\0') {
1379 /* XXX use sshkey_froms here; need to change key_blob, etc. */
1380 if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1381 fatal("%s: bad public key blob: %s", __func__, ssh_err(r));
1383 switch (key_blobtype) {
1385 valid_data = monitor_valid_userblob(data, datalen);
1386 auth_method = "publickey";
1389 valid_data = monitor_valid_hostbasedblob(data, datalen,
1390 hostbased_cuser, hostbased_chost);
1391 auth_method = "hostbased";
1398 fatal("%s: bad signature data blob", __func__);
1400 ret = sshkey_verify(key, signature, signaturelen, data, datalen,
1401 sigalg, active_state->compat);
1402 debug3("%s: %s %p signature %s", __func__, auth_method, key,
1403 (ret == 0) ? "verified" : "unverified");
1404 auth2_record_key(authctxt, ret == 0, key);
1411 if (key_blobtype == MM_USERKEY)
1412 auth_activate_options(ssh, key_opts);
1413 monitor_reset_key_state();
1418 /* encode ret != 0 as positive integer, since we're sending u32 */
1419 encoded_ret = (ret != 0);
1420 if ((r = sshbuf_put_u32(m, encoded_ret)) != 0)
1421 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1422 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1428 mm_record_login(Session *s, struct passwd *pw)
1430 struct ssh *ssh = active_state; /* XXX */
1432 struct sockaddr_storage from;
1435 * Get IP address of client. If the connection is not a socket, let
1436 * the address be 0.0.0.0.
1438 memset(&from, 0, sizeof(from));
1439 fromlen = sizeof(from);
1440 if (packet_connection_is_on_socket()) {
1441 if (getpeername(packet_get_connection_in(),
1442 (struct sockaddr *)&from, &fromlen) < 0) {
1443 debug("getpeername: %.100s", strerror(errno));
1447 /* Record that there was a login on that tty from the remote host. */
1448 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1449 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
1450 (struct sockaddr *)&from, fromlen);
1454 mm_session_close(Session *s)
1456 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1457 if (s->ttyfd != -1) {
1458 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1459 session_pty_cleanup2(s);
1461 session_unused(s->self);
1465 mm_answer_pty(int sock, struct sshbuf *m)
1467 extern struct monitor *pmonitor;
1471 debug3("%s entering", __func__);
1477 s->authctxt = authctxt;
1478 s->pw = authctxt->pw;
1479 s->pid = pmonitor->m_pid;
1480 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1483 pty_setowner(authctxt->pw, s->tty);
1485 if ((r = sshbuf_put_u32(m, 1)) != 0 ||
1486 (r = sshbuf_put_cstring(m, s->tty)) != 0)
1487 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1489 /* We need to trick ttyslot */
1490 if (dup2(s->ttyfd, 0) == -1)
1491 fatal("%s: dup2", __func__);
1493 mm_record_login(s, authctxt->pw);
1495 /* Now we can close the file descriptor again */
1498 /* send messages generated by record_login */
1499 if ((r = sshbuf_put_stringb(m, loginmsg)) != 0)
1500 fatal("%s: put login message: %s", __func__, ssh_err(r));
1501 sshbuf_reset(loginmsg);
1503 mm_request_send(sock, MONITOR_ANS_PTY, m);
1505 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1506 mm_send_fd(sock, s->ttyfd) == -1)
1507 fatal("%s: send fds failed", __func__);
1509 /* make sure nothing uses fd 0 */
1510 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1511 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1513 error("%s: fd0 %d != 0", __func__, fd0);
1515 /* slave is not needed */
1517 s->ttyfd = s->ptyfd;
1518 /* no need to dup() because nobody closes ptyfd */
1519 s->ptymaster = s->ptyfd;
1521 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1527 mm_session_close(s);
1528 if ((r = sshbuf_put_u32(m, 0)) != 0)
1529 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1530 mm_request_send(sock, MONITOR_ANS_PTY, m);
1535 mm_answer_pty_cleanup(int sock, struct sshbuf *m)
1541 debug3("%s entering", __func__);
1543 if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
1544 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1545 if ((s = session_by_tty(tty)) != NULL)
1546 mm_session_close(s);
1553 mm_answer_term(int sock, struct sshbuf *req)
1555 struct ssh *ssh = active_state; /* XXX */
1556 extern struct monitor *pmonitor;
1559 debug3("%s: tearing down sessions", __func__);
1561 /* The child is terminating */
1562 session_destroy_all(ssh, &mm_session_close);
1565 if (options.use_pam)
1569 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1573 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1575 /* Terminate process */
1579 #ifdef SSH_AUDIT_EVENTS
1580 /* Report that an audit event occurred */
1582 mm_answer_audit_event(int socket, struct sshbuf *m)
1585 ssh_audit_event_t event;
1588 debug3("%s entering", __func__);
1590 if ((r = sshbuf_get_u32(m, &n)) != 0)
1591 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1592 event = (ssh_audit_event_t)n;
1594 case SSH_AUTH_FAIL_PUBKEY:
1595 case SSH_AUTH_FAIL_HOSTBASED:
1596 case SSH_AUTH_FAIL_GSSAPI:
1597 case SSH_LOGIN_EXCEED_MAXTRIES:
1598 case SSH_LOGIN_ROOT_DENIED:
1599 case SSH_CONNECTION_CLOSE:
1600 case SSH_INVALID_USER:
1604 fatal("Audit event type %d not permitted", event);
1611 mm_answer_audit_command(int socket, struct sshbuf *m)
1616 debug3("%s entering", __func__);
1617 if ((r = sshbuf_get_cstring(m, &cmd, NULL)) != 0)
1618 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1619 /* sanity check command, if so how? */
1620 audit_run_command(cmd);
1624 #endif /* SSH_AUDIT_EVENTS */
1627 monitor_clear_keystate(struct monitor *pmonitor)
1629 struct ssh *ssh = active_state; /* XXX */
1631 ssh_clear_newkeys(ssh, MODE_IN);
1632 ssh_clear_newkeys(ssh, MODE_OUT);
1633 sshbuf_free(child_state);
1638 monitor_apply_keystate(struct monitor *pmonitor)
1640 struct ssh *ssh = active_state; /* XXX */
1644 debug3("%s: packet_set_state", __func__);
1645 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1646 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1647 sshbuf_free(child_state);
1650 if ((kex = ssh->kex) != NULL) {
1651 /* XXX set callbacks */
1653 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1654 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1655 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
1656 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
1657 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
1658 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1659 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1660 # ifdef OPENSSL_HAS_ECC
1661 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
1663 #endif /* WITH_OPENSSL */
1664 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1665 kex->load_host_public_key=&get_hostkey_public_by_type;
1666 kex->load_host_private_key=&get_hostkey_private_by_type;
1667 kex->host_key_index=&get_hostkey_index;
1668 kex->sign = sshd_hostkey_sign;
1672 /* This function requries careful sanity checking */
1675 mm_get_keystate(struct monitor *pmonitor)
1677 debug3("%s: Waiting for new keys", __func__);
1679 if ((child_state = sshbuf_new()) == NULL)
1680 fatal("%s: sshbuf_new failed", __func__);
1681 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1683 debug3("%s: GOT new keys", __func__);
1689 #define FD_CLOSEONEXEC(x) do { \
1690 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
1691 fatal("fcntl(%d, F_SETFD)", x); \
1695 monitor_openfds(struct monitor *mon, int do_logfds)
1702 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1703 fatal("%s: socketpair: %s", __func__, strerror(errno));
1705 if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1706 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
1707 if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1708 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
1710 FD_CLOSEONEXEC(pair[0]);
1711 FD_CLOSEONEXEC(pair[1]);
1712 mon->m_recvfd = pair[0];
1713 mon->m_sendfd = pair[1];
1716 if (pipe(pair) == -1)
1717 fatal("%s: pipe: %s", __func__, strerror(errno));
1718 FD_CLOSEONEXEC(pair[0]);
1719 FD_CLOSEONEXEC(pair[1]);
1720 mon->m_log_recvfd = pair[0];
1721 mon->m_log_sendfd = pair[1];
1723 mon->m_log_recvfd = mon->m_log_sendfd = -1;
1726 #define MM_MEMSIZE 65536
1731 struct monitor *mon;
1733 mon = xcalloc(1, sizeof(*mon));
1734 monitor_openfds(mon, 1);
1740 monitor_reinit(struct monitor *mon)
1742 monitor_openfds(mon, 0);
1747 mm_answer_gss_setup_ctx(int sock, struct sshbuf *m)
1755 if (!options.gss_authentication)
1756 fatal("%s: GSSAPI authentication not enabled", __func__);
1758 if ((r = sshbuf_get_string(m, &p, &len)) != 0)
1759 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1763 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1765 free(goid.elements);
1768 if ((r = sshbuf_put_u32(m, major)) != 0)
1769 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1771 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1773 /* Now we have a context, enable the step */
1774 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1780 mm_answer_gss_accept_ctx(int sock, struct sshbuf *m)
1783 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1784 OM_uint32 major, minor;
1785 OM_uint32 flags = 0; /* GSI needs this */
1788 if (!options.gss_authentication)
1789 fatal("%s: GSSAPI authentication not enabled", __func__);
1791 if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0)
1792 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1793 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1797 if ((r = sshbuf_put_u32(m, major)) != 0 ||
1798 (r = sshbuf_put_string(m, out.value, out.length)) != 0 ||
1799 (r = sshbuf_put_u32(m, flags)) != 0)
1800 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1801 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1803 gss_release_buffer(&minor, &out);
1805 if (major == GSS_S_COMPLETE) {
1806 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1807 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1808 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1814 mm_answer_gss_checkmic(int sock, struct sshbuf *m)
1816 gss_buffer_desc gssbuf, mic;
1820 if (!options.gss_authentication)
1821 fatal("%s: GSSAPI authentication not enabled", __func__);
1823 if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 ||
1824 (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0)
1825 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1827 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1833 if ((r = sshbuf_put_u32(m, ret)) != 0)
1834 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1836 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1838 if (!GSS_ERROR(ret))
1839 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1845 mm_answer_gss_userok(int sock, struct sshbuf *m)
1847 int r, authenticated;
1848 const char *displayname;
1850 if (!options.gss_authentication)
1851 fatal("%s: GSSAPI authentication not enabled", __func__);
1853 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1856 if ((r = sshbuf_put_u32(m, authenticated)) != 0)
1857 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1859 debug3("%s: sending result %d", __func__, authenticated);
1860 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1862 auth_method = "gssapi-with-mic";
1864 if ((displayname = ssh_gssapi_displayname()) != NULL)
1865 auth2_record_info(authctxt, "%s", displayname);
1867 /* Monitor loop will terminate if authenticated */
1868 return (authenticated);