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