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