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