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