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