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