rshd.c revision 51433
1/*- 2 * Copyright (c) 1988, 1989, 1992, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static const char copyright[] = 36"@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41#if 0 42static const char sccsid[] = "@(#)rshd.c 8.2 (Berkeley) 4/6/94"; 43#endif 44static const char rcsid[] = 45 "$FreeBSD: head/libexec/rshd/rshd.c 51433 1999-09-19 22:05:32Z markm $"; 46#endif /* not lint */ 47 48/* 49 * remote shell server: 50 * [port]\0 51 * remuser\0 52 * locuser\0 53 * command\0 54 * data 55 */ 56#include <sys/param.h> 57#include <sys/ioctl.h> 58#include <sys/time.h> 59#include <sys/socket.h> 60 61#include <netinet/in_systm.h> 62#include <netinet/in.h> 63#include <netinet/ip.h> 64#include <netinet/tcp.h> 65#include <arpa/inet.h> 66#include <netdb.h> 67 68#include <errno.h> 69#include <fcntl.h> 70#include <libutil.h> 71#include <paths.h> 72#include <pwd.h> 73#include <signal.h> 74#include <stdio.h> 75#include <stdlib.h> 76#include <string.h> 77#include <syslog.h> 78#include <unistd.h> 79#ifdef LOGIN_CAP 80#include <login_cap.h> 81#endif 82 83int keepalive = 1; 84int log_success; /* If TRUE, log all successful accesses */ 85int sent_null; 86int no_delay; 87#ifdef CRYPT 88int doencrypt = 0; 89#endif 90 91void doit __P((struct sockaddr_in *)); 92void error __P((const char *, ...)); 93void getstr __P((char *, int, char *)); 94int local_domain __P((char *)); 95char *topdomain __P((char *)); 96void usage __P((void)); 97 98#ifndef NO_PAM 99extern int auth_pam __P((char *)); 100#endif 101 102#define OPTIONS "alnDL" 103 104int 105main(argc, argv) 106 int argc; 107 char *argv[]; 108{ 109 extern int __check_rhosts_file; 110 struct linger linger; 111 int ch, on = 1, fromlen; 112 struct sockaddr_in from; 113 114 openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON); 115 116 opterr = 0; 117 while ((ch = getopt(argc, argv, OPTIONS)) != -1) 118 switch (ch) { 119 case 'a': 120 /* ignored for compatability */ 121 break; 122 case 'l': 123 __check_rhosts_file = 0; 124 break; 125 case 'n': 126 keepalive = 0; 127 break; 128#ifdef CRYPT 129 case 'x': 130 doencrypt = 1; 131 break; 132#endif 133 case 'D': 134 no_delay = 1; 135 break; 136 case 'L': 137 log_success = 1; 138 break; 139 case '?': 140 default: 141 usage(); 142 break; 143 } 144 145 argc -= optind; 146 argv += optind; 147 148#ifdef CRYPT 149 if (doencrypt) { 150 syslog(LOG_ERR, "-k is required for -x"); 151 exit(2); 152 } 153#endif 154 155 fromlen = sizeof (from); 156 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) { 157 syslog(LOG_ERR, "getpeername: %m"); 158 exit(1); 159 } 160 if (keepalive && 161 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, 162 sizeof(on)) < 0) 163 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m"); 164 linger.l_onoff = 1; 165 linger.l_linger = 60; /* XXX */ 166 if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger, 167 sizeof (linger)) < 0) 168 syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m"); 169 if (no_delay && 170 setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) 171 syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m"); 172 doit(&from); 173 /* NOTREACHED */ 174 return(0); 175} 176 177char username[20] = "USER="; 178char homedir[64] = "HOME="; 179char shell[64] = "SHELL="; 180char path[100] = "PATH="; 181char *envinit[] = 182 {homedir, shell, path, username, 0}; 183char **environ; 184 185void 186doit(fromp) 187 struct sockaddr_in *fromp; 188{ 189 extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */ 190 struct passwd *pwd; 191 u_short port; 192 fd_set ready, readfrom; 193 int cc, nfd, pv[2], pid, s; 194 int one = 1; 195 char *errorstr; 196 char *cp, sig, buf[BUFSIZ]; 197 char cmdbuf[NCARGS+1], locuser[16], remuser[16]; 198 char fromhost[MAXHOSTNAMELEN]; 199 int retval; 200#ifdef CRYPT 201 int rc; 202 int pv1[2], pv2[2]; 203#endif 204#ifdef LOGIN_CAP 205 login_cap_t *lc; 206#endif 207 208 (void) signal(SIGINT, SIG_DFL); 209 (void) signal(SIGQUIT, SIG_DFL); 210 (void) signal(SIGTERM, SIG_DFL); 211#ifdef DEBUG 212 { int t = open(_PATH_TTY, 2); 213 if (t >= 0) { 214 ioctl(t, TIOCNOTTY, (char *)0); 215 (void) close(t); 216 } 217 } 218#endif 219 fromp->sin_port = ntohs((u_short)fromp->sin_port); 220 if (fromp->sin_family != AF_INET) { 221 syslog(LOG_ERR, "malformed \"from\" address (af %d)", 222 fromp->sin_family); 223 exit(1); 224 } 225#ifdef IP_OPTIONS 226 { 227 u_char optbuf[BUFSIZ/3]; 228 int optsize = sizeof(optbuf), ipproto, i; 229 struct protoent *ip; 230 231 if ((ip = getprotobyname("ip")) != NULL) 232 ipproto = ip->p_proto; 233 else 234 ipproto = IPPROTO_IP; 235 if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) && 236 optsize != 0) { 237 for (i = 0; i < optsize; ) { 238 u_char c = optbuf[i]; 239 if (c == IPOPT_LSRR || c == IPOPT_SSRR) { 240 syslog(LOG_NOTICE, 241 "connection refused from %s with IP option %s", 242 inet_ntoa(fromp->sin_addr), 243 c == IPOPT_LSRR ? "LSRR" : "SSRR"); 244 exit(1); 245 } 246 if (c == IPOPT_EOL) 247 break; 248 i += (c == IPOPT_NOP) ? 1 : optbuf[i+1]; 249 } 250 } 251 } 252#endif 253 254 if (fromp->sin_port >= IPPORT_RESERVED || 255 fromp->sin_port < IPPORT_RESERVED/2) { 256 syslog(LOG_NOTICE|LOG_AUTH, 257 "connection from %s on illegal port %u", 258 inet_ntoa(fromp->sin_addr), 259 fromp->sin_port); 260 exit(1); 261 } 262 263 (void) alarm(60); 264 port = 0; 265 s = 0; /* not set or used if port == 0 */ 266 for (;;) { 267 char c; 268 if ((cc = read(STDIN_FILENO, &c, 1)) != 1) { 269 if (cc < 0) 270 syslog(LOG_NOTICE, "read: %m"); 271 shutdown(0, 1+1); 272 exit(1); 273 } 274 if (c == 0) 275 break; 276 port = port * 10 + c - '0'; 277 } 278 279 (void) alarm(0); 280 if (port != 0) { 281 int lport = IPPORT_RESERVED - 1; 282 s = rresvport(&lport); 283 if (s < 0) { 284 syslog(LOG_ERR, "can't get stderr port: %m"); 285 exit(1); 286 } 287 if (port >= IPPORT_RESERVED || 288 port < IPPORT_RESERVED/2) { 289 syslog(LOG_NOTICE|LOG_AUTH, 290 "2nd socket from %s on unreserved port %u", 291 inet_ntoa(fromp->sin_addr), 292 port); 293 exit(1); 294 } 295 fromp->sin_port = htons(port); 296 if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0) { 297 syslog(LOG_INFO, "connect second port %d: %m", port); 298 exit(1); 299 } 300 } 301 302 errorstr = NULL; 303 realhostname(fromhost, sizeof(fromhost) - 1, &fromp->sin_addr); 304 fromhost[sizeof(fromhost) - 1] = '\0'; 305 306#ifdef CRYPT 307 if (doencrypt) { 308 struct sockaddr_in local_addr; 309 rc = sizeof(local_addr); 310 if (getsockname(0, (struct sockaddr *)&local_addr, 311 &rc) < 0) { 312 syslog(LOG_ERR, "getsockname: %m"); 313 error("rlogind: getsockname: %m"); 314 exit(1); 315 } 316 authopts = KOPT_DO_MUTUAL; 317 rc = krb_recvauth(authopts, 0, ticket, 318 "rcmd", instance, &fromaddr, 319 &local_addr, kdata, "", schedule, 320 version); 321 des_set_key(&kdata->session, schedule); 322 } 323#endif 324 getstr(remuser, sizeof(remuser), "remuser"); 325 326 getstr(locuser, sizeof(locuser), "locuser"); 327 getstr(cmdbuf, sizeof(cmdbuf), "command"); 328 setpwent(); 329 pwd = getpwnam(locuser); 330 if (pwd == NULL) { 331 syslog(LOG_INFO|LOG_AUTH, 332 "%s@%s as %s: unknown login. cmd='%.80s'", 333 remuser, fromhost, locuser, cmdbuf); 334 if (errorstr == NULL) 335 errorstr = "Login incorrect.\n"; 336 goto fail; 337 } 338#ifdef LOGIN_CAP 339 lc = login_getpwclass(pwd); 340#endif 341 if (chdir(pwd->pw_dir) < 0) { 342#ifdef LOGIN_CAP 343 if (chdir("/") < 0 || 344 login_getcapbool(lc, "requirehome", !!pwd->pw_uid)) { 345 syslog(LOG_INFO|LOG_AUTH, 346 "%s@%s as %s: no home directory. cmd='%.80s'", 347 remuser, fromhost, locuser, cmdbuf); 348 error("No remote home directory.\n"); 349 exit(0); 350 } 351#else 352 (void) chdir("/"); 353#ifdef notdef 354 syslog(LOG_INFO|LOG_AUTH, 355 "%s@%s as %s: no home directory. cmd='%.80s'", 356 remuser, fromhost, locuser, cmdbuf); 357 error("No remote directory.\n"); 358 exit(1); 359#endif 360#endif 361 pwd->pw_dir = "/"; 362 } 363 364#ifndef NO_PAM 365 retval = auth_pam(locuser); 366 367 if (retval) { 368 if (retval == -1) { 369 syslog(LOG_ERR,"PAM authentication failed"); 370 } 371 else { 372 syslog(LOG_ERR, 373 "User %s failed PAM authentication", locuser); 374 exit(1); 375 } 376 } 377#endif 378 379 if (errorstr || 380 (pwd->pw_expire && time(NULL) >= pwd->pw_expire) || 381 (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' && 382 iruserok(fromp->sin_addr.s_addr, pwd->pw_uid == 0, 383 remuser, locuser) < 0)) { 384 if (__rcmd_errstr) 385 syslog(LOG_INFO|LOG_AUTH, 386 "%s@%s as %s: permission denied (%s). cmd='%.80s'", 387 remuser, fromhost, locuser, __rcmd_errstr, 388 cmdbuf); 389 else 390 syslog(LOG_INFO|LOG_AUTH, 391 "%s@%s as %s: permission denied. cmd='%.80s'", 392 remuser, fromhost, locuser, cmdbuf); 393fail: 394 if (errorstr == NULL) 395 errorstr = "Login incorrect.\n"; 396 error(errorstr, fromhost); 397 exit(1); 398 } 399 400 if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) { 401 error("Logins currently disabled.\n"); 402 exit(1); 403 } 404#ifdef LOGIN_CAP 405 if (lc != NULL) { 406 char remote_ip[MAXHOSTNAMELEN]; 407 408 strncpy(remote_ip, inet_ntoa(fromp->sin_addr), 409 sizeof(remote_ip) - 1); 410 remote_ip[sizeof(remote_ip) - 1] = 0; 411 if (!auth_hostok(lc, fromhost, remote_ip)) { 412 syslog(LOG_INFO|LOG_AUTH, 413 "%s@%s as %s: permission denied (%s). cmd='%.80s'", 414 remuser, fromhost, locuser, __rcmd_errstr, 415 cmdbuf); 416 error("Login incorrect.\n"); 417 exit(1); 418 } 419 if (!auth_timeok(lc, time(NULL))) { 420 error("Logins not available right now\n"); 421 exit(1); 422 } 423 } 424#endif /* !LOGIN_CAP */ 425#if BSD > 43 426 /* before fork, while we're session leader */ 427 if (setlogin(pwd->pw_name) < 0) 428 syslog(LOG_ERR, "setlogin() failed: %m"); 429#endif 430 431 (void) write(STDERR_FILENO, "\0", 1); 432 sent_null = 1; 433 434 if (port) { 435 if (pipe(pv) < 0) { 436 error("Can't make pipe.\n"); 437 exit(1); 438 } 439#ifdef CRYPT 440 if (doencrypt) { 441 if (pipe(pv1) < 0) { 442 error("Can't make 2nd pipe.\n"); 443 exit(1); 444 } 445 if (pipe(pv2) < 0) { 446 error("Can't make 3rd pipe.\n"); 447 exit(1); 448 } 449 } 450#endif 451 pid = fork(); 452 if (pid == -1) { 453 error("Can't fork; try again.\n"); 454 exit(1); 455 } 456 if (pid) { 457#ifdef CRYPT 458 if (doencrypt) { 459 static char msg[] = SECURE_MESSAGE; 460 (void) close(pv1[1]); 461 (void) close(pv2[1]); 462 des_enc_write(s, msg, sizeof(msg) - 1, 463 schedule, &kdata->session); 464 465 } else 466#endif 467 { 468 (void) close(0); 469 (void) close(1); 470 } 471 (void) close(2); 472 (void) close(pv[1]); 473 474 FD_ZERO(&readfrom); 475 FD_SET(s, &readfrom); 476 FD_SET(pv[0], &readfrom); 477 if (pv[0] > s) 478 nfd = pv[0]; 479 else 480 nfd = s; 481#ifdef CRYPT 482 if (doencrypt) { 483 FD_ZERO(&writeto); 484 FD_SET(pv2[0], &writeto); 485 FD_SET(pv1[0], &readfrom); 486 487 nfd = MAX(nfd, pv2[0]); 488 nfd = MAX(nfd, pv1[0]); 489 } else 490#endif 491 ioctl(pv[0], FIONBIO, (char *)&one); 492 493 /* should set s nbio! */ 494 nfd++; 495 do { 496 ready = readfrom; 497#ifdef CRYPT 498 if (doencrypt) { 499 wready = writeto; 500 if (select(nfd, &ready, 501 &wready, (fd_set *) 0, 502 (struct timeval *) 0) < 0) 503 break; 504 } else 505#endif 506 if (select(nfd, &ready, (fd_set *)0, 507 (fd_set *)0, (struct timeval *)0) < 0) 508 break; 509 if (FD_ISSET(s, &ready)) { 510 int ret; 511#ifdef CRYPT 512 if (doencrypt) 513 ret = des_enc_read(s, &sig, 1, 514 schedule, &kdata->session); 515 else 516#endif 517 ret = read(s, &sig, 1); 518 if (ret <= 0) 519 FD_CLR(s, &readfrom); 520 else 521 killpg(pid, sig); 522 } 523 if (FD_ISSET(pv[0], &ready)) { 524 errno = 0; 525 cc = read(pv[0], buf, sizeof(buf)); 526 if (cc <= 0) { 527 shutdown(s, 1+1); 528 FD_CLR(pv[0], &readfrom); 529 } else { 530#ifdef CRYPT 531 if (doencrypt) 532 (void) 533 des_enc_write(s, buf, cc, 534 schedule, &kdata->session); 535 else 536#endif 537 (void) 538 write(s, buf, cc); 539 } 540 } 541#ifdef CRYPT 542 if (doencrypt && FD_ISSET(pv1[0], &ready)) { 543 errno = 0; 544 cc = read(pv1[0], buf, sizeof(buf)); 545 if (cc <= 0) { 546 shutdown(pv1[0], 1+1); 547 FD_CLR(pv1[0], &readfrom); 548 } else 549 (void) des_enc_write(STDOUT_FILENO, 550 buf, cc, 551 schedule, &kdata->session); 552 } 553 554 if (doencrypt && FD_ISSET(pv2[0], &wready)) { 555 errno = 0; 556 cc = des_enc_read(STDIN_FILENO, 557 buf, sizeof(buf), 558 schedule, &kdata->session); 559 if (cc <= 0) { 560 shutdown(pv2[0], 1+1); 561 FD_CLR(pv2[0], &writeto); 562 } else 563 (void) write(pv2[0], buf, cc); 564 } 565#endif 566 567 } while (FD_ISSET(s, &readfrom) || 568#ifdef CRYPT 569 (doencrypt && FD_ISSET(pv1[0], &readfrom)) || 570#endif 571 FD_ISSET(pv[0], &readfrom)); 572 exit(0); 573 } 574 setpgrp(0, getpid()); 575 (void) close(s); 576 (void) close(pv[0]); 577#ifdef CRYPT 578 if (doencrypt) { 579 close(pv1[0]); close(pv2[0]); 580 dup2(pv1[1], 1); 581 dup2(pv2[1], 0); 582 close(pv1[1]); 583 close(pv2[1]); 584 } 585#endif 586 dup2(pv[1], 2); 587 close(pv[1]); 588 } 589 if (*pwd->pw_shell == '\0') 590 pwd->pw_shell = _PATH_BSHELL; 591 environ = envinit; 592 strncat(homedir, pwd->pw_dir, sizeof(homedir)-6); 593 strcat(path, _PATH_DEFPATH); 594 strncat(shell, pwd->pw_shell, sizeof(shell)-7); 595 strncat(username, pwd->pw_name, sizeof(username)-6); 596 cp = strrchr(pwd->pw_shell, '/'); 597 if (cp) 598 cp++; 599 else 600 cp = pwd->pw_shell; 601#ifdef LOGIN_CAP 602 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETALL) != 0) { 603 syslog(LOG_ERR, "setusercontext: %m"); 604 exit(1); 605 } 606 login_close(lc); 607#else 608 (void) setgid((gid_t)pwd->pw_gid); 609 initgroups(pwd->pw_name, pwd->pw_gid); 610 (void) setuid((uid_t)pwd->pw_uid); 611#endif 612 endpwent(); 613 if (log_success || pwd->pw_uid == 0) { 614 syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'", 615 remuser, fromhost, locuser, cmdbuf); 616 } 617 execl(pwd->pw_shell, cp, "-c", cmdbuf, 0); 618 perror(pwd->pw_shell); 619 exit(1); 620} 621 622/* 623 * Report error to client. Note: can't be used until second socket has 624 * connected to client, or older clients will hang waiting for that 625 * connection first. 626 */ 627#if __STDC__ 628#include <stdarg.h> 629#else 630#include <varargs.h> 631#endif 632 633void 634#if __STDC__ 635error(const char *fmt, ...) 636#else 637error(fmt, va_alist) 638 char *fmt; 639 va_dcl 640#endif 641{ 642 va_list ap; 643 int len; 644 char *bp, buf[BUFSIZ]; 645#if __STDC__ 646 va_start(ap, fmt); 647#else 648 va_start(ap); 649#endif 650 bp = buf; 651 if (sent_null == 0) { 652 *bp++ = 1; 653 len = 1; 654 } else 655 len = 0; 656 (void)vsnprintf(bp, sizeof(buf) - 1, fmt, ap); 657 (void)write(STDERR_FILENO, buf, len + strlen(bp)); 658} 659 660void 661getstr(buf, cnt, err) 662 char *buf, *err; 663 int cnt; 664{ 665 char c; 666 667 do { 668 if (read(STDIN_FILENO, &c, 1) != 1) 669 exit(1); 670 *buf++ = c; 671 if (--cnt == 0) { 672 error("%s too long\n", err); 673 exit(1); 674 } 675 } while (c != 0); 676} 677 678/* 679 * Check whether host h is in our local domain, 680 * defined as sharing the last two components of the domain part, 681 * or the entire domain part if the local domain has only one component. 682 * If either name is unqualified (contains no '.'), 683 * assume that the host is local, as it will be 684 * interpreted as such. 685 */ 686int 687local_domain(h) 688 char *h; 689{ 690 char localhost[MAXHOSTNAMELEN]; 691 char *p1, *p2; 692 693 localhost[0] = 0; 694 (void) gethostname(localhost, sizeof(localhost) - 1); 695 localhost[sizeof(localhost) - 1] = '\0'; 696 p1 = topdomain(localhost); 697 p2 = topdomain(h); 698 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2)) 699 return (1); 700 return (0); 701} 702 703char * 704topdomain(h) 705 char *h; 706{ 707 char *p, *maybe = NULL; 708 int dots = 0; 709 710 for (p = h + strlen(h); p >= h; p--) { 711 if (*p == '.') { 712 if (++dots == 2) 713 return (p); 714 maybe = p; 715 } 716 } 717 return (maybe); 718} 719 720void 721usage() 722{ 723 724 syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS); 725 exit(2); 726} 727