rshd.c revision 69707
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 69707 2000-12-07 15:09:48Z ru $"; 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 83/* wrapper for KAME-special getnameinfo() */ 84#ifndef NI_WITHSCOPEID 85#define NI_WITHSCOPEID 0 86#endif 87 88int keepalive = 1; 89int log_success; /* If TRUE, log all successful accesses */ 90int sent_null; 91int no_delay; 92#ifdef CRYPT 93int doencrypt = 0; 94#endif 95 96union sockunion { 97 struct sockinet { 98 u_char si_len; 99 u_char si_family; 100 u_short si_port; 101 } su_si; 102 struct sockaddr_in su_sin; 103 struct sockaddr_in6 su_sin6; 104}; 105#define su_len su_si.si_len 106#define su_family su_si.si_family 107#define su_port su_si.si_port 108 109void doit __P((union sockunion *)); 110void error __P((const char *, ...)); 111void getstr __P((char *, int, char *)); 112int local_domain __P((char *)); 113char *topdomain __P((char *)); 114void usage __P((void)); 115 116#define OPTIONS "alnDL" 117 118int 119main(argc, argv) 120 int argc; 121 char *argv[]; 122{ 123 extern int __check_rhosts_file; 124 struct linger linger; 125 int ch, on = 1, fromlen; 126 struct sockaddr_storage from; 127 128 openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON); 129 130 opterr = 0; 131 while ((ch = getopt(argc, argv, OPTIONS)) != -1) 132 switch (ch) { 133 case 'a': 134 /* ignored for compatability */ 135 break; 136 case 'l': 137 __check_rhosts_file = 0; 138 break; 139 case 'n': 140 keepalive = 0; 141 break; 142#ifdef CRYPT 143 case 'x': 144 doencrypt = 1; 145 break; 146#endif 147 case 'D': 148 no_delay = 1; 149 break; 150 case 'L': 151 log_success = 1; 152 break; 153 case '?': 154 default: 155 usage(); 156 break; 157 } 158 159 argc -= optind; 160 argv += optind; 161 162#ifdef CRYPT 163 if (doencrypt) { 164 syslog(LOG_ERR, "-k is required for -x"); 165 exit(2); 166 } 167#endif 168 169 fromlen = sizeof (from); 170 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) { 171 syslog(LOG_ERR, "getpeername: %m"); 172 exit(1); 173 } 174 if (keepalive && 175 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, 176 sizeof(on)) < 0) 177 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m"); 178 linger.l_onoff = 1; 179 linger.l_linger = 60; /* XXX */ 180 if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger, 181 sizeof (linger)) < 0) 182 syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m"); 183 if (no_delay && 184 setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) 185 syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m"); 186 doit((union sockunion *)&from); 187 /* NOTREACHED */ 188 return(0); 189} 190 191char username[20] = "USER="; 192char homedir[64] = "HOME="; 193char shell[64] = "SHELL="; 194char path[100] = "PATH="; 195char *envinit[] = 196 {homedir, shell, path, username, 0}; 197char **environ; 198 199void 200doit(fromp) 201 union sockunion *fromp; 202{ 203 extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */ 204 struct passwd *pwd; 205 u_short port; 206 fd_set ready, readfrom; 207 int cc, nfd, pv[2], pid, s; 208 int one = 1; 209 char *errorstr; 210 char *cp, sig, buf[BUFSIZ]; 211 char cmdbuf[NCARGS+1], locuser[16], remuser[16]; 212 char fromhost[2 * MAXHOSTNAMELEN + 1]; 213 char numericname[INET6_ADDRSTRLEN]; 214 int af = fromp->su_family, err; 215#ifdef CRYPT 216 int rc; 217 int pv1[2], pv2[2]; 218#endif 219#ifdef LOGIN_CAP 220 login_cap_t *lc; 221#endif 222 223 (void) signal(SIGINT, SIG_DFL); 224 (void) signal(SIGQUIT, SIG_DFL); 225 (void) signal(SIGTERM, SIG_DFL); 226 fromp->su_port = ntohs((u_short)fromp->su_port); 227 if (af != AF_INET 228#ifdef INET6 229 && af != AF_INET6 230#endif 231 ) { 232 syslog(LOG_ERR, "malformed \"from\" address (af %d)\n", af); 233 exit(1); 234 } 235 err = getnameinfo((struct sockaddr *)fromp, fromp->su_len, numericname, 236 sizeof(numericname), NULL, 0, 237 NI_NUMERICHOST|NI_WITHSCOPEID); 238 /* XXX: do 'err' check */ 239#ifdef IP_OPTIONS 240 if (af == AF_INET) { 241 u_char optbuf[BUFSIZ/3]; 242 int optsize = sizeof(optbuf), ipproto, i; 243 struct protoent *ip; 244 245 if ((ip = getprotobyname("ip")) != NULL) 246 ipproto = ip->p_proto; 247 else 248 ipproto = IPPROTO_IP; 249 if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) && 250 optsize != 0) { 251 for (i = 0; i < optsize; ) { 252 u_char c = optbuf[i]; 253 if (c == IPOPT_LSRR || c == IPOPT_SSRR) { 254 syslog(LOG_NOTICE, 255 "connection refused from %s with IP option %s", 256 numericname, 257 c == IPOPT_LSRR ? "LSRR" : "SSRR"); 258 exit(1); 259 } 260 if (c == IPOPT_EOL) 261 break; 262 i += (c == IPOPT_NOP) ? 1 : optbuf[i+1]; 263 } 264 } 265 } 266#endif 267 268 if (fromp->su_port >= IPPORT_RESERVED || 269 fromp->su_port < IPPORT_RESERVED/2) { 270 syslog(LOG_NOTICE|LOG_AUTH, 271 "connection from %s on illegal port %u", 272 numericname, 273 fromp->su_port); 274 exit(1); 275 } 276 277 (void) alarm(60); 278 port = 0; 279 s = 0; /* not set or used if port == 0 */ 280 for (;;) { 281 char c; 282 if ((cc = read(STDIN_FILENO, &c, 1)) != 1) { 283 if (cc < 0) 284 syslog(LOG_NOTICE, "read: %m"); 285 shutdown(0, 1+1); 286 exit(1); 287 } 288 if (c == 0) 289 break; 290 port = port * 10 + c - '0'; 291 } 292 293 (void) alarm(0); 294 if (port != 0) { 295 int lport = IPPORT_RESERVED - 1; 296 s = rresvport_af(&lport, af); 297 if (s < 0) { 298 syslog(LOG_ERR, "can't get stderr port: %m"); 299 exit(1); 300 } 301 if (port >= IPPORT_RESERVED || 302 port < IPPORT_RESERVED/2) { 303 syslog(LOG_NOTICE|LOG_AUTH, 304 "2nd socket from %s on unreserved port %u", 305 numericname, 306 port); 307 exit(1); 308 } 309 fromp->su_port = htons(port); 310 if (connect(s, (struct sockaddr *)fromp, fromp->su_len) < 0) { 311 syslog(LOG_INFO, "connect second port %d: %m", port); 312 exit(1); 313 } 314 } 315 316 errorstr = NULL; 317 realhostname_sa(fromhost, sizeof(fromhost) - 1, 318 (struct sockaddr *)fromp, 319 fromp->su_len); 320 fromhost[sizeof(fromhost) - 1] = '\0'; 321 322#ifdef CRYPT 323 if (doencrypt && af == AF_INET) { 324 struct sockaddr_in local_addr; 325 rc = sizeof(local_addr); 326 if (getsockname(0, (struct sockaddr *)&local_addr, 327 &rc) < 0) { 328 syslog(LOG_ERR, "getsockname: %m"); 329 error("rlogind: getsockname: %m"); 330 exit(1); 331 } 332 authopts = KOPT_DO_MUTUAL; 333 rc = krb_recvauth(authopts, 0, ticket, 334 "rcmd", instance, &fromaddr, 335 &local_addr, kdata, "", schedule, 336 version); 337 des_set_key(&kdata->session, schedule); 338 } 339#endif 340 getstr(remuser, sizeof(remuser), "remuser"); 341 342 getstr(locuser, sizeof(locuser), "locuser"); 343 getstr(cmdbuf, sizeof(cmdbuf), "command"); 344 setpwent(); 345 pwd = getpwnam(locuser); 346 if (pwd == NULL) { 347 syslog(LOG_INFO|LOG_AUTH, 348 "%s@%s as %s: unknown login. cmd='%.80s'", 349 remuser, fromhost, locuser, cmdbuf); 350 if (errorstr == NULL) 351 errorstr = "Login incorrect.\n"; 352 goto fail; 353 } 354#ifdef LOGIN_CAP 355 lc = login_getpwclass(pwd); 356#endif 357 if (chdir(pwd->pw_dir) < 0) { 358#ifdef LOGIN_CAP 359 if (chdir("/") < 0 || 360 login_getcapbool(lc, "requirehome", !!pwd->pw_uid)) { 361 syslog(LOG_INFO|LOG_AUTH, 362 "%s@%s as %s: no home directory. cmd='%.80s'", 363 remuser, fromhost, locuser, cmdbuf); 364 error("No remote home directory.\n"); 365 exit(0); 366 } 367#else 368 (void) chdir("/"); 369#ifdef notdef 370 syslog(LOG_INFO|LOG_AUTH, 371 "%s@%s as %s: no home directory. cmd='%.80s'", 372 remuser, fromhost, locuser, cmdbuf); 373 error("No remote directory.\n"); 374 exit(1); 375#endif 376#endif 377 pwd->pw_dir = "/"; 378 } 379 380 if (errorstr || 381 (pwd->pw_expire && time(NULL) >= pwd->pw_expire) || 382 iruserok_sa(fromp, fromp->su_len, 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 && fromp->su_family == AF_INET) { /*XXX*/ 406 char remote_ip[MAXHOSTNAMELEN]; 407 408 strncpy(remote_ip, numericname, 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