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