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