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