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