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