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