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.36 2003/04/01 10:22:21 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 u_int success; 655 656 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts, 657 &prompts, &echo_on) < 0 ? 0 : 1; 658 659 buffer_clear(m); 660 buffer_put_int(m, success); 661 if (success) 662 buffer_put_cstring(m, prompts[0]); 663 664 debug3("%s: sending challenge success: %u", __func__, success); 665 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m); 666 667 if (success) { 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 u_int success; 712 713 success = skeychallenge(&skey, authctxt->user, challenge) < 0 ? 0 : 1; 714 715 buffer_clear(m); 716 buffer_put_int(m, success); 717 if (success) 718 buffer_put_cstring(m, challenge); 719 720 debug3("%s: sending challenge success: %u", __func__, success); 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 } 924 if (key != NULL) 925 key_free(key); 926 927 /* clear temporarily storage (used by verify) */ 928 monitor_reset_key_state(); 929 930 if (allowed) { 931 /* Save temporarily for comparison in verify */ 932 key_blob = blob; 933 key_bloblen = bloblen; 934 key_blobtype = type; 935 hostbased_cuser = cuser; 936 hostbased_chost = chost; 937 } 938 939 debug3("%s: key %p is %s", 940 __func__, key, allowed ? "allowed" : "disallowed"); 941 942 buffer_clear(m); 943 buffer_put_int(m, allowed); 944 buffer_put_int(m, forced_command != NULL); 945 946 mm_append_debug(m); 947 948 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m); 949 950 if (type == MM_RSAHOSTKEY) 951 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed); 952 953 return (0); 954 } 955 956 static int 957 monitor_valid_userblob(u_char *data, u_int datalen) 958 { 959 Buffer b; 960 char *p; 961 u_int len; 962 int fail = 0; 963 964 buffer_init(&b); 965 buffer_append(&b, data, datalen); 966 967 if (datafellows & SSH_OLD_SESSIONID) { 968 p = buffer_ptr(&b); 969 len = buffer_len(&b); 970 if ((session_id2 == NULL) || 971 (len < session_id2_len) || 972 (memcmp(p, session_id2, session_id2_len) != 0)) 973 fail++; 974 buffer_consume(&b, session_id2_len); 975 } else { 976 p = buffer_get_string(&b, &len); 977 if ((session_id2 == NULL) || 978 (len != session_id2_len) || 979 (memcmp(p, session_id2, session_id2_len) != 0)) 980 fail++; 981 xfree(p); 982 } 983 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST) 984 fail++; 985 p = buffer_get_string(&b, NULL); 986 if (strcmp(authctxt->user, p) != 0) { 987 log("wrong user name passed to monitor: expected %s != %.100s", 988 authctxt->user, p); 989 fail++; 990 } 991 xfree(p); 992 buffer_skip_string(&b); 993 if (datafellows & SSH_BUG_PKAUTH) { 994 if (!buffer_get_char(&b)) 995 fail++; 996 } else { 997 p = buffer_get_string(&b, NULL); 998 if (strcmp("publickey", p) != 0) 999 fail++; 1000 xfree(p); 1001 if (!buffer_get_char(&b)) 1002 fail++; 1003 buffer_skip_string(&b); 1004 } 1005 buffer_skip_string(&b); 1006 if (buffer_len(&b) != 0) 1007 fail++; 1008 buffer_free(&b); 1009 return (fail == 0); 1010 } 1011 1012 static int 1013 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser, 1014 char *chost) 1015 { 1016 Buffer b; 1017 char *p; 1018 u_int len; 1019 int fail = 0; 1020 1021 buffer_init(&b); 1022 buffer_append(&b, data, datalen); 1023 1024 p = buffer_get_string(&b, &len); 1025 if ((session_id2 == NULL) || 1026 (len != session_id2_len) || 1027 (memcmp(p, session_id2, session_id2_len) != 0)) 1028 fail++; 1029 xfree(p); 1030 1031 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST) 1032 fail++; 1033 p = buffer_get_string(&b, NULL); 1034 if (strcmp(authctxt->user, p) != 0) { 1035 log("wrong user name passed to monitor: expected %s != %.100s", 1036 authctxt->user, p); 1037 fail++; 1038 } 1039 xfree(p); 1040 buffer_skip_string(&b); /* service */ 1041 p = buffer_get_string(&b, NULL); 1042 if (strcmp(p, "hostbased") != 0) 1043 fail++; 1044 xfree(p); 1045 buffer_skip_string(&b); /* pkalg */ 1046 buffer_skip_string(&b); /* pkblob */ 1047 1048 /* verify client host, strip trailing dot if necessary */ 1049 p = buffer_get_string(&b, NULL); 1050 if (((len = strlen(p)) > 0) && p[len - 1] == '.') 1051 p[len - 1] = '\0'; 1052 if (strcmp(p, chost) != 0) 1053 fail++; 1054 xfree(p); 1055 1056 /* verify client user */ 1057 p = buffer_get_string(&b, NULL); 1058 if (strcmp(p, cuser) != 0) 1059 fail++; 1060 xfree(p); 1061 1062 if (buffer_len(&b) != 0) 1063 fail++; 1064 buffer_free(&b); 1065 return (fail == 0); 1066 } 1067 1068 int 1069 mm_answer_keyverify(int socket, Buffer *m) 1070 { 1071 Key *key; 1072 u_char *signature, *data, *blob; 1073 u_int signaturelen, datalen, bloblen; 1074 int verified = 0; 1075 int valid_data = 0; 1076 1077 blob = buffer_get_string(m, &bloblen); 1078 signature = buffer_get_string(m, &signaturelen); 1079 data = buffer_get_string(m, &datalen); 1080 1081 if (hostbased_cuser == NULL || hostbased_chost == NULL || 1082 !monitor_allowed_key(blob, bloblen)) 1083 fatal("%s: bad key, not previously allowed", __func__); 1084 1085 key = key_from_blob(blob, bloblen); 1086 if (key == NULL) 1087 fatal("%s: bad public key blob", __func__); 1088 1089 switch (key_blobtype) { 1090 case MM_USERKEY: 1091 valid_data = monitor_valid_userblob(data, datalen); 1092 break; 1093 case MM_HOSTKEY: 1094 valid_data = monitor_valid_hostbasedblob(data, datalen, 1095 hostbased_cuser, hostbased_chost); 1096 break; 1097 default: 1098 valid_data = 0; 1099 break; 1100 } 1101 if (!valid_data) 1102 fatal("%s: bad signature data blob", __func__); 1103 1104 verified = key_verify(key, signature, signaturelen, data, datalen); 1105 debug3("%s: key %p signature %s", 1106 __func__, key, verified ? "verified" : "unverified"); 1107 1108 key_free(key); 1109 xfree(blob); 1110 xfree(signature); 1111 xfree(data); 1112 1113 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased"; 1114 1115 monitor_reset_key_state(); 1116 1117 buffer_clear(m); 1118 buffer_put_int(m, verified); 1119 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m); 1120 1121 return (verified); 1122 } 1123 1124 static void 1125 mm_record_login(Session *s, struct passwd *pw) 1126 { 1127 socklen_t fromlen; 1128 struct sockaddr_storage from; 1129 1130 /* 1131 * Get IP address of client. If the connection is not a socket, let 1132 * the address be 0.0.0.0. 1133 */ 1134 memset(&from, 0, sizeof(from)); 1135 fromlen = sizeof(from); 1136 if (packet_connection_is_on_socket()) { 1137 if (getpeername(packet_get_connection_in(), 1138 (struct sockaddr *) & from, &fromlen) < 0) { 1139 debug("getpeername: %.100s", strerror(errno)); 1140 fatal_cleanup(); 1141 } 1142 } 1143 /* Record that there was a login on that tty from the remote host. */ 1144 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid, 1145 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping), 1146 (struct sockaddr *)&from, fromlen); 1147 } 1148 1149 static void 1150 mm_session_close(Session *s) 1151 { 1152 debug3("%s: session %d pid %d", __func__, s->self, s->pid); 1153 if (s->ttyfd != -1) { 1154 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd); 1155 fatal_remove_cleanup(session_pty_cleanup2, (void *)s); 1156 session_pty_cleanup2(s); 1157 } 1158 s->used = 0; 1159 } 1160 1161 int 1162 mm_answer_pty(int socket, Buffer *m) 1163 { 1164 extern struct monitor *pmonitor; 1165 Session *s; 1166 int res, fd0; 1167 1168 debug3("%s entering", __func__); 1169 1170 buffer_clear(m); 1171 s = session_new(); 1172 if (s == NULL) 1173 goto error; 1174 s->authctxt = authctxt; 1175 s->pw = authctxt->pw; 1176 s->pid = pmonitor->m_pid; 1177 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)); 1178 if (res == 0) 1179 goto error; 1180 fatal_add_cleanup(session_pty_cleanup2, (void *)s); 1181 pty_setowner(authctxt->pw, s->tty); 1182 1183 buffer_put_int(m, 1); 1184 buffer_put_cstring(m, s->tty); 1185 mm_request_send(socket, MONITOR_ANS_PTY, m); 1186 1187 mm_send_fd(socket, s->ptyfd); 1188 mm_send_fd(socket, s->ttyfd); 1189 1190 /* We need to trick ttyslot */ 1191 if (dup2(s->ttyfd, 0) == -1) 1192 fatal("%s: dup2", __func__); 1193 1194 mm_record_login(s, authctxt->pw); 1195 1196 /* Now we can close the file descriptor again */ 1197 close(0); 1198 1199 /* make sure nothing uses fd 0 */ 1200 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0) 1201 fatal("%s: open(/dev/null): %s", __func__, strerror(errno)); 1202 if (fd0 != 0) 1203 error("%s: fd0 %d != 0", __func__, fd0); 1204 1205 /* slave is not needed */ 1206 close(s->ttyfd); 1207 s->ttyfd = s->ptyfd; 1208 /* no need to dup() because nobody closes ptyfd */ 1209 s->ptymaster = s->ptyfd; 1210 1211 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd); 1212 1213 return (0); 1214 1215 error: 1216 if (s != NULL) 1217 mm_session_close(s); 1218 buffer_put_int(m, 0); 1219 mm_request_send(socket, MONITOR_ANS_PTY, m); 1220 return (0); 1221 } 1222 1223 int 1224 mm_answer_pty_cleanup(int socket, Buffer *m) 1225 { 1226 Session *s; 1227 char *tty; 1228 1229 debug3("%s entering", __func__); 1230 1231 tty = buffer_get_string(m, NULL); 1232 if ((s = session_by_tty(tty)) != NULL) 1233 mm_session_close(s); 1234 buffer_clear(m); 1235 xfree(tty); 1236 return (0); 1237 } 1238 1239 int 1240 mm_answer_sesskey(int socket, Buffer *m) 1241 { 1242 BIGNUM *p; 1243 int rsafail; 1244 1245 /* Turn off permissions */ 1246 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1); 1247 1248 if ((p = BN_new()) == NULL) 1249 fatal("%s: BN_new", __func__); 1250 1251 buffer_get_bignum2(m, p); 1252 1253 rsafail = ssh1_session_key(p); 1254 1255 buffer_clear(m); 1256 buffer_put_int(m, rsafail); 1257 buffer_put_bignum2(m, p); 1258 1259 BN_clear_free(p); 1260 1261 mm_request_send(socket, MONITOR_ANS_SESSKEY, m); 1262 1263 /* Turn on permissions for sessid passing */ 1264 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1); 1265 1266 return (0); 1267 } 1268 1269 int 1270 mm_answer_sessid(int socket, Buffer *m) 1271 { 1272 int i; 1273 1274 debug3("%s entering", __func__); 1275 1276 if (buffer_len(m) != 16) 1277 fatal("%s: bad ssh1 session id", __func__); 1278 for (i = 0; i < 16; i++) 1279 session_id[i] = buffer_get_char(m); 1280 1281 /* Turn on permissions for getpwnam */ 1282 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1); 1283 1284 return (0); 1285 } 1286 1287 int 1288 mm_answer_rsa_keyallowed(int socket, Buffer *m) 1289 { 1290 BIGNUM *client_n; 1291 Key *key = NULL; 1292 u_char *blob = NULL; 1293 u_int blen = 0; 1294 int allowed = 0; 1295 1296 debug3("%s entering", __func__); 1297 1298 if (options.rsa_authentication && authctxt->valid) { 1299 if ((client_n = BN_new()) == NULL) 1300 fatal("%s: BN_new", __func__); 1301 buffer_get_bignum2(m, client_n); 1302 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key); 1303 BN_clear_free(client_n); 1304 } 1305 buffer_clear(m); 1306 buffer_put_int(m, allowed); 1307 buffer_put_int(m, forced_command != NULL); 1308 1309 /* clear temporarily storage (used by generate challenge) */ 1310 monitor_reset_key_state(); 1311 1312 if (allowed && key != NULL) { 1313 key->type = KEY_RSA; /* cheat for key_to_blob */ 1314 if (key_to_blob(key, &blob, &blen) == 0) 1315 fatal("%s: key_to_blob failed", __func__); 1316 buffer_put_string(m, blob, blen); 1317 1318 /* Save temporarily for comparison in verify */ 1319 key_blob = blob; 1320 key_bloblen = blen; 1321 key_blobtype = MM_RSAUSERKEY; 1322 } 1323 if (key != NULL) 1324 key_free(key); 1325 1326 mm_append_debug(m); 1327 1328 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m); 1329 1330 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed); 1331 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0); 1332 return (0); 1333 } 1334 1335 int 1336 mm_answer_rsa_challenge(int socket, Buffer *m) 1337 { 1338 Key *key = NULL; 1339 u_char *blob; 1340 u_int blen; 1341 1342 debug3("%s entering", __func__); 1343 1344 if (!authctxt->valid) 1345 fatal("%s: authctxt not valid", __func__); 1346 blob = buffer_get_string(m, &blen); 1347 if (!monitor_allowed_key(blob, blen)) 1348 fatal("%s: bad key, not previously allowed", __func__); 1349 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY) 1350 fatal("%s: key type mismatch", __func__); 1351 if ((key = key_from_blob(blob, blen)) == NULL) 1352 fatal("%s: received bad key", __func__); 1353 1354 if (ssh1_challenge) 1355 BN_clear_free(ssh1_challenge); 1356 ssh1_challenge = auth_rsa_generate_challenge(key); 1357 1358 buffer_clear(m); 1359 buffer_put_bignum2(m, ssh1_challenge); 1360 1361 debug3("%s sending reply", __func__); 1362 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m); 1363 1364 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1); 1365 1366 xfree(blob); 1367 key_free(key); 1368 return (0); 1369 } 1370 1371 int 1372 mm_answer_rsa_response(int socket, Buffer *m) 1373 { 1374 Key *key = NULL; 1375 u_char *blob, *response; 1376 u_int blen, len; 1377 int success; 1378 1379 debug3("%s entering", __func__); 1380 1381 if (!authctxt->valid) 1382 fatal("%s: authctxt not valid", __func__); 1383 if (ssh1_challenge == NULL) 1384 fatal("%s: no ssh1_challenge", __func__); 1385 1386 blob = buffer_get_string(m, &blen); 1387 if (!monitor_allowed_key(blob, blen)) 1388 fatal("%s: bad key, not previously allowed", __func__); 1389 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY) 1390 fatal("%s: key type mismatch: %d", __func__, key_blobtype); 1391 if ((key = key_from_blob(blob, blen)) == NULL) 1392 fatal("%s: received bad key", __func__); 1393 response = buffer_get_string(m, &len); 1394 if (len != 16) 1395 fatal("%s: received bad response to challenge", __func__); 1396 success = auth_rsa_verify_response(key, ssh1_challenge, response); 1397 1398 xfree(blob); 1399 key_free(key); 1400 xfree(response); 1401 1402 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa"; 1403 1404 /* reset state */ 1405 BN_clear_free(ssh1_challenge); 1406 ssh1_challenge = NULL; 1407 monitor_reset_key_state(); 1408 1409 buffer_clear(m); 1410 buffer_put_int(m, success); 1411 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m); 1412 1413 return (success); 1414 } 1415 1416 #ifdef KRB4 1417 int 1418 mm_answer_krb4(int socket, Buffer *m) 1419 { 1420 KTEXT_ST auth, reply; 1421 char *client, *p; 1422 int success; 1423 u_int alen; 1424 1425 reply.length = auth.length = 0; 1426 1427 p = buffer_get_string(m, &alen); 1428 if (alen >= MAX_KTXT_LEN) 1429 fatal("%s: auth too large", __func__); 1430 memcpy(auth.dat, p, alen); 1431 auth.length = alen; 1432 memset(p, 0, alen); 1433 xfree(p); 1434 1435 success = options.kerberos_authentication && 1436 authctxt->valid && 1437 auth_krb4(authctxt, &auth, &client, &reply); 1438 1439 memset(auth.dat, 0, alen); 1440 buffer_clear(m); 1441 buffer_put_int(m, success); 1442 1443 if (success) { 1444 buffer_put_cstring(m, client); 1445 buffer_put_string(m, reply.dat, reply.length); 1446 if (client) 1447 xfree(client); 1448 if (reply.length) 1449 memset(reply.dat, 0, reply.length); 1450 } 1451 1452 debug3("%s: sending result %d", __func__, success); 1453 mm_request_send(socket, MONITOR_ANS_KRB4, m); 1454 1455 auth_method = "kerberos"; 1456 1457 /* Causes monitor loop to terminate if authenticated */ 1458 return (success); 1459 } 1460 #endif 1461 1462 #ifdef KRB5 1463 int 1464 mm_answer_krb5(int socket, Buffer *m) 1465 { 1466 krb5_data tkt, reply; 1467 char *client_user; 1468 u_int len; 1469 int success; 1470 1471 /* use temporary var to avoid size issues on 64bit arch */ 1472 tkt.data = buffer_get_string(m, &len); 1473 tkt.length = len; 1474 1475 success = options.kerberos_authentication && 1476 authctxt->valid && 1477 auth_krb5(authctxt, &tkt, &client_user, &reply); 1478 1479 if (tkt.length) 1480 xfree(tkt.data); 1481 1482 buffer_clear(m); 1483 buffer_put_int(m, success); 1484 1485 if (success) { 1486 buffer_put_cstring(m, client_user); 1487 buffer_put_string(m, reply.data, reply.length); 1488 if (client_user) 1489 xfree(client_user); 1490 if (reply.length) 1491 xfree(reply.data); 1492 } 1493 mm_request_send(socket, MONITOR_ANS_KRB5, m); 1494 1495 return success; 1496 } 1497 #endif 1498 1499 int 1500 mm_answer_term(int socket, Buffer *req) 1501 { 1502 extern struct monitor *pmonitor; 1503 int res, status; 1504 1505 debug3("%s: tearing down sessions", __func__); 1506 1507 /* The child is terminating */ 1508 session_destroy_all(&mm_session_close); 1509 1510 while (waitpid(pmonitor->m_pid, &status, 0) == -1) 1511 if (errno != EINTR) 1512 exit(1); 1513 1514 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1; 1515 1516 /* Terminate process */ 1517 exit (res); 1518 } 1519 1520 void 1521 monitor_apply_keystate(struct monitor *pmonitor) 1522 { 1523 if (compat20) { 1524 set_newkeys(MODE_IN); 1525 set_newkeys(MODE_OUT); 1526 } else { 1527 packet_set_protocol_flags(child_state.ssh1protoflags); 1528 packet_set_encryption_key(child_state.ssh1key, 1529 child_state.ssh1keylen, child_state.ssh1cipher); 1530 xfree(child_state.ssh1key); 1531 } 1532 1533 /* for rc4 and other stateful ciphers */ 1534 packet_set_keycontext(MODE_OUT, child_state.keyout); 1535 xfree(child_state.keyout); 1536 packet_set_keycontext(MODE_IN, child_state.keyin); 1537 xfree(child_state.keyin); 1538 1539 if (!compat20) { 1540 packet_set_iv(MODE_OUT, child_state.ivout); 1541 xfree(child_state.ivout); 1542 packet_set_iv(MODE_IN, child_state.ivin); 1543 xfree(child_state.ivin); 1544 } 1545 1546 memcpy(&incoming_stream, &child_state.incoming, 1547 sizeof(incoming_stream)); 1548 memcpy(&outgoing_stream, &child_state.outgoing, 1549 sizeof(outgoing_stream)); 1550 1551 /* Update with new address */ 1552 if (options.compression) 1553 mm_init_compression(pmonitor->m_zlib); 1554 1555 /* Network I/O buffers */ 1556 /* XXX inefficient for large buffers, need: buffer_init_from_string */ 1557 buffer_clear(&input); 1558 buffer_append(&input, child_state.input, child_state.ilen); 1559 memset(child_state.input, 0, child_state.ilen); 1560 xfree(child_state.input); 1561 1562 buffer_clear(&output); 1563 buffer_append(&output, child_state.output, child_state.olen); 1564 memset(child_state.output, 0, child_state.olen); 1565 xfree(child_state.output); 1566 } 1567 1568 static Kex * 1569 mm_get_kex(Buffer *m) 1570 { 1571 Kex *kex; 1572 void *blob; 1573 u_int bloblen; 1574 1575 kex = xmalloc(sizeof(*kex)); 1576 memset(kex, 0, sizeof(*kex)); 1577 kex->session_id = buffer_get_string(m, &kex->session_id_len); 1578 if ((session_id2 == NULL) || 1579 (kex->session_id_len != session_id2_len) || 1580 (memcmp(kex->session_id, session_id2, session_id2_len) != 0)) 1581 fatal("mm_get_get: internal error: bad session id"); 1582 kex->we_need = buffer_get_int(m); 1583 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server; 1584 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server; 1585 kex->server = 1; 1586 kex->hostkey_type = buffer_get_int(m); 1587 kex->kex_type = buffer_get_int(m); 1588 blob = buffer_get_string(m, &bloblen); 1589 buffer_init(&kex->my); 1590 buffer_append(&kex->my, blob, bloblen); 1591 xfree(blob); 1592 blob = buffer_get_string(m, &bloblen); 1593 buffer_init(&kex->peer); 1594 buffer_append(&kex->peer, blob, bloblen); 1595 xfree(blob); 1596 kex->done = 1; 1597 kex->flags = buffer_get_int(m); 1598 kex->client_version_string = buffer_get_string(m, NULL); 1599 kex->server_version_string = buffer_get_string(m, NULL); 1600 kex->load_host_key=&get_hostkey_by_type; 1601 kex->host_key_index=&get_hostkey_index; 1602 1603 return (kex); 1604 } 1605 1606 /* This function requries careful sanity checking */ 1607 1608 void 1609 mm_get_keystate(struct monitor *pmonitor) 1610 { 1611 Buffer m; 1612 u_char *blob, *p; 1613 u_int bloblen, plen; 1614 1615 debug3("%s: Waiting for new keys", __func__); 1616 1617 buffer_init(&m); 1618 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m); 1619 if (!compat20) { 1620 child_state.ssh1protoflags = buffer_get_int(&m); 1621 child_state.ssh1cipher = buffer_get_int(&m); 1622 child_state.ssh1key = buffer_get_string(&m, 1623 &child_state.ssh1keylen); 1624 child_state.ivout = buffer_get_string(&m, 1625 &child_state.ivoutlen); 1626 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen); 1627 goto skip; 1628 } else { 1629 /* Get the Kex for rekeying */ 1630 *pmonitor->m_pkex = mm_get_kex(&m); 1631 } 1632 1633 blob = buffer_get_string(&m, &bloblen); 1634 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen); 1635 xfree(blob); 1636 1637 debug3("%s: Waiting for second key", __func__); 1638 blob = buffer_get_string(&m, &bloblen); 1639 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen); 1640 xfree(blob); 1641 1642 /* Now get sequence numbers for the packets */ 1643 packet_set_seqnr(MODE_OUT, buffer_get_int(&m)); 1644 packet_set_seqnr(MODE_IN, buffer_get_int(&m)); 1645 1646 skip: 1647 /* Get the key context */ 1648 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen); 1649 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen); 1650 1651 debug3("%s: Getting compression state", __func__); 1652 /* Get compression state */ 1653 p = buffer_get_string(&m, &plen); 1654 if (plen != sizeof(child_state.outgoing)) 1655 fatal("%s: bad request size", __func__); 1656 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing)); 1657 xfree(p); 1658 1659 p = buffer_get_string(&m, &plen); 1660 if (plen != sizeof(child_state.incoming)) 1661 fatal("%s: bad request size", __func__); 1662 memcpy(&child_state.incoming, p, sizeof(child_state.incoming)); 1663 xfree(p); 1664 1665 /* Network I/O buffers */ 1666 debug3("%s: Getting Network I/O buffers", __func__); 1667 child_state.input = buffer_get_string(&m, &child_state.ilen); 1668 child_state.output = buffer_get_string(&m, &child_state.olen); 1669 1670 buffer_free(&m); 1671 } 1672 1673 1674 /* Allocation functions for zlib */ 1675 void * 1676 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size) 1677 { 1678 size_t len = (size_t) size * ncount; 1679 void *address; 1680 1681 if (len == 0 || ncount > SIZE_T_MAX / size) 1682 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size); 1683 1684 address = mm_malloc(mm, len); 1685 1686 return (address); 1687 } 1688 1689 void 1690 mm_zfree(struct mm_master *mm, void *address) 1691 { 1692 mm_free(mm, address); 1693 } 1694 1695 void 1696 mm_init_compression(struct mm_master *mm) 1697 { 1698 outgoing_stream.zalloc = (alloc_func)mm_zalloc; 1699 outgoing_stream.zfree = (free_func)mm_zfree; 1700 outgoing_stream.opaque = mm; 1701 1702 incoming_stream.zalloc = (alloc_func)mm_zalloc; 1703 incoming_stream.zfree = (free_func)mm_zfree; 1704 incoming_stream.opaque = mm; 1705 } 1706 1707 /* XXX */ 1708 1709 #define FD_CLOSEONEXEC(x) do { \ 1710 if (fcntl(x, F_SETFD, 1) == -1) \ 1711 fatal("fcntl(%d, F_SETFD)", x); \ 1712 } while (0) 1713 1714 static void 1715 monitor_socketpair(int *pair) 1716 { 1717 #ifdef HAVE_SOCKETPAIR 1718 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) 1719 fatal("%s: socketpair", __func__); 1720 #else 1721 fatal("%s: UsePrivilegeSeparation=yes not supported", 1722 __func__); 1723 #endif 1724 FD_CLOSEONEXEC(pair[0]); 1725 FD_CLOSEONEXEC(pair[1]); 1726 } 1727 1728 #define MM_MEMSIZE 65536 1729 1730 struct monitor * 1731 monitor_init(void) 1732 { 1733 struct monitor *mon; 1734 int pair[2]; 1735 1736 mon = xmalloc(sizeof(*mon)); 1737 1738 monitor_socketpair(pair); 1739 1740 mon->m_recvfd = pair[0]; 1741 mon->m_sendfd = pair[1]; 1742 1743 /* Used to share zlib space across processes */ 1744 if (options.compression) { 1745 mon->m_zback = mm_create(NULL, MM_MEMSIZE); 1746 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE); 1747 1748 /* Compression needs to share state across borders */ 1749 mm_init_compression(mon->m_zlib); 1750 } 1751 1752 return mon; 1753 } 1754 1755 void 1756 monitor_reinit(struct monitor *mon) 1757 { 1758 int pair[2]; 1759 1760 monitor_socketpair(pair); 1761 1762 mon->m_recvfd = pair[0]; 1763 mon->m_sendfd = pair[1]; 1764 } 1765