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