]> andersk Git - openssh.git/blame - monitor.c
- jmc@cvs.openbsd.org 2003/05/14 08:25:39
[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
7fceb20d 570 if (options.use_pam)
571 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
efe44db6 572#endif
1853d1ef 573
574 return (0);
575}
576
94a73cdc 577int mm_answer_auth2_read_banner(int socket, Buffer *m)
578{
579 char *banner;
580
581 buffer_clear(m);
582 banner = auth2_read_banner();
583 buffer_put_cstring(m, banner != NULL ? banner : "");
584 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
585
586 if (banner != NULL)
30e37ee6 587 xfree(banner);
94a73cdc 588
589 return (0);
590}
591
1853d1ef 592int
593mm_answer_authserv(int socket, Buffer *m)
594{
595 monitor_permit_authentications(1);
596
597 authctxt->service = buffer_get_string(m, NULL);
598 authctxt->style = buffer_get_string(m, NULL);
599 debug3("%s: service=%s, style=%s",
1588c277 600 __func__, authctxt->service, authctxt->style);
1853d1ef 601
602 if (strlen(authctxt->style) == 0) {
603 xfree(authctxt->style);
604 authctxt->style = NULL;
605 }
606
607 return (0);
608}
609
610int
611mm_answer_authpassword(int socket, Buffer *m)
612{
613 static int call_count;
614 char *passwd;
d341742a 615 int authenticated;
616 u_int plen;
1853d1ef 617
618 passwd = buffer_get_string(m, &plen);
619 /* Only authenticate if the context is valid */
aa586f8e 620 authenticated = options.password_authentication &&
68ece370 621 auth_password(authctxt, passwd) && authctxt->valid;
1853d1ef 622 memset(passwd, 0, strlen(passwd));
623 xfree(passwd);
624
625 buffer_clear(m);
626 buffer_put_int(m, authenticated);
627
1588c277 628 debug3("%s: sending result %d", __func__, authenticated);
1853d1ef 629 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
630
631 call_count++;
632 if (plen == 0 && call_count == 1)
633 auth_method = "none";
634 else
635 auth_method = "password";
636
637 /* Causes monitor loop to terminate if authenticated */
638 return (authenticated);
639}
640
641#ifdef BSD_AUTH
642int
643mm_answer_bsdauthquery(int socket, Buffer *m)
644{
645 char *name, *infotxt;
646 u_int numprompts;
647 u_int *echo_on;
648 char **prompts;
6d6c25e3 649 u_int success;
1853d1ef 650
6d6c25e3 651 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
652 &prompts, &echo_on) < 0 ? 0 : 1;
1853d1ef 653
654 buffer_clear(m);
6d6c25e3 655 buffer_put_int(m, success);
656 if (success)
1853d1ef 657 buffer_put_cstring(m, prompts[0]);
658
6d6c25e3 659 debug3("%s: sending challenge success: %u", __func__, success);
1853d1ef 660 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
661
6d6c25e3 662 if (success) {
1853d1ef 663 xfree(name);
664 xfree(infotxt);
665 xfree(prompts);
666 xfree(echo_on);
667 }
668
669 return (0);
670}
671
672int
673mm_answer_bsdauthrespond(int socket, Buffer *m)
674{
675 char *response;
676 int authok;
677
678 if (authctxt->as == 0)
1588c277 679 fatal("%s: no bsd auth session", __func__);
1853d1ef 680
681 response = buffer_get_string(m, NULL);
aa586f8e 682 authok = options.challenge_response_authentication &&
683 auth_userresponse(authctxt->as, response, 0);
1853d1ef 684 authctxt->as = NULL;
1588c277 685 debug3("%s: <%s> = <%d>", __func__, response, authok);
1853d1ef 686 xfree(response);
687
688 buffer_clear(m);
689 buffer_put_int(m, authok);
690
1588c277 691 debug3("%s: sending authenticated: %d", __func__, authok);
1853d1ef 692 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
693
694 auth_method = "bsdauth";
695
696 return (authok != 0);
697}
698#endif
699
700#ifdef SKEY
701int
702mm_answer_skeyquery(int socket, Buffer *m)
703{
704 struct skey skey;
705 char challenge[1024];
6d6c25e3 706 u_int success;
1853d1ef 707
6d6c25e3 708 success = skeychallenge(&skey, authctxt->user, challenge) < 0 ? 0 : 1;
1853d1ef 709
710 buffer_clear(m);
6d6c25e3 711 buffer_put_int(m, success);
712 if (success)
1853d1ef 713 buffer_put_cstring(m, challenge);
714
6d6c25e3 715 debug3("%s: sending challenge success: %u", __func__, success);
1853d1ef 716 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
717
718 return (0);
719}
720
721int
722mm_answer_skeyrespond(int socket, Buffer *m)
723{
724 char *response;
725 int authok;
726
727 response = buffer_get_string(m, NULL);
728
aa586f8e 729 authok = (options.challenge_response_authentication &&
730 authctxt->valid &&
1853d1ef 731 skey_haskey(authctxt->pw->pw_name) == 0 &&
732 skey_passcheck(authctxt->pw->pw_name, response) != -1);
733
734 xfree(response);
735
736 buffer_clear(m);
737 buffer_put_int(m, authok);
738
1588c277 739 debug3("%s: sending authenticated: %d", __func__, authok);
1853d1ef 740 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
741
742 auth_method = "skey";
743
744 return (authok != 0);
745}
746#endif
747
ad200abb 748#ifdef USE_PAM
749int
750mm_answer_pam_start(int socket, Buffer *m)
751{
752 char *user;
753
7fceb20d 754 if (!options.use_pam)
755 fatal("UsePAM not set, but ended up in %s anyway", __func__);
756
ad200abb 757 user = buffer_get_string(m, NULL);
758
759 start_pam(user);
760
761 xfree(user);
762
763 return (0);
764}
05114c74 765
766static void *sshpam_ctxt, *sshpam_authok;
767extern KbdintDevice sshpam_device;
768
769int
770mm_answer_pam_init_ctx(int socket, Buffer *m)
771{
772
773 debug3("%s", __func__);
774 authctxt->user = buffer_get_string(m, NULL);
775 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
776 sshpam_authok = NULL;
777 buffer_clear(m);
778 if (sshpam_ctxt != NULL) {
779 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
780 buffer_put_int(m, 1);
781 } else {
782 buffer_put_int(m, 0);
783 }
784 mm_request_send(socket, MONITOR_ANS_PAM_INIT_CTX, m);
785 return (0);
786}
787
788int
789mm_answer_pam_query(int socket, Buffer *m)
790{
791 char *name, *info, **prompts;
792 u_int num, *echo_on;
793 int i, ret;
794
795 debug3("%s", __func__);
796 sshpam_authok = NULL;
797 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
798 if (ret == 0 && num == 0)
799 sshpam_authok = sshpam_ctxt;
800 if (num > 1 || name == NULL || info == NULL)
801 ret = -1;
802 buffer_clear(m);
803 buffer_put_int(m, ret);
804 buffer_put_cstring(m, name);
805 xfree(name);
806 buffer_put_cstring(m, info);
807 xfree(info);
808 buffer_put_int(m, num);
809 for (i = 0; i < num; ++i) {
810 buffer_put_cstring(m, prompts[i]);
811 xfree(prompts[i]);
812 buffer_put_int(m, echo_on[i]);
813 }
814 if (prompts != NULL)
815 xfree(prompts);
816 if (echo_on != NULL)
817 xfree(echo_on);
818 mm_request_send(socket, MONITOR_ANS_PAM_QUERY, m);
819 return (0);
820}
821
822int
823mm_answer_pam_respond(int socket, Buffer *m)
824{
825 char **resp;
826 u_int num;
827 int i, ret;
828
829 debug3("%s", __func__);
830 sshpam_authok = NULL;
831 num = buffer_get_int(m);
832 if (num > 0) {
833 resp = xmalloc(num * sizeof(char *));
834 for (i = 0; i < num; ++i)
835 resp[i] = buffer_get_string(m, NULL);
836 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
837 for (i = 0; i < num; ++i)
838 xfree(resp[i]);
839 xfree(resp);
840 } else {
841 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
842 }
843 buffer_clear(m);
844 buffer_put_int(m, ret);
845 mm_request_send(socket, MONITOR_ANS_PAM_RESPOND, m);
846 auth_method = "keyboard-interactive/pam";
847 if (ret == 0)
848 sshpam_authok = sshpam_ctxt;
849 return (0);
850}
851
852int
853mm_answer_pam_free_ctx(int socket, Buffer *m)
854{
855
856 debug3("%s", __func__);
857 (sshpam_device.free_ctx)(sshpam_ctxt);
858 buffer_clear(m);
859 mm_request_send(socket, MONITOR_ANS_PAM_FREE_CTX, m);
860 return (sshpam_authok == sshpam_ctxt);
861}
ad200abb 862#endif
863
1853d1ef 864static void
865mm_append_debug(Buffer *m)
866{
867 if (auth_debug_init && buffer_len(&auth_debug)) {
1588c277 868 debug3("%s: Appending debug messages for child", __func__);
1853d1ef 869 buffer_append(m, buffer_ptr(&auth_debug),
870 buffer_len(&auth_debug));
871 buffer_clear(&auth_debug);
872 }
873}
874
875int
876mm_answer_keyallowed(int socket, Buffer *m)
877{
878 Key *key;
661e45a0 879 char *cuser, *chost;
880 u_char *blob;
1853d1ef 881 u_int bloblen;
882 enum mm_keytype type = 0;
883 int allowed = 0;
884
1588c277 885 debug3("%s entering", __func__);
1853d1ef 886
887 type = buffer_get_int(m);
888 cuser = buffer_get_string(m, NULL);
889 chost = buffer_get_string(m, NULL);
890 blob = buffer_get_string(m, &bloblen);
891
892 key = key_from_blob(blob, bloblen);
893
894 if ((compat20 && type == MM_RSAHOSTKEY) ||
895 (!compat20 && type != MM_RSAHOSTKEY))
1588c277 896 fatal("%s: key type and protocol mismatch", __func__);
1853d1ef 897
1588c277 898 debug3("%s: key_from_blob: %p", __func__, key);
1853d1ef 899
900 if (key != NULL && authctxt->pw != NULL) {
901 switch(type) {
902 case MM_USERKEY:
aa586f8e 903 allowed = options.pubkey_authentication &&
904 user_key_allowed(authctxt->pw, key);
1853d1ef 905 break;
906 case MM_HOSTKEY:
aa586f8e 907 allowed = options.hostbased_authentication &&
908 hostbased_key_allowed(authctxt->pw,
1853d1ef 909 cuser, chost, key);
910 break;
911 case MM_RSAHOSTKEY:
912 key->type = KEY_RSA1; /* XXX */
aa586f8e 913 allowed = options.rhosts_rsa_authentication &&
914 auth_rhosts_rsa_key_allowed(authctxt->pw,
1853d1ef 915 cuser, chost, key);
916 break;
917 default:
1588c277 918 fatal("%s: unknown key type %d", __func__, type);
1853d1ef 919 break;
920 }
1853d1ef 921 }
3e2f2431 922 if (key != NULL)
923 key_free(key);
1853d1ef 924
925 /* clear temporarily storage (used by verify) */
926 monitor_reset_key_state();
927
928 if (allowed) {
929 /* Save temporarily for comparison in verify */
930 key_blob = blob;
931 key_bloblen = bloblen;
932 key_blobtype = type;
933 hostbased_cuser = cuser;
934 hostbased_chost = chost;
935 }
936
937 debug3("%s: key %p is %s",
1588c277 938 __func__, key, allowed ? "allowed" : "disallowed");
1853d1ef 939
940 buffer_clear(m);
941 buffer_put_int(m, allowed);
92e15201 942 buffer_put_int(m, forced_command != NULL);
1853d1ef 943
944 mm_append_debug(m);
945
946 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
947
948 if (type == MM_RSAHOSTKEY)
949 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
950
951 return (0);
952}
953
954static int
955monitor_valid_userblob(u_char *data, u_int datalen)
956{
957 Buffer b;
661e45a0 958 char *p;
1853d1ef 959 u_int len;
960 int fail = 0;
1853d1ef 961
962 buffer_init(&b);
963 buffer_append(&b, data, datalen);
964
965 if (datafellows & SSH_OLD_SESSIONID) {
521d606b 966 p = buffer_ptr(&b);
967 len = buffer_len(&b);
968 if ((session_id2 == NULL) ||
969 (len < session_id2_len) ||
970 (memcmp(p, session_id2, session_id2_len) != 0))
971 fail++;
1853d1ef 972 buffer_consume(&b, session_id2_len);
973 } else {
521d606b 974 p = buffer_get_string(&b, &len);
975 if ((session_id2 == NULL) ||
976 (len != session_id2_len) ||
977 (memcmp(p, session_id2, session_id2_len) != 0))
1853d1ef 978 fail++;
521d606b 979 xfree(p);
1853d1ef 980 }
981 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
982 fail++;
983 p = buffer_get_string(&b, NULL);
984 if (strcmp(authctxt->user, p) != 0) {
bbe88b6d 985 logit("wrong user name passed to monitor: expected %s != %.100s",
1853d1ef 986 authctxt->user, p);
987 fail++;
988 }
989 xfree(p);
990 buffer_skip_string(&b);
991 if (datafellows & SSH_BUG_PKAUTH) {
992 if (!buffer_get_char(&b))
993 fail++;
994 } else {
995 p = buffer_get_string(&b, NULL);
996 if (strcmp("publickey", p) != 0)
997 fail++;
998 xfree(p);
999 if (!buffer_get_char(&b))
1000 fail++;
1001 buffer_skip_string(&b);
1002 }
1003 buffer_skip_string(&b);
1004 if (buffer_len(&b) != 0)
1005 fail++;
1006 buffer_free(&b);
1007 return (fail == 0);
1008}
1009
1010static int
661e45a0 1011monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1012 char *chost)
1853d1ef 1013{
1014 Buffer b;
661e45a0 1015 char *p;
1853d1ef 1016 u_int len;
1017 int fail = 0;
1853d1ef 1018
1019 buffer_init(&b);
1020 buffer_append(&b, data, datalen);
1021
521d606b 1022 p = buffer_get_string(&b, &len);
1023 if ((session_id2 == NULL) ||
1024 (len != session_id2_len) ||
1025 (memcmp(p, session_id2, session_id2_len) != 0))
1853d1ef 1026 fail++;
521d606b 1027 xfree(p);
1028
1853d1ef 1029 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1030 fail++;
1031 p = buffer_get_string(&b, NULL);
1032 if (strcmp(authctxt->user, p) != 0) {
bbe88b6d 1033 logit("wrong user name passed to monitor: expected %s != %.100s",
1853d1ef 1034 authctxt->user, p);
1035 fail++;
1036 }
1037 xfree(p);
1038 buffer_skip_string(&b); /* service */
1039 p = buffer_get_string(&b, NULL);
1040 if (strcmp(p, "hostbased") != 0)
1041 fail++;
1042 xfree(p);
1043 buffer_skip_string(&b); /* pkalg */
1044 buffer_skip_string(&b); /* pkblob */
1045
1046 /* verify client host, strip trailing dot if necessary */
1047 p = buffer_get_string(&b, NULL);
1048 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1049 p[len - 1] = '\0';
1050 if (strcmp(p, chost) != 0)
1051 fail++;
1052 xfree(p);
1053
1054 /* verify client user */
1055 p = buffer_get_string(&b, NULL);
1056 if (strcmp(p, cuser) != 0)
1057 fail++;
1058 xfree(p);
1059
1060 if (buffer_len(&b) != 0)
1061 fail++;
1062 buffer_free(&b);
1063 return (fail == 0);
1064}
1065
1066int
1067mm_answer_keyverify(int socket, Buffer *m)
1068{
1069 Key *key;
1070 u_char *signature, *data, *blob;
1071 u_int signaturelen, datalen, bloblen;
1072 int verified = 0;
1073 int valid_data = 0;
1074
1075 blob = buffer_get_string(m, &bloblen);
1076 signature = buffer_get_string(m, &signaturelen);
1077 data = buffer_get_string(m, &datalen);
1078
1079 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
300e01c4 1080 !monitor_allowed_key(blob, bloblen))
1588c277 1081 fatal("%s: bad key, not previously allowed", __func__);
1853d1ef 1082
1083 key = key_from_blob(blob, bloblen);
1084 if (key == NULL)
1588c277 1085 fatal("%s: bad public key blob", __func__);
1853d1ef 1086
1087 switch (key_blobtype) {
1088 case MM_USERKEY:
1089 valid_data = monitor_valid_userblob(data, datalen);
1090 break;
1091 case MM_HOSTKEY:
1092 valid_data = monitor_valid_hostbasedblob(data, datalen,
1093 hostbased_cuser, hostbased_chost);
1094 break;
1095 default:
1096 valid_data = 0;
1097 break;
1098 }
1099 if (!valid_data)
1588c277 1100 fatal("%s: bad signature data blob", __func__);
1853d1ef 1101
1102 verified = key_verify(key, signature, signaturelen, data, datalen);
1103 debug3("%s: key %p signature %s",
1588c277 1104 __func__, key, verified ? "verified" : "unverified");
1853d1ef 1105
1106 key_free(key);
1107 xfree(blob);
1108 xfree(signature);
1109 xfree(data);
1110
d17ef027 1111 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1112
1853d1ef 1113 monitor_reset_key_state();
1114
1115 buffer_clear(m);
1116 buffer_put_int(m, verified);
1117 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
1118
1853d1ef 1119 return (verified);
1120}
1121
1122static void
1123mm_record_login(Session *s, struct passwd *pw)
1124{
1125 socklen_t fromlen;
1126 struct sockaddr_storage from;
1127
1128 /*
1129 * Get IP address of client. If the connection is not a socket, let
1130 * the address be 0.0.0.0.
1131 */
1132 memset(&from, 0, sizeof(from));
ba1566dd 1133 fromlen = sizeof(from);
1853d1ef 1134 if (packet_connection_is_on_socket()) {
1853d1ef 1135 if (getpeername(packet_get_connection_in(),
1136 (struct sockaddr *) & from, &fromlen) < 0) {
1137 debug("getpeername: %.100s", strerror(errno));
1138 fatal_cleanup();
1139 }
1140 }
1141 /* Record that there was a login on that tty from the remote host. */
1142 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1143 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
ba1566dd 1144 (struct sockaddr *)&from, fromlen);
1853d1ef 1145}
1146
1147static void
1148mm_session_close(Session *s)
1149{
1588c277 1150 debug3("%s: session %d pid %d", __func__, s->self, s->pid);
1853d1ef 1151 if (s->ttyfd != -1) {
1588c277 1152 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1853d1ef 1153 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1154 session_pty_cleanup2(s);
1155 }
1156 s->used = 0;
1157}
1158
1159int
1160mm_answer_pty(int socket, Buffer *m)
1161{
b5a28cbc 1162 extern struct monitor *pmonitor;
1853d1ef 1163 Session *s;
1164 int res, fd0;
1165
1588c277 1166 debug3("%s entering", __func__);
1853d1ef 1167
1168 buffer_clear(m);
1169 s = session_new();
1170 if (s == NULL)
1171 goto error;
1172 s->authctxt = authctxt;
1173 s->pw = authctxt->pw;
b5a28cbc 1174 s->pid = pmonitor->m_pid;
1853d1ef 1175 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1176 if (res == 0)
1177 goto error;
1178 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1179 pty_setowner(authctxt->pw, s->tty);
1180
1181 buffer_put_int(m, 1);
1182 buffer_put_cstring(m, s->tty);
1183 mm_request_send(socket, MONITOR_ANS_PTY, m);
1184
1185 mm_send_fd(socket, s->ptyfd);
1186 mm_send_fd(socket, s->ttyfd);
1187
1188 /* We need to trick ttyslot */
1189 if (dup2(s->ttyfd, 0) == -1)
1588c277 1190 fatal("%s: dup2", __func__);
1853d1ef 1191
1192 mm_record_login(s, authctxt->pw);
1193
1194 /* Now we can close the file descriptor again */
1195 close(0);
1196
1197 /* make sure nothing uses fd 0 */
1198 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1588c277 1199 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1853d1ef 1200 if (fd0 != 0)
1588c277 1201 error("%s: fd0 %d != 0", __func__, fd0);
1853d1ef 1202
1203 /* slave is not needed */
1204 close(s->ttyfd);
1205 s->ttyfd = s->ptyfd;
1206 /* no need to dup() because nobody closes ptyfd */
1207 s->ptymaster = s->ptyfd;
1208
1588c277 1209 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1853d1ef 1210
1211 return (0);
1212
1213 error:
1214 if (s != NULL)
1215 mm_session_close(s);
1216 buffer_put_int(m, 0);
1217 mm_request_send(socket, MONITOR_ANS_PTY, m);
1218 return (0);
1219}
1220
1221int
1222mm_answer_pty_cleanup(int socket, Buffer *m)
1223{
1224 Session *s;
1225 char *tty;
1226
1588c277 1227 debug3("%s entering", __func__);
1853d1ef 1228
1229 tty = buffer_get_string(m, NULL);
1230 if ((s = session_by_tty(tty)) != NULL)
1231 mm_session_close(s);
1232 buffer_clear(m);
1233 xfree(tty);
1234 return (0);
1235}
1236
1237int
1238mm_answer_sesskey(int socket, Buffer *m)
1239{
1240 BIGNUM *p;
1241 int rsafail;
1242
1243 /* Turn off permissions */
1244 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1245
1246 if ((p = BN_new()) == NULL)
1588c277 1247 fatal("%s: BN_new", __func__);
1853d1ef 1248
1249 buffer_get_bignum2(m, p);
1250
1251 rsafail = ssh1_session_key(p);
1252
1253 buffer_clear(m);
1254 buffer_put_int(m, rsafail);
1255 buffer_put_bignum2(m, p);
1256
1257 BN_clear_free(p);
1258
1259 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1260
1261 /* Turn on permissions for sessid passing */
1262 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1263
1264 return (0);
1265}
1266
1267int
1268mm_answer_sessid(int socket, Buffer *m)
1269{
1270 int i;
1271
1588c277 1272 debug3("%s entering", __func__);
1853d1ef 1273
1274 if (buffer_len(m) != 16)
1588c277 1275 fatal("%s: bad ssh1 session id", __func__);
1853d1ef 1276 for (i = 0; i < 16; i++)
1277 session_id[i] = buffer_get_char(m);
1278
1279 /* Turn on permissions for getpwnam */
1280 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1281
1282 return (0);
1283}
1284
1285int
1286mm_answer_rsa_keyallowed(int socket, Buffer *m)
1287{
1288 BIGNUM *client_n;
1289 Key *key = NULL;
1290 u_char *blob = NULL;
1291 u_int blen = 0;
1292 int allowed = 0;
1293
1588c277 1294 debug3("%s entering", __func__);
1853d1ef 1295
aa586f8e 1296 if (options.rsa_authentication && authctxt->valid) {
1853d1ef 1297 if ((client_n = BN_new()) == NULL)
1588c277 1298 fatal("%s: BN_new", __func__);
1853d1ef 1299 buffer_get_bignum2(m, client_n);
1300 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1301 BN_clear_free(client_n);
1302 }
1303 buffer_clear(m);
1304 buffer_put_int(m, allowed);
92e15201 1305 buffer_put_int(m, forced_command != NULL);
1853d1ef 1306
1307 /* clear temporarily storage (used by generate challenge) */
1308 monitor_reset_key_state();
1309
1310 if (allowed && key != NULL) {
1311 key->type = KEY_RSA; /* cheat for key_to_blob */
1312 if (key_to_blob(key, &blob, &blen) == 0)
1588c277 1313 fatal("%s: key_to_blob failed", __func__);
1853d1ef 1314 buffer_put_string(m, blob, blen);
1315
1316 /* Save temporarily for comparison in verify */
1317 key_blob = blob;
1318 key_bloblen = blen;
1319 key_blobtype = MM_RSAUSERKEY;
1853d1ef 1320 }
3e2f2431 1321 if (key != NULL)
1322 key_free(key);
1853d1ef 1323
1324 mm_append_debug(m);
1325
1326 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1327
1328 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1329 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1330 return (0);
1331}
1332
1333int
1334mm_answer_rsa_challenge(int socket, Buffer *m)
1335{
1336 Key *key = NULL;
1337 u_char *blob;
1338 u_int blen;
1339
1588c277 1340 debug3("%s entering", __func__);
1853d1ef 1341
1342 if (!authctxt->valid)
1588c277 1343 fatal("%s: authctxt not valid", __func__);
1853d1ef 1344 blob = buffer_get_string(m, &blen);
1345 if (!monitor_allowed_key(blob, blen))
1588c277 1346 fatal("%s: bad key, not previously allowed", __func__);
1853d1ef 1347 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1588c277 1348 fatal("%s: key type mismatch", __func__);
1853d1ef 1349 if ((key = key_from_blob(blob, blen)) == NULL)
1588c277 1350 fatal("%s: received bad key", __func__);
1853d1ef 1351
1352 if (ssh1_challenge)
1353 BN_clear_free(ssh1_challenge);
1354 ssh1_challenge = auth_rsa_generate_challenge(key);
1355
1356 buffer_clear(m);
1357 buffer_put_bignum2(m, ssh1_challenge);
1358
1588c277 1359 debug3("%s sending reply", __func__);
1853d1ef 1360 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1361
1362 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
3e2f2431 1363
1364 xfree(blob);
1365 key_free(key);
1853d1ef 1366 return (0);
1367}
1368
1369int
1370mm_answer_rsa_response(int socket, Buffer *m)
1371{
1372 Key *key = NULL;
1373 u_char *blob, *response;
1374 u_int blen, len;
1375 int success;
1376
1588c277 1377 debug3("%s entering", __func__);
1853d1ef 1378
1379 if (!authctxt->valid)
1588c277 1380 fatal("%s: authctxt not valid", __func__);
1853d1ef 1381 if (ssh1_challenge == NULL)
1588c277 1382 fatal("%s: no ssh1_challenge", __func__);
1853d1ef 1383
1384 blob = buffer_get_string(m, &blen);
1385 if (!monitor_allowed_key(blob, blen))
1588c277 1386 fatal("%s: bad key, not previously allowed", __func__);
1853d1ef 1387 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1588c277 1388 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1853d1ef 1389 if ((key = key_from_blob(blob, blen)) == NULL)
1588c277 1390 fatal("%s: received bad key", __func__);
1853d1ef 1391 response = buffer_get_string(m, &len);
1392 if (len != 16)
1588c277 1393 fatal("%s: received bad response to challenge", __func__);
1853d1ef 1394 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1395
3e2f2431 1396 xfree(blob);
1853d1ef 1397 key_free(key);
1398 xfree(response);
1399
1400 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1401
1402 /* reset state */
1403 BN_clear_free(ssh1_challenge);
1404 ssh1_challenge = NULL;
1405 monitor_reset_key_state();
1406
1407 buffer_clear(m);
1408 buffer_put_int(m, success);
1409 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1410
1411 return (success);
1412}
1413
2db9d8aa 1414#ifdef KRB4
1415int
1416mm_answer_krb4(int socket, Buffer *m)
1417{
1418 KTEXT_ST auth, reply;
1419 char *client, *p;
1420 int success;
1421 u_int alen;
1422
1423 reply.length = auth.length = 0;
1424
1425 p = buffer_get_string(m, &alen);
1426 if (alen >= MAX_KTXT_LEN)
1427 fatal("%s: auth too large", __func__);
1428 memcpy(auth.dat, p, alen);
1429 auth.length = alen;
1430 memset(p, 0, alen);
1431 xfree(p);
1432
1433 success = options.kerberos_authentication &&
1434 authctxt->valid &&
1435 auth_krb4(authctxt, &auth, &client, &reply);
1436
1437 memset(auth.dat, 0, alen);
1438 buffer_clear(m);
1439 buffer_put_int(m, success);
1440
1441 if (success) {
1442 buffer_put_cstring(m, client);
1443 buffer_put_string(m, reply.dat, reply.length);
1444 if (client)
1445 xfree(client);
1446 if (reply.length)
1447 memset(reply.dat, 0, reply.length);
1448 }
1449
1450 debug3("%s: sending result %d", __func__, success);
1451 mm_request_send(socket, MONITOR_ANS_KRB4, m);
1452
1453 auth_method = "kerberos";
1454
1455 /* Causes monitor loop to terminate if authenticated */
1456 return (success);
1457}
1458#endif
696f6bef 1459
1460#ifdef KRB5
1461int
1462mm_answer_krb5(int socket, Buffer *m)
1463{
1464 krb5_data tkt, reply;
1465 char *client_user;
1466 u_int len;
1467 int success;
1468
1469 /* use temporary var to avoid size issues on 64bit arch */
1470 tkt.data = buffer_get_string(m, &len);
1471 tkt.length = len;
1472
4741e3a6 1473 success = options.kerberos_authentication &&
1474 authctxt->valid &&
0082ad34 1475 auth_krb5(authctxt, &tkt, &client_user, &reply);
696f6bef 1476
1477 if (tkt.length)
1478 xfree(tkt.data);
1479
1480 buffer_clear(m);
1481 buffer_put_int(m, success);
1482
1483 if (success) {
1484 buffer_put_cstring(m, client_user);
1485 buffer_put_string(m, reply.data, reply.length);
1486 if (client_user)
1487 xfree(client_user);
1488 if (reply.length)
1489 xfree(reply.data);
1490 }
1491 mm_request_send(socket, MONITOR_ANS_KRB5, m);
1492
802e01b8 1493 auth_method = "kerberos";
1494
696f6bef 1495 return success;
1496}
1497#endif
1498
1853d1ef 1499int
1500mm_answer_term(int socket, Buffer *req)
1501{
b5a28cbc 1502 extern struct monitor *pmonitor;
1853d1ef 1503 int res, status;
1504
1588c277 1505 debug3("%s: tearing down sessions", __func__);
1853d1ef 1506
1507 /* The child is terminating */
1508 session_destroy_all(&mm_session_close);
1509
b5a28cbc 1510 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
8c38e88b 1511 if (errno != EINTR)
1512 exit(1);
1853d1ef 1513
1514 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1515
1516 /* Terminate process */
1517 exit (res);
1518}
1519
1520void
b5a28cbc 1521monitor_apply_keystate(struct monitor *pmonitor)
1853d1ef 1522{
1523 if (compat20) {
1524 set_newkeys(MODE_IN);
1525 set_newkeys(MODE_OUT);
1526 } else {
1853d1ef 1527 packet_set_protocol_flags(child_state.ssh1protoflags);
9459414c 1528 packet_set_encryption_key(child_state.ssh1key,
1529 child_state.ssh1keylen, child_state.ssh1cipher);
1530 xfree(child_state.ssh1key);
1853d1ef 1531 }
1532
9459414c 1533 /* for rc4 and other stateful ciphers */
1853d1ef 1534 packet_set_keycontext(MODE_OUT, child_state.keyout);
1535 xfree(child_state.keyout);
1536 packet_set_keycontext(MODE_IN, child_state.keyin);
1537 xfree(child_state.keyin);
1538
1539 if (!compat20) {
1540 packet_set_iv(MODE_OUT, child_state.ivout);
1541 xfree(child_state.ivout);
1542 packet_set_iv(MODE_IN, child_state.ivin);
1543 xfree(child_state.ivin);
1544 }
1545
1546 memcpy(&incoming_stream, &child_state.incoming,
1547 sizeof(incoming_stream));
1548 memcpy(&outgoing_stream, &child_state.outgoing,
1549 sizeof(outgoing_stream));
1550
1551 /* Update with new address */
0496cf34 1552 if (options.compression)
1553 mm_init_compression(pmonitor->m_zlib);
1853d1ef 1554
1555 /* Network I/O buffers */
1556 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1557 buffer_clear(&input);
1558 buffer_append(&input, child_state.input, child_state.ilen);
1559 memset(child_state.input, 0, child_state.ilen);
1560 xfree(child_state.input);
1561
1562 buffer_clear(&output);
1563 buffer_append(&output, child_state.output, child_state.olen);
1564 memset(child_state.output, 0, child_state.olen);
1565 xfree(child_state.output);
1566}
1567
1568static Kex *
1569mm_get_kex(Buffer *m)
1570{
1571 Kex *kex;
1572 void *blob;
1573 u_int bloblen;
1574
1575 kex = xmalloc(sizeof(*kex));
1576 memset(kex, 0, sizeof(*kex));
1577 kex->session_id = buffer_get_string(m, &kex->session_id_len);
521d606b 1578 if ((session_id2 == NULL) ||
1579 (kex->session_id_len != session_id2_len) ||
1580 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1581 fatal("mm_get_get: internal error: bad session id");
1853d1ef 1582 kex->we_need = buffer_get_int(m);
cdb64c4d 1583 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1584 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1853d1ef 1585 kex->server = 1;
1586 kex->hostkey_type = buffer_get_int(m);
1587 kex->kex_type = buffer_get_int(m);
1588 blob = buffer_get_string(m, &bloblen);
1589 buffer_init(&kex->my);
1590 buffer_append(&kex->my, blob, bloblen);
1591 xfree(blob);
1592 blob = buffer_get_string(m, &bloblen);
1593 buffer_init(&kex->peer);
1594 buffer_append(&kex->peer, blob, bloblen);
1595 xfree(blob);
1596 kex->done = 1;
1597 kex->flags = buffer_get_int(m);
1598 kex->client_version_string = buffer_get_string(m, NULL);
1599 kex->server_version_string = buffer_get_string(m, NULL);
1600 kex->load_host_key=&get_hostkey_by_type;
1601 kex->host_key_index=&get_hostkey_index;
1602
1603 return (kex);
1604}
1605
1606/* This function requries careful sanity checking */
1607
1608void
b5a28cbc 1609mm_get_keystate(struct monitor *pmonitor)
1853d1ef 1610{
1611 Buffer m;
1612 u_char *blob, *p;
1613 u_int bloblen, plen;
ffd7b36b 1614 u_int32_t seqnr, packets;
1615 u_int64_t blocks;
1853d1ef 1616
1588c277 1617 debug3("%s: Waiting for new keys", __func__);
1853d1ef 1618
1619 buffer_init(&m);
b5a28cbc 1620 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1853d1ef 1621 if (!compat20) {
1622 child_state.ssh1protoflags = buffer_get_int(&m);
1623 child_state.ssh1cipher = buffer_get_int(&m);
9459414c 1624 child_state.ssh1key = buffer_get_string(&m,
1625 &child_state.ssh1keylen);
1853d1ef 1626 child_state.ivout = buffer_get_string(&m,
1627 &child_state.ivoutlen);
1628 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1629 goto skip;
1630 } else {
1631 /* Get the Kex for rekeying */
b5a28cbc 1632 *pmonitor->m_pkex = mm_get_kex(&m);
1853d1ef 1633 }
1634
1635 blob = buffer_get_string(&m, &bloblen);
1636 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1637 xfree(blob);
1638
1588c277 1639 debug3("%s: Waiting for second key", __func__);
1853d1ef 1640 blob = buffer_get_string(&m, &bloblen);
1641 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1642 xfree(blob);
1643
1644 /* Now get sequence numbers for the packets */
ffd7b36b 1645 seqnr = buffer_get_int(&m);
1646 blocks = buffer_get_int64(&m);
1647 packets = buffer_get_int(&m);
1648 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1649 seqnr = buffer_get_int(&m);
1650 blocks = buffer_get_int64(&m);
1651 packets = buffer_get_int(&m);
1652 packet_set_state(MODE_IN, seqnr, blocks, packets);
1853d1ef 1653
1654 skip:
1655 /* Get the key context */
1656 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1657 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1658
1588c277 1659 debug3("%s: Getting compression state", __func__);
1853d1ef 1660 /* Get compression state */
1661 p = buffer_get_string(&m, &plen);
1662 if (plen != sizeof(child_state.outgoing))
1588c277 1663 fatal("%s: bad request size", __func__);
1853d1ef 1664 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1665 xfree(p);
1666
1667 p = buffer_get_string(&m, &plen);
1668 if (plen != sizeof(child_state.incoming))
1588c277 1669 fatal("%s: bad request size", __func__);
1853d1ef 1670 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1671 xfree(p);
1672
1673 /* Network I/O buffers */
1588c277 1674 debug3("%s: Getting Network I/O buffers", __func__);
1853d1ef 1675 child_state.input = buffer_get_string(&m, &child_state.ilen);
1676 child_state.output = buffer_get_string(&m, &child_state.olen);
1677
1678 buffer_free(&m);
1679}
1680
1681
1682/* Allocation functions for zlib */
1683void *
1684mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1685{
39e71188 1686 size_t len = (size_t) size * ncount;
1853d1ef 1687 void *address;
1688
b85698ab 1689 if (len == 0 || ncount > SIZE_T_MAX / size)
2f095a0e 1690 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1691
1692 address = mm_malloc(mm, len);
1853d1ef 1693
1694 return (address);
1695}
1696
1697void
1698mm_zfree(struct mm_master *mm, void *address)
1699{
1700 mm_free(mm, address);
1701}
1702
1703void
1704mm_init_compression(struct mm_master *mm)
1705{
1706 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1707 outgoing_stream.zfree = (free_func)mm_zfree;
1708 outgoing_stream.opaque = mm;
1709
1710 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1711 incoming_stream.zfree = (free_func)mm_zfree;
1712 incoming_stream.opaque = mm;
1713}
1714
1715/* XXX */
1716
1717#define FD_CLOSEONEXEC(x) do { \
1718 if (fcntl(x, F_SETFD, 1) == -1) \
1719 fatal("fcntl(%d, F_SETFD)", x); \
1720} while (0)
1721
1722static void
1723monitor_socketpair(int *pair)
1724{
f1af2dbf 1725#ifdef HAVE_SOCKETPAIR
1853d1ef 1726 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1588c277 1727 fatal("%s: socketpair", __func__);
f1af2dbf 1728#else
1729 fatal("%s: UsePrivilegeSeparation=yes not supported",
1588c277 1730 __func__);
f1af2dbf 1731#endif
1853d1ef 1732 FD_CLOSEONEXEC(pair[0]);
1733 FD_CLOSEONEXEC(pair[1]);
1734}
1735
1736#define MM_MEMSIZE 65536
1737
1738struct monitor *
1739monitor_init(void)
1740{
1741 struct monitor *mon;
1742 int pair[2];
1743
1744 mon = xmalloc(sizeof(*mon));
1745
1746 monitor_socketpair(pair);
1747
1748 mon->m_recvfd = pair[0];
1749 mon->m_sendfd = pair[1];
1750
1751 /* Used to share zlib space across processes */
0496cf34 1752 if (options.compression) {
1753 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1754 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1853d1ef 1755
0496cf34 1756 /* Compression needs to share state across borders */
1757 mm_init_compression(mon->m_zlib);
1758 }
1853d1ef 1759
1760 return mon;
1761}
1762
1763void
1764monitor_reinit(struct monitor *mon)
1765{
1766 int pair[2];
1767
1768 monitor_socketpair(pair);
1769
1770 mon->m_recvfd = pair[0];
1771 mon->m_sendfd = pair[1];
1772}
This page took 0.354607 seconds and 5 git commands to generate.