Home | History | Annotate | Line # | Download | only in rshd
rshd.c revision 1.6
      1 /*-
      2  * Copyright (c) 1988, 1989 The Regents of the University of California.
      3  * 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
     35 char copyright[] =
     36 "@(#) Copyright (c) 1988, 1989 The Regents of the University of California.\n\
     37  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 /*static char sccsid[] = "from: @(#)rshd.c	5.38 (Berkeley) 3/2/91";*/
     42 static char rcsid[] = "$Id: rshd.c,v 1.6 1994/05/19 22:54:19 pk Exp $";
     43 #endif /* not lint */
     44 
     45 /*
     46  * From:
     47  *	$Source: /tank/opengrok/rsync2/NetBSD/src/libexec/rshd/rshd.c,v $
     48  *	$Header: /mit/kerberos/ucb/mit/rshd/RCS/rshd.c,v
     49  *		5.2 89/07/31 19:30:04 kfall Exp $
     50  */
     51 
     52 /*
     53  * remote shell server:
     54  *	[port]\0
     55  *	remuser\0
     56  *	locuser\0
     57  *	command\0
     58  *	data
     59  */
     60 #include <sys/param.h>
     61 #include <sys/ioctl.h>
     62 #include <sys/time.h>
     63 #include <fcntl.h>
     64 #include <signal.h>
     65 
     66 #include <sys/socket.h>
     67 #include <netinet/in.h>
     68 #include <arpa/inet.h>
     69 #include <netdb.h>
     70 
     71 #include <pwd.h>
     72 #include <syslog.h>
     73 #include <arpa/nameser.h>
     74 #include <resolv.h>
     75 #include <unistd.h>
     76 #include <errno.h>
     77 #include <stdio.h>
     78 #include <stdlib.h>
     79 #include <string.h>
     80 #include <paths.h>
     81 
     82 int	keepalive = 1;
     83 int	check_all = 0;
     84 int	paranoid = 0;
     85 char	*index(), *rindex(), *strncat();
     86 /*VARARGS1*/
     87 int	error();
     88 int	sent_null;
     89 
     90 #ifdef	KERBEROS
     91 #include <kerberosIV/des.h>
     92 #include <kerberosIV/krb.h>
     93 #define	VERSION_SIZE	9
     94 #define SECURE_MESSAGE  "This rsh session is using DES encryption for all transmissions.\r\n"
     95 #define	OPTIONS		"alknvxL"
     96 char	authbuf[sizeof(AUTH_DAT)];
     97 char	tickbuf[sizeof(KTEXT_ST)];
     98 int	doencrypt, use_kerberos, vacuous;
     99 Key_schedule	schedule;
    100 #else
    101 #define	OPTIONS	"alnL"
    102 #endif
    103 
    104 /*ARGSUSED*/
    105 main(argc, argv)
    106 	int argc;
    107 	char **argv;
    108 {
    109 	extern int opterr, optind;
    110 	extern int __check_rhosts_file;
    111 	struct linger linger;
    112 	int ch, on = 1, fromlen;
    113 	struct sockaddr_in from;
    114 
    115 	openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
    116 
    117 	opterr = 0;
    118 	while ((ch = getopt(argc, argv, OPTIONS)) != EOF)
    119 		switch (ch) {
    120 		case 'a':
    121 			check_all = 1;
    122 			break;
    123 
    124 		case 'l':
    125 			__check_rhosts_file = 0;
    126 			break;
    127 
    128 		case 'n':
    129 			keepalive = 0;
    130 			break;
    131 #ifdef	KERBEROS
    132 		case 'k':
    133 			use_kerberos = 1;
    134 			break;
    135 
    136 		case 'v':
    137 			vacuous = 1;
    138 			break;
    139 
    140 #ifdef CRYPT
    141 		case 'x':
    142 			doencrypt = 1;
    143 			break;
    144 #endif
    145 #endif
    146 
    147 		case 'L':
    148 			paranoid = 1;
    149 			break;
    150 
    151 		case '?':
    152 		default:
    153 			usage();
    154 			exit(2);
    155 		}
    156 
    157 	argc -= optind;
    158 	argv += optind;
    159 
    160 #ifdef	KERBEROS
    161 	if (use_kerberos && vacuous) {
    162 		syslog(LOG_ERR, "only one of -k and -v allowed");
    163 		exit(2);
    164 	}
    165 #ifdef CRYPT
    166 	if (doencrypt && !use_kerberos) {
    167 		syslog(LOG_ERR, "-k is required for -x");
    168 		exit(2);
    169 	}
    170 #endif
    171 #endif
    172 
    173 	fromlen = sizeof (from);
    174 	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
    175 		syslog(LOG_ERR, "getpeername: %m");
    176 		_exit(1);
    177 	}
    178 	if (keepalive &&
    179 	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
    180 	    sizeof(on)) < 0)
    181 		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
    182 	linger.l_onoff = 1;
    183 	linger.l_linger = 60;			/* XXX */
    184 	if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger,
    185 	    sizeof (linger)) < 0)
    186 		syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
    187 	doit(&from);
    188 }
    189 
    190 char	username[20] = "USER=";
    191 char	homedir[64] = "HOME=";
    192 char	shell[64] = "SHELL=";
    193 char	path[100] = "PATH=";
    194 char	*envinit[] =
    195 	    {homedir, shell, path, username, 0};
    196 char	**environ;
    197 
    198 doit(fromp)
    199 	struct sockaddr_in *fromp;
    200 {
    201 	char cmdbuf[NCARGS+1], *cp;
    202 	char locuser[16], remuser[16];
    203 	struct passwd *pwd;
    204 	int s;
    205 	struct hostent *hp;
    206 	char *hostname, *errorstr = NULL, *errorhost;
    207 	u_short port;
    208 	int pv[2], pid, cc;
    209 	int nfd;
    210 	fd_set ready, readfrom;
    211 	char buf[BUFSIZ], sig;
    212 	int one = 1;
    213 	char remotehost[2 * MAXHOSTNAMELEN + 1];
    214 
    215 #ifdef	KERBEROS
    216 	AUTH_DAT	*kdata = (AUTH_DAT *) NULL;
    217 	KTEXT		ticket = (KTEXT) NULL;
    218 	char		instance[INST_SZ], version[VERSION_SIZE];
    219 	struct		sockaddr_in	fromaddr;
    220 	int		rc;
    221 	long		authopts;
    222 	int		pv1[2], pv2[2];
    223 	fd_set		wready, writeto;
    224 
    225 	fromaddr = *fromp;
    226 #endif
    227 
    228 	(void) signal(SIGINT, SIG_DFL);
    229 	(void) signal(SIGQUIT, SIG_DFL);
    230 	(void) signal(SIGTERM, SIG_DFL);
    231 #ifdef DEBUG
    232 	{ int t = open(_PATH_TTY, 2);
    233 	  if (t >= 0) {
    234 		ioctl(t, TIOCNOTTY, (char *)0);
    235 		(void) close(t);
    236 	  }
    237 	}
    238 #endif
    239 	fromp->sin_port = ntohs((u_short)fromp->sin_port);
    240 	if (fromp->sin_family != AF_INET) {
    241 		syslog(LOG_ERR, "malformed \"from\" address (af %d)\n",
    242 		    fromp->sin_family);
    243 		exit(1);
    244 	}
    245 #ifdef IP_OPTIONS
    246       {
    247 	u_char optbuf[BUFSIZ/3], *cp;
    248 	char lbuf[BUFSIZ], *lp;
    249 	int optsize = sizeof(optbuf), ipproto;
    250 	struct protoent *ip;
    251 
    252 	if ((ip = getprotobyname("ip")) != NULL)
    253 		ipproto = ip->p_proto;
    254 	else
    255 		ipproto = IPPROTO_IP;
    256 	if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) &&
    257 	    optsize != 0) {
    258 		lp = lbuf;
    259 		for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
    260 			sprintf(lp, " %2.2x", *cp);
    261 		syslog(LOG_NOTICE,
    262 		    "Connection received from %s using IP options (ignored):%s",
    263 		    inet_ntoa(fromp->sin_addr), lbuf);
    264 		if (setsockopt(0, ipproto, IP_OPTIONS,
    265 		    (char *)NULL, optsize) != 0) {
    266 			syslog(LOG_ERR, "setsockopt IP_OPTIONS NULL: %m");
    267 			exit(1);
    268 		}
    269 	}
    270       }
    271 #endif
    272 
    273 #ifdef	KERBEROS
    274 	if (!use_kerberos)
    275 #endif
    276 		if (fromp->sin_port >= IPPORT_RESERVED ||
    277 		    fromp->sin_port < IPPORT_RESERVED/2) {
    278 			syslog(LOG_NOTICE|LOG_AUTH,
    279 			    "Connection from %s on illegal port",
    280 			    inet_ntoa(fromp->sin_addr));
    281 			exit(1);
    282 		}
    283 
    284 	(void) alarm(60);
    285 	port = 0;
    286 	for (;;) {
    287 		char c;
    288 		if ((cc = read(0, &c, 1)) != 1) {
    289 			if (cc < 0)
    290 				syslog(LOG_NOTICE, "read: %m");
    291 			shutdown(0, 1+1);
    292 			exit(1);
    293 		}
    294 		if (c== 0)
    295 			break;
    296 		port = port * 10 + c - '0';
    297 	}
    298 
    299 	(void) alarm(0);
    300 	if (port != 0) {
    301 		int lport = IPPORT_RESERVED - 1;
    302 		s = rresvport(&lport);
    303 		if (s < 0) {
    304 			syslog(LOG_ERR, "can't get stderr port: %m");
    305 			exit(1);
    306 		}
    307 #ifdef	KERBEROS
    308 		if (!use_kerberos)
    309 #endif
    310 			if (port >= IPPORT_RESERVED) {
    311 				syslog(LOG_ERR, "2nd port not reserved\n");
    312 				exit(1);
    313 			}
    314 		fromp->sin_port = htons(port);
    315 		if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0) {
    316 			syslog(LOG_INFO, "connect second port: %m");
    317 			exit(1);
    318 		}
    319 	}
    320 
    321 #ifdef	KERBEROS
    322 	if (vacuous) {
    323 		error("rshd: remote host requires Kerberos authentication\n");
    324 		exit(1);
    325 	}
    326 #endif
    327 
    328 #ifdef notdef
    329 	/* from inetd, socket is already on 0, 1, 2 */
    330 	dup2(f, 0);
    331 	dup2(f, 1);
    332 	dup2(f, 2);
    333 #endif
    334 	hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof (struct in_addr),
    335 		fromp->sin_family);
    336 	if (hp) {
    337 		/*
    338 		 * If name returned by gethostbyaddr is in our domain,
    339 		 * attempt to verify that we haven't been fooled by someone
    340 		 * in a remote net; look up the name and check that this
    341 		 * address corresponds to the name.
    342 		 */
    343 		hostname = hp->h_name;
    344 #ifdef	KERBEROS
    345 		if (!use_kerberos)
    346 #endif
    347 		if (check_all || local_domain(hp->h_name)) {
    348 			strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1);
    349 			remotehost[sizeof(remotehost) - 1] = 0;
    350 			errorhost = remotehost;
    351 #ifdef	RES_DNSRCH
    352 			_res.options &= ~RES_DNSRCH;
    353 #endif
    354 			hp = gethostbyname(remotehost);
    355 			if (hp == NULL) {
    356 				syslog(LOG_INFO,
    357 				    "Couldn't look up address for %s",
    358 				    remotehost);
    359 				errorstr =
    360 				"Couldn't look up address for your host (%s)\n";
    361 				hostname = inet_ntoa(fromp->sin_addr);
    362 			} else for (; ; hp->h_addr_list++) {
    363 				if (hp->h_addr_list[0] == NULL) {
    364 					syslog(LOG_NOTICE,
    365 					  "Host addr %s not listed for host %s",
    366 					    inet_ntoa(fromp->sin_addr),
    367 					    hp->h_name);
    368 					errorstr =
    369 					    "Host address mismatch for %s\n";
    370 					hostname = inet_ntoa(fromp->sin_addr);
    371 					break;
    372 				}
    373 				if (!bcmp(hp->h_addr_list[0],
    374 				    (caddr_t)&fromp->sin_addr,
    375 				    sizeof(fromp->sin_addr))) {
    376 					hostname = hp->h_name;
    377 					break;
    378 				}
    379 			}
    380 		}
    381 	} else
    382 		errorhost = hostname = inet_ntoa(fromp->sin_addr);
    383 
    384 #ifdef	KERBEROS
    385 	if (use_kerberos) {
    386 		kdata = (AUTH_DAT *) authbuf;
    387 		ticket = (KTEXT) tickbuf;
    388 		authopts = 0L;
    389 		strcpy(instance, "*");
    390 		version[VERSION_SIZE - 1] = '\0';
    391 #ifdef CRYPT
    392 		if (doencrypt) {
    393 			struct sockaddr_in local_addr;
    394 			rc = sizeof(local_addr);
    395 			if (getsockname(0, (struct sockaddr *)&local_addr,
    396 			    &rc) < 0) {
    397 				syslog(LOG_ERR, "getsockname: %m");
    398 				error("rlogind: getsockname: %m");
    399 				exit(1);
    400 			}
    401 			authopts = KOPT_DO_MUTUAL;
    402 			rc = krb_recvauth(authopts, 0, ticket,
    403 				"rcmd", instance, &fromaddr,
    404 				&local_addr, kdata, "", schedule,
    405 				version);
    406 			des_set_key(kdata->session, schedule);
    407 		} else
    408 #endif
    409 			rc = krb_recvauth(authopts, 0, ticket, "rcmd",
    410 				instance, &fromaddr,
    411 				(struct sockaddr_in *) 0,
    412 				kdata, "", (bit_64 *) 0, version);
    413 		if (rc != KSUCCESS) {
    414 			error("Kerberos authentication failure: %s\n",
    415 				  krb_err_txt[rc]);
    416 			exit(1);
    417 		}
    418 	} else
    419 #endif
    420 		getstr(remuser, sizeof(remuser), "remuser");
    421 
    422 	getstr(locuser, sizeof(locuser), "locuser");
    423 	getstr(cmdbuf, sizeof(cmdbuf), "command");
    424 	setpwent();
    425 	pwd = getpwnam(locuser);
    426 	if (pwd == NULL) {
    427 		if (errorstr == NULL)
    428 			errorstr = "Login incorrect.\n";
    429 		goto fail;
    430 	}
    431 	if (chdir(pwd->pw_dir) < 0) {
    432 		(void) chdir("/");
    433 #ifdef notdef
    434 		error("No remote directory.\n");
    435 		exit(1);
    436 #endif
    437 	}
    438 
    439 #ifdef	KERBEROS
    440 	if (use_kerberos) {
    441 		if (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0') {
    442 			if (kuserok(kdata, locuser) != 0) {
    443 				syslog(LOG_NOTICE|LOG_AUTH,
    444 				    "Kerberos shell denied to %s.%s@%s on %s as %s: cmd='%s'; %s",
    445 				    kdata->pname, kdata->pinst, kdata->prealm,
    446                         	    hostname, locuser, cmdbuf, errorstr);
    447 				error("Permission denied.\n");
    448 				exit(1);
    449 			}
    450 		}
    451 	} else
    452 #endif
    453 
    454 		if (errorstr ||
    455 		    pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' &&
    456 		    ruserok(hostname, pwd->pw_uid == 0, remuser, locuser) < 0) {
    457 fail:
    458 			if (errorstr == NULL)
    459 				errorstr = "Permission denied.\n";
    460 
    461 			/* log the (failed) rsh request, if paranoid */
    462 			if (paranoid || (pwd && pwd->pw_uid == 0))
    463 		    		syslog(LOG_INFO|LOG_AUTH,
    464 				    "rsh denied to %s@%s as %s: cmd='%s'; %s",
    465 				    remuser, hostname, locuser, cmdbuf,
    466 				    errorstr);
    467 
    468 			error(errorstr, errorhost);
    469 			exit(1);
    470 		}
    471 
    472 	if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) {
    473 		error("Logins currently disabled.\n");
    474 		exit(1);
    475 	}
    476 
    477 	(void) write(2, "\0", 1);
    478 	sent_null = 1;
    479 
    480 	if (port) {
    481 		if (pipe(pv) < 0) {
    482 			error("Can't make pipe.\n");
    483 			exit(1);
    484 		}
    485 #ifdef CRYPT
    486 #ifdef KERBEROS
    487 		if (doencrypt) {
    488 			if (pipe(pv1) < 0) {
    489 				error("Can't make 2nd pipe.\n");
    490 				exit(1);
    491 			}
    492 			if (pipe(pv2) < 0) {
    493 				error("Can't make 3rd pipe.\n");
    494 				exit(1);
    495 			}
    496 		}
    497 #endif
    498 #endif
    499 		pid = fork();
    500 		if (pid == -1)  {
    501 			error("Can't fork; try again.\n");
    502 			exit(1);
    503 		}
    504 		if (pid) {
    505 #ifdef CRYPT
    506 #ifdef KERBEROS
    507 			if (doencrypt) {
    508 				static char msg[] = SECURE_MESSAGE;
    509 				(void) close(pv1[1]);
    510 				(void) close(pv2[1]);
    511 				des_write(s, msg, sizeof(msg));
    512 
    513 			} else
    514 #endif
    515 #endif
    516 			{
    517 				(void) close(0); (void) close(1);
    518 			}
    519 			(void) close(2); (void) close(pv[1]);
    520 
    521 			FD_ZERO(&readfrom);
    522 			FD_SET(s, &readfrom);
    523 			FD_SET(pv[0], &readfrom);
    524 			if (pv[0] > s)
    525 				nfd = pv[0];
    526 			else
    527 				nfd = s;
    528 #ifdef CRYPT
    529 #ifdef KERBEROS
    530 			if (doencrypt) {
    531 				FD_ZERO(&writeto);
    532 				FD_SET(pv2[0], &writeto);
    533 				FD_SET(pv1[0], &readfrom);
    534 
    535 				nfd = MAX(nfd, pv2[0]);
    536 				nfd = MAX(nfd, pv1[0]);
    537 			} else
    538 #endif
    539 #endif
    540 				ioctl(pv[0], FIONBIO, (char *)&one);
    541 
    542 			/* should set s nbio! */
    543 			nfd++;
    544 			do {
    545 				ready = readfrom;
    546 #ifdef CRYPT
    547 #ifdef KERBEROS
    548 				if (doencrypt) {
    549 					wready = writeto;
    550 					if (select(nfd, &ready,
    551 					    &wready, (fd_set *) 0,
    552 					    (struct timeval *) 0) < 0)
    553 						break;
    554 				} else
    555 #endif
    556 #endif
    557 					if (select(nfd, &ready, (fd_set *)0,
    558 					  (fd_set *)0, (struct timeval *)0) < 0)
    559 						break;
    560 				if (FD_ISSET(s, &ready)) {
    561 					int	ret;
    562 #ifdef CRYPT
    563 #ifdef KERBEROS
    564 					if (doencrypt)
    565 						ret = des_read(s, &sig, 1);
    566 					else
    567 #endif
    568 #endif
    569 						ret = read(s, &sig, 1);
    570 					if (ret <= 0)
    571 						FD_CLR(s, &readfrom);
    572 					else
    573 						killpg(pid, sig);
    574 				}
    575 				if (FD_ISSET(pv[0], &ready)) {
    576 					errno = 0;
    577 					cc = read(pv[0], buf, sizeof(buf));
    578 					if (cc <= 0) {
    579 						shutdown(s, 1+1);
    580 						FD_CLR(pv[0], &readfrom);
    581 					} else {
    582 #ifdef CRYPT
    583 #ifdef KERBEROS
    584 						if (doencrypt)
    585 							(void)
    586 							  des_write(s, buf, cc);
    587 						else
    588 #endif
    589 #endif
    590 							(void)
    591 							  write(s, buf, cc);
    592 					}
    593 				}
    594 #ifdef CRYPT
    595 #ifdef KERBEROS
    596 				if (doencrypt && FD_ISSET(pv1[0], &ready)) {
    597 					errno = 0;
    598 					cc = read(pv1[0], buf, sizeof(buf));
    599 					if (cc <= 0) {
    600 						shutdown(pv1[0], 1+1);
    601 						FD_CLR(pv1[0], &readfrom);
    602 					} else
    603 						(void) des_write(1, buf, cc);
    604 				}
    605 
    606 				if (doencrypt && FD_ISSET(pv2[0], &wready)) {
    607 					errno = 0;
    608 					cc = des_read(0, buf, sizeof(buf));
    609 					if (cc <= 0) {
    610 						shutdown(pv2[0], 1+1);
    611 						FD_CLR(pv2[0], &writeto);
    612 					} else
    613 						(void) write(pv2[0], buf, cc);
    614 				}
    615 #endif
    616 #endif
    617 
    618 			} while (FD_ISSET(s, &readfrom) ||
    619 #ifdef CRYPT
    620 #ifdef KERBEROS
    621 			    (doencrypt && FD_ISSET(pv1[0], &readfrom)) ||
    622 #endif
    623 #endif
    624 			    FD_ISSET(pv[0], &readfrom));
    625 			exit(0);
    626 		}
    627 		setpgrp(0, getpid());
    628 		(void) close(s); (void) close(pv[0]);
    629 #ifdef CRYPT
    630 #ifdef KERBEROS
    631 		if (doencrypt) {
    632 			close(pv1[0]); close(pv2[0]);
    633 			dup2(pv1[1], 1);
    634 			dup2(pv2[1], 0);
    635 			close(pv1[1]);
    636 			close(pv2[1]);
    637 		}
    638 #endif
    639 #endif
    640 		dup2(pv[1], 2);
    641 		close(pv[1]);
    642 	}
    643 	if (*pwd->pw_shell == '\0')
    644 		pwd->pw_shell = _PATH_BSHELL;
    645 #if	BSD > 43
    646 	if (setlogin(pwd->pw_name) < 0)
    647 		syslog(LOG_ERR, "setlogin() failed: %m");
    648 #endif
    649 	(void) setgid((gid_t)pwd->pw_gid);
    650 	initgroups(pwd->pw_name, pwd->pw_gid);
    651 	(void) setuid((uid_t)pwd->pw_uid);
    652 	environ = envinit;
    653 	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
    654 	strcat(path, _PATH_DEFPATH);
    655 	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
    656 	strncat(username, pwd->pw_name, sizeof(username)-6);
    657 	cp = rindex(pwd->pw_shell, '/');
    658 	if (cp)
    659 		cp++;
    660 	else
    661 		cp = pwd->pw_shell;
    662 	endpwent();
    663 	if (paranoid || pwd->pw_uid == 0) {
    664 #ifdef	KERBEROS
    665 		if (use_kerberos)
    666 		    syslog(LOG_INFO|LOG_AUTH,
    667                         "Kerberos shell from %s.%s@%s on %s as %s: cmd='%s'",
    668                         kdata->pname, kdata->pinst, kdata->prealm,
    669                         hostname, locuser, cmdbuf);
    670 		else
    671 #endif
    672 		    syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%s'",
    673 			remuser, hostname, locuser, cmdbuf);
    674 	}
    675 
    676 
    677 	execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
    678 	perror(pwd->pw_shell);
    679 	exit(1);
    680 }
    681 
    682 /*
    683  * Report error to client.
    684  * Note: can't be used until second socket has connected
    685  * to client, or older clients will hang waiting
    686  * for that connection first.
    687  */
    688 /*VARARGS1*/
    689 error(fmt, a1, a2, a3)
    690 	char *fmt;
    691 	int a1, a2, a3;
    692 {
    693 	char buf[BUFSIZ], *bp = buf;
    694 
    695 	if (sent_null == 0)
    696 		*bp++ = 1;
    697 	(void) sprintf(bp, fmt, a1, a2, a3);
    698 	(void) write(2, buf, strlen(buf));
    699 }
    700 
    701 getstr(buf, cnt, err)
    702 	char *buf;
    703 	int cnt;
    704 	char *err;
    705 {
    706 	char c;
    707 
    708 	do {
    709 		if (read(0, &c, 1) != 1)
    710 			exit(1);
    711 		*buf++ = c;
    712 		if (--cnt == 0) {
    713 			error("%s too long\n", err);
    714 			exit(1);
    715 		}
    716 	} while (c != 0);
    717 }
    718 
    719 /*
    720  * Check whether host h is in our local domain,
    721  * defined as sharing the last two components of the domain part,
    722  * or the entire domain part if the local domain has only one component.
    723  * If either name is unqualified (contains no '.'),
    724  * assume that the host is local, as it will be
    725  * interpreted as such.
    726  */
    727 local_domain(h)
    728 	char *h;
    729 {
    730 	char localhost[MAXHOSTNAMELEN];
    731 	char *p1, *p2, *topdomain();
    732 
    733 	localhost[0] = 0;
    734 	(void) gethostname(localhost, sizeof(localhost));
    735 	p1 = topdomain(localhost);
    736 	p2 = topdomain(h);
    737 	if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
    738 		return(1);
    739 	return(0);
    740 }
    741 
    742 char *
    743 topdomain(h)
    744 	char *h;
    745 {
    746 	register char *p;
    747 	char *maybe = NULL;
    748 	int dots = 0;
    749 
    750 	for (p = h + strlen(h); p >= h; p--) {
    751 		if (*p == '.') {
    752 			if (++dots == 2)
    753 				return (p);
    754 			maybe = p;
    755 		}
    756 	}
    757 	return (maybe);
    758 }
    759 
    760 usage()
    761 {
    762 	syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);
    763 }
    764