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