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