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