]> andersk Git - gssapi-openssh.git/blob - openssh/monitor.c
e0434d5bcf506f732da2fb2212953acdd1a8464d
[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         setproctitle("%s [priv]", pwent ? username : "unknown");
684         xfree(username);
685
686         buffer_clear(m);
687
688         if (pwent == NULL) {
689                 buffer_put_char(m, 0);
690                 authctxt->pw = fakepw();
691                 goto out;
692         }
693
694         allowed = 1;
695         authctxt->pw = pwent;
696         authctxt->valid = 1;
697
698         buffer_put_char(m, 1);
699         buffer_put_string(m, pwent, sizeof(struct passwd));
700         buffer_put_cstring(m, pwent->pw_name);
701         buffer_put_cstring(m, "*");
702         buffer_put_cstring(m, pwent->pw_gecos);
703 #ifdef HAVE_PW_CLASS_IN_PASSWD
704         buffer_put_cstring(m, pwent->pw_class);
705 #endif
706         buffer_put_cstring(m, pwent->pw_dir);
707         buffer_put_cstring(m, pwent->pw_shell);
708
709  out:
710         buffer_put_string(m, &options, sizeof(options));
711         if (options.banner != NULL)
712                 buffer_put_cstring(m, options.banner);
713         debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
714         mm_request_send(sock, MONITOR_ANS_PWNAM, m);
715
716         /* For SSHv1 allow authentication now */
717         if (!compat20)
718                 monitor_permit_authentications(1);
719         else {
720                 /* Allow service/style information on the auth context */
721                 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
722                 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
723         }
724
725 #ifdef USE_PAM
726         if (options.use_pam)
727                 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
728 #endif
729
730         return (0);
731 }
732
733 int mm_answer_auth2_read_banner(int sock, Buffer *m)
734 {
735         char *banner;
736
737         buffer_clear(m);
738         banner = auth2_read_banner();
739         buffer_put_cstring(m, banner != NULL ? banner : "");
740         mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
741
742         if (banner != NULL)
743                 xfree(banner);
744
745         return (0);
746 }
747
748 int
749 mm_answer_authserv(int sock, Buffer *m)
750 {
751         monitor_permit_authentications(1);
752
753         authctxt->service = buffer_get_string(m, NULL);
754         authctxt->style = buffer_get_string(m, NULL);
755         debug3("%s: service=%s, style=%s",
756             __func__, authctxt->service, authctxt->style);
757
758         if (strlen(authctxt->style) == 0) {
759                 xfree(authctxt->style);
760                 authctxt->style = NULL;
761         }
762
763         return (0);
764 }
765
766 int
767 mm_answer_authpassword(int sock, Buffer *m)
768 {
769         static int call_count;
770         char *passwd;
771         int authenticated;
772         u_int plen;
773
774         passwd = buffer_get_string(m, &plen);
775         /* Only authenticate if the context is valid */
776         authenticated = options.password_authentication &&
777             auth_password(authctxt, passwd);
778         memset(passwd, 0, strlen(passwd));
779         xfree(passwd);
780
781         buffer_clear(m);
782         buffer_put_int(m, authenticated);
783
784         debug3("%s: sending result %d", __func__, authenticated);
785         mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
786
787         call_count++;
788         if (plen == 0 && call_count == 1)
789                 auth_method = "none";
790         else
791                 auth_method = "password";
792
793         /* Causes monitor loop to terminate if authenticated */
794         return (authenticated);
795 }
796
797 #ifdef BSD_AUTH
798 int
799 mm_answer_bsdauthquery(int sock, Buffer *m)
800 {
801         char *name, *infotxt;
802         u_int numprompts;
803         u_int *echo_on;
804         char **prompts;
805         u_int success;
806
807         success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
808             &prompts, &echo_on) < 0 ? 0 : 1;
809
810         buffer_clear(m);
811         buffer_put_int(m, success);
812         if (success)
813                 buffer_put_cstring(m, prompts[0]);
814
815         debug3("%s: sending challenge success: %u", __func__, success);
816         mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
817
818         if (success) {
819                 xfree(name);
820                 xfree(infotxt);
821                 xfree(prompts);
822                 xfree(echo_on);
823         }
824
825         return (0);
826 }
827
828 int
829 mm_answer_bsdauthrespond(int sock, Buffer *m)
830 {
831         char *response;
832         int authok;
833
834         if (authctxt->as == 0)
835                 fatal("%s: no bsd auth session", __func__);
836
837         response = buffer_get_string(m, NULL);
838         authok = options.challenge_response_authentication &&
839             auth_userresponse(authctxt->as, response, 0);
840         authctxt->as = NULL;
841         debug3("%s: <%s> = <%d>", __func__, response, authok);
842         xfree(response);
843
844         buffer_clear(m);
845         buffer_put_int(m, authok);
846
847         debug3("%s: sending authenticated: %d", __func__, authok);
848         mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
849
850         auth_method = "bsdauth";
851
852         return (authok != 0);
853 }
854 #endif
855
856 #ifdef SKEY
857 int
858 mm_answer_skeyquery(int sock, Buffer *m)
859 {
860         struct skey skey;
861         char challenge[1024];
862         u_int success;
863
864         success = _compat_skeychallenge(&skey, authctxt->user, challenge,
865             sizeof(challenge)) < 0 ? 0 : 1;
866
867         buffer_clear(m);
868         buffer_put_int(m, success);
869         if (success)
870                 buffer_put_cstring(m, challenge);
871
872         debug3("%s: sending challenge success: %u", __func__, success);
873         mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
874
875         return (0);
876 }
877
878 int
879 mm_answer_skeyrespond(int sock, Buffer *m)
880 {
881         char *response;
882         int authok;
883
884         response = buffer_get_string(m, NULL);
885
886         authok = (options.challenge_response_authentication &&
887             authctxt->valid &&
888             skey_haskey(authctxt->pw->pw_name) == 0 &&
889             skey_passcheck(authctxt->pw->pw_name, response) != -1);
890
891         xfree(response);
892
893         buffer_clear(m);
894         buffer_put_int(m, authok);
895
896         debug3("%s: sending authenticated: %d", __func__, authok);
897         mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
898
899         auth_method = "skey";
900
901         return (authok != 0);
902 }
903 #endif
904
905 #ifdef USE_PAM
906 int
907 mm_answer_pam_start(int sock, Buffer *m)
908 {
909         if (!options.use_pam)
910                 fatal("UsePAM not set, but ended up in %s anyway", __func__);
911
912         start_pam(authctxt);
913
914         monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
915
916         return (0);
917 }
918
919 int
920 mm_answer_pam_account(int sock, Buffer *m)
921 {
922         u_int ret;
923
924         if (!options.use_pam)
925                 fatal("UsePAM not set, but ended up in %s anyway", __func__);
926
927         ret = do_pam_account();
928
929         buffer_put_int(m, ret);
930         buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
931
932         mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
933
934         return (ret);
935 }
936
937 static void *sshpam_ctxt, *sshpam_authok;
938 extern KbdintDevice sshpam_device;
939
940 int
941 mm_answer_pam_init_ctx(int sock, Buffer *m)
942 {
943
944         debug3("%s", __func__);
945         authctxt->user = buffer_get_string(m, NULL);
946         sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
947         sshpam_authok = NULL;
948         buffer_clear(m);
949         if (sshpam_ctxt != NULL) {
950                 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
951                 buffer_put_int(m, 1);
952         } else {
953                 buffer_put_int(m, 0);
954         }
955         mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
956         return (0);
957 }
958
959 int
960 mm_answer_pam_query(int sock, Buffer *m)
961 {
962         char *name, *info, **prompts;
963         u_int i, num, *echo_on;
964         int ret;
965
966         debug3("%s", __func__);
967         sshpam_authok = NULL;
968         ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
969         if (ret == 0 && num == 0)
970                 sshpam_authok = sshpam_ctxt;
971         if (num > 1 || name == NULL || info == NULL)
972                 ret = -1;
973         buffer_clear(m);
974         buffer_put_int(m, ret);
975         buffer_put_cstring(m, name);
976         xfree(name);
977         buffer_put_cstring(m, info);
978         xfree(info);
979         buffer_put_int(m, num);
980         for (i = 0; i < num; ++i) {
981                 buffer_put_cstring(m, prompts[i]);
982                 xfree(prompts[i]);
983                 buffer_put_int(m, echo_on[i]);
984         }
985         if (prompts != NULL)
986                 xfree(prompts);
987         if (echo_on != NULL)
988                 xfree(echo_on);
989         auth_method = "keyboard-interactive/pam";
990         mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
991         return (0);
992 }
993
994 int
995 mm_answer_pam_respond(int sock, Buffer *m)
996 {
997         char **resp;
998         u_int i, num;
999         int ret;
1000
1001         debug3("%s", __func__);
1002         sshpam_authok = NULL;
1003         num = buffer_get_int(m);
1004         if (num > 0) {
1005                 resp = xcalloc(num, sizeof(char *));
1006                 for (i = 0; i < num; ++i)
1007                         resp[i] = buffer_get_string(m, NULL);
1008                 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1009                 for (i = 0; i < num; ++i)
1010                         xfree(resp[i]);
1011                 xfree(resp);
1012         } else {
1013                 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1014         }
1015         buffer_clear(m);
1016         buffer_put_int(m, ret);
1017         mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
1018         auth_method = "keyboard-interactive/pam";
1019         if (ret == 0)
1020                 sshpam_authok = sshpam_ctxt;
1021         return (0);
1022 }
1023
1024 int
1025 mm_answer_pam_free_ctx(int sock, Buffer *m)
1026 {
1027
1028         debug3("%s", __func__);
1029         (sshpam_device.free_ctx)(sshpam_ctxt);
1030         buffer_clear(m);
1031         mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
1032         auth_method = "keyboard-interactive/pam";
1033         return (sshpam_authok == sshpam_ctxt);
1034 }
1035 #endif
1036
1037 static void
1038 mm_append_debug(Buffer *m)
1039 {
1040         if (auth_debug_init && buffer_len(&auth_debug)) {
1041                 debug3("%s: Appending debug messages for child", __func__);
1042                 buffer_append(m, buffer_ptr(&auth_debug),
1043                     buffer_len(&auth_debug));
1044                 buffer_clear(&auth_debug);
1045         }
1046 }
1047
1048 int
1049 mm_answer_keyallowed(int sock, Buffer *m)
1050 {
1051         Key *key;
1052         char *cuser, *chost;
1053         u_char *blob;
1054         u_int bloblen;
1055         enum mm_keytype type = 0;
1056         int allowed = 0;
1057
1058         debug3("%s entering", __func__);
1059
1060         type = buffer_get_int(m);
1061         cuser = buffer_get_string(m, NULL);
1062         chost = buffer_get_string(m, NULL);
1063         blob = buffer_get_string(m, &bloblen);
1064
1065         key = key_from_blob(blob, bloblen);
1066
1067         if ((compat20 && type == MM_RSAHOSTKEY) ||
1068             (!compat20 && type != MM_RSAHOSTKEY))
1069                 fatal("%s: key type and protocol mismatch", __func__);
1070
1071         debug3("%s: key_from_blob: %p", __func__, key);
1072
1073         if (key != NULL && authctxt->valid) {
1074                 switch (type) {
1075                 case MM_USERKEY:
1076                         allowed = options.pubkey_authentication &&
1077                             user_key_allowed(authctxt->pw, key);
1078                         auth_method = "publickey";
1079                         if (options.pubkey_authentication && allowed != 1)
1080                                 auth_clear_options();
1081                         break;
1082                 case MM_HOSTKEY:
1083                         allowed = options.hostbased_authentication &&
1084                             hostbased_key_allowed(authctxt->pw,
1085                             cuser, chost, key);
1086                         auth_method = "hostbased";
1087                         break;
1088                 case MM_RSAHOSTKEY:
1089                         key->type = KEY_RSA1; /* XXX */
1090                         allowed = options.rhosts_rsa_authentication &&
1091                             auth_rhosts_rsa_key_allowed(authctxt->pw,
1092                             cuser, chost, key);
1093                         if (options.rhosts_rsa_authentication && allowed != 1)
1094                                 auth_clear_options();
1095                         auth_method = "rsa";
1096                         break;
1097                 default:
1098                         fatal("%s: unknown key type %d", __func__, type);
1099                         break;
1100                 }
1101         }
1102         if (key != NULL)
1103                 key_free(key);
1104
1105         /* clear temporarily storage (used by verify) */
1106         monitor_reset_key_state();
1107
1108         if (allowed) {
1109                 /* Save temporarily for comparison in verify */
1110                 key_blob = blob;
1111                 key_bloblen = bloblen;
1112                 key_blobtype = type;
1113                 hostbased_cuser = cuser;
1114                 hostbased_chost = chost;
1115         } else {
1116                 /* Log failed attempt */
1117                 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1118                 xfree(blob);
1119                 xfree(cuser);
1120                 xfree(chost);
1121         }
1122
1123         debug3("%s: key %p is %s",
1124             __func__, key, allowed ? "allowed" : "not allowed");
1125
1126         buffer_clear(m);
1127         buffer_put_int(m, allowed);
1128         buffer_put_int(m, forced_command != NULL);
1129
1130         mm_append_debug(m);
1131
1132         mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1133
1134         if (type == MM_RSAHOSTKEY)
1135                 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1136
1137         return (0);
1138 }
1139
1140 static int
1141 monitor_valid_userblob(u_char *data, u_int datalen)
1142 {
1143         Buffer b;
1144         char *p;
1145         u_int len;
1146         int fail = 0;
1147
1148         buffer_init(&b);
1149         buffer_append(&b, data, datalen);
1150
1151         if (datafellows & SSH_OLD_SESSIONID) {
1152                 p = buffer_ptr(&b);
1153                 len = buffer_len(&b);
1154                 if ((session_id2 == NULL) ||
1155                     (len < session_id2_len) ||
1156                     (memcmp(p, session_id2, session_id2_len) != 0))
1157                         fail++;
1158                 buffer_consume(&b, session_id2_len);
1159         } else {
1160                 p = buffer_get_string(&b, &len);
1161                 if ((session_id2 == NULL) ||
1162                     (len != session_id2_len) ||
1163                     (memcmp(p, session_id2, session_id2_len) != 0))
1164                         fail++;
1165                 xfree(p);
1166         }
1167         if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1168                 fail++;
1169         p = buffer_get_string(&b, NULL);
1170         if (strcmp(authctxt->user, p) != 0) {
1171                 logit("wrong user name passed to monitor: expected %s != %.100s",
1172                     authctxt->user, p);
1173                 fail++;
1174         }
1175         xfree(p);
1176         buffer_skip_string(&b);
1177         if (datafellows & SSH_BUG_PKAUTH) {
1178                 if (!buffer_get_char(&b))
1179                         fail++;
1180         } else {
1181                 p = buffer_get_string(&b, NULL);
1182                 if (strcmp("publickey", p) != 0)
1183                         fail++;
1184                 xfree(p);
1185                 if (!buffer_get_char(&b))
1186                         fail++;
1187                 buffer_skip_string(&b);
1188         }
1189         buffer_skip_string(&b);
1190         if (buffer_len(&b) != 0)
1191                 fail++;
1192         buffer_free(&b);
1193         return (fail == 0);
1194 }
1195
1196 static int
1197 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1198     char *chost)
1199 {
1200         Buffer b;
1201         char *p;
1202         u_int len;
1203         int fail = 0;
1204
1205         buffer_init(&b);
1206         buffer_append(&b, data, datalen);
1207
1208         p = buffer_get_string(&b, &len);
1209         if ((session_id2 == NULL) ||
1210             (len != session_id2_len) ||
1211             (memcmp(p, session_id2, session_id2_len) != 0))
1212                 fail++;
1213         xfree(p);
1214
1215         if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1216                 fail++;
1217         p = buffer_get_string(&b, NULL);
1218         if (strcmp(authctxt->user, p) != 0) {
1219                 logit("wrong user name passed to monitor: expected %s != %.100s",
1220                     authctxt->user, p);
1221                 fail++;
1222         }
1223         xfree(p);
1224         buffer_skip_string(&b); /* service */
1225         p = buffer_get_string(&b, NULL);
1226         if (strcmp(p, "hostbased") != 0)
1227                 fail++;
1228         xfree(p);
1229         buffer_skip_string(&b); /* pkalg */
1230         buffer_skip_string(&b); /* pkblob */
1231
1232         /* verify client host, strip trailing dot if necessary */
1233         p = buffer_get_string(&b, NULL);
1234         if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1235                 p[len - 1] = '\0';
1236         if (strcmp(p, chost) != 0)
1237                 fail++;
1238         xfree(p);
1239
1240         /* verify client user */
1241         p = buffer_get_string(&b, NULL);
1242         if (strcmp(p, cuser) != 0)
1243                 fail++;
1244         xfree(p);
1245
1246         if (buffer_len(&b) != 0)
1247                 fail++;
1248         buffer_free(&b);
1249         return (fail == 0);
1250 }
1251
1252 int
1253 mm_answer_keyverify(int sock, Buffer *m)
1254 {
1255         Key *key;
1256         u_char *signature, *data, *blob;
1257         u_int signaturelen, datalen, bloblen;
1258         int verified = 0;
1259         int valid_data = 0;
1260
1261         blob = buffer_get_string(m, &bloblen);
1262         signature = buffer_get_string(m, &signaturelen);
1263         data = buffer_get_string(m, &datalen);
1264
1265         if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1266           !monitor_allowed_key(blob, bloblen))
1267                 fatal("%s: bad key, not previously allowed", __func__);
1268
1269         key = key_from_blob(blob, bloblen);
1270         if (key == NULL)
1271                 fatal("%s: bad public key blob", __func__);
1272
1273         switch (key_blobtype) {
1274         case MM_USERKEY:
1275                 valid_data = monitor_valid_userblob(data, datalen);
1276                 break;
1277         case MM_HOSTKEY:
1278                 valid_data = monitor_valid_hostbasedblob(data, datalen,
1279                     hostbased_cuser, hostbased_chost);
1280                 break;
1281         default:
1282                 valid_data = 0;
1283                 break;
1284         }
1285         if (!valid_data)
1286                 fatal("%s: bad signature data blob", __func__);
1287
1288         verified = key_verify(key, signature, signaturelen, data, datalen);
1289         debug3("%s: key %p signature %s",
1290             __func__, key, (verified == 1) ? "verified" : "unverified");
1291
1292         key_free(key);
1293         xfree(blob);
1294         xfree(signature);
1295         xfree(data);
1296
1297         auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1298
1299         monitor_reset_key_state();
1300
1301         buffer_clear(m);
1302         buffer_put_int(m, verified);
1303         mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1304
1305         return (verified == 1);
1306 }
1307
1308 static void
1309 mm_record_login(Session *s, struct passwd *pw)
1310 {
1311         socklen_t fromlen;
1312         struct sockaddr_storage from;
1313
1314         /*
1315          * Get IP address of client. If the connection is not a socket, let
1316          * the address be 0.0.0.0.
1317          */
1318         memset(&from, 0, sizeof(from));
1319         fromlen = sizeof(from);
1320         if (packet_connection_is_on_socket()) {
1321                 if (getpeername(packet_get_connection_in(),
1322                     (struct sockaddr *)&from, &fromlen) < 0) {
1323                         debug("getpeername: %.100s", strerror(errno));
1324                         cleanup_exit(255);
1325                 }
1326         }
1327         /* Record that there was a login on that tty from the remote host. */
1328         record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1329             get_remote_name_or_ip(utmp_len, options.use_dns),
1330             (struct sockaddr *)&from, fromlen);
1331 }
1332
1333 static void
1334 mm_session_close(Session *s)
1335 {
1336         debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1337         if (s->ttyfd != -1) {
1338                 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1339                 session_pty_cleanup2(s);
1340         }
1341         session_unused(s->self);
1342 }
1343
1344 int
1345 mm_answer_pty(int sock, Buffer *m)
1346 {
1347         extern struct monitor *pmonitor;
1348         Session *s;
1349         int res, fd0;
1350
1351         debug3("%s entering", __func__);
1352
1353         buffer_clear(m);
1354         s = session_new();
1355         if (s == NULL)
1356                 goto error;
1357         s->authctxt = authctxt;
1358         s->pw = authctxt->pw;
1359         s->pid = pmonitor->m_pid;
1360         res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1361         if (res == 0)
1362                 goto error;
1363         pty_setowner(authctxt->pw, s->tty);
1364
1365         buffer_put_int(m, 1);
1366         buffer_put_cstring(m, s->tty);
1367
1368         /* We need to trick ttyslot */
1369         if (dup2(s->ttyfd, 0) == -1)
1370                 fatal("%s: dup2", __func__);
1371
1372         mm_record_login(s, authctxt->pw);
1373
1374         /* Now we can close the file descriptor again */
1375         close(0);
1376
1377         /* send messages generated by record_login */
1378         buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1379         buffer_clear(&loginmsg);
1380
1381         mm_request_send(sock, MONITOR_ANS_PTY, m);
1382
1383         if (mm_send_fd(sock, s->ptyfd) == -1 ||
1384             mm_send_fd(sock, s->ttyfd) == -1)
1385                 fatal("%s: send fds failed", __func__);
1386
1387         /* make sure nothing uses fd 0 */
1388         if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1389                 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1390         if (fd0 != 0)
1391                 error("%s: fd0 %d != 0", __func__, fd0);
1392
1393         /* slave is not needed */
1394         close(s->ttyfd);
1395         s->ttyfd = s->ptyfd;
1396         /* no need to dup() because nobody closes ptyfd */
1397         s->ptymaster = s->ptyfd;
1398
1399         debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1400
1401         return (0);
1402
1403  error:
1404         if (s != NULL)
1405                 mm_session_close(s);
1406         buffer_put_int(m, 0);
1407         mm_request_send(sock, MONITOR_ANS_PTY, m);
1408         return (0);
1409 }
1410
1411 int
1412 mm_answer_pty_cleanup(int sock, Buffer *m)
1413 {
1414         Session *s;
1415         char *tty;
1416
1417         debug3("%s entering", __func__);
1418
1419         tty = buffer_get_string(m, NULL);
1420         if ((s = session_by_tty(tty)) != NULL)
1421                 mm_session_close(s);
1422         buffer_clear(m);
1423         xfree(tty);
1424         return (0);
1425 }
1426
1427 int
1428 mm_answer_sesskey(int sock, Buffer *m)
1429 {
1430         BIGNUM *p;
1431         int rsafail;
1432
1433         /* Turn off permissions */
1434         monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1435
1436         if ((p = BN_new()) == NULL)
1437                 fatal("%s: BN_new", __func__);
1438
1439         buffer_get_bignum2(m, p);
1440
1441         rsafail = ssh1_session_key(p);
1442
1443         buffer_clear(m);
1444         buffer_put_int(m, rsafail);
1445         buffer_put_bignum2(m, p);
1446
1447         BN_clear_free(p);
1448
1449         mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1450
1451         /* Turn on permissions for sessid passing */
1452         monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1453
1454         return (0);
1455 }
1456
1457 int
1458 mm_answer_sessid(int sock, Buffer *m)
1459 {
1460         int i;
1461
1462         debug3("%s entering", __func__);
1463
1464         if (buffer_len(m) != 16)
1465                 fatal("%s: bad ssh1 session id", __func__);
1466         for (i = 0; i < 16; i++)
1467                 session_id[i] = buffer_get_char(m);
1468
1469         /* Turn on permissions for getpwnam */
1470         monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1471
1472         return (0);
1473 }
1474
1475 int
1476 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1477 {
1478         BIGNUM *client_n;
1479         Key *key = NULL;
1480         u_char *blob = NULL;
1481         u_int blen = 0;
1482         int allowed = 0;
1483
1484         debug3("%s entering", __func__);
1485
1486         auth_method = "rsa";
1487         if (options.rsa_authentication && authctxt->valid) {
1488                 if ((client_n = BN_new()) == NULL)
1489                         fatal("%s: BN_new", __func__);
1490                 buffer_get_bignum2(m, client_n);
1491                 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1492                 BN_clear_free(client_n);
1493         }
1494         buffer_clear(m);
1495         buffer_put_int(m, allowed);
1496         buffer_put_int(m, forced_command != NULL);
1497
1498         /* clear temporarily storage (used by generate challenge) */
1499         monitor_reset_key_state();
1500
1501         if (allowed && key != NULL) {
1502                 key->type = KEY_RSA;    /* cheat for key_to_blob */
1503                 if (key_to_blob(key, &blob, &blen) == 0)
1504                         fatal("%s: key_to_blob failed", __func__);
1505                 buffer_put_string(m, blob, blen);
1506
1507                 /* Save temporarily for comparison in verify */
1508                 key_blob = blob;
1509                 key_bloblen = blen;
1510                 key_blobtype = MM_RSAUSERKEY;
1511         }
1512         if (key != NULL)
1513                 key_free(key);
1514
1515         mm_append_debug(m);
1516
1517         mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1518
1519         monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1520         monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1521         return (0);
1522 }
1523
1524 int
1525 mm_answer_rsa_challenge(int sock, Buffer *m)
1526 {
1527         Key *key = NULL;
1528         u_char *blob;
1529         u_int blen;
1530
1531         debug3("%s entering", __func__);
1532
1533         if (!authctxt->valid)
1534                 fatal("%s: authctxt not valid", __func__);
1535         blob = buffer_get_string(m, &blen);
1536         if (!monitor_allowed_key(blob, blen))
1537                 fatal("%s: bad key, not previously allowed", __func__);
1538         if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1539                 fatal("%s: key type mismatch", __func__);
1540         if ((key = key_from_blob(blob, blen)) == NULL)
1541                 fatal("%s: received bad key", __func__);
1542         if (key->type != KEY_RSA)
1543                 fatal("%s: received bad key type %d", __func__, key->type);
1544         key->type = KEY_RSA1;
1545         if (ssh1_challenge)
1546                 BN_clear_free(ssh1_challenge);
1547         ssh1_challenge = auth_rsa_generate_challenge(key);
1548
1549         buffer_clear(m);
1550         buffer_put_bignum2(m, ssh1_challenge);
1551
1552         debug3("%s sending reply", __func__);
1553         mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1554
1555         monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1556
1557         xfree(blob);
1558         key_free(key);
1559         return (0);
1560 }
1561
1562 int
1563 mm_answer_rsa_response(int sock, Buffer *m)
1564 {
1565         Key *key = NULL;
1566         u_char *blob, *response;
1567         u_int blen, len;
1568         int success;
1569
1570         debug3("%s entering", __func__);
1571
1572         if (!authctxt->valid)
1573                 fatal("%s: authctxt not valid", __func__);
1574         if (ssh1_challenge == NULL)
1575                 fatal("%s: no ssh1_challenge", __func__);
1576
1577         blob = buffer_get_string(m, &blen);
1578         if (!monitor_allowed_key(blob, blen))
1579                 fatal("%s: bad key, not previously allowed", __func__);
1580         if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1581                 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1582         if ((key = key_from_blob(blob, blen)) == NULL)
1583                 fatal("%s: received bad key", __func__);
1584         response = buffer_get_string(m, &len);
1585         if (len != 16)
1586                 fatal("%s: received bad response to challenge", __func__);
1587         success = auth_rsa_verify_response(key, ssh1_challenge, response);
1588
1589         xfree(blob);
1590         key_free(key);
1591         xfree(response);
1592
1593         auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1594
1595         /* reset state */
1596         BN_clear_free(ssh1_challenge);
1597         ssh1_challenge = NULL;
1598         monitor_reset_key_state();
1599
1600         buffer_clear(m);
1601         buffer_put_int(m, success);
1602         mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1603
1604         return (success);
1605 }
1606
1607 int
1608 mm_answer_term(int sock, Buffer *req)
1609 {
1610         extern struct monitor *pmonitor;
1611         int res, status;
1612
1613         debug3("%s: tearing down sessions", __func__);
1614
1615         /* The child is terminating */
1616         session_destroy_all(&mm_session_close);
1617
1618 #ifdef USE_PAM
1619         if (options.use_pam)
1620                 sshpam_cleanup();
1621 #endif
1622
1623         while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1624                 if (errno != EINTR)
1625                         exit(1);
1626
1627         res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1628
1629         /* Terminate process */
1630         exit(res);
1631 }
1632
1633 #ifdef SSH_AUDIT_EVENTS
1634 /* Report that an audit event occurred */
1635 int
1636 mm_answer_audit_event(int socket, Buffer *m)
1637 {
1638         ssh_audit_event_t event;
1639
1640         debug3("%s entering", __func__);
1641
1642         event = buffer_get_int(m);
1643         switch(event) {
1644         case SSH_AUTH_FAIL_PUBKEY:
1645         case SSH_AUTH_FAIL_HOSTBASED:
1646         case SSH_AUTH_FAIL_GSSAPI:
1647         case SSH_LOGIN_EXCEED_MAXTRIES:
1648         case SSH_LOGIN_ROOT_DENIED:
1649         case SSH_CONNECTION_CLOSE:
1650         case SSH_INVALID_USER:
1651                 audit_event(event);
1652                 break;
1653         default:
1654                 fatal("Audit event type %d not permitted", event);
1655         }
1656
1657         return (0);
1658 }
1659
1660 int
1661 mm_answer_audit_command(int socket, Buffer *m)
1662 {
1663         u_int len;
1664         char *cmd;
1665
1666         debug3("%s entering", __func__);
1667         cmd = buffer_get_string(m, &len);
1668         /* sanity check command, if so how? */
1669         audit_run_command(cmd);
1670         xfree(cmd);
1671         return (0);
1672 }
1673 #endif /* SSH_AUDIT_EVENTS */
1674
1675 void
1676 monitor_apply_keystate(struct monitor *pmonitor)
1677 {
1678         if (compat20) {
1679                 set_newkeys(MODE_IN);
1680                 set_newkeys(MODE_OUT);
1681         } else {
1682                 packet_set_protocol_flags(child_state.ssh1protoflags);
1683                 packet_set_encryption_key(child_state.ssh1key,
1684                     child_state.ssh1keylen, child_state.ssh1cipher);
1685                 xfree(child_state.ssh1key);
1686         }
1687
1688         /* for rc4 and other stateful ciphers */
1689         packet_set_keycontext(MODE_OUT, child_state.keyout);
1690         xfree(child_state.keyout);
1691         packet_set_keycontext(MODE_IN, child_state.keyin);
1692         xfree(child_state.keyin);
1693
1694         if (!compat20) {
1695                 packet_set_iv(MODE_OUT, child_state.ivout);
1696                 xfree(child_state.ivout);
1697                 packet_set_iv(MODE_IN, child_state.ivin);
1698                 xfree(child_state.ivin);
1699         }
1700
1701         memcpy(&incoming_stream, &child_state.incoming,
1702             sizeof(incoming_stream));
1703         memcpy(&outgoing_stream, &child_state.outgoing,
1704             sizeof(outgoing_stream));
1705
1706         /* Update with new address */
1707         if (options.compression)
1708                 mm_init_compression(pmonitor->m_zlib);
1709
1710         /* Network I/O buffers */
1711         /* XXX inefficient for large buffers, need: buffer_init_from_string */
1712         buffer_clear(&input);
1713         buffer_append(&input, child_state.input, child_state.ilen);
1714         memset(child_state.input, 0, child_state.ilen);
1715         xfree(child_state.input);
1716
1717         buffer_clear(&output);
1718         buffer_append(&output, child_state.output, child_state.olen);
1719         memset(child_state.output, 0, child_state.olen);
1720         xfree(child_state.output);
1721 }
1722
1723 static Kex *
1724 mm_get_kex(Buffer *m)
1725 {
1726         Kex *kex;
1727         void *blob;
1728         u_int bloblen;
1729
1730         kex = xcalloc(1, sizeof(*kex));
1731         kex->session_id = buffer_get_string(m, &kex->session_id_len);
1732         if ((session_id2 == NULL) ||
1733             (kex->session_id_len != session_id2_len) ||
1734             (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1735                 fatal("mm_get_get: internal error: bad session id");
1736         kex->we_need = buffer_get_int(m);
1737         kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1738         kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1739         kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1740         kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1741 #ifdef GSSAPI
1742         if (options.gss_keyex) {
1743                 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
1744                 kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
1745                 kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server;
1746         }
1747 #endif
1748         kex->server = 1;
1749         kex->hostkey_type = buffer_get_int(m);
1750         kex->kex_type = buffer_get_int(m);
1751         blob = buffer_get_string(m, &bloblen);
1752         buffer_init(&kex->my);
1753         buffer_append(&kex->my, blob, bloblen);
1754         xfree(blob);
1755         blob = buffer_get_string(m, &bloblen);
1756         buffer_init(&kex->peer);
1757         buffer_append(&kex->peer, blob, bloblen);
1758         xfree(blob);
1759         kex->done = 1;
1760         kex->flags = buffer_get_int(m);
1761         kex->client_version_string = buffer_get_string(m, NULL);
1762         kex->server_version_string = buffer_get_string(m, NULL);
1763         kex->load_host_key=&get_hostkey_by_type;
1764         kex->host_key_index=&get_hostkey_index;
1765
1766         return (kex);
1767 }
1768
1769 /* This function requries careful sanity checking */
1770
1771 void
1772 mm_get_keystate(struct monitor *pmonitor)
1773 {
1774         Buffer m;
1775         u_char *blob, *p;
1776         u_int bloblen, plen;
1777         u_int32_t seqnr, packets;
1778         u_int64_t blocks, bytes;
1779
1780         debug3("%s: Waiting for new keys", __func__);
1781
1782         buffer_init(&m);
1783         mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1784         if (!compat20) {
1785                 child_state.ssh1protoflags = buffer_get_int(&m);
1786                 child_state.ssh1cipher = buffer_get_int(&m);
1787                 child_state.ssh1key = buffer_get_string(&m,
1788                     &child_state.ssh1keylen);
1789                 child_state.ivout = buffer_get_string(&m,
1790                     &child_state.ivoutlen);
1791                 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1792                 goto skip;
1793         } else {
1794                 /* Get the Kex for rekeying */
1795                 *pmonitor->m_pkex = mm_get_kex(&m);
1796         }
1797
1798         blob = buffer_get_string(&m, &bloblen);
1799         current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1800         xfree(blob);
1801
1802         debug3("%s: Waiting for second key", __func__);
1803         blob = buffer_get_string(&m, &bloblen);
1804         current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1805         xfree(blob);
1806
1807         /* Now get sequence numbers for the packets */
1808         seqnr = buffer_get_int(&m);
1809         blocks = buffer_get_int64(&m);
1810         packets = buffer_get_int(&m);
1811         bytes = buffer_get_int64(&m);
1812         packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
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_IN, seqnr, blocks, packets, bytes);
1818
1819  skip:
1820         /* Get the key context */
1821         child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1822         child_state.keyin  = buffer_get_string(&m, &child_state.keyinlen);
1823
1824         debug3("%s: Getting compression state", __func__);
1825         /* Get compression state */
1826         p = buffer_get_string(&m, &plen);
1827         if (plen != sizeof(child_state.outgoing))
1828                 fatal("%s: bad request size", __func__);
1829         memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1830         xfree(p);
1831
1832         p = buffer_get_string(&m, &plen);
1833         if (plen != sizeof(child_state.incoming))
1834                 fatal("%s: bad request size", __func__);
1835         memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1836         xfree(p);
1837
1838         /* Network I/O buffers */
1839         debug3("%s: Getting Network I/O buffers", __func__);
1840         child_state.input = buffer_get_string(&m, &child_state.ilen);
1841         child_state.output = buffer_get_string(&m, &child_state.olen);
1842
1843         buffer_free(&m);
1844 }
1845
1846
1847 /* Allocation functions for zlib */
1848 void *
1849 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1850 {
1851         size_t len = (size_t) size * ncount;
1852         void *address;
1853
1854         if (len == 0 || ncount > SIZE_T_MAX / size)
1855                 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1856
1857         address = mm_malloc(mm, len);
1858
1859         return (address);
1860 }
1861
1862 void
1863 mm_zfree(struct mm_master *mm, void *address)
1864 {
1865         mm_free(mm, address);
1866 }
1867
1868 void
1869 mm_init_compression(struct mm_master *mm)
1870 {
1871         outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1872         outgoing_stream.zfree = (free_func)mm_zfree;
1873         outgoing_stream.opaque = mm;
1874
1875         incoming_stream.zalloc = (alloc_func)mm_zalloc;
1876         incoming_stream.zfree = (free_func)mm_zfree;
1877         incoming_stream.opaque = mm;
1878 }
1879
1880 /* XXX */
1881
1882 #define FD_CLOSEONEXEC(x) do { \
1883         if (fcntl(x, F_SETFD, 1) == -1) \
1884                 fatal("fcntl(%d, F_SETFD)", x); \
1885 } while (0)
1886
1887 static void
1888 monitor_socketpair(int *pair)
1889 {
1890 #ifdef HAVE_SOCKETPAIR
1891         if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1892                 fatal("%s: socketpair", __func__);
1893 #else
1894         fatal("%s: UsePrivilegeSeparation=yes not supported",
1895             __func__);
1896 #endif
1897         FD_CLOSEONEXEC(pair[0]);
1898         FD_CLOSEONEXEC(pair[1]);
1899 }
1900
1901 #define MM_MEMSIZE      65536
1902
1903 struct monitor *
1904 monitor_init(void)
1905 {
1906         struct monitor *mon;
1907         int pair[2];
1908
1909         mon = xcalloc(1, sizeof(*mon));
1910
1911         monitor_socketpair(pair);
1912
1913         mon->m_recvfd = pair[0];
1914         mon->m_sendfd = pair[1];
1915
1916         /* Used to share zlib space across processes */
1917         if (options.compression) {
1918                 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1919                 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1920
1921                 /* Compression needs to share state across borders */
1922                 mm_init_compression(mon->m_zlib);
1923         }
1924
1925         return mon;
1926 }
1927
1928 void
1929 monitor_reinit(struct monitor *mon)
1930 {
1931         int pair[2];
1932
1933         monitor_socketpair(pair);
1934
1935         mon->m_recvfd = pair[0];
1936         mon->m_sendfd = pair[1];
1937 }
1938
1939 #ifdef GSSAPI
1940 int
1941 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1942 {
1943         gss_OID_desc goid;
1944         OM_uint32 major;
1945         u_int len;
1946
1947         if (!options.gss_authentication && !options.gss_keyex)
1948                 fatal("In GSSAPI monitor when GSSAPI is disabled");
1949
1950         goid.elements = buffer_get_string(m, &len);
1951         goid.length = len;
1952
1953         major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1954
1955         xfree(goid.elements);
1956
1957         buffer_clear(m);
1958         buffer_put_int(m, major);
1959
1960         mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1961
1962         /* Now we have a context, enable the step */
1963         monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1964
1965         return (0);
1966 }
1967
1968 int
1969 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1970 {
1971         gss_buffer_desc in;
1972         gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1973         OM_uint32 major, minor;
1974         OM_uint32 flags = 0; /* GSI needs this */
1975         u_int len;
1976
1977         if (!options.gss_authentication && !options.gss_keyex)
1978                 fatal("In GSSAPI monitor when GSSAPI is disabled");
1979
1980         in.value = buffer_get_string(m, &len);
1981         in.length = len;
1982         major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1983         xfree(in.value);
1984
1985         buffer_clear(m);
1986         buffer_put_int(m, major);
1987         buffer_put_string(m, out.value, out.length);
1988         buffer_put_int(m, flags);
1989         mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1990
1991         gss_release_buffer(&minor, &out);
1992
1993         if (major == GSS_S_COMPLETE) {
1994                 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1995                 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1996                 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
1997                 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1998                 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
1999         }
2000         return (0);
2001 }
2002
2003 int
2004 mm_answer_gss_checkmic(int sock, Buffer *m)
2005 {
2006         gss_buffer_desc gssbuf, mic;
2007         OM_uint32 ret;
2008         u_int len;
2009
2010         if (!options.gss_authentication && !options.gss_keyex)
2011                 fatal("In GSSAPI monitor when GSSAPI is disabled");
2012
2013         gssbuf.value = buffer_get_string(m, &len);
2014         gssbuf.length = len;
2015         mic.value = buffer_get_string(m, &len);
2016         mic.length = len;
2017
2018         ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
2019
2020         xfree(gssbuf.value);
2021         xfree(mic.value);
2022
2023         buffer_clear(m);
2024         buffer_put_int(m, ret);
2025
2026         mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
2027
2028         if (!GSS_ERROR(ret))
2029                 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2030
2031         return (0);
2032 }
2033
2034 int
2035 mm_answer_gss_userok(int sock, Buffer *m)
2036 {
2037         int authenticated;
2038
2039         if (!options.gss_authentication && !options.gss_keyex)
2040                 fatal("In GSSAPI monitor when GSSAPI is disabled");
2041
2042         authenticated = authctxt->valid && 
2043             ssh_gssapi_userok(authctxt->user, authctxt->pw);
2044
2045         buffer_clear(m);
2046         buffer_put_int(m, authenticated);
2047
2048         debug3("%s: sending result %d", __func__, authenticated);
2049         mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
2050
2051         auth_method = "gssapi-with-mic";
2052
2053         /* Monitor loop will terminate if authenticated */
2054         return (authenticated);
2055 }
2056
2057 int
2058 mm_answer_gss_error(int socket, Buffer *m) {
2059         OM_uint32 major,minor;
2060         char *msg;
2061
2062         msg=ssh_gssapi_last_error(gsscontext,&major,&minor);
2063         buffer_clear(m);
2064         buffer_put_int(m,major);
2065         buffer_put_int(m,minor);
2066         buffer_put_cstring(m,msg);
2067
2068         mm_request_send(socket,MONITOR_ANS_GSSERR,m);
2069
2070         xfree(msg);
2071         
2072         return(0);
2073 }
2074
2075 int
2076 mm_answer_gss_indicate_mechs(int socket, Buffer *m) {
2077         OM_uint32 major,minor;
2078         gss_OID_set mech_set;
2079         size_t i;
2080
2081         major=gss_indicate_mechs(&minor, &mech_set);
2082
2083         buffer_clear(m);
2084         buffer_put_int(m, major);
2085         buffer_put_int(m, mech_set->count);
2086         for (i=0; i < mech_set->count; i++) {
2087             buffer_put_string(m, mech_set->elements[i].elements,
2088                               mech_set->elements[i].length);
2089         }
2090
2091 #if !defined(MECHGLUE) /* mechglue memory management bug ??? */
2092         gss_release_oid_set(&minor,&mech_set);
2093 #endif
2094         
2095         mm_request_send(socket,MONITOR_ANS_GSSMECHS,m);
2096
2097         return(0);
2098 }
2099
2100 int
2101 mm_answer_gss_localname(int socket, Buffer *m) {
2102         char *name;
2103
2104         ssh_gssapi_localname(&name);
2105
2106         buffer_clear(m);
2107         if (name) {
2108             buffer_put_cstring(m, name);
2109             debug3("%s: sending result %s", __func__, name);
2110             xfree(name);
2111         } else {
2112             buffer_put_cstring(m, "");
2113             debug3("%s: sending result \"\"", __func__);
2114         }
2115
2116         mm_request_send(socket, MONITOR_ANS_GSSLOCALNAME, m);
2117
2118         return(0);
2119 }
2120
2121 int 
2122 mm_answer_gss_sign(int socket, Buffer *m)
2123 {
2124         gss_buffer_desc data;
2125         gss_buffer_desc hash = GSS_C_EMPTY_BUFFER;
2126         OM_uint32 major, minor;
2127         u_int len;
2128
2129         if (!options.gss_authentication && !options.gss_keyex)
2130                 fatal("In GSSAPI monitor when GSSAPI is disabled");
2131
2132         data.value = buffer_get_string(m, &len);
2133         data.length = len;
2134         if (data.length != 20) 
2135                 fatal("%s: data length incorrect: %d", __func__, 
2136                     (int) data.length);
2137
2138         /* Save the session ID on the first time around */
2139         if (session_id2_len == 0) {
2140                 session_id2_len = data.length;
2141                 session_id2 = xmalloc(session_id2_len);
2142                 memcpy(session_id2, data.value, session_id2_len);
2143         }
2144         major = ssh_gssapi_sign(gsscontext, &data, &hash);
2145
2146         xfree(data.value);
2147
2148         buffer_clear(m);
2149         buffer_put_int(m, major);
2150         buffer_put_string(m, hash.value, hash.length);
2151
2152         mm_request_send(socket, MONITOR_ANS_GSSSIGN, m);
2153
2154         gss_release_buffer(&minor, &hash);
2155
2156         /* Turn on getpwnam permissions */
2157         monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
2158         
2159         /* And credential updating, for when rekeying */
2160         monitor_permit(mon_dispatch, MONITOR_REQ_GSSUPCREDS, 1);
2161
2162         return (0);
2163 }
2164
2165 int
2166 mm_answer_gss_updatecreds(int socket, Buffer *m) {
2167         ssh_gssapi_ccache store;
2168         int ok;
2169
2170         store.filename = buffer_get_string(m, NULL);
2171         store.envvar   = buffer_get_string(m, NULL);
2172         store.envval   = buffer_get_string(m, NULL);
2173
2174         ok = ssh_gssapi_update_creds(&store);
2175
2176         xfree(store.filename);
2177         xfree(store.envvar);
2178         xfree(store.envval);
2179
2180         buffer_clear(m);
2181         buffer_put_int(m, ok);
2182
2183         mm_request_send(socket, MONITOR_ANS_GSSUPCREDS, m);
2184
2185         return(0);
2186 }
2187
2188 #endif /* GSSAPI */
2189
2190 #ifdef JPAKE
2191 int
2192 mm_answer_jpake_step1(int sock, Buffer *m)
2193 {
2194         struct jpake_ctx *pctx;
2195         u_char *x3_proof, *x4_proof;
2196         u_int x3_proof_len, x4_proof_len;
2197
2198         if (!options.zero_knowledge_password_authentication)
2199                 fatal("zero_knowledge_password_authentication disabled");
2200
2201         if (authctxt->jpake_ctx != NULL)
2202                 fatal("%s: authctxt->jpake_ctx already set (%p)",
2203                     __func__, authctxt->jpake_ctx);
2204         authctxt->jpake_ctx = pctx = jpake_new();
2205
2206         jpake_step1(pctx->grp,
2207             &pctx->server_id, &pctx->server_id_len,
2208             &pctx->x3, &pctx->x4, &pctx->g_x3, &pctx->g_x4,
2209             &x3_proof, &x3_proof_len,
2210             &x4_proof, &x4_proof_len);
2211
2212         JPAKE_DEBUG_CTX((pctx, "step1 done in %s", __func__));
2213
2214         buffer_clear(m);
2215
2216         buffer_put_string(m, pctx->server_id, pctx->server_id_len);
2217         buffer_put_bignum2(m, pctx->g_x3);
2218         buffer_put_bignum2(m, pctx->g_x4);
2219         buffer_put_string(m, x3_proof, x3_proof_len);
2220         buffer_put_string(m, x4_proof, x4_proof_len);
2221
2222         debug3("%s: sending step1", __func__);
2223         mm_request_send(sock, MONITOR_ANS_JPAKE_STEP1, m);
2224
2225         bzero(x3_proof, x3_proof_len);
2226         bzero(x4_proof, x4_proof_len);
2227         xfree(x3_proof);
2228         xfree(x4_proof);
2229
2230         monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_GET_PWDATA, 1);
2231         monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 0);
2232
2233         return 0;
2234 }
2235
2236 int
2237 mm_answer_jpake_get_pwdata(int sock, Buffer *m)
2238 {
2239         struct jpake_ctx *pctx = authctxt->jpake_ctx;
2240         char *hash_scheme, *salt;
2241
2242         if (pctx == NULL)
2243                 fatal("%s: pctx == NULL", __func__);
2244
2245         auth2_jpake_get_pwdata(authctxt, &pctx->s, &hash_scheme, &salt);
2246
2247         buffer_clear(m);
2248         /* pctx->s is sensitive, not returned to slave */
2249         buffer_put_cstring(m, hash_scheme);
2250         buffer_put_cstring(m, salt);
2251
2252         debug3("%s: sending pwdata", __func__);
2253         mm_request_send(sock, MONITOR_ANS_JPAKE_GET_PWDATA, m);
2254
2255         bzero(hash_scheme, strlen(hash_scheme));
2256         bzero(salt, strlen(salt));
2257         xfree(hash_scheme);
2258         xfree(salt);
2259
2260         monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP2, 1);
2261
2262         return 0;
2263 }
2264
2265 int
2266 mm_answer_jpake_step2(int sock, Buffer *m)
2267 {
2268         struct jpake_ctx *pctx = authctxt->jpake_ctx;
2269         u_char *x1_proof, *x2_proof, *x4_s_proof;
2270         u_int x1_proof_len, x2_proof_len, x4_s_proof_len;
2271
2272         if (pctx == NULL)
2273                 fatal("%s: pctx == NULL", __func__);
2274
2275         if ((pctx->g_x1 = BN_new()) == NULL ||
2276             (pctx->g_x2 = BN_new()) == NULL)
2277                 fatal("%s: BN_new", __func__);
2278         buffer_get_bignum2(m, pctx->g_x1);
2279         buffer_get_bignum2(m, pctx->g_x2);
2280         pctx->client_id = buffer_get_string(m, &pctx->client_id_len);
2281         x1_proof = buffer_get_string(m, &x1_proof_len);
2282         x2_proof = buffer_get_string(m, &x2_proof_len);
2283
2284         jpake_step2(pctx->grp, pctx->s, pctx->g_x3,
2285             pctx->g_x1, pctx->g_x2, pctx->x4,
2286             pctx->client_id, pctx->client_id_len,
2287             pctx->server_id, pctx->server_id_len,
2288             x1_proof, x1_proof_len,
2289             x2_proof, x2_proof_len,
2290             &pctx->b,
2291             &x4_s_proof, &x4_s_proof_len);
2292
2293         JPAKE_DEBUG_CTX((pctx, "step2 done in %s", __func__));
2294
2295         bzero(x1_proof, x1_proof_len);
2296         bzero(x2_proof, x2_proof_len);
2297         xfree(x1_proof);
2298         xfree(x2_proof);
2299
2300         buffer_clear(m);
2301
2302         buffer_put_bignum2(m, pctx->b);
2303         buffer_put_string(m, x4_s_proof, x4_s_proof_len);
2304
2305         debug3("%s: sending step2", __func__);
2306         mm_request_send(sock, MONITOR_ANS_JPAKE_STEP2, m);
2307
2308         bzero(x4_s_proof, x4_s_proof_len);
2309         xfree(x4_s_proof);
2310
2311         monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_KEY_CONFIRM, 1);
2312
2313         return 0;
2314 }
2315
2316 int
2317 mm_answer_jpake_key_confirm(int sock, Buffer *m)
2318 {
2319         struct jpake_ctx *pctx = authctxt->jpake_ctx;
2320         u_char *x2_s_proof;
2321         u_int x2_s_proof_len;
2322
2323         if (pctx == NULL)
2324                 fatal("%s: pctx == NULL", __func__);
2325
2326         if ((pctx->a = BN_new()) == NULL)
2327                 fatal("%s: BN_new", __func__);
2328         buffer_get_bignum2(m, pctx->a);
2329         x2_s_proof = buffer_get_string(m, &x2_s_proof_len);
2330
2331         jpake_key_confirm(pctx->grp, pctx->s, pctx->a,
2332             pctx->x4, pctx->g_x3, pctx->g_x4, pctx->g_x1, pctx->g_x2,
2333             pctx->server_id, pctx->server_id_len,
2334             pctx->client_id, pctx->client_id_len,
2335             session_id2, session_id2_len,
2336             x2_s_proof, x2_s_proof_len,
2337             &pctx->k,
2338             &pctx->h_k_sid_sessid, &pctx->h_k_sid_sessid_len);
2339
2340         JPAKE_DEBUG_CTX((pctx, "key_confirm done in %s", __func__));
2341
2342         bzero(x2_s_proof, x2_s_proof_len);
2343         buffer_clear(m);
2344
2345         /* pctx->k is sensitive, not sent */
2346         buffer_put_string(m, pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
2347
2348         debug3("%s: sending confirmation hash", __func__);
2349         mm_request_send(sock, MONITOR_ANS_JPAKE_KEY_CONFIRM, m);
2350
2351         monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_CHECK_CONFIRM, 1);
2352
2353         return 0;
2354 }
2355
2356 int
2357 mm_answer_jpake_check_confirm(int sock, Buffer *m)
2358 {
2359         int authenticated = 0;
2360         u_char *peer_confirm_hash;
2361         u_int peer_confirm_hash_len;
2362         struct jpake_ctx *pctx = authctxt->jpake_ctx;
2363
2364         if (pctx == NULL)
2365                 fatal("%s: pctx == NULL", __func__);
2366
2367         peer_confirm_hash = buffer_get_string(m, &peer_confirm_hash_len);
2368
2369         authenticated = jpake_check_confirm(pctx->k,
2370             pctx->client_id, pctx->client_id_len,
2371             session_id2, session_id2_len,
2372             peer_confirm_hash, peer_confirm_hash_len) && authctxt->valid;
2373
2374         JPAKE_DEBUG_CTX((pctx, "check_confirm done in %s", __func__));
2375
2376         bzero(peer_confirm_hash, peer_confirm_hash_len);
2377         xfree(peer_confirm_hash);
2378
2379         buffer_clear(m);
2380         buffer_put_int(m, authenticated);
2381
2382         debug3("%s: sending result %d", __func__, authenticated);
2383         mm_request_send(sock, MONITOR_ANS_JPAKE_CHECK_CONFIRM, m);
2384
2385         monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 1);
2386
2387         auth_method = "jpake-01@openssh.com";
2388         return authenticated;
2389 }
2390
2391 #endif /* JPAKE */
This page took 0.219032 seconds and 3 git commands to generate.