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