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