]> andersk Git - gssapi-openssh.git/blame - openssh/monitor.c
Initial revision
[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"
08822d99 28RCSID("$OpenBSD: monitor.c,v 1.64 2005/10/13 22:24:31 stevesk 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"
12a403af 40#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
41#undef TARGET_OS_MAC
826f3a39 42#include "zlib.h"
12a403af 43#define TARGET_OS_MAC 1
44#else
45#include "zlib.h"
46#endif
826f3a39 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"
826f3a39 66
b59afbfe 67#ifdef GSSAPI
68#include "ssh-gss.h"
69static Gssctxt *gsscontext = NULL;
70#endif
71
826f3a39 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;
7e82606e 82extern Buffer loginmsg;
826f3a39 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;
44a053a3 97 u_char *ssh1key;
98 u_int ssh1keylen;
826f3a39 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
7cac2b65 107/* Functions on the monitor that answer unprivileged requests */
826f3a39 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 *);
7cac2b65 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 *);
826f3a39 137#endif
138
7cac2b65 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 *);
540d72c3 143int mm_answer_gss_checkmic(int, Buffer *);
b59afbfe 144int mm_answer_gss_sign(int, Buffer *);
23987cb8 145int mm_answer_gss_error(int, Buffer *);
e23e524c 146int mm_answer_gss_indicate_mechs(int, Buffer *);
147int mm_answer_gss_localname(int, Buffer *);
b59afbfe 148#endif
149
dfddba3d 150#ifdef SSH_AUDIT_EVENTS
151int mm_answer_audit_event(int, Buffer *);
152int mm_answer_audit_command(int, Buffer *);
153#endif
154
826f3a39 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;
d03f4262 162static char *hostbased_cuser = NULL;
163static char *hostbased_chost = NULL;
826f3a39 164static char *auth_method = "unknown";
7cac2b65 165static u_int session_id2_len = 0;
44a053a3 166static u_char *session_id2 = NULL;
7cac2b65 167static pid_t monitor_child_pid;
826f3a39 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},
b59afbfe 186 {MONITOR_REQ_PWNAM, MON_AUTH, mm_answer_pwnamallow},
826f3a39 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
e2447520 191 {MONITOR_REQ_PAM_START, MON_ISAUTH, mm_answer_pam_start},
7cac2b65 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},
826f3a39 197#endif
dfddba3d 198#ifdef SSH_AUDIT_EVENTS
199 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
200#endif
826f3a39 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},
b59afbfe 208#endif
7cac2b65 209 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
210 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
b59afbfe 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},
7cac2b65 214 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
fe4ad273 215 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
b59afbfe 216 {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
e23e524c 217 {MONITOR_REQ_GSSERR, MON_ISAUTH | MON_ONCE, mm_answer_gss_error},
9eeaa28e 218 {MONITOR_REQ_GSSMECHS, MON_ISAUTH, mm_answer_gss_indicate_mechs},
e23e524c 219 {MONITOR_REQ_GSSLOCALNAME, MON_ISAUTH, mm_answer_gss_localname},
826f3a39 220#endif
826f3a39 221 {0, 0, NULL}
222};
223
224struct mon_table mon_dispatch_postauth20[] = {
b59afbfe 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},
23987cb8 229 {MONITOR_REQ_GSSERR, 0, mm_answer_gss_error},
e23e524c 230 {MONITOR_REQ_GSSMECHS, 0, mm_answer_gss_indicate_mechs},
b59afbfe 231#endif
826f3a39 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},
dfddba3d 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
826f3a39 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},
826f3a39 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
b59afbfe 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},
9eeaa28e 266 {MONITOR_REQ_GSSMECHS, MON_ISAUTH, mm_answer_gss_indicate_mechs},
b59afbfe 267#endif
826f3a39 268#ifdef USE_PAM
e2447520 269 {MONITOR_REQ_PAM_START, MON_ISAUTH, mm_answer_pam_start},
7cac2b65 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},
dfddba3d 275#endif
276#ifdef SSH_AUDIT_EVENTS
277 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
826f3a39 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},
dfddba3d 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
826f3a39 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
540d72c3 324void
325monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
826f3a39 326{
327 struct mon_table *ent;
328 int authenticated = 0;
329
330 debug3("preauth child monitor started");
331
540d72c3 332 authctxt = _authctxt;
333 memset(authctxt, 0, sizeof(*authctxt));
334
8b32eddc 335 authctxt->loginmsg = &loginmsg;
336
826f3a39 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);
b59afbfe 343#ifdef GSSAPI
344 /* and for the GSSAPI key exchange */
345 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
23987cb8 346 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR, 1);
e23e524c 347 monitor_permit(mon_dispatch, MONITOR_REQ_GSSMECHS, 1);
b59afbfe 348#endif
826f3a39 349 } else {
350 mon_dispatch = mon_dispatch_proto15;
351
352 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
353 }
354
826f3a39 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",
44a053a3 361 __func__, ent->type);
826f3a39 362 if (authctxt->pw->pw_uid == 0 &&
363 !auth_root_allowed(auth_method))
364 authenticated = 0;
365#ifdef USE_PAM
7cac2b65 366 /* PAM needs to perform account checks after auth */
540d72c3 367 if (options.use_pam && authenticated) {
7cac2b65 368 Buffer m;
369
370 buffer_init(&m);
540d72c3 371 mm_request_receive_expect(pmonitor->m_sendfd,
7cac2b65 372 MONITOR_REQ_PAM_ACCOUNT, &m);
373 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
374 buffer_free(&m);
375 }
826f3a39 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)
44a053a3 388 fatal("%s: authenticated invalid user", __func__);
826f3a39 389
390 debug("%s: %s has been authenticated by privileged process",
44a053a3 391 __func__, authctxt->user);
826f3a39 392
393 mm_get_keystate(pmonitor);
826f3a39 394}
395
7cac2b65 396static void
397monitor_set_child_handler(pid_t pid)
398{
399 monitor_child_pid = pid;
400}
401
402static void
7e82606e 403monitor_child_handler(int sig)
7cac2b65 404{
7e82606e 405 kill(monitor_child_pid, sig);
7cac2b65 406}
407
826f3a39 408void
409monitor_child_postauth(struct monitor *pmonitor)
410{
7cac2b65 411 monitor_set_child_handler(pmonitor->m_pid);
412 signal(SIGHUP, &monitor_child_handler);
413 signal(SIGTERM, &monitor_child_handler);
414
826f3a39 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
e23e524c 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
826f3a39 430 } else {
431 mon_dispatch = mon_dispatch_postauth15;
432 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
433 }
9eeaa28e 434#ifdef GSSAPI
23987cb8 435 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR, 1);
9eeaa28e 436#endif
826f3a39 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{
44a053a3 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 }
826f3a39 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
44a053a3 468 debug3("%s: checking request %d", __func__, type);
826f3a39 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))
44a053a3 478 fatal("%s: unpermitted request %d", __func__,
826f3a39 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) {
44a053a3 485 debug2("%s: %d used once, disabling now", __func__,
826f3a39 486 type);
487 ent->flags &= ~MON_PERMIT;
488 }
489
490 if (pent != NULL)
491 *pent = ent;
492
493 return ret;
494 }
495
44a053a3 496 fatal("%s: unsupported request: %d", __func__, type);
826f3a39 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
7e82606e 531mm_answer_moduli(int sock, Buffer *m)
826f3a39 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",
44a053a3 541 __func__, min, want, max);
826f3a39 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",
44a053a3 545 __func__, min, want, max);
826f3a39 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 }
7e82606e 561 mm_request_send(sock, MONITOR_ANS_MODULI, m);
826f3a39 562 return (0);
563}
564
565int
7e82606e 566mm_answer_sign(int sock, Buffer *m)
826f3a39 567{
568 Key *key;
569 u_char *p;
570 u_char *signature;
571 u_int siglen, datlen;
572 int keyid;
573
44a053a3 574 debug3("%s", __func__);
826f3a39 575
576 keyid = buffer_get_int(m);
577 p = buffer_get_string(m, &datlen);
578
579 if (datlen != 20)
d03f4262 580 fatal("%s: data length incorrect: %u", __func__, datlen);
44a053a3 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 }
826f3a39 588
589 if ((key = get_hostkey_by_index(keyid)) == NULL)
44a053a3 590 fatal("%s: no hostkey from index %d", __func__, keyid);
826f3a39 591 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
44a053a3 592 fatal("%s: key_sign failed", __func__);
826f3a39 593
d03f4262 594 debug3("%s: signature %p(%u)", __func__, signature, siglen);
826f3a39 595
596 buffer_clear(m);
597 buffer_put_string(m, signature, siglen);
598
599 xfree(p);
600 xfree(signature);
601
7e82606e 602 mm_request_send(sock, MONITOR_ANS_SIGN, m);
826f3a39 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
7e82606e 613mm_answer_pwnamallow(int sock, Buffer *m)
826f3a39 614{
7e82606e 615 char *username;
826f3a39 616 struct passwd *pwent;
617 int allowed = 0;
618
44a053a3 619 debug3("%s", __func__);
826f3a39 620
7e82606e 621 username = buffer_get_string(m, NULL);
826f3a39 622
7e82606e 623 pwent = getpwnamallow(username);
826f3a39 624
b59afbfe 625 if (authctxt->user) xfree(authctxt->user);
7e82606e 626 authctxt->user = xstrdup(username);
627 setproctitle("%s [priv]", pwent ? username : "unknown");
628 xfree(username);
826f3a39 629
630 buffer_clear(m);
631
632 if (pwent == NULL) {
633 buffer_put_char(m, 0);
540d72c3 634 authctxt->pw = fakepw();
826f3a39 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:
44a053a3 654 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
7e82606e 655 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
826f3a39 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
7cac2b65 667 if (options.use_pam)
668 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
826f3a39 669#endif
dfddba3d 670#ifdef SSH_AUDIT_EVENTS
671 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
672#endif
826f3a39 673
674 return (0);
675}
676
7e82606e 677int mm_answer_auth2_read_banner(int sock, Buffer *m)
826f3a39 678{
679 char *banner;
680
681 buffer_clear(m);
682 banner = auth2_read_banner();
683 buffer_put_cstring(m, banner != NULL ? banner : "");
7e82606e 684 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
826f3a39 685
686 if (banner != NULL)
d03f4262 687 xfree(banner);
826f3a39 688
689 return (0);
690}
691
692int
7e82606e 693mm_answer_authserv(int sock, Buffer *m)
826f3a39 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",
44a053a3 700 __func__, authctxt->service, authctxt->style);
826f3a39 701
702 if (strlen(authctxt->style) == 0) {
703 xfree(authctxt->style);
704 authctxt->style = NULL;
705 }
706
707 return (0);
708}
709
710int
7e82606e 711mm_answer_authpassword(int sock, Buffer *m)
826f3a39 712{
713 static int call_count;
714 char *passwd;
d03f4262 715 int authenticated;
716 u_int plen;
826f3a39 717
718 passwd = buffer_get_string(m, &plen);
719 /* Only authenticate if the context is valid */
44a053a3 720 authenticated = options.password_authentication &&
7cac2b65 721 auth_password(authctxt, passwd);
826f3a39 722 memset(passwd, 0, strlen(passwd));
723 xfree(passwd);
724
725 buffer_clear(m);
726 buffer_put_int(m, authenticated);
727
44a053a3 728 debug3("%s: sending result %d", __func__, authenticated);
7e82606e 729 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
826f3a39 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
7e82606e 743mm_answer_bsdauthquery(int sock, Buffer *m)
826f3a39 744{
745 char *name, *infotxt;
746 u_int numprompts;
747 u_int *echo_on;
748 char **prompts;
bfe49944 749 u_int success;
826f3a39 750
bfe49944 751 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
752 &prompts, &echo_on) < 0 ? 0 : 1;
826f3a39 753
754 buffer_clear(m);
bfe49944 755 buffer_put_int(m, success);
756 if (success)
826f3a39 757 buffer_put_cstring(m, prompts[0]);
758
bfe49944 759 debug3("%s: sending challenge success: %u", __func__, success);
7e82606e 760 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
826f3a39 761
bfe49944 762 if (success) {
826f3a39 763 xfree(name);
764 xfree(infotxt);
765 xfree(prompts);
766 xfree(echo_on);
767 }
768
769 return (0);
770}
771
772int
7e82606e 773mm_answer_bsdauthrespond(int sock, Buffer *m)
826f3a39 774{
775 char *response;
776 int authok;
777
778 if (authctxt->as == 0)
44a053a3 779 fatal("%s: no bsd auth session", __func__);
826f3a39 780
781 response = buffer_get_string(m, NULL);
44a053a3 782 authok = options.challenge_response_authentication &&
783 auth_userresponse(authctxt->as, response, 0);
826f3a39 784 authctxt->as = NULL;
44a053a3 785 debug3("%s: <%s> = <%d>", __func__, response, authok);
826f3a39 786 xfree(response);
787
788 buffer_clear(m);
789 buffer_put_int(m, authok);
790
44a053a3 791 debug3("%s: sending authenticated: %d", __func__, authok);
7e82606e 792 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
826f3a39 793
794 auth_method = "bsdauth";
795
796 return (authok != 0);
797}
798#endif
799
800#ifdef SKEY
801int
7e82606e 802mm_answer_skeyquery(int sock, Buffer *m)
826f3a39 803{
804 struct skey skey;
805 char challenge[1024];
bfe49944 806 u_int success;
826f3a39 807
12a403af 808 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
809 sizeof(challenge)) < 0 ? 0 : 1;
826f3a39 810
811 buffer_clear(m);
bfe49944 812 buffer_put_int(m, success);
813 if (success)
826f3a39 814 buffer_put_cstring(m, challenge);
815
bfe49944 816 debug3("%s: sending challenge success: %u", __func__, success);
7e82606e 817 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
826f3a39 818
819 return (0);
820}
821
822int
7e82606e 823mm_answer_skeyrespond(int sock, Buffer *m)
826f3a39 824{
825 char *response;
826 int authok;
827
828 response = buffer_get_string(m, NULL);
829
44a053a3 830 authok = (options.challenge_response_authentication &&
831 authctxt->valid &&
826f3a39 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
44a053a3 840 debug3("%s: sending authenticated: %d", __func__, authok);
7e82606e 841 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
826f3a39 842
843 auth_method = "skey";
844
845 return (authok != 0);
846}
847#endif
848
849#ifdef USE_PAM
850int
7e82606e 851mm_answer_pam_start(int sock, Buffer *m)
826f3a39 852{
7cac2b65 853 if (!options.use_pam)
854 fatal("UsePAM not set, but ended up in %s anyway", __func__);
855
12a403af 856 start_pam(authctxt);
826f3a39 857
7cac2b65 858 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
859
826f3a39 860 return (0);
861}
7cac2b65 862
863int
7e82606e 864mm_answer_pam_account(int sock, Buffer *m)
7cac2b65 865{
866 u_int ret;
540d72c3 867
7cac2b65 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);
08822d99 874 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
7cac2b65 875
7e82606e 876 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
7cac2b65 877
878 return (ret);
879}
880
881static void *sshpam_ctxt, *sshpam_authok;
882extern KbdintDevice sshpam_device;
883
884int
7e82606e 885mm_answer_pam_init_ctx(int sock, Buffer *m)
7cac2b65 886{
887
888 debug3("%s", __func__);
889 authctxt->user = buffer_get_string(m, NULL);
890 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
891 sshpam_authok = NULL;
892 buffer_clear(m);
893 if (sshpam_ctxt != NULL) {
894 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
895 buffer_put_int(m, 1);
896 } else {
897 buffer_put_int(m, 0);
898 }
7e82606e 899 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
7cac2b65 900 return (0);
901}
902
903int
7e82606e 904mm_answer_pam_query(int sock, Buffer *m)
7cac2b65 905{
906 char *name, *info, **prompts;
2ce0bfe4 907 u_int i, num, *echo_on;
908 int ret;
7cac2b65 909
910 debug3("%s", __func__);
911 sshpam_authok = NULL;
912 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
913 if (ret == 0 && num == 0)
914 sshpam_authok = sshpam_ctxt;
915 if (num > 1 || name == NULL || info == NULL)
916 ret = -1;
917 buffer_clear(m);
918 buffer_put_int(m, ret);
919 buffer_put_cstring(m, name);
920 xfree(name);
921 buffer_put_cstring(m, info);
922 xfree(info);
923 buffer_put_int(m, num);
924 for (i = 0; i < num; ++i) {
925 buffer_put_cstring(m, prompts[i]);
926 xfree(prompts[i]);
927 buffer_put_int(m, echo_on[i]);
928 }
929 if (prompts != NULL)
930 xfree(prompts);
931 if (echo_on != NULL)
932 xfree(echo_on);
7e82606e 933 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
7cac2b65 934 return (0);
935}
936
937int
7e82606e 938mm_answer_pam_respond(int sock, Buffer *m)
7cac2b65 939{
940 char **resp;
2ce0bfe4 941 u_int i, num;
942 int ret;
7cac2b65 943
944 debug3("%s", __func__);
945 sshpam_authok = NULL;
946 num = buffer_get_int(m);
947 if (num > 0) {
948 resp = xmalloc(num * sizeof(char *));
949 for (i = 0; i < num; ++i)
950 resp[i] = buffer_get_string(m, NULL);
951 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
952 for (i = 0; i < num; ++i)
953 xfree(resp[i]);
954 xfree(resp);
955 } else {
956 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
957 }
958 buffer_clear(m);
959 buffer_put_int(m, ret);
7e82606e 960 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
7cac2b65 961 auth_method = "keyboard-interactive/pam";
962 if (ret == 0)
963 sshpam_authok = sshpam_ctxt;
964 return (0);
965}
966
967int
7e82606e 968mm_answer_pam_free_ctx(int sock, Buffer *m)
7cac2b65 969{
970
971 debug3("%s", __func__);
972 (sshpam_device.free_ctx)(sshpam_ctxt);
973 buffer_clear(m);
7e82606e 974 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
7cac2b65 975 return (sshpam_authok == sshpam_ctxt);
976}
826f3a39 977#endif
978
979static void
980mm_append_debug(Buffer *m)
981{
982 if (auth_debug_init && buffer_len(&auth_debug)) {
44a053a3 983 debug3("%s: Appending debug messages for child", __func__);
826f3a39 984 buffer_append(m, buffer_ptr(&auth_debug),
985 buffer_len(&auth_debug));
986 buffer_clear(&auth_debug);
987 }
988}
989
990int
7e82606e 991mm_answer_keyallowed(int sock, Buffer *m)
826f3a39 992{
993 Key *key;
d03f4262 994 char *cuser, *chost;
995 u_char *blob;
826f3a39 996 u_int bloblen;
997 enum mm_keytype type = 0;
998 int allowed = 0;
999
44a053a3 1000 debug3("%s entering", __func__);
826f3a39 1001
1002 type = buffer_get_int(m);
1003 cuser = buffer_get_string(m, NULL);
1004 chost = buffer_get_string(m, NULL);
1005 blob = buffer_get_string(m, &bloblen);
1006
1007 key = key_from_blob(blob, bloblen);
1008
1009 if ((compat20 && type == MM_RSAHOSTKEY) ||
1010 (!compat20 && type != MM_RSAHOSTKEY))
44a053a3 1011 fatal("%s: key type and protocol mismatch", __func__);
826f3a39 1012
44a053a3 1013 debug3("%s: key_from_blob: %p", __func__, key);
826f3a39 1014
540d72c3 1015 if (key != NULL && authctxt->valid) {
8b32eddc 1016 switch (type) {
826f3a39 1017 case MM_USERKEY:
44a053a3 1018 allowed = options.pubkey_authentication &&
1019 user_key_allowed(authctxt->pw, key);
826f3a39 1020 break;
1021 case MM_HOSTKEY:
44a053a3 1022 allowed = options.hostbased_authentication &&
1023 hostbased_key_allowed(authctxt->pw,
826f3a39 1024 cuser, chost, key);
1025 break;
1026 case MM_RSAHOSTKEY:
1027 key->type = KEY_RSA1; /* XXX */
44a053a3 1028 allowed = options.rhosts_rsa_authentication &&
1029 auth_rhosts_rsa_key_allowed(authctxt->pw,
826f3a39 1030 cuser, chost, key);
1031 break;
1032 default:
44a053a3 1033 fatal("%s: unknown key type %d", __func__, type);
826f3a39 1034 break;
1035 }
826f3a39 1036 }
bfe49944 1037 if (key != NULL)
1038 key_free(key);
826f3a39 1039
1040 /* clear temporarily storage (used by verify) */
1041 monitor_reset_key_state();
1042
1043 if (allowed) {
1044 /* Save temporarily for comparison in verify */
1045 key_blob = blob;
1046 key_bloblen = bloblen;
1047 key_blobtype = type;
1048 hostbased_cuser = cuser;
1049 hostbased_chost = chost;
1050 }
1051
1052 debug3("%s: key %p is %s",
44a053a3 1053 __func__, key, allowed ? "allowed" : "disallowed");
826f3a39 1054
1055 buffer_clear(m);
1056 buffer_put_int(m, allowed);
bfe49944 1057 buffer_put_int(m, forced_command != NULL);
826f3a39 1058
1059 mm_append_debug(m);
1060
7e82606e 1061 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
826f3a39 1062
1063 if (type == MM_RSAHOSTKEY)
1064 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1065
1066 return (0);
1067}
1068
1069static int
1070monitor_valid_userblob(u_char *data, u_int datalen)
1071{
1072 Buffer b;
d03f4262 1073 char *p;
826f3a39 1074 u_int len;
1075 int fail = 0;
826f3a39 1076
1077 buffer_init(&b);
1078 buffer_append(&b, data, datalen);
1079
1080 if (datafellows & SSH_OLD_SESSIONID) {
44a053a3 1081 p = buffer_ptr(&b);
1082 len = buffer_len(&b);
1083 if ((session_id2 == NULL) ||
1084 (len < session_id2_len) ||
1085 (memcmp(p, session_id2, session_id2_len) != 0))
1086 fail++;
826f3a39 1087 buffer_consume(&b, session_id2_len);
1088 } else {
44a053a3 1089 p = buffer_get_string(&b, &len);
1090 if ((session_id2 == NULL) ||
1091 (len != session_id2_len) ||
1092 (memcmp(p, session_id2, session_id2_len) != 0))
826f3a39 1093 fail++;
44a053a3 1094 xfree(p);
826f3a39 1095 }
1096 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1097 fail++;
1098 p = buffer_get_string(&b, NULL);
1099 if (strcmp(authctxt->user, p) != 0) {
7cac2b65 1100 logit("wrong user name passed to monitor: expected %s != %.100s",
826f3a39 1101 authctxt->user, p);
1102 fail++;
1103 }
1104 xfree(p);
1105 buffer_skip_string(&b);
1106 if (datafellows & SSH_BUG_PKAUTH) {
1107 if (!buffer_get_char(&b))
1108 fail++;
1109 } else {
1110 p = buffer_get_string(&b, NULL);
1111 if (strcmp("publickey", p) != 0)
1112 fail++;
1113 xfree(p);
1114 if (!buffer_get_char(&b))
1115 fail++;
1116 buffer_skip_string(&b);
1117 }
1118 buffer_skip_string(&b);
1119 if (buffer_len(&b) != 0)
1120 fail++;
1121 buffer_free(&b);
1122 return (fail == 0);
1123}
1124
1125static int
d03f4262 1126monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1127 char *chost)
826f3a39 1128{
1129 Buffer b;
d03f4262 1130 char *p;
826f3a39 1131 u_int len;
1132 int fail = 0;
826f3a39 1133
1134 buffer_init(&b);
1135 buffer_append(&b, data, datalen);
1136
44a053a3 1137 p = buffer_get_string(&b, &len);
1138 if ((session_id2 == NULL) ||
1139 (len != session_id2_len) ||
1140 (memcmp(p, session_id2, session_id2_len) != 0))
826f3a39 1141 fail++;
44a053a3 1142 xfree(p);
1143
826f3a39 1144 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1145 fail++;
1146 p = buffer_get_string(&b, NULL);
1147 if (strcmp(authctxt->user, p) != 0) {
7cac2b65 1148 logit("wrong user name passed to monitor: expected %s != %.100s",
826f3a39 1149 authctxt->user, p);
1150 fail++;
1151 }
1152 xfree(p);
1153 buffer_skip_string(&b); /* service */
1154 p = buffer_get_string(&b, NULL);
1155 if (strcmp(p, "hostbased") != 0)
1156 fail++;
1157 xfree(p);
1158 buffer_skip_string(&b); /* pkalg */
1159 buffer_skip_string(&b); /* pkblob */
1160
1161 /* verify client host, strip trailing dot if necessary */
1162 p = buffer_get_string(&b, NULL);
1163 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1164 p[len - 1] = '\0';
1165 if (strcmp(p, chost) != 0)
1166 fail++;
1167 xfree(p);
1168
1169 /* verify client user */
1170 p = buffer_get_string(&b, NULL);
1171 if (strcmp(p, cuser) != 0)
1172 fail++;
1173 xfree(p);
1174
1175 if (buffer_len(&b) != 0)
1176 fail++;
1177 buffer_free(&b);
1178 return (fail == 0);
1179}
1180
1181int
7e82606e 1182mm_answer_keyverify(int sock, Buffer *m)
826f3a39 1183{
1184 Key *key;
1185 u_char *signature, *data, *blob;
1186 u_int signaturelen, datalen, bloblen;
1187 int verified = 0;
1188 int valid_data = 0;
1189
1190 blob = buffer_get_string(m, &bloblen);
1191 signature = buffer_get_string(m, &signaturelen);
1192 data = buffer_get_string(m, &datalen);
1193
1194 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1195 !monitor_allowed_key(blob, bloblen))
44a053a3 1196 fatal("%s: bad key, not previously allowed", __func__);
826f3a39 1197
1198 key = key_from_blob(blob, bloblen);
1199 if (key == NULL)
44a053a3 1200 fatal("%s: bad public key blob", __func__);
826f3a39 1201
1202 switch (key_blobtype) {
1203 case MM_USERKEY:
1204 valid_data = monitor_valid_userblob(data, datalen);
1205 break;
1206 case MM_HOSTKEY:
1207 valid_data = monitor_valid_hostbasedblob(data, datalen,
1208 hostbased_cuser, hostbased_chost);
1209 break;
1210 default:
1211 valid_data = 0;
1212 break;
1213 }
1214 if (!valid_data)
44a053a3 1215 fatal("%s: bad signature data blob", __func__);
826f3a39 1216
1217 verified = key_verify(key, signature, signaturelen, data, datalen);
1218 debug3("%s: key %p signature %s",
44a053a3 1219 __func__, key, verified ? "verified" : "unverified");
826f3a39 1220
1221 key_free(key);
1222 xfree(blob);
1223 xfree(signature);
1224 xfree(data);
1225
276b07a3 1226 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1227
826f3a39 1228 monitor_reset_key_state();
1229
1230 buffer_clear(m);
1231 buffer_put_int(m, verified);
7e82606e 1232 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
826f3a39 1233
826f3a39 1234 return (verified);
1235}
1236
1237static void
1238mm_record_login(Session *s, struct passwd *pw)
1239{
1240 socklen_t fromlen;
1241 struct sockaddr_storage from;
1242
1243 /*
1244 * Get IP address of client. If the connection is not a socket, let
1245 * the address be 0.0.0.0.
1246 */
1247 memset(&from, 0, sizeof(from));
d03f4262 1248 fromlen = sizeof(from);
826f3a39 1249 if (packet_connection_is_on_socket()) {
826f3a39 1250 if (getpeername(packet_get_connection_in(),
1251 (struct sockaddr *) & from, &fromlen) < 0) {
1252 debug("getpeername: %.100s", strerror(errno));
540d72c3 1253 cleanup_exit(255);
826f3a39 1254 }
1255 }
1256 /* Record that there was a login on that tty from the remote host. */
1257 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
7cac2b65 1258 get_remote_name_or_ip(utmp_len, options.use_dns),
d03f4262 1259 (struct sockaddr *)&from, fromlen);
826f3a39 1260}
1261
1262static void
1263mm_session_close(Session *s)
1264{
7cac2b65 1265 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
826f3a39 1266 if (s->ttyfd != -1) {
44a053a3 1267 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
826f3a39 1268 session_pty_cleanup2(s);
1269 }
1270 s->used = 0;
1271}
1272
1273int
7e82606e 1274mm_answer_pty(int sock, Buffer *m)
826f3a39 1275{
1276 extern struct monitor *pmonitor;
1277 Session *s;
1278 int res, fd0;
1279
44a053a3 1280 debug3("%s entering", __func__);
826f3a39 1281
1282 buffer_clear(m);
1283 s = session_new();
1284 if (s == NULL)
1285 goto error;
1286 s->authctxt = authctxt;
1287 s->pw = authctxt->pw;
1288 s->pid = pmonitor->m_pid;
1289 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1290 if (res == 0)
1291 goto error;
826f3a39 1292 pty_setowner(authctxt->pw, s->tty);
1293
1294 buffer_put_int(m, 1);
1295 buffer_put_cstring(m, s->tty);
826f3a39 1296
1297 /* We need to trick ttyslot */
1298 if (dup2(s->ttyfd, 0) == -1)
44a053a3 1299 fatal("%s: dup2", __func__);
826f3a39 1300
1301 mm_record_login(s, authctxt->pw);
1302
1303 /* Now we can close the file descriptor again */
1304 close(0);
1305
7e82606e 1306 /* send messages generated by record_login */
1307 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1308 buffer_clear(&loginmsg);
1309
1310 mm_request_send(sock, MONITOR_ANS_PTY, m);
1311
1312 mm_send_fd(sock, s->ptyfd);
1313 mm_send_fd(sock, s->ttyfd);
1314
826f3a39 1315 /* make sure nothing uses fd 0 */
1316 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
44a053a3 1317 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
826f3a39 1318 if (fd0 != 0)
44a053a3 1319 error("%s: fd0 %d != 0", __func__, fd0);
826f3a39 1320
1321 /* slave is not needed */
1322 close(s->ttyfd);
1323 s->ttyfd = s->ptyfd;
1324 /* no need to dup() because nobody closes ptyfd */
1325 s->ptymaster = s->ptyfd;
1326
44a053a3 1327 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
826f3a39 1328
1329 return (0);
1330
1331 error:
1332 if (s != NULL)
1333 mm_session_close(s);
1334 buffer_put_int(m, 0);
7e82606e 1335 mm_request_send(sock, MONITOR_ANS_PTY, m);
826f3a39 1336 return (0);
1337}
1338
1339int
7e82606e 1340mm_answer_pty_cleanup(int sock, Buffer *m)
826f3a39 1341{
1342 Session *s;
1343 char *tty;
1344
44a053a3 1345 debug3("%s entering", __func__);
826f3a39 1346
1347 tty = buffer_get_string(m, NULL);
1348 if ((s = session_by_tty(tty)) != NULL)
1349 mm_session_close(s);
1350 buffer_clear(m);
1351 xfree(tty);
1352 return (0);
1353}
1354
1355int
7e82606e 1356mm_answer_sesskey(int sock, Buffer *m)
826f3a39 1357{
1358 BIGNUM *p;
1359 int rsafail;
1360
1361 /* Turn off permissions */
dfddba3d 1362 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
826f3a39 1363
1364 if ((p = BN_new()) == NULL)
44a053a3 1365 fatal("%s: BN_new", __func__);
826f3a39 1366
1367 buffer_get_bignum2(m, p);
1368
1369 rsafail = ssh1_session_key(p);
1370
1371 buffer_clear(m);
1372 buffer_put_int(m, rsafail);
1373 buffer_put_bignum2(m, p);
1374
1375 BN_clear_free(p);
1376
7e82606e 1377 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
826f3a39 1378
1379 /* Turn on permissions for sessid passing */
1380 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1381
1382 return (0);
1383}
1384
1385int
7e82606e 1386mm_answer_sessid(int sock, Buffer *m)
826f3a39 1387{
1388 int i;
1389
44a053a3 1390 debug3("%s entering", __func__);
826f3a39 1391
1392 if (buffer_len(m) != 16)
44a053a3 1393 fatal("%s: bad ssh1 session id", __func__);
826f3a39 1394 for (i = 0; i < 16; i++)
1395 session_id[i] = buffer_get_char(m);
1396
1397 /* Turn on permissions for getpwnam */
1398 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1399
1400 return (0);
1401}
1402
1403int
7e82606e 1404mm_answer_rsa_keyallowed(int sock, Buffer *m)
826f3a39 1405{
1406 BIGNUM *client_n;
1407 Key *key = NULL;
1408 u_char *blob = NULL;
1409 u_int blen = 0;
1410 int allowed = 0;
1411
44a053a3 1412 debug3("%s entering", __func__);
826f3a39 1413
44a053a3 1414 if (options.rsa_authentication && authctxt->valid) {
826f3a39 1415 if ((client_n = BN_new()) == NULL)
44a053a3 1416 fatal("%s: BN_new", __func__);
826f3a39 1417 buffer_get_bignum2(m, client_n);
1418 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1419 BN_clear_free(client_n);
1420 }
1421 buffer_clear(m);
1422 buffer_put_int(m, allowed);
bfe49944 1423 buffer_put_int(m, forced_command != NULL);
826f3a39 1424
1425 /* clear temporarily storage (used by generate challenge) */
1426 monitor_reset_key_state();
1427
1428 if (allowed && key != NULL) {
1429 key->type = KEY_RSA; /* cheat for key_to_blob */
1430 if (key_to_blob(key, &blob, &blen) == 0)
44a053a3 1431 fatal("%s: key_to_blob failed", __func__);
826f3a39 1432 buffer_put_string(m, blob, blen);
1433
1434 /* Save temporarily for comparison in verify */
1435 key_blob = blob;
1436 key_bloblen = blen;
1437 key_blobtype = MM_RSAUSERKEY;
826f3a39 1438 }
bfe49944 1439 if (key != NULL)
1440 key_free(key);
826f3a39 1441
1442 mm_append_debug(m);
1443
7e82606e 1444 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
826f3a39 1445
1446 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1447 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1448 return (0);
1449}
1450
1451int
7e82606e 1452mm_answer_rsa_challenge(int sock, Buffer *m)
826f3a39 1453{
1454 Key *key = NULL;
1455 u_char *blob;
1456 u_int blen;
1457
44a053a3 1458 debug3("%s entering", __func__);
826f3a39 1459
1460 if (!authctxt->valid)
44a053a3 1461 fatal("%s: authctxt not valid", __func__);
826f3a39 1462 blob = buffer_get_string(m, &blen);
1463 if (!monitor_allowed_key(blob, blen))
44a053a3 1464 fatal("%s: bad key, not previously allowed", __func__);
826f3a39 1465 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
44a053a3 1466 fatal("%s: key type mismatch", __func__);
826f3a39 1467 if ((key = key_from_blob(blob, blen)) == NULL)
44a053a3 1468 fatal("%s: received bad key", __func__);
826f3a39 1469
1470 if (ssh1_challenge)
1471 BN_clear_free(ssh1_challenge);
1472 ssh1_challenge = auth_rsa_generate_challenge(key);
1473
1474 buffer_clear(m);
1475 buffer_put_bignum2(m, ssh1_challenge);
1476
44a053a3 1477 debug3("%s sending reply", __func__);
7e82606e 1478 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
826f3a39 1479
1480 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
bfe49944 1481
1482 xfree(blob);
1483 key_free(key);
826f3a39 1484 return (0);
1485}
1486
1487int
7e82606e 1488mm_answer_rsa_response(int sock, Buffer *m)
826f3a39 1489{
1490 Key *key = NULL;
1491 u_char *blob, *response;
1492 u_int blen, len;
1493 int success;
1494
44a053a3 1495 debug3("%s entering", __func__);
826f3a39 1496
1497 if (!authctxt->valid)
44a053a3 1498 fatal("%s: authctxt not valid", __func__);
826f3a39 1499 if (ssh1_challenge == NULL)
44a053a3 1500 fatal("%s: no ssh1_challenge", __func__);
826f3a39 1501
1502 blob = buffer_get_string(m, &blen);
1503 if (!monitor_allowed_key(blob, blen))
44a053a3 1504 fatal("%s: bad key, not previously allowed", __func__);
826f3a39 1505 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
44a053a3 1506 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
826f3a39 1507 if ((key = key_from_blob(blob, blen)) == NULL)
44a053a3 1508 fatal("%s: received bad key", __func__);
826f3a39 1509 response = buffer_get_string(m, &len);
1510 if (len != 16)
44a053a3 1511 fatal("%s: received bad response to challenge", __func__);
826f3a39 1512 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1513
bfe49944 1514 xfree(blob);
826f3a39 1515 key_free(key);
1516 xfree(response);
1517
1518 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1519
1520 /* reset state */
1521 BN_clear_free(ssh1_challenge);
1522 ssh1_challenge = NULL;
1523 monitor_reset_key_state();
1524
1525 buffer_clear(m);
1526 buffer_put_int(m, success);
7e82606e 1527 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
826f3a39 1528
1529 return (success);
1530}
1531
1532int
7e82606e 1533mm_answer_term(int sock, Buffer *req)
826f3a39 1534{
1535 extern struct monitor *pmonitor;
1536 int res, status;
1537
44a053a3 1538 debug3("%s: tearing down sessions", __func__);
826f3a39 1539
1540 /* The child is terminating */
1541 session_destroy_all(&mm_session_close);
1542
1543 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1544 if (errno != EINTR)
1545 exit(1);
1546
1547 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1548
1549 /* Terminate process */
7e82606e 1550 exit(res);
826f3a39 1551}
1552
dfddba3d 1553#ifdef SSH_AUDIT_EVENTS
1554/* Report that an audit event occurred */
1555int
1556mm_answer_audit_event(int socket, Buffer *m)
1557{
1558 ssh_audit_event_t event;
1559
1560 debug3("%s entering", __func__);
1561
1562 event = buffer_get_int(m);
dfddba3d 1563 switch(event) {
1564 case SSH_AUTH_FAIL_PUBKEY:
1565 case SSH_AUTH_FAIL_HOSTBASED:
1566 case SSH_AUTH_FAIL_GSSAPI:
1567 case SSH_LOGIN_EXCEED_MAXTRIES:
1568 case SSH_LOGIN_ROOT_DENIED:
1569 case SSH_CONNECTION_CLOSE:
1570 case SSH_INVALID_USER:
1571 audit_event(event);
1572 break;
1573 default:
1574 fatal("Audit event type %d not permitted", event);
1575 }
1576
1577 return (0);
1578}
1579
1580int
1581mm_answer_audit_command(int socket, Buffer *m)
1582{
1583 u_int len;
1584 char *cmd;
1585
1586 debug3("%s entering", __func__);
1587 cmd = buffer_get_string(m, &len);
1588 /* sanity check command, if so how? */
1589 audit_run_command(cmd);
1590 xfree(cmd);
dfddba3d 1591 return (0);
1592}
1593#endif /* SSH_AUDIT_EVENTS */
1594
826f3a39 1595void
1596monitor_apply_keystate(struct monitor *pmonitor)
1597{
1598 if (compat20) {
1599 set_newkeys(MODE_IN);
1600 set_newkeys(MODE_OUT);
1601 } else {
826f3a39 1602 packet_set_protocol_flags(child_state.ssh1protoflags);
44a053a3 1603 packet_set_encryption_key(child_state.ssh1key,
1604 child_state.ssh1keylen, child_state.ssh1cipher);
1605 xfree(child_state.ssh1key);
826f3a39 1606 }
1607
44a053a3 1608 /* for rc4 and other stateful ciphers */
826f3a39 1609 packet_set_keycontext(MODE_OUT, child_state.keyout);
1610 xfree(child_state.keyout);
1611 packet_set_keycontext(MODE_IN, child_state.keyin);
1612 xfree(child_state.keyin);
1613
1614 if (!compat20) {
1615 packet_set_iv(MODE_OUT, child_state.ivout);
1616 xfree(child_state.ivout);
1617 packet_set_iv(MODE_IN, child_state.ivin);
1618 xfree(child_state.ivin);
1619 }
1620
1621 memcpy(&incoming_stream, &child_state.incoming,
1622 sizeof(incoming_stream));
1623 memcpy(&outgoing_stream, &child_state.outgoing,
1624 sizeof(outgoing_stream));
1625
1626 /* Update with new address */
44a053a3 1627 if (options.compression)
1628 mm_init_compression(pmonitor->m_zlib);
826f3a39 1629
1630 /* Network I/O buffers */
1631 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1632 buffer_clear(&input);
1633 buffer_append(&input, child_state.input, child_state.ilen);
1634 memset(child_state.input, 0, child_state.ilen);
1635 xfree(child_state.input);
1636
1637 buffer_clear(&output);
1638 buffer_append(&output, child_state.output, child_state.olen);
1639 memset(child_state.output, 0, child_state.olen);
1640 xfree(child_state.output);
1641}
1642
1643static Kex *
1644mm_get_kex(Buffer *m)
1645{
1646 Kex *kex;
1647 void *blob;
1648 u_int bloblen;
1649
1650 kex = xmalloc(sizeof(*kex));
1651 memset(kex, 0, sizeof(*kex));
1652 kex->session_id = buffer_get_string(m, &kex->session_id_len);
44a053a3 1653 if ((session_id2 == NULL) ||
1654 (kex->session_id_len != session_id2_len) ||
1655 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1656 fatal("mm_get_get: internal error: bad session id");
826f3a39 1657 kex->we_need = buffer_get_int(m);
bfe49944 1658 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
7e82606e 1659 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
bfe49944 1660 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
23987cb8 1661#ifdef GSSAPI
7cac2b65 1662 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
fe4ad273 1663 kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server;
23987cb8 1664#endif
826f3a39 1665 kex->server = 1;
1666 kex->hostkey_type = buffer_get_int(m);
1667 kex->kex_type = buffer_get_int(m);
1668 blob = buffer_get_string(m, &bloblen);
1669 buffer_init(&kex->my);
1670 buffer_append(&kex->my, blob, bloblen);
1671 xfree(blob);
1672 blob = buffer_get_string(m, &bloblen);
1673 buffer_init(&kex->peer);
1674 buffer_append(&kex->peer, blob, bloblen);
1675 xfree(blob);
1676 kex->done = 1;
1677 kex->flags = buffer_get_int(m);
1678 kex->client_version_string = buffer_get_string(m, NULL);
1679 kex->server_version_string = buffer_get_string(m, NULL);
1680 kex->load_host_key=&get_hostkey_by_type;
1681 kex->host_key_index=&get_hostkey_index;
1682
1683 return (kex);
1684}
1685
1686/* This function requries careful sanity checking */
1687
1688void
1689mm_get_keystate(struct monitor *pmonitor)
1690{
1691 Buffer m;
1692 u_char *blob, *p;
1693 u_int bloblen, plen;
7cac2b65 1694 u_int32_t seqnr, packets;
1695 u_int64_t blocks;
826f3a39 1696
44a053a3 1697 debug3("%s: Waiting for new keys", __func__);
826f3a39 1698
1699 buffer_init(&m);
1700 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1701 if (!compat20) {
1702 child_state.ssh1protoflags = buffer_get_int(&m);
1703 child_state.ssh1cipher = buffer_get_int(&m);
44a053a3 1704 child_state.ssh1key = buffer_get_string(&m,
1705 &child_state.ssh1keylen);
826f3a39 1706 child_state.ivout = buffer_get_string(&m,
1707 &child_state.ivoutlen);
1708 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1709 goto skip;
1710 } else {
1711 /* Get the Kex for rekeying */
1712 *pmonitor->m_pkex = mm_get_kex(&m);
1713 }
1714
1715 blob = buffer_get_string(&m, &bloblen);
1716 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1717 xfree(blob);
1718
44a053a3 1719 debug3("%s: Waiting for second key", __func__);
826f3a39 1720 blob = buffer_get_string(&m, &bloblen);
1721 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1722 xfree(blob);
1723
1724 /* Now get sequence numbers for the packets */
7cac2b65 1725 seqnr = buffer_get_int(&m);
1726 blocks = buffer_get_int64(&m);
1727 packets = buffer_get_int(&m);
1728 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1729 seqnr = buffer_get_int(&m);
1730 blocks = buffer_get_int64(&m);
1731 packets = buffer_get_int(&m);
1732 packet_set_state(MODE_IN, seqnr, blocks, packets);
826f3a39 1733
1734 skip:
1735 /* Get the key context */
1736 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1737 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1738
44a053a3 1739 debug3("%s: Getting compression state", __func__);
826f3a39 1740 /* Get compression state */
1741 p = buffer_get_string(&m, &plen);
1742 if (plen != sizeof(child_state.outgoing))
44a053a3 1743 fatal("%s: bad request size", __func__);
826f3a39 1744 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1745 xfree(p);
1746
1747 p = buffer_get_string(&m, &plen);
1748 if (plen != sizeof(child_state.incoming))
44a053a3 1749 fatal("%s: bad request size", __func__);
826f3a39 1750 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1751 xfree(p);
1752
1753 /* Network I/O buffers */
44a053a3 1754 debug3("%s: Getting Network I/O buffers", __func__);
826f3a39 1755 child_state.input = buffer_get_string(&m, &child_state.ilen);
1756 child_state.output = buffer_get_string(&m, &child_state.olen);
1757
1758 buffer_free(&m);
1759}
1760
1761
1762/* Allocation functions for zlib */
1763void *
1764mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1765{
bfe49944 1766 size_t len = (size_t) size * ncount;
826f3a39 1767 void *address;
1768
d03f4262 1769 if (len == 0 || ncount > SIZE_T_MAX / size)
276b07a3 1770 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1771
1772 address = mm_malloc(mm, len);
826f3a39 1773
1774 return (address);
1775}
1776
1777void
1778mm_zfree(struct mm_master *mm, void *address)
1779{
1780 mm_free(mm, address);
1781}
1782
1783void
1784mm_init_compression(struct mm_master *mm)
1785{
1786 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1787 outgoing_stream.zfree = (free_func)mm_zfree;
1788 outgoing_stream.opaque = mm;
1789
1790 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1791 incoming_stream.zfree = (free_func)mm_zfree;
1792 incoming_stream.opaque = mm;
1793}
1794
1795/* XXX */
1796
1797#define FD_CLOSEONEXEC(x) do { \
1798 if (fcntl(x, F_SETFD, 1) == -1) \
1799 fatal("fcntl(%d, F_SETFD)", x); \
1800} while (0)
1801
1802static void
1803monitor_socketpair(int *pair)
1804{
1805#ifdef HAVE_SOCKETPAIR
1806 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
44a053a3 1807 fatal("%s: socketpair", __func__);
826f3a39 1808#else
1809 fatal("%s: UsePrivilegeSeparation=yes not supported",
44a053a3 1810 __func__);
826f3a39 1811#endif
1812 FD_CLOSEONEXEC(pair[0]);
1813 FD_CLOSEONEXEC(pair[1]);
1814}
1815
1816#define MM_MEMSIZE 65536
1817
1818struct monitor *
1819monitor_init(void)
1820{
1821 struct monitor *mon;
1822 int pair[2];
1823
1824 mon = xmalloc(sizeof(*mon));
1825
540d72c3 1826 mon->m_pid = 0;
826f3a39 1827 monitor_socketpair(pair);
1828
1829 mon->m_recvfd = pair[0];
1830 mon->m_sendfd = pair[1];
1831
1832 /* Used to share zlib space across processes */
44a053a3 1833 if (options.compression) {
1834 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1835 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
826f3a39 1836
44a053a3 1837 /* Compression needs to share state across borders */
1838 mm_init_compression(mon->m_zlib);
1839 }
826f3a39 1840
1841 return mon;
1842}
1843
1844void
1845monitor_reinit(struct monitor *mon)
1846{
1847 int pair[2];
1848
1849 monitor_socketpair(pair);
1850
1851 mon->m_recvfd = pair[0];
1852 mon->m_sendfd = pair[1];
1853}
b59afbfe 1854
7cac2b65 1855#ifdef GSSAPI
1856int
7e82606e 1857mm_answer_gss_setup_ctx(int sock, Buffer *m)
7cac2b65 1858{
7e82606e 1859 gss_OID_desc goid;
7cac2b65 1860 OM_uint32 major;
1861 u_int len;
b59afbfe 1862
7e82606e 1863 goid.elements = buffer_get_string(m, &len);
1864 goid.length = len;
b59afbfe 1865
7e82606e 1866 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
e23e524c 1867
7e82606e 1868 xfree(goid.elements);
7cac2b65 1869
1870 buffer_clear(m);
1871 buffer_put_int(m, major);
1872
08822d99 1873 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
7cac2b65 1874
1875 /* Now we have a context, enable the step */
1876 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1877
1878 return (0);
b59afbfe 1879}
1880
1881int
7e82606e 1882mm_answer_gss_accept_ctx(int sock, Buffer *m)
7cac2b65 1883{
1884 gss_buffer_desc in;
1885 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
08822d99 1886 OM_uint32 major, minor;
7cac2b65 1887 OM_uint32 flags = 0; /* GSI needs this */
1888 u_int len;
b59afbfe 1889
7cac2b65 1890 in.value = buffer_get_string(m, &len);
1891 in.length = len;
1892 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1893 xfree(in.value);
b59afbfe 1894
7cac2b65 1895 buffer_clear(m);
1896 buffer_put_int(m, major);
1897 buffer_put_string(m, out.value, out.length);
1898 buffer_put_int(m, flags);
7e82606e 1899 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
b59afbfe 1900
7cac2b65 1901 gss_release_buffer(&minor, &out);
b59afbfe 1902
08822d99 1903 if (major == GSS_S_COMPLETE) {
7cac2b65 1904 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1905 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1906 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
540d72c3 1907 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
fe4ad273 1908 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
e23e524c 1909 }
7cac2b65 1910 return (0);
b59afbfe 1911}
e23e524c 1912
540d72c3 1913int
7e82606e 1914mm_answer_gss_checkmic(int sock, Buffer *m)
540d72c3 1915{
1916 gss_buffer_desc gssbuf, mic;
1917 OM_uint32 ret;
1918 u_int len;
1919
1920 gssbuf.value = buffer_get_string(m, &len);
1921 gssbuf.length = len;
1922 mic.value = buffer_get_string(m, &len);
1923 mic.length = len;
1924
1925 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1926
1927 xfree(gssbuf.value);
1928 xfree(mic.value);
1929
1930 buffer_clear(m);
1931 buffer_put_int(m, ret);
1932
7e82606e 1933 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
540d72c3 1934
1935 if (!GSS_ERROR(ret))
1936 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1937
1938 return (0);
1939}
1940
b59afbfe 1941int
7e82606e 1942mm_answer_gss_userok(int sock, Buffer *m)
7cac2b65 1943{
b59afbfe 1944 int authenticated;
1945
7cac2b65 1946 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
b59afbfe 1947
7cac2b65 1948 buffer_clear(m);
1949 buffer_put_int(m, authenticated);
b59afbfe 1950
7cac2b65 1951 debug3("%s: sending result %d", __func__, authenticated);
7e82606e 1952 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
b59afbfe 1953
08822d99 1954 auth_method = "gssapi-with-mic";
7cac2b65 1955
1956 /* Monitor loop will terminate if authenticated */
1957 return (authenticated);
b59afbfe 1958}
23987cb8 1959
fe4ad273 1960int
1961mm_answer_gss_sign(int socket, Buffer *m)
1962{
1963 gss_buffer_desc data;
1964 gss_buffer_desc hash = GSS_C_EMPTY_BUFFER;
1965 OM_uint32 major, minor;
77e22b94 1966 u_int len;
b59afbfe 1967
fe4ad273 1968 data.value = buffer_get_string(m, &len);
77e22b94 1969 data.length = len;
fe4ad273 1970 if (data.length != 20)
1971 fatal("%s: data length incorrect: %d", __func__, data.length);
b59afbfe 1972
fe4ad273 1973 /* Save the session ID on the first time around */
1974 if (session_id2_len == 0) {
1975 session_id2_len = data.length;
1976 session_id2 = xmalloc(session_id2_len);
1977 memcpy(session_id2, data.value, session_id2_len);
1978 }
1979 major = ssh_gssapi_sign(gsscontext, &data, &hash);
b59afbfe 1980
fe4ad273 1981 xfree(data.value);
b59afbfe 1982
fe4ad273 1983 buffer_clear(m);
1984 buffer_put_int(m, major);
1985 buffer_put_string(m, hash.value, hash.length);
b59afbfe 1986
fe4ad273 1987 mm_request_send(socket, MONITOR_ANS_GSSSIGN, m);
b59afbfe 1988
fe4ad273 1989 gss_release_buffer(&minor, &hash);
b59afbfe 1990
fe4ad273 1991 /* Turn on getpwnam permissions */
b59afbfe 1992 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
fe4ad273 1993
1994 return (0);
b59afbfe 1995}
1996
e23e524c 1997int
1998mm_answer_gss_error(int socket, Buffer *m) {
1999 OM_uint32 major,minor;
2000 char *msg;
2001
2002 msg=ssh_gssapi_last_error(gsscontext,&major,&minor);
2003 buffer_clear(m);
2004 buffer_put_int(m,major);
2005 buffer_put_int(m,minor);
2006 buffer_put_cstring(m,msg);
2007
2008 mm_request_send(socket,MONITOR_ANS_GSSERR,m);
2009
2010 xfree(msg);
2011
2012 return(0);
2013}
2014
9eeaa28e 2015int
2016mm_answer_gss_indicate_mechs(int socket, Buffer *m) {
2017 OM_uint32 major,minor;
2018 gss_OID_set mech_set;
97a8553e 2019 size_t i;
9eeaa28e 2020
2021 major=gss_indicate_mechs(&minor, &mech_set);
2022
2023 buffer_clear(m);
2024 buffer_put_int(m, major);
2025 buffer_put_int(m, mech_set->count);
2026 for (i=0; i < mech_set->count; i++) {
2027 buffer_put_string(m, mech_set->elements[i].elements,
2028 mech_set->elements[i].length);
2029 }
2030
e23e524c 2031#if !defined(MECHGLUE) /* mechglue memory management bug ??? */
2032 gss_release_oid_set(&minor,&mech_set);
2033#endif
2034
9eeaa28e 2035 mm_request_send(socket,MONITOR_ANS_GSSMECHS,m);
2036
2037 return(0);
2038}
2039
2040int
e23e524c 2041mm_answer_gss_localname(int socket, Buffer *m) {
2042 char *name;
9eeaa28e 2043
e23e524c 2044 ssh_gssapi_localname(&name);
9eeaa28e 2045
e23e524c 2046 buffer_clear(m);
2047 if (name) {
2048 buffer_put_cstring(m, name);
2049 debug3("%s: sending result %s", __func__, name);
2050 xfree(name);
2051 } else {
2052 buffer_put_cstring(m, "");
2053 debug3("%s: sending result \"\"", __func__);
2054 }
2055
2056 mm_request_send(socket, MONITOR_ANS_GSSLOCALNAME, m);
9eeaa28e 2057
23987cb8 2058 return(0);
9eeaa28e 2059}
fe4ad273 2060
b59afbfe 2061#endif /* GSSAPI */
This page took 0.604156 seconds and 5 git commands to generate.