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