]> andersk Git - openssh.git/blame - monitor.c
- markus@cvs.openbsd.org 2002/09/09 14:54:15
[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"
661e45a0 28RCSID("$OpenBSD: monitor.c,v 1.26 2002/09/09 14:54:15 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;
9459414c 86 u_char *ssh1key;
87 u_int ssh1keylen;
1853d1ef 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
98int mm_answer_moduli(int, Buffer *);
99int mm_answer_sign(int, Buffer *);
100int mm_answer_pwnamallow(int, Buffer *);
94a73cdc 101int mm_answer_auth2_read_banner(int, Buffer *);
1853d1ef 102int mm_answer_authserv(int, Buffer *);
103int mm_answer_authpassword(int, Buffer *);
104int mm_answer_bsdauthquery(int, Buffer *);
105int mm_answer_bsdauthrespond(int, Buffer *);
106int mm_answer_skeyquery(int, Buffer *);
107int mm_answer_skeyrespond(int, Buffer *);
108int mm_answer_keyallowed(int, Buffer *);
109int mm_answer_keyverify(int, Buffer *);
110int mm_answer_pty(int, Buffer *);
111int mm_answer_pty_cleanup(int, Buffer *);
112int mm_answer_term(int, Buffer *);
113int mm_answer_rsa_keyallowed(int, Buffer *);
114int mm_answer_rsa_challenge(int, Buffer *);
115int mm_answer_rsa_response(int, Buffer *);
116int mm_answer_sesskey(int, Buffer *);
117int mm_answer_sessid(int, Buffer *);
118
ad200abb 119#ifdef USE_PAM
120int mm_answer_pam_start(int, Buffer *);
121#endif
122
696f6bef 123#ifdef KRB5
124int mm_answer_krb5(int, Buffer *);
125#endif
126
1853d1ef 127static Authctxt *authctxt;
128static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
129
130/* local state for key verify */
131static u_char *key_blob = NULL;
132static u_int key_bloblen = 0;
133static int key_blobtype = MM_NOKEY;
661e45a0 134static char *hostbased_cuser = NULL;
135static char *hostbased_chost = NULL;
1853d1ef 136static char *auth_method = "unknown";
521d606b 137static int session_id2_len = 0;
138static u_char *session_id2 = NULL;
1853d1ef 139
140struct 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
154struct 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},
94a73cdc 159 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
1853d1ef 160 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
ad200abb 161#ifdef USE_PAM
162 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
8b314ec9 163#endif
1853d1ef 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
177struct 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
186struct 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},
efe44db6 202#endif
203#ifdef USE_PAM
204 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
696f6bef 205#endif
206#ifdef KRB5
207 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
1853d1ef 208#endif
209 {0, 0, NULL}
210};
211
212struct 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
219struct mon_table *mon_dispatch;
220
221/* Specifies if a certain message is allowed at the moment */
222
223static void
224monitor_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
236static void
237monitor_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
250Authctxt *
b5a28cbc 251monitor_child_preauth(struct monitor *pmonitor)
1853d1ef 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) {
b5a28cbc 274 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
1853d1ef 275 if (authenticated) {
276 if (!(ent->flags & MON_AUTHDECIDE))
277 fatal("%s: unexpected authentication from %d",
1588c277 278 __func__, ent->type);
1853d1ef 279 if (authctxt->pw->pw_uid == 0 &&
280 !auth_root_allowed(auth_method))
281 authenticated = 0;
ad200abb 282#ifdef USE_PAM
283 if (!do_pam_account(authctxt->pw->pw_name, NULL))
284 authenticated = 0;
285#endif
1853d1ef 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)
1588c277 297 fatal("%s: authenticated invalid user", __func__);
1853d1ef 298
299 debug("%s: %s has been authenticated by privileged process",
1588c277 300 __func__, authctxt->user);
1853d1ef 301
b5a28cbc 302 mm_get_keystate(pmonitor);
1853d1ef 303
304 return (authctxt);
305}
306
307void
b5a28cbc 308monitor_child_postauth(struct monitor *pmonitor)
1853d1ef 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 (;;)
b5a28cbc 328 monitor_read(pmonitor, mon_dispatch, NULL);
1853d1ef 329}
330
331void
b5a28cbc 332monitor_sync(struct monitor *pmonitor)
1853d1ef 333{
0496cf34 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 }
1853d1ef 338}
339
340int
b5a28cbc 341monitor_read(struct monitor *pmonitor, struct mon_table *ent,
1853d1ef 342 struct mon_table **pent)
343{
344 Buffer m;
345 int ret;
346 u_char type;
347
348 buffer_init(&m);
349
b5a28cbc 350 mm_request_receive(pmonitor->m_sendfd, &m);
1853d1ef 351 type = buffer_get_char(&m);
352
1588c277 353 debug3("%s: checking request %d", __func__, type);
1853d1ef 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))
1588c277 363 fatal("%s: unpermitted request %d", __func__,
1853d1ef 364 type);
b5a28cbc 365 ret = (*ent->f)(pmonitor->m_sendfd, &m);
1853d1ef 366 buffer_free(&m);
367
368 /* The child may use this request only once, disable it */
369 if (ent->flags & MON_ONCE) {
1588c277 370 debug2("%s: %d used once, disabling now", __func__,
1853d1ef 371 type);
372 ent->flags &= ~MON_PERMIT;
373 }
374
375 if (pent != NULL)
376 *pent = ent;
377
378 return ret;
379 }
380
1588c277 381 fatal("%s: unsupported request: %d", __func__, type);
1853d1ef 382
383 /* NOTREACHED */
384 return (-1);
385}
386
387/* allowed key state */
388static int
389monitor_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
398static void
399monitor_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
415int
416mm_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",
1588c277 426 __func__, min, want, max);
1853d1ef 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",
1588c277 430 __func__, min, want, max);
1853d1ef 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
450int
451mm_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
1588c277 459 debug3("%s", __func__);
1853d1ef 460
461 keyid = buffer_get_int(m);
462 p = buffer_get_string(m, &datlen);
463
464 if (datlen != 20)
d1ff09ba 465 fatal("%s: data length incorrect: %u", __func__, datlen);
1853d1ef 466
521d606b 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
1853d1ef 474 if ((key = get_hostkey_by_index(keyid)) == NULL)
1588c277 475 fatal("%s: no hostkey from index %d", __func__, keyid);
1853d1ef 476 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
1588c277 477 fatal("%s: key_sign failed", __func__);
1853d1ef 478
d1ff09ba 479 debug3("%s: signature %p(%u)", __func__, signature, siglen);
1853d1ef 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
497int
498mm_answer_pwnamallow(int socket, Buffer *m)
499{
500 char *login;
501 struct passwd *pwent;
502 int allowed = 0;
503
1588c277 504 debug3("%s", __func__);
1853d1ef 505
506 if (authctxt->attempt++ != 0)
1588c277 507 fatal("%s: multiple attempts for getpwnam", __func__);
1853d1ef 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);
7b18c353 533#ifdef HAVE_PW_CLASS_IN_PASSWD
1853d1ef 534 buffer_put_cstring(m, pwent->pw_class);
7b18c353 535#endif
1853d1ef 536 buffer_put_cstring(m, pwent->pw_dir);
537 buffer_put_cstring(m, pwent->pw_shell);
538
539 out:
1588c277 540 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
1853d1ef 541 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
542
543 /* For SSHv1 allow authentication now */
544 if (!compat20)
545 monitor_permit_authentications(1);
94a73cdc 546 else {
1853d1ef 547 /* Allow service/style information on the auth context */
548 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
94a73cdc 549 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
550 }
1853d1ef 551
efe44db6 552#ifdef USE_PAM
553 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
554#endif
1853d1ef 555
556 return (0);
557}
558
94a73cdc 559int 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)
30e37ee6 569 xfree(banner);
94a73cdc 570
571 return (0);
572}
573
1853d1ef 574int
575mm_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",
1588c277 582 __func__, authctxt->service, authctxt->style);
1853d1ef 583
584 if (strlen(authctxt->style) == 0) {
585 xfree(authctxt->style);
586 authctxt->style = NULL;
587 }
588
589 return (0);
590}
591
592int
593mm_answer_authpassword(int socket, Buffer *m)
594{
595 static int call_count;
596 char *passwd;
d341742a 597 int authenticated;
598 u_int plen;
1853d1ef 599
600 passwd = buffer_get_string(m, &plen);
601 /* Only authenticate if the context is valid */
aa586f8e 602 authenticated = options.password_authentication &&
603 authctxt->valid && auth_password(authctxt, passwd);
1853d1ef 604 memset(passwd, 0, strlen(passwd));
605 xfree(passwd);
606
607 buffer_clear(m);
608 buffer_put_int(m, authenticated);
609
1588c277 610 debug3("%s: sending result %d", __func__, authenticated);
1853d1ef 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
624int
625mm_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
1588c277 641 debug3("%s: sending challenge res: %d", __func__, res);
1853d1ef 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
654int
655mm_answer_bsdauthrespond(int socket, Buffer *m)
656{
657 char *response;
658 int authok;
659
660 if (authctxt->as == 0)
1588c277 661 fatal("%s: no bsd auth session", __func__);
1853d1ef 662
663 response = buffer_get_string(m, NULL);
aa586f8e 664 authok = options.challenge_response_authentication &&
665 auth_userresponse(authctxt->as, response, 0);
1853d1ef 666 authctxt->as = NULL;
1588c277 667 debug3("%s: <%s> = <%d>", __func__, response, authok);
1853d1ef 668 xfree(response);
669
670 buffer_clear(m);
671 buffer_put_int(m, authok);
672
1588c277 673 debug3("%s: sending authenticated: %d", __func__, authok);
1853d1ef 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
683int
684mm_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
1588c277 697 debug3("%s: sending challenge res: %d", __func__, res);
1853d1ef 698 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
699
700 return (0);
701}
702
703int
704mm_answer_skeyrespond(int socket, Buffer *m)
705{
706 char *response;
707 int authok;
708
709 response = buffer_get_string(m, NULL);
710
aa586f8e 711 authok = (options.challenge_response_authentication &&
712 authctxt->valid &&
1853d1ef 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
1588c277 721 debug3("%s: sending authenticated: %d", __func__, authok);
1853d1ef 722 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
723
724 auth_method = "skey";
725
726 return (authok != 0);
727}
728#endif
729
ad200abb 730#ifdef USE_PAM
731int
732mm_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
1853d1ef 746static void
747mm_append_debug(Buffer *m)
748{
749 if (auth_debug_init && buffer_len(&auth_debug)) {
1588c277 750 debug3("%s: Appending debug messages for child", __func__);
1853d1ef 751 buffer_append(m, buffer_ptr(&auth_debug),
752 buffer_len(&auth_debug));
753 buffer_clear(&auth_debug);
754 }
755}
756
757int
758mm_answer_keyallowed(int socket, Buffer *m)
759{
760 Key *key;
661e45a0 761 char *cuser, *chost;
762 u_char *blob;
1853d1ef 763 u_int bloblen;
764 enum mm_keytype type = 0;
765 int allowed = 0;
766
1588c277 767 debug3("%s entering", __func__);
1853d1ef 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))
1588c277 778 fatal("%s: key type and protocol mismatch", __func__);
1853d1ef 779
1588c277 780 debug3("%s: key_from_blob: %p", __func__, key);
1853d1ef 781
782 if (key != NULL && authctxt->pw != NULL) {
783 switch(type) {
784 case MM_USERKEY:
aa586f8e 785 allowed = options.pubkey_authentication &&
786 user_key_allowed(authctxt->pw, key);
1853d1ef 787 break;
788 case MM_HOSTKEY:
aa586f8e 789 allowed = options.hostbased_authentication &&
790 hostbased_key_allowed(authctxt->pw,
1853d1ef 791 cuser, chost, key);
792 break;
793 case MM_RSAHOSTKEY:
794 key->type = KEY_RSA1; /* XXX */
aa586f8e 795 allowed = options.rhosts_rsa_authentication &&
796 auth_rhosts_rsa_key_allowed(authctxt->pw,
1853d1ef 797 cuser, chost, key);
798 break;
799 default:
1588c277 800 fatal("%s: unknown key type %d", __func__, type);
1853d1ef 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",
1588c277 819 __func__, key, allowed ? "allowed" : "disallowed");
1853d1ef 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
834static int
835monitor_valid_userblob(u_char *data, u_int datalen)
836{
837 Buffer b;
661e45a0 838 char *p;
1853d1ef 839 u_int len;
840 int fail = 0;
1853d1ef 841
842 buffer_init(&b);
843 buffer_append(&b, data, datalen);
844
845 if (datafellows & SSH_OLD_SESSIONID) {
521d606b 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++;
1853d1ef 852 buffer_consume(&b, session_id2_len);
853 } else {
521d606b 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))
1853d1ef 858 fail++;
521d606b 859 xfree(p);
1853d1ef 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
890static int
661e45a0 891monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
892 char *chost)
1853d1ef 893{
894 Buffer b;
661e45a0 895 char *p;
1853d1ef 896 u_int len;
897 int fail = 0;
1853d1ef 898
899 buffer_init(&b);
900 buffer_append(&b, data, datalen);
901
521d606b 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))
1853d1ef 906 fail++;
521d606b 907 xfree(p);
908
1853d1ef 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
946int
947mm_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 ||
300e01c4 960 !monitor_allowed_key(blob, bloblen))
1588c277 961 fatal("%s: bad key, not previously allowed", __func__);
1853d1ef 962
963 key = key_from_blob(blob, bloblen);
964 if (key == NULL)
1588c277 965 fatal("%s: bad public key blob", __func__);
1853d1ef 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)
1588c277 980 fatal("%s: bad signature data blob", __func__);
1853d1ef 981
982 verified = key_verify(key, signature, signaturelen, data, datalen);
983 debug3("%s: key %p signature %s",
1588c277 984 __func__, key, verified ? "verified" : "unverified");
1853d1ef 985
986 key_free(key);
987 xfree(blob);
988 xfree(signature);
989 xfree(data);
990
d17ef027 991 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
992
1853d1ef 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
1853d1ef 999 return (verified);
1000}
1001
1002static void
1003mm_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));
ba1566dd 1013 fromlen = sizeof(from);
1853d1ef 1014 if (packet_connection_is_on_socket()) {
1853d1ef 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),
ba1566dd 1024 (struct sockaddr *)&from, fromlen);
1853d1ef 1025}
1026
1027static void
1028mm_session_close(Session *s)
1029{
1588c277 1030 debug3("%s: session %d pid %d", __func__, s->self, s->pid);
1853d1ef 1031 if (s->ttyfd != -1) {
1588c277 1032 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1853d1ef 1033 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1034 session_pty_cleanup2(s);
1035 }
1036 s->used = 0;
1037}
1038
1039int
1040mm_answer_pty(int socket, Buffer *m)
1041{
b5a28cbc 1042 extern struct monitor *pmonitor;
1853d1ef 1043 Session *s;
1044 int res, fd0;
1045
1588c277 1046 debug3("%s entering", __func__);
1853d1ef 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;
b5a28cbc 1054 s->pid = pmonitor->m_pid;
1853d1ef 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)
1588c277 1070 fatal("%s: dup2", __func__);
1853d1ef 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)
1588c277 1079 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1853d1ef 1080 if (fd0 != 0)
1588c277 1081 error("%s: fd0 %d != 0", __func__, fd0);
1853d1ef 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
1588c277 1089 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1853d1ef 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
1101int
1102mm_answer_pty_cleanup(int socket, Buffer *m)
1103{
1104 Session *s;
1105 char *tty;
1106
1588c277 1107 debug3("%s entering", __func__);
1853d1ef 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
1117int
1118mm_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)
1588c277 1127 fatal("%s: BN_new", __func__);
1853d1ef 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
1147int
1148mm_answer_sessid(int socket, Buffer *m)
1149{
1150 int i;
1151
1588c277 1152 debug3("%s entering", __func__);
1853d1ef 1153
1154 if (buffer_len(m) != 16)
1588c277 1155 fatal("%s: bad ssh1 session id", __func__);
1853d1ef 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
1165int
1166mm_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
1588c277 1174 debug3("%s entering", __func__);
1853d1ef 1175
aa586f8e 1176 if (options.rsa_authentication && authctxt->valid) {
1853d1ef 1177 if ((client_n = BN_new()) == NULL)
1588c277 1178 fatal("%s: BN_new", __func__);
1853d1ef 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)
1588c277 1192 fatal("%s: key_to_blob failed", __func__);
1853d1ef 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
1211int
1212mm_answer_rsa_challenge(int socket, Buffer *m)
1213{
1214 Key *key = NULL;
1215 u_char *blob;
1216 u_int blen;
1217
1588c277 1218 debug3("%s entering", __func__);
1853d1ef 1219
1220 if (!authctxt->valid)
1588c277 1221 fatal("%s: authctxt not valid", __func__);
1853d1ef 1222 blob = buffer_get_string(m, &blen);
1223 if (!monitor_allowed_key(blob, blen))
1588c277 1224 fatal("%s: bad key, not previously allowed", __func__);
1853d1ef 1225 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1588c277 1226 fatal("%s: key type mismatch", __func__);
1853d1ef 1227 if ((key = key_from_blob(blob, blen)) == NULL)
1588c277 1228 fatal("%s: received bad key", __func__);
1853d1ef 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
1588c277 1237 debug3("%s sending reply", __func__);
1853d1ef 1238 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1239
1240 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1241 return (0);
1242}
1243
1244int
1245mm_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
1588c277 1252 debug3("%s entering", __func__);
1853d1ef 1253
1254 if (!authctxt->valid)
1588c277 1255 fatal("%s: authctxt not valid", __func__);
1853d1ef 1256 if (ssh1_challenge == NULL)
1588c277 1257 fatal("%s: no ssh1_challenge", __func__);
1853d1ef 1258
1259 blob = buffer_get_string(m, &blen);
1260 if (!monitor_allowed_key(blob, blen))
1588c277 1261 fatal("%s: bad key, not previously allowed", __func__);
1853d1ef 1262 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1588c277 1263 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1853d1ef 1264 if ((key = key_from_blob(blob, blen)) == NULL)
1588c277 1265 fatal("%s: received bad key", __func__);
1853d1ef 1266 response = buffer_get_string(m, &len);
1267 if (len != 16)
1588c277 1268 fatal("%s: received bad response to challenge", __func__);
1853d1ef 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
696f6bef 1288
1289#ifdef KRB5
1290int
1291mm_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 = auth_krb5(authctxt, &tkt, &client_user, &reply);
1303
1304 if (tkt.length)
1305 xfree(tkt.data);
1306
1307 buffer_clear(m);
1308 buffer_put_int(m, success);
1309
1310 if (success) {
1311 buffer_put_cstring(m, client_user);
1312 buffer_put_string(m, reply.data, reply.length);
1313 if (client_user)
1314 xfree(client_user);
1315 if (reply.length)
1316 xfree(reply.data);
1317 }
1318 mm_request_send(socket, MONITOR_ANS_KRB5, m);
1319
1320 return success;
1321}
1322#endif
1323
1853d1ef 1324int
1325mm_answer_term(int socket, Buffer *req)
1326{
b5a28cbc 1327 extern struct monitor *pmonitor;
1853d1ef 1328 int res, status;
1329
1588c277 1330 debug3("%s: tearing down sessions", __func__);
1853d1ef 1331
1332 /* The child is terminating */
1333 session_destroy_all(&mm_session_close);
1334
b5a28cbc 1335 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
8c38e88b 1336 if (errno != EINTR)
1337 exit(1);
1853d1ef 1338
1339 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1340
1341 /* Terminate process */
1342 exit (res);
1343}
1344
1345void
b5a28cbc 1346monitor_apply_keystate(struct monitor *pmonitor)
1853d1ef 1347{
1348 if (compat20) {
1349 set_newkeys(MODE_IN);
1350 set_newkeys(MODE_OUT);
1351 } else {
1853d1ef 1352 packet_set_protocol_flags(child_state.ssh1protoflags);
9459414c 1353 packet_set_encryption_key(child_state.ssh1key,
1354 child_state.ssh1keylen, child_state.ssh1cipher);
1355 xfree(child_state.ssh1key);
1853d1ef 1356 }
1357
9459414c 1358 /* for rc4 and other stateful ciphers */
1853d1ef 1359 packet_set_keycontext(MODE_OUT, child_state.keyout);
1360 xfree(child_state.keyout);
1361 packet_set_keycontext(MODE_IN, child_state.keyin);
1362 xfree(child_state.keyin);
1363
1364 if (!compat20) {
1365 packet_set_iv(MODE_OUT, child_state.ivout);
1366 xfree(child_state.ivout);
1367 packet_set_iv(MODE_IN, child_state.ivin);
1368 xfree(child_state.ivin);
1369 }
1370
1371 memcpy(&incoming_stream, &child_state.incoming,
1372 sizeof(incoming_stream));
1373 memcpy(&outgoing_stream, &child_state.outgoing,
1374 sizeof(outgoing_stream));
1375
1376 /* Update with new address */
0496cf34 1377 if (options.compression)
1378 mm_init_compression(pmonitor->m_zlib);
1853d1ef 1379
1380 /* Network I/O buffers */
1381 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1382 buffer_clear(&input);
1383 buffer_append(&input, child_state.input, child_state.ilen);
1384 memset(child_state.input, 0, child_state.ilen);
1385 xfree(child_state.input);
1386
1387 buffer_clear(&output);
1388 buffer_append(&output, child_state.output, child_state.olen);
1389 memset(child_state.output, 0, child_state.olen);
1390 xfree(child_state.output);
1391}
1392
1393static Kex *
1394mm_get_kex(Buffer *m)
1395{
1396 Kex *kex;
1397 void *blob;
1398 u_int bloblen;
1399
1400 kex = xmalloc(sizeof(*kex));
1401 memset(kex, 0, sizeof(*kex));
1402 kex->session_id = buffer_get_string(m, &kex->session_id_len);
521d606b 1403 if ((session_id2 == NULL) ||
1404 (kex->session_id_len != session_id2_len) ||
1405 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1406 fatal("mm_get_get: internal error: bad session id");
1853d1ef 1407 kex->we_need = buffer_get_int(m);
1408 kex->server = 1;
1409 kex->hostkey_type = buffer_get_int(m);
1410 kex->kex_type = buffer_get_int(m);
1411 blob = buffer_get_string(m, &bloblen);
1412 buffer_init(&kex->my);
1413 buffer_append(&kex->my, blob, bloblen);
1414 xfree(blob);
1415 blob = buffer_get_string(m, &bloblen);
1416 buffer_init(&kex->peer);
1417 buffer_append(&kex->peer, blob, bloblen);
1418 xfree(blob);
1419 kex->done = 1;
1420 kex->flags = buffer_get_int(m);
1421 kex->client_version_string = buffer_get_string(m, NULL);
1422 kex->server_version_string = buffer_get_string(m, NULL);
1423 kex->load_host_key=&get_hostkey_by_type;
1424 kex->host_key_index=&get_hostkey_index;
1425
1426 return (kex);
1427}
1428
1429/* This function requries careful sanity checking */
1430
1431void
b5a28cbc 1432mm_get_keystate(struct monitor *pmonitor)
1853d1ef 1433{
1434 Buffer m;
1435 u_char *blob, *p;
1436 u_int bloblen, plen;
1437
1588c277 1438 debug3("%s: Waiting for new keys", __func__);
1853d1ef 1439
1440 buffer_init(&m);
b5a28cbc 1441 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1853d1ef 1442 if (!compat20) {
1443 child_state.ssh1protoflags = buffer_get_int(&m);
1444 child_state.ssh1cipher = buffer_get_int(&m);
9459414c 1445 child_state.ssh1key = buffer_get_string(&m,
1446 &child_state.ssh1keylen);
1853d1ef 1447 child_state.ivout = buffer_get_string(&m,
1448 &child_state.ivoutlen);
1449 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1450 goto skip;
1451 } else {
1452 /* Get the Kex for rekeying */
b5a28cbc 1453 *pmonitor->m_pkex = mm_get_kex(&m);
1853d1ef 1454 }
1455
1456 blob = buffer_get_string(&m, &bloblen);
1457 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1458 xfree(blob);
1459
1588c277 1460 debug3("%s: Waiting for second key", __func__);
1853d1ef 1461 blob = buffer_get_string(&m, &bloblen);
1462 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1463 xfree(blob);
1464
1465 /* Now get sequence numbers for the packets */
1466 packet_set_seqnr(MODE_OUT, buffer_get_int(&m));
1467 packet_set_seqnr(MODE_IN, buffer_get_int(&m));
1468
1469 skip:
1470 /* Get the key context */
1471 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1472 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1473
1588c277 1474 debug3("%s: Getting compression state", __func__);
1853d1ef 1475 /* Get compression state */
1476 p = buffer_get_string(&m, &plen);
1477 if (plen != sizeof(child_state.outgoing))
1588c277 1478 fatal("%s: bad request size", __func__);
1853d1ef 1479 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1480 xfree(p);
1481
1482 p = buffer_get_string(&m, &plen);
1483 if (plen != sizeof(child_state.incoming))
1588c277 1484 fatal("%s: bad request size", __func__);
1853d1ef 1485 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1486 xfree(p);
1487
1488 /* Network I/O buffers */
1588c277 1489 debug3("%s: Getting Network I/O buffers", __func__);
1853d1ef 1490 child_state.input = buffer_get_string(&m, &child_state.ilen);
1491 child_state.output = buffer_get_string(&m, &child_state.olen);
1492
1493 buffer_free(&m);
1494}
1495
1496
1497/* Allocation functions for zlib */
1498void *
1499mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1500{
b85698ab 1501 size_t len = size * ncount;
1853d1ef 1502 void *address;
1503
b85698ab 1504 if (len == 0 || ncount > SIZE_T_MAX / size)
2f095a0e 1505 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1506
1507 address = mm_malloc(mm, len);
1853d1ef 1508
1509 return (address);
1510}
1511
1512void
1513mm_zfree(struct mm_master *mm, void *address)
1514{
1515 mm_free(mm, address);
1516}
1517
1518void
1519mm_init_compression(struct mm_master *mm)
1520{
1521 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1522 outgoing_stream.zfree = (free_func)mm_zfree;
1523 outgoing_stream.opaque = mm;
1524
1525 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1526 incoming_stream.zfree = (free_func)mm_zfree;
1527 incoming_stream.opaque = mm;
1528}
1529
1530/* XXX */
1531
1532#define FD_CLOSEONEXEC(x) do { \
1533 if (fcntl(x, F_SETFD, 1) == -1) \
1534 fatal("fcntl(%d, F_SETFD)", x); \
1535} while (0)
1536
1537static void
1538monitor_socketpair(int *pair)
1539{
f1af2dbf 1540#ifdef HAVE_SOCKETPAIR
1853d1ef 1541 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1588c277 1542 fatal("%s: socketpair", __func__);
f1af2dbf 1543#else
1544 fatal("%s: UsePrivilegeSeparation=yes not supported",
1588c277 1545 __func__);
f1af2dbf 1546#endif
1853d1ef 1547 FD_CLOSEONEXEC(pair[0]);
1548 FD_CLOSEONEXEC(pair[1]);
1549}
1550
1551#define MM_MEMSIZE 65536
1552
1553struct monitor *
1554monitor_init(void)
1555{
1556 struct monitor *mon;
1557 int pair[2];
1558
1559 mon = xmalloc(sizeof(*mon));
1560
1561 monitor_socketpair(pair);
1562
1563 mon->m_recvfd = pair[0];
1564 mon->m_sendfd = pair[1];
1565
1566 /* Used to share zlib space across processes */
0496cf34 1567 if (options.compression) {
1568 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1569 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1853d1ef 1570
0496cf34 1571 /* Compression needs to share state across borders */
1572 mm_init_compression(mon->m_zlib);
1573 }
1853d1ef 1574
1575 return mon;
1576}
1577
1578void
1579monitor_reinit(struct monitor *mon)
1580{
1581 int pair[2];
1582
1583 monitor_socketpair(pair);
1584
1585 mon->m_recvfd = pair[0];
1586 mon->m_sendfd = pair[1];
1587}
This page took 0.763776 seconds and 5 git commands to generate.