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