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