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