Home | History | Annotate | Line # | Download | only in dist
monitor.c revision 1.24
      1  1.24  christos /*	$NetBSD: monitor.c,v 1.24 2018/02/05 00:13:50 christos Exp $	*/
      2  1.23  christos /* $OpenBSD: monitor.c,v 1.174 2017/10/02 19:33:20 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.24  christos __RCSID("$NetBSD: monitor.c,v 1.24 2018/02/05 00:13:50 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.1  christos 
     55   1.7  christos #include "atomicio.h"
     56   1.1  christos #include "xmalloc.h"
     57   1.1  christos #include "ssh.h"
     58   1.1  christos #include "key.h"
     59   1.1  christos #include "buffer.h"
     60   1.1  christos #include "hostfile.h"
     61   1.1  christos #include "auth.h"
     62   1.1  christos #include "cipher.h"
     63   1.1  christos #include "kex.h"
     64   1.1  christos #include "dh.h"
     65   1.1  christos #include <zlib.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.1  christos 
     88   1.1  christos #ifdef GSSAPI
     89   1.1  christos static Gssctxt *gsscontext = NULL;
     90   1.1  christos #endif
     91   1.1  christos 
     92   1.1  christos /* Imports */
     93   1.1  christos extern ServerOptions options;
     94   1.1  christos extern u_int utmp_len;
     95   1.1  christos extern u_char session_id[];
     96   1.1  christos extern Buffer auth_debug;
     97   1.1  christos extern int auth_debug_init;
     98   1.1  christos extern Buffer loginmsg;
     99   1.1  christos 
    100   1.1  christos /* State exported from the child */
    101  1.14  christos static struct sshbuf *child_state;
    102   1.1  christos 
    103   1.1  christos /* Functions on the monitor that answer unprivileged requests */
    104   1.1  christos 
    105   1.1  christos int mm_answer_moduli(int, Buffer *);
    106   1.1  christos int mm_answer_sign(int, Buffer *);
    107   1.1  christos int mm_answer_pwnamallow(int, Buffer *);
    108   1.1  christos int mm_answer_auth2_read_banner(int, Buffer *);
    109   1.1  christos int mm_answer_authserv(int, Buffer *);
    110   1.1  christos int mm_answer_authpassword(int, Buffer *);
    111   1.1  christos int mm_answer_bsdauthquery(int, Buffer *);
    112   1.1  christos int mm_answer_bsdauthrespond(int, Buffer *);
    113   1.1  christos int mm_answer_skeyquery(int, Buffer *);
    114   1.1  christos int mm_answer_skeyrespond(int, Buffer *);
    115   1.1  christos int mm_answer_keyallowed(int, Buffer *);
    116   1.1  christos int mm_answer_keyverify(int, Buffer *);
    117   1.1  christos int mm_answer_pty(int, Buffer *);
    118   1.1  christos int mm_answer_pty_cleanup(int, Buffer *);
    119   1.1  christos int mm_answer_term(int, Buffer *);
    120   1.1  christos int mm_answer_rsa_keyallowed(int, Buffer *);
    121   1.1  christos int mm_answer_rsa_challenge(int, Buffer *);
    122   1.1  christos int mm_answer_rsa_response(int, Buffer *);
    123   1.1  christos int mm_answer_sesskey(int, Buffer *);
    124   1.1  christos int mm_answer_sessid(int, Buffer *);
    125   1.1  christos 
    126   1.2  christos #ifdef USE_PAM
    127   1.2  christos int mm_answer_pam_start(int, Buffer *);
    128   1.2  christos int mm_answer_pam_account(int, Buffer *);
    129   1.2  christos int mm_answer_pam_init_ctx(int, Buffer *);
    130   1.2  christos int mm_answer_pam_query(int, Buffer *);
    131   1.2  christos int mm_answer_pam_respond(int, Buffer *);
    132   1.2  christos int mm_answer_pam_free_ctx(int, Buffer *);
    133   1.2  christos #endif
    134   1.2  christos 
    135   1.2  christos #ifdef KRB5
    136   1.2  christos int mm_answer_krb5(int, Buffer *);
    137  1.20  christos #endif
    138   1.2  christos 
    139   1.1  christos #ifdef GSSAPI
    140   1.1  christos int mm_answer_gss_setup_ctx(int, Buffer *);
    141   1.1  christos int mm_answer_gss_accept_ctx(int, Buffer *);
    142   1.1  christos int mm_answer_gss_userok(int, Buffer *);
    143   1.1  christos int mm_answer_gss_checkmic(int, Buffer *);
    144   1.1  christos #endif
    145   1.1  christos 
    146   1.7  christos static int monitor_read_log(struct monitor *);
    147   1.7  christos 
    148   1.1  christos static Authctxt *authctxt;
    149  1.13  christos 
    150   1.1  christos /* local state for key verify */
    151   1.1  christos static u_char *key_blob = NULL;
    152   1.1  christos static u_int key_bloblen = 0;
    153   1.1  christos static int key_blobtype = MM_NOKEY;
    154   1.1  christos static char *hostbased_cuser = NULL;
    155   1.1  christos static char *hostbased_chost = NULL;
    156   1.6  christos static const char *auth_method = "unknown";
    157  1.10  christos static const char *auth_submethod = NULL;
    158   1.1  christos static u_int session_id2_len = 0;
    159   1.1  christos static u_char *session_id2 = NULL;
    160   1.1  christos static pid_t monitor_child_pid;
    161   1.1  christos 
    162   1.1  christos struct mon_table {
    163   1.1  christos 	enum monitor_reqtype type;
    164   1.1  christos 	int flags;
    165   1.1  christos 	int (*f)(int, Buffer *);
    166   1.1  christos };
    167   1.1  christos 
    168   1.1  christos #define MON_ISAUTH	0x0004	/* Required for Authentication */
    169   1.1  christos #define MON_AUTHDECIDE	0x0008	/* Decides Authentication */
    170   1.1  christos #define MON_ONCE	0x0010	/* Disable after calling */
    171   1.1  christos #define MON_ALOG	0x0020	/* Log auth attempt without authenticating */
    172   1.1  christos 
    173   1.1  christos #define MON_AUTH	(MON_ISAUTH|MON_AUTHDECIDE)
    174   1.1  christos 
    175   1.1  christos #define MON_PERMIT	0x1000	/* Request is permitted */
    176   1.1  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 
    227   1.1  christos static void
    228   1.1  christos monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
    229   1.1  christos {
    230   1.1  christos 	while (ent->f != NULL) {
    231   1.1  christos 		if (ent->type == type) {
    232   1.1  christos 			ent->flags &= ~MON_PERMIT;
    233   1.1  christos 			ent->flags |= permit ? MON_PERMIT : 0;
    234   1.1  christos 			return;
    235   1.1  christos 		}
    236   1.1  christos 		ent++;
    237   1.1  christos 	}
    238   1.1  christos }
    239   1.1  christos 
    240   1.1  christos static void
    241   1.1  christos monitor_permit_authentications(int permit)
    242   1.1  christos {
    243   1.1  christos 	struct mon_table *ent = mon_dispatch;
    244   1.1  christos 
    245   1.1  christos 	while (ent->f != NULL) {
    246   1.1  christos 		if (ent->flags & MON_AUTH) {
    247   1.1  christos 			ent->flags &= ~MON_PERMIT;
    248   1.1  christos 			ent->flags |= permit ? MON_PERMIT : 0;
    249   1.1  christos 		}
    250   1.1  christos 		ent++;
    251   1.1  christos 	}
    252   1.1  christos }
    253   1.1  christos 
    254   1.1  christos void
    255   1.1  christos monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
    256   1.1  christos {
    257  1.22  christos 	struct ssh *ssh = active_state;	/* XXX */
    258   1.1  christos 	struct mon_table *ent;
    259  1.10  christos 	int authenticated = 0, partial = 0;
    260   1.1  christos 
    261   1.1  christos 	debug3("preauth child monitor started");
    262   1.1  christos 
    263   1.7  christos 	close(pmonitor->m_recvfd);
    264   1.7  christos 	close(pmonitor->m_log_sendfd);
    265   1.7  christos 	pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
    266   1.7  christos 
    267   1.1  christos 	authctxt = _authctxt;
    268   1.1  christos 	memset(authctxt, 0, sizeof(*authctxt));
    269   1.1  christos 
    270  1.20  christos 	mon_dispatch = mon_dispatch_proto20;
    271  1.20  christos 	/* Permit requests for moduli and signatures */
    272  1.20  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
    273  1.20  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
    274   1.1  christos 
    275   1.1  christos 	/* The first few requests do not require asynchronous access */
    276   1.1  christos 	while (!authenticated) {
    277  1.10  christos 		partial = 0;
    278   1.1  christos 		auth_method = "unknown";
    279  1.10  christos 		auth_submethod = NULL;
    280  1.23  christos 		auth2_authctxt_reset_info(authctxt);
    281  1.23  christos 
    282   1.1  christos 		authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
    283  1.10  christos 
    284  1.10  christos 		/* Special handling for multiple required authentications */
    285  1.10  christos 		if (options.num_auth_methods != 0) {
    286  1.10  christos 			if (authenticated &&
    287  1.10  christos 			    !auth2_update_methods_lists(authctxt,
    288  1.11  christos 			    auth_method, auth_submethod)) {
    289  1.10  christos 				debug3("%s: method %s: partial", __func__,
    290  1.10  christos 				    auth_method);
    291  1.10  christos 				authenticated = 0;
    292  1.10  christos 				partial = 1;
    293  1.10  christos 			}
    294  1.10  christos 		}
    295  1.10  christos 
    296   1.1  christos 		if (authenticated) {
    297   1.1  christos 			if (!(ent->flags & MON_AUTHDECIDE))
    298   1.1  christos 				fatal("%s: unexpected authentication from %d",
    299   1.1  christos 				    __func__, ent->type);
    300   1.1  christos 			if (authctxt->pw->pw_uid == 0 &&
    301   1.1  christos 			    !auth_root_allowed(auth_method))
    302   1.1  christos 				authenticated = 0;
    303   1.2  christos #ifdef USE_PAM
    304   1.2  christos 			/* PAM needs to perform account checks after auth */
    305   1.2  christos 			if (options.use_pam && authenticated) {
    306   1.2  christos 				Buffer m;
    307   1.2  christos 
    308   1.2  christos 				buffer_init(&m);
    309   1.2  christos 				mm_request_receive_expect(pmonitor->m_sendfd,
    310   1.2  christos 				    MONITOR_REQ_PAM_ACCOUNT, &m);
    311   1.2  christos 				authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
    312   1.2  christos 				buffer_free(&m);
    313   1.2  christos 			}
    314   1.2  christos #endif
    315   1.1  christos 		}
    316   1.1  christos 		if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
    317  1.10  christos 			auth_log(authctxt, authenticated, partial,
    318  1.11  christos 			    auth_method, auth_submethod);
    319  1.15  christos 			if (!partial && !authenticated)
    320   1.1  christos 				authctxt->failures++;
    321  1.23  christos 			if (authenticated || partial) {
    322  1.23  christos 				auth2_update_session_info(authctxt,
    323  1.23  christos 				    auth_method, auth_submethod);
    324  1.23  christos 			}
    325   1.1  christos 		}
    326   1.1  christos 	}
    327   1.1  christos 
    328   1.1  christos 	if (!authctxt->valid)
    329   1.1  christos 		fatal("%s: authenticated invalid user", __func__);
    330   1.1  christos 	if (strcmp(auth_method, "unknown") == 0)
    331   1.1  christos 		fatal("%s: authentication method name unknown", __func__);
    332   1.1  christos 
    333   1.1  christos 	debug("%s: %s has been authenticated by privileged process",
    334   1.1  christos 	    __func__, authctxt->user);
    335  1.22  christos 	ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
    336   1.1  christos 
    337   1.1  christos 	mm_get_keystate(pmonitor);
    338   1.7  christos 
    339  1.10  christos 	/* Drain any buffered messages from the child */
    340  1.10  christos 	while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
    341  1.10  christos 		;
    342  1.10  christos 
    343   1.7  christos 	close(pmonitor->m_sendfd);
    344   1.7  christos 	close(pmonitor->m_log_recvfd);
    345   1.7  christos 	pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
    346   1.1  christos }
    347   1.1  christos 
    348   1.1  christos static void
    349   1.1  christos monitor_set_child_handler(pid_t pid)
    350   1.1  christos {
    351   1.1  christos 	monitor_child_pid = pid;
    352   1.1  christos }
    353   1.1  christos 
    354   1.1  christos static void
    355   1.1  christos monitor_child_handler(int sig)
    356   1.1  christos {
    357   1.1  christos 	kill(monitor_child_pid, sig);
    358   1.1  christos }
    359   1.1  christos 
    360   1.1  christos void
    361   1.1  christos monitor_child_postauth(struct monitor *pmonitor)
    362   1.1  christos {
    363   1.7  christos 	close(pmonitor->m_recvfd);
    364   1.7  christos 	pmonitor->m_recvfd = -1;
    365   1.7  christos 
    366   1.1  christos 	monitor_set_child_handler(pmonitor->m_pid);
    367   1.1  christos 	signal(SIGHUP, &monitor_child_handler);
    368   1.1  christos 	signal(SIGTERM, &monitor_child_handler);
    369   1.1  christos 	signal(SIGINT, &monitor_child_handler);
    370   1.1  christos 
    371  1.20  christos 	mon_dispatch = mon_dispatch_postauth20;
    372  1.20  christos 
    373  1.20  christos 	/* Permit requests for moduli and signatures */
    374  1.20  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
    375  1.20  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
    376  1.20  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
    377   1.1  christos 
    378   1.1  christos 	if (!no_pty_flag) {
    379   1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
    380   1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
    381   1.1  christos 	}
    382   1.1  christos 
    383   1.1  christos 	for (;;)
    384   1.1  christos 		monitor_read(pmonitor, mon_dispatch, NULL);
    385   1.1  christos }
    386   1.1  christos 
    387   1.7  christos static int
    388   1.7  christos monitor_read_log(struct monitor *pmonitor)
    389   1.7  christos {
    390   1.7  christos 	Buffer logmsg;
    391   1.7  christos 	u_int len, level;
    392   1.7  christos 	char *msg;
    393   1.7  christos 
    394   1.7  christos 	buffer_init(&logmsg);
    395   1.7  christos 
    396   1.7  christos 	/* Read length */
    397   1.7  christos 	buffer_append_space(&logmsg, 4);
    398   1.7  christos 	if (atomicio(read, pmonitor->m_log_recvfd,
    399   1.7  christos 	    buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
    400   1.7  christos 		if (errno == EPIPE) {
    401   1.8  christos 			buffer_free(&logmsg);
    402   1.7  christos 			debug("%s: child log fd closed", __func__);
    403   1.7  christos 			close(pmonitor->m_log_recvfd);
    404   1.7  christos 			pmonitor->m_log_recvfd = -1;
    405   1.7  christos 			return -1;
    406   1.7  christos 		}
    407   1.7  christos 		fatal("%s: log fd read: %s", __func__, strerror(errno));
    408   1.7  christos 	}
    409   1.7  christos 	len = buffer_get_int(&logmsg);
    410   1.7  christos 	if (len <= 4 || len > 8192)
    411   1.7  christos 		fatal("%s: invalid log message length %u", __func__, len);
    412   1.7  christos 
    413   1.7  christos 	/* Read severity, message */
    414   1.7  christos 	buffer_clear(&logmsg);
    415   1.7  christos 	buffer_append_space(&logmsg, len);
    416   1.7  christos 	if (atomicio(read, pmonitor->m_log_recvfd,
    417   1.7  christos 	    buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
    418   1.7  christos 		fatal("%s: log fd read: %s", __func__, strerror(errno));
    419   1.7  christos 
    420   1.7  christos 	/* Log it */
    421   1.7  christos 	level = buffer_get_int(&logmsg);
    422   1.7  christos 	msg = buffer_get_string(&logmsg, NULL);
    423   1.7  christos 	if (log_level_name(level) == NULL)
    424   1.7  christos 		fatal("%s: invalid log level %u (corrupted message?)",
    425   1.7  christos 		    __func__, level);
    426   1.7  christos 	do_log2(level, "%s [preauth]", msg);
    427   1.7  christos 
    428   1.7  christos 	buffer_free(&logmsg);
    429  1.11  christos 	free(msg);
    430   1.7  christos 
    431   1.7  christos 	return 0;
    432   1.7  christos }
    433   1.7  christos 
    434   1.1  christos int
    435   1.1  christos monitor_read(struct monitor *pmonitor, struct mon_table *ent,
    436   1.1  christos     struct mon_table **pent)
    437   1.1  christos {
    438   1.1  christos 	Buffer m;
    439   1.1  christos 	int ret;
    440   1.1  christos 	u_char type;
    441   1.7  christos 	struct pollfd pfd[2];
    442   1.7  christos 
    443   1.7  christos 	for (;;) {
    444  1.13  christos 		memset(&pfd, 0, sizeof(pfd));
    445   1.7  christos 		pfd[0].fd = pmonitor->m_sendfd;
    446   1.7  christos 		pfd[0].events = POLLIN;
    447   1.7  christos 		pfd[1].fd = pmonitor->m_log_recvfd;
    448   1.7  christos 		pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
    449   1.7  christos 		if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
    450   1.7  christos 			if (errno == EINTR || errno == EAGAIN)
    451   1.7  christos 				continue;
    452   1.7  christos 			fatal("%s: poll: %s", __func__, strerror(errno));
    453   1.7  christos 		}
    454   1.7  christos 		if (pfd[1].revents) {
    455   1.7  christos 			/*
    456   1.7  christos 			 * Drain all log messages before processing next
    457   1.7  christos 			 * monitor request.
    458   1.7  christos 			 */
    459   1.7  christos 			monitor_read_log(pmonitor);
    460   1.7  christos 			continue;
    461   1.7  christos 		}
    462   1.7  christos 		if (pfd[0].revents)
    463   1.7  christos 			break;  /* Continues below */
    464   1.7  christos 	}
    465   1.1  christos 
    466   1.1  christos 	buffer_init(&m);
    467   1.1  christos 
    468   1.1  christos 	mm_request_receive(pmonitor->m_sendfd, &m);
    469   1.1  christos 	type = buffer_get_char(&m);
    470   1.1  christos 
    471   1.1  christos 	debug3("%s: checking request %d", __func__, type);
    472   1.1  christos 
    473   1.1  christos 	while (ent->f != NULL) {
    474   1.1  christos 		if (ent->type == type)
    475   1.1  christos 			break;
    476   1.1  christos 		ent++;
    477   1.1  christos 	}
    478   1.1  christos 
    479   1.1  christos 	if (ent->f != NULL) {
    480   1.1  christos 		if (!(ent->flags & MON_PERMIT))
    481   1.1  christos 			fatal("%s: unpermitted request %d", __func__,
    482   1.1  christos 			    type);
    483   1.1  christos 		ret = (*ent->f)(pmonitor->m_sendfd, &m);
    484   1.1  christos 		buffer_free(&m);
    485   1.1  christos 
    486   1.1  christos 		/* The child may use this request only once, disable it */
    487   1.1  christos 		if (ent->flags & MON_ONCE) {
    488   1.1  christos 			debug2("%s: %d used once, disabling now", __func__,
    489   1.1  christos 			    type);
    490   1.1  christos 			ent->flags &= ~MON_PERMIT;
    491   1.1  christos 		}
    492   1.1  christos 
    493   1.1  christos 		if (pent != NULL)
    494   1.1  christos 			*pent = ent;
    495   1.1  christos 
    496   1.1  christos 		return ret;
    497   1.1  christos 	}
    498   1.1  christos 
    499   1.1  christos 	fatal("%s: unsupported request: %d", __func__, type);
    500   1.1  christos 
    501   1.1  christos 	/* NOTREACHED */
    502   1.1  christos 	return (-1);
    503   1.1  christos }
    504   1.1  christos 
    505   1.1  christos /* allowed key state */
    506   1.1  christos static int
    507   1.1  christos monitor_allowed_key(u_char *blob, u_int bloblen)
    508   1.1  christos {
    509   1.1  christos 	/* make sure key is allowed */
    510   1.1  christos 	if (key_blob == NULL || key_bloblen != bloblen ||
    511   1.4      adam 	    timingsafe_bcmp(key_blob, blob, key_bloblen))
    512   1.1  christos 		return (0);
    513   1.1  christos 	return (1);
    514   1.1  christos }
    515   1.1  christos 
    516   1.1  christos static void
    517   1.1  christos monitor_reset_key_state(void)
    518   1.1  christos {
    519   1.1  christos 	/* reset state */
    520  1.11  christos 	free(key_blob);
    521  1.11  christos 	free(hostbased_cuser);
    522  1.11  christos 	free(hostbased_chost);
    523   1.1  christos 	key_blob = NULL;
    524   1.1  christos 	key_bloblen = 0;
    525   1.1  christos 	key_blobtype = MM_NOKEY;
    526   1.1  christos 	hostbased_cuser = NULL;
    527   1.1  christos 	hostbased_chost = NULL;
    528   1.1  christos }
    529   1.1  christos 
    530  1.13  christos #ifdef WITH_OPENSSL
    531   1.1  christos int
    532   1.1  christos mm_answer_moduli(int sock, Buffer *m)
    533   1.1  christos {
    534   1.1  christos 	DH *dh;
    535   1.1  christos 	int min, want, max;
    536   1.1  christos 
    537   1.1  christos 	min = buffer_get_int(m);
    538   1.1  christos 	want = buffer_get_int(m);
    539   1.1  christos 	max = buffer_get_int(m);
    540   1.1  christos 
    541   1.1  christos 	debug3("%s: got parameters: %d %d %d",
    542   1.1  christos 	    __func__, min, want, max);
    543   1.1  christos 	/* We need to check here, too, in case the child got corrupted */
    544   1.1  christos 	if (max < min || want < min || max < want)
    545   1.1  christos 		fatal("%s: bad parameters: %d %d %d",
    546   1.1  christos 		    __func__, min, want, max);
    547   1.1  christos 
    548   1.1  christos 	buffer_clear(m);
    549   1.1  christos 
    550   1.1  christos 	dh = choose_dh(min, want, max);
    551   1.1  christos 	if (dh == NULL) {
    552   1.1  christos 		buffer_put_char(m, 0);
    553   1.1  christos 		return (0);
    554   1.1  christos 	} else {
    555  1.24  christos 		const BIGNUM *p, *g;
    556  1.24  christos 		DH_get0_pqg(dh, &p, NULL, &g);
    557   1.1  christos 		/* Send first bignum */
    558   1.1  christos 		buffer_put_char(m, 1);
    559  1.24  christos 		buffer_put_bignum2(m, p);
    560  1.24  christos 		buffer_put_bignum2(m, g);
    561   1.1  christos 
    562   1.1  christos 		DH_free(dh);
    563   1.1  christos 	}
    564   1.1  christos 	mm_request_send(sock, MONITOR_ANS_MODULI, m);
    565   1.1  christos 	return (0);
    566   1.1  christos }
    567  1.13  christos #endif
    568   1.1  christos 
    569   1.1  christos int
    570   1.1  christos mm_answer_sign(int sock, Buffer *m)
    571   1.1  christos {
    572  1.14  christos 	struct ssh *ssh = active_state; 	/* XXX */
    573  1.14  christos 	extern int auth_sock;			/* XXX move to state struct? */
    574  1.14  christos 	struct sshkey *key;
    575  1.18  christos 	struct sshbuf *sigbuf = NULL;
    576  1.18  christos 	u_char *p = NULL, *signature = NULL;
    577  1.18  christos 	char *alg = NULL;
    578  1.18  christos 	size_t datlen, siglen, alglen;
    579  1.18  christos 	int r, is_proof = 0;
    580  1.19  christos 	u_int keyid;
    581  1.14  christos 	const char proof_req[] = "hostkeys-prove-00 (at) openssh.com";
    582   1.1  christos 
    583   1.1  christos 	debug3("%s", __func__);
    584   1.1  christos 
    585  1.14  christos 	if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
    586  1.18  christos 	    (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
    587  1.18  christos 	    (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0)
    588  1.14  christos 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
    589  1.19  christos 	if (keyid > INT_MAX)
    590  1.19  christos 		fatal("%s: invalid key ID", __func__);
    591   1.1  christos 
    592   1.1  christos 	/*
    593   1.6  christos 	 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
    594   1.6  christos 	 * SHA384 (48 bytes) and SHA512 (64 bytes).
    595  1.14  christos 	 *
    596  1.14  christos 	 * Otherwise, verify the signature request is for a hostkey
    597  1.14  christos 	 * proof.
    598  1.14  christos 	 *
    599  1.14  christos 	 * XXX perform similar check for KEX signature requests too?
    600  1.14  christos 	 * it's not trivial, since what is signed is the hash, rather
    601  1.14  christos 	 * than the full kex structure...
    602   1.1  christos 	 */
    603  1.14  christos 	if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
    604  1.14  christos 		/*
    605  1.14  christos 		 * Construct expected hostkey proof and compare it to what
    606  1.14  christos 		 * the client sent us.
    607  1.14  christos 		 */
    608  1.14  christos 		if (session_id2_len == 0) /* hostkeys is never first */
    609  1.14  christos 			fatal("%s: bad data length: %zu", __func__, datlen);
    610  1.14  christos 		if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
    611  1.14  christos 			fatal("%s: no hostkey for index %d", __func__, keyid);
    612  1.14  christos 		if ((sigbuf = sshbuf_new()) == NULL)
    613  1.14  christos 			fatal("%s: sshbuf_new", __func__);
    614  1.14  christos 		if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
    615  1.14  christos 		    (r = sshbuf_put_string(sigbuf, session_id2,
    616  1.18  christos 		    session_id2_len)) != 0 ||
    617  1.14  christos 		    (r = sshkey_puts(key, sigbuf)) != 0)
    618  1.14  christos 			fatal("%s: couldn't prepare private key "
    619  1.14  christos 			    "proof buffer: %s", __func__, ssh_err(r));
    620  1.14  christos 		if (datlen != sshbuf_len(sigbuf) ||
    621  1.14  christos 		    memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
    622  1.14  christos 			fatal("%s: bad data length: %zu, hostkey proof len %zu",
    623  1.14  christos 			    __func__, datlen, sshbuf_len(sigbuf));
    624  1.14  christos 		sshbuf_free(sigbuf);
    625  1.14  christos 		is_proof = 1;
    626  1.14  christos 	}
    627   1.1  christos 
    628   1.1  christos 	/* save session id, it will be passed on the first call */
    629   1.1  christos 	if (session_id2_len == 0) {
    630   1.1  christos 		session_id2_len = datlen;
    631   1.1  christos 		session_id2 = xmalloc(session_id2_len);
    632   1.1  christos 		memcpy(session_id2, p, session_id2_len);
    633   1.1  christos 	}
    634   1.1  christos 
    635  1.11  christos 	if ((key = get_hostkey_by_index(keyid)) != NULL) {
    636  1.18  christos 		if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
    637  1.14  christos 		    datafellows)) != 0)
    638  1.14  christos 			fatal("%s: sshkey_sign failed: %s",
    639  1.14  christos 			    __func__, ssh_err(r));
    640  1.14  christos 	} else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
    641  1.14  christos 	    auth_sock > 0) {
    642  1.14  christos 		if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
    643  1.18  christos 		    p, datlen, alg, datafellows)) != 0) {
    644  1.14  christos 			fatal("%s: ssh_agent_sign failed: %s",
    645  1.14  christos 			    __func__, ssh_err(r));
    646  1.14  christos 		}
    647  1.11  christos 	} else
    648   1.1  christos 		fatal("%s: no hostkey from index %d", __func__, keyid);
    649   1.1  christos 
    650  1.14  christos 	debug3("%s: %s signature %p(%zu)", __func__,
    651  1.14  christos 	    is_proof ? "KEX" : "hostkey proof", signature, siglen);
    652   1.1  christos 
    653  1.14  christos 	sshbuf_reset(m);
    654  1.14  christos 	if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
    655  1.14  christos 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
    656   1.1  christos 
    657  1.18  christos 	free(alg);
    658  1.11  christos 	free(p);
    659  1.11  christos 	free(signature);
    660   1.1  christos 
    661   1.1  christos 	mm_request_send(sock, MONITOR_ANS_SIGN, m);
    662   1.1  christos 
    663   1.1  christos 	/* Turn on permissions for getpwnam */
    664   1.1  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
    665   1.1  christos 
    666   1.1  christos 	return (0);
    667   1.1  christos }
    668   1.1  christos 
    669   1.1  christos /* Retrieves the password entry and also checks if the user is permitted */
    670   1.1  christos 
    671   1.1  christos int
    672   1.1  christos mm_answer_pwnamallow(int sock, Buffer *m)
    673   1.1  christos {
    674  1.22  christos 	struct ssh *ssh = active_state;	/* XXX */
    675   1.1  christos 	char *username;
    676   1.1  christos 	struct passwd *pwent;
    677   1.1  christos 	int allowed = 0;
    678   1.7  christos 	u_int i;
    679   1.1  christos 
    680   1.1  christos 	debug3("%s", __func__);
    681   1.1  christos 
    682   1.1  christos 	if (authctxt->attempt++ != 0)
    683   1.1  christos 		fatal("%s: multiple attempts for getpwnam", __func__);
    684   1.1  christos 
    685   1.1  christos 	username = buffer_get_string(m, NULL);
    686   1.1  christos 
    687   1.1  christos 	pwent = getpwnamallow(username);
    688   1.1  christos 
    689   1.1  christos 	authctxt->user = xstrdup(username);
    690   1.1  christos 	setproctitle("%s [priv]", pwent ? username : "unknown");
    691  1.11  christos 	free(username);
    692   1.1  christos 
    693   1.1  christos 	buffer_clear(m);
    694   1.1  christos 
    695   1.1  christos 	if (pwent == NULL) {
    696   1.1  christos 		buffer_put_char(m, 0);
    697   1.1  christos 		authctxt->pw = fakepw();
    698   1.1  christos 		goto out;
    699   1.1  christos 	}
    700   1.1  christos 
    701   1.1  christos 	allowed = 1;
    702   1.1  christos 	authctxt->pw = pwent;
    703   1.1  christos 	authctxt->valid = 1;
    704   1.1  christos 
    705   1.1  christos 	buffer_put_char(m, 1);
    706   1.1  christos 	buffer_put_string(m, pwent, sizeof(struct passwd));
    707   1.1  christos 	buffer_put_cstring(m, pwent->pw_name);
    708   1.1  christos 	buffer_put_cstring(m, "*");
    709   1.1  christos 	buffer_put_cstring(m, pwent->pw_gecos);
    710   1.1  christos 	buffer_put_cstring(m, pwent->pw_class);
    711   1.1  christos 	buffer_put_cstring(m, pwent->pw_dir);
    712   1.1  christos 	buffer_put_cstring(m, pwent->pw_shell);
    713   1.1  christos 
    714   1.1  christos  out:
    715  1.22  christos 	ssh_packet_set_log_preamble(ssh, "%suser %s",
    716  1.22  christos 	    authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
    717   1.1  christos 	buffer_put_string(m, &options, sizeof(options));
    718   1.7  christos 
    719   1.7  christos #define M_CP_STROPT(x) do { \
    720   1.7  christos 		if (options.x != NULL) \
    721   1.7  christos 			buffer_put_cstring(m, options.x); \
    722   1.7  christos 	} while (0)
    723   1.7  christos #define M_CP_STRARRAYOPT(x, nx) do { \
    724   1.7  christos 		for (i = 0; i < options.nx; i++) \
    725   1.7  christos 			buffer_put_cstring(m, options.x[i]); \
    726   1.7  christos 	} while (0)
    727  1.23  christos #define M_CP_STRARRAYOPT_ALLOC(x, nx) M_CP_STRARRAYOPT(x, nx)
    728   1.7  christos 	/* See comment in servconf.h */
    729   1.7  christos 	COPY_MATCH_STRING_OPTS();
    730   1.7  christos #undef M_CP_STROPT
    731   1.7  christos #undef M_CP_STRARRAYOPT
    732  1.23  christos #undef M_CP_STRARRAYOPT_ALLOC
    733  1.10  christos 
    734  1.10  christos 	/* Create valid auth method lists */
    735  1.20  christos 	if (auth2_setup_methods_lists(authctxt) != 0) {
    736  1.10  christos 		/*
    737  1.10  christos 		 * The monitor will continue long enough to let the child
    738  1.10  christos 		 * run to it's packet_disconnect(), but it must not allow any
    739  1.10  christos 		 * authentication to succeed.
    740  1.10  christos 		 */
    741  1.10  christos 		debug("%s: no valid authentication method lists", __func__);
    742  1.10  christos 	}
    743  1.10  christos 
    744   1.1  christos 	debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
    745   1.1  christos 	mm_request_send(sock, MONITOR_ANS_PWNAM, m);
    746   1.1  christos 
    747  1.20  christos 	/* Allow service/style information on the auth context */
    748  1.20  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
    749  1.20  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
    750   1.1  christos 
    751   1.2  christos #ifdef USE_PAM
    752   1.2  christos 	if (options.use_pam)
    753   1.2  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
    754   1.2  christos #endif
    755   1.1  christos 
    756   1.1  christos 	return (0);
    757   1.1  christos }
    758   1.1  christos 
    759   1.1  christos int mm_answer_auth2_read_banner(int sock, Buffer *m)
    760   1.1  christos {
    761   1.1  christos 	char *banner;
    762   1.1  christos 
    763   1.1  christos 	buffer_clear(m);
    764   1.1  christos 	banner = auth2_read_banner();
    765   1.1  christos 	buffer_put_cstring(m, banner != NULL ? banner : "");
    766   1.1  christos 	mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
    767  1.11  christos 	free(banner);
    768   1.1  christos 
    769   1.1  christos 	return (0);
    770   1.1  christos }
    771   1.1  christos 
    772   1.1  christos int
    773   1.1  christos mm_answer_authserv(int sock, Buffer *m)
    774   1.1  christos {
    775   1.1  christos 	monitor_permit_authentications(1);
    776   1.1  christos 
    777   1.1  christos 	authctxt->service = buffer_get_string(m, NULL);
    778   1.1  christos 	authctxt->style = buffer_get_string(m, NULL);
    779   1.1  christos 	debug3("%s: service=%s, style=%s",
    780   1.1  christos 	    __func__, authctxt->service, authctxt->style);
    781   1.1  christos 
    782   1.1  christos 	if (strlen(authctxt->style) == 0) {
    783  1.11  christos 		free(authctxt->style);
    784   1.1  christos 		authctxt->style = NULL;
    785   1.1  christos 	}
    786   1.1  christos 
    787   1.1  christos 	return (0);
    788   1.1  christos }
    789   1.1  christos 
    790   1.1  christos int
    791   1.1  christos mm_answer_authpassword(int sock, Buffer *m)
    792   1.1  christos {
    793   1.1  christos 	static int call_count;
    794   1.1  christos 	char *passwd;
    795   1.1  christos 	int authenticated;
    796   1.1  christos 	u_int plen;
    797   1.1  christos 
    798  1.20  christos 	if (!options.password_authentication)
    799  1.20  christos 		fatal("%s: password authentication not enabled", __func__);
    800   1.1  christos 	passwd = buffer_get_string(m, &plen);
    801   1.1  christos 	/* Only authenticate if the context is valid */
    802   1.1  christos 	authenticated = options.password_authentication &&
    803   1.1  christos 	    auth_password(authctxt, passwd);
    804  1.13  christos 	explicit_bzero(passwd, strlen(passwd));
    805  1.11  christos 	free(passwd);
    806   1.1  christos 
    807   1.1  christos 	buffer_clear(m);
    808   1.1  christos 	buffer_put_int(m, authenticated);
    809   1.1  christos 
    810   1.1  christos 	debug3("%s: sending result %d", __func__, authenticated);
    811   1.1  christos 	mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
    812   1.1  christos 
    813   1.1  christos 	call_count++;
    814   1.1  christos 	if (plen == 0 && call_count == 1)
    815   1.1  christos 		auth_method = "none";
    816   1.1  christos 	else
    817   1.1  christos 		auth_method = "password";
    818   1.1  christos 
    819   1.1  christos 	/* Causes monitor loop to terminate if authenticated */
    820   1.1  christos 	return (authenticated);
    821   1.1  christos }
    822   1.1  christos 
    823   1.2  christos #ifdef BSD_AUTH
    824   1.1  christos int
    825   1.1  christos mm_answer_bsdauthquery(int sock, Buffer *m)
    826   1.1  christos {
    827   1.1  christos 	char *name, *infotxt;
    828   1.1  christos 	u_int numprompts;
    829   1.1  christos 	u_int *echo_on;
    830   1.1  christos 	char **prompts;
    831   1.1  christos 	u_int success;
    832   1.1  christos 
    833  1.20  christos 	if (!options.kbd_interactive_authentication)
    834  1.20  christos 		fatal("%s: kbd-int authentication not enabled", __func__);
    835   1.1  christos 	success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
    836   1.1  christos 	    &prompts, &echo_on) < 0 ? 0 : 1;
    837   1.1  christos 
    838   1.1  christos 	buffer_clear(m);
    839   1.1  christos 	buffer_put_int(m, success);
    840   1.1  christos 	if (success)
    841   1.1  christos 		buffer_put_cstring(m, prompts[0]);
    842   1.1  christos 
    843   1.1  christos 	debug3("%s: sending challenge success: %u", __func__, success);
    844   1.1  christos 	mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
    845   1.1  christos 
    846   1.1  christos 	if (success) {
    847  1.11  christos 		free(name);
    848  1.11  christos 		free(infotxt);
    849  1.11  christos 		free(prompts);
    850  1.11  christos 		free(echo_on);
    851   1.1  christos 	}
    852   1.1  christos 
    853   1.1  christos 	return (0);
    854   1.1  christos }
    855   1.1  christos 
    856   1.1  christos int
    857   1.1  christos mm_answer_bsdauthrespond(int sock, Buffer *m)
    858   1.1  christos {
    859   1.1  christos 	char *response;
    860   1.1  christos 	int authok;
    861   1.1  christos 
    862  1.20  christos 	if (!options.kbd_interactive_authentication)
    863  1.20  christos 		fatal("%s: kbd-int authentication not enabled", __func__);
    864  1.18  christos 	if (authctxt->as == NULL)
    865   1.1  christos 		fatal("%s: no bsd auth session", __func__);
    866   1.1  christos 
    867   1.1  christos 	response = buffer_get_string(m, NULL);
    868   1.1  christos 	authok = options.challenge_response_authentication &&
    869   1.1  christos 	    auth_userresponse(authctxt->as, response, 0);
    870   1.1  christos 	authctxt->as = NULL;
    871   1.1  christos 	debug3("%s: <%s> = <%d>", __func__, response, authok);
    872  1.11  christos 	free(response);
    873   1.1  christos 
    874   1.1  christos 	buffer_clear(m);
    875   1.1  christos 	buffer_put_int(m, authok);
    876   1.1  christos 
    877   1.1  christos 	debug3("%s: sending authenticated: %d", __func__, authok);
    878   1.1  christos 	mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
    879   1.1  christos 
    880  1.20  christos 	auth_method = "keyboard-interactive";
    881  1.20  christos 	auth_submethod = "bsdauth";
    882   1.1  christos 
    883   1.1  christos 	return (authok != 0);
    884   1.1  christos }
    885   1.2  christos #endif
    886   1.2  christos 
    887   1.2  christos #ifdef SKEY
    888   1.2  christos int
    889   1.2  christos mm_answer_skeyquery(int sock, Buffer *m)
    890   1.2  christos {
    891   1.2  christos 	struct skey skey;
    892   1.2  christos 	char challenge[1024];
    893   1.2  christos 	u_int success;
    894   1.2  christos 
    895   1.2  christos 	success = skeychallenge(&skey, authctxt->user, challenge,
    896   1.2  christos 	    sizeof(challenge)) < 0 ? 0 : 1;
    897   1.2  christos 
    898   1.2  christos 	buffer_clear(m);
    899   1.2  christos 	buffer_put_int(m, success);
    900   1.2  christos 	if (success)
    901   1.2  christos 		buffer_put_cstring(m, challenge);
    902   1.2  christos 
    903   1.2  christos 	debug3("%s: sending challenge success: %u", __func__, success);
    904   1.2  christos 	mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
    905   1.2  christos 
    906   1.2  christos 	return (0);
    907   1.2  christos }
    908   1.2  christos 
    909   1.2  christos int
    910   1.2  christos mm_answer_skeyrespond(int sock, Buffer *m)
    911   1.2  christos {
    912   1.2  christos 	char *response;
    913   1.2  christos 	int authok;
    914   1.2  christos 
    915   1.2  christos 	response = buffer_get_string(m, NULL);
    916   1.2  christos 
    917   1.2  christos 	authok = (options.challenge_response_authentication &&
    918   1.2  christos 	    authctxt->valid &&
    919   1.2  christos 	    skey_haskey(authctxt->pw->pw_name) == 0 &&
    920   1.2  christos 	    skey_passcheck(authctxt->pw->pw_name, response) != -1);
    921   1.2  christos 
    922  1.11  christos 	free(response);
    923   1.2  christos 
    924   1.2  christos 	buffer_clear(m);
    925   1.2  christos 	buffer_put_int(m, authok);
    926   1.2  christos 
    927   1.2  christos 	debug3("%s: sending authenticated: %d", __func__, authok);
    928   1.2  christos 	mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
    929   1.2  christos 
    930   1.2  christos 	auth_method = "skey";
    931  1.20  christos 	auth_submethod = "bsdauth";
    932   1.2  christos 
    933   1.2  christos 	return (authok != 0);
    934   1.2  christos }
    935   1.2  christos #endif
    936   1.2  christos 
    937   1.2  christos #ifdef USE_PAM
    938   1.2  christos int
    939   1.2  christos mm_answer_pam_start(int sock, Buffer *m)
    940   1.2  christos {
    941   1.2  christos 	if (!options.use_pam)
    942   1.2  christos 		fatal("UsePAM not set, but ended up in %s anyway", __func__);
    943   1.2  christos 
    944   1.2  christos 	start_pam(authctxt);
    945   1.2  christos 
    946   1.2  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
    947   1.2  christos 
    948   1.2  christos 	return (0);
    949   1.2  christos }
    950   1.2  christos 
    951   1.2  christos int
    952   1.2  christos mm_answer_pam_account(int sock, Buffer *m)
    953   1.2  christos {
    954   1.2  christos 	u_int ret;
    955   1.2  christos 
    956   1.2  christos 	if (!options.use_pam)
    957   1.2  christos 		fatal("UsePAM not set, but ended up in %s anyway", __func__);
    958   1.2  christos 
    959   1.2  christos 	ret = do_pam_account();
    960   1.2  christos 
    961   1.2  christos 	buffer_put_int(m, ret);
    962   1.2  christos 	buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
    963   1.2  christos 
    964   1.2  christos 	mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
    965   1.2  christos 
    966   1.2  christos 	return (ret);
    967   1.2  christos }
    968   1.2  christos 
    969   1.2  christos static void *sshpam_ctxt, *sshpam_authok;
    970   1.2  christos extern KbdintDevice sshpam_device;
    971   1.2  christos 
    972   1.2  christos int
    973   1.2  christos mm_answer_pam_init_ctx(int sock, Buffer *m)
    974   1.2  christos {
    975   1.2  christos 	debug3("%s", __func__);
    976   1.2  christos 	sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
    977   1.2  christos 	sshpam_authok = NULL;
    978   1.2  christos 	buffer_clear(m);
    979   1.2  christos 	if (sshpam_ctxt != NULL) {
    980   1.2  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
    981   1.2  christos 		buffer_put_int(m, 1);
    982   1.2  christos 	} else {
    983   1.2  christos 		buffer_put_int(m, 0);
    984   1.2  christos 	}
    985   1.2  christos 	mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
    986   1.2  christos 	return (0);
    987   1.2  christos }
    988   1.2  christos 
    989   1.2  christos int
    990   1.2  christos mm_answer_pam_query(int sock, Buffer *m)
    991   1.2  christos {
    992   1.2  christos 	char *name, *info, **prompts;
    993   1.6  christos 	u_int i, num, *echo_on;
    994   1.6  christos 	int ret;
    995   1.2  christos 
    996   1.2  christos 	debug3("%s", __func__);
    997   1.2  christos 	sshpam_authok = NULL;
    998   1.2  christos 	ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
    999   1.2  christos 	if (ret == 0 && num == 0)
   1000   1.2  christos 		sshpam_authok = sshpam_ctxt;
   1001   1.2  christos 	if (num > 1 || name == NULL || info == NULL)
   1002   1.2  christos 		ret = -1;
   1003   1.2  christos 	buffer_clear(m);
   1004   1.2  christos 	buffer_put_int(m, ret);
   1005   1.2  christos 	buffer_put_cstring(m, name);
   1006  1.11  christos 	free(name);
   1007   1.2  christos 	buffer_put_cstring(m, info);
   1008  1.11  christos 	free(info);
   1009   1.2  christos 	buffer_put_int(m, num);
   1010   1.2  christos 	for (i = 0; i < num; ++i) {
   1011   1.2  christos 		buffer_put_cstring(m, prompts[i]);
   1012  1.11  christos 		free(prompts[i]);
   1013   1.2  christos 		buffer_put_int(m, echo_on[i]);
   1014   1.2  christos 	}
   1015   1.2  christos 	if (prompts != NULL)
   1016  1.11  christos 		free(prompts);
   1017   1.2  christos 	if (echo_on != NULL)
   1018  1.11  christos 		free(echo_on);
   1019   1.2  christos 	auth_method = "keyboard-interactive/pam";
   1020   1.2  christos 	mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
   1021   1.2  christos 	return (0);
   1022   1.2  christos }
   1023   1.1  christos 
   1024   1.2  christos int
   1025   1.2  christos mm_answer_pam_respond(int sock, Buffer *m)
   1026   1.2  christos {
   1027   1.2  christos 	char **resp;
   1028   1.6  christos 	u_int i, num;
   1029   1.6  christos 	int ret;
   1030   1.2  christos 
   1031   1.2  christos 	debug3("%s", __func__);
   1032   1.2  christos 	sshpam_authok = NULL;
   1033   1.2  christos 	num = buffer_get_int(m);
   1034   1.2  christos 	if (num > 0) {
   1035   1.2  christos 		resp = xmalloc(num * sizeof(char *));
   1036   1.2  christos 		for (i = 0; i < num; ++i)
   1037   1.2  christos 			resp[i] = buffer_get_string(m, NULL);
   1038   1.2  christos 		ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
   1039   1.2  christos 		for (i = 0; i < num; ++i)
   1040  1.11  christos 			free(resp[i]);
   1041  1.11  christos 		free(resp);
   1042   1.2  christos 	} else {
   1043   1.2  christos 		ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
   1044   1.2  christos 	}
   1045   1.2  christos 	buffer_clear(m);
   1046   1.2  christos 	buffer_put_int(m, ret);
   1047   1.2  christos 	mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
   1048   1.2  christos 	auth_method = "keyboard-interactive/pam";
   1049   1.2  christos 	if (ret == 0)
   1050   1.2  christos 		sshpam_authok = sshpam_ctxt;
   1051   1.2  christos 	return (0);
   1052   1.2  christos }
   1053   1.2  christos 
   1054   1.2  christos int
   1055   1.2  christos mm_answer_pam_free_ctx(int sock, Buffer *m)
   1056   1.2  christos {
   1057  1.16  christos 	int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
   1058   1.2  christos 
   1059   1.2  christos 	debug3("%s", __func__);
   1060   1.2  christos 	(sshpam_device.free_ctx)(sshpam_ctxt);
   1061  1.16  christos 	sshpam_ctxt = sshpam_authok = NULL;
   1062   1.2  christos 	buffer_clear(m);
   1063   1.2  christos 	mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
   1064   1.2  christos 	auth_method = "keyboard-interactive/pam";
   1065  1.16  christos 	return r;
   1066   1.2  christos }
   1067   1.2  christos #endif
   1068   1.1  christos 
   1069   1.1  christos int
   1070   1.1  christos mm_answer_keyallowed(int sock, Buffer *m)
   1071   1.1  christos {
   1072  1.23  christos 	struct sshkey *key;
   1073   1.1  christos 	char *cuser, *chost;
   1074   1.1  christos 	u_char *blob;
   1075  1.15  christos 	u_int bloblen, pubkey_auth_attempt;
   1076   1.1  christos 	enum mm_keytype type = 0;
   1077   1.1  christos 	int allowed = 0;
   1078   1.1  christos 
   1079   1.1  christos 	debug3("%s entering", __func__);
   1080   1.1  christos 
   1081   1.1  christos 	type = buffer_get_int(m);
   1082   1.1  christos 	cuser = buffer_get_string(m, NULL);
   1083   1.1  christos 	chost = buffer_get_string(m, NULL);
   1084   1.1  christos 	blob = buffer_get_string(m, &bloblen);
   1085  1.15  christos 	pubkey_auth_attempt = buffer_get_int(m);
   1086   1.1  christos 
   1087   1.1  christos 	key = key_from_blob(blob, bloblen);
   1088   1.1  christos 
   1089   1.1  christos 	debug3("%s: key_from_blob: %p", __func__, key);
   1090   1.1  christos 
   1091   1.1  christos 	if (key != NULL && authctxt->valid) {
   1092  1.14  christos 		/* These should not make it past the privsep child */
   1093  1.14  christos 		if (key_type_plain(key->type) == KEY_RSA &&
   1094  1.14  christos 		    (datafellows & SSH_BUG_RSASIGMD5) != 0)
   1095  1.14  christos 			fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
   1096  1.14  christos 
   1097   1.1  christos 		switch (type) {
   1098   1.1  christos 		case MM_USERKEY:
   1099   1.1  christos 			allowed = options.pubkey_authentication &&
   1100  1.23  christos 			    !auth2_key_already_used(authctxt, key) &&
   1101  1.14  christos 			    match_pattern_list(sshkey_ssh_name(key),
   1102  1.15  christos 			    options.pubkey_key_types, 0) == 1 &&
   1103  1.15  christos 			    user_key_allowed(authctxt->pw, key,
   1104  1.15  christos 			    pubkey_auth_attempt);
   1105   1.1  christos 			auth_method = "publickey";
   1106  1.15  christos 			if (options.pubkey_authentication &&
   1107  1.15  christos 			    (!pubkey_auth_attempt || allowed != 1))
   1108   1.1  christos 				auth_clear_options();
   1109   1.1  christos 			break;
   1110   1.1  christos 		case MM_HOSTKEY:
   1111   1.1  christos 			allowed = options.hostbased_authentication &&
   1112  1.23  christos 			    !auth2_key_already_used(authctxt, key) &&
   1113  1.14  christos 			    match_pattern_list(sshkey_ssh_name(key),
   1114  1.15  christos 			    options.hostbased_key_types, 0) == 1 &&
   1115   1.1  christos 			    hostbased_key_allowed(authctxt->pw,
   1116   1.1  christos 			    cuser, chost, key);
   1117  1.23  christos 			auth2_record_info(authctxt,
   1118  1.11  christos 			    "client user \"%.100s\", client host \"%.100s\"",
   1119  1.11  christos 			    cuser, chost);
   1120   1.1  christos 			auth_method = "hostbased";
   1121   1.1  christos 			break;
   1122   1.1  christos 		default:
   1123   1.1  christos 			fatal("%s: unknown key type %d", __func__, type);
   1124   1.1  christos 			break;
   1125   1.1  christos 		}
   1126   1.1  christos 	}
   1127  1.19  christos 
   1128  1.23  christos 	debug3("%s: key is %s", __func__, allowed ? "allowed" : "not allowed");
   1129  1.12       spz 
   1130  1.23  christos 	auth2_record_key(authctxt, 0, key);
   1131  1.23  christos 	sshkey_free(key);
   1132   1.1  christos 
   1133   1.1  christos 	/* clear temporarily storage (used by verify) */
   1134   1.1  christos 	monitor_reset_key_state();
   1135   1.1  christos 
   1136   1.1  christos 	if (allowed) {
   1137   1.1  christos 		/* Save temporarily for comparison in verify */
   1138   1.1  christos 		key_blob = blob;
   1139   1.1  christos 		key_bloblen = bloblen;
   1140   1.1  christos 		key_blobtype = type;
   1141   1.1  christos 		hostbased_cuser = cuser;
   1142   1.1  christos 		hostbased_chost = chost;
   1143   1.1  christos 	} else {
   1144   1.1  christos 		/* Log failed attempt */
   1145  1.11  christos 		auth_log(authctxt, 0, 0, auth_method, NULL);
   1146  1.11  christos 		free(blob);
   1147  1.11  christos 		free(cuser);
   1148  1.11  christos 		free(chost);
   1149   1.1  christos 	}
   1150   1.1  christos 
   1151   1.1  christos 	buffer_clear(m);
   1152   1.1  christos 	buffer_put_int(m, allowed);
   1153   1.1  christos 	buffer_put_int(m, forced_command != NULL);
   1154   1.1  christos 
   1155   1.1  christos 	mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
   1156   1.1  christos 
   1157   1.1  christos 	return (0);
   1158   1.1  christos }
   1159   1.1  christos 
   1160   1.1  christos static int
   1161   1.1  christos monitor_valid_userblob(u_char *data, u_int datalen)
   1162   1.1  christos {
   1163   1.1  christos 	Buffer b;
   1164  1.19  christos 	u_char *p;
   1165  1.19  christos 	char *userstyle, *cp;
   1166   1.1  christos 	u_int len;
   1167   1.1  christos 	int fail = 0;
   1168   1.1  christos 
   1169   1.1  christos 	buffer_init(&b);
   1170   1.1  christos 	buffer_append(&b, data, datalen);
   1171   1.1  christos 
   1172   1.1  christos 	if (datafellows & SSH_OLD_SESSIONID) {
   1173  1.19  christos 		p = buffer_ptr(&b);
   1174   1.1  christos 		len = buffer_len(&b);
   1175   1.1  christos 		if ((session_id2 == NULL) ||
   1176   1.1  christos 		    (len < session_id2_len) ||
   1177   1.4      adam 		    (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
   1178   1.1  christos 			fail++;
   1179   1.1  christos 		buffer_consume(&b, session_id2_len);
   1180   1.1  christos 	} else {
   1181   1.1  christos 		p = buffer_get_string(&b, &len);
   1182   1.1  christos 		if ((session_id2 == NULL) ||
   1183   1.1  christos 		    (len != session_id2_len) ||
   1184   1.4      adam 		    (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
   1185   1.1  christos 			fail++;
   1186  1.11  christos 		free(p);
   1187   1.1  christos 	}
   1188   1.1  christos 	if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
   1189   1.1  christos 		fail++;
   1190  1.19  christos 	cp = buffer_get_cstring(&b, NULL);
   1191  1.11  christos 	xasprintf(&userstyle, "%s%s%s", authctxt->user,
   1192  1.11  christos 	    authctxt->style ? ":" : "",
   1193  1.11  christos 	    authctxt->style ? authctxt->style : "");
   1194  1.19  christos 	if (strcmp(userstyle, cp) != 0) {
   1195  1.19  christos 		logit("wrong user name passed to monitor: "
   1196  1.19  christos 		    "expected %s != %.100s", userstyle, cp);
   1197   1.1  christos 		fail++;
   1198   1.1  christos 	}
   1199  1.11  christos 	free(userstyle);
   1200  1.19  christos 	free(cp);
   1201   1.1  christos 	buffer_skip_string(&b);
   1202   1.1  christos 	if (datafellows & SSH_BUG_PKAUTH) {
   1203   1.1  christos 		if (!buffer_get_char(&b))
   1204   1.1  christos 			fail++;
   1205   1.1  christos 	} else {
   1206  1.19  christos 		cp = buffer_get_cstring(&b, NULL);
   1207  1.19  christos 		if (strcmp("publickey", cp) != 0)
   1208   1.1  christos 			fail++;
   1209  1.19  christos 		free(cp);
   1210   1.1  christos 		if (!buffer_get_char(&b))
   1211   1.1  christos 			fail++;
   1212   1.1  christos 		buffer_skip_string(&b);
   1213   1.1  christos 	}
   1214   1.1  christos 	buffer_skip_string(&b);
   1215   1.1  christos 	if (buffer_len(&b) != 0)
   1216   1.1  christos 		fail++;
   1217   1.1  christos 	buffer_free(&b);
   1218   1.1  christos 	return (fail == 0);
   1219   1.1  christos }
   1220   1.1  christos 
   1221   1.1  christos static int
   1222   1.1  christos monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
   1223   1.1  christos     char *chost)
   1224   1.1  christos {
   1225   1.1  christos 	Buffer b;
   1226  1.11  christos 	char *p, *userstyle;
   1227   1.1  christos 	u_int len;
   1228   1.1  christos 	int fail = 0;
   1229   1.1  christos 
   1230   1.1  christos 	buffer_init(&b);
   1231   1.1  christos 	buffer_append(&b, data, datalen);
   1232   1.1  christos 
   1233   1.1  christos 	p = buffer_get_string(&b, &len);
   1234   1.1  christos 	if ((session_id2 == NULL) ||
   1235   1.1  christos 	    (len != session_id2_len) ||
   1236   1.4      adam 	    (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
   1237   1.1  christos 		fail++;
   1238  1.11  christos 	free(p);
   1239   1.1  christos 
   1240   1.1  christos 	if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
   1241   1.1  christos 		fail++;
   1242  1.11  christos 	p = buffer_get_cstring(&b, NULL);
   1243  1.11  christos 	xasprintf(&userstyle, "%s%s%s", authctxt->user,
   1244  1.11  christos 	    authctxt->style ? ":" : "",
   1245  1.11  christos 	    authctxt->style ? authctxt->style : "");
   1246  1.11  christos 	if (strcmp(userstyle, p) != 0) {
   1247   1.1  christos 		logit("wrong user name passed to monitor: expected %s != %.100s",
   1248  1.11  christos 		    userstyle, p);
   1249   1.1  christos 		fail++;
   1250   1.1  christos 	}
   1251  1.11  christos 	free(userstyle);
   1252  1.11  christos 	free(p);
   1253   1.1  christos 	buffer_skip_string(&b);	/* service */
   1254  1.11  christos 	p = buffer_get_cstring(&b, NULL);
   1255   1.1  christos 	if (strcmp(p, "hostbased") != 0)
   1256   1.1  christos 		fail++;
   1257  1.11  christos 	free(p);
   1258   1.1  christos 	buffer_skip_string(&b);	/* pkalg */
   1259   1.1  christos 	buffer_skip_string(&b);	/* pkblob */
   1260   1.1  christos 
   1261   1.1  christos 	/* verify client host, strip trailing dot if necessary */
   1262   1.1  christos 	p = buffer_get_string(&b, NULL);
   1263   1.1  christos 	if (((len = strlen(p)) > 0) && p[len - 1] == '.')
   1264   1.1  christos 		p[len - 1] = '\0';
   1265   1.1  christos 	if (strcmp(p, chost) != 0)
   1266   1.1  christos 		fail++;
   1267  1.11  christos 	free(p);
   1268   1.1  christos 
   1269   1.1  christos 	/* verify client user */
   1270   1.1  christos 	p = buffer_get_string(&b, NULL);
   1271   1.1  christos 	if (strcmp(p, cuser) != 0)
   1272   1.1  christos 		fail++;
   1273  1.11  christos 	free(p);
   1274   1.1  christos 
   1275   1.1  christos 	if (buffer_len(&b) != 0)
   1276   1.1  christos 		fail++;
   1277   1.1  christos 	buffer_free(&b);
   1278   1.1  christos 	return (fail == 0);
   1279   1.1  christos }
   1280   1.1  christos 
   1281   1.1  christos int
   1282  1.23  christos mm_answer_keyverify(int sock, struct sshbuf *m)
   1283   1.1  christos {
   1284  1.23  christos 	struct sshkey *key;
   1285   1.1  christos 	u_char *signature, *data, *blob;
   1286  1.23  christos 	size_t signaturelen, datalen, bloblen;
   1287  1.23  christos 	int r, ret, valid_data = 0, encoded_ret;
   1288   1.1  christos 
   1289  1.23  christos 	if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
   1290  1.23  christos 	    (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 ||
   1291  1.23  christos 	    (r = sshbuf_get_string(m, &data, &datalen)) != 0)
   1292  1.23  christos 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
   1293   1.1  christos 
   1294   1.1  christos 	if (hostbased_cuser == NULL || hostbased_chost == NULL ||
   1295   1.1  christos 	  !monitor_allowed_key(blob, bloblen))
   1296   1.1  christos 		fatal("%s: bad key, not previously allowed", __func__);
   1297   1.1  christos 
   1298  1.23  christos 	/* XXX use sshkey_froms here; need to change key_blob, etc. */
   1299  1.23  christos 	if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
   1300  1.23  christos 		fatal("%s: bad public key blob: %s", __func__, ssh_err(r));
   1301   1.1  christos 
   1302   1.1  christos 	switch (key_blobtype) {
   1303   1.1  christos 	case MM_USERKEY:
   1304   1.1  christos 		valid_data = monitor_valid_userblob(data, datalen);
   1305  1.23  christos 		auth_method = "publickey";
   1306   1.1  christos 		break;
   1307   1.1  christos 	case MM_HOSTKEY:
   1308   1.1  christos 		valid_data = monitor_valid_hostbasedblob(data, datalen,
   1309   1.1  christos 		    hostbased_cuser, hostbased_chost);
   1310  1.23  christos 		auth_method = "hostbased";
   1311   1.1  christos 		break;
   1312   1.1  christos 	default:
   1313   1.1  christos 		valid_data = 0;
   1314   1.1  christos 		break;
   1315   1.1  christos 	}
   1316   1.1  christos 	if (!valid_data)
   1317   1.1  christos 		fatal("%s: bad signature data blob", __func__);
   1318   1.1  christos 
   1319  1.23  christos 	ret = sshkey_verify(key, signature, signaturelen, data, datalen,
   1320  1.23  christos 	    active_state->compat);
   1321  1.23  christos 	debug3("%s: %s %p signature %s", __func__, auth_method, key,
   1322  1.23  christos 	    (ret == 0) ? "verified" : "unverified");
   1323  1.23  christos 	auth2_record_key(authctxt, ret == 0, key);
   1324  1.14  christos 
   1325  1.11  christos 	free(blob);
   1326  1.11  christos 	free(signature);
   1327  1.11  christos 	free(data);
   1328   1.1  christos 
   1329  1.23  christos 	monitor_reset_key_state();
   1330   1.1  christos 
   1331  1.23  christos 	sshkey_free(key);
   1332  1.23  christos 	sshbuf_reset(m);
   1333   1.1  christos 
   1334  1.23  christos 	/* encode ret != 0 as positive integer, since we're sending u32 */
   1335  1.23  christos 	encoded_ret = (ret != 0);
   1336  1.23  christos 	if ((r = sshbuf_put_u32(m, encoded_ret)) != 0)
   1337  1.23  christos 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
   1338   1.1  christos 	mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
   1339   1.1  christos 
   1340  1.23  christos 	return ret == 0;
   1341   1.1  christos }
   1342   1.1  christos 
   1343   1.1  christos static void
   1344   1.1  christos mm_record_login(Session *s, struct passwd *pw)
   1345   1.1  christos {
   1346  1.19  christos 	struct ssh *ssh = active_state;	/* XXX */
   1347   1.1  christos 	socklen_t fromlen;
   1348   1.1  christos 	struct sockaddr_storage from;
   1349   1.1  christos 
   1350   1.1  christos 	/*
   1351   1.1  christos 	 * Get IP address of client. If the connection is not a socket, let
   1352   1.1  christos 	 * the address be 0.0.0.0.
   1353   1.1  christos 	 */
   1354   1.1  christos 	memset(&from, 0, sizeof(from));
   1355   1.1  christos 	fromlen = sizeof(from);
   1356   1.1  christos 	if (packet_connection_is_on_socket()) {
   1357   1.1  christos 		if (getpeername(packet_get_connection_in(),
   1358   1.1  christos 		    (struct sockaddr *)&from, &fromlen) < 0) {
   1359   1.1  christos 			debug("getpeername: %.100s", strerror(errno));
   1360   1.1  christos 			cleanup_exit(255);
   1361   1.1  christos 		}
   1362   1.1  christos 	}
   1363   1.1  christos 	/* Record that there was a login on that tty from the remote host. */
   1364   1.1  christos 	record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
   1365  1.19  christos 	    session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
   1366   1.1  christos 	    (struct sockaddr *)&from, fromlen);
   1367   1.1  christos }
   1368   1.1  christos 
   1369   1.1  christos static void
   1370   1.1  christos mm_session_close(Session *s)
   1371   1.1  christos {
   1372   1.1  christos 	debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
   1373   1.1  christos 	if (s->ttyfd != -1) {
   1374   1.1  christos 		debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
   1375   1.1  christos 		session_pty_cleanup2(s);
   1376   1.1  christos 	}
   1377   1.1  christos 	session_unused(s->self);
   1378   1.1  christos }
   1379   1.1  christos 
   1380   1.1  christos int
   1381   1.1  christos mm_answer_pty(int sock, Buffer *m)
   1382   1.1  christos {
   1383   1.1  christos 	extern struct monitor *pmonitor;
   1384   1.1  christos 	Session *s;
   1385   1.1  christos 	int res, fd0;
   1386   1.1  christos 
   1387   1.1  christos 	debug3("%s entering", __func__);
   1388   1.1  christos 
   1389   1.1  christos 	buffer_clear(m);
   1390   1.1  christos 	s = session_new();
   1391   1.1  christos 	if (s == NULL)
   1392   1.1  christos 		goto error;
   1393   1.1  christos 	s->authctxt = authctxt;
   1394   1.1  christos 	s->pw = authctxt->pw;
   1395   1.1  christos 	s->pid = pmonitor->m_pid;
   1396   1.1  christos 	res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
   1397   1.1  christos 	if (res == 0)
   1398   1.1  christos 		goto error;
   1399   1.1  christos 	pty_setowner(authctxt->pw, s->tty);
   1400   1.1  christos 
   1401   1.1  christos 	buffer_put_int(m, 1);
   1402   1.1  christos 	buffer_put_cstring(m, s->tty);
   1403   1.1  christos 
   1404   1.1  christos 	/* We need to trick ttyslot */
   1405   1.1  christos 	if (dup2(s->ttyfd, 0) == -1)
   1406   1.1  christos 		fatal("%s: dup2", __func__);
   1407   1.1  christos 
   1408   1.1  christos 	mm_record_login(s, authctxt->pw);
   1409   1.1  christos 
   1410   1.1  christos 	/* Now we can close the file descriptor again */
   1411   1.1  christos 	close(0);
   1412   1.1  christos 
   1413   1.1  christos 	/* send messages generated by record_login */
   1414   1.1  christos 	buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
   1415   1.1  christos 	buffer_clear(&loginmsg);
   1416   1.1  christos 
   1417   1.1  christos 	mm_request_send(sock, MONITOR_ANS_PTY, m);
   1418   1.1  christos 
   1419   1.1  christos 	if (mm_send_fd(sock, s->ptyfd) == -1 ||
   1420   1.1  christos 	    mm_send_fd(sock, s->ttyfd) == -1)
   1421   1.1  christos 		fatal("%s: send fds failed", __func__);
   1422   1.1  christos 
   1423   1.1  christos 	/* make sure nothing uses fd 0 */
   1424   1.1  christos 	if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
   1425   1.1  christos 		fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
   1426   1.1  christos 	if (fd0 != 0)
   1427   1.1  christos 		error("%s: fd0 %d != 0", __func__, fd0);
   1428   1.1  christos 
   1429   1.1  christos 	/* slave is not needed */
   1430   1.1  christos 	close(s->ttyfd);
   1431   1.1  christos 	s->ttyfd = s->ptyfd;
   1432   1.1  christos 	/* no need to dup() because nobody closes ptyfd */
   1433   1.1  christos 	s->ptymaster = s->ptyfd;
   1434   1.1  christos 
   1435   1.1  christos 	debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
   1436   1.1  christos 
   1437   1.1  christos 	return (0);
   1438   1.1  christos 
   1439   1.1  christos  error:
   1440   1.1  christos 	if (s != NULL)
   1441   1.1  christos 		mm_session_close(s);
   1442   1.1  christos 	buffer_put_int(m, 0);
   1443   1.1  christos 	mm_request_send(sock, MONITOR_ANS_PTY, m);
   1444   1.1  christos 	return (0);
   1445   1.1  christos }
   1446   1.1  christos 
   1447   1.1  christos int
   1448   1.1  christos mm_answer_pty_cleanup(int sock, Buffer *m)
   1449   1.1  christos {
   1450   1.1  christos 	Session *s;
   1451   1.1  christos 	char *tty;
   1452   1.1  christos 
   1453   1.1  christos 	debug3("%s entering", __func__);
   1454   1.1  christos 
   1455   1.1  christos 	tty = buffer_get_string(m, NULL);
   1456   1.1  christos 	if ((s = session_by_tty(tty)) != NULL)
   1457   1.1  christos 		mm_session_close(s);
   1458   1.1  christos 	buffer_clear(m);
   1459  1.11  christos 	free(tty);
   1460   1.1  christos 	return (0);
   1461   1.1  christos }
   1462   1.1  christos 
   1463   1.2  christos #ifdef KRB5
   1464   1.2  christos int
   1465   1.6  christos mm_answer_krb5(int xsocket, Buffer *m)
   1466   1.2  christos {
   1467   1.2  christos 	krb5_data tkt, reply;
   1468   1.2  christos 	char *client_user;
   1469   1.2  christos 	u_int len;
   1470   1.2  christos 	int success;
   1471   1.2  christos 
   1472   1.2  christos 	/* use temporary var to avoid size issues on 64bit arch */
   1473   1.2  christos 	tkt.data = buffer_get_string(m, &len);
   1474   1.2  christos 	tkt.length = len;
   1475   1.2  christos 
   1476   1.2  christos 	success = options.kerberos_authentication &&
   1477   1.2  christos 	    authctxt->valid &&
   1478   1.2  christos 	    auth_krb5(authctxt, &tkt, &client_user, &reply);
   1479   1.2  christos 
   1480   1.2  christos 	if (tkt.length)
   1481  1.11  christos 		free(tkt.data);
   1482   1.2  christos 
   1483   1.2  christos 	buffer_clear(m);
   1484   1.2  christos 	buffer_put_int(m, success);
   1485   1.2  christos 
   1486   1.2  christos 	if (success) {
   1487   1.2  christos 		buffer_put_cstring(m, client_user);
   1488   1.2  christos 		buffer_put_string(m, reply.data, reply.length);
   1489   1.2  christos 		if (client_user)
   1490  1.11  christos 			free(client_user);
   1491   1.2  christos 		if (reply.length)
   1492  1.11  christos 			free(reply.data);
   1493   1.2  christos 	}
   1494   1.6  christos 	mm_request_send(xsocket, MONITOR_ANS_KRB5, m);
   1495   1.2  christos 
   1496   1.2  christos 	auth_method = "kerberos";
   1497   1.2  christos 
   1498   1.2  christos 	return success;
   1499   1.2  christos }
   1500   1.2  christos #endif
   1501   1.2  christos 
   1502   1.1  christos int
   1503   1.1  christos mm_answer_term(int sock, Buffer *req)
   1504   1.1  christos {
   1505  1.23  christos 	struct ssh *ssh = active_state;	/* XXX */
   1506   1.1  christos 	extern struct monitor *pmonitor;
   1507   1.1  christos 	int res, status;
   1508   1.1  christos 
   1509   1.1  christos 	debug3("%s: tearing down sessions", __func__);
   1510   1.1  christos 
   1511   1.1  christos 	/* The child is terminating */
   1512  1.23  christos 	session_destroy_all(ssh, &mm_session_close);
   1513   1.1  christos 
   1514   1.1  christos 	while (waitpid(pmonitor->m_pid, &status, 0) == -1)
   1515   1.1  christos 		if (errno != EINTR)
   1516   1.1  christos 			exit(1);
   1517   1.1  christos 
   1518   1.1  christos 	res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
   1519   1.1  christos 
   1520   1.1  christos 	/* Terminate process */
   1521   1.1  christos 	exit(res);
   1522   1.1  christos }
   1523   1.1  christos 
   1524   1.1  christos void
   1525  1.23  christos monitor_clear_keystate(struct monitor *pmonitor)
   1526  1.23  christos {
   1527  1.23  christos 	struct ssh *ssh = active_state;	/* XXX */
   1528  1.23  christos 
   1529  1.23  christos 	ssh_clear_newkeys(ssh, MODE_IN);
   1530  1.23  christos 	ssh_clear_newkeys(ssh, MODE_OUT);
   1531  1.23  christos 	sshbuf_free(child_state);
   1532  1.23  christos 	child_state = NULL;
   1533  1.23  christos }
   1534  1.23  christos 
   1535  1.23  christos void
   1536   1.1  christos monitor_apply_keystate(struct monitor *pmonitor)
   1537   1.1  christos {
   1538  1.14  christos 	struct ssh *ssh = active_state;	/* XXX */
   1539  1.14  christos 	struct kex *kex;
   1540  1.14  christos 	int r;
   1541  1.14  christos 
   1542  1.14  christos 	debug3("%s: packet_set_state", __func__);
   1543  1.14  christos 	if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
   1544  1.14  christos                 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
   1545  1.14  christos 	sshbuf_free(child_state);
   1546  1.14  christos 	child_state = NULL;
   1547  1.14  christos 
   1548  1.18  christos 	if ((kex = ssh->kex) != NULL) {
   1549  1.14  christos 		/* XXX set callbacks */
   1550  1.15  christos #ifdef WITH_OPENSSL
   1551  1.14  christos 		kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
   1552  1.14  christos 		kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
   1553  1.19  christos 		kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
   1554  1.19  christos 		kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
   1555  1.19  christos 		kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
   1556  1.14  christos 		kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
   1557  1.14  christos 		kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
   1558  1.14  christos 		kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
   1559  1.15  christos #endif
   1560  1.14  christos 		kex->kex[KEX_C25519_SHA256] = kexc25519_server;
   1561  1.14  christos 		kex->load_host_public_key=&get_hostkey_public_by_type;
   1562  1.14  christos 		kex->load_host_private_key=&get_hostkey_private_by_type;
   1563  1.14  christos 		kex->host_key_index=&get_hostkey_index;
   1564  1.14  christos 		kex->sign = sshd_hostkey_sign;
   1565  1.14  christos 	}
   1566   1.1  christos }
   1567   1.1  christos 
   1568   1.1  christos /* This function requries careful sanity checking */
   1569   1.1  christos 
   1570   1.1  christos void
   1571   1.1  christos mm_get_keystate(struct monitor *pmonitor)
   1572   1.1  christos {
   1573   1.1  christos 	debug3("%s: Waiting for new keys", __func__);
   1574   1.1  christos 
   1575  1.14  christos 	if ((child_state = sshbuf_new()) == NULL)
   1576  1.14  christos 		fatal("%s: sshbuf_new failed", __func__);
   1577  1.14  christos 	mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
   1578  1.14  christos 	    child_state);
   1579  1.14  christos 	debug3("%s: GOT new keys", __func__);
   1580   1.1  christos }
   1581   1.1  christos 
   1582   1.1  christos 
   1583   1.1  christos /* XXX */
   1584   1.1  christos 
   1585   1.1  christos #define FD_CLOSEONEXEC(x) do { \
   1586   1.7  christos 	if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
   1587   1.1  christos 		fatal("fcntl(%d, F_SETFD)", x); \
   1588   1.1  christos } while (0)
   1589   1.1  christos 
   1590   1.1  christos static void
   1591   1.7  christos monitor_openfds(struct monitor *mon, int do_logfds)
   1592   1.1  christos {
   1593   1.7  christos 	int pair[2];
   1594  1.23  christos #ifdef SO_ZEROIZE
   1595  1.23  christos 	int on = 1;
   1596  1.23  christos #endif
   1597   1.7  christos 
   1598   1.1  christos 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
   1599   1.7  christos 		fatal("%s: socketpair: %s", __func__, strerror(errno));
   1600  1.23  christos #ifdef SO_ZEROIZE
   1601  1.23  christos 	if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
   1602  1.23  christos 		error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
   1603  1.23  christos 	if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
   1604  1.23  christos 		error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
   1605  1.23  christos #endif
   1606   1.1  christos 	FD_CLOSEONEXEC(pair[0]);
   1607   1.1  christos 	FD_CLOSEONEXEC(pair[1]);
   1608   1.7  christos 	mon->m_recvfd = pair[0];
   1609   1.7  christos 	mon->m_sendfd = pair[1];
   1610   1.7  christos 
   1611   1.7  christos 	if (do_logfds) {
   1612   1.7  christos 		if (pipe(pair) == -1)
   1613   1.7  christos 			fatal("%s: pipe: %s", __func__, strerror(errno));
   1614   1.7  christos 		FD_CLOSEONEXEC(pair[0]);
   1615   1.7  christos 		FD_CLOSEONEXEC(pair[1]);
   1616   1.7  christos 		mon->m_log_recvfd = pair[0];
   1617   1.7  christos 		mon->m_log_sendfd = pair[1];
   1618   1.7  christos 	} else
   1619   1.7  christos 		mon->m_log_recvfd = mon->m_log_sendfd = -1;
   1620   1.1  christos }
   1621   1.1  christos 
   1622   1.1  christos #define MM_MEMSIZE	65536
   1623   1.1  christos 
   1624   1.1  christos struct monitor *
   1625   1.1  christos monitor_init(void)
   1626   1.1  christos {
   1627   1.1  christos 	struct monitor *mon;
   1628   1.1  christos 
   1629   1.1  christos 	mon = xcalloc(1, sizeof(*mon));
   1630   1.7  christos 	monitor_openfds(mon, 1);
   1631   1.1  christos 
   1632   1.1  christos 	return mon;
   1633   1.1  christos }
   1634   1.1  christos 
   1635   1.1  christos void
   1636   1.1  christos monitor_reinit(struct monitor *mon)
   1637   1.1  christos {
   1638   1.7  christos 	monitor_openfds(mon, 0);
   1639   1.1  christos }
   1640   1.1  christos 
   1641   1.1  christos #ifdef GSSAPI
   1642   1.1  christos int
   1643   1.1  christos mm_answer_gss_setup_ctx(int sock, Buffer *m)
   1644   1.1  christos {
   1645   1.1  christos 	gss_OID_desc goid;
   1646   1.1  christos 	OM_uint32 major;
   1647   1.1  christos 	u_int len;
   1648   1.1  christos 
   1649  1.20  christos 	if (!options.gss_authentication)
   1650  1.20  christos 		fatal("%s: GSSAPI authentication not enabled", __func__);
   1651  1.20  christos 
   1652   1.1  christos 	goid.elements = buffer_get_string(m, &len);
   1653   1.1  christos 	goid.length = len;
   1654   1.1  christos 
   1655   1.1  christos 	major = ssh_gssapi_server_ctx(&gsscontext, &goid);
   1656   1.1  christos 
   1657  1.11  christos 	free(goid.elements);
   1658   1.1  christos 
   1659   1.1  christos 	buffer_clear(m);
   1660   1.1  christos 	buffer_put_int(m, major);
   1661   1.1  christos 
   1662   1.1  christos 	mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
   1663   1.1  christos 
   1664   1.1  christos 	/* Now we have a context, enable the step */
   1665   1.1  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
   1666   1.1  christos 
   1667   1.1  christos 	return (0);
   1668   1.1  christos }
   1669   1.1  christos 
   1670   1.1  christos int
   1671   1.1  christos mm_answer_gss_accept_ctx(int sock, Buffer *m)
   1672   1.1  christos {
   1673   1.1  christos 	gss_buffer_desc in;
   1674   1.1  christos 	gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
   1675   1.1  christos 	OM_uint32 major, minor;
   1676   1.1  christos 	OM_uint32 flags = 0; /* GSI needs this */
   1677   1.1  christos 	u_int len;
   1678   1.1  christos 
   1679  1.20  christos 	if (!options.gss_authentication)
   1680  1.20  christos 		fatal("%s: GSSAPI authentication not enabled", __func__);
   1681  1.20  christos 
   1682   1.1  christos 	in.value = buffer_get_string(m, &len);
   1683   1.1  christos 	in.length = len;
   1684   1.1  christos 	major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
   1685  1.11  christos 	free(in.value);
   1686   1.1  christos 
   1687   1.1  christos 	buffer_clear(m);
   1688   1.1  christos 	buffer_put_int(m, major);
   1689   1.1  christos 	buffer_put_string(m, out.value, out.length);
   1690   1.1  christos 	buffer_put_int(m, flags);
   1691   1.1  christos 	mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
   1692   1.1  christos 
   1693   1.1  christos 	gss_release_buffer(&minor, &out);
   1694   1.1  christos 
   1695   1.1  christos 	if (major == GSS_S_COMPLETE) {
   1696   1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
   1697   1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
   1698   1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
   1699   1.1  christos 	}
   1700   1.1  christos 	return (0);
   1701   1.1  christos }
   1702   1.1  christos 
   1703   1.1  christos int
   1704   1.1  christos mm_answer_gss_checkmic(int sock, Buffer *m)
   1705   1.1  christos {
   1706   1.1  christos 	gss_buffer_desc gssbuf, mic;
   1707   1.1  christos 	OM_uint32 ret;
   1708   1.1  christos 	u_int len;
   1709   1.1  christos 
   1710  1.20  christos 	if (!options.gss_authentication)
   1711  1.20  christos 		fatal("%s: GSSAPI authentication not enabled", __func__);
   1712  1.20  christos 
   1713   1.1  christos 	gssbuf.value = buffer_get_string(m, &len);
   1714   1.1  christos 	gssbuf.length = len;
   1715   1.1  christos 	mic.value = buffer_get_string(m, &len);
   1716   1.1  christos 	mic.length = len;
   1717   1.1  christos 
   1718   1.1  christos 	ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
   1719   1.1  christos 
   1720  1.11  christos 	free(gssbuf.value);
   1721  1.11  christos 	free(mic.value);
   1722   1.1  christos 
   1723   1.1  christos 	buffer_clear(m);
   1724   1.1  christos 	buffer_put_int(m, ret);
   1725   1.1  christos 
   1726   1.1  christos 	mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
   1727   1.1  christos 
   1728   1.1  christos 	if (!GSS_ERROR(ret))
   1729   1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
   1730   1.1  christos 
   1731   1.1  christos 	return (0);
   1732   1.1  christos }
   1733   1.1  christos 
   1734   1.1  christos int
   1735   1.1  christos mm_answer_gss_userok(int sock, Buffer *m)
   1736   1.1  christos {
   1737   1.1  christos 	int authenticated;
   1738  1.23  christos 	const char *displayname;
   1739   1.1  christos 
   1740  1.20  christos 	if (!options.gss_authentication)
   1741  1.20  christos 		fatal("%s: GSSAPI authentication not enabled", __func__);
   1742  1.20  christos 
   1743   1.1  christos 	authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
   1744   1.1  christos 
   1745   1.1  christos 	buffer_clear(m);
   1746   1.1  christos 	buffer_put_int(m, authenticated);
   1747   1.1  christos 
   1748   1.1  christos 	debug3("%s: sending result %d", __func__, authenticated);
   1749   1.1  christos 	mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
   1750   1.1  christos 
   1751   1.1  christos 	auth_method = "gssapi-with-mic";
   1752   1.1  christos 
   1753  1.23  christos 	if ((displayname = ssh_gssapi_displayname()) != NULL)
   1754  1.23  christos 		auth2_record_info(authctxt, "%s", displayname);
   1755  1.23  christos 
   1756   1.1  christos 	/* Monitor loop will terminate if authenticated */
   1757   1.1  christos 	return (authenticated);
   1758   1.1  christos }
   1759   1.1  christos #endif /* GSSAPI */
   1760   1.1  christos 
   1761