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