xref: /csrg-svn/libexec/rexecd/rexecd.c (revision 36502)
121163Sdist /*
235734Sbostic  * Copyright (c) 1983 The Regents of the University of California.
335734Sbostic  * All rights reserved.
435734Sbostic  *
535734Sbostic  * Redistribution and use in source and binary forms are permitted
635734Sbostic  * provided that the above copyright notice and this paragraph are
735734Sbostic  * duplicated in all such forms and that any documentation,
835734Sbostic  * advertising materials, and other materials related to such
935734Sbostic  * distribution and use acknowledge that the software was developed
1035734Sbostic  * by the University of California, Berkeley.  The name of the
1135734Sbostic  * University may not be used to endorse or promote products derived
1235734Sbostic  * from this software without specific prior written permission.
1335734Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1435734Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1535734Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621163Sdist  */
1721163Sdist 
186442Swnj #ifndef lint
1921163Sdist char copyright[] =
2035734Sbostic "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
2121163Sdist  All rights reserved.\n";
2235734Sbostic #endif /* not lint */
236442Swnj 
2421163Sdist #ifndef lint
25*36502Sbostic static char sccsid[] = "@(#)rexecd.c	5.7 (Berkeley) 01/04/89";
2635734Sbostic #endif /* not lint */
2721163Sdist 
286442Swnj #include <sys/ioctl.h>
296442Swnj #include <sys/param.h>
306442Swnj #include <sys/socket.h>
3127902Slepreau #include <sys/time.h>
329200Ssam 
339200Ssam #include <netinet/in.h>
349200Ssam 
359200Ssam #include <stdio.h>
366442Swnj #include <errno.h>
376442Swnj #include <pwd.h>
386442Swnj #include <signal.h>
399200Ssam #include <netdb.h>
406442Swnj 
416442Swnj extern	errno;
426442Swnj struct	passwd *getpwnam();
4332446Sbostic char	*crypt(), *rindex(), *strncat();
4427902Slepreau /*VARARGS1*/
456442Swnj int	error();
4627902Slepreau 
476442Swnj /*
486442Swnj  * remote execute server:
496442Swnj  *	username\0
506442Swnj  *	password\0
516442Swnj  *	command\0
526442Swnj  *	data
536442Swnj  */
5427902Slepreau /*ARGSUSED*/
556442Swnj main(argc, argv)
566442Swnj 	int argc;
576442Swnj 	char **argv;
586442Swnj {
596442Swnj 	struct sockaddr_in from;
6016368Skarels 	int fromlen;
616442Swnj 
6216368Skarels 	fromlen = sizeof (from);
6316368Skarels 	if (getpeername(0, &from, &fromlen) < 0) {
6416368Skarels 		fprintf(stderr, "%s: ", argv[0]);
6516368Skarels 		perror("getpeername");
669200Ssam 		exit(1);
679200Ssam 	}
6816368Skarels 	doit(0, &from);
696442Swnj }
706442Swnj 
716442Swnj char	username[20] = "USER=";
726442Swnj char	homedir[64] = "HOME=";
736442Swnj char	shell[64] = "SHELL=";
746442Swnj char	*envinit[] =
756442Swnj 	    {homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", username, 0};
766442Swnj char	**environ;
776442Swnj 
786442Swnj struct	sockaddr_in asin = { AF_INET };
796442Swnj 
806442Swnj doit(f, fromp)
816442Swnj 	int f;
826442Swnj 	struct sockaddr_in *fromp;
836442Swnj {
846442Swnj 	char cmdbuf[NCARGS+1], *cp, *namep;
856442Swnj 	char user[16], pass[16];
866442Swnj 	struct passwd *pwd;
876442Swnj 	int s;
88*36502Sbostic 	u_short port;
896442Swnj 	int pv[2], pid, ready, readfrom, cc;
906442Swnj 	char buf[BUFSIZ], sig;
916442Swnj 	int one = 1;
926442Swnj 
936442Swnj 	(void) signal(SIGINT, SIG_DFL);
946442Swnj 	(void) signal(SIGQUIT, SIG_DFL);
956442Swnj 	(void) signal(SIGTERM, SIG_DFL);
966442Swnj #ifdef DEBUG
976442Swnj 	{ int t = open("/dev/tty", 2);
986442Swnj 	  if (t >= 0) {
996442Swnj 		ioctl(t, TIOCNOTTY, (char *)0);
1006442Swnj 		(void) close(t);
1016442Swnj 	  }
1026442Swnj 	}
1036442Swnj #endif
1046442Swnj 	dup2(f, 0);
1056442Swnj 	dup2(f, 1);
1066442Swnj 	dup2(f, 2);
1076442Swnj 	(void) alarm(60);
1086442Swnj 	port = 0;
1096442Swnj 	for (;;) {
1106442Swnj 		char c;
1116442Swnj 		if (read(f, &c, 1) != 1)
1126442Swnj 			exit(1);
1136442Swnj 		if (c == 0)
1146442Swnj 			break;
1156442Swnj 		port = port * 10 + c - '0';
1166442Swnj 	}
1176442Swnj 	(void) alarm(0);
1186442Swnj 	if (port != 0) {
11925294Sbloom 		s = socket(AF_INET, SOCK_STREAM, 0);
1206442Swnj 		if (s < 0)
1216442Swnj 			exit(1);
12225294Sbloom 		if (bind(s, &asin, sizeof (asin)) < 0)
1239257Ssam 			exit(1);
1246442Swnj 		(void) alarm(60);
125*36502Sbostic 		fromp->sin_port = htons(port);
12627902Slepreau 		if (connect(s, fromp, sizeof (*fromp)) < 0)
1276442Swnj 			exit(1);
1286442Swnj 		(void) alarm(0);
1296442Swnj 	}
1306442Swnj 	getstr(user, sizeof(user), "username");
1316442Swnj 	getstr(pass, sizeof(pass), "password");
1326442Swnj 	getstr(cmdbuf, sizeof(cmdbuf), "command");
1336442Swnj 	setpwent();
1346442Swnj 	pwd = getpwnam(user);
1356442Swnj 	if (pwd == NULL) {
1366442Swnj 		error("Login incorrect.\n");
1376442Swnj 		exit(1);
1386442Swnj 	}
1396442Swnj 	endpwent();
1406442Swnj 	if (*pwd->pw_passwd != '\0') {
1416442Swnj 		namep = crypt(pass, pwd->pw_passwd);
1426442Swnj 		if (strcmp(namep, pwd->pw_passwd)) {
1436442Swnj 			error("Password incorrect.\n");
1446442Swnj 			exit(1);
1456442Swnj 		}
1466442Swnj 	}
1476442Swnj 	if (chdir(pwd->pw_dir) < 0) {
1486442Swnj 		error("No remote directory.\n");
1496442Swnj 		exit(1);
1506442Swnj 	}
1516442Swnj 	(void) write(2, "\0", 1);
1526442Swnj 	if (port) {
1536442Swnj 		(void) pipe(pv);
1546442Swnj 		pid = fork();
1556442Swnj 		if (pid == -1)  {
1566442Swnj 			error("Try again.\n");
1576442Swnj 			exit(1);
1586442Swnj 		}
1596442Swnj 		if (pid) {
1606442Swnj 			(void) close(0); (void) close(1); (void) close(2);
1616442Swnj 			(void) close(f); (void) close(pv[1]);
1626442Swnj 			readfrom = (1<<s) | (1<<pv[0]);
1636442Swnj 			ioctl(pv[1], FIONBIO, (char *)&one);
1646442Swnj 			/* should set s nbio! */
1656442Swnj 			do {
1666442Swnj 				ready = readfrom;
16727902Slepreau 				(void) select(16, &ready, (fd_set *)0,
16827902Slepreau 				    (fd_set *)0, (struct timeval *)0);
1696442Swnj 				if (ready & (1<<s)) {
1706442Swnj 					if (read(s, &sig, 1) <= 0)
1716442Swnj 						readfrom &= ~(1<<s);
1726442Swnj 					else
1736442Swnj 						killpg(pid, sig);
1746442Swnj 				}
1756442Swnj 				if (ready & (1<<pv[0])) {
1766442Swnj 					cc = read(pv[0], buf, sizeof (buf));
1776442Swnj 					if (cc <= 0) {
17810193Ssam 						shutdown(s, 1+1);
1796442Swnj 						readfrom &= ~(1<<pv[0]);
1806442Swnj 					} else
1816442Swnj 						(void) write(s, buf, cc);
1826442Swnj 				}
1836442Swnj 			} while (readfrom);
1846442Swnj 			exit(0);
1856442Swnj 		}
1866442Swnj 		setpgrp(0, getpid());
1876442Swnj 		(void) close(s); (void)close(pv[0]);
1886442Swnj 		dup2(pv[1], 2);
1896442Swnj 	}
1906442Swnj 	if (*pwd->pw_shell == '\0')
1916442Swnj 		pwd->pw_shell = "/bin/sh";
19217184Sralph 	if (f > 2)
19317184Sralph 		(void) close(f);
19427902Slepreau 	(void) setgid((gid_t)pwd->pw_gid);
1959200Ssam 	initgroups(pwd->pw_name, pwd->pw_gid);
19627902Slepreau 	(void) setuid((uid_t)pwd->pw_uid);
1976442Swnj 	environ = envinit;
1986442Swnj 	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
1996442Swnj 	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
2006442Swnj 	strncat(username, pwd->pw_name, sizeof(username)-6);
2016442Swnj 	cp = rindex(pwd->pw_shell, '/');
2026442Swnj 	if (cp)
2036442Swnj 		cp++;
2046442Swnj 	else
2056442Swnj 		cp = pwd->pw_shell;
2066442Swnj 	execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
2076442Swnj 	perror(pwd->pw_shell);
2086442Swnj 	exit(1);
2096442Swnj }
2106442Swnj 
21127902Slepreau /*VARARGS1*/
2126442Swnj error(fmt, a1, a2, a3)
2136442Swnj 	char *fmt;
2146442Swnj 	int a1, a2, a3;
2156442Swnj {
2166442Swnj 	char buf[BUFSIZ];
2176442Swnj 
2186442Swnj 	buf[0] = 1;
2196442Swnj 	(void) sprintf(buf+1, fmt, a1, a2, a3);
2206442Swnj 	(void) write(2, buf, strlen(buf));
2216442Swnj }
2226442Swnj 
2236442Swnj getstr(buf, cnt, err)
2246442Swnj 	char *buf;
2256442Swnj 	int cnt;
2266442Swnj 	char *err;
2276442Swnj {
2286442Swnj 	char c;
2296442Swnj 
2306442Swnj 	do {
2316442Swnj 		if (read(0, &c, 1) != 1)
2326442Swnj 			exit(1);
2336442Swnj 		*buf++ = c;
2346442Swnj 		if (--cnt == 0) {
2356442Swnj 			error("%s too long\n", err);
2366442Swnj 			exit(1);
2376442Swnj 		}
2386442Swnj 	} while (c != 0);
2396442Swnj }
240