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