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