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