xref: /csrg-svn/libexec/rexecd/rexecd.c (revision 10589)
16442Swnj #ifndef lint
2*10589Ssam static char sccsid[] = "@(#)rexecd.c	4.7 83/01/22";
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();
24*10589Ssam 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 	}
71*10589Ssam 	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) {
78*10589Ssam 			if (errno == EINTR)
79*10589Ssam 				continue;
809200Ssam 			perror("rexecd: accept");
816442Swnj 			sleep(1);
826442Swnj 			continue;
836442Swnj 		}
846442Swnj 		if (fork() == 0)
859200Ssam 			doit(s, &from);
869200Ssam 		(void) close(s);
876442Swnj 	}
886442Swnj }
896442Swnj 
90*10589Ssam reapchild()
91*10589Ssam {
92*10589Ssam 	union wait status;
93*10589Ssam 
94*10589Ssam 	while (wait3(&status, WNOHANG, 0) > 0)
95*10589Ssam 		;
96*10589Ssam }
97*10589Ssam 
986442Swnj char	username[20] = "USER=";
996442Swnj char	homedir[64] = "HOME=";
1006442Swnj char	shell[64] = "SHELL=";
1016442Swnj char	*envinit[] =
1026442Swnj 	    {homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", username, 0};
1036442Swnj char	**environ;
1046442Swnj 
1056442Swnj struct	sockaddr_in asin = { AF_INET };
1066442Swnj 
1076442Swnj doit(f, fromp)
1086442Swnj 	int f;
1096442Swnj 	struct sockaddr_in *fromp;
1106442Swnj {
1116442Swnj 	char cmdbuf[NCARGS+1], *cp, *namep;
1126442Swnj 	char user[16], pass[16];
1136442Swnj 	struct passwd *pwd;
1146442Swnj 	int s;
1156442Swnj 	short port;
1166442Swnj 	int pv[2], pid, ready, readfrom, cc;
1176442Swnj 	char buf[BUFSIZ], sig;
1186442Swnj 	int one = 1;
1196442Swnj 
1206442Swnj 	(void) signal(SIGINT, SIG_DFL);
1216442Swnj 	(void) signal(SIGQUIT, SIG_DFL);
1226442Swnj 	(void) signal(SIGTERM, SIG_DFL);
1236442Swnj #ifdef DEBUG
1246442Swnj 	{ int t = open("/dev/tty", 2);
1256442Swnj 	  if (t >= 0) {
1266442Swnj 		ioctl(t, TIOCNOTTY, (char *)0);
1276442Swnj 		(void) close(t);
1286442Swnj 	  }
1296442Swnj 	}
1306442Swnj #endif
1316442Swnj 	dup2(f, 0);
1326442Swnj 	dup2(f, 1);
1336442Swnj 	dup2(f, 2);
1346442Swnj 	(void) alarm(60);
1356442Swnj 	port = 0;
1366442Swnj 	for (;;) {
1376442Swnj 		char c;
1386442Swnj 		if (read(f, &c, 1) != 1)
1396442Swnj 			exit(1);
1406442Swnj 		if (c == 0)
1416442Swnj 			break;
1426442Swnj 		port = port * 10 + c - '0';
1436442Swnj 	}
1446442Swnj 	(void) alarm(0);
1456442Swnj 	if (port != 0) {
1469257Ssam 		s = socket(AF_INET, SOCK_STREAM, 0, 0);
1476442Swnj 		if (s < 0)
1486442Swnj 			exit(1);
1499257Ssam 		if (bind(s, &asin, sizeof (asin), 0) < 0)
1509257Ssam 			exit(1);
1516442Swnj 		(void) alarm(60);
1529257Ssam 		fromp->sin_port = htons((u_short)port);
1539200Ssam 		if (connect(s, fromp, sizeof (*fromp), 0) < 0)
1546442Swnj 			exit(1);
1556442Swnj 		(void) alarm(0);
1566442Swnj 	}
1576442Swnj 	getstr(user, sizeof(user), "username");
1586442Swnj 	getstr(pass, sizeof(pass), "password");
1596442Swnj 	getstr(cmdbuf, sizeof(cmdbuf), "command");
1606442Swnj 	setpwent();
1616442Swnj 	pwd = getpwnam(user);
1626442Swnj 	if (pwd == NULL) {
1636442Swnj 		error("Login incorrect.\n");
1646442Swnj 		exit(1);
1656442Swnj 	}
1666442Swnj 	endpwent();
1676442Swnj 	if (*pwd->pw_passwd != '\0') {
1686442Swnj 		namep = crypt(pass, pwd->pw_passwd);
1696442Swnj 		if (strcmp(namep, pwd->pw_passwd)) {
1706442Swnj 			error("Password incorrect.\n");
1716442Swnj 			exit(1);
1726442Swnj 		}
1736442Swnj 	}
1746442Swnj 	if (chdir(pwd->pw_dir) < 0) {
1756442Swnj 		error("No remote directory.\n");
1766442Swnj 		exit(1);
1776442Swnj 	}
1786442Swnj 	(void) write(2, "\0", 1);
1796442Swnj 	if (port) {
1806442Swnj 		(void) pipe(pv);
1816442Swnj 		pid = fork();
1826442Swnj 		if (pid == -1)  {
1836442Swnj 			error("Try again.\n");
1846442Swnj 			exit(1);
1856442Swnj 		}
1866442Swnj 		if (pid) {
1876442Swnj 			(void) close(0); (void) close(1); (void) close(2);
1886442Swnj 			(void) close(f); (void) close(pv[1]);
1896442Swnj 			readfrom = (1<<s) | (1<<pv[0]);
1906442Swnj 			ioctl(pv[1], FIONBIO, (char *)&one);
1916442Swnj 			/* should set s nbio! */
1926442Swnj 			do {
1936442Swnj 				ready = readfrom;
1949200Ssam 				(void) select(16, &ready, 0, 0, 0);
1956442Swnj 				if (ready & (1<<s)) {
1966442Swnj 					if (read(s, &sig, 1) <= 0)
1976442Swnj 						readfrom &= ~(1<<s);
1986442Swnj 					else
1996442Swnj 						killpg(pid, sig);
2006442Swnj 				}
2016442Swnj 				if (ready & (1<<pv[0])) {
2026442Swnj 					cc = read(pv[0], buf, sizeof (buf));
2036442Swnj 					if (cc <= 0) {
20410193Ssam 						shutdown(s, 1+1);
2056442Swnj 						readfrom &= ~(1<<pv[0]);
2066442Swnj 					} else
2076442Swnj 						(void) write(s, buf, cc);
2086442Swnj 				}
2096442Swnj 			} while (readfrom);
2106442Swnj 			exit(0);
2116442Swnj 		}
2126442Swnj 		setpgrp(0, getpid());
2136442Swnj 		(void) close(s); (void)close(pv[0]);
2146442Swnj 		dup2(pv[1], 2);
2156442Swnj 	}
2166442Swnj 	if (*pwd->pw_shell == '\0')
2176442Swnj 		pwd->pw_shell = "/bin/sh";
2186442Swnj 	(void) close(f);
2199200Ssam 	initgroups(pwd->pw_name, pwd->pw_gid);
2206442Swnj 	(void) setuid(pwd->pw_uid);
2216442Swnj 	(void) setgid(pwd->pw_gid);
2226442Swnj 	environ = envinit;
2236442Swnj 	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
2246442Swnj 	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
2256442Swnj 	strncat(username, pwd->pw_name, sizeof(username)-6);
2266442Swnj 	cp = rindex(pwd->pw_shell, '/');
2276442Swnj 	if (cp)
2286442Swnj 		cp++;
2296442Swnj 	else
2306442Swnj 		cp = pwd->pw_shell;
2316442Swnj 	execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
2326442Swnj 	perror(pwd->pw_shell);
2336442Swnj 	exit(1);
2346442Swnj }
2356442Swnj 
2366442Swnj /* VARARGS 1 */
2376442Swnj error(fmt, a1, a2, a3)
2386442Swnj 	char *fmt;
2396442Swnj 	int a1, a2, a3;
2406442Swnj {
2416442Swnj 	char buf[BUFSIZ];
2426442Swnj 
2436442Swnj 	buf[0] = 1;
2446442Swnj 	(void) sprintf(buf+1, fmt, a1, a2, a3);
2456442Swnj 	(void) write(2, buf, strlen(buf));
2466442Swnj }
2476442Swnj 
2486442Swnj getstr(buf, cnt, err)
2496442Swnj 	char *buf;
2506442Swnj 	int cnt;
2516442Swnj 	char *err;
2526442Swnj {
2536442Swnj 	char c;
2546442Swnj 
2556442Swnj 	do {
2566442Swnj 		if (read(0, &c, 1) != 1)
2576442Swnj 			exit(1);
2586442Swnj 		*buf++ = c;
2596442Swnj 		if (--cnt == 0) {
2606442Swnj 			error("%s too long\n", err);
2616442Swnj 			exit(1);
2626442Swnj 		}
2636442Swnj 	} while (c != 0);
2646442Swnj }
265