xref: /csrg-svn/libexec/rexecd/rexecd.c (revision 11224)
16442Swnj #ifndef lint
2*11224Ssam static char sccsid[] = "@(#)rexecd.c	4.8 83/02/21";
36442Swnj #endif
46442Swnj 
56442Swnj #include <sys/ioctl.h>
66442Swnj #include <sys/param.h>
76442Swnj #include <sys/socket.h>
89200Ssam 
99200Ssam #include <netinet/in.h>
109200Ssam 
119200Ssam #include <stdio.h>
126442Swnj #include <errno.h>
136442Swnj #include <pwd.h>
146442Swnj #include <wait.h>
156442Swnj #include <signal.h>
169200Ssam #include <netdb.h>
176442Swnj 
186442Swnj extern	errno;
199200Ssam struct	sockaddr_in sin = { AF_INET };
206442Swnj struct	passwd *getpwnam();
216442Swnj char	*crypt(), *rindex(), *sprintf();
226442Swnj /* VARARGS 1 */
236442Swnj int	error();
2410589Ssam int	reapchild();
256442Swnj /*
266442Swnj  * remote execute server:
276442Swnj  *	username\0
286442Swnj  *	password\0
296442Swnj  *	command\0
306442Swnj  *	data
316442Swnj  */
326442Swnj main(argc, argv)
336442Swnj 	int argc;
346442Swnj 	char **argv;
356442Swnj {
366442Swnj 	int f;
376442Swnj 	struct sockaddr_in from;
389200Ssam 	struct servent *sp;
396442Swnj 
409200Ssam 	sp = getservbyname("exec", "tcp");
419200Ssam 	if (sp == 0) {
429200Ssam 		fprintf(stderr, "tcp/exec: unknown service\n");
439200Ssam 		exit(1);
449200Ssam 	}
459964Ssam 	sin.sin_port = sp->s_port;
466442Swnj #ifndef DEBUG
476442Swnj 	if (fork())
486442Swnj 		exit(0);
496442Swnj 	for (f = 0; f < 10; f++)
506442Swnj 		(void) close(f);
516442Swnj 	(void) open("/", 0);
526442Swnj 	(void) dup2(0, 1);
536442Swnj 	(void) dup2(0, 2);
546442Swnj 	{ int t = open("/dev/tty", 2);
556442Swnj 	  if (t >= 0) {
566442Swnj 		ioctl(t, TIOCNOTTY, (char *)0);
576442Swnj 		(void) close(t);
586442Swnj 	  }
596442Swnj 	}
606442Swnj #endif
616442Swnj 	argc--, argv++;
629257Ssam 	f = socket(AF_INET, SOCK_STREAM, 0, 0);
639200Ssam 	if (f < 0) {
649200Ssam 		perror("rexecd: socket");
659200Ssam 		exit(1);
669200Ssam 	}
679200Ssam 	if (bind(f, &sin, sizeof (sin), 0) < 0) {
689200Ssam 		perror("rexecd: bind:");
699200Ssam 		exit(1);
709200Ssam 	}
7110589Ssam 	sigset(SIGCHLD, reapchild);
729200Ssam 	listen(f, 10);
736442Swnj 	for (;;) {
749200Ssam 		int s, len = sizeof (from);
759200Ssam 
769200Ssam 		s = accept(f, &from, &len, 0);
779200Ssam 		if (s < 0) {
7810589Ssam 			if (errno == EINTR)
7910589Ssam 				continue;
809200Ssam 			perror("rexecd: accept");
816442Swnj 			sleep(1);
826442Swnj 			continue;
836442Swnj 		}
84*11224Ssam 		if (fork() == 0) {
85*11224Ssam 			signal(SIGCHLD, SIG_IGN);
869200Ssam 			doit(s, &from);
87*11224Ssam 		}
889200Ssam 		(void) close(s);
896442Swnj 	}
906442Swnj }
916442Swnj 
9210589Ssam reapchild()
9310589Ssam {
9410589Ssam 	union wait status;
9510589Ssam 
9610589Ssam 	while (wait3(&status, WNOHANG, 0) > 0)
9710589Ssam 		;
9810589Ssam }
9910589Ssam 
1006442Swnj char	username[20] = "USER=";
1016442Swnj char	homedir[64] = "HOME=";
1026442Swnj char	shell[64] = "SHELL=";
1036442Swnj char	*envinit[] =
1046442Swnj 	    {homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", username, 0};
1056442Swnj char	**environ;
1066442Swnj 
1076442Swnj struct	sockaddr_in asin = { AF_INET };
1086442Swnj 
1096442Swnj doit(f, fromp)
1106442Swnj 	int f;
1116442Swnj 	struct sockaddr_in *fromp;
1126442Swnj {
1136442Swnj 	char cmdbuf[NCARGS+1], *cp, *namep;
1146442Swnj 	char user[16], pass[16];
1156442Swnj 	struct passwd *pwd;
1166442Swnj 	int s;
1176442Swnj 	short port;
1186442Swnj 	int pv[2], pid, ready, readfrom, cc;
1196442Swnj 	char buf[BUFSIZ], sig;
1206442Swnj 	int one = 1;
1216442Swnj 
1226442Swnj 	(void) signal(SIGINT, SIG_DFL);
1236442Swnj 	(void) signal(SIGQUIT, SIG_DFL);
1246442Swnj 	(void) signal(SIGTERM, SIG_DFL);
1256442Swnj #ifdef DEBUG
1266442Swnj 	{ int t = open("/dev/tty", 2);
1276442Swnj 	  if (t >= 0) {
1286442Swnj 		ioctl(t, TIOCNOTTY, (char *)0);
1296442Swnj 		(void) close(t);
1306442Swnj 	  }
1316442Swnj 	}
1326442Swnj #endif
1336442Swnj 	dup2(f, 0);
1346442Swnj 	dup2(f, 1);
1356442Swnj 	dup2(f, 2);
1366442Swnj 	(void) alarm(60);
1376442Swnj 	port = 0;
1386442Swnj 	for (;;) {
1396442Swnj 		char c;
1406442Swnj 		if (read(f, &c, 1) != 1)
1416442Swnj 			exit(1);
1426442Swnj 		if (c == 0)
1436442Swnj 			break;
1446442Swnj 		port = port * 10 + c - '0';
1456442Swnj 	}
1466442Swnj 	(void) alarm(0);
1476442Swnj 	if (port != 0) {
1489257Ssam 		s = socket(AF_INET, SOCK_STREAM, 0, 0);
1496442Swnj 		if (s < 0)
1506442Swnj 			exit(1);
1519257Ssam 		if (bind(s, &asin, sizeof (asin), 0) < 0)
1529257Ssam 			exit(1);
1536442Swnj 		(void) alarm(60);
1549257Ssam 		fromp->sin_port = htons((u_short)port);
1559200Ssam 		if (connect(s, fromp, sizeof (*fromp), 0) < 0)
1566442Swnj 			exit(1);
1576442Swnj 		(void) alarm(0);
1586442Swnj 	}
1596442Swnj 	getstr(user, sizeof(user), "username");
1606442Swnj 	getstr(pass, sizeof(pass), "password");
1616442Swnj 	getstr(cmdbuf, sizeof(cmdbuf), "command");
1626442Swnj 	setpwent();
1636442Swnj 	pwd = getpwnam(user);
1646442Swnj 	if (pwd == NULL) {
1656442Swnj 		error("Login incorrect.\n");
1666442Swnj 		exit(1);
1676442Swnj 	}
1686442Swnj 	endpwent();
1696442Swnj 	if (*pwd->pw_passwd != '\0') {
1706442Swnj 		namep = crypt(pass, pwd->pw_passwd);
1716442Swnj 		if (strcmp(namep, pwd->pw_passwd)) {
1726442Swnj 			error("Password incorrect.\n");
1736442Swnj 			exit(1);
1746442Swnj 		}
1756442Swnj 	}
1766442Swnj 	if (chdir(pwd->pw_dir) < 0) {
1776442Swnj 		error("No remote directory.\n");
1786442Swnj 		exit(1);
1796442Swnj 	}
1806442Swnj 	(void) write(2, "\0", 1);
1816442Swnj 	if (port) {
1826442Swnj 		(void) pipe(pv);
1836442Swnj 		pid = fork();
1846442Swnj 		if (pid == -1)  {
1856442Swnj 			error("Try again.\n");
1866442Swnj 			exit(1);
1876442Swnj 		}
1886442Swnj 		if (pid) {
1896442Swnj 			(void) close(0); (void) close(1); (void) close(2);
1906442Swnj 			(void) close(f); (void) close(pv[1]);
1916442Swnj 			readfrom = (1<<s) | (1<<pv[0]);
1926442Swnj 			ioctl(pv[1], FIONBIO, (char *)&one);
1936442Swnj 			/* should set s nbio! */
1946442Swnj 			do {
1956442Swnj 				ready = readfrom;
1969200Ssam 				(void) select(16, &ready, 0, 0, 0);
1976442Swnj 				if (ready & (1<<s)) {
1986442Swnj 					if (read(s, &sig, 1) <= 0)
1996442Swnj 						readfrom &= ~(1<<s);
2006442Swnj 					else
2016442Swnj 						killpg(pid, sig);
2026442Swnj 				}
2036442Swnj 				if (ready & (1<<pv[0])) {
2046442Swnj 					cc = read(pv[0], buf, sizeof (buf));
2056442Swnj 					if (cc <= 0) {
20610193Ssam 						shutdown(s, 1+1);
2076442Swnj 						readfrom &= ~(1<<pv[0]);
2086442Swnj 					} else
2096442Swnj 						(void) write(s, buf, cc);
2106442Swnj 				}
2116442Swnj 			} while (readfrom);
2126442Swnj 			exit(0);
2136442Swnj 		}
2146442Swnj 		setpgrp(0, getpid());
2156442Swnj 		(void) close(s); (void)close(pv[0]);
2166442Swnj 		dup2(pv[1], 2);
2176442Swnj 	}
2186442Swnj 	if (*pwd->pw_shell == '\0')
2196442Swnj 		pwd->pw_shell = "/bin/sh";
2206442Swnj 	(void) close(f);
2219200Ssam 	initgroups(pwd->pw_name, pwd->pw_gid);
2226442Swnj 	(void) setuid(pwd->pw_uid);
2236442Swnj 	(void) setgid(pwd->pw_gid);
2246442Swnj 	environ = envinit;
2256442Swnj 	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
2266442Swnj 	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
2276442Swnj 	strncat(username, pwd->pw_name, sizeof(username)-6);
2286442Swnj 	cp = rindex(pwd->pw_shell, '/');
2296442Swnj 	if (cp)
2306442Swnj 		cp++;
2316442Swnj 	else
2326442Swnj 		cp = pwd->pw_shell;
2336442Swnj 	execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
2346442Swnj 	perror(pwd->pw_shell);
2356442Swnj 	exit(1);
2366442Swnj }
2376442Swnj 
2386442Swnj /* VARARGS 1 */
2396442Swnj error(fmt, a1, a2, a3)
2406442Swnj 	char *fmt;
2416442Swnj 	int a1, a2, a3;
2426442Swnj {
2436442Swnj 	char buf[BUFSIZ];
2446442Swnj 
2456442Swnj 	buf[0] = 1;
2466442Swnj 	(void) sprintf(buf+1, fmt, a1, a2, a3);
2476442Swnj 	(void) write(2, buf, strlen(buf));
2486442Swnj }
2496442Swnj 
2506442Swnj getstr(buf, cnt, err)
2516442Swnj 	char *buf;
2526442Swnj 	int cnt;
2536442Swnj 	char *err;
2546442Swnj {
2556442Swnj 	char c;
2566442Swnj 
2576442Swnj 	do {
2586442Swnj 		if (read(0, &c, 1) != 1)
2596442Swnj 			exit(1);
2606442Swnj 		*buf++ = c;
2616442Swnj 		if (--cnt == 0) {
2626442Swnj 			error("%s too long\n", err);
2636442Swnj 			exit(1);
2646442Swnj 		}
2656442Swnj 	} while (c != 0);
2666442Swnj }
267