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