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