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