1 /* $OpenBSD: monitor_wrap.c,v 1.123 2021/04/15 16:24:31 markus Exp $ */ 2 /* 3 * Copyright 2002 Niels Provos <provos@citi.umich.edu> 4 * Copyright 2002 Markus Friedl <markus@openbsd.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include "includes.h" 29 30 #include <sys/types.h> 31 #include <sys/uio.h> 32 33 #include <errno.h> 34 #include <pwd.h> 35 #include <signal.h> 36 #include <stdarg.h> 37 #include <stdio.h> 38 #include <string.h> 39 #include <unistd.h> 40 41 #ifdef WITH_OPENSSL 42 #include <openssl/bn.h> 43 #include <openssl/dh.h> 44 #include <openssl/evp.h> 45 #endif 46 47 #include "openbsd-compat/sys-queue.h" 48 #include "xmalloc.h" 49 #include "ssh.h" 50 #ifdef WITH_OPENSSL 51 #include "dh.h" 52 #endif 53 #include "sshbuf.h" 54 #include "sshkey.h" 55 #include "cipher.h" 56 #include "kex.h" 57 #include "hostfile.h" 58 #include "auth.h" 59 #include "auth-options.h" 60 #include "packet.h" 61 #include "mac.h" 62 #include "log.h" 63 #include "auth-pam.h" 64 #include "monitor.h" 65 #ifdef GSSAPI 66 #include "ssh-gss.h" 67 #endif 68 #include "monitor_wrap.h" 69 #include "atomicio.h" 70 #include "monitor_fdpass.h" 71 #include "misc.h" 72 73 #include "channels.h" 74 #include "session.h" 75 #include "servconf.h" 76 77 #include "ssherr.h" 78 79 /* Imports */ 80 extern struct monitor *pmonitor; 81 extern struct sshbuf *loginmsg; 82 extern ServerOptions options; 83 84 void 85 mm_log_handler(LogLevel level, int forced, const char *msg, void *ctx) 86 { 87 struct sshbuf *log_msg; 88 struct monitor *mon = (struct monitor *)ctx; 89 int r; 90 size_t len; 91 92 if (mon->m_log_sendfd == -1) 93 fatal_f("no log channel"); 94 95 if ((log_msg = sshbuf_new()) == NULL) 96 fatal_f("sshbuf_new failed"); 97 98 if ((r = sshbuf_put_u32(log_msg, 0)) != 0 || /* length; filled below */ 99 (r = sshbuf_put_u32(log_msg, level)) != 0 || 100 (r = sshbuf_put_u32(log_msg, forced)) != 0 || 101 (r = sshbuf_put_cstring(log_msg, msg)) != 0) 102 fatal_fr(r, "assemble"); 103 if ((len = sshbuf_len(log_msg)) < 4 || len > 0xffffffff) 104 fatal_f("bad length %zu", len); 105 POKE_U32(sshbuf_mutable_ptr(log_msg), len - 4); 106 if (atomicio(vwrite, mon->m_log_sendfd, 107 sshbuf_mutable_ptr(log_msg), len) != len) 108 fatal_f("write: %s", strerror(errno)); 109 sshbuf_free(log_msg); 110 } 111 112 int 113 mm_is_monitor(void) 114 { 115 /* 116 * m_pid is only set in the privileged part, and 117 * points to the unprivileged child. 118 */ 119 return (pmonitor && pmonitor->m_pid > 0); 120 } 121 122 void 123 mm_request_send(int sock, enum monitor_reqtype type, struct sshbuf *m) 124 { 125 size_t mlen = sshbuf_len(m); 126 u_char buf[5]; 127 128 debug3_f("entering, type %d", type); 129 130 if (mlen >= 0xffffffff) 131 fatal_f("bad length %zu", mlen); 132 POKE_U32(buf, mlen + 1); 133 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */ 134 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf)) 135 fatal_f("write: %s", strerror(errno)); 136 if (atomicio(vwrite, sock, sshbuf_mutable_ptr(m), mlen) != mlen) 137 fatal_f("write: %s", strerror(errno)); 138 } 139 140 void 141 mm_request_receive(int sock, struct sshbuf *m) 142 { 143 u_char buf[4], *p = NULL; 144 u_int msg_len; 145 int r; 146 147 debug3_f("entering"); 148 149 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) { 150 if (errno == EPIPE) 151 cleanup_exit(255); 152 fatal_f("read: %s", strerror(errno)); 153 } 154 msg_len = PEEK_U32(buf); 155 if (msg_len > 256 * 1024) 156 fatal_f("read: bad msg_len %d", msg_len); 157 sshbuf_reset(m); 158 if ((r = sshbuf_reserve(m, msg_len, &p)) != 0) 159 fatal_fr(r, "reserve"); 160 if (atomicio(read, sock, p, msg_len) != msg_len) 161 fatal_f("read: %s", strerror(errno)); 162 } 163 164 void 165 mm_request_receive_expect(int sock, enum monitor_reqtype type, struct sshbuf *m) 166 { 167 u_char rtype; 168 int r; 169 170 debug3_f("entering, type %d", type); 171 172 mm_request_receive(sock, m); 173 if ((r = sshbuf_get_u8(m, &rtype)) != 0) 174 fatal_fr(r, "parse"); 175 if (rtype != type) 176 fatal_f("read: rtype %d != type %d", rtype, type); 177 } 178 179 #ifdef WITH_OPENSSL 180 DH * 181 mm_choose_dh(int min, int nbits, int max) 182 { 183 BIGNUM *p, *g; 184 int r; 185 u_char success = 0; 186 struct sshbuf *m; 187 188 if ((m = sshbuf_new()) == NULL) 189 fatal_f("sshbuf_new failed"); 190 if ((r = sshbuf_put_u32(m, min)) != 0 || 191 (r = sshbuf_put_u32(m, nbits)) != 0 || 192 (r = sshbuf_put_u32(m, max)) != 0) 193 fatal_fr(r, "assemble"); 194 195 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, m); 196 197 debug3_f("waiting for MONITOR_ANS_MODULI"); 198 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, m); 199 200 if ((r = sshbuf_get_u8(m, &success)) != 0) 201 fatal_fr(r, "parse success"); 202 if (success == 0) 203 fatal_f("MONITOR_ANS_MODULI failed"); 204 205 if ((r = sshbuf_get_bignum2(m, &p)) != 0 || 206 (r = sshbuf_get_bignum2(m, &g)) != 0) 207 fatal_fr(r, "parse group"); 208 209 debug3_f("remaining %zu", sshbuf_len(m)); 210 sshbuf_free(m); 211 212 return (dh_new_group(g, p)); 213 } 214 #endif 215 216 int 217 mm_sshkey_sign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp, 218 const u_char *data, size_t datalen, const char *hostkey_alg, 219 const char *sk_provider, const char *sk_pin, u_int compat) 220 { 221 struct kex *kex = *pmonitor->m_pkex; 222 struct sshbuf *m; 223 u_int ndx = kex->host_key_index(key, 0, ssh); 224 int r; 225 226 debug3_f("entering"); 227 if ((m = sshbuf_new()) == NULL) 228 fatal_f("sshbuf_new failed"); 229 if ((r = sshbuf_put_u32(m, ndx)) != 0 || 230 (r = sshbuf_put_string(m, data, datalen)) != 0 || 231 (r = sshbuf_put_cstring(m, hostkey_alg)) != 0 || 232 (r = sshbuf_put_u32(m, compat)) != 0) 233 fatal_fr(r, "assemble"); 234 235 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, m); 236 237 debug3_f("waiting for MONITOR_ANS_SIGN"); 238 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, m); 239 if ((r = sshbuf_get_string(m, sigp, lenp)) != 0) 240 fatal_fr(r, "parse"); 241 sshbuf_free(m); 242 243 return (0); 244 } 245 246 #define GETPW(b, id) \ 247 do { \ 248 if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0) \ 249 fatal_fr(r, "parse pw %s", #id); \ 250 if (len != sizeof(pw->id)) \ 251 fatal_fr(r, "bad length for %s", #id); \ 252 memcpy(&pw->id, p, len); \ 253 } while (0) 254 255 struct passwd * 256 mm_getpwnamallow(struct ssh *ssh, const char *username) 257 { 258 struct sshbuf *m; 259 struct passwd *pw; 260 size_t len; 261 u_int i; 262 ServerOptions *newopts; 263 int r; 264 u_char ok; 265 const u_char *p; 266 267 debug3_f("entering"); 268 269 if ((m = sshbuf_new()) == NULL) 270 fatal_f("sshbuf_new failed"); 271 if ((r = sshbuf_put_cstring(m, username)) != 0) 272 fatal_fr(r, "assemble"); 273 274 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, m); 275 276 debug3_f("waiting for MONITOR_ANS_PWNAM"); 277 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, m); 278 279 if ((r = sshbuf_get_u8(m, &ok)) != 0) 280 fatal_fr(r, "parse success"); 281 if (ok == 0) { 282 pw = NULL; 283 goto out; 284 } 285 286 /* XXX don't like passing struct passwd like this */ 287 pw = xcalloc(sizeof(*pw), 1); 288 GETPW(m, pw_uid); 289 GETPW(m, pw_gid); 290 #ifdef HAVE_STRUCT_PASSWD_PW_CHANGE 291 GETPW(m, pw_change); 292 #endif 293 #ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE 294 GETPW(m, pw_expire); 295 #endif 296 if ((r = sshbuf_get_cstring(m, &pw->pw_name, NULL)) != 0 || 297 (r = sshbuf_get_cstring(m, &pw->pw_passwd, NULL)) != 0 || 298 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS 299 (r = sshbuf_get_cstring(m, &pw->pw_gecos, NULL)) != 0 || 300 #endif 301 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS 302 (r = sshbuf_get_cstring(m, &pw->pw_class, NULL)) != 0 || 303 #endif 304 (r = sshbuf_get_cstring(m, &pw->pw_dir, NULL)) != 0 || 305 (r = sshbuf_get_cstring(m, &pw->pw_shell, NULL)) != 0) 306 fatal_fr(r, "parse pw"); 307 308 out: 309 /* copy options block as a Match directive may have changed some */ 310 if ((r = sshbuf_get_string_direct(m, &p, &len)) != 0) 311 fatal_fr(r, "parse opts"); 312 if (len != sizeof(*newopts)) 313 fatal_f("option block size mismatch"); 314 newopts = xcalloc(sizeof(*newopts), 1); 315 memcpy(newopts, p, sizeof(*newopts)); 316 317 #define M_CP_STROPT(x) do { \ 318 if (newopts->x != NULL && \ 319 (r = sshbuf_get_cstring(m, &newopts->x, NULL)) != 0) \ 320 fatal_fr(r, "parse %s", #x); \ 321 } while (0) 322 #define M_CP_STRARRAYOPT(x, nx) do { \ 323 newopts->x = newopts->nx == 0 ? \ 324 NULL : xcalloc(newopts->nx, sizeof(*newopts->x)); \ 325 for (i = 0; i < newopts->nx; i++) { \ 326 if ((r = sshbuf_get_cstring(m, \ 327 &newopts->x[i], NULL)) != 0) \ 328 fatal_fr(r, "parse %s", #x); \ 329 } \ 330 } while (0) 331 /* See comment in servconf.h */ 332 COPY_MATCH_STRING_OPTS(); 333 #undef M_CP_STROPT 334 #undef M_CP_STRARRAYOPT 335 336 copy_set_server_options(&options, newopts, 1); 337 log_change_level(options.log_level); 338 log_verbose_reset(); 339 for (i = 0; i < options.num_log_verbose; i++) 340 log_verbose_add(options.log_verbose[i]); 341 process_permitopen(ssh, &options); 342 free(newopts); 343 344 sshbuf_free(m); 345 346 return (pw); 347 } 348 349 char * 350 mm_auth2_read_banner(void) 351 { 352 struct sshbuf *m; 353 char *banner; 354 int r; 355 356 debug3_f("entering"); 357 358 if ((m = sshbuf_new()) == NULL) 359 fatal_f("sshbuf_new failed"); 360 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, m); 361 sshbuf_reset(m); 362 363 mm_request_receive_expect(pmonitor->m_recvfd, 364 MONITOR_ANS_AUTH2_READ_BANNER, m); 365 if ((r = sshbuf_get_cstring(m, &banner, NULL)) != 0) 366 fatal_fr(r, "parse"); 367 sshbuf_free(m); 368 369 /* treat empty banner as missing banner */ 370 if (strlen(banner) == 0) { 371 free(banner); 372 banner = NULL; 373 } 374 return (banner); 375 } 376 377 /* Inform the privileged process about service and style */ 378 379 void 380 mm_inform_authserv(char *service, char *style) 381 { 382 struct sshbuf *m; 383 int r; 384 385 debug3_f("entering"); 386 387 if ((m = sshbuf_new()) == NULL) 388 fatal_f("sshbuf_new failed"); 389 if ((r = sshbuf_put_cstring(m, service)) != 0 || 390 (r = sshbuf_put_cstring(m, style ? style : "")) != 0) 391 fatal_fr(r, "assemble"); 392 393 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, m); 394 395 sshbuf_free(m); 396 } 397 398 /* Do the password authentication */ 399 int 400 mm_auth_password(struct ssh *ssh, char *password) 401 { 402 struct sshbuf *m; 403 int r, authenticated = 0; 404 #ifdef USE_PAM 405 u_int maxtries = 0; 406 #endif 407 408 debug3_f("entering"); 409 410 if ((m = sshbuf_new()) == NULL) 411 fatal_f("sshbuf_new failed"); 412 if ((r = sshbuf_put_cstring(m, password)) != 0) 413 fatal_fr(r, "assemble"); 414 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, m); 415 416 debug3_f("waiting for MONITOR_ANS_AUTHPASSWORD"); 417 mm_request_receive_expect(pmonitor->m_recvfd, 418 MONITOR_ANS_AUTHPASSWORD, m); 419 420 if ((r = sshbuf_get_u32(m, &authenticated)) != 0) 421 fatal_fr(r, "parse"); 422 #ifdef USE_PAM 423 if ((r = sshbuf_get_u32(m, &maxtries)) != 0) 424 fatal_fr(r, "parse PAM"); 425 if (maxtries > INT_MAX) 426 fatal_fr(r, "bad maxtries"); 427 sshpam_set_maxtries_reached(maxtries); 428 #endif 429 430 sshbuf_free(m); 431 432 debug3_f("user %sauthenticated", authenticated ? "" : "not "); 433 return (authenticated); 434 } 435 436 int 437 mm_user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key, 438 int pubkey_auth_attempt, struct sshauthopt **authoptp) 439 { 440 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key, 441 pubkey_auth_attempt, authoptp)); 442 } 443 444 int 445 mm_hostbased_key_allowed(struct ssh *ssh, struct passwd *pw, 446 const char *user, const char *host, struct sshkey *key) 447 { 448 return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0, NULL)); 449 } 450 451 int 452 mm_key_allowed(enum mm_keytype type, const char *user, const char *host, 453 struct sshkey *key, int pubkey_auth_attempt, struct sshauthopt **authoptp) 454 { 455 struct sshbuf *m; 456 int r, allowed = 0; 457 struct sshauthopt *opts = NULL; 458 459 debug3_f("entering"); 460 461 if (authoptp != NULL) 462 *authoptp = NULL; 463 464 if ((m = sshbuf_new()) == NULL) 465 fatal_f("sshbuf_new failed"); 466 if ((r = sshbuf_put_u32(m, type)) != 0 || 467 (r = sshbuf_put_cstring(m, user ? user : "")) != 0 || 468 (r = sshbuf_put_cstring(m, host ? host : "")) != 0 || 469 (r = sshkey_puts(key, m)) != 0 || 470 (r = sshbuf_put_u32(m, pubkey_auth_attempt)) != 0) 471 fatal_fr(r, "assemble"); 472 473 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, m); 474 475 debug3_f("waiting for MONITOR_ANS_KEYALLOWED"); 476 mm_request_receive_expect(pmonitor->m_recvfd, 477 MONITOR_ANS_KEYALLOWED, m); 478 479 if ((r = sshbuf_get_u32(m, &allowed)) != 0) 480 fatal_fr(r, "parse"); 481 if (allowed && type == MM_USERKEY && 482 (r = sshauthopt_deserialise(m, &opts)) != 0) 483 fatal_fr(r, "sshauthopt_deserialise"); 484 sshbuf_free(m); 485 486 if (authoptp != NULL) { 487 *authoptp = opts; 488 opts = NULL; 489 } 490 sshauthopt_free(opts); 491 492 return allowed; 493 } 494 495 /* 496 * This key verify needs to send the key type along, because the 497 * privileged parent makes the decision if the key is allowed 498 * for authentication. 499 */ 500 501 int 502 mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen, 503 const u_char *data, size_t datalen, const char *sigalg, u_int compat, 504 struct sshkey_sig_details **sig_detailsp) 505 { 506 struct sshbuf *m; 507 u_int encoded_ret = 0; 508 int r; 509 u_char sig_details_present, flags; 510 u_int counter; 511 512 debug3_f("entering"); 513 514 if (sig_detailsp != NULL) 515 *sig_detailsp = NULL; 516 if ((m = sshbuf_new()) == NULL) 517 fatal_f("sshbuf_new failed"); 518 if ((r = sshkey_puts(key, m)) != 0 || 519 (r = sshbuf_put_string(m, sig, siglen)) != 0 || 520 (r = sshbuf_put_string(m, data, datalen)) != 0 || 521 (r = sshbuf_put_cstring(m, sigalg == NULL ? "" : sigalg)) != 0) 522 fatal_fr(r, "assemble"); 523 524 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, m); 525 526 debug3_f("waiting for MONITOR_ANS_KEYVERIFY"); 527 mm_request_receive_expect(pmonitor->m_recvfd, 528 MONITOR_ANS_KEYVERIFY, m); 529 530 if ((r = sshbuf_get_u32(m, &encoded_ret)) != 0 || 531 (r = sshbuf_get_u8(m, &sig_details_present)) != 0) 532 fatal_fr(r, "parse"); 533 if (sig_details_present && encoded_ret == 0) { 534 if ((r = sshbuf_get_u32(m, &counter)) != 0 || 535 (r = sshbuf_get_u8(m, &flags)) != 0) 536 fatal_fr(r, "parse sig_details"); 537 if (sig_detailsp != NULL) { 538 *sig_detailsp = xcalloc(1, sizeof(**sig_detailsp)); 539 (*sig_detailsp)->sk_counter = counter; 540 (*sig_detailsp)->sk_flags = flags; 541 } 542 } 543 544 sshbuf_free(m); 545 546 if (encoded_ret != 0) 547 return SSH_ERR_SIGNATURE_INVALID; 548 return 0; 549 } 550 551 void 552 mm_send_keystate(struct ssh *ssh, struct monitor *monitor) 553 { 554 struct sshbuf *m; 555 int r; 556 557 if ((m = sshbuf_new()) == NULL) 558 fatal_f("sshbuf_new failed"); 559 if ((r = ssh_packet_get_state(ssh, m)) != 0) 560 fatal_fr(r, "ssh_packet_get_state"); 561 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m); 562 debug3_f("Finished sending state"); 563 sshbuf_free(m); 564 } 565 566 int 567 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen) 568 { 569 struct sshbuf *m; 570 char *p, *msg; 571 int success = 0, tmp1 = -1, tmp2 = -1, r; 572 573 /* Kludge: ensure there are fds free to receive the pty/tty */ 574 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 || 575 (tmp2 = dup(pmonitor->m_recvfd)) == -1) { 576 error_f("cannot allocate fds for pty"); 577 if (tmp1 > 0) 578 close(tmp1); 579 if (tmp2 > 0) 580 close(tmp2); 581 return 0; 582 } 583 close(tmp1); 584 close(tmp2); 585 586 if ((m = sshbuf_new()) == NULL) 587 fatal_f("sshbuf_new failed"); 588 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, m); 589 590 debug3_f("waiting for MONITOR_ANS_PTY"); 591 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, m); 592 593 if ((r = sshbuf_get_u32(m, &success)) != 0) 594 fatal_fr(r, "parse success"); 595 if (success == 0) { 596 debug3_f("pty alloc failed"); 597 sshbuf_free(m); 598 return (0); 599 } 600 if ((r = sshbuf_get_cstring(m, &p, NULL)) != 0 || 601 (r = sshbuf_get_cstring(m, &msg, NULL)) != 0) 602 fatal_fr(r, "parse"); 603 sshbuf_free(m); 604 605 strlcpy(namebuf, p, namebuflen); /* Possible truncation */ 606 free(p); 607 608 if ((r = sshbuf_put(loginmsg, msg, strlen(msg))) != 0) 609 fatal_fr(r, "put loginmsg"); 610 free(msg); 611 612 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 || 613 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1) 614 fatal_f("receive fds failed"); 615 616 /* Success */ 617 return (1); 618 } 619 620 void 621 mm_session_pty_cleanup2(Session *s) 622 { 623 struct sshbuf *m; 624 int r; 625 626 if (s->ttyfd == -1) 627 return; 628 if ((m = sshbuf_new()) == NULL) 629 fatal_f("sshbuf_new failed"); 630 if ((r = sshbuf_put_cstring(m, s->tty)) != 0) 631 fatal_fr(r, "assmble"); 632 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, m); 633 sshbuf_free(m); 634 635 /* closed dup'ed master */ 636 if (s->ptymaster != -1 && close(s->ptymaster) == -1) 637 error("close(s->ptymaster/%d): %s", 638 s->ptymaster, strerror(errno)); 639 640 /* unlink pty from session */ 641 s->ttyfd = -1; 642 } 643 644 #ifdef USE_PAM 645 void 646 mm_start_pam(struct ssh *ssh) 647 { 648 struct sshbuf *m; 649 650 debug3("%s entering", __func__); 651 if (!options.use_pam) 652 fatal("UsePAM=no, but ended up in %s anyway", __func__); 653 if ((m = sshbuf_new()) == NULL) 654 fatal("%s: sshbuf_new failed", __func__); 655 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, m); 656 657 sshbuf_free(m); 658 } 659 660 u_int 661 mm_do_pam_account(void) 662 { 663 struct sshbuf *m; 664 u_int ret; 665 char *msg; 666 size_t msglen; 667 int r; 668 669 debug3("%s entering", __func__); 670 if (!options.use_pam) 671 fatal("UsePAM=no, but ended up in %s anyway", __func__); 672 673 if ((m = sshbuf_new()) == NULL) 674 fatal("%s: sshbuf_new failed", __func__); 675 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, m); 676 677 mm_request_receive_expect(pmonitor->m_recvfd, 678 MONITOR_ANS_PAM_ACCOUNT, m); 679 if ((r = sshbuf_get_u32(m, &ret)) != 0 || 680 (r = sshbuf_get_cstring(m, &msg, &msglen)) != 0 || 681 (r = sshbuf_put(loginmsg, msg, msglen)) != 0) 682 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 683 684 free(msg); 685 sshbuf_free(m); 686 687 debug3("%s returning %d", __func__, ret); 688 689 return (ret); 690 } 691 692 void * 693 mm_sshpam_init_ctx(Authctxt *authctxt) 694 { 695 struct sshbuf *m; 696 int r, success; 697 698 debug3("%s", __func__); 699 if ((m = sshbuf_new()) == NULL) 700 fatal("%s: sshbuf_new failed", __func__); 701 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, m); 702 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__); 703 mm_request_receive_expect(pmonitor->m_recvfd, 704 MONITOR_ANS_PAM_INIT_CTX, m); 705 if ((r = sshbuf_get_u32(m, &success)) != 0) 706 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 707 if (success == 0) { 708 debug3("%s: pam_init_ctx failed", __func__); 709 sshbuf_free(m); 710 return (NULL); 711 } 712 sshbuf_free(m); 713 return (authctxt); 714 } 715 716 int 717 mm_sshpam_query(void *ctx, char **name, char **info, 718 u_int *num, char ***prompts, u_int **echo_on) 719 { 720 struct sshbuf *m; 721 u_int i, n; 722 int r, ret; 723 724 debug3("%s", __func__); 725 if ((m = sshbuf_new()) == NULL) 726 fatal("%s: sshbuf_new failed", __func__); 727 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, m); 728 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__); 729 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, m); 730 if ((r = sshbuf_get_u32(m, &ret)) != 0 || 731 (r = sshbuf_get_cstring(m, name, NULL)) != 0 || 732 (r = sshbuf_get_cstring(m, info, NULL)) != 0 || 733 (r = sshbuf_get_u32(m, &n)) != 0 || 734 (r = sshbuf_get_u32(m, num)) != 0) 735 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 736 debug3("%s: pam_query returned %d", __func__, ret); 737 sshpam_set_maxtries_reached(n); 738 if (*num > PAM_MAX_NUM_MSG) 739 fatal("%s: received %u PAM messages, expected <= %u", 740 __func__, *num, PAM_MAX_NUM_MSG); 741 *prompts = xcalloc((*num + 1), sizeof(char *)); 742 *echo_on = xcalloc((*num + 1), sizeof(u_int)); 743 for (i = 0; i < *num; ++i) { 744 if ((r = sshbuf_get_cstring(m, &((*prompts)[i]), NULL)) != 0 || 745 (r = sshbuf_get_u32(m, &((*echo_on)[i]))) != 0) 746 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 747 } 748 sshbuf_free(m); 749 return (ret); 750 } 751 752 int 753 mm_sshpam_respond(void *ctx, u_int num, char **resp) 754 { 755 struct sshbuf *m; 756 u_int n, i; 757 int r, ret; 758 759 debug3("%s", __func__); 760 if ((m = sshbuf_new()) == NULL) 761 fatal("%s: sshbuf_new failed", __func__); 762 if ((r = sshbuf_put_u32(m, num)) != 0) 763 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 764 for (i = 0; i < num; ++i) { 765 if ((r = sshbuf_put_cstring(m, resp[i])) != 0) 766 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 767 } 768 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, m); 769 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__); 770 mm_request_receive_expect(pmonitor->m_recvfd, 771 MONITOR_ANS_PAM_RESPOND, m); 772 if ((r = sshbuf_get_u32(m, &n)) != 0) 773 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 774 ret = (int)n; /* XXX */ 775 debug3("%s: pam_respond returned %d", __func__, ret); 776 sshbuf_free(m); 777 return (ret); 778 } 779 780 void 781 mm_sshpam_free_ctx(void *ctxtp) 782 { 783 struct sshbuf *m; 784 785 debug3("%s", __func__); 786 if ((m = sshbuf_new()) == NULL) 787 fatal("%s: sshbuf_new failed", __func__); 788 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, m); 789 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__); 790 mm_request_receive_expect(pmonitor->m_recvfd, 791 MONITOR_ANS_PAM_FREE_CTX, m); 792 sshbuf_free(m); 793 } 794 #endif /* USE_PAM */ 795 796 /* Request process termination */ 797 798 void 799 mm_terminate(void) 800 { 801 struct sshbuf *m; 802 803 if ((m = sshbuf_new()) == NULL) 804 fatal_f("sshbuf_new failed"); 805 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, m); 806 sshbuf_free(m); 807 } 808 809 static void 810 mm_chall_setup(char **name, char **infotxt, u_int *numprompts, 811 char ***prompts, u_int **echo_on) 812 { 813 *name = xstrdup(""); 814 *infotxt = xstrdup(""); 815 *numprompts = 1; 816 *prompts = xcalloc(*numprompts, sizeof(char *)); 817 *echo_on = xcalloc(*numprompts, sizeof(u_int)); 818 (*echo_on)[0] = 0; 819 } 820 821 int 822 mm_bsdauth_query(void *ctx, char **name, char **infotxt, 823 u_int *numprompts, char ***prompts, u_int **echo_on) 824 { 825 struct sshbuf *m; 826 u_int success; 827 char *challenge; 828 int r; 829 830 debug3_f("entering"); 831 832 if ((m = sshbuf_new()) == NULL) 833 fatal_f("sshbuf_new failed"); 834 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, m); 835 836 mm_request_receive_expect(pmonitor->m_recvfd, 837 MONITOR_ANS_BSDAUTHQUERY, m); 838 if ((r = sshbuf_get_u32(m, &success)) != 0) 839 fatal_fr(r, "parse success"); 840 if (success == 0) { 841 debug3_f("no challenge"); 842 sshbuf_free(m); 843 return (-1); 844 } 845 846 /* Get the challenge, and format the response */ 847 if ((r = sshbuf_get_cstring(m, &challenge, NULL)) != 0) 848 fatal_fr(r, "parse challenge"); 849 sshbuf_free(m); 850 851 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on); 852 (*prompts)[0] = challenge; 853 854 debug3_f("received challenge: %s", challenge); 855 856 return (0); 857 } 858 859 int 860 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses) 861 { 862 struct sshbuf *m; 863 int r, authok; 864 865 debug3_f("entering"); 866 if (numresponses != 1) 867 return (-1); 868 869 if ((m = sshbuf_new()) == NULL) 870 fatal_f("sshbuf_new failed"); 871 if ((r = sshbuf_put_cstring(m, responses[0])) != 0) 872 fatal_fr(r, "assemble"); 873 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, m); 874 875 mm_request_receive_expect(pmonitor->m_recvfd, 876 MONITOR_ANS_BSDAUTHRESPOND, m); 877 878 if ((r = sshbuf_get_u32(m, &authok)) != 0) 879 fatal_fr(r, "parse"); 880 sshbuf_free(m); 881 882 return ((authok == 0) ? -1 : 0); 883 } 884 885 #ifdef SSH_AUDIT_EVENTS 886 void 887 mm_audit_event(struct ssh *ssh, ssh_audit_event_t event) 888 { 889 struct sshbuf *m; 890 int r; 891 892 debug3("%s entering", __func__); 893 894 if ((m = sshbuf_new()) == NULL) 895 fatal("%s: sshbuf_new failed", __func__); 896 if ((r = sshbuf_put_u32(m, event)) != 0) 897 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 898 899 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, m); 900 sshbuf_free(m); 901 } 902 903 void 904 mm_audit_run_command(const char *command) 905 { 906 struct sshbuf *m; 907 int r; 908 909 debug3("%s entering command %s", __func__, command); 910 911 if ((m = sshbuf_new()) == NULL) 912 fatal("%s: sshbuf_new failed", __func__); 913 if ((r = sshbuf_put_cstring(m, command)) != 0) 914 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 915 916 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, m); 917 sshbuf_free(m); 918 } 919 #endif /* SSH_AUDIT_EVENTS */ 920 921 #ifdef GSSAPI 922 OM_uint32 923 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid) 924 { 925 struct sshbuf *m; 926 OM_uint32 major; 927 int r; 928 929 /* Client doesn't get to see the context */ 930 *ctx = NULL; 931 932 if ((m = sshbuf_new()) == NULL) 933 fatal_f("sshbuf_new failed"); 934 if ((r = sshbuf_put_string(m, goid->elements, goid->length)) != 0) 935 fatal_fr(r, "assemble"); 936 937 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, m); 938 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, m); 939 940 if ((r = sshbuf_get_u32(m, &major)) != 0) 941 fatal_fr(r, "parse"); 942 943 sshbuf_free(m); 944 return (major); 945 } 946 947 OM_uint32 948 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in, 949 gss_buffer_desc *out, OM_uint32 *flagsp) 950 { 951 struct sshbuf *m; 952 OM_uint32 major; 953 u_int flags; 954 int r; 955 956 if ((m = sshbuf_new()) == NULL) 957 fatal_f("sshbuf_new failed"); 958 if ((r = sshbuf_put_string(m, in->value, in->length)) != 0) 959 fatal_fr(r, "assemble"); 960 961 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, m); 962 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, m); 963 964 if ((r = sshbuf_get_u32(m, &major)) != 0 || 965 (r = ssh_gssapi_get_buffer_desc(m, out)) != 0) 966 fatal_fr(r, "parse"); 967 if (flagsp != NULL) { 968 if ((r = sshbuf_get_u32(m, &flags)) != 0) 969 fatal_fr(r, "parse flags"); 970 *flagsp = flags; 971 } 972 973 sshbuf_free(m); 974 975 return (major); 976 } 977 978 OM_uint32 979 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic) 980 { 981 struct sshbuf *m; 982 OM_uint32 major; 983 int r; 984 985 if ((m = sshbuf_new()) == NULL) 986 fatal_f("sshbuf_new failed"); 987 if ((r = sshbuf_put_string(m, gssbuf->value, gssbuf->length)) != 0 || 988 (r = sshbuf_put_string(m, gssmic->value, gssmic->length)) != 0) 989 fatal_fr(r, "assemble"); 990 991 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, m); 992 mm_request_receive_expect(pmonitor->m_recvfd, 993 MONITOR_ANS_GSSCHECKMIC, m); 994 995 if ((r = sshbuf_get_u32(m, &major)) != 0) 996 fatal_fr(r, "parse"); 997 sshbuf_free(m); 998 return(major); 999 } 1000 1001 int 1002 mm_ssh_gssapi_userok(char *user) 1003 { 1004 struct sshbuf *m; 1005 int r, authenticated = 0; 1006 1007 if ((m = sshbuf_new()) == NULL) 1008 fatal_f("sshbuf_new failed"); 1009 1010 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, m); 1011 mm_request_receive_expect(pmonitor->m_recvfd, 1012 MONITOR_ANS_GSSUSEROK, m); 1013 1014 if ((r = sshbuf_get_u32(m, &authenticated)) != 0) 1015 fatal_fr(r, "parse"); 1016 1017 sshbuf_free(m); 1018 debug3_f("user %sauthenticated", authenticated ? "" : "not "); 1019 return (authenticated); 1020 } 1021 #endif /* GSSAPI */ 1022