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