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