1 /* $OpenBSD: monitor.c,v 1.256 2026/05/31 11:30:50 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 "includes.h" 29 30 #include <sys/types.h> 31 #include <sys/wait.h> 32 #include <sys/socket.h> 33 #include <sys/tree.h> 34 #include <sys/queue.h> 35 36 #include <errno.h> 37 #include <fcntl.h> 38 #include <limits.h> 39 #include <paths.h> 40 #include <poll.h> 41 #include <pwd.h> 42 #include <signal.h> 43 #include <stdarg.h> 44 #include <stdint.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <unistd.h> 49 50 #ifdef WITH_OPENSSL 51 #include <openssl/dh.h> 52 #endif 53 54 #include "openbsd-compat/openssl-compat.h" 55 56 #include "atomicio.h" 57 #include "xmalloc.h" 58 #include "ssh.h" 59 #include "sshkey.h" 60 #include "sshbuf.h" 61 #include "hostfile.h" 62 #include "auth.h" 63 #include "cipher.h" 64 #include "kex.h" 65 #include "dh.h" 66 #include "auth-pam.h" 67 #include "packet.h" 68 #include "auth-options.h" 69 #include "sshpty.h" 70 #include "channels.h" 71 #include "session.h" 72 #include "sshlogin.h" 73 #include "canohost.h" 74 #include "log.h" 75 #include "misc.h" 76 #include "servconf.h" 77 #include "monitor.h" 78 #include "blocklist_client.h" 79 80 #ifdef GSSAPI 81 #include "ssh-gss.h" 82 #endif 83 #include "monitor_wrap.h" 84 #include "monitor_fdpass.h" 85 #include "compat.h" 86 #include "ssh2.h" 87 #include "authfd.h" 88 #include "match.h" 89 #include "ssherr.h" 90 #include "sk-api.h" 91 #include "srclimit.h" 92 93 #ifdef GSSAPI 94 static Gssctxt *gsscontext = NULL; 95 #endif 96 97 /* Imports */ 98 extern ServerOptions options; 99 extern u_int utmp_len; 100 extern struct sshbuf *cfg; 101 extern struct sshbuf *loginmsg; 102 extern struct include_list includes; 103 extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */ 104 105 /* State exported from the child */ 106 static struct sshbuf *child_state; 107 108 /* Functions on the monitor that answer unprivileged requests */ 109 110 int mm_answer_moduli(struct ssh *, int, struct sshbuf *); 111 int mm_answer_setcompat(struct ssh *, int, struct sshbuf *); 112 int mm_answer_sign(struct ssh *, int, struct sshbuf *); 113 int mm_answer_pwnamallow(struct ssh *, int, struct sshbuf *); 114 int mm_answer_auth2_read_banner(struct ssh *, int, struct sshbuf *); 115 int mm_answer_authserv(struct ssh *, int, struct sshbuf *); 116 int mm_answer_authpassword(struct ssh *, int, struct sshbuf *); 117 int mm_answer_bsdauthquery(struct ssh *, int, struct sshbuf *); 118 int mm_answer_bsdauthrespond(struct ssh *, int, struct sshbuf *); 119 int mm_answer_keyallowed(struct ssh *, int, struct sshbuf *); 120 int mm_answer_keyverify(struct ssh *, int, struct sshbuf *); 121 int mm_answer_pty(struct ssh *, int, struct sshbuf *); 122 int mm_answer_pty_cleanup(struct ssh *, int, struct sshbuf *); 123 int mm_answer_term(struct ssh *, int, struct sshbuf *); 124 int mm_answer_state(struct ssh *, int, struct sshbuf *); 125 126 #ifdef USE_PAM 127 int mm_answer_pam_start(struct ssh *, int, struct sshbuf *); 128 int mm_answer_pam_account(struct ssh *, int, struct sshbuf *); 129 int mm_answer_pam_init_ctx(struct ssh *, int, struct sshbuf *); 130 int mm_answer_pam_query(struct ssh *, int, struct sshbuf *); 131 int mm_answer_pam_respond(struct ssh *, int, struct sshbuf *); 132 int mm_answer_pam_free_ctx(struct ssh *, int, struct sshbuf *); 133 #endif 134 135 #ifdef GSSAPI 136 int mm_answer_gss_setup_ctx(struct ssh *, int, struct sshbuf *); 137 int mm_answer_gss_accept_ctx(struct ssh *, int, struct sshbuf *); 138 int mm_answer_gss_userok(struct ssh *, int, struct sshbuf *); 139 int mm_answer_gss_checkmic(struct ssh *, int, struct sshbuf *); 140 #endif 141 142 #ifdef SSH_AUDIT_EVENTS 143 int mm_answer_audit_event(struct ssh *, int, struct sshbuf *); 144 int mm_answer_audit_command(struct ssh *, int, struct sshbuf *); 145 #endif 146 147 static Authctxt *authctxt; 148 149 /* local state for key verify */ 150 static u_char *key_blob = NULL; 151 static size_t key_bloblen = 0; 152 static u_int key_blobtype = MM_NOKEY; 153 static struct sshauthopt *key_opts = NULL; 154 static char *hostbased_cuser = NULL; 155 static char *hostbased_chost = NULL; 156 static char *auth_method = "unknown"; 157 static char *auth_submethod = NULL; 158 static u_int session_id2_len = 0; 159 static u_char *session_id2 = NULL; 160 static pid_t monitor_child_pid; 161 static int auth_attempted = 0; 162 static int invalid_user = 0; 163 static int compat_set = 0; 164 165 struct mon_table { 166 enum monitor_reqtype type; 167 int flags; 168 int (*f)(struct ssh *, int, struct sshbuf *); 169 }; 170 171 #define MON_ISAUTH 0x0004 /* Required for Authentication */ 172 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */ 173 #define MON_ONCE 0x0010 /* Disable after calling */ 174 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */ 175 176 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE) 177 178 #define MON_PERMIT 0x1000 /* Request is permitted */ 179 180 static int monitor_read(struct ssh *, struct monitor *, struct mon_table *, 181 struct mon_table **); 182 static int monitor_read_log(struct monitor *); 183 184 struct mon_table mon_dispatch_proto20[] = { 185 {MONITOR_REQ_STATE, MON_ONCE, mm_answer_state}, 186 #ifdef WITH_OPENSSL 187 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli}, 188 #endif 189 {MONITOR_REQ_SETCOMPAT, MON_ONCE, mm_answer_setcompat}, 190 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign}, 191 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow}, 192 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv}, 193 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner}, 194 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword}, 195 #ifdef USE_PAM 196 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start}, 197 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account}, 198 {MONITOR_REQ_PAM_INIT_CTX, MON_ONCE, mm_answer_pam_init_ctx}, 199 {MONITOR_REQ_PAM_QUERY, 0, mm_answer_pam_query}, 200 {MONITOR_REQ_PAM_RESPOND, MON_ONCE, mm_answer_pam_respond}, 201 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx}, 202 #endif 203 #ifdef SSH_AUDIT_EVENTS 204 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event}, 205 #endif 206 #ifdef BSD_AUTH 207 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery}, 208 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond}, 209 #endif 210 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed}, 211 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify}, 212 #ifdef GSSAPI 213 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx}, 214 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx}, 215 {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok}, 216 {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic}, 217 #endif 218 {0, 0, NULL} 219 }; 220 221 struct mon_table mon_dispatch_postauth20[] = { 222 {MONITOR_REQ_STATE, MON_ONCE, mm_answer_state}, 223 #ifdef WITH_OPENSSL 224 {MONITOR_REQ_MODULI, 0, mm_answer_moduli}, 225 #endif 226 {MONITOR_REQ_SIGN, 0, mm_answer_sign}, 227 {MONITOR_REQ_PTY, 0, mm_answer_pty}, 228 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup}, 229 {MONITOR_REQ_TERM, 0, mm_answer_term}, 230 #ifdef SSH_AUDIT_EVENTS 231 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event}, 232 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command}, 233 #endif 234 {0, 0, NULL} 235 }; 236 237 struct mon_table *mon_dispatch; 238 239 /* Specifies if a certain message is allowed at the moment */ 240 static void 241 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit) 242 { 243 while (ent->f != NULL) { 244 if (ent->type == type) { 245 ent->flags &= ~MON_PERMIT; 246 ent->flags |= permit ? MON_PERMIT : 0; 247 return; 248 } 249 ent++; 250 } 251 } 252 253 static void 254 monitor_permit_authentications(int permit) 255 { 256 struct mon_table *ent = mon_dispatch; 257 258 while (ent->f != NULL) { 259 if (ent->flags & MON_AUTH) { 260 ent->flags &= ~MON_PERMIT; 261 ent->flags |= permit ? MON_PERMIT : 0; 262 } 263 ent++; 264 } 265 } 266 267 void 268 monitor_child_preauth(struct ssh *ssh, struct monitor *pmonitor) 269 { 270 struct mon_table *ent; 271 int status, authenticated = 0, partial = 0; 272 273 debug3("preauth child monitor started"); 274 275 if (pmonitor->m_recvfd >= 0) 276 close(pmonitor->m_recvfd); 277 if (pmonitor->m_log_sendfd >= 0) 278 close(pmonitor->m_log_sendfd); 279 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1; 280 281 authctxt = (Authctxt *)ssh->authctxt; 282 memset(authctxt, 0, sizeof(*authctxt)); 283 ssh->authctxt = authctxt; 284 285 authctxt->loginmsg = loginmsg; 286 287 mon_dispatch = mon_dispatch_proto20; 288 /* Permit requests for state, moduli and signatures */ 289 monitor_permit(mon_dispatch, MONITOR_REQ_STATE, 1); 290 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1); 291 monitor_permit(mon_dispatch, MONITOR_REQ_SETCOMPAT, 1); 292 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1); 293 294 /* The first few requests do not require asynchronous access */ 295 while (!authenticated) { 296 partial = 0; 297 auth_method = "unknown"; 298 auth_submethod = NULL; 299 auth2_authctxt_reset_info(authctxt); 300 301 authenticated = (monitor_read(ssh, pmonitor, 302 mon_dispatch, &ent) == 1); 303 304 /* Record that auth was attempted to set exit status later */ 305 if ((ent->flags & MON_AUTH) != 0) 306 auth_attempted = 1; 307 308 /* Special handling for multiple required authentications */ 309 if (options.num_auth_methods != 0) { 310 if (authenticated && 311 !auth2_update_methods_lists(authctxt, 312 auth_method, auth_submethod)) { 313 debug3_f("method %s: partial", auth_method); 314 authenticated = 0; 315 partial = 1; 316 } 317 } 318 319 if (authenticated) { 320 if (!(ent->flags & MON_AUTHDECIDE)) 321 fatal_f("unexpected authentication from %d", 322 ent->type); 323 if (authctxt->pw->pw_uid == 0 && 324 !auth_root_allowed(ssh, auth_method)) 325 authenticated = 0; 326 #ifdef USE_PAM 327 /* PAM needs to perform account checks after auth */ 328 if (options.use_pam && authenticated) { 329 struct sshbuf *m; 330 331 if ((m = sshbuf_new()) == NULL) 332 fatal("%s: sshbuf_new failed", 333 __func__); 334 mm_request_receive_expect(pmonitor->m_sendfd, 335 MONITOR_REQ_PAM_ACCOUNT, m); 336 authenticated = mm_answer_pam_account( 337 ssh, pmonitor->m_sendfd, m); 338 sshbuf_free(m); 339 } 340 #endif 341 } 342 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) { 343 auth_log(ssh, authenticated, partial, 344 auth_method, auth_submethod); 345 if (!partial && !authenticated) 346 authctxt->failures++; 347 if (authenticated || partial) { 348 auth2_update_session_info(authctxt, 349 auth_method, auth_submethod); 350 } 351 } 352 if (authctxt->failures > options.max_authtries) { 353 BLOCKLIST_NOTIFY(ssh, BLOCKLIST_AUTH_FAIL, 354 "Too many authentication attempts"); 355 /* Shouldn't happen */ 356 fatal_f("privsep child made too many authentication " 357 "attempts"); 358 } 359 } 360 361 if (!authctxt->valid) { 362 BLOCKLIST_NOTIFY(ssh, BLOCKLIST_AUTH_FAIL, 363 "Authenticated invalid user"); 364 fatal_f("authenticated invalid user"); 365 } 366 if (strcmp(auth_method, "unknown") == 0) { 367 BLOCKLIST_NOTIFY(ssh, BLOCKLIST_AUTH_FAIL, 368 "Authentication method name unknown"); 369 fatal_f("authentication method name unknown"); 370 } 371 372 debug_f("user %s authenticated by privileged process", authctxt->user); 373 auth_attempted = 0; 374 ssh->authctxt = NULL; 375 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user); 376 377 mm_get_keystate(ssh, pmonitor); 378 379 /* Drain any buffered messages from the child */ 380 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0) 381 ; 382 383 /* Wait for the child's exit status */ 384 while (waitpid(pmonitor->m_pid, &status, 0) == -1) { 385 if (errno == EINTR) 386 continue; 387 fatal_f("waitpid: %s", strerror(errno)); 388 } 389 if (WIFEXITED(status)) { 390 if (WEXITSTATUS(status) != 0) 391 fatal_f("preauth child %ld exited with status %d", 392 (long)pmonitor->m_pid, WEXITSTATUS(status)); 393 } else if (WIFSIGNALED(status)) { 394 fatal_f("preauth child %ld terminated by signal %d", 395 (long)pmonitor->m_pid, WTERMSIG(status)); 396 } 397 debug3_f("preauth child %ld terminated successfully", 398 (long)pmonitor->m_pid); 399 400 if (pmonitor->m_recvfd >= 0) 401 close(pmonitor->m_recvfd); 402 if (pmonitor->m_log_sendfd >= 0) 403 close(pmonitor->m_log_sendfd); 404 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1; 405 pmonitor->m_pid = -1; 406 } 407 408 static void 409 monitor_set_child_handler(pid_t pid) 410 { 411 monitor_child_pid = pid; 412 } 413 414 static void 415 monitor_child_handler(int sig) 416 { 417 kill(monitor_child_pid, sig); 418 } 419 420 void 421 monitor_child_postauth(struct ssh *ssh, struct monitor *pmonitor) 422 { 423 close(pmonitor->m_recvfd); 424 pmonitor->m_recvfd = -1; 425 426 monitor_set_child_handler(pmonitor->m_pid); 427 ssh_signal(SIGHUP, &monitor_child_handler); 428 ssh_signal(SIGTERM, &monitor_child_handler); 429 ssh_signal(SIGINT, &monitor_child_handler); 430 #ifdef SIGXFSZ 431 ssh_signal(SIGXFSZ, SIG_IGN); 432 #endif 433 434 mon_dispatch = mon_dispatch_postauth20; 435 436 /* Permit requests for moduli and signatures */ 437 monitor_permit(mon_dispatch, MONITOR_REQ_STATE, 1); 438 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1); 439 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1); 440 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1); 441 442 if (auth_opts->permit_pty_flag) { 443 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1); 444 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1); 445 } 446 447 for (;;) 448 monitor_read(ssh, pmonitor, mon_dispatch, NULL); 449 } 450 451 static int 452 monitor_read_log(struct monitor *pmonitor) 453 { 454 struct sshbuf *logmsg; 455 u_int len, level, forced; 456 char *msg; 457 u_char *p; 458 int r; 459 460 if ((logmsg = sshbuf_new()) == NULL) 461 fatal_f("sshbuf_new"); 462 463 /* Read length */ 464 if ((r = sshbuf_reserve(logmsg, 4, &p)) != 0) 465 fatal_fr(r, "reserve len"); 466 if (atomicio(read, pmonitor->m_log_recvfd, p, 4) != 4) { 467 if (errno == EPIPE) { 468 sshbuf_free(logmsg); 469 debug_f("child log fd closed"); 470 close(pmonitor->m_log_recvfd); 471 pmonitor->m_log_recvfd = -1; 472 return -1; 473 } 474 fatal_f("log fd read: %s", strerror(errno)); 475 } 476 if ((r = sshbuf_get_u32(logmsg, &len)) != 0) 477 fatal_fr(r, "parse len"); 478 if (len <= 4 || len > 8192) 479 fatal_f("invalid log message length %u", len); 480 481 /* Read severity, message */ 482 sshbuf_reset(logmsg); 483 if ((r = sshbuf_reserve(logmsg, len, &p)) != 0) 484 fatal_fr(r, "reserve msg"); 485 if (atomicio(read, pmonitor->m_log_recvfd, p, len) != len) 486 fatal_f("log fd read: %s", strerror(errno)); 487 if ((r = sshbuf_get_u32(logmsg, &level)) != 0 || 488 (r = sshbuf_get_u32(logmsg, &forced)) != 0 || 489 (r = sshbuf_get_cstring(logmsg, &msg, NULL)) != 0) 490 fatal_fr(r, "parse"); 491 492 /* Log it */ 493 if (log_level_name(level) == NULL) 494 fatal_f("invalid log level %u (corrupted message?)", level); 495 sshlogdirect(level, forced, "%s [%s]", msg, 496 mon_dispatch == mon_dispatch_postauth20 ? "postauth" : "preauth"); 497 498 sshbuf_free(logmsg); 499 free(msg); 500 501 return 0; 502 } 503 504 static int 505 monitor_read(struct ssh *ssh, struct monitor *pmonitor, struct mon_table *ent, 506 struct mon_table **pent) 507 { 508 struct sshbuf *m; 509 int r, ret; 510 u_char type; 511 struct pollfd pfd[2]; 512 513 for (;;) { 514 memset(&pfd, 0, sizeof(pfd)); 515 pfd[0].fd = pmonitor->m_sendfd; 516 pfd[0].events = POLLIN; 517 pfd[1].fd = pmonitor->m_log_recvfd; 518 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN; 519 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) { 520 if (errno == EINTR || errno == EAGAIN) 521 continue; 522 fatal_f("poll: %s", strerror(errno)); 523 } 524 if (pfd[1].revents) { 525 /* 526 * Drain all log messages before processing next 527 * monitor request. 528 */ 529 monitor_read_log(pmonitor); 530 continue; 531 } 532 if (pfd[0].revents) 533 break; /* Continues below */ 534 } 535 536 if ((m = sshbuf_new()) == NULL) 537 fatal_f("sshbuf_new"); 538 539 mm_request_receive(pmonitor->m_sendfd, m); 540 if ((r = sshbuf_get_u8(m, &type)) != 0) 541 fatal_fr(r, "parse type"); 542 543 debug3_f("checking request %d", type); 544 545 while (ent->f != NULL) { 546 if (ent->type == type) 547 break; 548 ent++; 549 } 550 551 if (ent->f != NULL) { 552 if (!(ent->flags & MON_PERMIT)) 553 fatal_f("unpermitted request %d", type); 554 ret = (*ent->f)(ssh, pmonitor->m_sendfd, m); 555 sshbuf_free(m); 556 557 /* The child may use this request only once, disable it */ 558 if (ent->flags & MON_ONCE) { 559 debug2_f("%d used once, disabling now", type); 560 ent->flags &= ~MON_PERMIT; 561 } 562 563 if (pent != NULL) 564 *pent = ent; 565 566 return ret; 567 } 568 569 fatal_f("unsupported request: %d", type); 570 571 /* NOTREACHED */ 572 return (-1); 573 } 574 575 /* allowed key state */ 576 static int 577 monitor_allowed_key(const u_char *blob, u_int bloblen) 578 { 579 /* make sure key is allowed */ 580 if (key_blob == NULL || key_bloblen != bloblen || 581 timingsafe_bcmp(key_blob, blob, key_bloblen)) 582 return (0); 583 return (1); 584 } 585 586 static void 587 monitor_reset_key_state(void) 588 { 589 /* reset state */ 590 free(key_blob); 591 free(hostbased_cuser); 592 free(hostbased_chost); 593 sshauthopt_free(key_opts); 594 key_blob = NULL; 595 key_bloblen = 0; 596 key_blobtype = MM_NOKEY; 597 key_opts = NULL; 598 hostbased_cuser = NULL; 599 hostbased_chost = NULL; 600 } 601 602 int 603 mm_answer_state(struct ssh *ssh, int sock, struct sshbuf *unused) 604 { 605 struct sshbuf *m = NULL, *config = NULL, *hostkeys = NULL; 606 struct sshbuf *opts = NULL, *confdata = NULL; 607 int postauth; 608 int r; 609 610 debug_f("config len %zu", sshbuf_len(cfg)); 611 612 if ((m = sshbuf_new()) == NULL || 613 (config = sshbuf_new()) == NULL || 614 (opts = sshbuf_new()) == NULL || 615 (confdata = sshbuf_new()) == NULL) 616 fatal_f("sshbuf_new failed"); 617 618 hostkeys = pack_hostkeys(); 619 if ((r = serialise_server_options(&options, &config)) != 0) 620 fatal_fr(r, "serialise_server_options"); 621 622 /* 623 * Protocol from monitor to unpriv privsep process: 624 * string configuration_blob 625 * uint64 timing_secret XXX move delays to monitor and remove 626 * string host_keys[] { 627 * string public_key 628 * string certificate 629 * } 630 * string server_banner 631 * string client_banner 632 * string configuration_data (postauth) 633 * string keystate (postauth) 634 * string authenticated_user (postauth) 635 * string session_info (postauth) 636 * string authopts (postauth) 637 */ 638 if ((r = sshbuf_put_stringb(m, config)) != 0 || 639 (r = sshbuf_put_u64(m, options.timing_secret)) != 0 || 640 (r = sshbuf_put_stringb(m, hostkeys)) != 0 || 641 (r = sshbuf_put_stringb(m, ssh->kex->server_version)) != 0 || 642 (r = sshbuf_put_stringb(m, ssh->kex->client_version)) != 0) 643 fatal_fr(r, "compose config"); 644 645 postauth = (authctxt && authctxt->pw && authctxt->authenticated); 646 if (postauth) { 647 /* XXX shouldn't be reachable */ 648 fatal_f("internal error: called in postauth"); 649 } 650 651 sshbuf_free(config); 652 sshbuf_free(opts); 653 sshbuf_free(confdata); 654 sshbuf_free(hostkeys); 655 656 mm_request_send(sock, MONITOR_ANS_STATE, m); 657 sshbuf_free(m); 658 debug3_f("done"); 659 660 return (0); 661 } 662 663 #ifdef WITH_OPENSSL 664 int 665 mm_answer_moduli(struct ssh *ssh, int sock, struct sshbuf *m) 666 { 667 DH *dh; 668 const BIGNUM *dh_p, *dh_g; 669 int r; 670 u_int min, want, max; 671 672 if ((r = sshbuf_get_u32(m, &min)) != 0 || 673 (r = sshbuf_get_u32(m, &want)) != 0 || 674 (r = sshbuf_get_u32(m, &max)) != 0) 675 fatal_fr(r, "parse"); 676 677 debug3_f("got parameters: %d %d %d", min, want, max); 678 /* We need to check here, too, in case the child got corrupted */ 679 if (max < min || want < min || max < want) 680 fatal_f("bad parameters: %d %d %d", min, want, max); 681 682 sshbuf_reset(m); 683 684 dh = choose_dh(min, want, max); 685 if (dh == NULL) { 686 if ((r = sshbuf_put_u8(m, 0)) != 0) 687 fatal_fr(r, "assemble empty"); 688 } else { 689 /* Send first bignum */ 690 DH_get0_pqg(dh, &dh_p, NULL, &dh_g); 691 if ((r = sshbuf_put_u8(m, 1)) != 0 || 692 (r = sshbuf_put_bignum2(m, dh_p)) != 0 || 693 (r = sshbuf_put_bignum2(m, dh_g)) != 0) 694 fatal_fr(r, "assemble"); 695 696 DH_free(dh); 697 } 698 mm_request_send(sock, MONITOR_ANS_MODULI, m); 699 return (0); 700 } 701 #endif 702 703 int 704 mm_answer_setcompat(struct ssh *ssh, int sock, struct sshbuf *m) 705 { 706 int r; 707 708 debug3_f("entering"); 709 710 if ((r = sshbuf_get_u32(m, &ssh->compat)) != 0) 711 fatal_fr(r, "parse"); 712 compat_set = 1; 713 714 return (0); 715 } 716 717 int 718 mm_answer_sign(struct ssh *ssh, int sock, struct sshbuf *m) 719 { 720 extern int auth_sock; /* XXX move to state struct? */ 721 struct sshkey *pubkey, *key; 722 struct sshbuf *sigbuf = NULL; 723 u_char *p = NULL, *signature = NULL; 724 char *alg = NULL; 725 size_t datlen, siglen; 726 int r, is_proof = 0, keyid; 727 u_int compat; 728 const char proof_req[] = "hostkeys-prove-00@openssh.com"; 729 730 debug3_f("entering"); 731 732 /* Make sure the unpriv process sent the compat bits already */ 733 if (!compat_set) 734 fatal_f("state error: setcompat never called"); 735 736 if ((r = sshkey_froms(m, &pubkey)) != 0 || 737 (r = sshbuf_get_string(m, &p, &datlen)) != 0 || 738 (r = sshbuf_get_cstring(m, &alg, NULL)) != 0 || 739 (r = sshbuf_get_u32(m, &compat)) != 0) 740 fatal_fr(r, "parse"); 741 742 if ((keyid = get_hostkey_index(pubkey, 1, ssh)) == -1) 743 fatal_f("unknown hostkey"); 744 debug_f("hostkey %s index %d", sshkey_ssh_name(pubkey), keyid); 745 sshkey_free(pubkey); 746 747 /* 748 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes), 749 * SHA384 (48 bytes) and SHA512 (64 bytes). 750 * 751 * Otherwise, verify the signature request is for a hostkey 752 * proof. 753 * 754 * XXX perform similar check for KEX signature requests too? 755 * it's not trivial, since what is signed is the hash, rather 756 * than the full kex structure... 757 */ 758 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) { 759 /* 760 * Construct expected hostkey proof and compare it to what 761 * the client sent us. 762 */ 763 if (session_id2_len == 0) /* hostkeys is never first */ 764 fatal_f("bad data length: %zu", datlen); 765 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL) 766 fatal_f("no hostkey for index %d", keyid); 767 if ((sigbuf = sshbuf_new()) == NULL) 768 fatal_f("sshbuf_new"); 769 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 || 770 (r = sshbuf_put_string(sigbuf, session_id2, 771 session_id2_len)) != 0 || 772 (r = sshkey_puts(key, sigbuf)) != 0) 773 fatal_fr(r, "assemble private key proof"); 774 if (datlen != sshbuf_len(sigbuf) || 775 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0) 776 fatal_f("bad data length: %zu, hostkey proof len %zu", 777 datlen, sshbuf_len(sigbuf)); 778 sshbuf_free(sigbuf); 779 is_proof = 1; 780 } 781 782 /* save session id, it will be passed on the first call */ 783 if (session_id2_len == 0) { 784 session_id2_len = datlen; 785 session_id2 = xmalloc(session_id2_len); 786 memcpy(session_id2, p, session_id2_len); 787 } 788 789 if ((key = get_hostkey_by_index(keyid)) != NULL) { 790 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg, 791 options.sk_provider, NULL, compat)) != 0) 792 fatal_fr(r, "sign"); 793 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL && 794 auth_sock > 0) { 795 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen, 796 p, datlen, alg, compat)) != 0) 797 fatal_fr(r, "agent sign"); 798 } else 799 fatal_f("no hostkey from index %d", keyid); 800 801 debug3_f("%s %s signature len=%zu", alg, 802 is_proof ? "hostkey proof" : "KEX", siglen); 803 804 sshbuf_reset(m); 805 if ((r = sshbuf_put_string(m, signature, siglen)) != 0) 806 fatal_fr(r, "assemble"); 807 808 free(alg); 809 free(p); 810 free(signature); 811 812 mm_request_send(sock, MONITOR_ANS_SIGN, m); 813 814 /* Turn on permissions for getpwnam */ 815 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1); 816 817 return (0); 818 } 819 820 #define PUTPW(b, id) \ 821 do { \ 822 if ((r = sshbuf_put_string(b, \ 823 &pwent->id, sizeof(pwent->id))) != 0) \ 824 fatal_fr(r, "assemble %s", #id); \ 825 } while (0) 826 827 void 828 mm_encode_server_options(struct sshbuf *m) 829 { 830 int r; 831 struct sshbuf *config; 832 833 if ((config = sshbuf_new()) == NULL) 834 fatal_f("sshbuf_new failed"); 835 if ((r = serialise_server_options(&options, &config)) != 0) 836 fatal_fr(r, "serialise_server_options"); 837 if ((r = sshbuf_put_stringb(m, config)) != 0) 838 fatal_fr(r, "assemble options"); 839 sshbuf_free(config); 840 } 841 842 /* Retrieves the password entry and also checks if the user is permitted */ 843 int 844 mm_answer_pwnamallow(struct ssh *ssh, int sock, struct sshbuf *m) 845 { 846 struct passwd *pwent; 847 int r, allowed = 0; 848 849 debug3_f("entering"); 850 851 /* Make sure the unpriv process sent the compat bits already */ 852 if (!compat_set) 853 fatal_f("state error: setcompat never called"); 854 855 if (authctxt->attempt++ != 0) 856 fatal_f("multiple attempts for getpwnam"); 857 858 if ((r = sshbuf_get_cstring(m, &authctxt->user, NULL)) != 0) 859 fatal_fr(r, "parse"); 860 861 pwent = getpwnamallow(ssh, authctxt->user); 862 863 setproctitle("%s [priv]", pwent ? authctxt->user : "unknown"); 864 865 sshbuf_reset(m); 866 867 if (pwent == NULL) { 868 invalid_user = 1; 869 if ((r = sshbuf_put_u8(m, 0)) != 0) 870 fatal_fr(r, "assemble fakepw"); 871 authctxt->pw = fakepw(); 872 goto out; 873 } 874 875 allowed = 1; 876 authctxt->pw = pwent; 877 authctxt->valid = 1; 878 879 /* XXX send fake class/dir/shell, etc. */ 880 if ((r = sshbuf_put_u8(m, 1)) != 0) 881 fatal_fr(r, "assemble ok"); 882 PUTPW(m, pw_uid); 883 PUTPW(m, pw_gid); 884 #ifdef HAVE_STRUCT_PASSWD_PW_CHANGE 885 PUTPW(m, pw_change); 886 #endif 887 #ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE 888 PUTPW(m, pw_expire); 889 #endif 890 if ((r = sshbuf_put_cstring(m, pwent->pw_name)) != 0 || 891 (r = sshbuf_put_cstring(m, "*")) != 0 || 892 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS 893 (r = sshbuf_put_cstring(m, pwent->pw_gecos)) != 0 || 894 #endif 895 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS 896 (r = sshbuf_put_cstring(m, pwent->pw_class)) != 0 || 897 #endif 898 (r = sshbuf_put_cstring(m, pwent->pw_dir)) != 0 || 899 (r = sshbuf_put_cstring(m, pwent->pw_shell)) != 0) 900 fatal_fr(r, "assemble pw"); 901 902 out: 903 ssh_packet_set_log_preamble(ssh, "%suser %s", 904 authctxt->valid ? "authenticating" : "invalid ", authctxt->user); 905 906 if (options.refuse_connection) { 907 logit("administratively prohibited connection for " 908 "%s%s from %.128s port %d", 909 authctxt->valid ? "" : "invalid user ", 910 authctxt->user, ssh_remote_ipaddr(ssh), 911 ssh_remote_port(ssh)); 912 cleanup_exit(EXIT_CONFIG_REFUSED); 913 } 914 915 /* Send active options to unpriv */ 916 mm_encode_server_options(m); 917 918 /* Create valid auth method lists */ 919 if (auth2_setup_methods_lists(authctxt) != 0) { 920 /* 921 * The monitor will continue long enough to let the child 922 * run to its packet_disconnect(), but it must not allow any 923 * authentication to succeed. 924 */ 925 debug_f("no valid authentication method lists"); 926 } 927 928 debug3_f("sending MONITOR_ANS_PWNAM: %d", allowed); 929 mm_request_send(sock, MONITOR_ANS_PWNAM, m); 930 931 /* Allow service/style information on the auth context */ 932 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1); 933 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1); 934 935 #ifdef USE_PAM 936 if (options.use_pam) 937 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1); 938 #endif 939 940 return (0); 941 } 942 943 int mm_answer_auth2_read_banner(struct ssh *ssh, int sock, struct sshbuf *m) 944 { 945 char *banner; 946 int r; 947 948 sshbuf_reset(m); 949 banner = auth2_read_banner(); 950 if ((r = sshbuf_put_cstring(m, banner != NULL ? banner : "")) != 0) 951 fatal_fr(r, "assemble"); 952 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m); 953 free(banner); 954 955 return (0); 956 } 957 958 int 959 mm_answer_authserv(struct ssh *ssh, int sock, struct sshbuf *m) 960 { 961 int r; 962 963 monitor_permit_authentications(1); 964 965 if ((r = sshbuf_get_cstring(m, &authctxt->service, NULL)) != 0 || 966 (r = sshbuf_get_cstring(m, &authctxt->style, NULL)) != 0) 967 fatal_fr(r, "parse"); 968 debug3_f("service=%s, style=%s", authctxt->service, authctxt->style); 969 970 if (strlen(authctxt->style) == 0) { 971 free(authctxt->style); 972 authctxt->style = NULL; 973 } 974 975 return (0); 976 } 977 978 /* 979 * Check that the key type appears in the supplied pattern list, ignoring 980 * mismatches in the signature algorithm. (Signature algorithm checks are 981 * performed in the unprivileged authentication code). 982 * Returns 1 on success, 0 otherwise. 983 */ 984 static int 985 key_base_type_match(const char *method, const struct sshkey *key, 986 const char *list) 987 { 988 char *s, *l, *ol = xstrdup(list); 989 int found = 0; 990 991 l = ol; 992 for ((s = strsep(&l, ",")); s && *s != '\0'; (s = strsep(&l, ","))) { 993 if (sshkey_type_from_name(s) == key->type) { 994 found = 1; 995 break; 996 } 997 } 998 if (!found) { 999 error("%s key type %s is not in permitted list %s", method, 1000 sshkey_ssh_name(key), list); 1001 } 1002 1003 free(ol); 1004 return found; 1005 } 1006 1007 int 1008 mm_answer_authpassword(struct ssh *ssh, int sock, struct sshbuf *m) 1009 { 1010 static int call_count; 1011 char *passwd; 1012 int r, authenticated; 1013 size_t plen; 1014 1015 if (!options.password_authentication) 1016 fatal_f("password authentication not enabled"); 1017 if ((r = sshbuf_get_cstring(m, &passwd, &plen)) != 0) 1018 fatal_fr(r, "parse"); 1019 /* Only authenticate if the context is valid */ 1020 authenticated = options.password_authentication && 1021 auth_password(ssh, passwd); 1022 freezero(passwd, plen); 1023 1024 sshbuf_reset(m); 1025 if ((r = sshbuf_put_u32(m, authenticated)) != 0) 1026 fatal_fr(r, "assemble"); 1027 #ifdef USE_PAM 1028 if ((r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0) 1029 fatal_fr(r, "assemble PAM"); 1030 #endif 1031 1032 debug3_f("sending result %d", authenticated); 1033 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m); 1034 1035 call_count++; 1036 if (plen == 0 && call_count == 1) 1037 auth_method = "none"; 1038 else 1039 auth_method = "password"; 1040 1041 /* Causes monitor loop to terminate if authenticated */ 1042 return (authenticated); 1043 } 1044 1045 #ifdef BSD_AUTH 1046 int 1047 mm_answer_bsdauthquery(struct ssh *ssh, int sock, struct sshbuf *m) 1048 { 1049 char *name, *infotxt; 1050 u_int numprompts, *echo_on, success; 1051 char **prompts; 1052 int r; 1053 1054 if (!options.kbd_interactive_authentication) 1055 fatal_f("kbd-int authentication not enabled"); 1056 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts, 1057 &prompts, &echo_on) < 0 ? 0 : 1; 1058 1059 sshbuf_reset(m); 1060 if ((r = sshbuf_put_u32(m, success)) != 0) 1061 fatal_fr(r, "assemble"); 1062 if (success) { 1063 if ((r = sshbuf_put_cstring(m, prompts[0])) != 0) 1064 fatal_fr(r, "assemble prompt"); 1065 } 1066 1067 debug3_f("sending challenge success: %u", success); 1068 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m); 1069 1070 if (success) { 1071 free(name); 1072 free(infotxt); 1073 free(prompts); 1074 free(echo_on); 1075 } 1076 1077 return (0); 1078 } 1079 1080 int 1081 mm_answer_bsdauthrespond(struct ssh *ssh, int sock, struct sshbuf *m) 1082 { 1083 char *response; 1084 int r, authok; 1085 1086 if (!options.kbd_interactive_authentication) 1087 fatal_f("kbd-int authentication not enabled"); 1088 if (authctxt->as == NULL) 1089 fatal_f("no bsd auth session"); 1090 1091 if ((r = sshbuf_get_cstring(m, &response, NULL)) != 0) 1092 fatal_fr(r, "parse"); 1093 authok = options.kbd_interactive_authentication && 1094 auth_userresponse(authctxt->as, response, 0); 1095 authctxt->as = NULL; 1096 debug3_f("<%s> = <%d>", response, authok); 1097 free(response); 1098 1099 sshbuf_reset(m); 1100 if ((r = sshbuf_put_u32(m, authok)) != 0) 1101 fatal_fr(r, "assemble"); 1102 1103 debug3_f("sending authenticated: %d", authok); 1104 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m); 1105 1106 auth_method = "keyboard-interactive"; 1107 auth_submethod = "bsdauth"; 1108 1109 return (authok != 0); 1110 } 1111 #endif 1112 1113 #ifdef USE_PAM 1114 int 1115 mm_answer_pam_start(struct ssh *ssh, int sock, struct sshbuf *m) 1116 { 1117 if (!options.use_pam) 1118 fatal_f("UsePAM not set, but ended up in %s anyway", __func__); 1119 1120 start_pam(ssh); 1121 1122 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1); 1123 if (options.kbd_interactive_authentication) 1124 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1); 1125 1126 return (0); 1127 } 1128 1129 int 1130 mm_answer_pam_account(struct ssh *ssh, int sock, struct sshbuf *m) 1131 { 1132 u_int ret; 1133 int r; 1134 1135 if (!options.use_pam) 1136 fatal_f("PAM not enabled"); 1137 1138 ret = do_pam_account(); 1139 1140 if ((r = sshbuf_put_u32(m, ret)) != 0 || 1141 (r = sshbuf_put_stringb(m, loginmsg)) != 0) 1142 fatal_fr(r, "buffer error"); 1143 sshbuf_reset(loginmsg); 1144 1145 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m); 1146 1147 return (ret); 1148 } 1149 1150 static void *sshpam_ctxt, *sshpam_authok; 1151 extern KbdintDevice sshpam_device; 1152 1153 int 1154 mm_answer_pam_init_ctx(struct ssh *ssh, int sock, struct sshbuf *m) 1155 { 1156 u_int ok = 0; 1157 int r; 1158 1159 debug3_f("entering"); 1160 if (!options.kbd_interactive_authentication) 1161 fatal_f("kbd-int authentication not enabled"); 1162 if (sshpam_ctxt != NULL) 1163 fatal_f("already called"); 1164 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt); 1165 sshpam_authok = NULL; 1166 sshbuf_reset(m); 1167 if (sshpam_ctxt != NULL) { 1168 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1); 1169 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1); 1170 ok = 1; 1171 } 1172 if ((r = sshbuf_put_u32(m, ok)) != 0) 1173 fatal_fr(r, "buffer error"); 1174 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m); 1175 return (0); 1176 } 1177 1178 int 1179 mm_answer_pam_query(struct ssh *ssh, int sock, struct sshbuf *m) 1180 { 1181 char *name = NULL, *info = NULL, **prompts = NULL; 1182 u_int i, num = 0, *echo_on = 0; 1183 int r, ret; 1184 1185 debug3_f("entering"); 1186 sshpam_authok = NULL; 1187 if (sshpam_ctxt == NULL) 1188 fatal_f("no context"); 1189 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, 1190 &num, &prompts, &echo_on); 1191 if (ret == 0 && num == 0 && sshpam_priv_kbdint_authdone(sshpam_ctxt)) 1192 sshpam_authok = sshpam_ctxt; 1193 if (num > 1 || name == NULL || info == NULL) 1194 fatal("sshpam_device.query failed"); 1195 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1); 1196 sshbuf_reset(m); 1197 if ((r = sshbuf_put_u32(m, ret)) != 0 || 1198 (r = sshbuf_put_cstring(m, name)) != 0 || 1199 (r = sshbuf_put_cstring(m, info)) != 0 || 1200 (r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0 || 1201 (r = sshbuf_put_u32(m, num)) != 0) 1202 fatal_fr(r, "buffer error"); 1203 free(name); 1204 free(info); 1205 for (i = 0; i < num; ++i) { 1206 if ((r = sshbuf_put_cstring(m, prompts[i])) != 0 || 1207 (r = sshbuf_put_u32(m, echo_on[i])) != 0) 1208 fatal_fr(r, "buffer error"); 1209 free(prompts[i]); 1210 } 1211 free(prompts); 1212 free(echo_on); 1213 auth_method = "keyboard-interactive"; 1214 auth_submethod = "pam"; 1215 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m); 1216 return (0); 1217 } 1218 1219 int 1220 mm_answer_pam_respond(struct ssh *ssh, int sock, struct sshbuf *m) 1221 { 1222 char **resp; 1223 u_int i, num; 1224 int r, ret; 1225 1226 debug3_f("entering"); 1227 if (sshpam_ctxt == NULL) 1228 fatal_f("no context"); 1229 sshpam_authok = NULL; 1230 if ((r = sshbuf_get_u32(m, &num)) != 0) 1231 fatal_fr(r, "buffer error"); 1232 if (num > PAM_MAX_NUM_MSG) { 1233 fatal_f("Too many PAM messages, got %u, expected <= %u", 1234 num, (unsigned)PAM_MAX_NUM_MSG); 1235 } 1236 if (num > 0) { 1237 resp = xcalloc(num, sizeof(char *)); 1238 for (i = 0; i < num; ++i) { 1239 if ((r = sshbuf_get_cstring(m, &(resp[i]), NULL)) != 0) 1240 fatal("%s: buffer error: %s", 1241 __func__, ssh_err(r)); 1242 } 1243 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp); 1244 for (i = 0; i < num; ++i) 1245 free(resp[i]); 1246 free(resp); 1247 } else { 1248 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL); 1249 } 1250 sshbuf_reset(m); 1251 if ((r = sshbuf_put_u32(m, ret)) != 0) 1252 fatal_fr(r, "buffer error"); 1253 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m); 1254 auth_method = "keyboard-interactive"; 1255 auth_submethod = "pam"; 1256 if (ret == 0) 1257 sshpam_authok = sshpam_ctxt; 1258 return (0); 1259 } 1260 1261 int 1262 mm_answer_pam_free_ctx(struct ssh *ssh, int sock, struct sshbuf *m) 1263 { 1264 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt; 1265 1266 debug3_f("entering"); 1267 if (sshpam_ctxt == NULL) 1268 fatal_f("no context"); 1269 (sshpam_device.free_ctx)(sshpam_ctxt); 1270 sshpam_ctxt = sshpam_authok = NULL; 1271 sshbuf_reset(m); 1272 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m); 1273 /* Allow another attempt */ 1274 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1); 1275 auth_method = "keyboard-interactive"; 1276 auth_submethod = "pam"; 1277 return r; 1278 } 1279 #endif 1280 1281 int 1282 mm_answer_keyallowed(struct ssh *ssh, int sock, struct sshbuf *m) 1283 { 1284 struct sshkey *key = NULL; 1285 char *cuser, *chost; 1286 u_int pubkey_auth_attempt; 1287 u_int type = 0; 1288 int r, allowed = 0; 1289 struct sshauthopt *opts = NULL; 1290 1291 debug3_f("entering"); 1292 if ((r = sshbuf_get_u32(m, &type)) != 0 || 1293 (r = sshbuf_get_cstring(m, &cuser, NULL)) != 0 || 1294 (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 || 1295 (r = sshkey_froms(m, &key)) != 0 || 1296 (r = sshbuf_get_u32(m, &pubkey_auth_attempt)) != 0) 1297 fatal_fr(r, "parse"); 1298 1299 if (key != NULL && authctxt->valid) { 1300 switch (type) { 1301 case MM_USERKEY: 1302 auth_method = "publickey"; 1303 if (!options.pubkey_authentication) 1304 break; 1305 if (auth2_key_already_used(authctxt, key)) 1306 break; 1307 if (!key_base_type_match(auth_method, key, 1308 options.pubkey_accepted_algos)) 1309 break; 1310 allowed = user_key_allowed(ssh, authctxt->pw, key, 1311 pubkey_auth_attempt, &opts); 1312 break; 1313 case MM_HOSTKEY: 1314 auth_method = "hostbased"; 1315 if (!options.hostbased_authentication) 1316 break; 1317 if (auth2_key_already_used(authctxt, key)) 1318 break; 1319 if (!key_base_type_match(auth_method, key, 1320 options.hostbased_accepted_algos)) 1321 break; 1322 allowed = hostbased_key_allowed(ssh, authctxt->pw, 1323 cuser, chost, key); 1324 auth2_record_info(authctxt, 1325 "client user \"%.100s\", client host \"%.100s\"", 1326 cuser, chost); 1327 break; 1328 default: 1329 fatal_f("unknown key type %u", type); 1330 break; 1331 } 1332 } 1333 1334 debug3_f("%s authentication%s: %s key is %s", auth_method, 1335 pubkey_auth_attempt ? "" : " test", 1336 (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key), 1337 allowed ? "allowed" : "not allowed"); 1338 1339 auth2_record_key(authctxt, 0, key); 1340 1341 /* clear temporarily storage (used by verify) */ 1342 monitor_reset_key_state(); 1343 1344 if (allowed) { 1345 /* Save temporarily for comparison in verify */ 1346 if ((r = sshkey_to_blob(key, &key_blob, &key_bloblen)) != 0) 1347 fatal_fr(r, "sshkey_to_blob"); 1348 key_blobtype = type; 1349 key_opts = opts; 1350 hostbased_cuser = cuser; 1351 hostbased_chost = chost; 1352 } else { 1353 /* Log failed attempt */ 1354 auth_log(ssh, 0, 0, auth_method, NULL); 1355 free(cuser); 1356 free(chost); 1357 } 1358 sshkey_free(key); 1359 1360 sshbuf_reset(m); 1361 if ((r = sshbuf_put_u32(m, allowed)) != 0) 1362 fatal_fr(r, "assemble"); 1363 if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0) 1364 fatal_fr(r, "sshauthopt_serialise"); 1365 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m); 1366 1367 if (!allowed) 1368 sshauthopt_free(opts); 1369 1370 return (0); 1371 } 1372 1373 static int 1374 monitor_valid_userblob(struct ssh *ssh, const u_char *data, u_int datalen) 1375 { 1376 struct sshbuf *b; 1377 struct sshkey *hostkey = NULL; 1378 const u_char *p; 1379 char *userstyle, *cp; 1380 size_t len; 1381 u_char type; 1382 int hostbound = 0, r, fail = 0; 1383 1384 if ((b = sshbuf_from(data, datalen)) == NULL) 1385 fatal_f("sshbuf_from"); 1386 1387 if (ssh->compat & SSH_OLD_SESSIONID) { 1388 p = sshbuf_ptr(b); 1389 len = sshbuf_len(b); 1390 if ((session_id2 == NULL) || 1391 (len < session_id2_len) || 1392 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) 1393 fail++; 1394 if ((r = sshbuf_consume(b, session_id2_len)) != 0) 1395 fatal_fr(r, "consume"); 1396 } else { 1397 if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0) 1398 fatal_fr(r, "parse sessionid"); 1399 if ((session_id2 == NULL) || 1400 (len != session_id2_len) || 1401 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) 1402 fail++; 1403 } 1404 if ((r = sshbuf_get_u8(b, &type)) != 0) 1405 fatal_fr(r, "parse type"); 1406 if (type != SSH2_MSG_USERAUTH_REQUEST) 1407 fail++; 1408 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0) 1409 fatal_fr(r, "parse userstyle"); 1410 xasprintf(&userstyle, "%s%s%s", authctxt->user, 1411 authctxt->style ? ":" : "", 1412 authctxt->style ? authctxt->style : ""); 1413 if (strcmp(userstyle, cp) != 0) { 1414 logit("wrong user name passed to monitor: " 1415 "expected %s != %.100s", userstyle, cp); 1416 fail++; 1417 } 1418 free(userstyle); 1419 free(cp); 1420 if ((r = sshbuf_skip_string(b)) != 0 || /* service */ 1421 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0) 1422 fatal_fr(r, "parse method"); 1423 if (strcmp("publickey", cp) != 0) { 1424 if (strcmp("publickey-hostbound-v00@openssh.com", cp) == 0) 1425 hostbound = 1; 1426 else 1427 fail++; 1428 } 1429 free(cp); 1430 if ((r = sshbuf_get_u8(b, &type)) != 0) 1431 fatal_fr(r, "parse pktype"); 1432 if (type == 0) 1433 fail++; 1434 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */ 1435 (r = sshbuf_skip_string(b)) != 0 || /* pkblob */ 1436 (hostbound && (r = sshkey_froms(b, &hostkey)) != 0)) 1437 fatal_fr(r, "parse pk"); 1438 if (sshbuf_len(b) != 0) 1439 fail++; 1440 sshbuf_free(b); 1441 if (hostkey != NULL) { 1442 /* 1443 * Ensure this is actually one of our hostkeys; unfortunately 1444 * can't check ssh->kex->initial_hostkey directly at this point 1445 * as packet state has not yet been exported to monitor. 1446 */ 1447 if (get_hostkey_index(hostkey, 1, ssh) == -1) 1448 fatal_f("hostbound hostkey does not match"); 1449 sshkey_free(hostkey); 1450 } 1451 return (fail == 0); 1452 } 1453 1454 static int 1455 monitor_valid_hostbasedblob(const u_char *data, u_int datalen, 1456 const char *cuser, const char *chost) 1457 { 1458 struct sshbuf *b; 1459 const u_char *p; 1460 char *cp, *userstyle; 1461 size_t len; 1462 int r, fail = 0; 1463 u_char type; 1464 1465 if ((b = sshbuf_from(data, datalen)) == NULL) 1466 fatal_f("sshbuf_new"); 1467 if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0) 1468 fatal_fr(r, "parse sessionid"); 1469 1470 if ((session_id2 == NULL) || 1471 (len != session_id2_len) || 1472 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) 1473 fail++; 1474 1475 if ((r = sshbuf_get_u8(b, &type)) != 0) 1476 fatal_fr(r, "parse type"); 1477 if (type != SSH2_MSG_USERAUTH_REQUEST) 1478 fail++; 1479 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0) 1480 fatal_fr(r, "parse userstyle"); 1481 xasprintf(&userstyle, "%s%s%s", authctxt->user, 1482 authctxt->style ? ":" : "", 1483 authctxt->style ? authctxt->style : ""); 1484 if (strcmp(userstyle, cp) != 0) { 1485 logit("wrong user name passed to monitor: " 1486 "expected %s != %.100s", userstyle, cp); 1487 fail++; 1488 } 1489 free(userstyle); 1490 free(cp); 1491 if ((r = sshbuf_skip_string(b)) != 0 || /* service */ 1492 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0) 1493 fatal_fr(r, "parse method"); 1494 if (strcmp(cp, "hostbased") != 0) 1495 fail++; 1496 free(cp); 1497 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */ 1498 (r = sshbuf_skip_string(b)) != 0) /* pkblob */ 1499 fatal_fr(r, "parse pk"); 1500 1501 /* verify client host, strip trailing dot if necessary */ 1502 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0) 1503 fatal_fr(r, "parse host"); 1504 if (((len = strlen(cp)) > 0) && cp[len - 1] == '.') 1505 cp[len - 1] = '\0'; 1506 if (strcmp(cp, chost) != 0) 1507 fail++; 1508 free(cp); 1509 1510 /* verify client user */ 1511 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0) 1512 fatal_fr(r, "parse ruser"); 1513 if (strcmp(cp, cuser) != 0) 1514 fail++; 1515 free(cp); 1516 1517 if (sshbuf_len(b) != 0) 1518 fail++; 1519 sshbuf_free(b); 1520 return (fail == 0); 1521 } 1522 1523 int 1524 mm_answer_keyverify(struct ssh *ssh, int sock, struct sshbuf *m) 1525 { 1526 struct sshkey *key; 1527 const u_char *signature, *data, *blob; 1528 char *sigalg = NULL, *fp = NULL; 1529 size_t signaturelen, datalen, bloblen; 1530 int r, ret, req_presence = 0, req_verify = 0, valid_data = 0; 1531 int encoded_ret; 1532 struct sshkey_sig_details *sig_details = NULL; 1533 1534 if ((r = sshbuf_get_string_direct(m, &blob, &bloblen)) != 0 || 1535 (r = sshbuf_get_string_direct(m, &signature, &signaturelen)) != 0 || 1536 (r = sshbuf_get_string_direct(m, &data, &datalen)) != 0 || 1537 (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0) 1538 fatal_fr(r, "parse"); 1539 1540 if (hostbased_cuser == NULL || hostbased_chost == NULL || 1541 !monitor_allowed_key(blob, bloblen)) 1542 fatal_f("bad key, not previously allowed"); 1543 1544 /* Empty signature algorithm means NULL. */ 1545 if (*sigalg == '\0') { 1546 free(sigalg); 1547 sigalg = NULL; 1548 } 1549 1550 /* XXX use sshkey_froms here; need to change key_blob, etc. */ 1551 if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0) 1552 fatal_fr(r, "parse key"); 1553 1554 switch (key_blobtype) { 1555 case MM_USERKEY: 1556 valid_data = monitor_valid_userblob(ssh, data, datalen); 1557 auth_method = "publickey"; 1558 break; 1559 case MM_HOSTKEY: 1560 valid_data = monitor_valid_hostbasedblob(data, datalen, 1561 hostbased_cuser, hostbased_chost); 1562 auth_method = "hostbased"; 1563 break; 1564 default: 1565 valid_data = 0; 1566 break; 1567 } 1568 if (!valid_data) 1569 fatal_f("bad %s signature data blob", 1570 key_blobtype == MM_USERKEY ? "userkey" : 1571 (key_blobtype == MM_HOSTKEY ? "hostkey" : "unknown")); 1572 1573 if ((fp = sshkey_fingerprint(key, options.fingerprint_hash, 1574 SSH_FP_DEFAULT)) == NULL) 1575 fatal_f("sshkey_fingerprint failed"); 1576 1577 ret = sshkey_verify(key, signature, signaturelen, data, datalen, 1578 sigalg, ssh->compat, &sig_details); 1579 debug3_f("%s %s signature using %s %s%s%s", auth_method, 1580 sshkey_type(key), sigalg == NULL ? "default" : sigalg, 1581 (ret == 0) ? "verified" : "unverified", 1582 (ret != 0) ? ": " : "", (ret != 0) ? ssh_err(ret) : ""); 1583 1584 if (ret == 0 && key_blobtype == MM_USERKEY && sig_details != NULL) { 1585 req_presence = (options.pubkey_auth_options & 1586 PUBKEYAUTH_TOUCH_REQUIRED) || 1587 !key_opts->no_require_user_presence; 1588 if (req_presence && 1589 (sig_details->sk_flags & SSH_SK_USER_PRESENCE_REQD) == 0) { 1590 error("public key %s %s signature for %s%s from %.128s " 1591 "port %d rejected: user presence " 1592 "(authenticator touch) requirement not met ", 1593 sshkey_type(key), fp, 1594 authctxt->valid ? "" : "invalid user ", 1595 authctxt->user, ssh_remote_ipaddr(ssh), 1596 ssh_remote_port(ssh)); 1597 ret = SSH_ERR_SIGNATURE_INVALID; 1598 } 1599 req_verify = (options.pubkey_auth_options & 1600 PUBKEYAUTH_VERIFY_REQUIRED) || key_opts->require_verify; 1601 if (req_verify && 1602 (sig_details->sk_flags & SSH_SK_USER_VERIFICATION_REQD) == 0) { 1603 error("public key %s %s signature for %s%s from %.128s " 1604 "port %d rejected: user verification requirement " 1605 "not met ", sshkey_type(key), fp, 1606 authctxt->valid ? "" : "invalid user ", 1607 authctxt->user, ssh_remote_ipaddr(ssh), 1608 ssh_remote_port(ssh)); 1609 ret = SSH_ERR_SIGNATURE_INVALID; 1610 } 1611 } 1612 auth2_record_key(authctxt, ret == 0, key); 1613 1614 if (key_blobtype == MM_USERKEY && ret == 0) 1615 auth_activate_options(ssh, key_opts); 1616 monitor_reset_key_state(); 1617 1618 sshbuf_reset(m); 1619 1620 /* encode ret != 0 as positive integer, since we're sending u32 */ 1621 encoded_ret = (ret != 0); 1622 if ((r = sshbuf_put_u32(m, encoded_ret)) != 0 || 1623 (r = sshbuf_put_u8(m, sig_details != NULL)) != 0) 1624 fatal_fr(r, "assemble"); 1625 if (sig_details != NULL) { 1626 if ((r = sshbuf_put_u32(m, sig_details->sk_counter)) != 0 || 1627 (r = sshbuf_put_u8(m, sig_details->sk_flags)) != 0) 1628 fatal_fr(r, "assemble sk"); 1629 } 1630 sshkey_sig_details_free(sig_details); 1631 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m); 1632 1633 free(sigalg); 1634 free(fp); 1635 sshkey_free(key); 1636 1637 return ret == 0; 1638 } 1639 1640 static void 1641 mm_record_login(struct ssh *ssh, Session *s, struct passwd *pw) 1642 { 1643 socklen_t fromlen; 1644 struct sockaddr_storage from; 1645 1646 /* 1647 * Get IP address of client. If the connection is not a socket, let 1648 * the address be 0.0.0.0. 1649 */ 1650 memset(&from, 0, sizeof(from)); 1651 fromlen = sizeof(from); 1652 if (ssh_packet_connection_is_on_socket(ssh)) { 1653 if (getpeername(ssh_packet_get_connection_in(ssh), 1654 (struct sockaddr *)&from, &fromlen) == -1) { 1655 debug("getpeername: %.100s", strerror(errno)); 1656 cleanup_exit(255); 1657 } 1658 } 1659 /* Record that there was a login on that tty from the remote host. */ 1660 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid, 1661 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns), 1662 (struct sockaddr *)&from, fromlen); 1663 } 1664 1665 static void 1666 mm_session_close(Session *s) 1667 { 1668 debug3_f("session %d pid %ld", s->self, (long)s->pid); 1669 if (s->ttyfd != -1) { 1670 debug3_f("tty %s ptyfd %d", s->tty, s->ptyfd); 1671 session_pty_cleanup2(s); 1672 } 1673 session_unused(s->self); 1674 } 1675 1676 int 1677 mm_answer_pty(struct ssh *ssh, int sock, struct sshbuf *m) 1678 { 1679 extern struct monitor *pmonitor; 1680 Session *s; 1681 int r, res, fd0; 1682 1683 debug3_f("entering"); 1684 1685 sshbuf_reset(m); 1686 s = session_new(); 1687 if (s == NULL) 1688 goto error; 1689 s->authctxt = authctxt; 1690 s->pw = authctxt->pw; 1691 s->pid = pmonitor->m_pid; 1692 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)); 1693 if (res == 0) 1694 goto error; 1695 pty_setowner(authctxt->pw, s->tty); 1696 1697 if ((r = sshbuf_put_u32(m, 1)) != 0 || 1698 (r = sshbuf_put_cstring(m, s->tty)) != 0) 1699 fatal_fr(r, "assemble"); 1700 1701 /* We need to trick ttyslot */ 1702 if (dup2(s->ttyfd, 0) == -1) 1703 fatal_f("dup2"); 1704 1705 mm_record_login(ssh, s, authctxt->pw); 1706 1707 /* Now we can close the file descriptor again */ 1708 close(0); 1709 1710 /* send messages generated by record_login */ 1711 if ((r = sshbuf_put_stringb(m, loginmsg)) != 0) 1712 fatal_fr(r, "assemble loginmsg"); 1713 sshbuf_reset(loginmsg); 1714 1715 mm_request_send(sock, MONITOR_ANS_PTY, m); 1716 1717 if (mm_send_fd(sock, s->ptyfd) == -1 || 1718 mm_send_fd(sock, s->ttyfd) == -1) 1719 fatal_f("send fds failed"); 1720 1721 /* make sure nothing uses fd 0 */ 1722 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) == -1) 1723 fatal_f("open(/dev/null): %s", strerror(errno)); 1724 if (fd0 != 0) 1725 error_f("fd0 %d != 0", fd0); 1726 1727 /* slave side of pty is not needed */ 1728 close(s->ttyfd); 1729 s->ttyfd = s->ptyfd; 1730 /* no need to dup() because nobody closes ptyfd */ 1731 s->ptymaster = s->ptyfd; 1732 1733 debug3_f("tty %s ptyfd %d", s->tty, s->ttyfd); 1734 1735 return (0); 1736 1737 error: 1738 if (s != NULL) 1739 mm_session_close(s); 1740 if ((r = sshbuf_put_u32(m, 0)) != 0) 1741 fatal_fr(r, "assemble 0"); 1742 mm_request_send(sock, MONITOR_ANS_PTY, m); 1743 return (0); 1744 } 1745 1746 int 1747 mm_answer_pty_cleanup(struct ssh *ssh, int sock, struct sshbuf *m) 1748 { 1749 Session *s; 1750 char *tty; 1751 int r; 1752 1753 debug3_f("entering"); 1754 1755 if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0) 1756 fatal_fr(r, "parse tty"); 1757 if ((s = session_by_tty(tty)) != NULL) 1758 mm_session_close(s); 1759 sshbuf_reset(m); 1760 free(tty); 1761 return (0); 1762 } 1763 1764 int 1765 mm_answer_term(struct ssh *ssh, int sock, struct sshbuf *req) 1766 { 1767 extern struct monitor *pmonitor; 1768 int res, status; 1769 1770 debug3_f("tearing down sessions"); 1771 1772 /* The child is terminating */ 1773 session_destroy_all(ssh, &mm_session_close); 1774 1775 #ifdef USE_PAM 1776 if (options.use_pam) 1777 sshpam_cleanup(); 1778 #endif 1779 1780 while (waitpid(pmonitor->m_pid, &status, 0) == -1) 1781 if (errno != EINTR) 1782 exit(1); 1783 1784 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1; 1785 1786 /* Terminate process */ 1787 exit(res); 1788 } 1789 1790 #ifdef SSH_AUDIT_EVENTS 1791 /* Report that an audit event occurred */ 1792 int 1793 mm_answer_audit_event(struct ssh *ssh, int socket, struct sshbuf *m) 1794 { 1795 u_int n; 1796 ssh_audit_event_t event; 1797 int r; 1798 1799 debug3_f("entering"); 1800 1801 if ((r = sshbuf_get_u32(m, &n)) != 0) 1802 fatal_fr(r, "buffer error"); 1803 event = (ssh_audit_event_t)n; 1804 switch (event) { 1805 case SSH_AUTH_FAIL_PUBKEY: 1806 case SSH_AUTH_FAIL_HOSTBASED: 1807 case SSH_AUTH_FAIL_GSSAPI: 1808 case SSH_LOGIN_EXCEED_MAXTRIES: 1809 case SSH_LOGIN_ROOT_DENIED: 1810 case SSH_CONNECTION_CLOSE: 1811 case SSH_INVALID_USER: 1812 audit_event(ssh, event); 1813 break; 1814 default: 1815 fatal("Audit event type %d not permitted", event); 1816 } 1817 1818 return (0); 1819 } 1820 1821 int 1822 mm_answer_audit_command(struct ssh *ssh, int socket, struct sshbuf *m) 1823 { 1824 char *cmd; 1825 int r; 1826 1827 debug3_f("entering"); 1828 if ((r = sshbuf_get_cstring(m, &cmd, NULL)) != 0) 1829 fatal_fr(r, "buffer error"); 1830 /* sanity check command, if so how? */ 1831 audit_run_command(cmd); 1832 free(cmd); 1833 return (0); 1834 } 1835 #endif /* SSH_AUDIT_EVENTS */ 1836 1837 void 1838 monitor_clear_keystate(struct ssh *ssh, struct monitor *pmonitor) 1839 { 1840 ssh_clear_newkeys(ssh, MODE_IN); 1841 ssh_clear_newkeys(ssh, MODE_OUT); 1842 sshbuf_free(child_state); 1843 child_state = NULL; 1844 } 1845 1846 void 1847 monitor_apply_keystate(struct ssh *ssh, struct monitor *pmonitor) 1848 { 1849 struct kex *kex; 1850 int r; 1851 1852 debug3_f("packet_set_state"); 1853 if ((r = ssh_packet_set_state(ssh, child_state)) != 0) 1854 fatal_fr(r, "packet_set_state"); 1855 sshbuf_free(child_state); 1856 child_state = NULL; 1857 if ((kex = ssh->kex) == NULL) 1858 fatal_f("internal error: ssh->kex == NULL"); 1859 if (session_id2_len != sshbuf_len(ssh->kex->session_id)) { 1860 fatal_f("incorrect session id length %zu (expected %u)", 1861 sshbuf_len(ssh->kex->session_id), session_id2_len); 1862 } 1863 if (memcmp(sshbuf_ptr(ssh->kex->session_id), session_id2, 1864 session_id2_len) != 0) 1865 fatal_f("session ID mismatch"); 1866 /* XXX set callbacks */ 1867 #ifdef WITH_OPENSSL 1868 kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_server; 1869 kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_server; 1870 kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_server; 1871 kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_server; 1872 kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_server; 1873 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server; 1874 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server; 1875 # ifdef OPENSSL_HAS_ECC 1876 kex->kex[KEX_ECDH_SHA2] = kex_gen_server; 1877 # endif 1878 #endif /* WITH_OPENSSL */ 1879 kex->kex[KEX_C25519_SHA256] = kex_gen_server; 1880 kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_server; 1881 kex->kex[KEX_KEM_MLKEM768X25519_SHA256] = kex_gen_server; 1882 kex->load_host_public_key=&get_hostkey_public_by_type; 1883 kex->load_host_private_key=&get_hostkey_private_by_type; 1884 kex->host_key_index=&get_hostkey_index; 1885 kex->sign = sshd_hostkey_sign; 1886 } 1887 1888 /* This function requires careful sanity checking */ 1889 1890 void 1891 mm_get_keystate(struct ssh *ssh, struct monitor *pmonitor) 1892 { 1893 debug3_f("Waiting for new keys"); 1894 1895 if ((child_state = sshbuf_new()) == NULL) 1896 fatal_f("sshbuf_new failed"); 1897 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, 1898 child_state); 1899 debug3_f("GOT new keys"); 1900 } 1901 1902 1903 /* XXX */ 1904 1905 static void 1906 monitor_openfds(struct monitor *mon, int do_logfds) 1907 { 1908 int pair[2]; 1909 #ifdef SO_ZEROIZE 1910 int on = 1; 1911 #endif 1912 1913 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) 1914 fatal_f("socketpair: %s", strerror(errno)); 1915 #ifdef SO_ZEROIZE 1916 if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) == -1) 1917 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno)); 1918 if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) == -1) 1919 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno)); 1920 #endif 1921 FD_CLOSEONEXEC(pair[0]); 1922 FD_CLOSEONEXEC(pair[1]); 1923 mon->m_recvfd = pair[0]; 1924 mon->m_sendfd = pair[1]; 1925 1926 if (do_logfds) { 1927 if (pipe(pair) == -1) 1928 fatal_f("pipe: %s", strerror(errno)); 1929 FD_CLOSEONEXEC(pair[0]); 1930 FD_CLOSEONEXEC(pair[1]); 1931 mon->m_log_recvfd = pair[0]; 1932 mon->m_log_sendfd = pair[1]; 1933 } else 1934 mon->m_log_recvfd = mon->m_log_sendfd = -1; 1935 } 1936 1937 struct monitor * 1938 monitor_init(void) 1939 { 1940 struct monitor *mon; 1941 1942 mon = xcalloc(1, sizeof(*mon)); 1943 monitor_openfds(mon, 1); 1944 1945 return mon; 1946 } 1947 1948 void 1949 monitor_reinit(struct monitor *mon) 1950 { 1951 monitor_openfds(mon, 0); 1952 } 1953 1954 int 1955 monitor_auth_attempted(void) 1956 { 1957 return auth_attempted; 1958 } 1959 1960 int 1961 monitor_invalid_user(void) 1962 { 1963 return invalid_user; 1964 } 1965 1966 #ifdef GSSAPI 1967 int 1968 mm_answer_gss_setup_ctx(struct ssh *ssh, int sock, struct sshbuf *m) 1969 { 1970 gss_OID_desc goid; 1971 OM_uint32 major; 1972 size_t len; 1973 u_char *p; 1974 int r; 1975 1976 if (!options.gss_authentication) 1977 fatal_f("GSSAPI authentication not enabled"); 1978 1979 if ((r = sshbuf_get_string(m, &p, &len)) != 0) 1980 fatal_fr(r, "parse"); 1981 goid.elements = p; 1982 goid.length = len; 1983 1984 major = ssh_gssapi_server_ctx(&gsscontext, &goid); 1985 1986 free(goid.elements); 1987 1988 sshbuf_reset(m); 1989 if ((r = sshbuf_put_u32(m, major)) != 0) 1990 fatal_fr(r, "assemble"); 1991 1992 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m); 1993 1994 /* Now we have a context, enable the step */ 1995 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1); 1996 1997 return (0); 1998 } 1999 2000 int 2001 mm_answer_gss_accept_ctx(struct ssh *ssh, int sock, struct sshbuf *m) 2002 { 2003 gss_buffer_desc in; 2004 gss_buffer_desc out = GSS_C_EMPTY_BUFFER; 2005 OM_uint32 major, minor; 2006 OM_uint32 flags = 0; /* GSI needs this */ 2007 int r; 2008 2009 if (!options.gss_authentication) 2010 fatal_f("GSSAPI authentication not enabled"); 2011 2012 if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0) 2013 fatal_fr(r, "ssh_gssapi_get_buffer_desc"); 2014 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags); 2015 free(in.value); 2016 2017 sshbuf_reset(m); 2018 if ((r = sshbuf_put_u32(m, major)) != 0 || 2019 (r = sshbuf_put_string(m, out.value, out.length)) != 0 || 2020 (r = sshbuf_put_u32(m, flags)) != 0) 2021 fatal_fr(r, "assemble"); 2022 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m); 2023 2024 gss_release_buffer(&minor, &out); 2025 2026 if (major == GSS_S_COMPLETE) { 2027 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0); 2028 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1); 2029 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1); 2030 } 2031 return (0); 2032 } 2033 2034 int 2035 mm_answer_gss_checkmic(struct ssh *ssh, int sock, struct sshbuf *m) 2036 { 2037 gss_buffer_desc gssbuf, mic; 2038 OM_uint32 ret; 2039 int r; 2040 2041 if (!options.gss_authentication) 2042 fatal_f("GSSAPI authentication not enabled"); 2043 2044 if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 || 2045 (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0) 2046 fatal_fr(r, "ssh_gssapi_get_buffer_desc"); 2047 2048 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic); 2049 2050 free(gssbuf.value); 2051 free(mic.value); 2052 2053 sshbuf_reset(m); 2054 if ((r = sshbuf_put_u32(m, ret)) != 0) 2055 fatal_fr(r, "assemble"); 2056 2057 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m); 2058 2059 if (!GSS_ERROR(ret)) 2060 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1); 2061 2062 return (0); 2063 } 2064 2065 int 2066 mm_answer_gss_userok(struct ssh *ssh, int sock, struct sshbuf *m) 2067 { 2068 int r, authenticated; 2069 const char *displayname; 2070 2071 if (!options.gss_authentication) 2072 fatal_f("GSSAPI authentication not enabled"); 2073 2074 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user); 2075 2076 sshbuf_reset(m); 2077 if ((r = sshbuf_put_u32(m, authenticated)) != 0) 2078 fatal_fr(r, "assemble"); 2079 2080 debug3_f("sending result %d", authenticated); 2081 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m); 2082 2083 auth_method = "gssapi-with-mic"; 2084 2085 if ((displayname = ssh_gssapi_displayname()) != NULL) 2086 auth2_record_info(authctxt, "%s", displayname); 2087 2088 /* Monitor loop will terminate if authenticated */ 2089 return (authenticated); 2090 } 2091 #endif /* GSSAPI */ 2092 2093