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