Home | History | Annotate | Line # | Download | only in dist
monitor.c revision 1.1
      1  1.1  christos /*	$NetBSD: monitor.c,v 1.1 2009/06/07 22:19:12 christos Exp $	*/
      2  1.1  christos /* $OpenBSD: monitor.c,v 1.101 2009/02/12 03:26:22 djm Exp $ */
      3  1.1  christos /*
      4  1.1  christos  * Copyright 2002 Niels Provos <provos (at) citi.umich.edu>
      5  1.1  christos  * Copyright 2002 Markus Friedl <markus (at) openbsd.org>
      6  1.1  christos  * All rights reserved.
      7  1.1  christos  *
      8  1.1  christos  * Redistribution and use in source and binary forms, with or without
      9  1.1  christos  * modification, are permitted provided that the following conditions
     10  1.1  christos  * are met:
     11  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     12  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     13  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  christos  *    documentation and/or other materials provided with the distribution.
     16  1.1  christos  *
     17  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1  christos  */
     28  1.1  christos 
     29  1.1  christos #include <sys/types.h>
     30  1.1  christos #include <sys/wait.h>
     31  1.1  christos #include <sys/socket.h>
     32  1.1  christos #include <sys/tree.h>
     33  1.1  christos #include <sys/param.h>
     34  1.1  christos #include <sys/queue.h>
     35  1.1  christos 
     36  1.1  christos #include <openssl/dh.h>
     37  1.1  christos 
     38  1.1  christos #include <errno.h>
     39  1.1  christos #include <fcntl.h>
     40  1.1  christos #include <paths.h>
     41  1.1  christos #include <pwd.h>
     42  1.1  christos #include <signal.h>
     43  1.1  christos #include <stdlib.h>
     44  1.1  christos #include <string.h>
     45  1.1  christos 
     46  1.1  christos 
     47  1.1  christos #include "xmalloc.h"
     48  1.1  christos #include "ssh.h"
     49  1.1  christos #include "key.h"
     50  1.1  christos #include "buffer.h"
     51  1.1  christos #include "hostfile.h"
     52  1.1  christos #include "auth.h"
     53  1.1  christos #include "cipher.h"
     54  1.1  christos #include "kex.h"
     55  1.1  christos #include "dh.h"
     56  1.1  christos #include <zlib.h>
     57  1.1  christos #include "packet.h"
     58  1.1  christos #include "auth-options.h"
     59  1.1  christos #include "sshpty.h"
     60  1.1  christos #include "channels.h"
     61  1.1  christos #include "session.h"
     62  1.1  christos #include "sshlogin.h"
     63  1.1  christos #include "canohost.h"
     64  1.1  christos #include "log.h"
     65  1.1  christos #include "servconf.h"
     66  1.1  christos #include "monitor.h"
     67  1.1  christos #include "monitor_mm.h"
     68  1.1  christos #ifdef GSSAPI
     69  1.1  christos #include "ssh-gss.h"
     70  1.1  christos #endif
     71  1.1  christos #include "monitor_wrap.h"
     72  1.1  christos #include "monitor_fdpass.h"
     73  1.1  christos #include "misc.h"
     74  1.1  christos #include "compat.h"
     75  1.1  christos #include "ssh2.h"
     76  1.1  christos #include "jpake.h"
     77  1.1  christos 
     78  1.1  christos #ifdef GSSAPI
     79  1.1  christos static Gssctxt *gsscontext = NULL;
     80  1.1  christos #endif
     81  1.1  christos 
     82  1.1  christos /* Imports */
     83  1.1  christos extern ServerOptions options;
     84  1.1  christos extern u_int utmp_len;
     85  1.1  christos extern Newkeys *current_keys[];
     86  1.1  christos extern z_stream incoming_stream;
     87  1.1  christos extern z_stream outgoing_stream;
     88  1.1  christos extern u_char session_id[];
     89  1.1  christos extern Buffer input, output;
     90  1.1  christos extern Buffer auth_debug;
     91  1.1  christos extern int auth_debug_init;
     92  1.1  christos extern Buffer loginmsg;
     93  1.1  christos 
     94  1.1  christos /* State exported from the child */
     95  1.1  christos 
     96  1.1  christos struct {
     97  1.1  christos 	z_stream incoming;
     98  1.1  christos 	z_stream outgoing;
     99  1.1  christos 	u_char *keyin;
    100  1.1  christos 	u_int keyinlen;
    101  1.1  christos 	u_char *keyout;
    102  1.1  christos 	u_int keyoutlen;
    103  1.1  christos 	u_char *ivin;
    104  1.1  christos 	u_int ivinlen;
    105  1.1  christos 	u_char *ivout;
    106  1.1  christos 	u_int ivoutlen;
    107  1.1  christos 	u_char *ssh1key;
    108  1.1  christos 	u_int ssh1keylen;
    109  1.1  christos 	int ssh1cipher;
    110  1.1  christos 	int ssh1protoflags;
    111  1.1  christos 	u_char *input;
    112  1.1  christos 	u_int ilen;
    113  1.1  christos 	u_char *output;
    114  1.1  christos 	u_int olen;
    115  1.1  christos } child_state;
    116  1.1  christos 
    117  1.1  christos /* Functions on the monitor that answer unprivileged requests */
    118  1.1  christos 
    119  1.1  christos int mm_answer_moduli(int, Buffer *);
    120  1.1  christos int mm_answer_sign(int, Buffer *);
    121  1.1  christos int mm_answer_pwnamallow(int, Buffer *);
    122  1.1  christos int mm_answer_auth2_read_banner(int, Buffer *);
    123  1.1  christos int mm_answer_authserv(int, Buffer *);
    124  1.1  christos int mm_answer_authpassword(int, Buffer *);
    125  1.1  christos int mm_answer_bsdauthquery(int, Buffer *);
    126  1.1  christos int mm_answer_bsdauthrespond(int, Buffer *);
    127  1.1  christos int mm_answer_skeyquery(int, Buffer *);
    128  1.1  christos int mm_answer_skeyrespond(int, Buffer *);
    129  1.1  christos int mm_answer_keyallowed(int, Buffer *);
    130  1.1  christos int mm_answer_keyverify(int, Buffer *);
    131  1.1  christos int mm_answer_pty(int, Buffer *);
    132  1.1  christos int mm_answer_pty_cleanup(int, Buffer *);
    133  1.1  christos int mm_answer_term(int, Buffer *);
    134  1.1  christos int mm_answer_rsa_keyallowed(int, Buffer *);
    135  1.1  christos int mm_answer_rsa_challenge(int, Buffer *);
    136  1.1  christos int mm_answer_rsa_response(int, Buffer *);
    137  1.1  christos int mm_answer_sesskey(int, Buffer *);
    138  1.1  christos int mm_answer_sessid(int, Buffer *);
    139  1.1  christos int mm_answer_jpake_get_pwdata(int, Buffer *);
    140  1.1  christos int mm_answer_jpake_step1(int, Buffer *);
    141  1.1  christos int mm_answer_jpake_step2(int, Buffer *);
    142  1.1  christos int mm_answer_jpake_key_confirm(int, Buffer *);
    143  1.1  christos int mm_answer_jpake_check_confirm(int, Buffer *);
    144  1.1  christos 
    145  1.1  christos #ifdef GSSAPI
    146  1.1  christos int mm_answer_gss_setup_ctx(int, Buffer *);
    147  1.1  christos int mm_answer_gss_accept_ctx(int, Buffer *);
    148  1.1  christos int mm_answer_gss_userok(int, Buffer *);
    149  1.1  christos int mm_answer_gss_checkmic(int, Buffer *);
    150  1.1  christos #endif
    151  1.1  christos 
    152  1.1  christos static Authctxt *authctxt;
    153  1.1  christos static BIGNUM *ssh1_challenge = NULL;	/* used for ssh1 rsa auth */
    154  1.1  christos 
    155  1.1  christos /* local state for key verify */
    156  1.1  christos static u_char *key_blob = NULL;
    157  1.1  christos static u_int key_bloblen = 0;
    158  1.1  christos static int key_blobtype = MM_NOKEY;
    159  1.1  christos static char *hostbased_cuser = NULL;
    160  1.1  christos static char *hostbased_chost = NULL;
    161  1.1  christos static char *auth_method = "unknown";
    162  1.1  christos static u_int session_id2_len = 0;
    163  1.1  christos static u_char *session_id2 = NULL;
    164  1.1  christos static pid_t monitor_child_pid;
    165  1.1  christos 
    166  1.1  christos struct mon_table {
    167  1.1  christos 	enum monitor_reqtype type;
    168  1.1  christos 	int flags;
    169  1.1  christos 	int (*f)(int, Buffer *);
    170  1.1  christos };
    171  1.1  christos 
    172  1.1  christos #define MON_ISAUTH	0x0004	/* Required for Authentication */
    173  1.1  christos #define MON_AUTHDECIDE	0x0008	/* Decides Authentication */
    174  1.1  christos #define MON_ONCE	0x0010	/* Disable after calling */
    175  1.1  christos #define MON_ALOG	0x0020	/* Log auth attempt without authenticating */
    176  1.1  christos 
    177  1.1  christos #define MON_AUTH	(MON_ISAUTH|MON_AUTHDECIDE)
    178  1.1  christos 
    179  1.1  christos #define MON_PERMIT	0x1000	/* Request is permitted */
    180  1.1  christos 
    181  1.1  christos struct mon_table mon_dispatch_proto20[] = {
    182  1.1  christos     {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
    183  1.1  christos     {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
    184  1.1  christos     {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
    185  1.1  christos     {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
    186  1.1  christos     {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
    187  1.1  christos     {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
    188  1.1  christos     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
    189  1.1  christos     {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
    190  1.1  christos     {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
    191  1.1  christos     {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
    192  1.1  christos #ifdef GSSAPI
    193  1.1  christos     {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
    194  1.1  christos     {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
    195  1.1  christos     {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
    196  1.1  christos     {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
    197  1.1  christos #endif
    198  1.1  christos #ifdef JPAKE
    199  1.1  christos     {MONITOR_REQ_JPAKE_GET_PWDATA, MON_ONCE, mm_answer_jpake_get_pwdata},
    200  1.1  christos     {MONITOR_REQ_JPAKE_STEP1, MON_ISAUTH, mm_answer_jpake_step1},
    201  1.1  christos     {MONITOR_REQ_JPAKE_STEP2, MON_ONCE, mm_answer_jpake_step2},
    202  1.1  christos     {MONITOR_REQ_JPAKE_KEY_CONFIRM, MON_ONCE, mm_answer_jpake_key_confirm},
    203  1.1  christos     {MONITOR_REQ_JPAKE_CHECK_CONFIRM, MON_AUTH, mm_answer_jpake_check_confirm},
    204  1.1  christos #endif
    205  1.1  christos     {0, 0, NULL}
    206  1.1  christos };
    207  1.1  christos 
    208  1.1  christos struct mon_table mon_dispatch_postauth20[] = {
    209  1.1  christos     {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
    210  1.1  christos     {MONITOR_REQ_SIGN, 0, mm_answer_sign},
    211  1.1  christos     {MONITOR_REQ_PTY, 0, mm_answer_pty},
    212  1.1  christos     {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
    213  1.1  christos     {MONITOR_REQ_TERM, 0, mm_answer_term},
    214  1.1  christos     {0, 0, NULL}
    215  1.1  christos };
    216  1.1  christos 
    217  1.1  christos struct mon_table mon_dispatch_proto15[] = {
    218  1.1  christos     {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
    219  1.1  christos     {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
    220  1.1  christos     {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
    221  1.1  christos     {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
    222  1.1  christos     {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
    223  1.1  christos     {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
    224  1.1  christos     {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
    225  1.1  christos     {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
    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.1  christos     {0, 0, NULL}
    229  1.1  christos };
    230  1.1  christos 
    231  1.1  christos struct mon_table mon_dispatch_postauth15[] = {
    232  1.1  christos     {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
    233  1.1  christos     {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
    234  1.1  christos     {MONITOR_REQ_TERM, 0, mm_answer_term},
    235  1.1  christos     {0, 0, NULL}
    236  1.1  christos };
    237  1.1  christos 
    238  1.1  christos struct mon_table *mon_dispatch;
    239  1.1  christos 
    240  1.1  christos /* Specifies if a certain message is allowed at the moment */
    241  1.1  christos 
    242  1.1  christos static void
    243  1.1  christos monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
    244  1.1  christos {
    245  1.1  christos 	while (ent->f != NULL) {
    246  1.1  christos 		if (ent->type == type) {
    247  1.1  christos 			ent->flags &= ~MON_PERMIT;
    248  1.1  christos 			ent->flags |= permit ? MON_PERMIT : 0;
    249  1.1  christos 			return;
    250  1.1  christos 		}
    251  1.1  christos 		ent++;
    252  1.1  christos 	}
    253  1.1  christos }
    254  1.1  christos 
    255  1.1  christos static void
    256  1.1  christos monitor_permit_authentications(int permit)
    257  1.1  christos {
    258  1.1  christos 	struct mon_table *ent = mon_dispatch;
    259  1.1  christos 
    260  1.1  christos 	while (ent->f != NULL) {
    261  1.1  christos 		if (ent->flags & MON_AUTH) {
    262  1.1  christos 			ent->flags &= ~MON_PERMIT;
    263  1.1  christos 			ent->flags |= permit ? MON_PERMIT : 0;
    264  1.1  christos 		}
    265  1.1  christos 		ent++;
    266  1.1  christos 	}
    267  1.1  christos }
    268  1.1  christos 
    269  1.1  christos void
    270  1.1  christos monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
    271  1.1  christos {
    272  1.1  christos 	struct mon_table *ent;
    273  1.1  christos 	int authenticated = 0;
    274  1.1  christos 
    275  1.1  christos 	debug3("preauth child monitor started");
    276  1.1  christos 
    277  1.1  christos 	authctxt = _authctxt;
    278  1.1  christos 	memset(authctxt, 0, sizeof(*authctxt));
    279  1.1  christos 
    280  1.1  christos 	if (compat20) {
    281  1.1  christos 		mon_dispatch = mon_dispatch_proto20;
    282  1.1  christos 
    283  1.1  christos 		/* Permit requests for moduli and signatures */
    284  1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
    285  1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
    286  1.1  christos 	} else {
    287  1.1  christos 		mon_dispatch = mon_dispatch_proto15;
    288  1.1  christos 
    289  1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
    290  1.1  christos 	}
    291  1.1  christos 
    292  1.1  christos 	/* The first few requests do not require asynchronous access */
    293  1.1  christos 	while (!authenticated) {
    294  1.1  christos 		auth_method = "unknown";
    295  1.1  christos 		authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
    296  1.1  christos 		if (authenticated) {
    297  1.1  christos 			if (!(ent->flags & MON_AUTHDECIDE))
    298  1.1  christos 				fatal("%s: unexpected authentication from %d",
    299  1.1  christos 				    __func__, ent->type);
    300  1.1  christos 			if (authctxt->pw->pw_uid == 0 &&
    301  1.1  christos 			    !auth_root_allowed(auth_method))
    302  1.1  christos 				authenticated = 0;
    303  1.1  christos 		}
    304  1.1  christos 
    305  1.1  christos 		if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
    306  1.1  christos 			auth_log(authctxt, authenticated, auth_method,
    307  1.1  christos 			    compat20 ? " ssh2" : "");
    308  1.1  christos 			if (!authenticated)
    309  1.1  christos 				authctxt->failures++;
    310  1.1  christos 		}
    311  1.1  christos #ifdef JPAKE
    312  1.1  christos 		/* Cleanup JPAKE context after authentication */
    313  1.1  christos 		if (ent->flags & MON_AUTHDECIDE) {
    314  1.1  christos 			if (authctxt->jpake_ctx != NULL) {
    315  1.1  christos 				jpake_free(authctxt->jpake_ctx);
    316  1.1  christos 				authctxt->jpake_ctx = NULL;
    317  1.1  christos 			}
    318  1.1  christos 		}
    319  1.1  christos #endif
    320  1.1  christos 	}
    321  1.1  christos 
    322  1.1  christos 	if (!authctxt->valid)
    323  1.1  christos 		fatal("%s: authenticated invalid user", __func__);
    324  1.1  christos 	if (strcmp(auth_method, "unknown") == 0)
    325  1.1  christos 		fatal("%s: authentication method name unknown", __func__);
    326  1.1  christos 
    327  1.1  christos 	debug("%s: %s has been authenticated by privileged process",
    328  1.1  christos 	    __func__, authctxt->user);
    329  1.1  christos 
    330  1.1  christos 	mm_get_keystate(pmonitor);
    331  1.1  christos }
    332  1.1  christos 
    333  1.1  christos static void
    334  1.1  christos monitor_set_child_handler(pid_t pid)
    335  1.1  christos {
    336  1.1  christos 	monitor_child_pid = pid;
    337  1.1  christos }
    338  1.1  christos 
    339  1.1  christos static void
    340  1.1  christos monitor_child_handler(int sig)
    341  1.1  christos {
    342  1.1  christos 	kill(monitor_child_pid, sig);
    343  1.1  christos }
    344  1.1  christos 
    345  1.1  christos void
    346  1.1  christos monitor_child_postauth(struct monitor *pmonitor)
    347  1.1  christos {
    348  1.1  christos 	monitor_set_child_handler(pmonitor->m_pid);
    349  1.1  christos 	signal(SIGHUP, &monitor_child_handler);
    350  1.1  christos 	signal(SIGTERM, &monitor_child_handler);
    351  1.1  christos 	signal(SIGINT, &monitor_child_handler);
    352  1.1  christos 
    353  1.1  christos 	if (compat20) {
    354  1.1  christos 		mon_dispatch = mon_dispatch_postauth20;
    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 		monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
    360  1.1  christos 	} else {
    361  1.1  christos 		mon_dispatch = mon_dispatch_postauth15;
    362  1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
    363  1.1  christos 	}
    364  1.1  christos 	if (!no_pty_flag) {
    365  1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
    366  1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
    367  1.1  christos 	}
    368  1.1  christos 
    369  1.1  christos 	for (;;)
    370  1.1  christos 		monitor_read(pmonitor, mon_dispatch, NULL);
    371  1.1  christos }
    372  1.1  christos 
    373  1.1  christos void
    374  1.1  christos monitor_sync(struct monitor *pmonitor)
    375  1.1  christos {
    376  1.1  christos 	if (options.compression) {
    377  1.1  christos 		/* The member allocation is not visible, so sync it */
    378  1.1  christos 		mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
    379  1.1  christos 	}
    380  1.1  christos }
    381  1.1  christos 
    382  1.1  christos int
    383  1.1  christos monitor_read(struct monitor *pmonitor, struct mon_table *ent,
    384  1.1  christos     struct mon_table **pent)
    385  1.1  christos {
    386  1.1  christos 	Buffer m;
    387  1.1  christos 	int ret;
    388  1.1  christos 	u_char type;
    389  1.1  christos 
    390  1.1  christos 	buffer_init(&m);
    391  1.1  christos 
    392  1.1  christos 	mm_request_receive(pmonitor->m_sendfd, &m);
    393  1.1  christos 	type = buffer_get_char(&m);
    394  1.1  christos 
    395  1.1  christos 	debug3("%s: checking request %d", __func__, type);
    396  1.1  christos 
    397  1.1  christos 	while (ent->f != NULL) {
    398  1.1  christos 		if (ent->type == type)
    399  1.1  christos 			break;
    400  1.1  christos 		ent++;
    401  1.1  christos 	}
    402  1.1  christos 
    403  1.1  christos 	if (ent->f != NULL) {
    404  1.1  christos 		if (!(ent->flags & MON_PERMIT))
    405  1.1  christos 			fatal("%s: unpermitted request %d", __func__,
    406  1.1  christos 			    type);
    407  1.1  christos 		ret = (*ent->f)(pmonitor->m_sendfd, &m);
    408  1.1  christos 		buffer_free(&m);
    409  1.1  christos 
    410  1.1  christos 		/* The child may use this request only once, disable it */
    411  1.1  christos 		if (ent->flags & MON_ONCE) {
    412  1.1  christos 			debug2("%s: %d used once, disabling now", __func__,
    413  1.1  christos 			    type);
    414  1.1  christos 			ent->flags &= ~MON_PERMIT;
    415  1.1  christos 		}
    416  1.1  christos 
    417  1.1  christos 		if (pent != NULL)
    418  1.1  christos 			*pent = ent;
    419  1.1  christos 
    420  1.1  christos 		return ret;
    421  1.1  christos 	}
    422  1.1  christos 
    423  1.1  christos 	fatal("%s: unsupported request: %d", __func__, type);
    424  1.1  christos 
    425  1.1  christos 	/* NOTREACHED */
    426  1.1  christos 	return (-1);
    427  1.1  christos }
    428  1.1  christos 
    429  1.1  christos /* allowed key state */
    430  1.1  christos static int
    431  1.1  christos monitor_allowed_key(u_char *blob, u_int bloblen)
    432  1.1  christos {
    433  1.1  christos 	/* make sure key is allowed */
    434  1.1  christos 	if (key_blob == NULL || key_bloblen != bloblen ||
    435  1.1  christos 	    memcmp(key_blob, blob, key_bloblen))
    436  1.1  christos 		return (0);
    437  1.1  christos 	return (1);
    438  1.1  christos }
    439  1.1  christos 
    440  1.1  christos static void
    441  1.1  christos monitor_reset_key_state(void)
    442  1.1  christos {
    443  1.1  christos 	/* reset state */
    444  1.1  christos 	if (key_blob != NULL)
    445  1.1  christos 		xfree(key_blob);
    446  1.1  christos 	if (hostbased_cuser != NULL)
    447  1.1  christos 		xfree(hostbased_cuser);
    448  1.1  christos 	if (hostbased_chost != NULL)
    449  1.1  christos 		xfree(hostbased_chost);
    450  1.1  christos 	key_blob = NULL;
    451  1.1  christos 	key_bloblen = 0;
    452  1.1  christos 	key_blobtype = MM_NOKEY;
    453  1.1  christos 	hostbased_cuser = NULL;
    454  1.1  christos 	hostbased_chost = NULL;
    455  1.1  christos }
    456  1.1  christos 
    457  1.1  christos int
    458  1.1  christos mm_answer_moduli(int sock, Buffer *m)
    459  1.1  christos {
    460  1.1  christos 	DH *dh;
    461  1.1  christos 	int min, want, max;
    462  1.1  christos 
    463  1.1  christos 	min = buffer_get_int(m);
    464  1.1  christos 	want = buffer_get_int(m);
    465  1.1  christos 	max = buffer_get_int(m);
    466  1.1  christos 
    467  1.1  christos 	debug3("%s: got parameters: %d %d %d",
    468  1.1  christos 	    __func__, min, want, max);
    469  1.1  christos 	/* We need to check here, too, in case the child got corrupted */
    470  1.1  christos 	if (max < min || want < min || max < want)
    471  1.1  christos 		fatal("%s: bad parameters: %d %d %d",
    472  1.1  christos 		    __func__, min, want, max);
    473  1.1  christos 
    474  1.1  christos 	buffer_clear(m);
    475  1.1  christos 
    476  1.1  christos 	dh = choose_dh(min, want, max);
    477  1.1  christos 	if (dh == NULL) {
    478  1.1  christos 		buffer_put_char(m, 0);
    479  1.1  christos 		return (0);
    480  1.1  christos 	} else {
    481  1.1  christos 		/* Send first bignum */
    482  1.1  christos 		buffer_put_char(m, 1);
    483  1.1  christos 		buffer_put_bignum2(m, dh->p);
    484  1.1  christos 		buffer_put_bignum2(m, dh->g);
    485  1.1  christos 
    486  1.1  christos 		DH_free(dh);
    487  1.1  christos 	}
    488  1.1  christos 	mm_request_send(sock, MONITOR_ANS_MODULI, m);
    489  1.1  christos 	return (0);
    490  1.1  christos }
    491  1.1  christos 
    492  1.1  christos int
    493  1.1  christos mm_answer_sign(int sock, Buffer *m)
    494  1.1  christos {
    495  1.1  christos 	Key *key;
    496  1.1  christos 	u_char *p;
    497  1.1  christos 	u_char *signature;
    498  1.1  christos 	u_int siglen, datlen;
    499  1.1  christos 	int keyid;
    500  1.1  christos 
    501  1.1  christos 	debug3("%s", __func__);
    502  1.1  christos 
    503  1.1  christos 	keyid = buffer_get_int(m);
    504  1.1  christos 	p = buffer_get_string(m, &datlen);
    505  1.1  christos 
    506  1.1  christos 	/*
    507  1.1  christos 	 * Supported KEX types will only return SHA1 (20 byte) or
    508  1.1  christos 	 * SHA256 (32 byte) hashes
    509  1.1  christos 	 */
    510  1.1  christos 	if (datlen != 20 && datlen != 32)
    511  1.1  christos 		fatal("%s: data length incorrect: %u", __func__, datlen);
    512  1.1  christos 
    513  1.1  christos 	/* save session id, it will be passed on the first call */
    514  1.1  christos 	if (session_id2_len == 0) {
    515  1.1  christos 		session_id2_len = datlen;
    516  1.1  christos 		session_id2 = xmalloc(session_id2_len);
    517  1.1  christos 		memcpy(session_id2, p, session_id2_len);
    518  1.1  christos 	}
    519  1.1  christos 
    520  1.1  christos 	if ((key = get_hostkey_by_index(keyid)) == NULL)
    521  1.1  christos 		fatal("%s: no hostkey from index %d", __func__, keyid);
    522  1.1  christos 	if (key_sign(key, &signature, &siglen, p, datlen) < 0)
    523  1.1  christos 		fatal("%s: key_sign failed", __func__);
    524  1.1  christos 
    525  1.1  christos 	debug3("%s: signature %p(%u)", __func__, signature, siglen);
    526  1.1  christos 
    527  1.1  christos 	buffer_clear(m);
    528  1.1  christos 	buffer_put_string(m, signature, siglen);
    529  1.1  christos 
    530  1.1  christos 	xfree(p);
    531  1.1  christos 	xfree(signature);
    532  1.1  christos 
    533  1.1  christos 	mm_request_send(sock, MONITOR_ANS_SIGN, m);
    534  1.1  christos 
    535  1.1  christos 	/* Turn on permissions for getpwnam */
    536  1.1  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
    537  1.1  christos 
    538  1.1  christos 	return (0);
    539  1.1  christos }
    540  1.1  christos 
    541  1.1  christos /* Retrieves the password entry and also checks if the user is permitted */
    542  1.1  christos 
    543  1.1  christos int
    544  1.1  christos mm_answer_pwnamallow(int sock, Buffer *m)
    545  1.1  christos {
    546  1.1  christos 	char *username;
    547  1.1  christos 	struct passwd *pwent;
    548  1.1  christos 	int allowed = 0;
    549  1.1  christos 
    550  1.1  christos 	debug3("%s", __func__);
    551  1.1  christos 
    552  1.1  christos 	if (authctxt->attempt++ != 0)
    553  1.1  christos 		fatal("%s: multiple attempts for getpwnam", __func__);
    554  1.1  christos 
    555  1.1  christos 	username = buffer_get_string(m, NULL);
    556  1.1  christos 
    557  1.1  christos 	pwent = getpwnamallow(username);
    558  1.1  christos 
    559  1.1  christos 	authctxt->user = xstrdup(username);
    560  1.1  christos 	setproctitle("%s [priv]", pwent ? username : "unknown");
    561  1.1  christos 	xfree(username);
    562  1.1  christos 
    563  1.1  christos 	buffer_clear(m);
    564  1.1  christos 
    565  1.1  christos 	if (pwent == NULL) {
    566  1.1  christos 		buffer_put_char(m, 0);
    567  1.1  christos 		authctxt->pw = fakepw();
    568  1.1  christos 		goto out;
    569  1.1  christos 	}
    570  1.1  christos 
    571  1.1  christos 	allowed = 1;
    572  1.1  christos 	authctxt->pw = pwent;
    573  1.1  christos 	authctxt->valid = 1;
    574  1.1  christos 
    575  1.1  christos 	buffer_put_char(m, 1);
    576  1.1  christos 	buffer_put_string(m, pwent, sizeof(struct passwd));
    577  1.1  christos 	buffer_put_cstring(m, pwent->pw_name);
    578  1.1  christos 	buffer_put_cstring(m, "*");
    579  1.1  christos 	buffer_put_cstring(m, pwent->pw_gecos);
    580  1.1  christos 	buffer_put_cstring(m, pwent->pw_class);
    581  1.1  christos 	buffer_put_cstring(m, pwent->pw_dir);
    582  1.1  christos 	buffer_put_cstring(m, pwent->pw_shell);
    583  1.1  christos 
    584  1.1  christos  out:
    585  1.1  christos 	buffer_put_string(m, &options, sizeof(options));
    586  1.1  christos 	if (options.banner != NULL)
    587  1.1  christos 		buffer_put_cstring(m, options.banner);
    588  1.1  christos 	debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
    589  1.1  christos 	mm_request_send(sock, MONITOR_ANS_PWNAM, m);
    590  1.1  christos 
    591  1.1  christos 	/* For SSHv1 allow authentication now */
    592  1.1  christos 	if (!compat20)
    593  1.1  christos 		monitor_permit_authentications(1);
    594  1.1  christos 	else {
    595  1.1  christos 		/* Allow service/style information on the auth context */
    596  1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
    597  1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
    598  1.1  christos 	}
    599  1.1  christos 
    600  1.1  christos 
    601  1.1  christos 	return (0);
    602  1.1  christos }
    603  1.1  christos 
    604  1.1  christos int mm_answer_auth2_read_banner(int sock, Buffer *m)
    605  1.1  christos {
    606  1.1  christos 	char *banner;
    607  1.1  christos 
    608  1.1  christos 	buffer_clear(m);
    609  1.1  christos 	banner = auth2_read_banner();
    610  1.1  christos 	buffer_put_cstring(m, banner != NULL ? banner : "");
    611  1.1  christos 	mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
    612  1.1  christos 
    613  1.1  christos 	if (banner != NULL)
    614  1.1  christos 		xfree(banner);
    615  1.1  christos 
    616  1.1  christos 	return (0);
    617  1.1  christos }
    618  1.1  christos 
    619  1.1  christos int
    620  1.1  christos mm_answer_authserv(int sock, Buffer *m)
    621  1.1  christos {
    622  1.1  christos 	monitor_permit_authentications(1);
    623  1.1  christos 
    624  1.1  christos 	authctxt->service = buffer_get_string(m, NULL);
    625  1.1  christos 	authctxt->style = buffer_get_string(m, NULL);
    626  1.1  christos 	debug3("%s: service=%s, style=%s",
    627  1.1  christos 	    __func__, authctxt->service, authctxt->style);
    628  1.1  christos 
    629  1.1  christos 	if (strlen(authctxt->style) == 0) {
    630  1.1  christos 		xfree(authctxt->style);
    631  1.1  christos 		authctxt->style = NULL;
    632  1.1  christos 	}
    633  1.1  christos 
    634  1.1  christos 	return (0);
    635  1.1  christos }
    636  1.1  christos 
    637  1.1  christos int
    638  1.1  christos mm_answer_authpassword(int sock, Buffer *m)
    639  1.1  christos {
    640  1.1  christos 	static int call_count;
    641  1.1  christos 	char *passwd;
    642  1.1  christos 	int authenticated;
    643  1.1  christos 	u_int plen;
    644  1.1  christos 
    645  1.1  christos 	passwd = buffer_get_string(m, &plen);
    646  1.1  christos 	/* Only authenticate if the context is valid */
    647  1.1  christos 	authenticated = options.password_authentication &&
    648  1.1  christos 	    auth_password(authctxt, passwd);
    649  1.1  christos 	memset(passwd, 0, strlen(passwd));
    650  1.1  christos 	xfree(passwd);
    651  1.1  christos 
    652  1.1  christos 	buffer_clear(m);
    653  1.1  christos 	buffer_put_int(m, authenticated);
    654  1.1  christos 
    655  1.1  christos 	debug3("%s: sending result %d", __func__, authenticated);
    656  1.1  christos 	mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
    657  1.1  christos 
    658  1.1  christos 	call_count++;
    659  1.1  christos 	if (plen == 0 && call_count == 1)
    660  1.1  christos 		auth_method = "none";
    661  1.1  christos 	else
    662  1.1  christos 		auth_method = "password";
    663  1.1  christos 
    664  1.1  christos 	/* Causes monitor loop to terminate if authenticated */
    665  1.1  christos 	return (authenticated);
    666  1.1  christos }
    667  1.1  christos 
    668  1.1  christos int
    669  1.1  christos mm_answer_bsdauthquery(int sock, Buffer *m)
    670  1.1  christos {
    671  1.1  christos 	char *name, *infotxt;
    672  1.1  christos 	u_int numprompts;
    673  1.1  christos 	u_int *echo_on;
    674  1.1  christos 	char **prompts;
    675  1.1  christos 	u_int success;
    676  1.1  christos 
    677  1.1  christos 	success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
    678  1.1  christos 	    &prompts, &echo_on) < 0 ? 0 : 1;
    679  1.1  christos 
    680  1.1  christos 	buffer_clear(m);
    681  1.1  christos 	buffer_put_int(m, success);
    682  1.1  christos 	if (success)
    683  1.1  christos 		buffer_put_cstring(m, prompts[0]);
    684  1.1  christos 
    685  1.1  christos 	debug3("%s: sending challenge success: %u", __func__, success);
    686  1.1  christos 	mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
    687  1.1  christos 
    688  1.1  christos 	if (success) {
    689  1.1  christos 		xfree(name);
    690  1.1  christos 		xfree(infotxt);
    691  1.1  christos 		xfree(prompts);
    692  1.1  christos 		xfree(echo_on);
    693  1.1  christos 	}
    694  1.1  christos 
    695  1.1  christos 	return (0);
    696  1.1  christos }
    697  1.1  christos 
    698  1.1  christos int
    699  1.1  christos mm_answer_bsdauthrespond(int sock, Buffer *m)
    700  1.1  christos {
    701  1.1  christos 	char *response;
    702  1.1  christos 	int authok;
    703  1.1  christos 
    704  1.1  christos 	if (authctxt->as == 0)
    705  1.1  christos 		fatal("%s: no bsd auth session", __func__);
    706  1.1  christos 
    707  1.1  christos 	response = buffer_get_string(m, NULL);
    708  1.1  christos 	authok = options.challenge_response_authentication &&
    709  1.1  christos 	    auth_userresponse(authctxt->as, response, 0);
    710  1.1  christos 	authctxt->as = NULL;
    711  1.1  christos 	debug3("%s: <%s> = <%d>", __func__, response, authok);
    712  1.1  christos 	xfree(response);
    713  1.1  christos 
    714  1.1  christos 	buffer_clear(m);
    715  1.1  christos 	buffer_put_int(m, authok);
    716  1.1  christos 
    717  1.1  christos 	debug3("%s: sending authenticated: %d", __func__, authok);
    718  1.1  christos 	mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
    719  1.1  christos 
    720  1.1  christos 	auth_method = "bsdauth";
    721  1.1  christos 
    722  1.1  christos 	return (authok != 0);
    723  1.1  christos }
    724  1.1  christos 
    725  1.1  christos 
    726  1.1  christos static void
    727  1.1  christos mm_append_debug(Buffer *m)
    728  1.1  christos {
    729  1.1  christos 	if (auth_debug_init && buffer_len(&auth_debug)) {
    730  1.1  christos 		debug3("%s: Appending debug messages for child", __func__);
    731  1.1  christos 		buffer_append(m, buffer_ptr(&auth_debug),
    732  1.1  christos 		    buffer_len(&auth_debug));
    733  1.1  christos 		buffer_clear(&auth_debug);
    734  1.1  christos 	}
    735  1.1  christos }
    736  1.1  christos 
    737  1.1  christos int
    738  1.1  christos mm_answer_keyallowed(int sock, Buffer *m)
    739  1.1  christos {
    740  1.1  christos 	Key *key;
    741  1.1  christos 	char *cuser, *chost;
    742  1.1  christos 	u_char *blob;
    743  1.1  christos 	u_int bloblen;
    744  1.1  christos 	enum mm_keytype type = 0;
    745  1.1  christos 	int allowed = 0;
    746  1.1  christos 
    747  1.1  christos 	debug3("%s entering", __func__);
    748  1.1  christos 
    749  1.1  christos 	type = buffer_get_int(m);
    750  1.1  christos 	cuser = buffer_get_string(m, NULL);
    751  1.1  christos 	chost = buffer_get_string(m, NULL);
    752  1.1  christos 	blob = buffer_get_string(m, &bloblen);
    753  1.1  christos 
    754  1.1  christos 	key = key_from_blob(blob, bloblen);
    755  1.1  christos 
    756  1.1  christos 	if ((compat20 && type == MM_RSAHOSTKEY) ||
    757  1.1  christos 	    (!compat20 && type != MM_RSAHOSTKEY))
    758  1.1  christos 		fatal("%s: key type and protocol mismatch", __func__);
    759  1.1  christos 
    760  1.1  christos 	debug3("%s: key_from_blob: %p", __func__, key);
    761  1.1  christos 
    762  1.1  christos 	if (key != NULL && authctxt->valid) {
    763  1.1  christos 		switch (type) {
    764  1.1  christos 		case MM_USERKEY:
    765  1.1  christos 			allowed = options.pubkey_authentication &&
    766  1.1  christos 			    user_key_allowed(authctxt->pw, key);
    767  1.1  christos 			auth_method = "publickey";
    768  1.1  christos 			if (options.pubkey_authentication && allowed != 1)
    769  1.1  christos 				auth_clear_options();
    770  1.1  christos 			break;
    771  1.1  christos 		case MM_HOSTKEY:
    772  1.1  christos 			allowed = options.hostbased_authentication &&
    773  1.1  christos 			    hostbased_key_allowed(authctxt->pw,
    774  1.1  christos 			    cuser, chost, key);
    775  1.1  christos 			auth_method = "hostbased";
    776  1.1  christos 			break;
    777  1.1  christos 		case MM_RSAHOSTKEY:
    778  1.1  christos 			key->type = KEY_RSA1; /* XXX */
    779  1.1  christos 			allowed = options.rhosts_rsa_authentication &&
    780  1.1  christos 			    auth_rhosts_rsa_key_allowed(authctxt->pw,
    781  1.1  christos 			    cuser, chost, key);
    782  1.1  christos 			if (options.rhosts_rsa_authentication && allowed != 1)
    783  1.1  christos 				auth_clear_options();
    784  1.1  christos 			auth_method = "rsa";
    785  1.1  christos 			break;
    786  1.1  christos 		default:
    787  1.1  christos 			fatal("%s: unknown key type %d", __func__, type);
    788  1.1  christos 			break;
    789  1.1  christos 		}
    790  1.1  christos 	}
    791  1.1  christos 	if (key != NULL)
    792  1.1  christos 		key_free(key);
    793  1.1  christos 
    794  1.1  christos 	/* clear temporarily storage (used by verify) */
    795  1.1  christos 	monitor_reset_key_state();
    796  1.1  christos 
    797  1.1  christos 	if (allowed) {
    798  1.1  christos 		/* Save temporarily for comparison in verify */
    799  1.1  christos 		key_blob = blob;
    800  1.1  christos 		key_bloblen = bloblen;
    801  1.1  christos 		key_blobtype = type;
    802  1.1  christos 		hostbased_cuser = cuser;
    803  1.1  christos 		hostbased_chost = chost;
    804  1.1  christos 	} else {
    805  1.1  christos 		/* Log failed attempt */
    806  1.1  christos 		auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
    807  1.1  christos 		xfree(blob);
    808  1.1  christos 		xfree(cuser);
    809  1.1  christos 		xfree(chost);
    810  1.1  christos 	}
    811  1.1  christos 
    812  1.1  christos 	debug3("%s: key %p is %s",
    813  1.1  christos 	    __func__, key, allowed ? "allowed" : "not allowed");
    814  1.1  christos 
    815  1.1  christos 	buffer_clear(m);
    816  1.1  christos 	buffer_put_int(m, allowed);
    817  1.1  christos 	buffer_put_int(m, forced_command != NULL);
    818  1.1  christos 
    819  1.1  christos 	mm_append_debug(m);
    820  1.1  christos 
    821  1.1  christos 	mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
    822  1.1  christos 
    823  1.1  christos 	if (type == MM_RSAHOSTKEY)
    824  1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
    825  1.1  christos 
    826  1.1  christos 	return (0);
    827  1.1  christos }
    828  1.1  christos 
    829  1.1  christos static int
    830  1.1  christos monitor_valid_userblob(u_char *data, u_int datalen)
    831  1.1  christos {
    832  1.1  christos 	Buffer b;
    833  1.1  christos 	char *p;
    834  1.1  christos 	u_int len;
    835  1.1  christos 	int fail = 0;
    836  1.1  christos 
    837  1.1  christos 	buffer_init(&b);
    838  1.1  christos 	buffer_append(&b, data, datalen);
    839  1.1  christos 
    840  1.1  christos 	if (datafellows & SSH_OLD_SESSIONID) {
    841  1.1  christos 		p = buffer_ptr(&b);
    842  1.1  christos 		len = buffer_len(&b);
    843  1.1  christos 		if ((session_id2 == NULL) ||
    844  1.1  christos 		    (len < session_id2_len) ||
    845  1.1  christos 		    (memcmp(p, session_id2, session_id2_len) != 0))
    846  1.1  christos 			fail++;
    847  1.1  christos 		buffer_consume(&b, session_id2_len);
    848  1.1  christos 	} else {
    849  1.1  christos 		p = buffer_get_string(&b, &len);
    850  1.1  christos 		if ((session_id2 == NULL) ||
    851  1.1  christos 		    (len != session_id2_len) ||
    852  1.1  christos 		    (memcmp(p, session_id2, session_id2_len) != 0))
    853  1.1  christos 			fail++;
    854  1.1  christos 		xfree(p);
    855  1.1  christos 	}
    856  1.1  christos 	if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
    857  1.1  christos 		fail++;
    858  1.1  christos 	p = buffer_get_string(&b, NULL);
    859  1.1  christos 	if (strcmp(authctxt->user, p) != 0) {
    860  1.1  christos 		logit("wrong user name passed to monitor: expected %s != %.100s",
    861  1.1  christos 		    authctxt->user, p);
    862  1.1  christos 		fail++;
    863  1.1  christos 	}
    864  1.1  christos 	xfree(p);
    865  1.1  christos 	buffer_skip_string(&b);
    866  1.1  christos 	if (datafellows & SSH_BUG_PKAUTH) {
    867  1.1  christos 		if (!buffer_get_char(&b))
    868  1.1  christos 			fail++;
    869  1.1  christos 	} else {
    870  1.1  christos 		p = buffer_get_string(&b, NULL);
    871  1.1  christos 		if (strcmp("publickey", p) != 0)
    872  1.1  christos 			fail++;
    873  1.1  christos 		xfree(p);
    874  1.1  christos 		if (!buffer_get_char(&b))
    875  1.1  christos 			fail++;
    876  1.1  christos 		buffer_skip_string(&b);
    877  1.1  christos 	}
    878  1.1  christos 	buffer_skip_string(&b);
    879  1.1  christos 	if (buffer_len(&b) != 0)
    880  1.1  christos 		fail++;
    881  1.1  christos 	buffer_free(&b);
    882  1.1  christos 	return (fail == 0);
    883  1.1  christos }
    884  1.1  christos 
    885  1.1  christos static int
    886  1.1  christos monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
    887  1.1  christos     char *chost)
    888  1.1  christos {
    889  1.1  christos 	Buffer b;
    890  1.1  christos 	char *p;
    891  1.1  christos 	u_int len;
    892  1.1  christos 	int fail = 0;
    893  1.1  christos 
    894  1.1  christos 	buffer_init(&b);
    895  1.1  christos 	buffer_append(&b, data, datalen);
    896  1.1  christos 
    897  1.1  christos 	p = buffer_get_string(&b, &len);
    898  1.1  christos 	if ((session_id2 == NULL) ||
    899  1.1  christos 	    (len != session_id2_len) ||
    900  1.1  christos 	    (memcmp(p, session_id2, session_id2_len) != 0))
    901  1.1  christos 		fail++;
    902  1.1  christos 	xfree(p);
    903  1.1  christos 
    904  1.1  christos 	if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
    905  1.1  christos 		fail++;
    906  1.1  christos 	p = buffer_get_string(&b, NULL);
    907  1.1  christos 	if (strcmp(authctxt->user, p) != 0) {
    908  1.1  christos 		logit("wrong user name passed to monitor: expected %s != %.100s",
    909  1.1  christos 		    authctxt->user, p);
    910  1.1  christos 		fail++;
    911  1.1  christos 	}
    912  1.1  christos 	xfree(p);
    913  1.1  christos 	buffer_skip_string(&b);	/* service */
    914  1.1  christos 	p = buffer_get_string(&b, NULL);
    915  1.1  christos 	if (strcmp(p, "hostbased") != 0)
    916  1.1  christos 		fail++;
    917  1.1  christos 	xfree(p);
    918  1.1  christos 	buffer_skip_string(&b);	/* pkalg */
    919  1.1  christos 	buffer_skip_string(&b);	/* pkblob */
    920  1.1  christos 
    921  1.1  christos 	/* verify client host, strip trailing dot if necessary */
    922  1.1  christos 	p = buffer_get_string(&b, NULL);
    923  1.1  christos 	if (((len = strlen(p)) > 0) && p[len - 1] == '.')
    924  1.1  christos 		p[len - 1] = '\0';
    925  1.1  christos 	if (strcmp(p, chost) != 0)
    926  1.1  christos 		fail++;
    927  1.1  christos 	xfree(p);
    928  1.1  christos 
    929  1.1  christos 	/* verify client user */
    930  1.1  christos 	p = buffer_get_string(&b, NULL);
    931  1.1  christos 	if (strcmp(p, cuser) != 0)
    932  1.1  christos 		fail++;
    933  1.1  christos 	xfree(p);
    934  1.1  christos 
    935  1.1  christos 	if (buffer_len(&b) != 0)
    936  1.1  christos 		fail++;
    937  1.1  christos 	buffer_free(&b);
    938  1.1  christos 	return (fail == 0);
    939  1.1  christos }
    940  1.1  christos 
    941  1.1  christos int
    942  1.1  christos mm_answer_keyverify(int sock, Buffer *m)
    943  1.1  christos {
    944  1.1  christos 	Key *key;
    945  1.1  christos 	u_char *signature, *data, *blob;
    946  1.1  christos 	u_int signaturelen, datalen, bloblen;
    947  1.1  christos 	int verified = 0;
    948  1.1  christos 	int valid_data = 0;
    949  1.1  christos 
    950  1.1  christos 	blob = buffer_get_string(m, &bloblen);
    951  1.1  christos 	signature = buffer_get_string(m, &signaturelen);
    952  1.1  christos 	data = buffer_get_string(m, &datalen);
    953  1.1  christos 
    954  1.1  christos 	if (hostbased_cuser == NULL || hostbased_chost == NULL ||
    955  1.1  christos 	  !monitor_allowed_key(blob, bloblen))
    956  1.1  christos 		fatal("%s: bad key, not previously allowed", __func__);
    957  1.1  christos 
    958  1.1  christos 	key = key_from_blob(blob, bloblen);
    959  1.1  christos 	if (key == NULL)
    960  1.1  christos 		fatal("%s: bad public key blob", __func__);
    961  1.1  christos 
    962  1.1  christos 	switch (key_blobtype) {
    963  1.1  christos 	case MM_USERKEY:
    964  1.1  christos 		valid_data = monitor_valid_userblob(data, datalen);
    965  1.1  christos 		break;
    966  1.1  christos 	case MM_HOSTKEY:
    967  1.1  christos 		valid_data = monitor_valid_hostbasedblob(data, datalen,
    968  1.1  christos 		    hostbased_cuser, hostbased_chost);
    969  1.1  christos 		break;
    970  1.1  christos 	default:
    971  1.1  christos 		valid_data = 0;
    972  1.1  christos 		break;
    973  1.1  christos 	}
    974  1.1  christos 	if (!valid_data)
    975  1.1  christos 		fatal("%s: bad signature data blob", __func__);
    976  1.1  christos 
    977  1.1  christos 	verified = key_verify(key, signature, signaturelen, data, datalen);
    978  1.1  christos 	debug3("%s: key %p signature %s",
    979  1.1  christos 	    __func__, key, (verified == 1) ? "verified" : "unverified");
    980  1.1  christos 
    981  1.1  christos 	key_free(key);
    982  1.1  christos 	xfree(blob);
    983  1.1  christos 	xfree(signature);
    984  1.1  christos 	xfree(data);
    985  1.1  christos 
    986  1.1  christos 	auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
    987  1.1  christos 
    988  1.1  christos 	monitor_reset_key_state();
    989  1.1  christos 
    990  1.1  christos 	buffer_clear(m);
    991  1.1  christos 	buffer_put_int(m, verified);
    992  1.1  christos 	mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
    993  1.1  christos 
    994  1.1  christos 	return (verified == 1);
    995  1.1  christos }
    996  1.1  christos 
    997  1.1  christos static void
    998  1.1  christos mm_record_login(Session *s, struct passwd *pw)
    999  1.1  christos {
   1000  1.1  christos 	socklen_t fromlen;
   1001  1.1  christos 	struct sockaddr_storage from;
   1002  1.1  christos 
   1003  1.1  christos 	/*
   1004  1.1  christos 	 * Get IP address of client. If the connection is not a socket, let
   1005  1.1  christos 	 * the address be 0.0.0.0.
   1006  1.1  christos 	 */
   1007  1.1  christos 	memset(&from, 0, sizeof(from));
   1008  1.1  christos 	fromlen = sizeof(from);
   1009  1.1  christos 	if (packet_connection_is_on_socket()) {
   1010  1.1  christos 		if (getpeername(packet_get_connection_in(),
   1011  1.1  christos 		    (struct sockaddr *)&from, &fromlen) < 0) {
   1012  1.1  christos 			debug("getpeername: %.100s", strerror(errno));
   1013  1.1  christos 			cleanup_exit(255);
   1014  1.1  christos 		}
   1015  1.1  christos 	}
   1016  1.1  christos 	/* Record that there was a login on that tty from the remote host. */
   1017  1.1  christos 	record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
   1018  1.1  christos 	    get_remote_name_or_ip(utmp_len, options.use_dns),
   1019  1.1  christos 	    (struct sockaddr *)&from, fromlen);
   1020  1.1  christos }
   1021  1.1  christos 
   1022  1.1  christos static void
   1023  1.1  christos mm_session_close(Session *s)
   1024  1.1  christos {
   1025  1.1  christos 	debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
   1026  1.1  christos 	if (s->ttyfd != -1) {
   1027  1.1  christos 		debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
   1028  1.1  christos 		session_pty_cleanup2(s);
   1029  1.1  christos 	}
   1030  1.1  christos 	session_unused(s->self);
   1031  1.1  christos }
   1032  1.1  christos 
   1033  1.1  christos int
   1034  1.1  christos mm_answer_pty(int sock, Buffer *m)
   1035  1.1  christos {
   1036  1.1  christos 	extern struct monitor *pmonitor;
   1037  1.1  christos 	Session *s;
   1038  1.1  christos 	int res, fd0;
   1039  1.1  christos 
   1040  1.1  christos 	debug3("%s entering", __func__);
   1041  1.1  christos 
   1042  1.1  christos 	buffer_clear(m);
   1043  1.1  christos 	s = session_new();
   1044  1.1  christos 	if (s == NULL)
   1045  1.1  christos 		goto error;
   1046  1.1  christos 	s->authctxt = authctxt;
   1047  1.1  christos 	s->pw = authctxt->pw;
   1048  1.1  christos 	s->pid = pmonitor->m_pid;
   1049  1.1  christos 	res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
   1050  1.1  christos 	if (res == 0)
   1051  1.1  christos 		goto error;
   1052  1.1  christos 	pty_setowner(authctxt->pw, s->tty);
   1053  1.1  christos 
   1054  1.1  christos 	buffer_put_int(m, 1);
   1055  1.1  christos 	buffer_put_cstring(m, s->tty);
   1056  1.1  christos 
   1057  1.1  christos 	/* We need to trick ttyslot */
   1058  1.1  christos 	if (dup2(s->ttyfd, 0) == -1)
   1059  1.1  christos 		fatal("%s: dup2", __func__);
   1060  1.1  christos 
   1061  1.1  christos 	mm_record_login(s, authctxt->pw);
   1062  1.1  christos 
   1063  1.1  christos 	/* Now we can close the file descriptor again */
   1064  1.1  christos 	close(0);
   1065  1.1  christos 
   1066  1.1  christos 	/* send messages generated by record_login */
   1067  1.1  christos 	buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
   1068  1.1  christos 	buffer_clear(&loginmsg);
   1069  1.1  christos 
   1070  1.1  christos 	mm_request_send(sock, MONITOR_ANS_PTY, m);
   1071  1.1  christos 
   1072  1.1  christos 	if (mm_send_fd(sock, s->ptyfd) == -1 ||
   1073  1.1  christos 	    mm_send_fd(sock, s->ttyfd) == -1)
   1074  1.1  christos 		fatal("%s: send fds failed", __func__);
   1075  1.1  christos 
   1076  1.1  christos 	/* make sure nothing uses fd 0 */
   1077  1.1  christos 	if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
   1078  1.1  christos 		fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
   1079  1.1  christos 	if (fd0 != 0)
   1080  1.1  christos 		error("%s: fd0 %d != 0", __func__, fd0);
   1081  1.1  christos 
   1082  1.1  christos 	/* slave is not needed */
   1083  1.1  christos 	close(s->ttyfd);
   1084  1.1  christos 	s->ttyfd = s->ptyfd;
   1085  1.1  christos 	/* no need to dup() because nobody closes ptyfd */
   1086  1.1  christos 	s->ptymaster = s->ptyfd;
   1087  1.1  christos 
   1088  1.1  christos 	debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
   1089  1.1  christos 
   1090  1.1  christos 	return (0);
   1091  1.1  christos 
   1092  1.1  christos  error:
   1093  1.1  christos 	if (s != NULL)
   1094  1.1  christos 		mm_session_close(s);
   1095  1.1  christos 	buffer_put_int(m, 0);
   1096  1.1  christos 	mm_request_send(sock, MONITOR_ANS_PTY, m);
   1097  1.1  christos 	return (0);
   1098  1.1  christos }
   1099  1.1  christos 
   1100  1.1  christos int
   1101  1.1  christos mm_answer_pty_cleanup(int sock, Buffer *m)
   1102  1.1  christos {
   1103  1.1  christos 	Session *s;
   1104  1.1  christos 	char *tty;
   1105  1.1  christos 
   1106  1.1  christos 	debug3("%s entering", __func__);
   1107  1.1  christos 
   1108  1.1  christos 	tty = buffer_get_string(m, NULL);
   1109  1.1  christos 	if ((s = session_by_tty(tty)) != NULL)
   1110  1.1  christos 		mm_session_close(s);
   1111  1.1  christos 	buffer_clear(m);
   1112  1.1  christos 	xfree(tty);
   1113  1.1  christos 	return (0);
   1114  1.1  christos }
   1115  1.1  christos 
   1116  1.1  christos int
   1117  1.1  christos mm_answer_sesskey(int sock, Buffer *m)
   1118  1.1  christos {
   1119  1.1  christos 	BIGNUM *p;
   1120  1.1  christos 	int rsafail;
   1121  1.1  christos 
   1122  1.1  christos 	/* Turn off permissions */
   1123  1.1  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
   1124  1.1  christos 
   1125  1.1  christos 	if ((p = BN_new()) == NULL)
   1126  1.1  christos 		fatal("%s: BN_new", __func__);
   1127  1.1  christos 
   1128  1.1  christos 	buffer_get_bignum2(m, p);
   1129  1.1  christos 
   1130  1.1  christos 	rsafail = ssh1_session_key(p);
   1131  1.1  christos 
   1132  1.1  christos 	buffer_clear(m);
   1133  1.1  christos 	buffer_put_int(m, rsafail);
   1134  1.1  christos 	buffer_put_bignum2(m, p);
   1135  1.1  christos 
   1136  1.1  christos 	BN_clear_free(p);
   1137  1.1  christos 
   1138  1.1  christos 	mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
   1139  1.1  christos 
   1140  1.1  christos 	/* Turn on permissions for sessid passing */
   1141  1.1  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
   1142  1.1  christos 
   1143  1.1  christos 	return (0);
   1144  1.1  christos }
   1145  1.1  christos 
   1146  1.1  christos int
   1147  1.1  christos mm_answer_sessid(int sock, Buffer *m)
   1148  1.1  christos {
   1149  1.1  christos 	int i;
   1150  1.1  christos 
   1151  1.1  christos 	debug3("%s entering", __func__);
   1152  1.1  christos 
   1153  1.1  christos 	if (buffer_len(m) != 16)
   1154  1.1  christos 		fatal("%s: bad ssh1 session id", __func__);
   1155  1.1  christos 	for (i = 0; i < 16; i++)
   1156  1.1  christos 		session_id[i] = buffer_get_char(m);
   1157  1.1  christos 
   1158  1.1  christos 	/* Turn on permissions for getpwnam */
   1159  1.1  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
   1160  1.1  christos 
   1161  1.1  christos 	return (0);
   1162  1.1  christos }
   1163  1.1  christos 
   1164  1.1  christos int
   1165  1.1  christos mm_answer_rsa_keyallowed(int sock, Buffer *m)
   1166  1.1  christos {
   1167  1.1  christos 	BIGNUM *client_n;
   1168  1.1  christos 	Key *key = NULL;
   1169  1.1  christos 	u_char *blob = NULL;
   1170  1.1  christos 	u_int blen = 0;
   1171  1.1  christos 	int allowed = 0;
   1172  1.1  christos 
   1173  1.1  christos 	debug3("%s entering", __func__);
   1174  1.1  christos 
   1175  1.1  christos 	auth_method = "rsa";
   1176  1.1  christos 	if (options.rsa_authentication && authctxt->valid) {
   1177  1.1  christos 		if ((client_n = BN_new()) == NULL)
   1178  1.1  christos 			fatal("%s: BN_new", __func__);
   1179  1.1  christos 		buffer_get_bignum2(m, client_n);
   1180  1.1  christos 		allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
   1181  1.1  christos 		BN_clear_free(client_n);
   1182  1.1  christos 	}
   1183  1.1  christos 	buffer_clear(m);
   1184  1.1  christos 	buffer_put_int(m, allowed);
   1185  1.1  christos 	buffer_put_int(m, forced_command != NULL);
   1186  1.1  christos 
   1187  1.1  christos 	/* clear temporarily storage (used by generate challenge) */
   1188  1.1  christos 	monitor_reset_key_state();
   1189  1.1  christos 
   1190  1.1  christos 	if (allowed && key != NULL) {
   1191  1.1  christos 		key->type = KEY_RSA;	/* cheat for key_to_blob */
   1192  1.1  christos 		if (key_to_blob(key, &blob, &blen) == 0)
   1193  1.1  christos 			fatal("%s: key_to_blob failed", __func__);
   1194  1.1  christos 		buffer_put_string(m, blob, blen);
   1195  1.1  christos 
   1196  1.1  christos 		/* Save temporarily for comparison in verify */
   1197  1.1  christos 		key_blob = blob;
   1198  1.1  christos 		key_bloblen = blen;
   1199  1.1  christos 		key_blobtype = MM_RSAUSERKEY;
   1200  1.1  christos 	}
   1201  1.1  christos 	if (key != NULL)
   1202  1.1  christos 		key_free(key);
   1203  1.1  christos 
   1204  1.1  christos 	mm_append_debug(m);
   1205  1.1  christos 
   1206  1.1  christos 	mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
   1207  1.1  christos 
   1208  1.1  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
   1209  1.1  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
   1210  1.1  christos 	return (0);
   1211  1.1  christos }
   1212  1.1  christos 
   1213  1.1  christos int
   1214  1.1  christos mm_answer_rsa_challenge(int sock, Buffer *m)
   1215  1.1  christos {
   1216  1.1  christos 	Key *key = NULL;
   1217  1.1  christos 	u_char *blob;
   1218  1.1  christos 	u_int blen;
   1219  1.1  christos 
   1220  1.1  christos 	debug3("%s entering", __func__);
   1221  1.1  christos 
   1222  1.1  christos 	if (!authctxt->valid)
   1223  1.1  christos 		fatal("%s: authctxt not valid", __func__);
   1224  1.1  christos 	blob = buffer_get_string(m, &blen);
   1225  1.1  christos 	if (!monitor_allowed_key(blob, blen))
   1226  1.1  christos 		fatal("%s: bad key, not previously allowed", __func__);
   1227  1.1  christos 	if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
   1228  1.1  christos 		fatal("%s: key type mismatch", __func__);
   1229  1.1  christos 	if ((key = key_from_blob(blob, blen)) == NULL)
   1230  1.1  christos 		fatal("%s: received bad key", __func__);
   1231  1.1  christos 	if (key->type != KEY_RSA)
   1232  1.1  christos 		fatal("%s: received bad key type %d", __func__, key->type);
   1233  1.1  christos 	key->type = KEY_RSA1;
   1234  1.1  christos 	if (ssh1_challenge)
   1235  1.1  christos 		BN_clear_free(ssh1_challenge);
   1236  1.1  christos 	ssh1_challenge = auth_rsa_generate_challenge(key);
   1237  1.1  christos 
   1238  1.1  christos 	buffer_clear(m);
   1239  1.1  christos 	buffer_put_bignum2(m, ssh1_challenge);
   1240  1.1  christos 
   1241  1.1  christos 	debug3("%s sending reply", __func__);
   1242  1.1  christos 	mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
   1243  1.1  christos 
   1244  1.1  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
   1245  1.1  christos 
   1246  1.1  christos 	xfree(blob);
   1247  1.1  christos 	key_free(key);
   1248  1.1  christos 	return (0);
   1249  1.1  christos }
   1250  1.1  christos 
   1251  1.1  christos int
   1252  1.1  christos mm_answer_rsa_response(int sock, Buffer *m)
   1253  1.1  christos {
   1254  1.1  christos 	Key *key = NULL;
   1255  1.1  christos 	u_char *blob, *response;
   1256  1.1  christos 	u_int blen, len;
   1257  1.1  christos 	int success;
   1258  1.1  christos 
   1259  1.1  christos 	debug3("%s entering", __func__);
   1260  1.1  christos 
   1261  1.1  christos 	if (!authctxt->valid)
   1262  1.1  christos 		fatal("%s: authctxt not valid", __func__);
   1263  1.1  christos 	if (ssh1_challenge == NULL)
   1264  1.1  christos 		fatal("%s: no ssh1_challenge", __func__);
   1265  1.1  christos 
   1266  1.1  christos 	blob = buffer_get_string(m, &blen);
   1267  1.1  christos 	if (!monitor_allowed_key(blob, blen))
   1268  1.1  christos 		fatal("%s: bad key, not previously allowed", __func__);
   1269  1.1  christos 	if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
   1270  1.1  christos 		fatal("%s: key type mismatch: %d", __func__, key_blobtype);
   1271  1.1  christos 	if ((key = key_from_blob(blob, blen)) == NULL)
   1272  1.1  christos 		fatal("%s: received bad key", __func__);
   1273  1.1  christos 	response = buffer_get_string(m, &len);
   1274  1.1  christos 	if (len != 16)
   1275  1.1  christos 		fatal("%s: received bad response to challenge", __func__);
   1276  1.1  christos 	success = auth_rsa_verify_response(key, ssh1_challenge, response);
   1277  1.1  christos 
   1278  1.1  christos 	xfree(blob);
   1279  1.1  christos 	key_free(key);
   1280  1.1  christos 	xfree(response);
   1281  1.1  christos 
   1282  1.1  christos 	auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
   1283  1.1  christos 
   1284  1.1  christos 	/* reset state */
   1285  1.1  christos 	BN_clear_free(ssh1_challenge);
   1286  1.1  christos 	ssh1_challenge = NULL;
   1287  1.1  christos 	monitor_reset_key_state();
   1288  1.1  christos 
   1289  1.1  christos 	buffer_clear(m);
   1290  1.1  christos 	buffer_put_int(m, success);
   1291  1.1  christos 	mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
   1292  1.1  christos 
   1293  1.1  christos 	return (success);
   1294  1.1  christos }
   1295  1.1  christos 
   1296  1.1  christos int
   1297  1.1  christos mm_answer_term(int sock, Buffer *req)
   1298  1.1  christos {
   1299  1.1  christos 	extern struct monitor *pmonitor;
   1300  1.1  christos 	int res, status;
   1301  1.1  christos 
   1302  1.1  christos 	debug3("%s: tearing down sessions", __func__);
   1303  1.1  christos 
   1304  1.1  christos 	/* The child is terminating */
   1305  1.1  christos 	session_destroy_all(&mm_session_close);
   1306  1.1  christos 
   1307  1.1  christos 	while (waitpid(pmonitor->m_pid, &status, 0) == -1)
   1308  1.1  christos 		if (errno != EINTR)
   1309  1.1  christos 			exit(1);
   1310  1.1  christos 
   1311  1.1  christos 	res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
   1312  1.1  christos 
   1313  1.1  christos 	/* Terminate process */
   1314  1.1  christos 	exit(res);
   1315  1.1  christos }
   1316  1.1  christos 
   1317  1.1  christos void
   1318  1.1  christos monitor_apply_keystate(struct monitor *pmonitor)
   1319  1.1  christos {
   1320  1.1  christos 	if (compat20) {
   1321  1.1  christos 		set_newkeys(MODE_IN);
   1322  1.1  christos 		set_newkeys(MODE_OUT);
   1323  1.1  christos 	} else {
   1324  1.1  christos 		packet_set_protocol_flags(child_state.ssh1protoflags);
   1325  1.1  christos 		packet_set_encryption_key(child_state.ssh1key,
   1326  1.1  christos 		    child_state.ssh1keylen, child_state.ssh1cipher);
   1327  1.1  christos 		xfree(child_state.ssh1key);
   1328  1.1  christos 	}
   1329  1.1  christos 
   1330  1.1  christos 	/* for rc4 and other stateful ciphers */
   1331  1.1  christos 	packet_set_keycontext(MODE_OUT, child_state.keyout);
   1332  1.1  christos 	xfree(child_state.keyout);
   1333  1.1  christos 	packet_set_keycontext(MODE_IN, child_state.keyin);
   1334  1.1  christos 	xfree(child_state.keyin);
   1335  1.1  christos 
   1336  1.1  christos 	if (!compat20) {
   1337  1.1  christos 		packet_set_iv(MODE_OUT, child_state.ivout);
   1338  1.1  christos 		xfree(child_state.ivout);
   1339  1.1  christos 		packet_set_iv(MODE_IN, child_state.ivin);
   1340  1.1  christos 		xfree(child_state.ivin);
   1341  1.1  christos 	}
   1342  1.1  christos 
   1343  1.1  christos 	memcpy(&incoming_stream, &child_state.incoming,
   1344  1.1  christos 	    sizeof(incoming_stream));
   1345  1.1  christos 	memcpy(&outgoing_stream, &child_state.outgoing,
   1346  1.1  christos 	    sizeof(outgoing_stream));
   1347  1.1  christos 
   1348  1.1  christos 	/* Update with new address */
   1349  1.1  christos 	if (options.compression)
   1350  1.1  christos 		mm_init_compression(pmonitor->m_zlib);
   1351  1.1  christos 
   1352  1.1  christos 	/* Network I/O buffers */
   1353  1.1  christos 	/* XXX inefficient for large buffers, need: buffer_init_from_string */
   1354  1.1  christos 	buffer_clear(&input);
   1355  1.1  christos 	buffer_append(&input, child_state.input, child_state.ilen);
   1356  1.1  christos 	memset(child_state.input, 0, child_state.ilen);
   1357  1.1  christos 	xfree(child_state.input);
   1358  1.1  christos 
   1359  1.1  christos 	buffer_clear(&output);
   1360  1.1  christos 	buffer_append(&output, child_state.output, child_state.olen);
   1361  1.1  christos 	memset(child_state.output, 0, child_state.olen);
   1362  1.1  christos 	xfree(child_state.output);
   1363  1.1  christos }
   1364  1.1  christos 
   1365  1.1  christos static Kex *
   1366  1.1  christos mm_get_kex(Buffer *m)
   1367  1.1  christos {
   1368  1.1  christos 	Kex *kex;
   1369  1.1  christos 	void *blob;
   1370  1.1  christos 	u_int bloblen;
   1371  1.1  christos 
   1372  1.1  christos 	kex = xcalloc(1, sizeof(*kex));
   1373  1.1  christos 	kex->session_id = buffer_get_string(m, &kex->session_id_len);
   1374  1.1  christos 	if ((session_id2 == NULL) ||
   1375  1.1  christos 	    (kex->session_id_len != session_id2_len) ||
   1376  1.1  christos 	    (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
   1377  1.1  christos 		fatal("mm_get_get: internal error: bad session id");
   1378  1.1  christos 	kex->we_need = buffer_get_int(m);
   1379  1.1  christos 	kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
   1380  1.1  christos 	kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
   1381  1.1  christos 	kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
   1382  1.1  christos 	kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
   1383  1.1  christos 	kex->server = 1;
   1384  1.1  christos 	kex->hostkey_type = buffer_get_int(m);
   1385  1.1  christos 	kex->kex_type = buffer_get_int(m);
   1386  1.1  christos 	blob = buffer_get_string(m, &bloblen);
   1387  1.1  christos 	buffer_init(&kex->my);
   1388  1.1  christos 	buffer_append(&kex->my, blob, bloblen);
   1389  1.1  christos 	xfree(blob);
   1390  1.1  christos 	blob = buffer_get_string(m, &bloblen);
   1391  1.1  christos 	buffer_init(&kex->peer);
   1392  1.1  christos 	buffer_append(&kex->peer, blob, bloblen);
   1393  1.1  christos 	xfree(blob);
   1394  1.1  christos 	kex->done = 1;
   1395  1.1  christos 	kex->flags = buffer_get_int(m);
   1396  1.1  christos 	kex->client_version_string = buffer_get_string(m, NULL);
   1397  1.1  christos 	kex->server_version_string = buffer_get_string(m, NULL);
   1398  1.1  christos 	kex->load_host_key=&get_hostkey_by_type;
   1399  1.1  christos 	kex->host_key_index=&get_hostkey_index;
   1400  1.1  christos 
   1401  1.1  christos 	return (kex);
   1402  1.1  christos }
   1403  1.1  christos 
   1404  1.1  christos /* This function requries careful sanity checking */
   1405  1.1  christos 
   1406  1.1  christos void
   1407  1.1  christos mm_get_keystate(struct monitor *pmonitor)
   1408  1.1  christos {
   1409  1.1  christos 	Buffer m;
   1410  1.1  christos 	u_char *blob, *p;
   1411  1.1  christos 	u_int bloblen, plen;
   1412  1.1  christos 	u_int32_t seqnr, packets;
   1413  1.1  christos 	u_int64_t blocks, bytes;
   1414  1.1  christos 
   1415  1.1  christos 	debug3("%s: Waiting for new keys", __func__);
   1416  1.1  christos 
   1417  1.1  christos 	buffer_init(&m);
   1418  1.1  christos 	mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
   1419  1.1  christos 	if (!compat20) {
   1420  1.1  christos 		child_state.ssh1protoflags = buffer_get_int(&m);
   1421  1.1  christos 		child_state.ssh1cipher = buffer_get_int(&m);
   1422  1.1  christos 		child_state.ssh1key = buffer_get_string(&m,
   1423  1.1  christos 		    &child_state.ssh1keylen);
   1424  1.1  christos 		child_state.ivout = buffer_get_string(&m,
   1425  1.1  christos 		    &child_state.ivoutlen);
   1426  1.1  christos 		child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
   1427  1.1  christos 		goto skip;
   1428  1.1  christos 	} else {
   1429  1.1  christos 		/* Get the Kex for rekeying */
   1430  1.1  christos 		*pmonitor->m_pkex = mm_get_kex(&m);
   1431  1.1  christos 	}
   1432  1.1  christos 
   1433  1.1  christos 	blob = buffer_get_string(&m, &bloblen);
   1434  1.1  christos 	current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
   1435  1.1  christos 	xfree(blob);
   1436  1.1  christos 
   1437  1.1  christos 	debug3("%s: Waiting for second key", __func__);
   1438  1.1  christos 	blob = buffer_get_string(&m, &bloblen);
   1439  1.1  christos 	current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
   1440  1.1  christos 	xfree(blob);
   1441  1.1  christos 
   1442  1.1  christos 	/* Now get sequence numbers for the packets */
   1443  1.1  christos 	seqnr = buffer_get_int(&m);
   1444  1.1  christos 	blocks = buffer_get_int64(&m);
   1445  1.1  christos 	packets = buffer_get_int(&m);
   1446  1.1  christos 	bytes = buffer_get_int64(&m);
   1447  1.1  christos 	packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
   1448  1.1  christos 	seqnr = buffer_get_int(&m);
   1449  1.1  christos 	blocks = buffer_get_int64(&m);
   1450  1.1  christos 	packets = buffer_get_int(&m);
   1451  1.1  christos 	bytes = buffer_get_int64(&m);
   1452  1.1  christos 	packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
   1453  1.1  christos 
   1454  1.1  christos  skip:
   1455  1.1  christos 	/* Get the key context */
   1456  1.1  christos 	child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
   1457  1.1  christos 	child_state.keyin  = buffer_get_string(&m, &child_state.keyinlen);
   1458  1.1  christos 
   1459  1.1  christos 	debug3("%s: Getting compression state", __func__);
   1460  1.1  christos 	/* Get compression state */
   1461  1.1  christos 	p = buffer_get_string(&m, &plen);
   1462  1.1  christos 	if (plen != sizeof(child_state.outgoing))
   1463  1.1  christos 		fatal("%s: bad request size", __func__);
   1464  1.1  christos 	memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
   1465  1.1  christos 	xfree(p);
   1466  1.1  christos 
   1467  1.1  christos 	p = buffer_get_string(&m, &plen);
   1468  1.1  christos 	if (plen != sizeof(child_state.incoming))
   1469  1.1  christos 		fatal("%s: bad request size", __func__);
   1470  1.1  christos 	memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
   1471  1.1  christos 	xfree(p);
   1472  1.1  christos 
   1473  1.1  christos 	/* Network I/O buffers */
   1474  1.1  christos 	debug3("%s: Getting Network I/O buffers", __func__);
   1475  1.1  christos 	child_state.input = buffer_get_string(&m, &child_state.ilen);
   1476  1.1  christos 	child_state.output = buffer_get_string(&m, &child_state.olen);
   1477  1.1  christos 
   1478  1.1  christos 	buffer_free(&m);
   1479  1.1  christos }
   1480  1.1  christos 
   1481  1.1  christos 
   1482  1.1  christos /* Allocation functions for zlib */
   1483  1.1  christos void *
   1484  1.1  christos mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
   1485  1.1  christos {
   1486  1.1  christos 	size_t len = (size_t) size * ncount;
   1487  1.1  christos 	void *address;
   1488  1.1  christos 
   1489  1.1  christos 	if (len == 0 || ncount > SIZE_T_MAX / size)
   1490  1.1  christos 		fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
   1491  1.1  christos 
   1492  1.1  christos 	address = mm_malloc(mm, len);
   1493  1.1  christos 
   1494  1.1  christos 	return (address);
   1495  1.1  christos }
   1496  1.1  christos 
   1497  1.1  christos void
   1498  1.1  christos mm_zfree(struct mm_master *mm, void *address)
   1499  1.1  christos {
   1500  1.1  christos 	mm_free(mm, address);
   1501  1.1  christos }
   1502  1.1  christos 
   1503  1.1  christos void
   1504  1.1  christos mm_init_compression(struct mm_master *mm)
   1505  1.1  christos {
   1506  1.1  christos 	outgoing_stream.zalloc = (alloc_func)mm_zalloc;
   1507  1.1  christos 	outgoing_stream.zfree = (free_func)mm_zfree;
   1508  1.1  christos 	outgoing_stream.opaque = mm;
   1509  1.1  christos 
   1510  1.1  christos 	incoming_stream.zalloc = (alloc_func)mm_zalloc;
   1511  1.1  christos 	incoming_stream.zfree = (free_func)mm_zfree;
   1512  1.1  christos 	incoming_stream.opaque = mm;
   1513  1.1  christos }
   1514  1.1  christos 
   1515  1.1  christos /* XXX */
   1516  1.1  christos 
   1517  1.1  christos #define FD_CLOSEONEXEC(x) do { \
   1518  1.1  christos 	if (fcntl(x, F_SETFD, 1) == -1) \
   1519  1.1  christos 		fatal("fcntl(%d, F_SETFD)", x); \
   1520  1.1  christos } while (0)
   1521  1.1  christos 
   1522  1.1  christos static void
   1523  1.1  christos monitor_socketpair(int *pair)
   1524  1.1  christos {
   1525  1.1  christos 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
   1526  1.1  christos 		fatal("%s: socketpair", __func__);
   1527  1.1  christos 	FD_CLOSEONEXEC(pair[0]);
   1528  1.1  christos 	FD_CLOSEONEXEC(pair[1]);
   1529  1.1  christos }
   1530  1.1  christos 
   1531  1.1  christos #define MM_MEMSIZE	65536
   1532  1.1  christos 
   1533  1.1  christos struct monitor *
   1534  1.1  christos monitor_init(void)
   1535  1.1  christos {
   1536  1.1  christos 	struct monitor *mon;
   1537  1.1  christos 	int pair[2];
   1538  1.1  christos 
   1539  1.1  christos 	mon = xcalloc(1, sizeof(*mon));
   1540  1.1  christos 
   1541  1.1  christos 	monitor_socketpair(pair);
   1542  1.1  christos 
   1543  1.1  christos 	mon->m_recvfd = pair[0];
   1544  1.1  christos 	mon->m_sendfd = pair[1];
   1545  1.1  christos 
   1546  1.1  christos 	/* Used to share zlib space across processes */
   1547  1.1  christos 	if (options.compression) {
   1548  1.1  christos 		mon->m_zback = mm_create(NULL, MM_MEMSIZE);
   1549  1.1  christos 		mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
   1550  1.1  christos 
   1551  1.1  christos 		/* Compression needs to share state across borders */
   1552  1.1  christos 		mm_init_compression(mon->m_zlib);
   1553  1.1  christos 	}
   1554  1.1  christos 
   1555  1.1  christos 	return mon;
   1556  1.1  christos }
   1557  1.1  christos 
   1558  1.1  christos void
   1559  1.1  christos monitor_reinit(struct monitor *mon)
   1560  1.1  christos {
   1561  1.1  christos 	int pair[2];
   1562  1.1  christos 
   1563  1.1  christos 	monitor_socketpair(pair);
   1564  1.1  christos 
   1565  1.1  christos 	mon->m_recvfd = pair[0];
   1566  1.1  christos 	mon->m_sendfd = pair[1];
   1567  1.1  christos }
   1568  1.1  christos 
   1569  1.1  christos #ifdef GSSAPI
   1570  1.1  christos int
   1571  1.1  christos mm_answer_gss_setup_ctx(int sock, Buffer *m)
   1572  1.1  christos {
   1573  1.1  christos 	gss_OID_desc goid;
   1574  1.1  christos 	OM_uint32 major;
   1575  1.1  christos 	u_int len;
   1576  1.1  christos 
   1577  1.1  christos 	goid.elements = buffer_get_string(m, &len);
   1578  1.1  christos 	goid.length = len;
   1579  1.1  christos 
   1580  1.1  christos 	major = ssh_gssapi_server_ctx(&gsscontext, &goid);
   1581  1.1  christos 
   1582  1.1  christos 	xfree(goid.elements);
   1583  1.1  christos 
   1584  1.1  christos 	buffer_clear(m);
   1585  1.1  christos 	buffer_put_int(m, major);
   1586  1.1  christos 
   1587  1.1  christos 	mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
   1588  1.1  christos 
   1589  1.1  christos 	/* Now we have a context, enable the step */
   1590  1.1  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
   1591  1.1  christos 
   1592  1.1  christos 	return (0);
   1593  1.1  christos }
   1594  1.1  christos 
   1595  1.1  christos int
   1596  1.1  christos mm_answer_gss_accept_ctx(int sock, Buffer *m)
   1597  1.1  christos {
   1598  1.1  christos 	gss_buffer_desc in;
   1599  1.1  christos 	gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
   1600  1.1  christos 	OM_uint32 major, minor;
   1601  1.1  christos 	OM_uint32 flags = 0; /* GSI needs this */
   1602  1.1  christos 	u_int len;
   1603  1.1  christos 
   1604  1.1  christos 	in.value = buffer_get_string(m, &len);
   1605  1.1  christos 	in.length = len;
   1606  1.1  christos 	major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
   1607  1.1  christos 	xfree(in.value);
   1608  1.1  christos 
   1609  1.1  christos 	buffer_clear(m);
   1610  1.1  christos 	buffer_put_int(m, major);
   1611  1.1  christos 	buffer_put_string(m, out.value, out.length);
   1612  1.1  christos 	buffer_put_int(m, flags);
   1613  1.1  christos 	mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
   1614  1.1  christos 
   1615  1.1  christos 	gss_release_buffer(&minor, &out);
   1616  1.1  christos 
   1617  1.1  christos 	if (major == GSS_S_COMPLETE) {
   1618  1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
   1619  1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
   1620  1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
   1621  1.1  christos 	}
   1622  1.1  christos 	return (0);
   1623  1.1  christos }
   1624  1.1  christos 
   1625  1.1  christos int
   1626  1.1  christos mm_answer_gss_checkmic(int sock, Buffer *m)
   1627  1.1  christos {
   1628  1.1  christos 	gss_buffer_desc gssbuf, mic;
   1629  1.1  christos 	OM_uint32 ret;
   1630  1.1  christos 	u_int len;
   1631  1.1  christos 
   1632  1.1  christos 	gssbuf.value = buffer_get_string(m, &len);
   1633  1.1  christos 	gssbuf.length = len;
   1634  1.1  christos 	mic.value = buffer_get_string(m, &len);
   1635  1.1  christos 	mic.length = len;
   1636  1.1  christos 
   1637  1.1  christos 	ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
   1638  1.1  christos 
   1639  1.1  christos 	xfree(gssbuf.value);
   1640  1.1  christos 	xfree(mic.value);
   1641  1.1  christos 
   1642  1.1  christos 	buffer_clear(m);
   1643  1.1  christos 	buffer_put_int(m, ret);
   1644  1.1  christos 
   1645  1.1  christos 	mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
   1646  1.1  christos 
   1647  1.1  christos 	if (!GSS_ERROR(ret))
   1648  1.1  christos 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
   1649  1.1  christos 
   1650  1.1  christos 	return (0);
   1651  1.1  christos }
   1652  1.1  christos 
   1653  1.1  christos int
   1654  1.1  christos mm_answer_gss_userok(int sock, Buffer *m)
   1655  1.1  christos {
   1656  1.1  christos 	int authenticated;
   1657  1.1  christos 
   1658  1.1  christos 	authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
   1659  1.1  christos 
   1660  1.1  christos 	buffer_clear(m);
   1661  1.1  christos 	buffer_put_int(m, authenticated);
   1662  1.1  christos 
   1663  1.1  christos 	debug3("%s: sending result %d", __func__, authenticated);
   1664  1.1  christos 	mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
   1665  1.1  christos 
   1666  1.1  christos 	auth_method = "gssapi-with-mic";
   1667  1.1  christos 
   1668  1.1  christos 	/* Monitor loop will terminate if authenticated */
   1669  1.1  christos 	return (authenticated);
   1670  1.1  christos }
   1671  1.1  christos #endif /* GSSAPI */
   1672  1.1  christos 
   1673  1.1  christos #ifdef JPAKE
   1674  1.1  christos int
   1675  1.1  christos mm_answer_jpake_step1(int sock, Buffer *m)
   1676  1.1  christos {
   1677  1.1  christos 	struct jpake_ctx *pctx;
   1678  1.1  christos 	u_char *x3_proof, *x4_proof;
   1679  1.1  christos 	u_int x3_proof_len, x4_proof_len;
   1680  1.1  christos 
   1681  1.1  christos 	if (!options.zero_knowledge_password_authentication)
   1682  1.1  christos 		fatal("zero_knowledge_password_authentication disabled");
   1683  1.1  christos 
   1684  1.1  christos 	if (authctxt->jpake_ctx != NULL)
   1685  1.1  christos 		fatal("%s: authctxt->jpake_ctx already set (%p)",
   1686  1.1  christos 		    __func__, authctxt->jpake_ctx);
   1687  1.1  christos 	authctxt->jpake_ctx = pctx = jpake_new();
   1688  1.1  christos 
   1689  1.1  christos 	jpake_step1(pctx->grp,
   1690  1.1  christos 	    &pctx->server_id, &pctx->server_id_len,
   1691  1.1  christos 	    &pctx->x3, &pctx->x4, &pctx->g_x3, &pctx->g_x4,
   1692  1.1  christos 	    &x3_proof, &x3_proof_len,
   1693  1.1  christos 	    &x4_proof, &x4_proof_len);
   1694  1.1  christos 
   1695  1.1  christos 	JPAKE_DEBUG_CTX((pctx, "step1 done in %s", __func__));
   1696  1.1  christos 
   1697  1.1  christos 	buffer_clear(m);
   1698  1.1  christos 
   1699  1.1  christos 	buffer_put_string(m, pctx->server_id, pctx->server_id_len);
   1700  1.1  christos 	buffer_put_bignum2(m, pctx->g_x3);
   1701  1.1  christos 	buffer_put_bignum2(m, pctx->g_x4);
   1702  1.1  christos 	buffer_put_string(m, x3_proof, x3_proof_len);
   1703  1.1  christos 	buffer_put_string(m, x4_proof, x4_proof_len);
   1704  1.1  christos 
   1705  1.1  christos 	debug3("%s: sending step1", __func__);
   1706  1.1  christos 	mm_request_send(sock, MONITOR_ANS_JPAKE_STEP1, m);
   1707  1.1  christos 
   1708  1.1  christos 	bzero(x3_proof, x3_proof_len);
   1709  1.1  christos 	bzero(x4_proof, x4_proof_len);
   1710  1.1  christos 	xfree(x3_proof);
   1711  1.1  christos 	xfree(x4_proof);
   1712  1.1  christos 
   1713  1.1  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_GET_PWDATA, 1);
   1714  1.1  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 0);
   1715  1.1  christos 
   1716  1.1  christos 	return 0;
   1717  1.1  christos }
   1718  1.1  christos 
   1719  1.1  christos int
   1720  1.1  christos mm_answer_jpake_get_pwdata(int sock, Buffer *m)
   1721  1.1  christos {
   1722  1.1  christos 	struct jpake_ctx *pctx = authctxt->jpake_ctx;
   1723  1.1  christos 	char *hash_scheme, *salt;
   1724  1.1  christos 
   1725  1.1  christos 	if (pctx == NULL)
   1726  1.1  christos 		fatal("%s: pctx == NULL", __func__);
   1727  1.1  christos 
   1728  1.1  christos 	auth2_jpake_get_pwdata(authctxt, &pctx->s, &hash_scheme, &salt);
   1729  1.1  christos 
   1730  1.1  christos 	buffer_clear(m);
   1731  1.1  christos 	/* pctx->s is sensitive, not returned to slave */
   1732  1.1  christos 	buffer_put_cstring(m, hash_scheme);
   1733  1.1  christos 	buffer_put_cstring(m, salt);
   1734  1.1  christos 
   1735  1.1  christos 	debug3("%s: sending pwdata", __func__);
   1736  1.1  christos 	mm_request_send(sock, MONITOR_ANS_JPAKE_GET_PWDATA, m);
   1737  1.1  christos 
   1738  1.1  christos 	bzero(hash_scheme, strlen(hash_scheme));
   1739  1.1  christos 	bzero(salt, strlen(salt));
   1740  1.1  christos 	xfree(hash_scheme);
   1741  1.1  christos 	xfree(salt);
   1742  1.1  christos 
   1743  1.1  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP2, 1);
   1744  1.1  christos 
   1745  1.1  christos 	return 0;
   1746  1.1  christos }
   1747  1.1  christos 
   1748  1.1  christos int
   1749  1.1  christos mm_answer_jpake_step2(int sock, Buffer *m)
   1750  1.1  christos {
   1751  1.1  christos 	struct jpake_ctx *pctx = authctxt->jpake_ctx;
   1752  1.1  christos 	u_char *x1_proof, *x2_proof, *x4_s_proof;
   1753  1.1  christos 	u_int x1_proof_len, x2_proof_len, x4_s_proof_len;
   1754  1.1  christos 
   1755  1.1  christos 	if (pctx == NULL)
   1756  1.1  christos 		fatal("%s: pctx == NULL", __func__);
   1757  1.1  christos 
   1758  1.1  christos 	if ((pctx->g_x1 = BN_new()) == NULL ||
   1759  1.1  christos 	    (pctx->g_x2 = BN_new()) == NULL)
   1760  1.1  christos 		fatal("%s: BN_new", __func__);
   1761  1.1  christos 	buffer_get_bignum2(m, pctx->g_x1);
   1762  1.1  christos 	buffer_get_bignum2(m, pctx->g_x2);
   1763  1.1  christos 	pctx->client_id = buffer_get_string(m, &pctx->client_id_len);
   1764  1.1  christos 	x1_proof = buffer_get_string(m, &x1_proof_len);
   1765  1.1  christos 	x2_proof = buffer_get_string(m, &x2_proof_len);
   1766  1.1  christos 
   1767  1.1  christos 	jpake_step2(pctx->grp, pctx->s, pctx->g_x3,
   1768  1.1  christos 	    pctx->g_x1, pctx->g_x2, pctx->x4,
   1769  1.1  christos 	    pctx->client_id, pctx->client_id_len,
   1770  1.1  christos 	    pctx->server_id, pctx->server_id_len,
   1771  1.1  christos 	    x1_proof, x1_proof_len,
   1772  1.1  christos 	    x2_proof, x2_proof_len,
   1773  1.1  christos 	    &pctx->b,
   1774  1.1  christos 	    &x4_s_proof, &x4_s_proof_len);
   1775  1.1  christos 
   1776  1.1  christos 	JPAKE_DEBUG_CTX((pctx, "step2 done in %s", __func__));
   1777  1.1  christos 
   1778  1.1  christos 	bzero(x1_proof, x1_proof_len);
   1779  1.1  christos 	bzero(x2_proof, x2_proof_len);
   1780  1.1  christos 	xfree(x1_proof);
   1781  1.1  christos 	xfree(x2_proof);
   1782  1.1  christos 
   1783  1.1  christos 	buffer_clear(m);
   1784  1.1  christos 
   1785  1.1  christos 	buffer_put_bignum2(m, pctx->b);
   1786  1.1  christos 	buffer_put_string(m, x4_s_proof, x4_s_proof_len);
   1787  1.1  christos 
   1788  1.1  christos 	debug3("%s: sending step2", __func__);
   1789  1.1  christos 	mm_request_send(sock, MONITOR_ANS_JPAKE_STEP2, m);
   1790  1.1  christos 
   1791  1.1  christos 	bzero(x4_s_proof, x4_s_proof_len);
   1792  1.1  christos 	xfree(x4_s_proof);
   1793  1.1  christos 
   1794  1.1  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_KEY_CONFIRM, 1);
   1795  1.1  christos 
   1796  1.1  christos 	return 0;
   1797  1.1  christos }
   1798  1.1  christos 
   1799  1.1  christos int
   1800  1.1  christos mm_answer_jpake_key_confirm(int sock, Buffer *m)
   1801  1.1  christos {
   1802  1.1  christos 	struct jpake_ctx *pctx = authctxt->jpake_ctx;
   1803  1.1  christos 	u_char *x2_s_proof;
   1804  1.1  christos 	u_int x2_s_proof_len;
   1805  1.1  christos 
   1806  1.1  christos 	if (pctx == NULL)
   1807  1.1  christos 		fatal("%s: pctx == NULL", __func__);
   1808  1.1  christos 
   1809  1.1  christos 	if ((pctx->a = BN_new()) == NULL)
   1810  1.1  christos 		fatal("%s: BN_new", __func__);
   1811  1.1  christos 	buffer_get_bignum2(m, pctx->a);
   1812  1.1  christos 	x2_s_proof = buffer_get_string(m, &x2_s_proof_len);
   1813  1.1  christos 
   1814  1.1  christos 	jpake_key_confirm(pctx->grp, pctx->s, pctx->a,
   1815  1.1  christos 	    pctx->x4, pctx->g_x3, pctx->g_x4, pctx->g_x1, pctx->g_x2,
   1816  1.1  christos 	    pctx->server_id, pctx->server_id_len,
   1817  1.1  christos 	    pctx->client_id, pctx->client_id_len,
   1818  1.1  christos 	    session_id2, session_id2_len,
   1819  1.1  christos 	    x2_s_proof, x2_s_proof_len,
   1820  1.1  christos 	    &pctx->k,
   1821  1.1  christos 	    &pctx->h_k_sid_sessid, &pctx->h_k_sid_sessid_len);
   1822  1.1  christos 
   1823  1.1  christos 	JPAKE_DEBUG_CTX((pctx, "key_confirm done in %s", __func__));
   1824  1.1  christos 
   1825  1.1  christos 	bzero(x2_s_proof, x2_s_proof_len);
   1826  1.1  christos 	buffer_clear(m);
   1827  1.1  christos 
   1828  1.1  christos 	/* pctx->k is sensitive, not sent */
   1829  1.1  christos 	buffer_put_string(m, pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
   1830  1.1  christos 
   1831  1.1  christos 	debug3("%s: sending confirmation hash", __func__);
   1832  1.1  christos 	mm_request_send(sock, MONITOR_ANS_JPAKE_KEY_CONFIRM, m);
   1833  1.1  christos 
   1834  1.1  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_CHECK_CONFIRM, 1);
   1835  1.1  christos 
   1836  1.1  christos 	return 0;
   1837  1.1  christos }
   1838  1.1  christos 
   1839  1.1  christos int
   1840  1.1  christos mm_answer_jpake_check_confirm(int sock, Buffer *m)
   1841  1.1  christos {
   1842  1.1  christos 	int authenticated = 0;
   1843  1.1  christos 	u_char *peer_confirm_hash;
   1844  1.1  christos 	u_int peer_confirm_hash_len;
   1845  1.1  christos 	struct jpake_ctx *pctx = authctxt->jpake_ctx;
   1846  1.1  christos 
   1847  1.1  christos 	if (pctx == NULL)
   1848  1.1  christos 		fatal("%s: pctx == NULL", __func__);
   1849  1.1  christos 
   1850  1.1  christos 	peer_confirm_hash = buffer_get_string(m, &peer_confirm_hash_len);
   1851  1.1  christos 
   1852  1.1  christos 	authenticated = jpake_check_confirm(pctx->k,
   1853  1.1  christos 	    pctx->client_id, pctx->client_id_len,
   1854  1.1  christos 	    session_id2, session_id2_len,
   1855  1.1  christos 	    peer_confirm_hash, peer_confirm_hash_len) && authctxt->valid;
   1856  1.1  christos 
   1857  1.1  christos 	JPAKE_DEBUG_CTX((pctx, "check_confirm done in %s", __func__));
   1858  1.1  christos 
   1859  1.1  christos 	bzero(peer_confirm_hash, peer_confirm_hash_len);
   1860  1.1  christos 	xfree(peer_confirm_hash);
   1861  1.1  christos 
   1862  1.1  christos 	buffer_clear(m);
   1863  1.1  christos 	buffer_put_int(m, authenticated);
   1864  1.1  christos 
   1865  1.1  christos 	debug3("%s: sending result %d", __func__, authenticated);
   1866  1.1  christos 	mm_request_send(sock, MONITOR_ANS_JPAKE_CHECK_CONFIRM, m);
   1867  1.1  christos 
   1868  1.1  christos 	monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 1);
   1869  1.1  christos 
   1870  1.1  christos 	auth_method = "jpake-01 (at) openssh.com";
   1871  1.1  christos 	return authenticated;
   1872  1.1  christos }
   1873  1.1  christos 
   1874  1.1  christos #endif /* JPAKE */
   1875