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