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