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