Home | History | Annotate | Line # | Download | only in rexecd
rexecd.c revision 1.17
      1  1.17      agc /*	$NetBSD: rexecd.c,v 1.17 2003/08/07 09:46:46 agc Exp $	*/
      2   1.4      mrg 
      3   1.1      cgd /*
      4   1.4      mrg  * Copyright (c) 1983, 1993
      5   1.4      mrg  *	The Regents of the University of California.  All rights reserved.
      6   1.1      cgd  *
      7   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1      cgd  * modification, are permitted provided that the following conditions
      9   1.1      cgd  * are met:
     10   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15  1.17      agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1      cgd  *    may be used to endorse or promote products derived from this software
     17   1.1      cgd  *    without specific prior written permission.
     18   1.1      cgd  *
     19   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1      cgd  * SUCH DAMAGE.
     30   1.1      cgd  */
     31   1.1      cgd 
     32   1.4      mrg #include <sys/cdefs.h>
     33   1.1      cgd #ifndef lint
     34   1.4      mrg __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
     35   1.4      mrg 	The Regents of the University of California.  All rights reserved.\n");
     36   1.4      mrg #if 0
     37   1.4      mrg static char sccsid[] = "from: @(#)rexecd.c	8.1 (Berkeley) 6/4/93";
     38   1.4      mrg #else
     39  1.17      agc __RCSID("$NetBSD: rexecd.c,v 1.17 2003/08/07 09:46:46 agc Exp $");
     40   1.4      mrg #endif
     41   1.1      cgd #endif /* not lint */
     42   1.1      cgd 
     43   1.1      cgd #include <sys/param.h>
     44   1.1      cgd #include <sys/ioctl.h>
     45   1.1      cgd #include <sys/socket.h>
     46   1.5      mrg #include <sys/syslog.h>
     47   1.1      cgd #include <sys/time.h>
     48   1.4      mrg 
     49   1.1      cgd #include <netinet/in.h>
     50   1.4      mrg 
     51   1.5      mrg #include <err.h>
     52   1.4      mrg #include <errno.h>
     53   1.1      cgd #include <netdb.h>
     54   1.4      mrg #include <paths.h>
     55   1.1      cgd #include <pwd.h>
     56   1.4      mrg #include <signal.h>
     57  1.10      wiz #include <stdarg.h>
     58   1.1      cgd #include <stdio.h>
     59   1.1      cgd #include <stdlib.h>
     60   1.1      cgd #include <string.h>
     61   1.4      mrg #include <unistd.h>
     62  1.12   itojun #include <poll.h>
     63   1.1      cgd 
     64   1.7       is void error __P((const char *, ...))
     65   1.7       is      __attribute__((__format__(__printf__, 1, 2)));
     66   1.4      mrg int main __P((int, char **));
     67  1.11   itojun void doit __P((int, struct sockaddr *));
     68   1.4      mrg void getstr __P((char *, int, char *));
     69   1.1      cgd 
     70   1.5      mrg char	username[32 + 1] = "USER=";
     71   1.5      mrg char	homedir[PATH_MAX + 1] = "HOME=";
     72   1.5      mrg char	shell[PATH_MAX + 1] = "SHELL=";
     73   1.5      mrg char	path[sizeof(_PATH_DEFPATH) + sizeof("PATH=")] = "PATH=";
     74   1.5      mrg char	*envinit[] = { homedir, shell, path, username, 0 };
     75   1.5      mrg char	**environ;
     76  1.14  thorpej int	dolog;
     77   1.5      mrg 
     78   1.1      cgd /*
     79   1.1      cgd  * remote execute server:
     80   1.1      cgd  *	username\0
     81   1.1      cgd  *	password\0
     82   1.1      cgd  *	command\0
     83   1.1      cgd  *	data
     84   1.1      cgd  */
     85   1.4      mrg int
     86   1.1      cgd main(argc, argv)
     87   1.1      cgd 	int argc;
     88   1.1      cgd 	char **argv;
     89   1.1      cgd {
     90  1.11   itojun 	struct sockaddr_storage from;
     91   1.5      mrg 	int fromlen, ch;
     92   1.5      mrg 
     93   1.5      mrg 	while ((ch = getopt(argc, argv, "l")) != -1)
     94   1.5      mrg 		switch (ch) {
     95   1.5      mrg 		case 'l':
     96  1.14  thorpej 			dolog = 1;
     97   1.5      mrg 			openlog("rexecd", LOG_PID, LOG_DAEMON);
     98   1.5      mrg 			break;
     99   1.5      mrg 		default:
    100   1.5      mrg 			exit(1);
    101   1.5      mrg 		}
    102   1.1      cgd 
    103   1.1      cgd 	fromlen = sizeof (from);
    104   1.5      mrg 	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0)
    105   1.5      mrg 		err(1, "getpeername");
    106   1.5      mrg 
    107  1.11   itojun 	doit(0, (struct sockaddr *)&from);
    108   1.4      mrg 	exit(0);
    109   1.1      cgd }
    110   1.1      cgd 
    111   1.4      mrg void
    112   1.1      cgd doit(f, fromp)
    113   1.1      cgd 	int f;
    114  1.11   itojun 	struct sockaddr *fromp;
    115   1.1      cgd {
    116   1.5      mrg 	struct pollfd fds[2];
    117   1.6  mycroft 	char cmdbuf[NCARGS+1], *namep;
    118   1.6  mycroft 	const char *cp;
    119   1.1      cgd 	char user[16], pass[16];
    120   1.5      mrg 	char buf[BUFSIZ], sig;
    121   1.1      cgd 	struct passwd *pwd;
    122   1.4      mrg 	int s = -1; /* XXX gcc */
    123   1.5      mrg 	int pv[2], pid, cc;
    124   1.1      cgd 	int one = 1;
    125   1.5      mrg 	in_port_t port;
    126   1.1      cgd 
    127   1.5      mrg 	(void)signal(SIGINT, SIG_DFL);
    128   1.5      mrg 	(void)signal(SIGQUIT, SIG_DFL);
    129   1.5      mrg 	(void)signal(SIGTERM, SIG_DFL);
    130   1.1      cgd 	dup2(f, 0);
    131   1.1      cgd 	dup2(f, 1);
    132   1.1      cgd 	dup2(f, 2);
    133   1.5      mrg 	(void)alarm(60);
    134   1.1      cgd 	port = 0;
    135   1.1      cgd 	for (;;) {
    136   1.1      cgd 		char c;
    137   1.5      mrg 		if (read(f, &c, 1) != 1) {
    138  1.14  thorpej 			if (dolog)
    139   1.5      mrg 				syslog(LOG_ERR,
    140   1.5      mrg 				    "initial read failed");
    141   1.1      cgd 			exit(1);
    142   1.5      mrg 		}
    143   1.1      cgd 		if (c == 0)
    144   1.1      cgd 			break;
    145   1.1      cgd 		port = port * 10 + c - '0';
    146   1.1      cgd 	}
    147   1.5      mrg 	(void)alarm(0);
    148   1.1      cgd 	if (port != 0) {
    149  1.11   itojun 		s = socket(fromp->sa_family, SOCK_STREAM, 0);
    150   1.5      mrg 		if (s < 0) {
    151  1.14  thorpej 			if (dolog)
    152   1.5      mrg 				syslog(LOG_ERR, "socket: %m");
    153   1.1      cgd 			exit(1);
    154   1.5      mrg 		}
    155  1.11   itojun 		(void)alarm(60);
    156  1.11   itojun 		switch (fromp->sa_family) {
    157  1.11   itojun 		case AF_INET:
    158  1.11   itojun 			((struct sockaddr_in *)fromp)->sin_port = htons(port);
    159  1.11   itojun 			break;
    160  1.11   itojun 		case AF_INET6:
    161  1.11   itojun 			((struct sockaddr_in6 *)fromp)->sin6_port = htons(port);
    162  1.11   itojun 			break;
    163  1.11   itojun 		default:
    164  1.11   itojun 			syslog(LOG_ERR, "unsupported address family");
    165   1.1      cgd 			exit(1);
    166   1.5      mrg 		}
    167  1.11   itojun 		if (connect(s, (struct sockaddr *)fromp, fromp->sa_len) < 0) {
    168  1.14  thorpej 			if (dolog)
    169   1.5      mrg 				syslog(LOG_ERR, "connect: %m");
    170   1.1      cgd 			exit(1);
    171   1.5      mrg 		}
    172   1.5      mrg 		(void)alarm(0);
    173   1.1      cgd 	}
    174   1.1      cgd 	getstr(user, sizeof(user), "username");
    175   1.1      cgd 	getstr(pass, sizeof(pass), "password");
    176   1.1      cgd 	getstr(cmdbuf, sizeof(cmdbuf), "command");
    177   1.1      cgd 	setpwent();
    178   1.1      cgd 	pwd = getpwnam(user);
    179   1.1      cgd 	if (pwd == NULL) {
    180   1.1      cgd 		error("Login incorrect.\n");
    181  1.14  thorpej 		if (dolog)
    182   1.5      mrg 			syslog(LOG_ERR, "no such user %s", user);
    183   1.1      cgd 		exit(1);
    184   1.1      cgd 	}
    185   1.1      cgd 	endpwent();
    186   1.1      cgd 	if (*pwd->pw_passwd != '\0') {
    187   1.1      cgd 		namep = crypt(pass, pwd->pw_passwd);
    188   1.1      cgd 		if (strcmp(namep, pwd->pw_passwd)) {
    189   1.5      mrg 			error("Password incorrect.\n");	/* XXX: wrong! */
    190  1.14  thorpej 			if (dolog)
    191   1.5      mrg 				syslog(LOG_ERR, "incorrect password for %s",
    192   1.5      mrg 				    user);
    193   1.1      cgd 			exit(1);
    194   1.1      cgd 		}
    195   1.5      mrg 	} else
    196   1.5      mrg 		(void)crypt("dummy password", "PA");	/* must always crypt */
    197   1.1      cgd 	if (chdir(pwd->pw_dir) < 0) {
    198   1.1      cgd 		error("No remote directory.\n");
    199  1.14  thorpej 		if (dolog)
    200   1.5      mrg 			syslog(LOG_ERR, "%s does not exist for %s", pwd->pw_dir,
    201   1.5      mrg 			    user);
    202   1.1      cgd 		exit(1);
    203   1.1      cgd 	}
    204   1.5      mrg 	(void)write(2, "\0", 1);
    205   1.1      cgd 	if (port) {
    206   1.5      mrg 		if (pipe(pv) < 0 || (pid = fork()) == -1) {
    207   1.1      cgd 			error("Try again.\n");
    208  1.14  thorpej 			if (dolog)
    209   1.5      mrg 				syslog(LOG_ERR,"pipe or fork failed for %s: %m",
    210   1.5      mrg 				    user);
    211   1.1      cgd 			exit(1);
    212   1.1      cgd 		}
    213   1.1      cgd 		if (pid) {
    214   1.5      mrg 			(void)close(0);
    215   1.5      mrg 			(void)close(1);
    216   1.5      mrg 			(void)close(2);
    217   1.5      mrg 			(void)close(f);
    218   1.5      mrg 			(void)close(pv[1]);
    219   1.5      mrg 			fds[0].fd = s;
    220   1.5      mrg 			fds[1].fd = pv[0];
    221   1.5      mrg 			fds[0].events = fds[1].events = POLLIN;
    222   1.5      mrg 			if (ioctl(pv[1], FIONBIO, (char *)&one) < 0)
    223   1.8      wiz 				_exit(1);
    224   1.1      cgd 			/* should set s nbio! */
    225   1.1      cgd 			do {
    226   1.5      mrg 				if (poll(fds, 2, 0) < 0) {
    227   1.5      mrg 					close(s);
    228   1.5      mrg 					close(pv[0]);
    229   1.8      wiz 					_exit(1);
    230   1.5      mrg 				}
    231   1.5      mrg 				if (fds[0].revents & POLLIN) {
    232   1.1      cgd 					if (read(s, &sig, 1) <= 0)
    233   1.5      mrg 						fds[0].events = 0;
    234   1.1      cgd 					else
    235   1.1      cgd 						killpg(pid, sig);
    236   1.1      cgd 				}
    237   1.5      mrg 				if (fds[1].revents & POLLIN) {
    238   1.1      cgd 					cc = read(pv[0], buf, sizeof (buf));
    239   1.1      cgd 					if (cc <= 0) {
    240   1.1      cgd 						shutdown(s, 1+1);
    241   1.5      mrg 						fds[1].events = 0;
    242   1.1      cgd 					} else
    243   1.5      mrg 						(void)write(s, buf, cc);
    244   1.1      cgd 				}
    245   1.5      mrg 			} while ((fds[0].events | fds[1].events) & POLLIN);
    246   1.5      mrg 			_exit(0);
    247   1.5      mrg 		}
    248   1.5      mrg 		(void)close(s);
    249   1.5      mrg 		(void)close(pv[0]);
    250   1.5      mrg 		if (dup2(pv[1], 2) < 0) {
    251   1.5      mrg 			error("Try again.\n");
    252  1.14  thorpej 			if (dolog)
    253   1.5      mrg 				syslog(LOG_ERR, "dup2 failed for %s", user);
    254   1.5      mrg 			exit(1);
    255   1.1      cgd 		}
    256   1.1      cgd 	}
    257   1.1      cgd 	if (*pwd->pw_shell == '\0')
    258   1.1      cgd 		pwd->pw_shell = _PATH_BSHELL;
    259   1.1      cgd 	if (f > 2)
    260   1.5      mrg 		(void)close(f);
    261  1.15      dsl 	if (setsid() < 0 ||
    262  1.15      dsl 	    setlogin(pwd->pw_name) < 0 ||
    263   1.5      mrg 	    initgroups(pwd->pw_name, pwd->pw_gid) < 0 ||
    264   1.5      mrg 	    setgid((gid_t)pwd->pw_gid) < 0 ||
    265   1.5      mrg 	    setuid((uid_t)pwd->pw_uid) < 0) {
    266   1.5      mrg 		error("Try again.\n");
    267  1.14  thorpej 		if (dolog)
    268   1.5      mrg 			syslog(LOG_ERR, "could not set permissions for %s: %m",
    269   1.5      mrg 			    user);
    270   1.5      mrg 		exit(1);
    271   1.5      mrg 	}
    272  1.16   itojun 	(void)strlcat(path, _PATH_DEFPATH, sizeof(path));
    273   1.1      cgd 	environ = envinit;
    274  1.16   itojun 	strlcat(homedir, pwd->pw_dir, sizeof(homedir));
    275  1.16   itojun 	strlcat(shell, pwd->pw_shell, sizeof(shell));
    276  1.16   itojun 	strlcat(username, pwd->pw_name, sizeof(username));
    277   1.4      mrg 	cp = strrchr(pwd->pw_shell, '/');
    278   1.1      cgd 	if (cp)
    279   1.1      cgd 		cp++;
    280   1.1      cgd 	else
    281   1.1      cgd 		cp = pwd->pw_shell;
    282  1.14  thorpej 	if (dolog)
    283   1.5      mrg 		syslog(LOG_INFO, "running command for %s: %s", user, cmdbuf);
    284   1.1      cgd 	execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
    285   1.1      cgd 	perror(pwd->pw_shell);
    286  1.14  thorpej 	if (dolog)
    287   1.5      mrg 		syslog(LOG_ERR, "execl failed for %s: %m", user);
    288   1.1      cgd 	exit(1);
    289   1.1      cgd }
    290   1.1      cgd 
    291   1.4      mrg void
    292   1.4      mrg error(const char *fmt, ...)
    293   1.1      cgd {
    294   1.1      cgd 	char buf[BUFSIZ];
    295   1.4      mrg 	va_list ap;
    296   1.4      mrg 
    297   1.4      mrg 	va_start(ap, fmt);
    298   1.1      cgd 	buf[0] = 1;
    299   1.5      mrg 	(void)vsnprintf(buf+1, sizeof(buf) - 1, fmt, ap);
    300   1.4      mrg 	(void)write(2, buf, strlen(buf));
    301   1.9      wiz 	va_end(ap);
    302   1.1      cgd }
    303   1.1      cgd 
    304   1.4      mrg void
    305   1.1      cgd getstr(buf, cnt, err)
    306   1.1      cgd 	char *buf;
    307   1.1      cgd 	int cnt;
    308   1.1      cgd 	char *err;
    309   1.1      cgd {
    310   1.1      cgd 	char c;
    311   1.1      cgd 
    312   1.1      cgd 	do {
    313   1.1      cgd 		if (read(0, &c, 1) != 1)
    314   1.1      cgd 			exit(1);
    315   1.1      cgd 		*buf++ = c;
    316   1.1      cgd 		if (--cnt == 0) {
    317   1.1      cgd 			error("%s too long\n", err);
    318   1.1      cgd 			exit(1);
    319   1.1      cgd 		}
    320   1.1      cgd 	} while (c != 0);
    321   1.1      cgd }
    322