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