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