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