Home | History | Annotate | Line # | Download | only in dist
monitor.c revision 1.40
      1  1.32  christos /*	$NetBSD: monitor.c,v 1.40 2022/04/15 14:00:06 christos Exp $	*/
      2  1.40  christos /* $OpenBSD: monitor.c,v 1.232 2022/02/25 02:09:27 djm Exp $ */
      3   1.1  christos /*
      4   1.1  christos  * Copyright 2002 Niels Provos <provos (at) citi.umich.edu>
      5   1.1  christos  * Copyright 2002 Markus Friedl <markus (at) openbsd.org>
      6   1.1  christos  * All rights reserved.
      7   1.1  christos  *
      8   1.1  christos  * Redistribution and use in source and binary forms, with or without
      9   1.1  christos  * modification, are permitted provided that the following conditions
     10   1.1  christos  * are met:
     11   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     12   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     13   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     15   1.1  christos  *    documentation and/or other materials provided with the distribution.
     16   1.1  christos  *
     17   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18   1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19   1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20   1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21   1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22   1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23   1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24   1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25   1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26   1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27   1.1  christos  */
     28   1.1  christos 
     29   1.2  christos #include "includes.h"
     30  1.32  christos __RCSID("$NetBSD: monitor.c,v 1.40 2022/04/15 14:00:06 christos Exp $");
     31   1.1  christos #include <sys/types.h>
     32   1.1  christos #include <sys/wait.h>
     33   1.1  christos #include <sys/socket.h>
     34   1.1  christos #include <sys/tree.h>
     35   1.1  christos #include <sys/queue.h>
     36   1.1  christos 
     37  1.13  christos #ifdef WITH_OPENSSL
     38   1.1  christos #include <openssl/dh.h>
     39  1.13  christos #endif
     40   1.1  christos 
     41   1.1  christos #include <errno.h>
     42   1.1  christos #include <fcntl.h>
     43  1.19  christos #include <limits.h>
     44   1.1  christos #include <paths.h>
     45   1.7  christos #include <poll.h>
     46   1.1  christos #include <pwd.h>
     47   1.1  christos #include <signal.h>
     48  1.13  christos #include <stdarg.h>
     49  1.21  christos #include <unistd.h>
     50  1.14  christos #include <stdint.h>
     51  1.13  christos #include <stdio.h>
     52   1.1  christos #include <stdlib.h>
     53   1.1  christos #include <string.h>
     54  1.27  christos #include <unistd.h>
     55   1.1  christos 
     56   1.7  christos #include "atomicio.h"
     57   1.1  christos #include "xmalloc.h"
     58   1.1  christos #include "ssh.h"
     59  1.27  christos #include "sshkey.h"
     60  1.27  christos #include "sshbuf.h"
     61   1.1  christos #include "hostfile.h"
     62   1.1  christos #include "auth.h"
     63   1.1  christos #include "cipher.h"
     64   1.1  christos #include "kex.h"
     65   1.1  christos #include "dh.h"
     66   1.1  christos #include "packet.h"
     67   1.1  christos #include "auth-options.h"
     68   1.1  christos #include "sshpty.h"
     69   1.1  christos #include "channels.h"
     70   1.1  christos #include "session.h"
     71   1.1  christos #include "sshlogin.h"
     72   1.1  christos #include "canohost.h"
     73   1.1  christos #include "log.h"
     74  1.13  christos #include "misc.h"
     75   1.1  christos #include "servconf.h"
     76   1.1  christos #include "monitor.h"
     77   1.1  christos #ifdef GSSAPI
     78   1.1  christos #include "ssh-gss.h"
     79   1.1  christos #endif
     80   1.1  christos #include "monitor_wrap.h"
     81   1.1  christos #include "monitor_fdpass.h"
     82   1.1  christos #include "compat.h"
     83   1.1  christos #include "ssh2.h"
     84  1.11  christos #include "authfd.h"
     85  1.14  christos #include "match.h"
     86  1.14  christos #include "ssherr.h"
     87  1.33  christos #include "sk-api.h"
     88   1.1  christos 
     89  1.32  christos #include "pfilter.h"
     90  1.32  christos 
     91   1.1  christos #ifdef GSSAPI
     92   1.1  christos static Gssctxt *gsscontext = NULL;
     93   1.1  christos #endif
     94   1.1  christos 
     95   1.1  christos /* Imports */
     96   1.1  christos extern ServerOptions options;
     97   1.1  christos extern u_int utmp_len;
     98  1.27  christos extern struct sshbuf *loginmsg;
     99  1.25  christos extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */
    100   1.1  christos 
    101   1.1  christos /* State exported from the child */
    102  1.14  christos static struct sshbuf *child_state;
    103   1.1  christos 
    104   1.1  christos /* Functions on the monitor that answer unprivileged requests */
    105   1.1  christos 
    106  1.29  christos int mm_answer_moduli(struct ssh *, int, struct sshbuf *);
    107  1.29  christos int mm_answer_sign(struct ssh *, int, struct sshbuf *);
    108  1.29  christos int mm_answer_pwnamallow(struct ssh *, int, struct sshbuf *);
    109  1.29  christos int mm_answer_auth2_read_banner(struct ssh *, int, struct sshbuf *);
    110  1.29  christos int mm_answer_authserv(struct ssh *, int, struct sshbuf *);
    111  1.29  christos int mm_answer_authpassword(struct ssh *, int, struct sshbuf *);
    112  1.29  christos int mm_answer_bsdauthquery(struct ssh *, int, struct sshbuf *);
    113  1.29  christos int mm_answer_bsdauthrespond(struct ssh *, int, struct sshbuf *);
    114  1.29  christos int mm_answer_keyallowed(struct ssh *, int, struct sshbuf *);
    115  1.29  christos int mm_answer_keyverify(struct ssh *, int, struct sshbuf *);
    116  1.29  christos int mm_answer_pty(struct ssh *, int, struct sshbuf *);
    117  1.29  christos int mm_answer_pty_cleanup(struct ssh *, int, struct sshbuf *);
    118  1.29  christos int mm_answer_term(struct ssh *, int, struct sshbuf *);
    119  1.29  christos int mm_answer_rsa_keyallowed(struct ssh *, int, struct sshbuf *);
    120  1.29  christos int mm_answer_rsa_challenge(struct ssh *, int, struct sshbuf *);
    121  1.29  christos int mm_answer_rsa_response(struct ssh *, int, struct sshbuf *);
    122  1.29  christos int mm_answer_sesskey(struct ssh *, int, struct sshbuf *);
    123  1.29  christos int mm_answer_sessid(struct ssh *, int, struct sshbuf *);
    124   1.1  christos 
    125   1.2  christos #ifdef USE_PAM
    126  1.29  christos int mm_answer_pam_start(struct ssh *, int, struct sshbuf *);
    127  1.29  christos int mm_answer_pam_account(struct ssh *, int, struct sshbuf *);
    128  1.29  christos int mm_answer_pam_init_ctx(struct ssh *, int, struct sshbuf *);
    129  1.29  christos int mm_answer_pam_query(struct ssh *, int, struct sshbuf *);
    130  1.29  christos int mm_answer_pam_respond(struct ssh *, int, struct sshbuf *);
    131  1.29  christos int mm_answer_pam_free_ctx(struct ssh *, int, struct sshbuf *);
    132   1.2  christos #endif
    133   1.2  christos 
    134   1.2  christos #ifdef KRB5
    135  1.29  christos int mm_answer_krb5(struct ssh *, int, struct sshbuf *);
    136  1.20  christos #endif
    137   1.2  christos 
    138   1.1  christos #ifdef GSSAPI
    139  1.29  christos int mm_answer_gss_setup_ctx(struct ssh *, int, struct sshbuf *);
    140  1.29  christos int mm_answer_gss_accept_ctx(struct ssh *, int, struct sshbuf *);
    141  1.29  christos int mm_answer_gss_userok(struct ssh *, int, struct sshbuf *);
    142  1.29  christos int mm_answer_gss_checkmic(struct ssh *, int, struct sshbuf *);
    143   1.1  christos #endif
    144   1.1  christos 
    145   1.1  christos /* local state for key verify */
    146   1.1  christos static u_char *key_blob = NULL;
    147  1.27  christos static size_t key_bloblen = 0;
    148  1.37  christos static u_int key_blobtype = MM_NOKEY;
    149  1.25  christos static struct sshauthopt *key_opts = NULL;
    150   1.1  christos static char *hostbased_cuser = NULL;
    151   1.1  christos static char *hostbased_chost = NULL;
    152   1.6  christos static const char *auth_method = "unknown";
    153  1.10  christos static const char *auth_submethod = NULL;
    154   1.1  christos static u_int session_id2_len = 0;
    155   1.1  christos static u_char *session_id2 = NULL;
    156   1.1  christos static pid_t monitor_child_pid;
    157   1.1  christos 
    158   1.1  christos struct mon_table {
    159   1.1  christos 	enum monitor_reqtype type;
    160   1.1  christos 	int flags;
    161  1.29  christos 	int (*f)(struct ssh *, int, struct sshbuf *);
    162   1.1  christos };
    163   1.1  christos 
    164   1.1  christos #define MON_ISAUTH	0x0004	/* Required for Authentication */
    165   1.1  christos #define MON_AUTHDECIDE	0x0008	/* Decides Authentication */
    166   1.1  christos #define MON_ONCE	0x0010	/* Disable after calling */
    167   1.1  christos #define MON_ALOG	0x0020	/* Log auth attempt without authenticating */
    168   1.1  christos 
    169   1.1  christos #define MON_AUTH	(MON_ISAUTH|MON_AUTHDECIDE)
    170   1.1  christos 
    171   1.1  christos #define MON_PERMIT	0x1000	/* Request is permitted */
    172   1.1  christos 
    173  1.29  christos static int monitor_read(struct ssh *, struct monitor *, struct mon_table *,
    174  1.29  christos     struct mon_table **);
    175  1.29  christos static int monitor_read_log(struct monitor *);
    176  1.29  christos 
    177   1.1  christos struct mon_table mon_dispatch_proto20[] = {
    178  1.13  christos #ifdef WITH_OPENSSL
    179   1.1  christos     {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
    180  1.13  christos #endif
    181   1.1  christos     {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
    182   1.1  christos     {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
    183   1.1  christos     {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
    184   1.1  christos     {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
    185   1.1  christos     {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
    186   1.2  christos #ifdef USE_PAM
    187   1.2  christos     {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
    188   1.2  christos     {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
    189   1.2  christos     {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
    190   1.2  christos     {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
    191   1.2  christos     {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
    192   1.2  christos     {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
    193   1.2  christos #endif
    194   1.2  christos #ifdef BSD_AUTH
    195   1.1  christos     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
    196   1.1  christos     {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
    197   1.2  christos #endif
    198   1.1  christos     {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
    199   1.1  christos     {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
    200   1.2  christos #ifdef KRB5
    201   1.2  christos     {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
    202   1.2  christos #endif
    203   1.1  christos #ifdef GSSAPI
    204   1.1  christos     {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
    205  1.20  christos     {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
    206  1.20  christos     {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
    207  1.20  christos     {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
    208   1.1  christos #endif
    209   1.1  christos     {0, 0, NULL}
    210   1.1  christos };
    211   1.1  christos 
    212   1.1  christos struct mon_table mon_dispatch_postauth20[] = {
    213  1.13  christos #ifdef WITH_OPENSSL
    214   1.1  christos     {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
    215  1.13  christos #endif
    216   1.1  christos     {MONITOR_REQ_SIGN, 0, mm_answer_sign},
    217   1.1  christos     {MONITOR_REQ_PTY, 0, mm_answer_pty},
    218   1.1  christos     {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
    219   1.1  christos     {MONITOR_REQ_TERM, 0, mm_answer_term},
    220   1.1  christos     {0, 0, NULL}
    221   1.1  christos };
    222   1.1  christos 
    223   1.1  christos struct mon_table *mon_dispatch;
    224   1.1  christos 
    225   1.1  christos /* Specifies if a certain message is allowed at the moment */
    226   1.1  christos static void
    227   1.1  christos monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
    228   1.1  christos {
    229   1.1  christos 	while (ent->f != NULL) {
    230   1.1  christos 		if (ent->type == type) {
    231   1.1  christos 			ent->flags &= ~MON_PERMIT;
    232   1.1  christos 			ent->flags |= permit ? MON_PERMIT : 0;
    233   1.1  christos 			return;
    234   1.1  christos 		}
    235   1.1  christos 		ent++;
    236   1.1  christos 	}
    237   1.1  christos }
    238   1.1  christos 
    239   1.1  christos static void
    240   1.1  christos monitor_permit_authentications(int permit)
    241   1.1  christos {
    242   1.1  christos 	struct mon_table *ent = mon_dispatch;
    243   1.1  christos 
    244   1.1  christos 	while (ent->f != NULL) {
    245   1.1  christos 		if (ent->flags & MON_AUTH) {
    246   1.1  christos 			ent->flags &= ~MON_PERMIT;
    247   1.1  christos 			ent->flags |= permit ? MON_PERMIT : 0;
    248   1.1  christos 		}
    249   1.1  christos 		ent++;
    250   1.1  christos 	}
    251   1.1  christos }
    252   1.1  christos 
    253   1.1  christos void
    254  1.29  christos monitor_child_preauth(struct ssh *ssh, struct monitor *pmonitor)
    255   1.1  christos {
    256   1.1  christos 	struct mon_table *ent;
    257  1.10  christos 	int authenticated = 0, partial = 0;
    258  1.29  christos 	Authctxt *authctxt = ssh->authctxt;
    259   1.1  christos 
    260   1.1  christos 	debug3("preauth child monitor started");
    261   1.1  christos 
    262  1.25  christos 	if (pmonitor->m_recvfd >= 0)
    263  1.25  christos 		close(pmonitor->m_recvfd);
    264  1.25  christos 	if (pmonitor->m_log_sendfd >= 0)
    265  1.25  christos 		close(pmonitor->m_log_sendfd);
    266   1.7  christos 	pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
    267   1.7  christos 
    268   1.1  christos 	memset(authctxt, 0, sizeof(*authctxt));
    269  1.25  christos 	ssh->authctxt = authctxt;
    270   1.1  christos 
    271  1.20  christos 	mon_dispatch = mon_dispatch_proto20;
    272  1.20  christos 	/* Permit requests for moduli and signatures */
    273  1.20  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
    274  1.20  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
    275   1.1  christos 
    276   1.1  christos 	/* The first few requests do not require asynchronous access */
    277   1.1  christos 	while (!authenticated) {
    278  1.10  christos 		partial = 0;
    279   1.1  christos 		auth_method = "unknown";
    280  1.10  christos 		auth_submethod = NULL;
    281  1.23  christos 		auth2_authctxt_reset_info(authctxt);
    282  1.23  christos 
    283  1.29  christos 		authenticated = (monitor_read(ssh, pmonitor,
    284  1.29  christos 		    mon_dispatch, &ent) == 1);
    285  1.10  christos 
    286  1.10  christos 		/* Special handling for multiple required authentications */
    287  1.10  christos 		if (options.num_auth_methods != 0) {
    288  1.10  christos 			if (authenticated &&
    289  1.10  christos 			    !auth2_update_methods_lists(authctxt,
    290  1.11  christos 			    auth_method, auth_submethod)) {
    291  1.36  christos 				debug3_f("method %s: partial", auth_method);
    292  1.10  christos 				authenticated = 0;
    293  1.10  christos 				partial = 1;
    294  1.10  christos 			}
    295  1.10  christos 		}
    296  1.10  christos 
    297   1.1  christos 		if (authenticated) {
    298   1.1  christos 			if (!(ent->flags & MON_AUTHDECIDE))
    299  1.36  christos 				fatal_f("unexpected authentication from %d",
    300  1.36  christos 				    ent->type);
    301   1.1  christos 			if (authctxt->pw->pw_uid == 0 &&
    302  1.25  christos 			    !auth_root_allowed(ssh, auth_method))
    303   1.1  christos 				authenticated = 0;
    304   1.2  christos #ifdef USE_PAM
    305   1.2  christos 			/* PAM needs to perform account checks after auth */
    306   1.2  christos 			if (options.use_pam && authenticated) {
    307  1.27  christos 				struct sshbuf *m;
    308  1.27  christos 
    309  1.27  christos 				if ((m = sshbuf_new()) == NULL)
    310  1.27  christos 					fatal("%s: sshbuf_new failed", __func__);
    311   1.2  christos 
    312   1.2  christos 				mm_request_receive_expect(pmonitor->m_sendfd,
    313  1.27  christos 				    MONITOR_REQ_PAM_ACCOUNT, m);
    314  1.29  christos 				authenticated = mm_answer_pam_account(ssh, pmonitor->m_sendfd, m);
    315  1.27  christos 				sshbuf_free(m);
    316   1.2  christos 			}
    317   1.2  christos #endif
    318   1.1  christos 		}
    319   1.1  christos 		if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
    320  1.29  christos 			auth_log(ssh, authenticated, partial,
    321  1.11  christos 			    auth_method, auth_submethod);
    322  1.31  christos 			if (!partial && !authenticated) {
    323  1.31  christos 				pfilter_notify(1);
    324   1.1  christos 				authctxt->failures++;
    325  1.31  christos 			}
    326  1.23  christos 			if (authenticated || partial) {
    327  1.23  christos 				auth2_update_session_info(authctxt,
    328  1.23  christos 				    auth_method, auth_submethod);
    329  1.23  christos 			}
    330   1.1  christos 		}
    331   1.1  christos 	}
    332   1.1  christos 
    333   1.1  christos 	if (!authctxt->valid)
    334  1.36  christos 		fatal_f("authenticated invalid user");
    335   1.1  christos 	if (strcmp(auth_method, "unknown") == 0)
    336  1.36  christos 		fatal_f("authentication method name unknown");
    337   1.1  christos 
    338  1.36  christos 	debug_f("user %s authenticated by privileged process", authctxt->user);
    339  1.25  christos 	ssh->authctxt = NULL;
    340  1.22  christos 	ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
    341   1.1  christos 
    342  1.29  christos 	mm_get_keystate(ssh, pmonitor);
    343   1.7  christos 
    344  1.10  christos 	/* Drain any buffered messages from the child */
    345  1.10  christos 	while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
    346  1.10  christos 		;
    347  1.10  christos 
    348  1.25  christos 	if (pmonitor->m_recvfd >= 0)
    349  1.25  christos 		close(pmonitor->m_recvfd);
    350  1.25  christos 	if (pmonitor->m_log_sendfd >= 0)
    351  1.25  christos 		close(pmonitor->m_log_sendfd);
    352   1.7  christos 	pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
    353   1.1  christos }
    354   1.1  christos 
    355   1.1  christos static void
    356   1.1  christos monitor_set_child_handler(pid_t pid)
    357   1.1  christos {
    358   1.1  christos 	monitor_child_pid = pid;
    359   1.1  christos }
    360   1.1  christos 
    361   1.1  christos static void
    362   1.1  christos monitor_child_handler(int sig)
    363   1.1  christos {
    364   1.1  christos 	kill(monitor_child_pid, sig);
    365   1.1  christos }
    366   1.1  christos 
    367   1.1  christos void
    368  1.29  christos monitor_child_postauth(struct ssh *ssh, struct monitor *pmonitor)
    369   1.1  christos {
    370   1.7  christos 	close(pmonitor->m_recvfd);
    371   1.7  christos 	pmonitor->m_recvfd = -1;
    372   1.7  christos 
    373   1.1  christos 	monitor_set_child_handler(pmonitor->m_pid);
    374  1.33  christos 	ssh_signal(SIGHUP, &monitor_child_handler);
    375  1.33  christos 	ssh_signal(SIGTERM, &monitor_child_handler);
    376  1.33  christos 	ssh_signal(SIGINT, &monitor_child_handler);
    377   1.1  christos 
    378  1.20  christos 	mon_dispatch = mon_dispatch_postauth20;
    379  1.20  christos 
    380  1.20  christos 	/* Permit requests for moduli and signatures */
    381  1.20  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
    382  1.20  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
    383  1.20  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
    384   1.1  christos 
    385  1.25  christos 	if (auth_opts->permit_pty_flag) {
    386   1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
    387   1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
    388   1.1  christos 	}
    389   1.1  christos 
    390   1.1  christos 	for (;;)
    391  1.29  christos 		monitor_read(ssh, pmonitor, mon_dispatch, NULL);
    392   1.1  christos }
    393   1.1  christos 
    394   1.7  christos static int
    395   1.7  christos monitor_read_log(struct monitor *pmonitor)
    396   1.7  christos {
    397  1.27  christos 	struct sshbuf *logmsg;
    398  1.37  christos 	u_int len, level, forced;
    399  1.37  christos 	char *msg;
    400  1.27  christos 	u_char *p;
    401  1.27  christos 	int r;
    402   1.7  christos 
    403  1.27  christos 	if ((logmsg = sshbuf_new()) == NULL)
    404  1.36  christos 		fatal_f("sshbuf_new");
    405   1.7  christos 
    406   1.7  christos 	/* Read length */
    407  1.27  christos 	if ((r = sshbuf_reserve(logmsg, 4, &p)) != 0)
    408  1.36  christos 		fatal_fr(r, "reserve len");
    409  1.27  christos 	if (atomicio(read, pmonitor->m_log_recvfd, p, 4) != 4) {
    410   1.7  christos 		if (errno == EPIPE) {
    411  1.27  christos 			sshbuf_free(logmsg);
    412  1.36  christos 			debug_f("child log fd closed");
    413   1.7  christos 			close(pmonitor->m_log_recvfd);
    414   1.7  christos 			pmonitor->m_log_recvfd = -1;
    415   1.7  christos 			return -1;
    416   1.7  christos 		}
    417  1.36  christos 		fatal_f("log fd read: %s", strerror(errno));
    418   1.7  christos 	}
    419  1.27  christos 	if ((r = sshbuf_get_u32(logmsg, &len)) != 0)
    420  1.36  christos 		fatal_fr(r, "parse len");
    421   1.7  christos 	if (len <= 4 || len > 8192)
    422  1.36  christos 		fatal_f("invalid log message length %u", len);
    423   1.7  christos 
    424   1.7  christos 	/* Read severity, message */
    425  1.27  christos 	sshbuf_reset(logmsg);
    426  1.27  christos 	if ((r = sshbuf_reserve(logmsg, len, &p)) != 0)
    427  1.36  christos 		fatal_fr(r, "reserve msg");
    428  1.27  christos 	if (atomicio(read, pmonitor->m_log_recvfd, p, len) != len)
    429  1.36  christos 		fatal_f("log fd read: %s", strerror(errno));
    430  1.37  christos 	if ((r = sshbuf_get_u32(logmsg, &level)) != 0 ||
    431  1.37  christos 	    (r = sshbuf_get_u32(logmsg, &forced)) != 0 ||
    432  1.27  christos 	    (r = sshbuf_get_cstring(logmsg, &msg, NULL)) != 0)
    433  1.36  christos 		fatal_fr(r, "parse");
    434   1.7  christos 
    435   1.7  christos 	/* Log it */
    436   1.7  christos 	if (log_level_name(level) == NULL)
    437  1.36  christos 		fatal_f("invalid log level %u (corrupted message?)", level);
    438  1.37  christos 	sshlogdirect(level, forced, "%s [preauth]", msg);
    439   1.7  christos 
    440  1.27  christos 	sshbuf_free(logmsg);
    441  1.11  christos 	free(msg);
    442   1.7  christos 
    443   1.7  christos 	return 0;
    444   1.7  christos }
    445   1.7  christos 
    446  1.29  christos static int
    447  1.29  christos monitor_read(struct ssh *ssh, struct monitor *pmonitor, struct mon_table *ent,
    448   1.1  christos     struct mon_table **pent)
    449   1.1  christos {
    450  1.27  christos 	struct sshbuf *m;
    451  1.27  christos 	int r, ret;
    452   1.1  christos 	u_char type;
    453   1.7  christos 	struct pollfd pfd[2];
    454   1.7  christos 
    455   1.7  christos 	for (;;) {
    456  1.13  christos 		memset(&pfd, 0, sizeof(pfd));
    457   1.7  christos 		pfd[0].fd = pmonitor->m_sendfd;
    458   1.7  christos 		pfd[0].events = POLLIN;
    459   1.7  christos 		pfd[1].fd = pmonitor->m_log_recvfd;
    460   1.7  christos 		pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
    461   1.7  christos 		if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
    462   1.7  christos 			if (errno == EINTR || errno == EAGAIN)
    463   1.7  christos 				continue;
    464  1.36  christos 			fatal_f("poll: %s", strerror(errno));
    465   1.7  christos 		}
    466   1.7  christos 		if (pfd[1].revents) {
    467   1.7  christos 			/*
    468   1.7  christos 			 * Drain all log messages before processing next
    469   1.7  christos 			 * monitor request.
    470   1.7  christos 			 */
    471   1.7  christos 			monitor_read_log(pmonitor);
    472   1.7  christos 			continue;
    473   1.7  christos 		}
    474   1.7  christos 		if (pfd[0].revents)
    475   1.7  christos 			break;  /* Continues below */
    476   1.7  christos 	}
    477   1.1  christos 
    478  1.27  christos 	if ((m = sshbuf_new()) == NULL)
    479  1.36  christos 		fatal_f("sshbuf_new");
    480   1.1  christos 
    481  1.27  christos 	mm_request_receive(pmonitor->m_sendfd, m);
    482  1.27  christos 	if ((r = sshbuf_get_u8(m, &type)) != 0)
    483  1.36  christos 		fatal_fr(r, "parse type");
    484   1.1  christos 
    485  1.36  christos 	debug3_f("checking request %d", type);
    486   1.1  christos 
    487   1.1  christos 	while (ent->f != NULL) {
    488   1.1  christos 		if (ent->type == type)
    489   1.1  christos 			break;
    490   1.1  christos 		ent++;
    491   1.1  christos 	}
    492   1.1  christos 
    493   1.1  christos 	if (ent->f != NULL) {
    494   1.1  christos 		if (!(ent->flags & MON_PERMIT))
    495  1.36  christos 			fatal_f("unpermitted request %d", type);
    496  1.29  christos 		ret = (*ent->f)(ssh, pmonitor->m_sendfd, m);
    497  1.27  christos 		sshbuf_free(m);
    498   1.1  christos 
    499   1.1  christos 		/* The child may use this request only once, disable it */
    500   1.1  christos 		if (ent->flags & MON_ONCE) {
    501  1.36  christos 			debug2_f("%d used once, disabling now", type);
    502   1.1  christos 			ent->flags &= ~MON_PERMIT;
    503   1.1  christos 		}
    504   1.1  christos 
    505   1.1  christos 		if (pent != NULL)
    506   1.1  christos 			*pent = ent;
    507   1.1  christos 
    508   1.1  christos 		return ret;
    509   1.1  christos 	}
    510   1.1  christos 
    511  1.36  christos 	fatal_f("unsupported request: %d", type);
    512   1.1  christos 
    513   1.1  christos 	/* NOTREACHED */
    514   1.1  christos 	return (-1);
    515   1.1  christos }
    516   1.1  christos 
    517   1.1  christos /* allowed key state */
    518   1.1  christos static int
    519  1.33  christos monitor_allowed_key(const u_char *blob, u_int bloblen)
    520   1.1  christos {
    521   1.1  christos 	/* make sure key is allowed */
    522   1.1  christos 	if (key_blob == NULL || key_bloblen != bloblen ||
    523   1.4      adam 	    timingsafe_bcmp(key_blob, blob, key_bloblen))
    524   1.1  christos 		return (0);
    525   1.1  christos 	return (1);
    526   1.1  christos }
    527   1.1  christos 
    528   1.1  christos static void
    529   1.1  christos monitor_reset_key_state(void)
    530   1.1  christos {
    531   1.1  christos 	/* reset state */
    532  1.11  christos 	free(key_blob);
    533  1.11  christos 	free(hostbased_cuser);
    534  1.11  christos 	free(hostbased_chost);
    535  1.25  christos 	sshauthopt_free(key_opts);
    536   1.1  christos 	key_blob = NULL;
    537   1.1  christos 	key_bloblen = 0;
    538   1.1  christos 	key_blobtype = MM_NOKEY;
    539  1.25  christos 	key_opts = NULL;
    540   1.1  christos 	hostbased_cuser = NULL;
    541   1.1  christos 	hostbased_chost = NULL;
    542   1.1  christos }
    543   1.1  christos 
    544  1.13  christos #ifdef WITH_OPENSSL
    545   1.1  christos int
    546  1.29  christos mm_answer_moduli(struct ssh *ssh, int sock, struct sshbuf *m)
    547   1.1  christos {
    548   1.1  christos 	DH *dh;
    549  1.29  christos 	const BIGNUM *dh_p, *dh_g;
    550  1.27  christos 	int r;
    551  1.27  christos 	u_int min, want, max;
    552   1.1  christos 
    553  1.27  christos 	if ((r = sshbuf_get_u32(m, &min)) != 0 ||
    554  1.27  christos 	    (r = sshbuf_get_u32(m, &want)) != 0 ||
    555  1.27  christos 	    (r = sshbuf_get_u32(m, &max)) != 0)
    556  1.36  christos 		fatal_fr(r, "parse");
    557   1.1  christos 
    558  1.36  christos 	debug3_f("got parameters: %d %d %d", min, want, max);
    559   1.1  christos 	/* We need to check here, too, in case the child got corrupted */
    560   1.1  christos 	if (max < min || want < min || max < want)
    561  1.36  christos 		fatal_f("bad parameters: %d %d %d", min, want, max);
    562   1.1  christos 
    563  1.27  christos 	sshbuf_reset(m);
    564   1.1  christos 
    565   1.1  christos 	dh = choose_dh(min, want, max);
    566   1.1  christos 	if (dh == NULL) {
    567  1.27  christos 		if ((r = sshbuf_put_u8(m, 0)) != 0)
    568  1.36  christos 			fatal_fr(r, "assemble empty");
    569   1.1  christos 		return (0);
    570   1.1  christos 	} else {
    571   1.1  christos 		/* Send first bignum */
    572  1.29  christos 		DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
    573  1.27  christos 		if ((r = sshbuf_put_u8(m, 1)) != 0 ||
    574  1.29  christos 		    (r = sshbuf_put_bignum2(m, dh_p)) != 0 ||
    575  1.29  christos 		    (r = sshbuf_put_bignum2(m, dh_g)) != 0)
    576  1.36  christos 			fatal_fr(r, "assemble");
    577   1.1  christos 
    578   1.1  christos 		DH_free(dh);
    579   1.1  christos 	}
    580   1.1  christos 	mm_request_send(sock, MONITOR_ANS_MODULI, m);
    581   1.1  christos 	return (0);
    582   1.1  christos }
    583  1.13  christos #endif
    584   1.1  christos 
    585   1.1  christos int
    586  1.29  christos mm_answer_sign(struct ssh *ssh, int sock, struct sshbuf *m)
    587   1.1  christos {
    588  1.14  christos 	extern int auth_sock;			/* XXX move to state struct? */
    589  1.14  christos 	struct sshkey *key;
    590  1.18  christos 	struct sshbuf *sigbuf = NULL;
    591  1.18  christos 	u_char *p = NULL, *signature = NULL;
    592  1.18  christos 	char *alg = NULL;
    593  1.18  christos 	size_t datlen, siglen, alglen;
    594  1.18  christos 	int r, is_proof = 0;
    595  1.27  christos 	u_int keyid, compat;
    596  1.14  christos 	const char proof_req[] = "hostkeys-prove-00 (at) openssh.com";
    597   1.1  christos 
    598  1.36  christos 	debug3_f("entering");
    599   1.1  christos 
    600  1.14  christos 	if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
    601  1.18  christos 	    (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
    602  1.27  christos 	    (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0 ||
    603  1.27  christos 	    (r = sshbuf_get_u32(m, &compat)) != 0)
    604  1.36  christos 		fatal_fr(r, "parse");
    605  1.19  christos 	if (keyid > INT_MAX)
    606  1.36  christos 		fatal_f("invalid key ID");
    607   1.1  christos 
    608   1.1  christos 	/*
    609   1.6  christos 	 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
    610   1.6  christos 	 * SHA384 (48 bytes) and SHA512 (64 bytes).
    611  1.14  christos 	 *
    612  1.14  christos 	 * Otherwise, verify the signature request is for a hostkey
    613  1.14  christos 	 * proof.
    614  1.14  christos 	 *
    615  1.14  christos 	 * XXX perform similar check for KEX signature requests too?
    616  1.14  christos 	 * it's not trivial, since what is signed is the hash, rather
    617  1.14  christos 	 * than the full kex structure...
    618   1.1  christos 	 */
    619  1.14  christos 	if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
    620  1.14  christos 		/*
    621  1.14  christos 		 * Construct expected hostkey proof and compare it to what
    622  1.14  christos 		 * the client sent us.
    623  1.14  christos 		 */
    624  1.14  christos 		if (session_id2_len == 0) /* hostkeys is never first */
    625  1.36  christos 			fatal_f("bad data length: %zu", datlen);
    626  1.14  christos 		if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
    627  1.36  christos 			fatal_f("no hostkey for index %d", keyid);
    628  1.14  christos 		if ((sigbuf = sshbuf_new()) == NULL)
    629  1.36  christos 			fatal_f("sshbuf_new");
    630  1.14  christos 		if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
    631  1.14  christos 		    (r = sshbuf_put_string(sigbuf, session_id2,
    632  1.18  christos 		    session_id2_len)) != 0 ||
    633  1.14  christos 		    (r = sshkey_puts(key, sigbuf)) != 0)
    634  1.36  christos 			fatal_fr(r, "assemble private key proof");
    635  1.14  christos 		if (datlen != sshbuf_len(sigbuf) ||
    636  1.14  christos 		    memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
    637  1.36  christos 			fatal_f("bad data length: %zu, hostkey proof len %zu",
    638  1.36  christos 			    datlen, sshbuf_len(sigbuf));
    639  1.14  christos 		sshbuf_free(sigbuf);
    640  1.14  christos 		is_proof = 1;
    641  1.14  christos 	}
    642   1.1  christos 
    643   1.1  christos 	/* save session id, it will be passed on the first call */
    644   1.1  christos 	if (session_id2_len == 0) {
    645   1.1  christos 		session_id2_len = datlen;
    646   1.1  christos 		session_id2 = xmalloc(session_id2_len);
    647   1.1  christos 		memcpy(session_id2, p, session_id2_len);
    648   1.1  christos 	}
    649   1.1  christos 
    650  1.11  christos 	if ((key = get_hostkey_by_index(keyid)) != NULL) {
    651  1.18  christos 		if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
    652  1.35  christos 		    options.sk_provider, NULL, compat)) != 0)
    653  1.36  christos 			fatal_fr(r, "sign");
    654  1.14  christos 	} else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
    655  1.14  christos 	    auth_sock > 0) {
    656  1.14  christos 		if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
    657  1.36  christos 		    p, datlen, alg, compat)) != 0)
    658  1.36  christos 			fatal_fr(r, "agent sign");
    659  1.11  christos 	} else
    660  1.36  christos 		fatal_f("no hostkey from index %d", keyid);
    661   1.1  christos 
    662  1.38  christos 	debug3_f("%s %s signature len=%zu", alg,
    663  1.38  christos 	    is_proof ? "hostkey proof" : "KEX", siglen);
    664   1.1  christos 
    665  1.14  christos 	sshbuf_reset(m);
    666  1.14  christos 	if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
    667  1.36  christos 		fatal_fr(r, "assemble");
    668   1.1  christos 
    669  1.18  christos 	free(alg);
    670  1.11  christos 	free(p);
    671  1.11  christos 	free(signature);
    672   1.1  christos 
    673   1.1  christos 	mm_request_send(sock, MONITOR_ANS_SIGN, m);
    674   1.1  christos 
    675   1.1  christos 	/* Turn on permissions for getpwnam */
    676   1.1  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
    677   1.1  christos 
    678   1.1  christos 	return (0);
    679   1.1  christos }
    680   1.1  christos 
    681  1.36  christos #define PUTPW(b, id) \
    682  1.36  christos 	do { \
    683  1.36  christos 		if ((r = sshbuf_put_string(b, \
    684  1.36  christos 		    &pwent->id, sizeof(pwent->id))) != 0) \
    685  1.36  christos 			fatal_fr(r, "assemble %s", #id); \
    686  1.36  christos 	} while (0)
    687  1.36  christos 
    688   1.1  christos /* Retrieves the password entry and also checks if the user is permitted */
    689   1.1  christos int
    690  1.29  christos mm_answer_pwnamallow(struct ssh *ssh, int sock, struct sshbuf *m)
    691   1.1  christos {
    692   1.1  christos 	struct passwd *pwent;
    693  1.27  christos 	int r, allowed = 0;
    694   1.7  christos 	u_int i;
    695  1.29  christos 	Authctxt *authctxt = ssh->authctxt;
    696   1.1  christos 
    697  1.36  christos 	debug3_f("entering");
    698   1.1  christos 
    699   1.1  christos 	if (authctxt->attempt++ != 0)
    700  1.36  christos 		fatal_f("multiple attempts for getpwnam");
    701   1.1  christos 
    702  1.40  christos 	if ((r = sshbuf_get_cstring(m, &authctxt->user, NULL)) != 0)
    703  1.36  christos 		fatal_fr(r, "parse");
    704   1.1  christos 
    705  1.40  christos 	pwent = getpwnamallow(ssh, authctxt->user);
    706   1.1  christos 
    707  1.40  christos 	setproctitle("%s [priv]", pwent ? authctxt->user : "unknown");
    708   1.1  christos 
    709  1.27  christos 	sshbuf_reset(m);
    710   1.1  christos 
    711   1.1  christos 	if (pwent == NULL) {
    712  1.27  christos 		if ((r = sshbuf_put_u8(m, 0)) != 0)
    713  1.36  christos 			fatal_fr(r, "assemble fakepw");
    714   1.1  christos 		authctxt->pw = fakepw();
    715   1.1  christos 		goto out;
    716   1.1  christos 	}
    717   1.1  christos 
    718   1.1  christos 	allowed = 1;
    719   1.1  christos 	authctxt->pw = pwent;
    720   1.1  christos 	authctxt->valid = 1;
    721   1.1  christos 
    722  1.36  christos 	/* XXX send fake class/dir/shell, etc. */
    723  1.36  christos 	if ((r = sshbuf_put_u8(m, 1)) != 0)
    724  1.36  christos 		fatal_fr(r, "assemble ok");
    725  1.36  christos 	PUTPW(m, pw_uid);
    726  1.36  christos 	PUTPW(m, pw_gid);
    727  1.36  christos 	PUTPW(m, pw_change);
    728  1.36  christos 	PUTPW(m, pw_expire);
    729  1.36  christos 	if ((r = sshbuf_put_cstring(m, pwent->pw_name)) != 0 ||
    730  1.27  christos 	    (r = sshbuf_put_cstring(m, "*")) != 0 ||
    731  1.27  christos 	    (r = sshbuf_put_cstring(m, pwent->pw_gecos)) != 0 ||
    732  1.27  christos 	    (r = sshbuf_put_cstring(m, pwent->pw_class)) != 0 ||
    733  1.27  christos 	    (r = sshbuf_put_cstring(m, pwent->pw_dir)) != 0 ||
    734  1.27  christos 	    (r = sshbuf_put_cstring(m, pwent->pw_shell)) != 0)
    735  1.36  christos 		fatal_fr(r, "assemble pw");
    736   1.1  christos 
    737   1.1  christos  out:
    738  1.22  christos 	ssh_packet_set_log_preamble(ssh, "%suser %s",
    739  1.22  christos 	    authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
    740  1.27  christos 	if ((r = sshbuf_put_string(m, &options, sizeof(options))) != 0)
    741  1.36  christos 		fatal_fr(r, "assemble options");
    742   1.7  christos 
    743   1.7  christos #define M_CP_STROPT(x) do { \
    744  1.36  christos 		if (options.x != NULL && \
    745  1.36  christos 		    (r = sshbuf_put_cstring(m, options.x)) != 0) \
    746  1.36  christos 			fatal_fr(r, "assemble %s", #x); \
    747   1.7  christos 	} while (0)
    748   1.7  christos #define M_CP_STRARRAYOPT(x, nx) do { \
    749  1.27  christos 		for (i = 0; i < options.nx; i++) { \
    750  1.27  christos 			if ((r = sshbuf_put_cstring(m, options.x[i])) != 0) \
    751  1.36  christos 				fatal_fr(r, "assemble %s", #x); \
    752  1.27  christos 		} \
    753   1.7  christos 	} while (0)
    754   1.7  christos 	/* See comment in servconf.h */
    755   1.7  christos 	COPY_MATCH_STRING_OPTS();
    756   1.7  christos #undef M_CP_STROPT
    757   1.7  christos #undef M_CP_STRARRAYOPT
    758  1.10  christos 
    759  1.10  christos 	/* Create valid auth method lists */
    760  1.20  christos 	if (auth2_setup_methods_lists(authctxt) != 0) {
    761  1.10  christos 		/*
    762  1.10  christos 		 * The monitor will continue long enough to let the child
    763  1.39  christos 		 * run to its packet_disconnect(), but it must not allow any
    764  1.10  christos 		 * authentication to succeed.
    765  1.10  christos 		 */
    766  1.36  christos 		debug_f("no valid authentication method lists");
    767  1.10  christos 	}
    768  1.10  christos 
    769  1.36  christos 	debug3_f("sending MONITOR_ANS_PWNAM: %d", allowed);
    770   1.1  christos 	mm_request_send(sock, MONITOR_ANS_PWNAM, m);
    771   1.1  christos 
    772  1.20  christos 	/* Allow service/style information on the auth context */
    773  1.20  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
    774  1.20  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
    775   1.1  christos 
    776   1.2  christos #ifdef USE_PAM
    777   1.2  christos 	if (options.use_pam)
    778   1.2  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
    779   1.2  christos #endif
    780   1.1  christos 
    781   1.1  christos 	return (0);
    782   1.1  christos }
    783   1.1  christos 
    784  1.29  christos int mm_answer_auth2_read_banner(struct ssh *ssh, int sock, struct sshbuf *m)
    785   1.1  christos {
    786   1.1  christos 	char *banner;
    787  1.27  christos 	int r;
    788   1.1  christos 
    789  1.27  christos 	sshbuf_reset(m);
    790   1.1  christos 	banner = auth2_read_banner();
    791  1.27  christos 	if ((r = sshbuf_put_cstring(m, banner != NULL ? banner : "")) != 0)
    792  1.36  christos 		fatal_fr(r, "assemble");
    793   1.1  christos 	mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
    794  1.11  christos 	free(banner);
    795   1.1  christos 
    796   1.1  christos 	return (0);
    797   1.1  christos }
    798   1.1  christos 
    799   1.1  christos int
    800  1.29  christos mm_answer_authserv(struct ssh *ssh, int sock, struct sshbuf *m)
    801   1.1  christos {
    802  1.27  christos 	int r;
    803  1.29  christos 	Authctxt *authctxt = ssh->authctxt;
    804  1.27  christos 
    805   1.1  christos 	monitor_permit_authentications(1);
    806   1.1  christos 
    807  1.27  christos 	if ((r = sshbuf_get_cstring(m, &authctxt->service, NULL)) != 0 ||
    808  1.27  christos 	    (r = sshbuf_get_cstring(m, &authctxt->style, NULL)) != 0)
    809  1.36  christos 		fatal_fr(r, "parse");
    810  1.36  christos 	debug3_f("service=%s, style=%s", authctxt->service, authctxt->style);
    811   1.1  christos 
    812   1.1  christos 	if (strlen(authctxt->style) == 0) {
    813  1.11  christos 		free(authctxt->style);
    814   1.1  christos 		authctxt->style = NULL;
    815   1.1  christos 	}
    816   1.1  christos 
    817   1.1  christos 	return (0);
    818   1.1  christos }
    819   1.1  christos 
    820   1.1  christos int
    821  1.29  christos mm_answer_authpassword(struct ssh *ssh, int sock, struct sshbuf *m)
    822   1.1  christos {
    823   1.1  christos 	static int call_count;
    824   1.1  christos 	char *passwd;
    825  1.27  christos 	int r, authenticated;
    826  1.27  christos 	size_t plen;
    827   1.1  christos 
    828  1.20  christos 	if (!options.password_authentication)
    829  1.36  christos 		fatal_f("password authentication not enabled");
    830  1.27  christos 	if ((r = sshbuf_get_cstring(m, &passwd, &plen)) != 0)
    831  1.36  christos 		fatal_fr(r, "parse");
    832   1.1  christos 	/* Only authenticate if the context is valid */
    833   1.1  christos 	authenticated = options.password_authentication &&
    834  1.25  christos 	    auth_password(ssh, passwd);
    835  1.34  christos 	freezero(passwd, plen);
    836   1.1  christos 
    837  1.27  christos 	sshbuf_reset(m);
    838  1.27  christos 	if ((r = sshbuf_put_u32(m, authenticated)) != 0)
    839  1.36  christos 		fatal_fr(r, "assemble");
    840   1.1  christos 
    841  1.36  christos 	debug3_f("sending result %d", authenticated);
    842   1.1  christos 	mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
    843   1.1  christos 
    844   1.1  christos 	call_count++;
    845   1.1  christos 	if (plen == 0 && call_count == 1)
    846   1.1  christos 		auth_method = "none";
    847   1.1  christos 	else
    848   1.1  christos 		auth_method = "password";
    849   1.1  christos 
    850   1.1  christos 	/* Causes monitor loop to terminate if authenticated */
    851   1.1  christos 	return (authenticated);
    852   1.1  christos }
    853   1.1  christos 
    854   1.2  christos #ifdef BSD_AUTH
    855   1.1  christos int
    856  1.29  christos mm_answer_bsdauthquery(struct ssh *ssh, int sock, struct sshbuf *m)
    857   1.1  christos {
    858   1.1  christos 	char *name, *infotxt;
    859  1.27  christos 	u_int numprompts, *echo_on, success;
    860   1.1  christos 	char **prompts;
    861  1.27  christos 	int r;
    862   1.1  christos 
    863  1.20  christos 	if (!options.kbd_interactive_authentication)
    864  1.36  christos 		fatal_f("kbd-int authentication not enabled");
    865   1.1  christos 	success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
    866   1.1  christos 	    &prompts, &echo_on) < 0 ? 0 : 1;
    867   1.1  christos 
    868  1.27  christos 	sshbuf_reset(m);
    869  1.27  christos 	if ((r = sshbuf_put_u32(m, success)) != 0)
    870  1.36  christos 		fatal_fr(r, "assemble");
    871  1.27  christos 	if (success) {
    872  1.27  christos 		if ((r = sshbuf_put_cstring(m, prompts[0])) != 0)
    873  1.36  christos 			fatal_fr(r, "assemble prompt");
    874  1.27  christos 	}
    875   1.1  christos 
    876  1.36  christos 	debug3_f("sending challenge success: %u", success);
    877   1.1  christos 	mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
    878   1.1  christos 
    879   1.1  christos 	if (success) {
    880  1.11  christos 		free(name);
    881  1.11  christos 		free(infotxt);
    882  1.11  christos 		free(prompts);
    883  1.11  christos 		free(echo_on);
    884   1.1  christos 	}
    885   1.1  christos 
    886   1.1  christos 	return (0);
    887   1.1  christos }
    888   1.1  christos 
    889   1.1  christos int
    890  1.29  christos mm_answer_bsdauthrespond(struct ssh *ssh, int sock, struct sshbuf *m)
    891   1.1  christos {
    892   1.1  christos 	char *response;
    893  1.27  christos 	int r, authok;
    894   1.1  christos 
    895  1.20  christos 	if (!options.kbd_interactive_authentication)
    896  1.36  christos 		fatal_f("kbd-int authentication not enabled");
    897  1.18  christos 	if (authctxt->as == NULL)
    898  1.36  christos 		fatal_f("no bsd auth session");
    899   1.1  christos 
    900  1.27  christos 	if ((r = sshbuf_get_cstring(m, &response, NULL)) != 0)
    901  1.36  christos 		fatal_fr(r, "parse");
    902  1.38  christos 	authok = options.kbd_interactive_authentication &&
    903   1.1  christos 	    auth_userresponse(authctxt->as, response, 0);
    904   1.1  christos 	authctxt->as = NULL;
    905  1.36  christos 	debug3_f("<%s> = <%d>", response, authok);
    906  1.11  christos 	free(response);
    907   1.1  christos 
    908  1.27  christos 	sshbuf_reset(m);
    909  1.27  christos 	if ((r = sshbuf_put_u32(m, authok)) != 0)
    910  1.36  christos 		fatal_fr(r, "assemble");
    911   1.1  christos 
    912  1.36  christos 	debug3_f("sending authenticated: %d", authok);
    913   1.1  christos 	mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
    914   1.1  christos 
    915  1.20  christos 	auth_method = "keyboard-interactive";
    916  1.20  christos 	auth_submethod = "bsdauth";
    917   1.1  christos 
    918   1.1  christos 	return (authok != 0);
    919   1.1  christos }
    920   1.2  christos #endif
    921   1.2  christos 
    922   1.2  christos #ifdef SKEY
    923   1.2  christos int
    924  1.27  christos mm_answer_skeyquery(int sock, struct sshbuf *m)
    925   1.2  christos {
    926   1.2  christos 	struct skey skey;
    927   1.2  christos 	char challenge[1024];
    928   1.2  christos 	u_int success;
    929   1.2  christos 
    930   1.2  christos 	success = skeychallenge(&skey, authctxt->user, challenge,
    931   1.2  christos 	    sizeof(challenge)) < 0 ? 0 : 1;
    932   1.2  christos 
    933  1.27  christos 	sshbuf_reset(m);
    934  1.27  christos 	sshbuf_put_int(m, success);
    935   1.2  christos 	if (success)
    936  1.27  christos 		sshbuf_put_cstring(m, challenge);
    937   1.2  christos 
    938   1.2  christos 	debug3("%s: sending challenge success: %u", __func__, success);
    939   1.2  christos 	mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
    940   1.2  christos 
    941   1.2  christos 	return (0);
    942   1.2  christos }
    943   1.2  christos 
    944   1.2  christos int
    945  1.27  christos mm_answer_skeyrespond(int sock, struct sshbuf *m)
    946   1.2  christos {
    947   1.2  christos 	char *response;
    948   1.2  christos 	int authok;
    949  1.27  christos 	int r;
    950   1.2  christos 
    951  1.27  christos 	if ((r = sshbuf_get_cstring(m, &response, NULL)) != 0)
    952  1.27  christos 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
    953   1.2  christos 
    954  1.38  christos 	authok = (options.kbd_interactive_authentication &&
    955   1.2  christos 	    authctxt->valid &&
    956   1.2  christos 	    skey_haskey(authctxt->pw->pw_name) == 0 &&
    957   1.2  christos 	    skey_passcheck(authctxt->pw->pw_name, response) != -1);
    958   1.2  christos 
    959  1.11  christos 	free(response);
    960   1.2  christos 
    961  1.27  christos 	sshbuf_reset(m);
    962  1.27  christos 	sshbuf_put_int(m, authok);
    963   1.2  christos 
    964   1.2  christos 	debug3("%s: sending authenticated: %d", __func__, authok);
    965   1.2  christos 	mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
    966   1.2  christos 
    967   1.2  christos 	auth_method = "skey";
    968  1.20  christos 	auth_submethod = "bsdauth";
    969   1.2  christos 
    970   1.2  christos 	return (authok != 0);
    971   1.2  christos }
    972   1.2  christos #endif
    973   1.2  christos 
    974   1.2  christos #ifdef USE_PAM
    975   1.2  christos int
    976  1.29  christos mm_answer_pam_start(struct ssh *ssh, int sock, struct sshbuf *m)
    977   1.2  christos {
    978   1.2  christos 	if (!options.use_pam)
    979   1.2  christos 		fatal("UsePAM not set, but ended up in %s anyway", __func__);
    980   1.2  christos 
    981  1.29  christos 	start_pam(ssh);
    982   1.2  christos 
    983   1.2  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
    984   1.2  christos 
    985   1.2  christos 	return (0);
    986   1.2  christos }
    987   1.2  christos 
    988   1.2  christos int
    989  1.29  christos mm_answer_pam_account(struct ssh *ssh, int sock, struct sshbuf *m)
    990   1.2  christos {
    991   1.2  christos 	u_int ret;
    992   1.2  christos 
    993   1.2  christos 	if (!options.use_pam)
    994   1.2  christos 		fatal("UsePAM not set, but ended up in %s anyway", __func__);
    995   1.2  christos 
    996   1.2  christos 	ret = do_pam_account();
    997   1.2  christos 
    998  1.27  christos 	sshbuf_put_u32(m, ret);
    999  1.27  christos 	sshbuf_put_string(m, sshbuf_ptr(loginmsg), sshbuf_len(loginmsg));
   1000   1.2  christos 
   1001   1.2  christos 	mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
   1002   1.2  christos 
   1003   1.2  christos 	return (ret);
   1004   1.2  christos }
   1005   1.2  christos 
   1006   1.2  christos static void *sshpam_ctxt, *sshpam_authok;
   1007   1.2  christos extern KbdintDevice sshpam_device;
   1008   1.2  christos 
   1009   1.2  christos int
   1010  1.29  christos mm_answer_pam_init_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
   1011   1.2  christos {
   1012   1.2  christos 	debug3("%s", __func__);
   1013  1.29  christos 	sshpam_ctxt = (sshpam_device.init_ctx)(ssh->authctxt);
   1014   1.2  christos 	sshpam_authok = NULL;
   1015  1.27  christos 	sshbuf_reset(m);
   1016   1.2  christos 	if (sshpam_ctxt != NULL) {
   1017   1.2  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
   1018  1.27  christos 		sshbuf_put_u32(m, 1);
   1019   1.2  christos 	} else {
   1020  1.27  christos 		sshbuf_put_u32(m, 0);
   1021   1.2  christos 	}
   1022   1.2  christos 	mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
   1023   1.2  christos 	return (0);
   1024   1.2  christos }
   1025   1.2  christos 
   1026   1.2  christos int
   1027  1.29  christos mm_answer_pam_query(struct ssh *ssh, int sock, struct sshbuf *m)
   1028   1.2  christos {
   1029   1.2  christos 	char *name, *info, **prompts;
   1030   1.6  christos 	u_int i, num, *echo_on;
   1031   1.6  christos 	int ret;
   1032   1.2  christos 
   1033   1.2  christos 	debug3("%s", __func__);
   1034   1.2  christos 	sshpam_authok = NULL;
   1035   1.2  christos 	ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
   1036   1.2  christos 	if (ret == 0 && num == 0)
   1037   1.2  christos 		sshpam_authok = sshpam_ctxt;
   1038   1.2  christos 	if (num > 1 || name == NULL || info == NULL)
   1039   1.2  christos 		ret = -1;
   1040  1.27  christos 	sshbuf_reset(m);
   1041  1.27  christos 	sshbuf_put_u32(m, ret);
   1042  1.27  christos 	sshbuf_put_cstring(m, name);
   1043  1.11  christos 	free(name);
   1044  1.27  christos 	sshbuf_put_cstring(m, info);
   1045  1.11  christos 	free(info);
   1046  1.27  christos 	sshbuf_put_u32(m, num);
   1047   1.2  christos 	for (i = 0; i < num; ++i) {
   1048  1.27  christos 		sshbuf_put_cstring(m, prompts[i]);
   1049  1.11  christos 		free(prompts[i]);
   1050  1.27  christos 		sshbuf_put_u32(m, echo_on[i]);
   1051   1.2  christos 	}
   1052   1.2  christos 	if (prompts != NULL)
   1053  1.11  christos 		free(prompts);
   1054   1.2  christos 	if (echo_on != NULL)
   1055  1.11  christos 		free(echo_on);
   1056   1.2  christos 	auth_method = "keyboard-interactive/pam";
   1057   1.2  christos 	mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
   1058   1.2  christos 	return (0);
   1059   1.2  christos }
   1060   1.1  christos 
   1061   1.2  christos int
   1062  1.29  christos mm_answer_pam_respond(struct ssh *ssh, int sock, struct sshbuf *m)
   1063   1.2  christos {
   1064   1.2  christos 	char **resp;
   1065   1.6  christos 	u_int i, num;
   1066  1.27  christos 	int ret, r;
   1067   1.2  christos 
   1068   1.2  christos 	debug3("%s", __func__);
   1069   1.2  christos 	sshpam_authok = NULL;
   1070  1.27  christos 	if ((r = sshbuf_get_u32(m, &num)) != 0)
   1071  1.27  christos 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
   1072   1.2  christos 	if (num > 0) {
   1073   1.2  christos 		resp = xmalloc(num * sizeof(char *));
   1074   1.2  christos 		for (i = 0; i < num; ++i)
   1075  1.27  christos 			if ((r = sshbuf_get_cstring(m, &resp[i], NULL)) != 0)
   1076  1.27  christos 				fatal("%s: buffer error: %s", __func__,
   1077  1.27  christos 				    ssh_err(r));
   1078   1.2  christos 		ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
   1079   1.2  christos 		for (i = 0; i < num; ++i)
   1080  1.11  christos 			free(resp[i]);
   1081  1.11  christos 		free(resp);
   1082   1.2  christos 	} else {
   1083   1.2  christos 		ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
   1084   1.2  christos 	}
   1085  1.27  christos 	sshbuf_reset(m);
   1086  1.27  christos 	sshbuf_put_u32(m, ret);
   1087   1.2  christos 	mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
   1088   1.2  christos 	auth_method = "keyboard-interactive/pam";
   1089   1.2  christos 	if (ret == 0)
   1090   1.2  christos 		sshpam_authok = sshpam_ctxt;
   1091   1.2  christos 	return (0);
   1092   1.2  christos }
   1093   1.2  christos 
   1094   1.2  christos int
   1095  1.29  christos mm_answer_pam_free_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
   1096   1.2  christos {
   1097  1.16  christos 	int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
   1098   1.2  christos 
   1099   1.2  christos 	debug3("%s", __func__);
   1100   1.2  christos 	(sshpam_device.free_ctx)(sshpam_ctxt);
   1101  1.16  christos 	sshpam_ctxt = sshpam_authok = NULL;
   1102  1.27  christos 	sshbuf_reset(m);
   1103   1.2  christos 	mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
   1104   1.2  christos 	auth_method = "keyboard-interactive/pam";
   1105  1.16  christos 	return r;
   1106   1.2  christos }
   1107   1.2  christos #endif
   1108   1.1  christos 
   1109  1.29  christos /*
   1110  1.29  christos  * Check that the key type appears in the supplied pattern list, ignoring
   1111  1.29  christos  * mismatches in the signature algorithm. (Signature algorithm checks are
   1112  1.29  christos  * performed in the unprivileged authentication code).
   1113  1.29  christos  * Returns 1 on success, 0 otherwise.
   1114  1.29  christos  */
   1115  1.29  christos static int
   1116  1.29  christos key_base_type_match(const char *method, const struct sshkey *key,
   1117  1.29  christos     const char *list)
   1118  1.29  christos {
   1119  1.29  christos 	char *s, *l, *ol = xstrdup(list);
   1120  1.29  christos 	int found = 0;
   1121  1.29  christos 
   1122  1.29  christos 	l = ol;
   1123  1.29  christos 	for ((s = strsep(&l, ",")); s && *s != '\0'; (s = strsep(&l, ","))) {
   1124  1.29  christos 		if (sshkey_type_from_name(s) == key->type) {
   1125  1.29  christos 			found = 1;
   1126  1.29  christos 			break;
   1127  1.29  christos 		}
   1128  1.29  christos 	}
   1129  1.29  christos 	if (!found) {
   1130  1.29  christos 		error("%s key type %s is not in permitted list %s", method,
   1131  1.29  christos 		    sshkey_ssh_name(key), list);
   1132  1.29  christos 	}
   1133  1.29  christos 
   1134  1.29  christos 	free(ol);
   1135  1.29  christos 	return found;
   1136  1.29  christos }
   1137  1.29  christos 
   1138   1.1  christos int
   1139  1.29  christos mm_answer_keyallowed(struct ssh *ssh, int sock, struct sshbuf *m)
   1140   1.1  christos {
   1141  1.27  christos 	struct sshkey *key = NULL;
   1142   1.1  christos 	char *cuser, *chost;
   1143  1.27  christos 	u_int pubkey_auth_attempt;
   1144  1.37  christos 	u_int type = 0;
   1145  1.25  christos 	int r, allowed = 0;
   1146  1.25  christos 	struct sshauthopt *opts = NULL;
   1147  1.29  christos 	Authctxt *authctxt = ssh->authctxt;
   1148   1.1  christos 
   1149  1.36  christos 	debug3_f("entering");
   1150  1.27  christos 	if ((r = sshbuf_get_u32(m, &type)) != 0 ||
   1151  1.27  christos 	    (r = sshbuf_get_cstring(m, &cuser, NULL)) != 0 ||
   1152  1.27  christos 	    (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
   1153  1.27  christos 	    (r = sshkey_froms(m, &key)) != 0 ||
   1154  1.27  christos 	    (r = sshbuf_get_u32(m, &pubkey_auth_attempt)) != 0)
   1155  1.36  christos 		fatal_fr(r, "parse");
   1156   1.1  christos 
   1157   1.1  christos 	if (key != NULL && authctxt->valid) {
   1158  1.14  christos 		/* These should not make it past the privsep child */
   1159  1.27  christos 		if (sshkey_type_plain(key->type) == KEY_RSA &&
   1160  1.36  christos 		    (ssh->compat & SSH_BUG_RSASIGMD5) != 0)
   1161  1.36  christos 			fatal_f("passed a SSH_BUG_RSASIGMD5 key");
   1162  1.14  christos 
   1163   1.1  christos 		switch (type) {
   1164   1.1  christos 		case MM_USERKEY:
   1165   1.1  christos 			auth_method = "publickey";
   1166  1.25  christos 			if (!options.pubkey_authentication)
   1167  1.25  christos 				break;
   1168  1.25  christos 			if (auth2_key_already_used(authctxt, key))
   1169  1.25  christos 				break;
   1170  1.29  christos 			if (!key_base_type_match(auth_method, key,
   1171  1.36  christos 			    options.pubkey_accepted_algos))
   1172  1.25  christos 				break;
   1173  1.25  christos 			allowed = user_key_allowed(ssh, authctxt->pw, key,
   1174  1.25  christos 			    pubkey_auth_attempt, &opts);
   1175   1.1  christos 			break;
   1176   1.1  christos 		case MM_HOSTKEY:
   1177  1.25  christos 			auth_method = "hostbased";
   1178  1.25  christos 			if (!options.hostbased_authentication)
   1179  1.25  christos 				break;
   1180  1.25  christos 			if (auth2_key_already_used(authctxt, key))
   1181  1.25  christos 				break;
   1182  1.29  christos 			if (!key_base_type_match(auth_method, key,
   1183  1.36  christos 			    options.hostbased_accepted_algos))
   1184  1.25  christos 				break;
   1185  1.29  christos 			allowed = hostbased_key_allowed(ssh, authctxt->pw,
   1186   1.1  christos 			    cuser, chost, key);
   1187  1.23  christos 			auth2_record_info(authctxt,
   1188  1.11  christos 			    "client user \"%.100s\", client host \"%.100s\"",
   1189  1.11  christos 			    cuser, chost);
   1190   1.1  christos 			break;
   1191   1.1  christos 		default:
   1192  1.37  christos 			fatal_f("unknown key type %u", type);
   1193   1.1  christos 			break;
   1194   1.1  christos 		}
   1195   1.1  christos 	}
   1196  1.19  christos 
   1197  1.36  christos 	debug3_f("%s authentication%s: %s key is %s", auth_method,
   1198  1.36  christos 	    pubkey_auth_attempt ? "" : " test",
   1199  1.25  christos 	    (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key),
   1200  1.25  christos 	    allowed ? "allowed" : "not allowed");
   1201  1.12       spz 
   1202  1.23  christos 	auth2_record_key(authctxt, 0, key);
   1203   1.1  christos 
   1204   1.1  christos 	/* clear temporarily storage (used by verify) */
   1205   1.1  christos 	monitor_reset_key_state();
   1206   1.1  christos 
   1207   1.1  christos 	if (allowed) {
   1208   1.1  christos 		/* Save temporarily for comparison in verify */
   1209  1.27  christos 		if ((r = sshkey_to_blob(key, &key_blob, &key_bloblen)) != 0)
   1210  1.36  christos 			fatal_fr(r, "sshkey_to_blob");
   1211   1.1  christos 		key_blobtype = type;
   1212  1.25  christos 		key_opts = opts;
   1213   1.1  christos 		hostbased_cuser = cuser;
   1214   1.1  christos 		hostbased_chost = chost;
   1215   1.1  christos 	} else {
   1216   1.1  christos 		/* Log failed attempt */
   1217  1.29  christos 		auth_log(ssh, 0, 0, auth_method, NULL);
   1218  1.31  christos 		pfilter_notify(1);
   1219  1.11  christos 		free(cuser);
   1220  1.11  christos 		free(chost);
   1221   1.1  christos 	}
   1222  1.27  christos 	sshkey_free(key);
   1223   1.1  christos 
   1224  1.27  christos 	sshbuf_reset(m);
   1225  1.27  christos 	if ((r = sshbuf_put_u32(m, allowed)) != 0)
   1226  1.36  christos 		fatal_fr(r, "assemble");
   1227  1.25  christos 	if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0)
   1228  1.36  christos 		fatal_fr(r, "sshauthopt_serialise");
   1229  1.25  christos 	mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
   1230   1.1  christos 
   1231  1.25  christos 	if (!allowed)
   1232  1.25  christos 		sshauthopt_free(opts);
   1233   1.1  christos 
   1234   1.1  christos 	return (0);
   1235   1.1  christos }
   1236   1.1  christos 
   1237   1.1  christos static int
   1238  1.33  christos monitor_valid_userblob(struct ssh *ssh, const u_char *data, u_int datalen)
   1239   1.1  christos {
   1240  1.27  christos 	struct sshbuf *b;
   1241  1.39  christos 	struct sshkey *hostkey = NULL;
   1242  1.27  christos 	const u_char *p;
   1243  1.19  christos 	char *userstyle, *cp;
   1244  1.27  christos 	size_t len;
   1245  1.27  christos 	u_char type;
   1246  1.39  christos 	int hostbound = 0, r, fail = 0;
   1247  1.29  christos 	Authctxt *authctxt = ssh->authctxt;
   1248   1.1  christos 
   1249  1.33  christos 	if ((b = sshbuf_from(data, datalen)) == NULL)
   1250  1.36  christos 		fatal_f("sshbuf_from");
   1251   1.1  christos 
   1252  1.36  christos 	if (ssh->compat & SSH_OLD_SESSIONID) {
   1253  1.27  christos 		p = sshbuf_ptr(b);
   1254  1.27  christos 		len = sshbuf_len(b);
   1255   1.1  christos 		if ((session_id2 == NULL) ||
   1256   1.1  christos 		    (len < session_id2_len) ||
   1257   1.4      adam 		    (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
   1258   1.1  christos 			fail++;
   1259  1.27  christos 		if ((r = sshbuf_consume(b, session_id2_len)) != 0)
   1260  1.36  christos 			fatal_fr(r, "consume");
   1261   1.1  christos 	} else {
   1262  1.27  christos 		if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
   1263  1.36  christos 			fatal_fr(r, "parse sessionid");
   1264   1.1  christos 		if ((session_id2 == NULL) ||
   1265   1.1  christos 		    (len != session_id2_len) ||
   1266   1.4      adam 		    (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
   1267   1.1  christos 			fail++;
   1268   1.1  christos 	}
   1269  1.27  christos 	if ((r = sshbuf_get_u8(b, &type)) != 0)
   1270  1.36  christos 		fatal_fr(r, "parse type");
   1271  1.27  christos 	if (type != SSH2_MSG_USERAUTH_REQUEST)
   1272   1.1  christos 		fail++;
   1273  1.27  christos 	if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
   1274  1.36  christos 		fatal_fr(r, "parse userstyle");
   1275  1.11  christos 	xasprintf(&userstyle, "%s%s%s", authctxt->user,
   1276  1.11  christos 	    authctxt->style ? ":" : "",
   1277  1.11  christos 	    authctxt->style ? authctxt->style : "");
   1278  1.19  christos 	if (strcmp(userstyle, cp) != 0) {
   1279  1.19  christos 		logit("wrong user name passed to monitor: "
   1280  1.19  christos 		    "expected %s != %.100s", userstyle, cp);
   1281   1.1  christos 		fail++;
   1282   1.1  christos 	}
   1283  1.11  christos 	free(userstyle);
   1284  1.19  christos 	free(cp);
   1285  1.27  christos 	if ((r = sshbuf_skip_string(b)) != 0 ||	/* service */
   1286  1.27  christos 	    (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
   1287  1.36  christos 		fatal_fr(r, "parse method");
   1288  1.39  christos 	if (strcmp("publickey", cp) != 0) {
   1289  1.39  christos 		if (strcmp("publickey-hostbound-v00 (at) openssh.com", cp) == 0)
   1290  1.39  christos 			hostbound = 1;
   1291  1.39  christos 		else
   1292  1.39  christos 			fail++;
   1293  1.39  christos 	}
   1294  1.25  christos 	free(cp);
   1295  1.27  christos 	if ((r = sshbuf_get_u8(b, &type)) != 0)
   1296  1.36  christos 		fatal_fr(r, "parse pktype");
   1297  1.27  christos 	if (type == 0)
   1298  1.25  christos 		fail++;
   1299  1.27  christos 	if ((r = sshbuf_skip_string(b)) != 0 ||	/* pkalg */
   1300  1.39  christos 	    (r = sshbuf_skip_string(b)) != 0 ||	/* pkblob */
   1301  1.39  christos 	    (hostbound && (r = sshkey_froms(b, &hostkey)) != 0))
   1302  1.36  christos 		fatal_fr(r, "parse pk");
   1303  1.27  christos 	if (sshbuf_len(b) != 0)
   1304   1.1  christos 		fail++;
   1305  1.27  christos 	sshbuf_free(b);
   1306  1.39  christos 	if (hostkey != NULL) {
   1307  1.39  christos 		/*
   1308  1.39  christos 		 * Ensure this is actually one of our hostkeys; unfortunately
   1309  1.39  christos 		 * can't check ssh->kex->initial_hostkey directly at this point
   1310  1.39  christos 		 * as packet state has not yet been exported to monitor.
   1311  1.39  christos 		 */
   1312  1.39  christos 		if (get_hostkey_index(hostkey, 1, ssh) == -1)
   1313  1.39  christos 			fatal_f("hostbound hostkey does not match");
   1314  1.39  christos 		sshkey_free(hostkey);
   1315  1.39  christos 	}
   1316   1.1  christos 	return (fail == 0);
   1317   1.1  christos }
   1318   1.1  christos 
   1319   1.1  christos static int
   1320  1.33  christos monitor_valid_hostbasedblob(struct ssh *ssh, const u_char *data, u_int datalen,
   1321  1.33  christos     const char *cuser, const char *chost)
   1322   1.1  christos {
   1323  1.27  christos 	struct sshbuf *b;
   1324  1.27  christos 	const u_char *p;
   1325  1.27  christos 	char *cp, *userstyle;
   1326  1.27  christos 	size_t len;
   1327  1.27  christos 	int r, fail = 0;
   1328  1.27  christos 	u_char type;
   1329  1.29  christos 	Authctxt *authctxt = ssh->authctxt;
   1330   1.1  christos 
   1331  1.33  christos 	if ((b = sshbuf_from(data, datalen)) == NULL)
   1332  1.36  christos 		fatal_f("sshbuf_new");
   1333  1.33  christos 	if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
   1334  1.36  christos 		fatal_fr(r, "parse sessionid");
   1335   1.1  christos 
   1336   1.1  christos 	if ((session_id2 == NULL) ||
   1337   1.1  christos 	    (len != session_id2_len) ||
   1338   1.4      adam 	    (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
   1339   1.1  christos 		fail++;
   1340   1.1  christos 
   1341  1.27  christos 	if ((r = sshbuf_get_u8(b, &type)) != 0)
   1342  1.36  christos 		fatal_fr(r, "parse type");
   1343  1.27  christos 	if (type != SSH2_MSG_USERAUTH_REQUEST)
   1344   1.1  christos 		fail++;
   1345  1.27  christos 	if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
   1346  1.36  christos 		fatal_fr(r, "parse userstyle");
   1347  1.11  christos 	xasprintf(&userstyle, "%s%s%s", authctxt->user,
   1348  1.11  christos 	    authctxt->style ? ":" : "",
   1349  1.11  christos 	    authctxt->style ? authctxt->style : "");
   1350  1.27  christos 	if (strcmp(userstyle, cp) != 0) {
   1351  1.27  christos 		logit("wrong user name passed to monitor: "
   1352  1.27  christos 		    "expected %s != %.100s", userstyle, cp);
   1353   1.1  christos 		fail++;
   1354   1.1  christos 	}
   1355  1.11  christos 	free(userstyle);
   1356  1.27  christos 	free(cp);
   1357  1.27  christos 	if ((r = sshbuf_skip_string(b)) != 0 ||	/* service */
   1358  1.27  christos 	    (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
   1359  1.36  christos 		fatal_fr(r, "parse method");
   1360  1.27  christos 	if (strcmp(cp, "hostbased") != 0)
   1361   1.1  christos 		fail++;
   1362  1.27  christos 	free(cp);
   1363  1.27  christos 	if ((r = sshbuf_skip_string(b)) != 0 ||	/* pkalg */
   1364  1.27  christos 	    (r = sshbuf_skip_string(b)) != 0)	/* pkblob */
   1365  1.36  christos 		fatal_fr(r, "parse pk");
   1366   1.1  christos 
   1367   1.1  christos 	/* verify client host, strip trailing dot if necessary */
   1368  1.27  christos 	if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
   1369  1.36  christos 		fatal_fr(r, "parse host");
   1370  1.27  christos 	if (((len = strlen(cp)) > 0) && cp[len - 1] == '.')
   1371  1.27  christos 		cp[len - 1] = '\0';
   1372  1.27  christos 	if (strcmp(cp, chost) != 0)
   1373   1.1  christos 		fail++;
   1374  1.27  christos 	free(cp);
   1375   1.1  christos 
   1376   1.1  christos 	/* verify client user */
   1377  1.27  christos 	if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
   1378  1.36  christos 		fatal_fr(r, "parse ruser");
   1379  1.27  christos 	if (strcmp(cp, cuser) != 0)
   1380   1.1  christos 		fail++;
   1381  1.27  christos 	free(cp);
   1382   1.1  christos 
   1383  1.27  christos 	if (sshbuf_len(b) != 0)
   1384   1.1  christos 		fail++;
   1385  1.27  christos 	sshbuf_free(b);
   1386   1.1  christos 	return (fail == 0);
   1387   1.1  christos }
   1388   1.1  christos 
   1389   1.1  christos int
   1390  1.29  christos mm_answer_keyverify(struct ssh *ssh, int sock, struct sshbuf *m)
   1391   1.1  christos {
   1392  1.23  christos 	struct sshkey *key;
   1393  1.33  christos 	const u_char *signature, *data, *blob;
   1394  1.33  christos 	char *sigalg = NULL, *fp = NULL;
   1395  1.23  christos 	size_t signaturelen, datalen, bloblen;
   1396  1.35  christos 	int r, ret, req_presence = 0, req_verify = 0, valid_data = 0;
   1397  1.35  christos 	int encoded_ret;
   1398  1.33  christos 	struct sshkey_sig_details *sig_details = NULL;
   1399  1.29  christos 	Authctxt *authctxt = ssh->authctxt;
   1400   1.1  christos 
   1401  1.33  christos 	if ((r = sshbuf_get_string_direct(m, &blob, &bloblen)) != 0 ||
   1402  1.33  christos 	    (r = sshbuf_get_string_direct(m, &signature, &signaturelen)) != 0 ||
   1403  1.33  christos 	    (r = sshbuf_get_string_direct(m, &data, &datalen)) != 0 ||
   1404  1.25  christos 	    (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
   1405  1.36  christos 		fatal_fr(r, "parse");
   1406   1.1  christos 
   1407   1.1  christos 	if (hostbased_cuser == NULL || hostbased_chost == NULL ||
   1408   1.1  christos 	  !monitor_allowed_key(blob, bloblen))
   1409  1.36  christos 		fatal_f("bad key, not previously allowed");
   1410   1.1  christos 
   1411  1.25  christos 	/* Empty signature algorithm means NULL. */
   1412  1.25  christos 	if (*sigalg == '\0') {
   1413  1.25  christos 		free(sigalg);
   1414  1.25  christos 		sigalg = NULL;
   1415  1.25  christos 	}
   1416  1.25  christos 
   1417  1.23  christos 	/* XXX use sshkey_froms here; need to change key_blob, etc. */
   1418  1.23  christos 	if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
   1419  1.36  christos 		fatal_fr(r, "parse key");
   1420   1.1  christos 
   1421   1.1  christos 	switch (key_blobtype) {
   1422   1.1  christos 	case MM_USERKEY:
   1423  1.29  christos 		valid_data = monitor_valid_userblob(ssh, data, datalen);
   1424  1.23  christos 		auth_method = "publickey";
   1425   1.1  christos 		break;
   1426   1.1  christos 	case MM_HOSTKEY:
   1427  1.29  christos 		valid_data = monitor_valid_hostbasedblob(ssh, data, datalen,
   1428   1.1  christos 		    hostbased_cuser, hostbased_chost);
   1429  1.23  christos 		auth_method = "hostbased";
   1430   1.1  christos 		break;
   1431   1.1  christos 	default:
   1432   1.1  christos 		valid_data = 0;
   1433   1.1  christos 		break;
   1434   1.1  christos 	}
   1435   1.1  christos 	if (!valid_data)
   1436  1.36  christos 		fatal_f("bad %s signature data blob",
   1437  1.36  christos 		    key_blobtype == MM_USERKEY ? "userkey" :
   1438  1.36  christos 		    (key_blobtype == MM_HOSTKEY ? "hostkey" : "unknown"));
   1439   1.1  christos 
   1440  1.33  christos 	if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
   1441  1.33  christos 	    SSH_FP_DEFAULT)) == NULL)
   1442  1.36  christos 		fatal_f("sshkey_fingerprint failed");
   1443  1.33  christos 
   1444  1.23  christos 	ret = sshkey_verify(key, signature, signaturelen, data, datalen,
   1445  1.33  christos 	    sigalg, ssh->compat, &sig_details);
   1446  1.39  christos 	debug3_f("%s %s signature using %s %s%s%s", auth_method,
   1447  1.39  christos 	    sshkey_type(key), sigalg == NULL ? "default" : sigalg,
   1448  1.33  christos 	    (ret == 0) ? "verified" : "unverified",
   1449  1.33  christos 	    (ret != 0) ? ": " : "", (ret != 0) ? ssh_err(ret) : "");
   1450  1.33  christos 
   1451  1.33  christos 	if (ret == 0 && key_blobtype == MM_USERKEY && sig_details != NULL) {
   1452  1.33  christos 		req_presence = (options.pubkey_auth_options &
   1453  1.33  christos 		    PUBKEYAUTH_TOUCH_REQUIRED) ||
   1454  1.33  christos 		    !key_opts->no_require_user_presence;
   1455  1.33  christos 		if (req_presence &&
   1456  1.33  christos 		    (sig_details->sk_flags & SSH_SK_USER_PRESENCE_REQD) == 0) {
   1457  1.33  christos 			error("public key %s %s signature for %s%s from %.128s "
   1458  1.33  christos 			    "port %d rejected: user presence "
   1459  1.33  christos 			    "(authenticator touch) requirement not met ",
   1460  1.33  christos 			    sshkey_type(key), fp,
   1461  1.33  christos 			    authctxt->valid ? "" : "invalid user ",
   1462  1.33  christos 			    authctxt->user, ssh_remote_ipaddr(ssh),
   1463  1.33  christos 			    ssh_remote_port(ssh));
   1464  1.33  christos 			ret = SSH_ERR_SIGNATURE_INVALID;
   1465  1.33  christos 		}
   1466  1.35  christos 		req_verify = (options.pubkey_auth_options &
   1467  1.35  christos 		    PUBKEYAUTH_VERIFY_REQUIRED) || key_opts->require_verify;
   1468  1.35  christos 		if (req_verify &&
   1469  1.35  christos 		    (sig_details->sk_flags & SSH_SK_USER_VERIFICATION_REQD) == 0) {
   1470  1.35  christos 			error("public key %s %s signature for %s%s from %.128s "
   1471  1.35  christos 			    "port %d rejected: user verification requirement "
   1472  1.35  christos 			    "not met ", sshkey_type(key), fp,
   1473  1.35  christos 			    authctxt->valid ? "" : "invalid user ",
   1474  1.35  christos 			    authctxt->user, ssh_remote_ipaddr(ssh),
   1475  1.35  christos 			    ssh_remote_port(ssh));
   1476  1.35  christos 			ret = SSH_ERR_SIGNATURE_INVALID;
   1477  1.35  christos 		}
   1478  1.33  christos 	}
   1479  1.23  christos 	auth2_record_key(authctxt, ret == 0, key);
   1480  1.14  christos 
   1481  1.25  christos 	if (key_blobtype == MM_USERKEY)
   1482  1.25  christos 		auth_activate_options(ssh, key_opts);
   1483  1.23  christos 	monitor_reset_key_state();
   1484   1.1  christos 
   1485  1.23  christos 	sshbuf_reset(m);
   1486   1.1  christos 
   1487  1.23  christos 	/* encode ret != 0 as positive integer, since we're sending u32 */
   1488  1.23  christos 	encoded_ret = (ret != 0);
   1489  1.33  christos 	if ((r = sshbuf_put_u32(m, encoded_ret)) != 0 ||
   1490  1.33  christos 	    (r = sshbuf_put_u8(m, sig_details != NULL)) != 0)
   1491  1.36  christos 		fatal_fr(r, "assemble");
   1492  1.33  christos 	if (sig_details != NULL) {
   1493  1.33  christos 		if ((r = sshbuf_put_u32(m, sig_details->sk_counter)) != 0 ||
   1494  1.33  christos 		    (r = sshbuf_put_u8(m, sig_details->sk_flags)) != 0)
   1495  1.36  christos 			fatal_fr(r, "assemble sk");
   1496  1.33  christos 	}
   1497  1.33  christos 	sshkey_sig_details_free(sig_details);
   1498   1.1  christos 	mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
   1499   1.1  christos 
   1500  1.33  christos 	free(sigalg);
   1501  1.33  christos 	free(fp);
   1502  1.33  christos 	sshkey_free(key);
   1503  1.33  christos 
   1504  1.23  christos 	return ret == 0;
   1505   1.1  christos }
   1506   1.1  christos 
   1507   1.1  christos static void
   1508  1.29  christos mm_record_login(struct ssh *ssh, Session *s, struct passwd *pw)
   1509   1.1  christos {
   1510   1.1  christos 	socklen_t fromlen;
   1511   1.1  christos 	struct sockaddr_storage from;
   1512   1.1  christos 
   1513   1.1  christos 	/*
   1514   1.1  christos 	 * Get IP address of client. If the connection is not a socket, let
   1515   1.1  christos 	 * the address be 0.0.0.0.
   1516   1.1  christos 	 */
   1517   1.1  christos 	memset(&from, 0, sizeof(from));
   1518   1.1  christos 	fromlen = sizeof(from);
   1519  1.29  christos 	if (ssh_packet_connection_is_on_socket(ssh)) {
   1520  1.29  christos 		if (getpeername(ssh_packet_get_connection_in(ssh),
   1521  1.30  christos 		    (struct sockaddr *)&from, &fromlen) == -1) {
   1522   1.1  christos 			debug("getpeername: %.100s", strerror(errno));
   1523  1.26  christos 			cleanup_exit(254);
   1524   1.1  christos 		}
   1525   1.1  christos 	}
   1526   1.1  christos 	/* Record that there was a login on that tty from the remote host. */
   1527   1.1  christos 	record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
   1528  1.19  christos 	    session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
   1529   1.1  christos 	    (struct sockaddr *)&from, fromlen);
   1530   1.1  christos }
   1531   1.1  christos 
   1532   1.1  christos static void
   1533   1.1  christos mm_session_close(Session *s)
   1534   1.1  christos {
   1535  1.36  christos 	debug3_f("session %d pid %ld", s->self, (long)s->pid);
   1536   1.1  christos 	if (s->ttyfd != -1) {
   1537  1.36  christos 		debug3_f("tty %s ptyfd %d", s->tty, s->ptyfd);
   1538   1.1  christos 		session_pty_cleanup2(s);
   1539   1.1  christos 	}
   1540   1.1  christos 	session_unused(s->self);
   1541   1.1  christos }
   1542   1.1  christos 
   1543   1.1  christos int
   1544  1.29  christos mm_answer_pty(struct ssh *ssh, int sock, struct sshbuf *m)
   1545   1.1  christos {
   1546   1.1  christos 	extern struct monitor *pmonitor;
   1547   1.1  christos 	Session *s;
   1548  1.27  christos 	int r, res, fd0;
   1549  1.29  christos 	Authctxt *authctxt = ssh->authctxt;
   1550   1.1  christos 
   1551  1.36  christos 	debug3_f("entering");
   1552   1.1  christos 
   1553  1.27  christos 	sshbuf_reset(m);
   1554   1.1  christos 	s = session_new();
   1555   1.1  christos 	if (s == NULL)
   1556   1.1  christos 		goto error;
   1557   1.1  christos 	s->authctxt = authctxt;
   1558   1.1  christos 	s->pw = authctxt->pw;
   1559   1.1  christos 	s->pid = pmonitor->m_pid;
   1560   1.1  christos 	res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
   1561   1.1  christos 	if (res == 0)
   1562   1.1  christos 		goto error;
   1563   1.1  christos 	pty_setowner(authctxt->pw, s->tty);
   1564   1.1  christos 
   1565  1.27  christos 	if ((r = sshbuf_put_u32(m, 1)) != 0 ||
   1566  1.27  christos 	    (r = sshbuf_put_cstring(m, s->tty)) != 0)
   1567  1.36  christos 		fatal_fr(r, "assemble");
   1568   1.1  christos 
   1569   1.1  christos 	/* We need to trick ttyslot */
   1570   1.1  christos 	if (dup2(s->ttyfd, 0) == -1)
   1571  1.36  christos 		fatal_f("dup2");
   1572   1.1  christos 
   1573  1.29  christos 	mm_record_login(ssh, s, authctxt->pw);
   1574   1.1  christos 
   1575   1.1  christos 	/* Now we can close the file descriptor again */
   1576   1.1  christos 	close(0);
   1577   1.1  christos 
   1578   1.1  christos 	/* send messages generated by record_login */
   1579  1.27  christos 	if ((r = sshbuf_put_stringb(m, loginmsg)) != 0)
   1580  1.36  christos 		fatal_fr(r, "assemble loginmsg");
   1581  1.27  christos 	sshbuf_reset(loginmsg);
   1582   1.1  christos 
   1583   1.1  christos 	mm_request_send(sock, MONITOR_ANS_PTY, m);
   1584   1.1  christos 
   1585   1.1  christos 	if (mm_send_fd(sock, s->ptyfd) == -1 ||
   1586   1.1  christos 	    mm_send_fd(sock, s->ttyfd) == -1)
   1587  1.36  christos 		fatal_f("send fds failed");
   1588   1.1  christos 
   1589   1.1  christos 	/* make sure nothing uses fd 0 */
   1590  1.30  christos 	if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) == -1)
   1591  1.36  christos 		fatal_f("open(/dev/null): %s", strerror(errno));
   1592   1.1  christos 	if (fd0 != 0)
   1593  1.36  christos 		error_f("fd0 %d != 0", fd0);
   1594   1.1  christos 
   1595  1.35  christos 	/* slave side of pty is not needed */
   1596   1.1  christos 	close(s->ttyfd);
   1597   1.1  christos 	s->ttyfd = s->ptyfd;
   1598   1.1  christos 	/* no need to dup() because nobody closes ptyfd */
   1599   1.1  christos 	s->ptymaster = s->ptyfd;
   1600   1.1  christos 
   1601  1.36  christos 	debug3_f("tty %s ptyfd %d", s->tty, s->ttyfd);
   1602   1.1  christos 
   1603   1.1  christos 	return (0);
   1604   1.1  christos 
   1605   1.1  christos  error:
   1606   1.1  christos 	if (s != NULL)
   1607   1.1  christos 		mm_session_close(s);
   1608  1.27  christos 	if ((r = sshbuf_put_u32(m, 0)) != 0)
   1609  1.36  christos 		fatal_fr(r, "assemble 0");
   1610   1.1  christos 	mm_request_send(sock, MONITOR_ANS_PTY, m);
   1611   1.1  christos 	return (0);
   1612   1.1  christos }
   1613   1.1  christos 
   1614   1.1  christos int
   1615  1.29  christos mm_answer_pty_cleanup(struct ssh *ssh, int sock, struct sshbuf *m)
   1616   1.1  christos {
   1617   1.1  christos 	Session *s;
   1618   1.1  christos 	char *tty;
   1619  1.27  christos 	int r;
   1620   1.1  christos 
   1621  1.36  christos 	debug3_f("entering");
   1622   1.1  christos 
   1623  1.27  christos 	if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
   1624  1.36  christos 		fatal_fr(r, "parse tty");
   1625   1.1  christos 	if ((s = session_by_tty(tty)) != NULL)
   1626   1.1  christos 		mm_session_close(s);
   1627  1.27  christos 	sshbuf_reset(m);
   1628  1.11  christos 	free(tty);
   1629   1.1  christos 	return (0);
   1630   1.1  christos }
   1631   1.1  christos 
   1632   1.2  christos #ifdef KRB5
   1633   1.2  christos int
   1634  1.29  christos mm_answer_krb5(struct ssh *ssh, int xsocket, struct sshbuf *m)
   1635   1.2  christos {
   1636   1.2  christos 	krb5_data tkt, reply;
   1637   1.2  christos 	char *client_user;
   1638  1.27  christos 	unsigned char *data;
   1639  1.27  christos 	size_t len;
   1640  1.27  christos 	int r;
   1641   1.2  christos 	int success;
   1642  1.29  christos 	Authctxt *authctxt = ssh->authctxt;
   1643   1.2  christos 
   1644   1.2  christos 	/* use temporary var to avoid size issues on 64bit arch */
   1645  1.27  christos 	if ((r = sshbuf_get_string(m, &data, &len)) != 0)
   1646  1.27  christos 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
   1647  1.27  christos 	tkt.data = data;
   1648   1.2  christos 	tkt.length = len;
   1649   1.2  christos 
   1650   1.2  christos 	success = options.kerberos_authentication &&
   1651   1.2  christos 	    authctxt->valid &&
   1652  1.29  christos 	    auth_krb5(ssh, &tkt, &client_user, &reply);
   1653   1.2  christos 
   1654   1.2  christos 	if (tkt.length)
   1655  1.11  christos 		free(tkt.data);
   1656   1.2  christos 
   1657  1.27  christos 	sshbuf_reset(m);
   1658  1.27  christos 	sshbuf_put_u32(m, success);
   1659   1.2  christos 
   1660   1.2  christos 	if (success) {
   1661  1.27  christos 		sshbuf_put_cstring(m, client_user);
   1662  1.27  christos 		sshbuf_put_string(m, reply.data, reply.length);
   1663   1.2  christos 		if (client_user)
   1664  1.11  christos 			free(client_user);
   1665   1.2  christos 		if (reply.length)
   1666  1.11  christos 			free(reply.data);
   1667   1.2  christos 	}
   1668   1.6  christos 	mm_request_send(xsocket, MONITOR_ANS_KRB5, m);
   1669   1.2  christos 
   1670   1.2  christos 	auth_method = "kerberos";
   1671   1.2  christos 
   1672   1.2  christos 	return success;
   1673   1.2  christos }
   1674   1.2  christos #endif
   1675   1.2  christos 
   1676   1.1  christos int
   1677  1.29  christos mm_answer_term(struct ssh *ssh, int sock, struct sshbuf *req)
   1678   1.1  christos {
   1679   1.1  christos 	extern struct monitor *pmonitor;
   1680   1.1  christos 	int res, status;
   1681   1.1  christos 
   1682  1.36  christos 	debug3_f("tearing down sessions");
   1683   1.1  christos 
   1684   1.1  christos 	/* The child is terminating */
   1685  1.23  christos 	session_destroy_all(ssh, &mm_session_close);
   1686   1.1  christos 
   1687   1.1  christos 	while (waitpid(pmonitor->m_pid, &status, 0) == -1)
   1688   1.1  christos 		if (errno != EINTR)
   1689   1.1  christos 			exit(1);
   1690   1.1  christos 
   1691   1.1  christos 	res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
   1692   1.1  christos 
   1693   1.1  christos 	/* Terminate process */
   1694   1.1  christos 	exit(res);
   1695   1.1  christos }
   1696   1.1  christos 
   1697   1.1  christos void
   1698  1.29  christos monitor_clear_keystate(struct ssh *ssh, struct monitor *pmonitor)
   1699  1.23  christos {
   1700  1.23  christos 	ssh_clear_newkeys(ssh, MODE_IN);
   1701  1.23  christos 	ssh_clear_newkeys(ssh, MODE_OUT);
   1702  1.23  christos 	sshbuf_free(child_state);
   1703  1.23  christos 	child_state = NULL;
   1704  1.23  christos }
   1705  1.23  christos 
   1706  1.23  christos void
   1707  1.29  christos monitor_apply_keystate(struct ssh *ssh, struct monitor *pmonitor)
   1708   1.1  christos {
   1709  1.14  christos 	struct kex *kex;
   1710  1.14  christos 	int r;
   1711  1.14  christos 
   1712  1.36  christos 	debug3_f("packet_set_state");
   1713  1.14  christos 	if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
   1714  1.36  christos 		fatal_fr(r, "packet_set_state");
   1715  1.14  christos 	sshbuf_free(child_state);
   1716  1.14  christos 	child_state = NULL;
   1717  1.36  christos 	if ((kex = ssh->kex) == NULL)
   1718  1.36  christos 		fatal_f("internal error: ssh->kex == NULL");
   1719  1.36  christos 	if (session_id2_len != sshbuf_len(ssh->kex->session_id)) {
   1720  1.36  christos 		fatal_f("incorrect session id length %zu (expected %u)",
   1721  1.36  christos 		    sshbuf_len(ssh->kex->session_id), session_id2_len);
   1722  1.36  christos 	}
   1723  1.36  christos 	if (memcmp(sshbuf_ptr(ssh->kex->session_id), session_id2,
   1724  1.36  christos 	    session_id2_len) != 0)
   1725  1.36  christos 		fatal_f("session ID mismatch");
   1726  1.36  christos 	/* XXX set callbacks */
   1727  1.15  christos #ifdef WITH_OPENSSL
   1728  1.36  christos 	kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_server;
   1729  1.36  christos 	kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_server;
   1730  1.36  christos 	kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_server;
   1731  1.36  christos 	kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_server;
   1732  1.36  christos 	kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_server;
   1733  1.36  christos 	kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
   1734  1.36  christos 	kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
   1735  1.36  christos 	kex->kex[KEX_ECDH_SHA2] = kex_gen_server;
   1736  1.36  christos #endif
   1737  1.36  christos 	kex->kex[KEX_C25519_SHA256] = kex_gen_server;
   1738  1.36  christos 	kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_server;
   1739  1.36  christos 	kex->load_host_public_key=&get_hostkey_public_by_type;
   1740  1.36  christos 	kex->load_host_private_key=&get_hostkey_private_by_type;
   1741  1.36  christos 	kex->host_key_index=&get_hostkey_index;
   1742  1.36  christos 	kex->sign = sshd_hostkey_sign;
   1743   1.1  christos }
   1744   1.1  christos 
   1745  1.34  christos /* This function requires careful sanity checking */
   1746   1.1  christos 
   1747   1.1  christos void
   1748  1.29  christos mm_get_keystate(struct ssh *ssh, struct monitor *pmonitor)
   1749   1.1  christos {
   1750  1.36  christos 	debug3_f("Waiting for new keys");
   1751   1.1  christos 
   1752  1.14  christos 	if ((child_state = sshbuf_new()) == NULL)
   1753  1.36  christos 		fatal_f("sshbuf_new failed");
   1754  1.14  christos 	mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
   1755  1.14  christos 	    child_state);
   1756  1.36  christos 	debug3_f("GOT new keys");
   1757   1.1  christos }
   1758   1.1  christos 
   1759   1.1  christos 
   1760   1.1  christos /* XXX */
   1761   1.1  christos 
   1762   1.1  christos #define FD_CLOSEONEXEC(x) do { \
   1763   1.7  christos 	if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
   1764   1.1  christos 		fatal("fcntl(%d, F_SETFD)", x); \
   1765   1.1  christos } while (0)
   1766   1.1  christos 
   1767   1.1  christos static void
   1768   1.7  christos monitor_openfds(struct monitor *mon, int do_logfds)
   1769   1.1  christos {
   1770   1.7  christos 	int pair[2];
   1771  1.23  christos #ifdef SO_ZEROIZE
   1772  1.23  christos 	int on = 1;
   1773  1.23  christos #endif
   1774   1.7  christos 
   1775   1.1  christos 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
   1776  1.36  christos 		fatal_f("socketpair: %s", strerror(errno));
   1777  1.23  christos #ifdef SO_ZEROIZE
   1778  1.30  christos 	if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) == -1)
   1779  1.23  christos 		error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
   1780  1.30  christos 	if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) == -1)
   1781  1.23  christos 		error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
   1782  1.23  christos #endif
   1783   1.1  christos 	FD_CLOSEONEXEC(pair[0]);
   1784   1.1  christos 	FD_CLOSEONEXEC(pair[1]);
   1785   1.7  christos 	mon->m_recvfd = pair[0];
   1786   1.7  christos 	mon->m_sendfd = pair[1];
   1787   1.7  christos 
   1788   1.7  christos 	if (do_logfds) {
   1789   1.7  christos 		if (pipe(pair) == -1)
   1790  1.36  christos 			fatal_f("pipe: %s", strerror(errno));
   1791   1.7  christos 		FD_CLOSEONEXEC(pair[0]);
   1792   1.7  christos 		FD_CLOSEONEXEC(pair[1]);
   1793   1.7  christos 		mon->m_log_recvfd = pair[0];
   1794   1.7  christos 		mon->m_log_sendfd = pair[1];
   1795   1.7  christos 	} else
   1796   1.7  christos 		mon->m_log_recvfd = mon->m_log_sendfd = -1;
   1797   1.1  christos }
   1798   1.1  christos 
   1799   1.1  christos #define MM_MEMSIZE	65536
   1800   1.1  christos 
   1801   1.1  christos struct monitor *
   1802   1.1  christos monitor_init(void)
   1803   1.1  christos {
   1804   1.1  christos 	struct monitor *mon;
   1805   1.1  christos 
   1806   1.1  christos 	mon = xcalloc(1, sizeof(*mon));
   1807   1.7  christos 	monitor_openfds(mon, 1);
   1808   1.1  christos 
   1809   1.1  christos 	return mon;
   1810   1.1  christos }
   1811   1.1  christos 
   1812   1.1  christos void
   1813   1.1  christos monitor_reinit(struct monitor *mon)
   1814   1.1  christos {
   1815   1.7  christos 	monitor_openfds(mon, 0);
   1816   1.1  christos }
   1817   1.1  christos 
   1818   1.1  christos #ifdef GSSAPI
   1819   1.1  christos int
   1820  1.29  christos mm_answer_gss_setup_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
   1821   1.1  christos {
   1822   1.1  christos 	gss_OID_desc goid;
   1823   1.1  christos 	OM_uint32 major;
   1824  1.27  christos 	size_t len;
   1825  1.27  christos 	u_char *p;
   1826  1.27  christos 	int r;
   1827   1.1  christos 
   1828  1.20  christos 	if (!options.gss_authentication)
   1829  1.36  christos 		fatal_f("GSSAPI authentication not enabled");
   1830  1.20  christos 
   1831  1.27  christos 	if ((r = sshbuf_get_string(m, &p, &len)) != 0)
   1832  1.36  christos 		fatal_fr(r, "parse");
   1833  1.27  christos 	goid.elements = p;
   1834   1.1  christos 	goid.length = len;
   1835   1.1  christos 
   1836   1.1  christos 	major = ssh_gssapi_server_ctx(&gsscontext, &goid);
   1837   1.1  christos 
   1838  1.11  christos 	free(goid.elements);
   1839   1.1  christos 
   1840  1.27  christos 	sshbuf_reset(m);
   1841  1.27  christos 	if ((r = sshbuf_put_u32(m, major)) != 0)
   1842  1.36  christos 		fatal_fr(r, "assemble");
   1843   1.1  christos 
   1844   1.1  christos 	mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
   1845   1.1  christos 
   1846   1.1  christos 	/* Now we have a context, enable the step */
   1847   1.1  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
   1848   1.1  christos 
   1849   1.1  christos 	return (0);
   1850   1.1  christos }
   1851   1.1  christos 
   1852   1.1  christos int
   1853  1.29  christos mm_answer_gss_accept_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
   1854   1.1  christos {
   1855   1.1  christos 	gss_buffer_desc in;
   1856   1.1  christos 	gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
   1857   1.1  christos 	OM_uint32 major, minor;
   1858   1.1  christos 	OM_uint32 flags = 0; /* GSI needs this */
   1859  1.27  christos 	int r;
   1860   1.1  christos 
   1861  1.20  christos 	if (!options.gss_authentication)
   1862  1.36  christos 		fatal_f("GSSAPI authentication not enabled");
   1863  1.20  christos 
   1864  1.27  christos 	if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0)
   1865  1.36  christos 		fatal_fr(r, "ssh_gssapi_get_buffer_desc");
   1866   1.1  christos 	major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
   1867  1.11  christos 	free(in.value);
   1868   1.1  christos 
   1869  1.27  christos 	sshbuf_reset(m);
   1870  1.27  christos 	if ((r = sshbuf_put_u32(m, major)) != 0 ||
   1871  1.27  christos 	    (r = sshbuf_put_string(m, out.value, out.length)) != 0 ||
   1872  1.27  christos 	    (r = sshbuf_put_u32(m, flags)) != 0)
   1873  1.36  christos 		fatal_fr(r, "assemble");
   1874   1.1  christos 	mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
   1875   1.1  christos 
   1876   1.1  christos 	gss_release_buffer(&minor, &out);
   1877   1.1  christos 
   1878   1.1  christos 	if (major == GSS_S_COMPLETE) {
   1879   1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
   1880   1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
   1881   1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
   1882   1.1  christos 	}
   1883   1.1  christos 	return (0);
   1884   1.1  christos }
   1885   1.1  christos 
   1886   1.1  christos int
   1887  1.29  christos mm_answer_gss_checkmic(struct ssh *ssh, int sock, struct sshbuf *m)
   1888   1.1  christos {
   1889   1.1  christos 	gss_buffer_desc gssbuf, mic;
   1890   1.1  christos 	OM_uint32 ret;
   1891  1.27  christos 	int r;
   1892   1.1  christos 
   1893  1.20  christos 	if (!options.gss_authentication)
   1894  1.36  christos 		fatal_f("GSSAPI authentication not enabled");
   1895  1.20  christos 
   1896  1.27  christos 	if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 ||
   1897  1.27  christos 	    (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0)
   1898  1.36  christos 		fatal_fr(r, "ssh_gssapi_get_buffer_desc");
   1899   1.1  christos 
   1900   1.1  christos 	ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
   1901   1.1  christos 
   1902  1.11  christos 	free(gssbuf.value);
   1903  1.11  christos 	free(mic.value);
   1904   1.1  christos 
   1905  1.27  christos 	sshbuf_reset(m);
   1906  1.27  christos 	if ((r = sshbuf_put_u32(m, ret)) != 0)
   1907  1.36  christos 		fatal_fr(r, "assemble");
   1908   1.1  christos 
   1909   1.1  christos 	mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
   1910   1.1  christos 
   1911   1.1  christos 	if (!GSS_ERROR(ret))
   1912   1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
   1913   1.1  christos 
   1914   1.1  christos 	return (0);
   1915   1.1  christos }
   1916   1.1  christos 
   1917   1.1  christos int
   1918  1.29  christos mm_answer_gss_userok(struct ssh *ssh, int sock, struct sshbuf *m)
   1919   1.1  christos {
   1920  1.27  christos 	int r, authenticated;
   1921  1.23  christos 	const char *displayname;
   1922  1.29  christos 	Authctxt *authctxt = ssh->authctxt;
   1923   1.1  christos 
   1924  1.20  christos 	if (!options.gss_authentication)
   1925  1.36  christos 		fatal_f("GSSAPI authentication not enabled");
   1926  1.20  christos 
   1927   1.1  christos 	authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
   1928   1.1  christos 
   1929  1.27  christos 	sshbuf_reset(m);
   1930  1.27  christos 	if ((r = sshbuf_put_u32(m, authenticated)) != 0)
   1931  1.36  christos 		fatal_fr(r, "assemble");
   1932   1.1  christos 
   1933  1.36  christos 	debug3_f("sending result %d", authenticated);
   1934   1.1  christos 	mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
   1935   1.1  christos 
   1936   1.1  christos 	auth_method = "gssapi-with-mic";
   1937   1.1  christos 
   1938  1.23  christos 	if ((displayname = ssh_gssapi_displayname()) != NULL)
   1939  1.23  christos 		auth2_record_info(authctxt, "%s", displayname);
   1940  1.23  christos 
   1941   1.1  christos 	/* Monitor loop will terminate if authenticated */
   1942   1.1  christos 	return (authenticated);
   1943   1.1  christos }
   1944   1.1  christos #endif /* GSSAPI */
   1945   1.1  christos 
   1946