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