Home | History | Annotate | Line # | Download | only in rexecd
rexecd.c revision 1.19
      1  1.19  ginsbach /*	$NetBSD: rexecd.c,v 1.19 2005/01/08 03:14:02 ginsbach 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.19  ginsbach __RCSID("$NetBSD: rexecd.c,v 1.19 2005/01/08 03:14:02 ginsbach 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.19  ginsbach void error(const char *, ...)
     65   1.7        is      __attribute__((__format__(__printf__, 1, 2)));
     66  1.19  ginsbach int main(int, char *[]);
     67  1.19  ginsbach void doit(int, struct sockaddr *);
     68  1.19  ginsbach void getstr(char *, int, char *);
     69   1.1       cgd 
     70   1.5       mrg char	username[32 + 1] = "USER=";
     71  1.18    kleink char	logname[32 + 3 + 1] = "LOGNAME=";
     72   1.5       mrg char	homedir[PATH_MAX + 1] = "HOME=";
     73   1.5       mrg char	shell[PATH_MAX + 1] = "SHELL=";
     74   1.5       mrg char	path[sizeof(_PATH_DEFPATH) + sizeof("PATH=")] = "PATH=";
     75  1.18    kleink char	*envinit[] = { homedir, shell, path, username, logname, 0 };
     76   1.5       mrg char	**environ;
     77  1.14   thorpej int	dolog;
     78   1.5       mrg 
     79   1.1       cgd /*
     80   1.1       cgd  * remote execute server:
     81   1.1       cgd  *	username\0
     82   1.1       cgd  *	password\0
     83   1.1       cgd  *	command\0
     84   1.1       cgd  *	data
     85   1.1       cgd  */
     86   1.4       mrg int
     87  1.19  ginsbach main(int argc, char *argv[])
     88   1.1       cgd {
     89  1.11    itojun 	struct sockaddr_storage from;
     90   1.5       mrg 	int fromlen, ch;
     91   1.5       mrg 
     92   1.5       mrg 	while ((ch = getopt(argc, argv, "l")) != -1)
     93   1.5       mrg 		switch (ch) {
     94   1.5       mrg 		case 'l':
     95  1.14   thorpej 			dolog = 1;
     96   1.5       mrg 			openlog("rexecd", LOG_PID, LOG_DAEMON);
     97   1.5       mrg 			break;
     98   1.5       mrg 		default:
     99   1.5       mrg 			exit(1);
    100   1.5       mrg 		}
    101   1.1       cgd 
    102   1.1       cgd 	fromlen = sizeof (from);
    103   1.5       mrg 	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0)
    104   1.5       mrg 		err(1, "getpeername");
    105   1.5       mrg 
    106  1.11    itojun 	doit(0, (struct sockaddr *)&from);
    107   1.4       mrg 	exit(0);
    108   1.1       cgd }
    109   1.1       cgd 
    110   1.4       mrg void
    111  1.19  ginsbach doit(int f, struct sockaddr *fromp)
    112   1.1       cgd {
    113   1.5       mrg 	struct pollfd fds[2];
    114   1.6   mycroft 	char cmdbuf[NCARGS+1], *namep;
    115   1.6   mycroft 	const char *cp;
    116   1.1       cgd 	char user[16], pass[16];
    117   1.5       mrg 	char buf[BUFSIZ], sig;
    118   1.1       cgd 	struct passwd *pwd;
    119   1.4       mrg 	int s = -1; /* XXX gcc */
    120   1.5       mrg 	int pv[2], pid, cc;
    121   1.1       cgd 	int one = 1;
    122   1.5       mrg 	in_port_t port;
    123   1.1       cgd 
    124   1.5       mrg 	(void)signal(SIGINT, SIG_DFL);
    125   1.5       mrg 	(void)signal(SIGQUIT, SIG_DFL);
    126   1.5       mrg 	(void)signal(SIGTERM, SIG_DFL);
    127  1.19  ginsbach 	dup2(f, STDIN_FILENO);
    128  1.19  ginsbach 	dup2(f, STDOUT_FILENO);
    129  1.19  ginsbach 	dup2(f, STDERR_FILENO);
    130   1.5       mrg 	(void)alarm(60);
    131   1.1       cgd 	port = 0;
    132   1.1       cgd 	for (;;) {
    133   1.1       cgd 		char c;
    134  1.19  ginsbach 		if (read(f, &c, STDIN_FILENO) != 1) {
    135  1.14   thorpej 			if (dolog)
    136   1.5       mrg 				syslog(LOG_ERR,
    137   1.5       mrg 				    "initial read failed");
    138   1.1       cgd 			exit(1);
    139   1.5       mrg 		}
    140   1.1       cgd 		if (c == 0)
    141   1.1       cgd 			break;
    142   1.1       cgd 		port = port * 10 + c - '0';
    143   1.1       cgd 	}
    144   1.5       mrg 	(void)alarm(0);
    145   1.1       cgd 	if (port != 0) {
    146  1.11    itojun 		s = socket(fromp->sa_family, SOCK_STREAM, 0);
    147   1.5       mrg 		if (s < 0) {
    148  1.14   thorpej 			if (dolog)
    149   1.5       mrg 				syslog(LOG_ERR, "socket: %m");
    150   1.1       cgd 			exit(1);
    151   1.5       mrg 		}
    152  1.11    itojun 		(void)alarm(60);
    153  1.11    itojun 		switch (fromp->sa_family) {
    154  1.11    itojun 		case AF_INET:
    155  1.11    itojun 			((struct sockaddr_in *)fromp)->sin_port = htons(port);
    156  1.11    itojun 			break;
    157  1.11    itojun 		case AF_INET6:
    158  1.11    itojun 			((struct sockaddr_in6 *)fromp)->sin6_port = htons(port);
    159  1.11    itojun 			break;
    160  1.11    itojun 		default:
    161  1.11    itojun 			syslog(LOG_ERR, "unsupported address family");
    162   1.1       cgd 			exit(1);
    163   1.5       mrg 		}
    164  1.11    itojun 		if (connect(s, (struct sockaddr *)fromp, fromp->sa_len) < 0) {
    165  1.14   thorpej 			if (dolog)
    166   1.5       mrg 				syslog(LOG_ERR, "connect: %m");
    167   1.1       cgd 			exit(1);
    168   1.5       mrg 		}
    169   1.5       mrg 		(void)alarm(0);
    170   1.1       cgd 	}
    171   1.1       cgd 	getstr(user, sizeof(user), "username");
    172   1.1       cgd 	getstr(pass, sizeof(pass), "password");
    173   1.1       cgd 	getstr(cmdbuf, sizeof(cmdbuf), "command");
    174   1.1       cgd 	setpwent();
    175   1.1       cgd 	pwd = getpwnam(user);
    176   1.1       cgd 	if (pwd == NULL) {
    177   1.1       cgd 		error("Login incorrect.\n");
    178  1.14   thorpej 		if (dolog)
    179   1.5       mrg 			syslog(LOG_ERR, "no such user %s", user);
    180   1.1       cgd 		exit(1);
    181   1.1       cgd 	}
    182   1.1       cgd 	endpwent();
    183   1.1       cgd 	if (*pwd->pw_passwd != '\0') {
    184   1.1       cgd 		namep = crypt(pass, pwd->pw_passwd);
    185   1.1       cgd 		if (strcmp(namep, pwd->pw_passwd)) {
    186   1.5       mrg 			error("Password incorrect.\n");	/* XXX: wrong! */
    187  1.14   thorpej 			if (dolog)
    188   1.5       mrg 				syslog(LOG_ERR, "incorrect password for %s",
    189   1.5       mrg 				    user);
    190   1.1       cgd 			exit(1);
    191   1.1       cgd 		}
    192   1.5       mrg 	} else
    193   1.5       mrg 		(void)crypt("dummy password", "PA");	/* must always crypt */
    194   1.1       cgd 	if (chdir(pwd->pw_dir) < 0) {
    195   1.1       cgd 		error("No remote directory.\n");
    196  1.14   thorpej 		if (dolog)
    197   1.5       mrg 			syslog(LOG_ERR, "%s does not exist for %s", pwd->pw_dir,
    198   1.5       mrg 			    user);
    199   1.1       cgd 		exit(1);
    200   1.1       cgd 	}
    201  1.19  ginsbach 	(void)write(STDERR_FILENO, "\0", 1);
    202   1.1       cgd 	if (port) {
    203   1.5       mrg 		if (pipe(pv) < 0 || (pid = fork()) == -1) {
    204   1.1       cgd 			error("Try again.\n");
    205  1.14   thorpej 			if (dolog)
    206   1.5       mrg 				syslog(LOG_ERR,"pipe or fork failed for %s: %m",
    207   1.5       mrg 				    user);
    208   1.1       cgd 			exit(1);
    209   1.1       cgd 		}
    210   1.1       cgd 		if (pid) {
    211  1.19  ginsbach 			(void)close(STDIN_FILENO);
    212  1.19  ginsbach 			(void)close(STDOUT_FILENO);
    213  1.19  ginsbach 			(void)close(STDERR_FILENO);
    214   1.5       mrg 			(void)close(f);
    215   1.5       mrg 			(void)close(pv[1]);
    216   1.5       mrg 			fds[0].fd = s;
    217   1.5       mrg 			fds[1].fd = pv[0];
    218   1.5       mrg 			fds[0].events = fds[1].events = POLLIN;
    219   1.5       mrg 			if (ioctl(pv[1], FIONBIO, (char *)&one) < 0)
    220   1.8       wiz 				_exit(1);
    221   1.1       cgd 			/* should set s nbio! */
    222   1.1       cgd 			do {
    223   1.5       mrg 				if (poll(fds, 2, 0) < 0) {
    224   1.5       mrg 					close(s);
    225   1.5       mrg 					close(pv[0]);
    226   1.8       wiz 					_exit(1);
    227   1.5       mrg 				}
    228   1.5       mrg 				if (fds[0].revents & POLLIN) {
    229   1.1       cgd 					if (read(s, &sig, 1) <= 0)
    230   1.5       mrg 						fds[0].events = 0;
    231   1.1       cgd 					else
    232   1.1       cgd 						killpg(pid, sig);
    233   1.1       cgd 				}
    234   1.5       mrg 				if (fds[1].revents & POLLIN) {
    235   1.1       cgd 					cc = read(pv[0], buf, sizeof (buf));
    236   1.1       cgd 					if (cc <= 0) {
    237   1.1       cgd 						shutdown(s, 1+1);
    238   1.5       mrg 						fds[1].events = 0;
    239   1.1       cgd 					} else
    240   1.5       mrg 						(void)write(s, buf, cc);
    241   1.1       cgd 				}
    242   1.5       mrg 			} while ((fds[0].events | fds[1].events) & POLLIN);
    243   1.5       mrg 			_exit(0);
    244   1.5       mrg 		}
    245   1.5       mrg 		(void)close(s);
    246   1.5       mrg 		(void)close(pv[0]);
    247   1.5       mrg 		if (dup2(pv[1], 2) < 0) {
    248   1.5       mrg 			error("Try again.\n");
    249  1.14   thorpej 			if (dolog)
    250   1.5       mrg 				syslog(LOG_ERR, "dup2 failed for %s", user);
    251   1.5       mrg 			exit(1);
    252   1.1       cgd 		}
    253   1.1       cgd 	}
    254   1.1       cgd 	if (*pwd->pw_shell == '\0')
    255   1.1       cgd 		pwd->pw_shell = _PATH_BSHELL;
    256   1.1       cgd 	if (f > 2)
    257   1.5       mrg 		(void)close(f);
    258  1.15       dsl 	if (setsid() < 0 ||
    259  1.15       dsl 	    setlogin(pwd->pw_name) < 0 ||
    260   1.5       mrg 	    initgroups(pwd->pw_name, pwd->pw_gid) < 0 ||
    261  1.19  ginsbach 	    setgid((gid_t)pwd->pw_gid) < 0 ||
    262   1.5       mrg 	    setuid((uid_t)pwd->pw_uid) < 0) {
    263   1.5       mrg 		error("Try again.\n");
    264  1.14   thorpej 		if (dolog)
    265   1.5       mrg 			syslog(LOG_ERR, "could not set permissions for %s: %m",
    266   1.5       mrg 			    user);
    267   1.5       mrg 		exit(1);
    268   1.5       mrg 	}
    269  1.16    itojun 	(void)strlcat(path, _PATH_DEFPATH, sizeof(path));
    270   1.1       cgd 	environ = envinit;
    271  1.16    itojun 	strlcat(homedir, pwd->pw_dir, sizeof(homedir));
    272  1.16    itojun 	strlcat(shell, pwd->pw_shell, sizeof(shell));
    273  1.16    itojun 	strlcat(username, pwd->pw_name, sizeof(username));
    274  1.18    kleink 	strlcat(logname, pwd->pw_name, sizeof(logname));
    275   1.4       mrg 	cp = strrchr(pwd->pw_shell, '/');
    276   1.1       cgd 	if (cp)
    277   1.1       cgd 		cp++;
    278   1.1       cgd 	else
    279   1.1       cgd 		cp = pwd->pw_shell;
    280  1.14   thorpej 	if (dolog)
    281   1.5       mrg 		syslog(LOG_INFO, "running command for %s: %s", user, cmdbuf);
    282   1.1       cgd 	execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
    283   1.1       cgd 	perror(pwd->pw_shell);
    284  1.14   thorpej 	if (dolog)
    285   1.5       mrg 		syslog(LOG_ERR, "execl failed for %s: %m", user);
    286   1.1       cgd 	exit(1);
    287   1.1       cgd }
    288   1.1       cgd 
    289   1.4       mrg void
    290   1.4       mrg error(const char *fmt, ...)
    291   1.1       cgd {
    292   1.1       cgd 	char buf[BUFSIZ];
    293   1.4       mrg 	va_list ap;
    294   1.4       mrg 
    295   1.4       mrg 	va_start(ap, fmt);
    296   1.1       cgd 	buf[0] = 1;
    297   1.5       mrg 	(void)vsnprintf(buf+1, sizeof(buf) - 1, fmt, ap);
    298  1.19  ginsbach 	(void)write(STDERR_FILENO, buf, strlen(buf));
    299   1.9       wiz 	va_end(ap);
    300   1.1       cgd }
    301   1.1       cgd 
    302   1.4       mrg void
    303  1.19  ginsbach getstr(char *buf, int cnt, char *err)
    304   1.1       cgd {
    305   1.1       cgd 	char c;
    306   1.1       cgd 
    307   1.1       cgd 	do {
    308  1.19  ginsbach 		if (read(STDIN_FILENO, &c, 1) != 1)
    309   1.1       cgd 			exit(1);
    310   1.1       cgd 		*buf++ = c;
    311   1.1       cgd 		if (--cnt == 0) {
    312   1.1       cgd 			error("%s too long\n", err);
    313   1.1       cgd 			exit(1);
    314   1.1       cgd 		}
    315   1.1       cgd 	} while (c != 0);
    316   1.1       cgd }
    317