1 /* $OpenBSD: monitor.c,v 1.180 2018/03/03 03:15:51 djm 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/queue.h> 33 34 #ifdef WITH_OPENSSL 35 #include <openssl/dh.h> 36 #endif 37 38 #include <errno.h> 39 #include <fcntl.h> 40 #include <limits.h> 41 #include <paths.h> 42 #include <poll.h> 43 #include <pwd.h> 44 #include <signal.h> 45 #include <stdarg.h> 46 #include <stdint.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 51 #include "atomicio.h" 52 #include "xmalloc.h" 53 #include "ssh.h" 54 #include "key.h" 55 #include "buffer.h" 56 #include "hostfile.h" 57 #include "auth.h" 58 #include "cipher.h" 59 #include "kex.h" 60 #include "dh.h" 61 #include <zlib.h> 62 #include "packet.h" 63 #include "auth-options.h" 64 #include "sshpty.h" 65 #include "channels.h" 66 #include "session.h" 67 #include "sshlogin.h" 68 #include "canohost.h" 69 #include "log.h" 70 #include "misc.h" 71 #include "servconf.h" 72 #include "monitor.h" 73 #ifdef GSSAPI 74 #include "ssh-gss.h" 75 #endif 76 #include "monitor_wrap.h" 77 #include "monitor_fdpass.h" 78 #include "compat.h" 79 #include "ssh2.h" 80 #include "authfd.h" 81 #include "match.h" 82 #include "ssherr.h" 83 84 #ifdef GSSAPI 85 static Gssctxt *gsscontext = NULL; 86 #endif 87 88 /* Imports */ 89 extern ServerOptions options; 90 extern u_int utmp_len; 91 extern u_char session_id[]; 92 extern Buffer auth_debug; 93 extern int auth_debug_init; 94 extern Buffer loginmsg; 95 extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */ 96 97 /* State exported from the child */ 98 static struct sshbuf *child_state; 99 100 /* Functions on the monitor that answer unprivileged requests */ 101 102 int mm_answer_moduli(int, Buffer *); 103 int mm_answer_sign(int, Buffer *); 104 int mm_answer_pwnamallow(int, Buffer *); 105 int mm_answer_auth2_read_banner(int, Buffer *); 106 int mm_answer_authserv(int, Buffer *); 107 int mm_answer_authpassword(int, Buffer *); 108 int mm_answer_bsdauthquery(int, Buffer *); 109 int mm_answer_bsdauthrespond(int, Buffer *); 110 int mm_answer_skeyquery(int, Buffer *); 111 int mm_answer_skeyrespond(int, Buffer *); 112 int mm_answer_keyallowed(int, Buffer *); 113 int mm_answer_keyverify(int, Buffer *); 114 int mm_answer_pty(int, Buffer *); 115 int mm_answer_pty_cleanup(int, Buffer *); 116 int mm_answer_term(int, Buffer *); 117 int mm_answer_rsa_keyallowed(int, Buffer *); 118 int mm_answer_rsa_challenge(int, Buffer *); 119 int mm_answer_rsa_response(int, Buffer *); 120 int mm_answer_sesskey(int, Buffer *); 121 int mm_answer_sessid(int, Buffer *); 122 123 #ifdef GSSAPI 124 int mm_answer_gss_setup_ctx(int, Buffer *); 125 int mm_answer_gss_accept_ctx(int, Buffer *); 126 int mm_answer_gss_userok(int, Buffer *); 127 int mm_answer_gss_checkmic(int, Buffer *); 128 #endif 129 130 static int monitor_read_log(struct monitor *); 131 132 static Authctxt *authctxt; 133 134 /* local state for key verify */ 135 static u_char *key_blob = NULL; 136 static u_int key_bloblen = 0; 137 static int key_blobtype = MM_NOKEY; 138 static struct sshauthopt *key_opts = NULL; 139 static char *hostbased_cuser = NULL; 140 static char *hostbased_chost = NULL; 141 static char *auth_method = "unknown"; 142 static char *auth_submethod = NULL; 143 static u_int session_id2_len = 0; 144 static u_char *session_id2 = NULL; 145 static pid_t monitor_child_pid; 146 147 struct mon_table { 148 enum monitor_reqtype type; 149 int flags; 150 int (*f)(int, Buffer *); 151 }; 152 153 #define MON_ISAUTH 0x0004 /* Required for Authentication */ 154 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */ 155 #define MON_ONCE 0x0010 /* Disable after calling */ 156 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */ 157 158 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE) 159 160 #define MON_PERMIT 0x1000 /* Request is permitted */ 161 162 struct mon_table mon_dispatch_proto20[] = { 163 #ifdef WITH_OPENSSL 164 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli}, 165 #endif 166 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign}, 167 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow}, 168 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv}, 169 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner}, 170 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword}, 171 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery}, 172 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond}, 173 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed}, 174 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify}, 175 #ifdef GSSAPI 176 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx}, 177 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx}, 178 {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok}, 179 {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic}, 180 #endif 181 {0, 0, NULL} 182 }; 183 184 struct mon_table mon_dispatch_postauth20[] = { 185 #ifdef WITH_OPENSSL 186 {MONITOR_REQ_MODULI, 0, mm_answer_moduli}, 187 #endif 188 {MONITOR_REQ_SIGN, 0, mm_answer_sign}, 189 {MONITOR_REQ_PTY, 0, mm_answer_pty}, 190 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup}, 191 {MONITOR_REQ_TERM, 0, mm_answer_term}, 192 {0, 0, NULL} 193 }; 194 195 struct mon_table *mon_dispatch; 196 197 /* Specifies if a certain message is allowed at the moment */ 198 static void 199 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit) 200 { 201 while (ent->f != NULL) { 202 if (ent->type == type) { 203 ent->flags &= ~MON_PERMIT; 204 ent->flags |= permit ? MON_PERMIT : 0; 205 return; 206 } 207 ent++; 208 } 209 } 210 211 static void 212 monitor_permit_authentications(int permit) 213 { 214 struct mon_table *ent = mon_dispatch; 215 216 while (ent->f != NULL) { 217 if (ent->flags & MON_AUTH) { 218 ent->flags &= ~MON_PERMIT; 219 ent->flags |= permit ? MON_PERMIT : 0; 220 } 221 ent++; 222 } 223 } 224 225 void 226 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor) 227 { 228 struct ssh *ssh = active_state; /* XXX */ 229 struct mon_table *ent; 230 int authenticated = 0, partial = 0; 231 232 debug3("preauth child monitor started"); 233 234 if (pmonitor->m_recvfd >= 0) 235 close(pmonitor->m_recvfd); 236 if (pmonitor->m_log_sendfd >= 0) 237 close(pmonitor->m_log_sendfd); 238 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1; 239 240 authctxt = _authctxt; 241 memset(authctxt, 0, sizeof(*authctxt)); 242 ssh->authctxt = authctxt; 243 244 mon_dispatch = mon_dispatch_proto20; 245 /* Permit requests for moduli and signatures */ 246 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1); 247 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1); 248 249 /* The first few requests do not require asynchronous access */ 250 while (!authenticated) { 251 partial = 0; 252 auth_method = "unknown"; 253 auth_submethod = NULL; 254 auth2_authctxt_reset_info(authctxt); 255 256 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1); 257 258 /* Special handling for multiple required authentications */ 259 if (options.num_auth_methods != 0) { 260 if (authenticated && 261 !auth2_update_methods_lists(authctxt, 262 auth_method, auth_submethod)) { 263 debug3("%s: method %s: partial", __func__, 264 auth_method); 265 authenticated = 0; 266 partial = 1; 267 } 268 } 269 270 if (authenticated) { 271 if (!(ent->flags & MON_AUTHDECIDE)) 272 fatal("%s: unexpected authentication from %d", 273 __func__, ent->type); 274 if (authctxt->pw->pw_uid == 0 && 275 !auth_root_allowed(ssh, auth_method)) 276 authenticated = 0; 277 } 278 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) { 279 auth_log(authctxt, authenticated, partial, 280 auth_method, auth_submethod); 281 if (!partial && !authenticated) 282 authctxt->failures++; 283 if (authenticated || partial) { 284 auth2_update_session_info(authctxt, 285 auth_method, auth_submethod); 286 } 287 } 288 } 289 290 if (!authctxt->valid) 291 fatal("%s: authenticated invalid user", __func__); 292 if (strcmp(auth_method, "unknown") == 0) 293 fatal("%s: authentication method name unknown", __func__); 294 295 debug("%s: %s has been authenticated by privileged process", 296 __func__, authctxt->user); 297 ssh->authctxt = NULL; 298 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user); 299 300 mm_get_keystate(pmonitor); 301 302 /* Drain any buffered messages from the child */ 303 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0) 304 ; 305 306 if (pmonitor->m_recvfd >= 0) 307 close(pmonitor->m_recvfd); 308 if (pmonitor->m_log_sendfd >= 0) 309 close(pmonitor->m_log_sendfd); 310 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1; 311 } 312 313 static void 314 monitor_set_child_handler(pid_t pid) 315 { 316 monitor_child_pid = pid; 317 } 318 319 static void 320 monitor_child_handler(int sig) 321 { 322 kill(monitor_child_pid, sig); 323 } 324 325 void 326 monitor_child_postauth(struct monitor *pmonitor) 327 { 328 close(pmonitor->m_recvfd); 329 pmonitor->m_recvfd = -1; 330 331 monitor_set_child_handler(pmonitor->m_pid); 332 signal(SIGHUP, &monitor_child_handler); 333 signal(SIGTERM, &monitor_child_handler); 334 signal(SIGINT, &monitor_child_handler); 335 336 mon_dispatch = mon_dispatch_postauth20; 337 338 /* Permit requests for moduli and signatures */ 339 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1); 340 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1); 341 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1); 342 343 if (auth_opts->permit_pty_flag) { 344 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1); 345 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1); 346 } 347 348 for (;;) 349 monitor_read(pmonitor, mon_dispatch, NULL); 350 } 351 352 static int 353 monitor_read_log(struct monitor *pmonitor) 354 { 355 Buffer logmsg; 356 u_int len, level; 357 char *msg; 358 359 buffer_init(&logmsg); 360 361 /* Read length */ 362 buffer_append_space(&logmsg, 4); 363 if (atomicio(read, pmonitor->m_log_recvfd, 364 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) { 365 if (errno == EPIPE) { 366 buffer_free(&logmsg); 367 debug("%s: child log fd closed", __func__); 368 close(pmonitor->m_log_recvfd); 369 pmonitor->m_log_recvfd = -1; 370 return -1; 371 } 372 fatal("%s: log fd read: %s", __func__, strerror(errno)); 373 } 374 len = buffer_get_int(&logmsg); 375 if (len <= 4 || len > 8192) 376 fatal("%s: invalid log message length %u", __func__, len); 377 378 /* Read severity, message */ 379 buffer_clear(&logmsg); 380 buffer_append_space(&logmsg, len); 381 if (atomicio(read, pmonitor->m_log_recvfd, 382 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) 383 fatal("%s: log fd read: %s", __func__, strerror(errno)); 384 385 /* Log it */ 386 level = buffer_get_int(&logmsg); 387 msg = buffer_get_string(&logmsg, NULL); 388 if (log_level_name(level) == NULL) 389 fatal("%s: invalid log level %u (corrupted message?)", 390 __func__, level); 391 do_log2(level, "%s [preauth]", msg); 392 393 buffer_free(&logmsg); 394 free(msg); 395 396 return 0; 397 } 398 399 int 400 monitor_read(struct monitor *pmonitor, struct mon_table *ent, 401 struct mon_table **pent) 402 { 403 Buffer m; 404 int ret; 405 u_char type; 406 struct pollfd pfd[2]; 407 408 for (;;) { 409 memset(&pfd, 0, sizeof(pfd)); 410 pfd[0].fd = pmonitor->m_sendfd; 411 pfd[0].events = POLLIN; 412 pfd[1].fd = pmonitor->m_log_recvfd; 413 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN; 414 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) { 415 if (errno == EINTR || errno == EAGAIN) 416 continue; 417 fatal("%s: poll: %s", __func__, strerror(errno)); 418 } 419 if (pfd[1].revents) { 420 /* 421 * Drain all log messages before processing next 422 * monitor request. 423 */ 424 monitor_read_log(pmonitor); 425 continue; 426 } 427 if (pfd[0].revents) 428 break; /* Continues below */ 429 } 430 431 buffer_init(&m); 432 433 mm_request_receive(pmonitor->m_sendfd, &m); 434 type = buffer_get_char(&m); 435 436 debug3("%s: checking request %d", __func__, type); 437 438 while (ent->f != NULL) { 439 if (ent->type == type) 440 break; 441 ent++; 442 } 443 444 if (ent->f != NULL) { 445 if (!(ent->flags & MON_PERMIT)) 446 fatal("%s: unpermitted request %d", __func__, 447 type); 448 ret = (*ent->f)(pmonitor->m_sendfd, &m); 449 buffer_free(&m); 450 451 /* The child may use this request only once, disable it */ 452 if (ent->flags & MON_ONCE) { 453 debug2("%s: %d used once, disabling now", __func__, 454 type); 455 ent->flags &= ~MON_PERMIT; 456 } 457 458 if (pent != NULL) 459 *pent = ent; 460 461 return ret; 462 } 463 464 fatal("%s: unsupported request: %d", __func__, type); 465 466 /* NOTREACHED */ 467 return (-1); 468 } 469 470 /* allowed key state */ 471 static int 472 monitor_allowed_key(u_char *blob, u_int bloblen) 473 { 474 /* make sure key is allowed */ 475 if (key_blob == NULL || key_bloblen != bloblen || 476 timingsafe_bcmp(key_blob, blob, key_bloblen)) 477 return (0); 478 return (1); 479 } 480 481 static void 482 monitor_reset_key_state(void) 483 { 484 /* reset state */ 485 free(key_blob); 486 free(hostbased_cuser); 487 free(hostbased_chost); 488 sshauthopt_free(key_opts); 489 key_blob = NULL; 490 key_bloblen = 0; 491 key_blobtype = MM_NOKEY; 492 key_opts = NULL; 493 hostbased_cuser = NULL; 494 hostbased_chost = NULL; 495 } 496 497 #ifdef WITH_OPENSSL 498 int 499 mm_answer_moduli(int sock, Buffer *m) 500 { 501 DH *dh; 502 int min, want, max; 503 504 min = buffer_get_int(m); 505 want = buffer_get_int(m); 506 max = buffer_get_int(m); 507 508 debug3("%s: got parameters: %d %d %d", 509 __func__, min, want, max); 510 /* We need to check here, too, in case the child got corrupted */ 511 if (max < min || want < min || max < want) 512 fatal("%s: bad parameters: %d %d %d", 513 __func__, min, want, max); 514 515 buffer_clear(m); 516 517 dh = choose_dh(min, want, max); 518 if (dh == NULL) { 519 buffer_put_char(m, 0); 520 return (0); 521 } else { 522 /* Send first bignum */ 523 buffer_put_char(m, 1); 524 buffer_put_bignum2(m, dh->p); 525 buffer_put_bignum2(m, dh->g); 526 527 DH_free(dh); 528 } 529 mm_request_send(sock, MONITOR_ANS_MODULI, m); 530 return (0); 531 } 532 #endif 533 534 int 535 mm_answer_sign(int sock, Buffer *m) 536 { 537 struct ssh *ssh = active_state; /* XXX */ 538 extern int auth_sock; /* XXX move to state struct? */ 539 struct sshkey *key; 540 struct sshbuf *sigbuf = NULL; 541 u_char *p = NULL, *signature = NULL; 542 char *alg = NULL; 543 size_t datlen, siglen, alglen; 544 int r, is_proof = 0; 545 u_int keyid; 546 const char proof_req[] = "hostkeys-prove-00@openssh.com"; 547 548 debug3("%s", __func__); 549 550 if ((r = sshbuf_get_u32(m, &keyid)) != 0 || 551 (r = sshbuf_get_string(m, &p, &datlen)) != 0 || 552 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0) 553 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 554 if (keyid > INT_MAX) 555 fatal("%s: invalid key ID", __func__); 556 557 /* 558 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes), 559 * SHA384 (48 bytes) and SHA512 (64 bytes). 560 * 561 * Otherwise, verify the signature request is for a hostkey 562 * proof. 563 * 564 * XXX perform similar check for KEX signature requests too? 565 * it's not trivial, since what is signed is the hash, rather 566 * than the full kex structure... 567 */ 568 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) { 569 /* 570 * Construct expected hostkey proof and compare it to what 571 * the client sent us. 572 */ 573 if (session_id2_len == 0) /* hostkeys is never first */ 574 fatal("%s: bad data length: %zu", __func__, datlen); 575 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL) 576 fatal("%s: no hostkey for index %d", __func__, keyid); 577 if ((sigbuf = sshbuf_new()) == NULL) 578 fatal("%s: sshbuf_new", __func__); 579 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 || 580 (r = sshbuf_put_string(sigbuf, session_id2, 581 session_id2_len)) != 0 || 582 (r = sshkey_puts(key, sigbuf)) != 0) 583 fatal("%s: couldn't prepare private key " 584 "proof buffer: %s", __func__, ssh_err(r)); 585 if (datlen != sshbuf_len(sigbuf) || 586 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0) 587 fatal("%s: bad data length: %zu, hostkey proof len %zu", 588 __func__, datlen, sshbuf_len(sigbuf)); 589 sshbuf_free(sigbuf); 590 is_proof = 1; 591 } 592 593 /* save session id, it will be passed on the first call */ 594 if (session_id2_len == 0) { 595 session_id2_len = datlen; 596 session_id2 = xmalloc(session_id2_len); 597 memcpy(session_id2, p, session_id2_len); 598 } 599 600 if ((key = get_hostkey_by_index(keyid)) != NULL) { 601 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg, 602 datafellows)) != 0) 603 fatal("%s: sshkey_sign failed: %s", 604 __func__, ssh_err(r)); 605 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL && 606 auth_sock > 0) { 607 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen, 608 p, datlen, alg, datafellows)) != 0) { 609 fatal("%s: ssh_agent_sign failed: %s", 610 __func__, ssh_err(r)); 611 } 612 } else 613 fatal("%s: no hostkey from index %d", __func__, keyid); 614 615 debug3("%s: %s signature %p(%zu)", __func__, 616 is_proof ? "KEX" : "hostkey proof", signature, siglen); 617 618 sshbuf_reset(m); 619 if ((r = sshbuf_put_string(m, signature, siglen)) != 0) 620 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 621 622 free(alg); 623 free(p); 624 free(signature); 625 626 mm_request_send(sock, MONITOR_ANS_SIGN, m); 627 628 /* Turn on permissions for getpwnam */ 629 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1); 630 631 return (0); 632 } 633 634 /* Retrieves the password entry and also checks if the user is permitted */ 635 636 int 637 mm_answer_pwnamallow(int sock, Buffer *m) 638 { 639 struct ssh *ssh = active_state; /* XXX */ 640 char *username; 641 struct passwd *pwent; 642 int allowed = 0; 643 u_int i; 644 645 debug3("%s", __func__); 646 647 if (authctxt->attempt++ != 0) 648 fatal("%s: multiple attempts for getpwnam", __func__); 649 650 username = buffer_get_string(m, NULL); 651 652 pwent = getpwnamallow(username); 653 654 authctxt->user = xstrdup(username); 655 setproctitle("%s [priv]", pwent ? username : "unknown"); 656 free(username); 657 658 buffer_clear(m); 659 660 if (pwent == NULL) { 661 buffer_put_char(m, 0); 662 authctxt->pw = fakepw(); 663 goto out; 664 } 665 666 allowed = 1; 667 authctxt->pw = pwent; 668 authctxt->valid = 1; 669 670 buffer_put_char(m, 1); 671 buffer_put_string(m, pwent, sizeof(struct passwd)); 672 buffer_put_cstring(m, pwent->pw_name); 673 buffer_put_cstring(m, "*"); 674 buffer_put_cstring(m, pwent->pw_gecos); 675 buffer_put_cstring(m, pwent->pw_class); 676 buffer_put_cstring(m, pwent->pw_dir); 677 buffer_put_cstring(m, pwent->pw_shell); 678 679 out: 680 ssh_packet_set_log_preamble(ssh, "%suser %s", 681 authctxt->valid ? "authenticating" : "invalid ", authctxt->user); 682 buffer_put_string(m, &options, sizeof(options)); 683 684 #define M_CP_STROPT(x) do { \ 685 if (options.x != NULL) \ 686 buffer_put_cstring(m, options.x); \ 687 } while (0) 688 #define M_CP_STRARRAYOPT(x, nx) do { \ 689 for (i = 0; i < options.nx; i++) \ 690 buffer_put_cstring(m, options.x[i]); \ 691 } while (0) 692 /* See comment in servconf.h */ 693 COPY_MATCH_STRING_OPTS(); 694 #undef M_CP_STROPT 695 #undef M_CP_STRARRAYOPT 696 697 /* Create valid auth method lists */ 698 if (auth2_setup_methods_lists(authctxt) != 0) { 699 /* 700 * The monitor will continue long enough to let the child 701 * run to it's packet_disconnect(), but it must not allow any 702 * authentication to succeed. 703 */ 704 debug("%s: no valid authentication method lists", __func__); 705 } 706 707 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed); 708 mm_request_send(sock, MONITOR_ANS_PWNAM, m); 709 710 /* Allow service/style information on the auth context */ 711 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1); 712 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1); 713 714 return (0); 715 } 716 717 int mm_answer_auth2_read_banner(int sock, Buffer *m) 718 { 719 char *banner; 720 721 buffer_clear(m); 722 banner = auth2_read_banner(); 723 buffer_put_cstring(m, banner != NULL ? banner : ""); 724 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m); 725 free(banner); 726 727 return (0); 728 } 729 730 int 731 mm_answer_authserv(int sock, Buffer *m) 732 { 733 monitor_permit_authentications(1); 734 735 authctxt->service = buffer_get_string(m, NULL); 736 authctxt->style = buffer_get_string(m, NULL); 737 debug3("%s: service=%s, style=%s", 738 __func__, authctxt->service, authctxt->style); 739 740 if (strlen(authctxt->style) == 0) { 741 free(authctxt->style); 742 authctxt->style = NULL; 743 } 744 745 return (0); 746 } 747 748 int 749 mm_answer_authpassword(int sock, Buffer *m) 750 { 751 struct ssh *ssh = active_state; /* XXX */ 752 static int call_count; 753 char *passwd; 754 int authenticated; 755 u_int plen; 756 757 if (!options.password_authentication) 758 fatal("%s: password authentication not enabled", __func__); 759 passwd = buffer_get_string(m, &plen); 760 /* Only authenticate if the context is valid */ 761 authenticated = options.password_authentication && 762 auth_password(ssh, passwd); 763 explicit_bzero(passwd, strlen(passwd)); 764 free(passwd); 765 766 buffer_clear(m); 767 buffer_put_int(m, authenticated); 768 769 debug3("%s: sending result %d", __func__, authenticated); 770 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m); 771 772 call_count++; 773 if (plen == 0 && call_count == 1) 774 auth_method = "none"; 775 else 776 auth_method = "password"; 777 778 /* Causes monitor loop to terminate if authenticated */ 779 return (authenticated); 780 } 781 782 int 783 mm_answer_bsdauthquery(int sock, Buffer *m) 784 { 785 char *name, *infotxt; 786 u_int numprompts; 787 u_int *echo_on; 788 char **prompts; 789 u_int success; 790 791 if (!options.kbd_interactive_authentication) 792 fatal("%s: kbd-int authentication not enabled", __func__); 793 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts, 794 &prompts, &echo_on) < 0 ? 0 : 1; 795 796 buffer_clear(m); 797 buffer_put_int(m, success); 798 if (success) 799 buffer_put_cstring(m, prompts[0]); 800 801 debug3("%s: sending challenge success: %u", __func__, success); 802 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m); 803 804 if (success) { 805 free(name); 806 free(infotxt); 807 free(prompts); 808 free(echo_on); 809 } 810 811 return (0); 812 } 813 814 int 815 mm_answer_bsdauthrespond(int sock, Buffer *m) 816 { 817 char *response; 818 int authok; 819 820 if (!options.kbd_interactive_authentication) 821 fatal("%s: kbd-int authentication not enabled", __func__); 822 if (authctxt->as == NULL) 823 fatal("%s: no bsd auth session", __func__); 824 825 response = buffer_get_string(m, NULL); 826 authok = options.challenge_response_authentication && 827 auth_userresponse(authctxt->as, response, 0); 828 authctxt->as = NULL; 829 debug3("%s: <%s> = <%d>", __func__, response, authok); 830 free(response); 831 832 buffer_clear(m); 833 buffer_put_int(m, authok); 834 835 debug3("%s: sending authenticated: %d", __func__, authok); 836 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m); 837 838 auth_method = "keyboard-interactive"; 839 auth_submethod = "bsdauth"; 840 841 return (authok != 0); 842 } 843 844 int 845 mm_answer_keyallowed(int sock, Buffer *m) 846 { 847 struct ssh *ssh = active_state; /* XXX */ 848 struct sshkey *key; 849 char *cuser, *chost; 850 u_char *blob; 851 u_int bloblen, pubkey_auth_attempt; 852 enum mm_keytype type = 0; 853 int r, allowed = 0; 854 struct sshauthopt *opts = NULL; 855 856 debug3("%s entering", __func__); 857 type = buffer_get_int(m); 858 cuser = buffer_get_string(m, NULL); 859 chost = buffer_get_string(m, NULL); 860 blob = buffer_get_string(m, &bloblen); 861 pubkey_auth_attempt = buffer_get_int(m); 862 863 key = key_from_blob(blob, bloblen); 864 865 debug3("%s: key_from_blob: %p", __func__, key); 866 867 if (key != NULL && authctxt->valid) { 868 /* These should not make it past the privsep child */ 869 if (key_type_plain(key->type) == KEY_RSA && 870 (datafellows & SSH_BUG_RSASIGMD5) != 0) 871 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__); 872 873 switch (type) { 874 case MM_USERKEY: 875 auth_method = "publickey"; 876 if (!options.pubkey_authentication) 877 break; 878 if (auth2_key_already_used(authctxt, key)) 879 break; 880 if (match_pattern_list(sshkey_ssh_name(key), 881 options.pubkey_key_types, 0) != 1) 882 break; 883 allowed = user_key_allowed(ssh, authctxt->pw, key, 884 pubkey_auth_attempt, &opts); 885 break; 886 case MM_HOSTKEY: 887 auth_method = "hostbased"; 888 if (!options.hostbased_authentication) 889 break; 890 if (auth2_key_already_used(authctxt, key)) 891 break; 892 if (match_pattern_list(sshkey_ssh_name(key), 893 options.hostbased_key_types, 0) != 1) 894 break; 895 allowed = hostbased_key_allowed(authctxt->pw, 896 cuser, chost, key); 897 auth2_record_info(authctxt, 898 "client user \"%.100s\", client host \"%.100s\"", 899 cuser, chost); 900 break; 901 default: 902 fatal("%s: unknown key type %d", __func__, type); 903 break; 904 } 905 } 906 907 debug3("%s: %s authentication%s: %s key is %s", __func__, 908 auth_method, pubkey_auth_attempt ? "" : " test", 909 (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key), 910 allowed ? "allowed" : "not allowed"); 911 912 auth2_record_key(authctxt, 0, key); 913 sshkey_free(key); 914 915 /* clear temporarily storage (used by verify) */ 916 monitor_reset_key_state(); 917 918 if (allowed) { 919 /* Save temporarily for comparison in verify */ 920 key_blob = blob; 921 key_bloblen = bloblen; 922 key_blobtype = type; 923 key_opts = opts; 924 hostbased_cuser = cuser; 925 hostbased_chost = chost; 926 } else { 927 /* Log failed attempt */ 928 auth_log(authctxt, 0, 0, auth_method, NULL); 929 free(blob); 930 free(cuser); 931 free(chost); 932 } 933 934 buffer_clear(m); 935 buffer_put_int(m, allowed); 936 if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0) 937 fatal("%s: sshauthopt_serialise: %s", __func__, ssh_err(r)); 938 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m); 939 940 if (!allowed) 941 sshauthopt_free(opts); 942 943 return (0); 944 } 945 946 static int 947 monitor_valid_userblob(u_char *data, u_int datalen) 948 { 949 Buffer b; 950 u_char *p; 951 char *userstyle, *cp; 952 u_int len; 953 int fail = 0; 954 955 buffer_init(&b); 956 buffer_append(&b, data, datalen); 957 958 if (datafellows & SSH_OLD_SESSIONID) { 959 p = buffer_ptr(&b); 960 len = buffer_len(&b); 961 if ((session_id2 == NULL) || 962 (len < session_id2_len) || 963 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) 964 fail++; 965 buffer_consume(&b, session_id2_len); 966 } else { 967 p = buffer_get_string(&b, &len); 968 if ((session_id2 == NULL) || 969 (len != session_id2_len) || 970 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) 971 fail++; 972 free(p); 973 } 974 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST) 975 fail++; 976 cp = buffer_get_cstring(&b, NULL); 977 xasprintf(&userstyle, "%s%s%s", authctxt->user, 978 authctxt->style ? ":" : "", 979 authctxt->style ? authctxt->style : ""); 980 if (strcmp(userstyle, cp) != 0) { 981 logit("wrong user name passed to monitor: " 982 "expected %s != %.100s", userstyle, cp); 983 fail++; 984 } 985 free(userstyle); 986 free(cp); 987 buffer_skip_string(&b); 988 cp = buffer_get_cstring(&b, NULL); 989 if (strcmp("publickey", cp) != 0) 990 fail++; 991 free(cp); 992 if (!buffer_get_char(&b)) 993 fail++; 994 buffer_skip_string(&b); 995 buffer_skip_string(&b); 996 if (buffer_len(&b) != 0) 997 fail++; 998 buffer_free(&b); 999 return (fail == 0); 1000 } 1001 1002 static int 1003 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser, 1004 char *chost) 1005 { 1006 Buffer b; 1007 char *p, *userstyle; 1008 u_int len; 1009 int fail = 0; 1010 1011 buffer_init(&b); 1012 buffer_append(&b, data, datalen); 1013 1014 p = buffer_get_string(&b, &len); 1015 if ((session_id2 == NULL) || 1016 (len != session_id2_len) || 1017 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) 1018 fail++; 1019 free(p); 1020 1021 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST) 1022 fail++; 1023 p = buffer_get_cstring(&b, NULL); 1024 xasprintf(&userstyle, "%s%s%s", authctxt->user, 1025 authctxt->style ? ":" : "", 1026 authctxt->style ? authctxt->style : ""); 1027 if (strcmp(userstyle, p) != 0) { 1028 logit("wrong user name passed to monitor: expected %s != %.100s", 1029 userstyle, p); 1030 fail++; 1031 } 1032 free(userstyle); 1033 free(p); 1034 buffer_skip_string(&b); /* service */ 1035 p = buffer_get_cstring(&b, NULL); 1036 if (strcmp(p, "hostbased") != 0) 1037 fail++; 1038 free(p); 1039 buffer_skip_string(&b); /* pkalg */ 1040 buffer_skip_string(&b); /* pkblob */ 1041 1042 /* verify client host, strip trailing dot if necessary */ 1043 p = buffer_get_string(&b, NULL); 1044 if (((len = strlen(p)) > 0) && p[len - 1] == '.') 1045 p[len - 1] = '\0'; 1046 if (strcmp(p, chost) != 0) 1047 fail++; 1048 free(p); 1049 1050 /* verify client user */ 1051 p = buffer_get_string(&b, NULL); 1052 if (strcmp(p, cuser) != 0) 1053 fail++; 1054 free(p); 1055 1056 if (buffer_len(&b) != 0) 1057 fail++; 1058 buffer_free(&b); 1059 return (fail == 0); 1060 } 1061 1062 int 1063 mm_answer_keyverify(int sock, struct sshbuf *m) 1064 { 1065 struct ssh *ssh = active_state; /* XXX */ 1066 struct sshkey *key; 1067 u_char *signature, *data, *blob; 1068 char *sigalg; 1069 size_t signaturelen, datalen, bloblen; 1070 int r, ret, valid_data = 0, encoded_ret; 1071 1072 if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 || 1073 (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 || 1074 (r = sshbuf_get_string(m, &data, &datalen)) != 0 || 1075 (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0) 1076 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1077 1078 if (hostbased_cuser == NULL || hostbased_chost == NULL || 1079 !monitor_allowed_key(blob, bloblen)) 1080 fatal("%s: bad key, not previously allowed", __func__); 1081 1082 /* Empty signature algorithm means NULL. */ 1083 if (*sigalg == '\0') { 1084 free(sigalg); 1085 sigalg = NULL; 1086 } 1087 1088 /* XXX use sshkey_froms here; need to change key_blob, etc. */ 1089 if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0) 1090 fatal("%s: bad public key blob: %s", __func__, ssh_err(r)); 1091 1092 switch (key_blobtype) { 1093 case MM_USERKEY: 1094 valid_data = monitor_valid_userblob(data, datalen); 1095 auth_method = "publickey"; 1096 break; 1097 case MM_HOSTKEY: 1098 valid_data = monitor_valid_hostbasedblob(data, datalen, 1099 hostbased_cuser, hostbased_chost); 1100 auth_method = "hostbased"; 1101 break; 1102 default: 1103 valid_data = 0; 1104 break; 1105 } 1106 if (!valid_data) 1107 fatal("%s: bad signature data blob", __func__); 1108 1109 ret = sshkey_verify(key, signature, signaturelen, data, datalen, 1110 sigalg, active_state->compat); 1111 debug3("%s: %s %p signature %s", __func__, auth_method, key, 1112 (ret == 0) ? "verified" : "unverified"); 1113 auth2_record_key(authctxt, ret == 0, key); 1114 1115 free(blob); 1116 free(signature); 1117 free(data); 1118 free(sigalg); 1119 1120 if (key_blobtype == MM_USERKEY) 1121 auth_activate_options(ssh, key_opts); 1122 monitor_reset_key_state(); 1123 1124 sshkey_free(key); 1125 sshbuf_reset(m); 1126 1127 /* encode ret != 0 as positive integer, since we're sending u32 */ 1128 encoded_ret = (ret != 0); 1129 if ((r = sshbuf_put_u32(m, encoded_ret)) != 0) 1130 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1131 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m); 1132 1133 return ret == 0; 1134 } 1135 1136 static void 1137 mm_record_login(Session *s, struct passwd *pw) 1138 { 1139 struct ssh *ssh = active_state; /* XXX */ 1140 socklen_t fromlen; 1141 struct sockaddr_storage from; 1142 1143 /* 1144 * Get IP address of client. If the connection is not a socket, let 1145 * the address be 0.0.0.0. 1146 */ 1147 memset(&from, 0, sizeof(from)); 1148 fromlen = sizeof(from); 1149 if (packet_connection_is_on_socket()) { 1150 if (getpeername(packet_get_connection_in(), 1151 (struct sockaddr *)&from, &fromlen) < 0) { 1152 debug("getpeername: %.100s", strerror(errno)); 1153 cleanup_exit(255); 1154 } 1155 } 1156 /* Record that there was a login on that tty from the remote host. */ 1157 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid, 1158 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns), 1159 (struct sockaddr *)&from, fromlen); 1160 } 1161 1162 static void 1163 mm_session_close(Session *s) 1164 { 1165 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid); 1166 if (s->ttyfd != -1) { 1167 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd); 1168 session_pty_cleanup2(s); 1169 } 1170 session_unused(s->self); 1171 } 1172 1173 int 1174 mm_answer_pty(int sock, Buffer *m) 1175 { 1176 extern struct monitor *pmonitor; 1177 Session *s; 1178 int res, fd0; 1179 1180 debug3("%s entering", __func__); 1181 1182 buffer_clear(m); 1183 s = session_new(); 1184 if (s == NULL) 1185 goto error; 1186 s->authctxt = authctxt; 1187 s->pw = authctxt->pw; 1188 s->pid = pmonitor->m_pid; 1189 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)); 1190 if (res == 0) 1191 goto error; 1192 pty_setowner(authctxt->pw, s->tty); 1193 1194 buffer_put_int(m, 1); 1195 buffer_put_cstring(m, s->tty); 1196 1197 /* We need to trick ttyslot */ 1198 if (dup2(s->ttyfd, 0) == -1) 1199 fatal("%s: dup2", __func__); 1200 1201 mm_record_login(s, authctxt->pw); 1202 1203 /* Now we can close the file descriptor again */ 1204 close(0); 1205 1206 /* send messages generated by record_login */ 1207 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg)); 1208 buffer_clear(&loginmsg); 1209 1210 mm_request_send(sock, MONITOR_ANS_PTY, m); 1211 1212 if (mm_send_fd(sock, s->ptyfd) == -1 || 1213 mm_send_fd(sock, s->ttyfd) == -1) 1214 fatal("%s: send fds failed", __func__); 1215 1216 /* make sure nothing uses fd 0 */ 1217 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0) 1218 fatal("%s: open(/dev/null): %s", __func__, strerror(errno)); 1219 if (fd0 != 0) 1220 error("%s: fd0 %d != 0", __func__, fd0); 1221 1222 /* slave is not needed */ 1223 close(s->ttyfd); 1224 s->ttyfd = s->ptyfd; 1225 /* no need to dup() because nobody closes ptyfd */ 1226 s->ptymaster = s->ptyfd; 1227 1228 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd); 1229 1230 return (0); 1231 1232 error: 1233 if (s != NULL) 1234 mm_session_close(s); 1235 buffer_put_int(m, 0); 1236 mm_request_send(sock, MONITOR_ANS_PTY, m); 1237 return (0); 1238 } 1239 1240 int 1241 mm_answer_pty_cleanup(int sock, Buffer *m) 1242 { 1243 Session *s; 1244 char *tty; 1245 1246 debug3("%s entering", __func__); 1247 1248 tty = buffer_get_string(m, NULL); 1249 if ((s = session_by_tty(tty)) != NULL) 1250 mm_session_close(s); 1251 buffer_clear(m); 1252 free(tty); 1253 return (0); 1254 } 1255 1256 int 1257 mm_answer_term(int sock, Buffer *req) 1258 { 1259 struct ssh *ssh = active_state; /* XXX */ 1260 extern struct monitor *pmonitor; 1261 int res, status; 1262 1263 debug3("%s: tearing down sessions", __func__); 1264 1265 /* The child is terminating */ 1266 session_destroy_all(ssh, &mm_session_close); 1267 1268 while (waitpid(pmonitor->m_pid, &status, 0) == -1) 1269 if (errno != EINTR) 1270 exit(1); 1271 1272 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1; 1273 1274 /* Terminate process */ 1275 exit(res); 1276 } 1277 1278 void 1279 monitor_clear_keystate(struct monitor *pmonitor) 1280 { 1281 struct ssh *ssh = active_state; /* XXX */ 1282 1283 ssh_clear_newkeys(ssh, MODE_IN); 1284 ssh_clear_newkeys(ssh, MODE_OUT); 1285 sshbuf_free(child_state); 1286 child_state = NULL; 1287 } 1288 1289 void 1290 monitor_apply_keystate(struct monitor *pmonitor) 1291 { 1292 struct ssh *ssh = active_state; /* XXX */ 1293 struct kex *kex; 1294 int r; 1295 1296 debug3("%s: packet_set_state", __func__); 1297 if ((r = ssh_packet_set_state(ssh, child_state)) != 0) 1298 fatal("%s: packet_set_state: %s", __func__, ssh_err(r)); 1299 sshbuf_free(child_state); 1300 child_state = NULL; 1301 1302 if ((kex = ssh->kex) != NULL) { 1303 /* XXX set callbacks */ 1304 #ifdef WITH_OPENSSL 1305 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server; 1306 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server; 1307 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server; 1308 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server; 1309 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server; 1310 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server; 1311 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server; 1312 kex->kex[KEX_ECDH_SHA2] = kexecdh_server; 1313 #endif 1314 kex->kex[KEX_C25519_SHA256] = kexc25519_server; 1315 kex->load_host_public_key=&get_hostkey_public_by_type; 1316 kex->load_host_private_key=&get_hostkey_private_by_type; 1317 kex->host_key_index=&get_hostkey_index; 1318 kex->sign = sshd_hostkey_sign; 1319 } 1320 } 1321 1322 /* This function requries careful sanity checking */ 1323 1324 void 1325 mm_get_keystate(struct monitor *pmonitor) 1326 { 1327 debug3("%s: Waiting for new keys", __func__); 1328 1329 if ((child_state = sshbuf_new()) == NULL) 1330 fatal("%s: sshbuf_new failed", __func__); 1331 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, 1332 child_state); 1333 debug3("%s: GOT new keys", __func__); 1334 } 1335 1336 1337 /* XXX */ 1338 1339 #define FD_CLOSEONEXEC(x) do { \ 1340 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \ 1341 fatal("fcntl(%d, F_SETFD)", x); \ 1342 } while (0) 1343 1344 static void 1345 monitor_openfds(struct monitor *mon, int do_logfds) 1346 { 1347 int pair[2]; 1348 #ifdef SO_ZEROIZE 1349 int on = 1; 1350 #endif 1351 1352 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) 1353 fatal("%s: socketpair: %s", __func__, strerror(errno)); 1354 #ifdef SO_ZEROIZE 1355 if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0) 1356 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno)); 1357 if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0) 1358 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno)); 1359 #endif 1360 FD_CLOSEONEXEC(pair[0]); 1361 FD_CLOSEONEXEC(pair[1]); 1362 mon->m_recvfd = pair[0]; 1363 mon->m_sendfd = pair[1]; 1364 1365 if (do_logfds) { 1366 if (pipe(pair) == -1) 1367 fatal("%s: pipe: %s", __func__, strerror(errno)); 1368 FD_CLOSEONEXEC(pair[0]); 1369 FD_CLOSEONEXEC(pair[1]); 1370 mon->m_log_recvfd = pair[0]; 1371 mon->m_log_sendfd = pair[1]; 1372 } else 1373 mon->m_log_recvfd = mon->m_log_sendfd = -1; 1374 } 1375 1376 #define MM_MEMSIZE 65536 1377 1378 struct monitor * 1379 monitor_init(void) 1380 { 1381 struct monitor *mon; 1382 1383 mon = xcalloc(1, sizeof(*mon)); 1384 monitor_openfds(mon, 1); 1385 1386 return mon; 1387 } 1388 1389 void 1390 monitor_reinit(struct monitor *mon) 1391 { 1392 monitor_openfds(mon, 0); 1393 } 1394 1395 #ifdef GSSAPI 1396 int 1397 mm_answer_gss_setup_ctx(int sock, Buffer *m) 1398 { 1399 gss_OID_desc goid; 1400 OM_uint32 major; 1401 u_int len; 1402 1403 if (!options.gss_authentication) 1404 fatal("%s: GSSAPI authentication not enabled", __func__); 1405 1406 goid.elements = buffer_get_string(m, &len); 1407 goid.length = len; 1408 1409 major = ssh_gssapi_server_ctx(&gsscontext, &goid); 1410 1411 free(goid.elements); 1412 1413 buffer_clear(m); 1414 buffer_put_int(m, major); 1415 1416 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m); 1417 1418 /* Now we have a context, enable the step */ 1419 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1); 1420 1421 return (0); 1422 } 1423 1424 int 1425 mm_answer_gss_accept_ctx(int sock, Buffer *m) 1426 { 1427 gss_buffer_desc in; 1428 gss_buffer_desc out = GSS_C_EMPTY_BUFFER; 1429 OM_uint32 major, minor; 1430 OM_uint32 flags = 0; /* GSI needs this */ 1431 u_int len; 1432 1433 if (!options.gss_authentication) 1434 fatal("%s: GSSAPI authentication not enabled", __func__); 1435 1436 in.value = buffer_get_string(m, &len); 1437 in.length = len; 1438 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags); 1439 free(in.value); 1440 1441 buffer_clear(m); 1442 buffer_put_int(m, major); 1443 buffer_put_string(m, out.value, out.length); 1444 buffer_put_int(m, flags); 1445 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m); 1446 1447 gss_release_buffer(&minor, &out); 1448 1449 if (major == GSS_S_COMPLETE) { 1450 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0); 1451 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1); 1452 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1); 1453 } 1454 return (0); 1455 } 1456 1457 int 1458 mm_answer_gss_checkmic(int sock, Buffer *m) 1459 { 1460 gss_buffer_desc gssbuf, mic; 1461 OM_uint32 ret; 1462 u_int len; 1463 1464 if (!options.gss_authentication) 1465 fatal("%s: GSSAPI authentication not enabled", __func__); 1466 1467 gssbuf.value = buffer_get_string(m, &len); 1468 gssbuf.length = len; 1469 mic.value = buffer_get_string(m, &len); 1470 mic.length = len; 1471 1472 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic); 1473 1474 free(gssbuf.value); 1475 free(mic.value); 1476 1477 buffer_clear(m); 1478 buffer_put_int(m, ret); 1479 1480 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m); 1481 1482 if (!GSS_ERROR(ret)) 1483 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1); 1484 1485 return (0); 1486 } 1487 1488 int 1489 mm_answer_gss_userok(int sock, Buffer *m) 1490 { 1491 int authenticated; 1492 const char *displayname; 1493 1494 if (!options.gss_authentication) 1495 fatal("%s: GSSAPI authentication not enabled", __func__); 1496 1497 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user); 1498 1499 buffer_clear(m); 1500 buffer_put_int(m, authenticated); 1501 1502 debug3("%s: sending result %d", __func__, authenticated); 1503 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m); 1504 1505 auth_method = "gssapi-with-mic"; 1506 1507 if ((displayname = ssh_gssapi_displayname()) != NULL) 1508 auth2_record_info(authctxt, "%s", displayname); 1509 1510 /* Monitor loop will terminate if authenticated */ 1511 return (authenticated); 1512 } 1513 #endif /* GSSAPI */ 1514 1515