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