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