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