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