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