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