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