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