rshd.c revision 302408
1/*- 2 * Copyright (c) 1988, 1989, 1992, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (c) 2002 Networks Associates Technology, Inc. 5 * All rights reserved. 6 * 7 * Portions of this software were developed for the FreeBSD Project by 8 * ThinkSec AS and NAI Labs, the Security Research Division of Network 9 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 10 * ("CBOSS"), as part of the DARPA CHATS research program. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37#ifndef lint 38static const char copyright[] = 39"@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41#endif /* not lint */ 42 43#ifndef lint 44#if 0 45static const char sccsid[] = "@(#)rshd.c 8.2 (Berkeley) 4/6/94"; 46#endif 47#endif /* not lint */ 48 49#include <sys/cdefs.h> 50__FBSDID("$FreeBSD: stable/11/libexec/rshd/rshd.c 301242 2016-06-03 06:58:20Z lidl $"); 51 52/* 53 * remote shell server: 54 * [port]\0 55 * ruser\0 56 * luser\0 57 * command\0 58 * data 59 */ 60#include <sys/param.h> 61#include <sys/ioctl.h> 62#include <sys/time.h> 63#include <sys/socket.h> 64 65#include <netinet/in_systm.h> 66#include <netinet/in.h> 67#include <netinet/ip.h> 68#include <netinet/tcp.h> 69#include <arpa/inet.h> 70#include <netdb.h> 71 72#include <err.h> 73#include <errno.h> 74#include <fcntl.h> 75#include <libutil.h> 76#include <paths.h> 77#include <pwd.h> 78#include <signal.h> 79#include <stdarg.h> 80#include <stdio.h> 81#include <stdlib.h> 82#include <string.h> 83#include <syslog.h> 84#include <unistd.h> 85#include <login_cap.h> 86 87#include <security/pam_appl.h> 88#include <security/openpam.h> 89#include <sys/wait.h> 90 91#ifdef USE_BLACKLIST 92#include <blacklist.h> 93#endif 94 95static struct pam_conv pamc = { openpam_nullconv, NULL }; 96static pam_handle_t *pamh; 97static int pam_err; 98 99#define PAM_END { \ 100 if ((pam_err = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) \ 101 syslog(LOG_ERR|LOG_AUTH, "pam_setcred(): %s", pam_strerror(pamh, pam_err)); \ 102 if ((pam_err = pam_close_session(pamh,0)) != PAM_SUCCESS) \ 103 syslog(LOG_ERR|LOG_AUTH, "pam_close_session(): %s", pam_strerror(pamh, pam_err)); \ 104 if ((pam_err = pam_end(pamh, pam_err)) != PAM_SUCCESS) \ 105 syslog(LOG_ERR|LOG_AUTH, "pam_end(): %s", pam_strerror(pamh, pam_err)); \ 106} 107 108int keepalive = 1; 109int log_success; /* If TRUE, log all successful accesses */ 110int sent_null; 111int no_delay; 112 113void doit(struct sockaddr *); 114static void rshd_errx(int, const char *, ...) __printf0like(2, 3); 115void getstr(char *, int, const char *); 116int local_domain(char *); 117char *topdomain(char *); 118void usage(void); 119 120char slash[] = "/"; 121char bshell[] = _PATH_BSHELL; 122 123#define OPTIONS "aDLln" 124 125int 126main(int argc, char *argv[]) 127{ 128 extern int __check_rhosts_file; 129 struct linger linger; 130 socklen_t fromlen; 131 int ch, on = 1; 132 struct sockaddr_storage from; 133 134 openlog("rshd", LOG_PID, LOG_DAEMON); 135 136 opterr = 0; 137 while ((ch = getopt(argc, argv, OPTIONS)) != -1) 138 switch (ch) { 139 case 'a': 140 /* ignored for compatibility */ 141 break; 142 case 'l': 143 __check_rhosts_file = 0; 144 break; 145 case 'n': 146 keepalive = 0; 147 break; 148 case 'D': 149 no_delay = 1; 150 break; 151 case 'L': 152 log_success = 1; 153 break; 154 case '?': 155 default: 156 usage(); 157 break; 158 } 159 160 argc -= optind; 161 argv += optind; 162 163 fromlen = sizeof (from); 164 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) { 165 syslog(LOG_ERR, "getpeername: %m"); 166 exit(1); 167 } 168 if (keepalive && 169 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, 170 sizeof(on)) < 0) 171 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m"); 172 linger.l_onoff = 1; 173 linger.l_linger = 60; /* XXX */ 174 if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger, 175 sizeof (linger)) < 0) 176 syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m"); 177 if (no_delay && 178 setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) 179 syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m"); 180 doit((struct sockaddr *)&from); 181 /* NOTREACHED */ 182 return(0); 183} 184 185extern char **environ; 186 187void 188doit(struct sockaddr *fromp) 189{ 190 extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */ 191 struct passwd *pwd; 192 u_short port; 193 fd_set ready, readfrom; 194 int cc, fd, nfd, pv[2], pid, s; 195 int one = 1; 196 const char *cp, *errorstr; 197 char sig, buf[BUFSIZ]; 198 char *cmdbuf, luser[16], ruser[16]; 199 char rhost[2 * MAXHOSTNAMELEN + 1]; 200 char numericname[INET6_ADDRSTRLEN]; 201 int af, srcport; 202 int maxcmdlen; 203 login_cap_t *lc; 204 205 maxcmdlen = (int)sysconf(_SC_ARG_MAX); 206 if (maxcmdlen <= 0 || (cmdbuf = malloc(maxcmdlen)) == NULL) 207 exit(1); 208 209 (void) signal(SIGINT, SIG_DFL); 210 (void) signal(SIGQUIT, SIG_DFL); 211 (void) signal(SIGTERM, SIG_DFL); 212 af = fromp->sa_family; 213 srcport = ntohs(*((in_port_t *)&fromp->sa_data)); 214 if (af == AF_INET) { 215 inet_ntop(af, &((struct sockaddr_in *)fromp)->sin_addr, 216 numericname, sizeof numericname); 217 } else if (af == AF_INET6) { 218 inet_ntop(af, &((struct sockaddr_in6 *)fromp)->sin6_addr, 219 numericname, sizeof numericname); 220 } else { 221 syslog(LOG_ERR, "malformed \"from\" address (af %d)", af); 222 exit(1); 223 } 224#ifdef IP_OPTIONS 225 if (af == AF_INET) { 226 u_char optbuf[BUFSIZ/3]; 227 socklen_t optsize = sizeof(optbuf), ipproto, i; 228 struct protoent *ip; 229 230 if ((ip = getprotobyname("ip")) != NULL) 231 ipproto = ip->p_proto; 232 else 233 ipproto = IPPROTO_IP; 234 if (!getsockopt(0, ipproto, IP_OPTIONS, optbuf, &optsize) && 235 optsize != 0) { 236 for (i = 0; i < optsize; ) { 237 u_char c = optbuf[i]; 238 if (c == IPOPT_LSRR || c == IPOPT_SSRR) { 239 syslog(LOG_NOTICE, 240 "connection refused from %s with IP option %s", 241 numericname, 242 c == IPOPT_LSRR ? "LSRR" : "SSRR"); 243 exit(1); 244 } 245 if (c == IPOPT_EOL) 246 break; 247 i += (c == IPOPT_NOP) ? 1 : optbuf[i+1]; 248 } 249 } 250 } 251#endif 252 253 if (srcport >= IPPORT_RESERVED || 254 srcport < IPPORT_RESERVED/2) { 255 syslog(LOG_NOTICE|LOG_AUTH, 256 "connection from %s on illegal port %u", 257 numericname, 258 srcport); 259#ifdef USE_BLACKLIST 260 blacklist(1, STDIN_FILENO, "illegal port"); 261#endif 262 exit(1); 263 } 264 265 (void) alarm(60); 266 port = 0; 267 s = 0; /* not set or used if port == 0 */ 268 for (;;) { 269 char c; 270 if ((cc = read(STDIN_FILENO, &c, 1)) != 1) { 271 if (cc < 0) 272 syslog(LOG_NOTICE, "read: %m"); 273 shutdown(0, SHUT_RDWR); 274 exit(1); 275 } 276 if (c == 0) 277 break; 278 port = port * 10 + c - '0'; 279 } 280 281 (void) alarm(0); 282 if (port != 0) { 283 int lport = IPPORT_RESERVED - 1; 284 s = rresvport_af(&lport, af); 285 if (s < 0) { 286 syslog(LOG_ERR, "can't get stderr port: %m"); 287 exit(1); 288 } 289 if (port >= IPPORT_RESERVED || 290 port < IPPORT_RESERVED/2) { 291 syslog(LOG_NOTICE|LOG_AUTH, 292 "2nd socket from %s on unreserved port %u", 293 numericname, 294 port); 295#ifdef USE_BLACKLIST 296 blacklist(1, STDIN_FILENO, "unreserved port"); 297#endif 298 exit(1); 299 } 300 *((in_port_t *)&fromp->sa_data) = htons(port); 301 if (connect(s, fromp, fromp->sa_len) < 0) { 302 syslog(LOG_INFO, "connect second port %d: %m", port); 303 exit(1); 304 } 305 } 306 307 errorstr = NULL; 308 realhostname_sa(rhost, sizeof(rhost) - 1, fromp, fromp->sa_len); 309 rhost[sizeof(rhost) - 1] = '\0'; 310 /* XXX truncation! */ 311 312 (void) alarm(60); 313 getstr(ruser, sizeof(ruser), "ruser"); 314 getstr(luser, sizeof(luser), "luser"); 315 getstr(cmdbuf, maxcmdlen, "command"); 316 (void) alarm(0); 317 318 pam_err = pam_start("rsh", luser, &pamc, &pamh); 319 if (pam_err != PAM_SUCCESS) { 320 syslog(LOG_ERR|LOG_AUTH, "pam_start(): %s", 321 pam_strerror(pamh, pam_err)); 322#ifdef USE_BLACKLIST 323 blacklist(1, STDIN_FILENO, "login incorrect"); 324#endif 325 rshd_errx(1, "Login incorrect."); 326 } 327 328 if ((pam_err = pam_set_item(pamh, PAM_RUSER, ruser)) != PAM_SUCCESS || 329 (pam_err = pam_set_item(pamh, PAM_RHOST, rhost)) != PAM_SUCCESS) { 330 syslog(LOG_ERR|LOG_AUTH, "pam_set_item(): %s", 331 pam_strerror(pamh, pam_err)); 332#ifdef USE_BLACKLIST 333 blacklist(1, STDIN_FILENO, "login incorrect"); 334#endif 335 rshd_errx(1, "Login incorrect."); 336 } 337 338 pam_err = pam_authenticate(pamh, 0); 339 if (pam_err == PAM_SUCCESS) { 340 if ((pam_err = pam_get_user(pamh, &cp, NULL)) == PAM_SUCCESS) { 341 strncpy(luser, cp, sizeof(luser)); 342 luser[sizeof(luser) - 1] = '\0'; 343 /* XXX truncation! */ 344 } 345 pam_err = pam_acct_mgmt(pamh, 0); 346 } 347 if (pam_err != PAM_SUCCESS) { 348 syslog(LOG_INFO|LOG_AUTH, 349 "%s@%s as %s: permission denied (%s). cmd='%.80s'", 350 ruser, rhost, luser, pam_strerror(pamh, pam_err), cmdbuf); 351#ifdef USE_BLACKLIST 352 blacklist(1, STDIN_FILENO, "permission denied"); 353#endif 354 rshd_errx(1, "Login incorrect."); 355 } 356 357 setpwent(); 358 pwd = getpwnam(luser); 359 if (pwd == NULL) { 360 syslog(LOG_INFO|LOG_AUTH, 361 "%s@%s as %s: unknown login. cmd='%.80s'", 362 ruser, rhost, luser, cmdbuf); 363#ifdef USE_BLACKLIST 364 blacklist(1, STDIN_FILENO, "unknown login"); 365#endif 366 if (errorstr == NULL) 367 errorstr = "Login incorrect."; 368 rshd_errx(1, errorstr, rhost); 369 } 370 371 lc = login_getpwclass(pwd); 372 if (pwd->pw_uid) 373 auth_checknologin(lc); 374 375 if (chdir(pwd->pw_dir) < 0) { 376 if (chdir("/") < 0 || 377 login_getcapbool(lc, "requirehome", !!pwd->pw_uid)) { 378 syslog(LOG_INFO|LOG_AUTH, 379 "%s@%s as %s: no home directory. cmd='%.80s'", 380 ruser, rhost, luser, cmdbuf); 381 rshd_errx(0, "No remote home directory."); 382 } 383 pwd->pw_dir = slash; 384 } 385 386 if (lc != NULL && fromp->sa_family == AF_INET) { /*XXX*/ 387 char remote_ip[MAXHOSTNAMELEN]; 388 389 strncpy(remote_ip, numericname, 390 sizeof(remote_ip) - 1); 391 remote_ip[sizeof(remote_ip) - 1] = 0; 392 /* XXX truncation! */ 393 if (!auth_hostok(lc, rhost, remote_ip)) { 394 syslog(LOG_INFO|LOG_AUTH, 395 "%s@%s as %s: permission denied (%s). cmd='%.80s'", 396 ruser, rhost, luser, __rcmd_errstr, 397 cmdbuf); 398#ifdef USE_BLACKLIST 399 blacklist(1, STDIN_FILENO, "permission denied"); 400#endif 401 rshd_errx(1, "Login incorrect."); 402 } 403 if (!auth_timeok(lc, time(NULL))) 404 rshd_errx(1, "Logins not available right now"); 405 } 406 407 /* 408 * PAM modules might add supplementary groups in 409 * pam_setcred(), so initialize them first. 410 * But we need to open the session as root. 411 */ 412 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) { 413 syslog(LOG_ERR, "setusercontext: %m"); 414 exit(1); 415 } 416 417 if ((pam_err = pam_open_session(pamh, 0)) != PAM_SUCCESS) { 418 syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, pam_err)); 419 } else if ((pam_err = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) { 420 syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, pam_err)); 421 } 422 423 (void) write(STDERR_FILENO, "\0", 1); 424 sent_null = 1; 425 426 if (port) { 427 if (pipe(pv) < 0) 428 rshd_errx(1, "Can't make pipe."); 429 pid = fork(); 430 if (pid == -1) 431 rshd_errx(1, "Can't fork; try again."); 432 if (pid) { 433 (void) close(0); 434 (void) close(1); 435 (void) close(2); 436 (void) close(pv[1]); 437 438 FD_ZERO(&readfrom); 439 FD_SET(s, &readfrom); 440 FD_SET(pv[0], &readfrom); 441 if (pv[0] > s) 442 nfd = pv[0]; 443 else 444 nfd = s; 445 ioctl(pv[0], FIONBIO, (char *)&one); 446 447 /* should set s nbio! */ 448 nfd++; 449 do { 450 ready = readfrom; 451 if (select(nfd, &ready, (fd_set *)0, 452 (fd_set *)0, (struct timeval *)0) < 0) 453 break; 454 if (FD_ISSET(s, &ready)) { 455 int ret; 456 ret = read(s, &sig, 1); 457 if (ret <= 0) 458 FD_CLR(s, &readfrom); 459 else 460 killpg(pid, sig); 461 } 462 if (FD_ISSET(pv[0], &ready)) { 463 errno = 0; 464 cc = read(pv[0], buf, sizeof(buf)); 465 if (cc <= 0) { 466 shutdown(s, SHUT_RDWR); 467 FD_CLR(pv[0], &readfrom); 468 } else { 469 (void)write(s, buf, cc); 470 } 471 } 472 473 } while (FD_ISSET(s, &readfrom) || 474 FD_ISSET(pv[0], &readfrom)); 475 PAM_END; 476 exit(0); 477 } 478 (void) close(s); 479 (void) close(pv[0]); 480 dup2(pv[1], 2); 481 close(pv[1]); 482 } 483 else { 484 pid = fork(); 485 if (pid == -1) 486 rshd_errx(1, "Can't fork; try again."); 487 if (pid) { 488 /* Parent. */ 489 while (wait(NULL) > 0 || errno == EINTR) 490 /* nothing */ ; 491 PAM_END; 492 exit(0); 493 } 494 } 495 496#ifdef USE_BLACKLIST 497 blacklist(0, STDIN_FILENO, "success"); 498#endif 499 for (fd = getdtablesize(); fd > 2; fd--) 500 (void) close(fd); 501 if (setsid() == -1) 502 syslog(LOG_ERR, "setsid() failed: %m"); 503 if (setlogin(pwd->pw_name) < 0) 504 syslog(LOG_ERR, "setlogin() failed: %m"); 505 506 if (*pwd->pw_shell == '\0') 507 pwd->pw_shell = bshell; 508 (void) pam_setenv(pamh, "HOME", pwd->pw_dir, 1); 509 (void) pam_setenv(pamh, "SHELL", pwd->pw_shell, 1); 510 (void) pam_setenv(pamh, "USER", pwd->pw_name, 1); 511 (void) pam_setenv(pamh, "PATH", _PATH_DEFPATH, 1); 512 environ = pam_getenvlist(pamh); 513 (void) pam_end(pamh, pam_err); 514 cp = strrchr(pwd->pw_shell, '/'); 515 if (cp) 516 cp++; 517 else 518 cp = pwd->pw_shell; 519 520 if (setusercontext(lc, pwd, pwd->pw_uid, 521 LOGIN_SETALL & ~LOGIN_SETGROUP) < 0) { 522 syslog(LOG_ERR, "setusercontext(): %m"); 523 exit(1); 524 } 525 login_close(lc); 526 endpwent(); 527 if (log_success || pwd->pw_uid == 0) { 528 syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'", 529 ruser, rhost, luser, cmdbuf); 530 } 531 execl(pwd->pw_shell, cp, "-c", cmdbuf, (char *)NULL); 532 err(1, "%s", pwd->pw_shell); 533 exit(1); 534} 535 536/* 537 * Report error to client. Note: can't be used until second socket has 538 * connected to client, or older clients will hang waiting for that 539 * connection first. 540 */ 541 542static void 543rshd_errx(int errcode, const char *fmt, ...) 544{ 545 va_list ap; 546 547 va_start(ap, fmt); 548 549 if (sent_null == 0) 550 write(STDERR_FILENO, "\1", 1); 551 552 verrx(errcode, fmt, ap); 553 /* NOTREACHED */ 554} 555 556void 557getstr(char *buf, int cnt, const char *error) 558{ 559 char c; 560 561 do { 562 if (read(STDIN_FILENO, &c, 1) != 1) 563 exit(1); 564 *buf++ = c; 565 if (--cnt == 0) { 566#ifdef USE_BLACKLIST 567 blacklist(1, STDIN_FILENO, "buffer overflow"); 568#endif 569 rshd_errx(1, "%s too long", error); 570 } 571 } while (c != 0); 572} 573 574/* 575 * Check whether host h is in our local domain, 576 * defined as sharing the last two components of the domain part, 577 * or the entire domain part if the local domain has only one component. 578 * If either name is unqualified (contains no '.'), 579 * assume that the host is local, as it will be 580 * interpreted as such. 581 */ 582int 583local_domain(char *h) 584{ 585 char localhost[MAXHOSTNAMELEN]; 586 char *p1, *p2; 587 588 localhost[0] = 0; 589 (void) gethostname(localhost, sizeof(localhost) - 1); 590 localhost[sizeof(localhost) - 1] = '\0'; 591 /* XXX truncation! */ 592 p1 = topdomain(localhost); 593 p2 = topdomain(h); 594 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2)) 595 return (1); 596 return (0); 597} 598 599char * 600topdomain(char *h) 601{ 602 char *p, *maybe = NULL; 603 int dots = 0; 604 605 for (p = h + strlen(h); p >= h; p--) { 606 if (*p == '.') { 607 if (++dots == 2) 608 return (p); 609 maybe = p; 610 } 611 } 612 return (maybe); 613} 614 615void 616usage(void) 617{ 618 619 syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS); 620 exit(2); 621} 622