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