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