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