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