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