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