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