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