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