Home | History | Annotate | Line # | Download | only in rexecd
rexecd.c revision 1.4
      1  1.4  mrg /*	$NetBSD: rexecd.c,v 1.4 1997/10/07 10:11:31 mrg 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.1  cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1  cgd  *    must display the following acknowledgement:
     17  1.1  cgd  *	This product includes software developed by the University of
     18  1.1  cgd  *	California, Berkeley and its contributors.
     19  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1  cgd  *    may be used to endorse or promote products derived from this software
     21  1.1  cgd  *    without specific prior written permission.
     22  1.1  cgd  *
     23  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1  cgd  * SUCH DAMAGE.
     34  1.1  cgd  */
     35  1.1  cgd 
     36  1.4  mrg #include <sys/cdefs.h>
     37  1.1  cgd #ifndef lint
     38  1.4  mrg __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
     39  1.4  mrg 	The Regents of the University of California.  All rights reserved.\n");
     40  1.4  mrg #if 0
     41  1.4  mrg static char sccsid[] = "from: @(#)rexecd.c	8.1 (Berkeley) 6/4/93";
     42  1.4  mrg #else
     43  1.4  mrg __RCSID("$NetBSD: rexecd.c,v 1.4 1997/10/07 10:11:31 mrg Exp $");
     44  1.4  mrg #endif
     45  1.1  cgd #endif /* not lint */
     46  1.1  cgd 
     47  1.1  cgd #include <sys/param.h>
     48  1.1  cgd #include <sys/ioctl.h>
     49  1.1  cgd #include <sys/socket.h>
     50  1.1  cgd #include <sys/time.h>
     51  1.4  mrg 
     52  1.1  cgd #include <netinet/in.h>
     53  1.4  mrg 
     54  1.4  mrg #include <errno.h>
     55  1.1  cgd #include <netdb.h>
     56  1.4  mrg #include <paths.h>
     57  1.1  cgd #include <pwd.h>
     58  1.4  mrg #include <signal.h>
     59  1.1  cgd #include <stdio.h>
     60  1.1  cgd #include <stdlib.h>
     61  1.1  cgd #include <string.h>
     62  1.4  mrg #include <unistd.h>
     63  1.1  cgd 
     64  1.4  mrg void error __P((const char *, ...));
     65  1.4  mrg int main __P((int, char **));
     66  1.4  mrg void doit __P((int, struct sockaddr_in *));
     67  1.4  mrg void getstr __P((char *, int, char *));
     68  1.1  cgd 
     69  1.1  cgd /*
     70  1.1  cgd  * remote execute server:
     71  1.1  cgd  *	username\0
     72  1.1  cgd  *	password\0
     73  1.1  cgd  *	command\0
     74  1.1  cgd  *	data
     75  1.1  cgd  */
     76  1.4  mrg int
     77  1.1  cgd main(argc, argv)
     78  1.1  cgd 	int argc;
     79  1.1  cgd 	char **argv;
     80  1.1  cgd {
     81  1.1  cgd 	struct sockaddr_in from;
     82  1.1  cgd 	int fromlen;
     83  1.1  cgd 
     84  1.1  cgd 	fromlen = sizeof (from);
     85  1.1  cgd 	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
     86  1.1  cgd 		(void)fprintf(stderr,
     87  1.1  cgd 		    "rexecd: getpeername: %s\n", strerror(errno));
     88  1.1  cgd 		exit(1);
     89  1.1  cgd 	}
     90  1.1  cgd 	doit(0, &from);
     91  1.4  mrg 	exit(0);
     92  1.1  cgd }
     93  1.1  cgd 
     94  1.1  cgd char	username[20] = "USER=";
     95  1.1  cgd char	homedir[64] = "HOME=";
     96  1.1  cgd char	shell[64] = "SHELL=";
     97  1.1  cgd char	path[sizeof(_PATH_DEFPATH) + sizeof("PATH=")] = "PATH=";
     98  1.1  cgd char	*envinit[] =
     99  1.1  cgd 	    {homedir, shell, path, username, 0};
    100  1.1  cgd char	**environ;
    101  1.1  cgd 
    102  1.1  cgd struct	sockaddr_in asin = { AF_INET };
    103  1.1  cgd 
    104  1.4  mrg void
    105  1.1  cgd doit(f, fromp)
    106  1.1  cgd 	int f;
    107  1.1  cgd 	struct sockaddr_in *fromp;
    108  1.1  cgd {
    109  1.1  cgd 	char cmdbuf[NCARGS+1], *cp, *namep;
    110  1.1  cgd 	char user[16], pass[16];
    111  1.1  cgd 	struct passwd *pwd;
    112  1.4  mrg 	int s = -1; /* XXX gcc */
    113  1.1  cgd 	u_short port;
    114  1.1  cgd 	int pv[2], pid, ready, readfrom, cc;
    115  1.1  cgd 	char buf[BUFSIZ], sig;
    116  1.1  cgd 	int one = 1;
    117  1.1  cgd 
    118  1.1  cgd 	(void) signal(SIGINT, SIG_DFL);
    119  1.1  cgd 	(void) signal(SIGQUIT, SIG_DFL);
    120  1.1  cgd 	(void) signal(SIGTERM, SIG_DFL);
    121  1.1  cgd #ifdef DEBUG
    122  1.1  cgd 	{ int t = open(_PATH_TTY, 2);
    123  1.1  cgd 	  if (t >= 0) {
    124  1.1  cgd 		ioctl(t, TIOCNOTTY, (char *)0);
    125  1.1  cgd 		(void) close(t);
    126  1.1  cgd 	  }
    127  1.1  cgd 	}
    128  1.1  cgd #endif
    129  1.1  cgd 	dup2(f, 0);
    130  1.1  cgd 	dup2(f, 1);
    131  1.1  cgd 	dup2(f, 2);
    132  1.1  cgd 	(void) alarm(60);
    133  1.1  cgd 	port = 0;
    134  1.1  cgd 	for (;;) {
    135  1.1  cgd 		char c;
    136  1.1  cgd 		if (read(f, &c, 1) != 1)
    137  1.1  cgd 			exit(1);
    138  1.1  cgd 		if (c == 0)
    139  1.1  cgd 			break;
    140  1.1  cgd 		port = port * 10 + c - '0';
    141  1.1  cgd 	}
    142  1.1  cgd 	(void) alarm(0);
    143  1.1  cgd 	if (port != 0) {
    144  1.1  cgd 		s = socket(AF_INET, SOCK_STREAM, 0);
    145  1.1  cgd 		if (s < 0)
    146  1.1  cgd 			exit(1);
    147  1.1  cgd 		if (bind(s, (struct sockaddr *)&asin, sizeof (asin)) < 0)
    148  1.1  cgd 			exit(1);
    149  1.1  cgd 		(void) alarm(60);
    150  1.1  cgd 		fromp->sin_port = htons(port);
    151  1.1  cgd 		if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0)
    152  1.1  cgd 			exit(1);
    153  1.1  cgd 		(void) alarm(0);
    154  1.1  cgd 	}
    155  1.1  cgd 	getstr(user, sizeof(user), "username");
    156  1.1  cgd 	getstr(pass, sizeof(pass), "password");
    157  1.1  cgd 	getstr(cmdbuf, sizeof(cmdbuf), "command");
    158  1.1  cgd 	setpwent();
    159  1.1  cgd 	pwd = getpwnam(user);
    160  1.1  cgd 	if (pwd == NULL) {
    161  1.1  cgd 		error("Login incorrect.\n");
    162  1.1  cgd 		exit(1);
    163  1.1  cgd 	}
    164  1.1  cgd 	endpwent();
    165  1.1  cgd 	if (*pwd->pw_passwd != '\0') {
    166  1.1  cgd 		namep = crypt(pass, pwd->pw_passwd);
    167  1.1  cgd 		if (strcmp(namep, pwd->pw_passwd)) {
    168  1.1  cgd 			error("Password incorrect.\n");
    169  1.1  cgd 			exit(1);
    170  1.1  cgd 		}
    171  1.1  cgd 	}
    172  1.1  cgd 	if (chdir(pwd->pw_dir) < 0) {
    173  1.1  cgd 		error("No remote directory.\n");
    174  1.1  cgd 		exit(1);
    175  1.1  cgd 	}
    176  1.1  cgd 	(void) write(2, "\0", 1);
    177  1.1  cgd 	if (port) {
    178  1.1  cgd 		(void) pipe(pv);
    179  1.1  cgd 		pid = fork();
    180  1.1  cgd 		if (pid == -1)  {
    181  1.1  cgd 			error("Try again.\n");
    182  1.1  cgd 			exit(1);
    183  1.1  cgd 		}
    184  1.1  cgd 		if (pid) {
    185  1.1  cgd 			(void) close(0); (void) close(1); (void) close(2);
    186  1.1  cgd 			(void) close(f); (void) close(pv[1]);
    187  1.1  cgd 			readfrom = (1<<s) | (1<<pv[0]);
    188  1.1  cgd 			ioctl(pv[1], FIONBIO, (char *)&one);
    189  1.1  cgd 			/* should set s nbio! */
    190  1.1  cgd 			do {
    191  1.1  cgd 				ready = readfrom;
    192  1.1  cgd 				(void) select(16, (fd_set *)&ready,
    193  1.1  cgd 				    (fd_set *)NULL, (fd_set *)NULL,
    194  1.1  cgd 				    (struct timeval *)NULL);
    195  1.1  cgd 				if (ready & (1<<s)) {
    196  1.1  cgd 					if (read(s, &sig, 1) <= 0)
    197  1.1  cgd 						readfrom &= ~(1<<s);
    198  1.1  cgd 					else
    199  1.1  cgd 						killpg(pid, sig);
    200  1.1  cgd 				}
    201  1.1  cgd 				if (ready & (1<<pv[0])) {
    202  1.1  cgd 					cc = read(pv[0], buf, sizeof (buf));
    203  1.1  cgd 					if (cc <= 0) {
    204  1.1  cgd 						shutdown(s, 1+1);
    205  1.1  cgd 						readfrom &= ~(1<<pv[0]);
    206  1.1  cgd 					} else
    207  1.1  cgd 						(void) write(s, buf, cc);
    208  1.1  cgd 				}
    209  1.1  cgd 			} while (readfrom);
    210  1.1  cgd 			exit(0);
    211  1.1  cgd 		}
    212  1.1  cgd 		setpgrp(0, getpid());
    213  1.1  cgd 		(void) close(s); (void)close(pv[0]);
    214  1.1  cgd 		dup2(pv[1], 2);
    215  1.1  cgd 	}
    216  1.1  cgd 	if (*pwd->pw_shell == '\0')
    217  1.1  cgd 		pwd->pw_shell = _PATH_BSHELL;
    218  1.1  cgd 	if (f > 2)
    219  1.1  cgd 		(void) close(f);
    220  1.3  mrg 	setlogin(pwd->pw_name);
    221  1.1  cgd 	(void) setgid((gid_t)pwd->pw_gid);
    222  1.1  cgd 	initgroups(pwd->pw_name, pwd->pw_gid);
    223  1.1  cgd 	(void) setuid((uid_t)pwd->pw_uid);
    224  1.1  cgd 	(void)strcat(path, _PATH_DEFPATH);
    225  1.1  cgd 	environ = envinit;
    226  1.1  cgd 	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
    227  1.1  cgd 	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
    228  1.1  cgd 	strncat(username, pwd->pw_name, sizeof(username)-6);
    229  1.4  mrg 	cp = strrchr(pwd->pw_shell, '/');
    230  1.1  cgd 	if (cp)
    231  1.1  cgd 		cp++;
    232  1.1  cgd 	else
    233  1.1  cgd 		cp = pwd->pw_shell;
    234  1.1  cgd 	execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
    235  1.1  cgd 	perror(pwd->pw_shell);
    236  1.1  cgd 	exit(1);
    237  1.1  cgd }
    238  1.1  cgd 
    239  1.4  mrg #ifdef __STDC__
    240  1.4  mrg #include <stdarg.h>
    241  1.4  mrg #else
    242  1.4  mrg #include <varargs.h>
    243  1.4  mrg #endif
    244  1.4  mrg 
    245  1.4  mrg void
    246  1.4  mrg #ifdef __STDC__
    247  1.4  mrg error(const char *fmt, ...)
    248  1.4  mrg #else
    249  1.4  mrg error(fmt, va_alist)
    250  1.1  cgd 	char *fmt;
    251  1.4  mrg 	va_dcl
    252  1.4  mrg #endif
    253  1.1  cgd {
    254  1.1  cgd 	char buf[BUFSIZ];
    255  1.4  mrg 	va_list ap;
    256  1.4  mrg 
    257  1.4  mrg #ifdef __STDC__
    258  1.4  mrg 	va_start(ap, fmt);
    259  1.4  mrg #else
    260  1.4  mrg 	va_start(ap);
    261  1.4  mrg #endif
    262  1.1  cgd 
    263  1.1  cgd 	buf[0] = 1;
    264  1.4  mrg 	(void)vsprintf(buf+1, fmt, ap);
    265  1.4  mrg 	(void)write(2, buf, strlen(buf));
    266  1.1  cgd }
    267  1.1  cgd 
    268  1.4  mrg void
    269  1.1  cgd getstr(buf, cnt, err)
    270  1.1  cgd 	char *buf;
    271  1.1  cgd 	int cnt;
    272  1.1  cgd 	char *err;
    273  1.1  cgd {
    274  1.1  cgd 	char c;
    275  1.1  cgd 
    276  1.1  cgd 	do {
    277  1.1  cgd 		if (read(0, &c, 1) != 1)
    278  1.1  cgd 			exit(1);
    279  1.1  cgd 		*buf++ = c;
    280  1.1  cgd 		if (--cnt == 0) {
    281  1.1  cgd 			error("%s too long\n", err);
    282  1.1  cgd 			exit(1);
    283  1.1  cgd 		}
    284  1.1  cgd 	} while (c != 0);
    285  1.1  cgd }
    286