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