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