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