xref: /csrg-svn/libexec/rexecd/rexecd.c (revision 21163)
1*21163Sdist /*
2*21163Sdist  * Copyright (c) 1983 Regents of the University of California.
3*21163Sdist  * All rights reserved.  The Berkeley software License Agreement
4*21163Sdist  * specifies the terms and conditions for redistribution.
5*21163Sdist  */
6*21163Sdist 
76442Swnj #ifndef lint
8*21163Sdist char copyright[] =
9*21163Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\
10*21163Sdist  All rights reserved.\n";
11*21163Sdist #endif not lint
126442Swnj 
13*21163Sdist #ifndef lint
14*21163Sdist static char sccsid[] = "@(#)rexecd.c	5.1 (Berkeley) 05/28/85";
15*21163Sdist #endif not lint
16*21163Sdist 
176442Swnj #include <sys/ioctl.h>
186442Swnj #include <sys/param.h>
196442Swnj #include <sys/socket.h>
2013606Ssam #include <sys/wait.h>
219200Ssam 
229200Ssam #include <netinet/in.h>
239200Ssam 
249200Ssam #include <stdio.h>
256442Swnj #include <errno.h>
266442Swnj #include <pwd.h>
276442Swnj #include <signal.h>
289200Ssam #include <netdb.h>
296442Swnj 
306442Swnj extern	errno;
316442Swnj struct	passwd *getpwnam();
326442Swnj char	*crypt(), *rindex(), *sprintf();
336442Swnj /* VARARGS 1 */
346442Swnj int	error();
3510589Ssam int	reapchild();
366442Swnj /*
376442Swnj  * remote execute server:
386442Swnj  *	username\0
396442Swnj  *	password\0
406442Swnj  *	command\0
416442Swnj  *	data
426442Swnj  */
436442Swnj main(argc, argv)
446442Swnj 	int argc;
456442Swnj 	char **argv;
466442Swnj {
476442Swnj 	struct sockaddr_in from;
4816368Skarels 	int fromlen;
496442Swnj 
5016368Skarels 	fromlen = sizeof (from);
5116368Skarels 	if (getpeername(0, &from, &fromlen) < 0) {
5216368Skarels 		fprintf(stderr, "%s: ", argv[0]);
5316368Skarels 		perror("getpeername");
549200Ssam 		exit(1);
559200Ssam 	}
5616368Skarels 	doit(0, &from);
576442Swnj }
586442Swnj 
5910589Ssam reapchild()
6010589Ssam {
6110589Ssam 	union wait status;
6210589Ssam 
6310589Ssam 	while (wait3(&status, WNOHANG, 0) > 0)
6410589Ssam 		;
6510589Ssam }
6610589Ssam 
676442Swnj char	username[20] = "USER=";
686442Swnj char	homedir[64] = "HOME=";
696442Swnj char	shell[64] = "SHELL=";
706442Swnj char	*envinit[] =
716442Swnj 	    {homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", username, 0};
726442Swnj char	**environ;
736442Swnj 
746442Swnj struct	sockaddr_in asin = { AF_INET };
756442Swnj 
766442Swnj doit(f, fromp)
776442Swnj 	int f;
786442Swnj 	struct sockaddr_in *fromp;
796442Swnj {
806442Swnj 	char cmdbuf[NCARGS+1], *cp, *namep;
816442Swnj 	char user[16], pass[16];
826442Swnj 	struct passwd *pwd;
836442Swnj 	int s;
846442Swnj 	short port;
856442Swnj 	int pv[2], pid, ready, readfrom, cc;
866442Swnj 	char buf[BUFSIZ], sig;
876442Swnj 	int one = 1;
886442Swnj 
896442Swnj 	(void) signal(SIGINT, SIG_DFL);
906442Swnj 	(void) signal(SIGQUIT, SIG_DFL);
916442Swnj 	(void) signal(SIGTERM, SIG_DFL);
926442Swnj #ifdef DEBUG
936442Swnj 	{ int t = open("/dev/tty", 2);
946442Swnj 	  if (t >= 0) {
956442Swnj 		ioctl(t, TIOCNOTTY, (char *)0);
966442Swnj 		(void) close(t);
976442Swnj 	  }
986442Swnj 	}
996442Swnj #endif
1006442Swnj 	dup2(f, 0);
1016442Swnj 	dup2(f, 1);
1026442Swnj 	dup2(f, 2);
1036442Swnj 	(void) alarm(60);
1046442Swnj 	port = 0;
1056442Swnj 	for (;;) {
1066442Swnj 		char c;
1076442Swnj 		if (read(f, &c, 1) != 1)
1086442Swnj 			exit(1);
1096442Swnj 		if (c == 0)
1106442Swnj 			break;
1116442Swnj 		port = port * 10 + c - '0';
1126442Swnj 	}
1136442Swnj 	(void) alarm(0);
1146442Swnj 	if (port != 0) {
1159257Ssam 		s = socket(AF_INET, SOCK_STREAM, 0, 0);
1166442Swnj 		if (s < 0)
1176442Swnj 			exit(1);
1189257Ssam 		if (bind(s, &asin, sizeof (asin), 0) < 0)
1199257Ssam 			exit(1);
1206442Swnj 		(void) alarm(60);
1219257Ssam 		fromp->sin_port = htons((u_short)port);
1229200Ssam 		if (connect(s, fromp, sizeof (*fromp), 0) < 0)
1236442Swnj 			exit(1);
1246442Swnj 		(void) alarm(0);
1256442Swnj 	}
1266442Swnj 	getstr(user, sizeof(user), "username");
1276442Swnj 	getstr(pass, sizeof(pass), "password");
1286442Swnj 	getstr(cmdbuf, sizeof(cmdbuf), "command");
1296442Swnj 	setpwent();
1306442Swnj 	pwd = getpwnam(user);
1316442Swnj 	if (pwd == NULL) {
1326442Swnj 		error("Login incorrect.\n");
1336442Swnj 		exit(1);
1346442Swnj 	}
1356442Swnj 	endpwent();
1366442Swnj 	if (*pwd->pw_passwd != '\0') {
1376442Swnj 		namep = crypt(pass, pwd->pw_passwd);
1386442Swnj 		if (strcmp(namep, pwd->pw_passwd)) {
1396442Swnj 			error("Password incorrect.\n");
1406442Swnj 			exit(1);
1416442Swnj 		}
1426442Swnj 	}
1436442Swnj 	if (chdir(pwd->pw_dir) < 0) {
1446442Swnj 		error("No remote directory.\n");
1456442Swnj 		exit(1);
1466442Swnj 	}
1476442Swnj 	(void) write(2, "\0", 1);
1486442Swnj 	if (port) {
1496442Swnj 		(void) pipe(pv);
1506442Swnj 		pid = fork();
1516442Swnj 		if (pid == -1)  {
1526442Swnj 			error("Try again.\n");
1536442Swnj 			exit(1);
1546442Swnj 		}
1556442Swnj 		if (pid) {
1566442Swnj 			(void) close(0); (void) close(1); (void) close(2);
1576442Swnj 			(void) close(f); (void) close(pv[1]);
1586442Swnj 			readfrom = (1<<s) | (1<<pv[0]);
1596442Swnj 			ioctl(pv[1], FIONBIO, (char *)&one);
1606442Swnj 			/* should set s nbio! */
1616442Swnj 			do {
1626442Swnj 				ready = readfrom;
1639200Ssam 				(void) select(16, &ready, 0, 0, 0);
1646442Swnj 				if (ready & (1<<s)) {
1656442Swnj 					if (read(s, &sig, 1) <= 0)
1666442Swnj 						readfrom &= ~(1<<s);
1676442Swnj 					else
1686442Swnj 						killpg(pid, sig);
1696442Swnj 				}
1706442Swnj 				if (ready & (1<<pv[0])) {
1716442Swnj 					cc = read(pv[0], buf, sizeof (buf));
1726442Swnj 					if (cc <= 0) {
17310193Ssam 						shutdown(s, 1+1);
1746442Swnj 						readfrom &= ~(1<<pv[0]);
1756442Swnj 					} else
1766442Swnj 						(void) write(s, buf, cc);
1776442Swnj 				}
1786442Swnj 			} while (readfrom);
1796442Swnj 			exit(0);
1806442Swnj 		}
1816442Swnj 		setpgrp(0, getpid());
1826442Swnj 		(void) close(s); (void)close(pv[0]);
1836442Swnj 		dup2(pv[1], 2);
1846442Swnj 	}
1856442Swnj 	if (*pwd->pw_shell == '\0')
1866442Swnj 		pwd->pw_shell = "/bin/sh";
18717184Sralph 	if (f > 2)
18817184Sralph 		(void) close(f);
1899200Ssam 	initgroups(pwd->pw_name, pwd->pw_gid);
1906442Swnj 	(void) setuid(pwd->pw_uid);
1916442Swnj 	(void) setgid(pwd->pw_gid);
1926442Swnj 	environ = envinit;
1936442Swnj 	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
1946442Swnj 	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
1956442Swnj 	strncat(username, pwd->pw_name, sizeof(username)-6);
1966442Swnj 	cp = rindex(pwd->pw_shell, '/');
1976442Swnj 	if (cp)
1986442Swnj 		cp++;
1996442Swnj 	else
2006442Swnj 		cp = pwd->pw_shell;
2016442Swnj 	execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
2026442Swnj 	perror(pwd->pw_shell);
2036442Swnj 	exit(1);
2046442Swnj }
2056442Swnj 
2066442Swnj /* VARARGS 1 */
2076442Swnj error(fmt, a1, a2, a3)
2086442Swnj 	char *fmt;
2096442Swnj 	int a1, a2, a3;
2106442Swnj {
2116442Swnj 	char buf[BUFSIZ];
2126442Swnj 
2136442Swnj 	buf[0] = 1;
2146442Swnj 	(void) sprintf(buf+1, fmt, a1, a2, a3);
2156442Swnj 	(void) write(2, buf, strlen(buf));
2166442Swnj }
2176442Swnj 
2186442Swnj getstr(buf, cnt, err)
2196442Swnj 	char *buf;
2206442Swnj 	int cnt;
2216442Swnj 	char *err;
2226442Swnj {
2236442Swnj 	char c;
2246442Swnj 
2256442Swnj 	do {
2266442Swnj 		if (read(0, &c, 1) != 1)
2276442Swnj 			exit(1);
2286442Swnj 		*buf++ = c;
2296442Swnj 		if (--cnt == 0) {
2306442Swnj 			error("%s too long\n", err);
2316442Swnj 			exit(1);
2326442Swnj 		}
2336442Swnj 	} while (c != 0);
2346442Swnj }
235