xref: /csrg-svn/libexec/rexecd/rexecd.c (revision 37288)
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*37288Sbostic static char sccsid[] = "@(#)rexecd.c	5.8 (Berkeley) 04/02/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>
40*37288Sbostic #include "pathnames.h"
416442Swnj 
42*37288Sbostic extern	int errno;
436442Swnj struct	passwd *getpwnam();
4432446Sbostic char	*crypt(), *rindex(), *strncat();
4527902Slepreau /*VARARGS1*/
466442Swnj int	error();
4727902Slepreau 
486442Swnj /*
496442Swnj  * remote execute server:
506442Swnj  *	username\0
516442Swnj  *	password\0
526442Swnj  *	command\0
536442Swnj  *	data
546442Swnj  */
5527902Slepreau /*ARGSUSED*/
566442Swnj main(argc, argv)
576442Swnj 	int argc;
586442Swnj 	char **argv;
596442Swnj {
606442Swnj 	struct sockaddr_in from;
6116368Skarels 	int fromlen;
626442Swnj 
6316368Skarels 	fromlen = sizeof (from);
6416368Skarels 	if (getpeername(0, &from, &fromlen) < 0) {
6516368Skarels 		fprintf(stderr, "%s: ", argv[0]);
6616368Skarels 		perror("getpeername");
679200Ssam 		exit(1);
689200Ssam 	}
6916368Skarels 	doit(0, &from);
706442Swnj }
716442Swnj 
726442Swnj char	username[20] = "USER=";
736442Swnj char	homedir[64] = "HOME=";
746442Swnj char	shell[64] = "SHELL=";
756442Swnj char	*envinit[] =
76*37288Sbostic 	    {homedir, shell, _PATH_DEFPATH, username, 0};
776442Swnj char	**environ;
786442Swnj 
796442Swnj struct	sockaddr_in asin = { AF_INET };
806442Swnj 
816442Swnj doit(f, fromp)
826442Swnj 	int f;
836442Swnj 	struct sockaddr_in *fromp;
846442Swnj {
856442Swnj 	char cmdbuf[NCARGS+1], *cp, *namep;
866442Swnj 	char user[16], pass[16];
876442Swnj 	struct passwd *pwd;
886442Swnj 	int s;
8936502Sbostic 	u_short port;
906442Swnj 	int pv[2], pid, ready, readfrom, cc;
916442Swnj 	char buf[BUFSIZ], sig;
926442Swnj 	int one = 1;
936442Swnj 
946442Swnj 	(void) signal(SIGINT, SIG_DFL);
956442Swnj 	(void) signal(SIGQUIT, SIG_DFL);
966442Swnj 	(void) signal(SIGTERM, SIG_DFL);
976442Swnj #ifdef DEBUG
986442Swnj 	{ int t = open("/dev/tty", 2);
996442Swnj 	  if (t >= 0) {
1006442Swnj 		ioctl(t, TIOCNOTTY, (char *)0);
1016442Swnj 		(void) close(t);
1026442Swnj 	  }
1036442Swnj 	}
1046442Swnj #endif
1056442Swnj 	dup2(f, 0);
1066442Swnj 	dup2(f, 1);
1076442Swnj 	dup2(f, 2);
1086442Swnj 	(void) alarm(60);
1096442Swnj 	port = 0;
1106442Swnj 	for (;;) {
1116442Swnj 		char c;
1126442Swnj 		if (read(f, &c, 1) != 1)
1136442Swnj 			exit(1);
1146442Swnj 		if (c == 0)
1156442Swnj 			break;
1166442Swnj 		port = port * 10 + c - '0';
1176442Swnj 	}
1186442Swnj 	(void) alarm(0);
1196442Swnj 	if (port != 0) {
12025294Sbloom 		s = socket(AF_INET, SOCK_STREAM, 0);
1216442Swnj 		if (s < 0)
1226442Swnj 			exit(1);
12325294Sbloom 		if (bind(s, &asin, sizeof (asin)) < 0)
1249257Ssam 			exit(1);
1256442Swnj 		(void) alarm(60);
12636502Sbostic 		fromp->sin_port = htons(port);
12727902Slepreau 		if (connect(s, fromp, sizeof (*fromp)) < 0)
1286442Swnj 			exit(1);
1296442Swnj 		(void) alarm(0);
1306442Swnj 	}
1316442Swnj 	getstr(user, sizeof(user), "username");
1326442Swnj 	getstr(pass, sizeof(pass), "password");
1336442Swnj 	getstr(cmdbuf, sizeof(cmdbuf), "command");
1346442Swnj 	setpwent();
1356442Swnj 	pwd = getpwnam(user);
1366442Swnj 	if (pwd == NULL) {
1376442Swnj 		error("Login incorrect.\n");
1386442Swnj 		exit(1);
1396442Swnj 	}
1406442Swnj 	endpwent();
1416442Swnj 	if (*pwd->pw_passwd != '\0') {
1426442Swnj 		namep = crypt(pass, pwd->pw_passwd);
1436442Swnj 		if (strcmp(namep, pwd->pw_passwd)) {
1446442Swnj 			error("Password incorrect.\n");
1456442Swnj 			exit(1);
1466442Swnj 		}
1476442Swnj 	}
1486442Swnj 	if (chdir(pwd->pw_dir) < 0) {
1496442Swnj 		error("No remote directory.\n");
1506442Swnj 		exit(1);
1516442Swnj 	}
1526442Swnj 	(void) write(2, "\0", 1);
1536442Swnj 	if (port) {
1546442Swnj 		(void) pipe(pv);
1556442Swnj 		pid = fork();
1566442Swnj 		if (pid == -1)  {
1576442Swnj 			error("Try again.\n");
1586442Swnj 			exit(1);
1596442Swnj 		}
1606442Swnj 		if (pid) {
1616442Swnj 			(void) close(0); (void) close(1); (void) close(2);
1626442Swnj 			(void) close(f); (void) close(pv[1]);
1636442Swnj 			readfrom = (1<<s) | (1<<pv[0]);
1646442Swnj 			ioctl(pv[1], FIONBIO, (char *)&one);
1656442Swnj 			/* should set s nbio! */
1666442Swnj 			do {
1676442Swnj 				ready = readfrom;
16827902Slepreau 				(void) select(16, &ready, (fd_set *)0,
16927902Slepreau 				    (fd_set *)0, (struct timeval *)0);
1706442Swnj 				if (ready & (1<<s)) {
1716442Swnj 					if (read(s, &sig, 1) <= 0)
1726442Swnj 						readfrom &= ~(1<<s);
1736442Swnj 					else
1746442Swnj 						killpg(pid, sig);
1756442Swnj 				}
1766442Swnj 				if (ready & (1<<pv[0])) {
1776442Swnj 					cc = read(pv[0], buf, sizeof (buf));
1786442Swnj 					if (cc <= 0) {
17910193Ssam 						shutdown(s, 1+1);
1806442Swnj 						readfrom &= ~(1<<pv[0]);
1816442Swnj 					} else
1826442Swnj 						(void) write(s, buf, cc);
1836442Swnj 				}
1846442Swnj 			} while (readfrom);
1856442Swnj 			exit(0);
1866442Swnj 		}
1876442Swnj 		setpgrp(0, getpid());
1886442Swnj 		(void) close(s); (void)close(pv[0]);
1896442Swnj 		dup2(pv[1], 2);
1906442Swnj 	}
1916442Swnj 	if (*pwd->pw_shell == '\0')
192*37288Sbostic 		pwd->pw_shell = _PATH_BSHELL;
19317184Sralph 	if (f > 2)
19417184Sralph 		(void) close(f);
19527902Slepreau 	(void) setgid((gid_t)pwd->pw_gid);
1969200Ssam 	initgroups(pwd->pw_name, pwd->pw_gid);
19727902Slepreau 	(void) setuid((uid_t)pwd->pw_uid);
1986442Swnj 	environ = envinit;
1996442Swnj 	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
2006442Swnj 	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
2016442Swnj 	strncat(username, pwd->pw_name, sizeof(username)-6);
2026442Swnj 	cp = rindex(pwd->pw_shell, '/');
2036442Swnj 	if (cp)
2046442Swnj 		cp++;
2056442Swnj 	else
2066442Swnj 		cp = pwd->pw_shell;
2076442Swnj 	execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
2086442Swnj 	perror(pwd->pw_shell);
2096442Swnj 	exit(1);
2106442Swnj }
2116442Swnj 
21227902Slepreau /*VARARGS1*/
2136442Swnj error(fmt, a1, a2, a3)
2146442Swnj 	char *fmt;
2156442Swnj 	int a1, a2, a3;
2166442Swnj {
2176442Swnj 	char buf[BUFSIZ];
2186442Swnj 
2196442Swnj 	buf[0] = 1;
2206442Swnj 	(void) sprintf(buf+1, fmt, a1, a2, a3);
2216442Swnj 	(void) write(2, buf, strlen(buf));
2226442Swnj }
2236442Swnj 
2246442Swnj getstr(buf, cnt, err)
2256442Swnj 	char *buf;
2266442Swnj 	int cnt;
2276442Swnj 	char *err;
2286442Swnj {
2296442Swnj 	char c;
2306442Swnj 
2316442Swnj 	do {
2326442Swnj 		if (read(0, &c, 1) != 1)
2336442Swnj 			exit(1);
2346442Swnj 		*buf++ = c;
2356442Swnj 		if (--cnt == 0) {
2366442Swnj 			error("%s too long\n", err);
2376442Swnj 			exit(1);
2386442Swnj 		}
2396442Swnj 	} while (c != 0);
2406442Swnj }
241