121163Sdist /*
2*61437Sbostic * Copyright (c) 1983, 1993
3*61437Sbostic * The Regents of the University of California. All rights reserved.
435734Sbostic *
542670Sbostic * %sccs.include.redist.c%
621163Sdist */
721163Sdist
86442Swnj #ifndef lint
9*61437Sbostic static char copyright[] =
10*61437Sbostic "@(#) Copyright (c) 1983, 1993\n\
11*61437Sbostic The Regents of the University of California. All rights reserved.\n";
1235734Sbostic #endif /* not lint */
136442Swnj
1421163Sdist #ifndef lint
15*61437Sbostic static char sccsid[] = "@(#)rexecd.c 8.1 (Berkeley) 06/04/93";
1635734Sbostic #endif /* not lint */
1721163Sdist
1846674Sbostic #include <sys/param.h>
196442Swnj #include <sys/ioctl.h>
206442Swnj #include <sys/socket.h>
2127902Slepreau #include <sys/time.h>
2260099Sbostic
239200Ssam #include <netinet/in.h>
2460099Sbostic
2560099Sbostic #include <errno.h>
269200Ssam #include <netdb.h>
2760099Sbostic #include <paths.h>
2846674Sbostic #include <pwd.h>
2960099Sbostic #include <signal.h>
3046674Sbostic #include <stdio.h>
3146674Sbostic #include <stdlib.h>
3246674Sbostic #include <string.h>
3360099Sbostic #include <unistd.h>
346442Swnj
3527902Slepreau /*VARARGS1*/
3646674Sbostic int error();
3727902Slepreau
386442Swnj /*
396442Swnj * remote execute server:
406442Swnj * username\0
416442Swnj * password\0
426442Swnj * command\0
436442Swnj * data
446442Swnj */
4527902Slepreau /*ARGSUSED*/
main(argc,argv)466442Swnj main(argc, argv)
476442Swnj int argc;
486442Swnj char **argv;
496442Swnj {
506442Swnj struct sockaddr_in from;
5116368Skarels int fromlen;
526442Swnj
5316368Skarels fromlen = sizeof (from);
5446674Sbostic if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
5546674Sbostic (void)fprintf(stderr,
5646674Sbostic "rexecd: getpeername: %s\n", strerror(errno));
579200Ssam exit(1);
589200Ssam }
5916368Skarels doit(0, &from);
606442Swnj }
616442Swnj
626442Swnj char username[20] = "USER=";
636442Swnj char homedir[64] = "HOME=";
646442Swnj char shell[64] = "SHELL=";
6546676Sbostic char path[sizeof(_PATH_DEFPATH) + sizeof("PATH=")] = "PATH=";
666442Swnj char *envinit[] =
6746676Sbostic {homedir, shell, path, username, 0};
686442Swnj char **environ;
696442Swnj
706442Swnj struct sockaddr_in asin = { AF_INET };
716442Swnj
doit(f,fromp)726442Swnj doit(f, fromp)
736442Swnj int f;
746442Swnj struct sockaddr_in *fromp;
756442Swnj {
766442Swnj char cmdbuf[NCARGS+1], *cp, *namep;
776442Swnj char user[16], pass[16];
786442Swnj struct passwd *pwd;
796442Swnj int s;
8036502Sbostic u_short port;
816442Swnj int pv[2], pid, ready, readfrom, cc;
826442Swnj char buf[BUFSIZ], sig;
836442Swnj int one = 1;
846442Swnj
856442Swnj (void) signal(SIGINT, SIG_DFL);
866442Swnj (void) signal(SIGQUIT, SIG_DFL);
876442Swnj (void) signal(SIGTERM, SIG_DFL);
886442Swnj #ifdef DEBUG
8937989Sbostic { int t = open(_PATH_TTY, 2);
906442Swnj if (t >= 0) {
916442Swnj ioctl(t, TIOCNOTTY, (char *)0);
926442Swnj (void) close(t);
936442Swnj }
946442Swnj }
956442Swnj #endif
966442Swnj dup2(f, 0);
976442Swnj dup2(f, 1);
986442Swnj dup2(f, 2);
996442Swnj (void) alarm(60);
1006442Swnj port = 0;
1016442Swnj for (;;) {
1026442Swnj char c;
1036442Swnj if (read(f, &c, 1) != 1)
1046442Swnj exit(1);
1056442Swnj if (c == 0)
1066442Swnj break;
1076442Swnj port = port * 10 + c - '0';
1086442Swnj }
1096442Swnj (void) alarm(0);
1106442Swnj if (port != 0) {
11125294Sbloom s = socket(AF_INET, SOCK_STREAM, 0);
1126442Swnj if (s < 0)
1136442Swnj exit(1);
11446674Sbostic if (bind(s, (struct sockaddr *)&asin, sizeof (asin)) < 0)
1159257Ssam exit(1);
1166442Swnj (void) alarm(60);
11736502Sbostic fromp->sin_port = htons(port);
11846674Sbostic if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0)
1196442Swnj exit(1);
1206442Swnj (void) alarm(0);
1216442Swnj }
1226442Swnj getstr(user, sizeof(user), "username");
1236442Swnj getstr(pass, sizeof(pass), "password");
1246442Swnj getstr(cmdbuf, sizeof(cmdbuf), "command");
1256442Swnj setpwent();
1266442Swnj pwd = getpwnam(user);
1276442Swnj if (pwd == NULL) {
1286442Swnj error("Login incorrect.\n");
1296442Swnj exit(1);
1306442Swnj }
1316442Swnj endpwent();
1326442Swnj if (*pwd->pw_passwd != '\0') {
1336442Swnj namep = crypt(pass, pwd->pw_passwd);
1346442Swnj if (strcmp(namep, pwd->pw_passwd)) {
1356442Swnj error("Password incorrect.\n");
1366442Swnj exit(1);
1376442Swnj }
1386442Swnj }
1396442Swnj if (chdir(pwd->pw_dir) < 0) {
1406442Swnj error("No remote directory.\n");
1416442Swnj exit(1);
1426442Swnj }
1436442Swnj (void) write(2, "\0", 1);
1446442Swnj if (port) {
1456442Swnj (void) pipe(pv);
1466442Swnj pid = fork();
1476442Swnj if (pid == -1) {
1486442Swnj error("Try again.\n");
1496442Swnj exit(1);
1506442Swnj }
1516442Swnj if (pid) {
1526442Swnj (void) close(0); (void) close(1); (void) close(2);
1536442Swnj (void) close(f); (void) close(pv[1]);
1546442Swnj readfrom = (1<<s) | (1<<pv[0]);
1556442Swnj ioctl(pv[1], FIONBIO, (char *)&one);
1566442Swnj /* should set s nbio! */
1576442Swnj do {
1586442Swnj ready = readfrom;
15946674Sbostic (void) select(16, (fd_set *)&ready,
16046674Sbostic (fd_set *)NULL, (fd_set *)NULL,
16146674Sbostic (struct timeval *)NULL);
1626442Swnj if (ready & (1<<s)) {
1636442Swnj if (read(s, &sig, 1) <= 0)
1646442Swnj readfrom &= ~(1<<s);
1656442Swnj else
1666442Swnj killpg(pid, sig);
1676442Swnj }
1686442Swnj if (ready & (1<<pv[0])) {
1696442Swnj cc = read(pv[0], buf, sizeof (buf));
1706442Swnj if (cc <= 0) {
17110193Ssam shutdown(s, 1+1);
1726442Swnj readfrom &= ~(1<<pv[0]);
1736442Swnj } else
1746442Swnj (void) write(s, buf, cc);
1756442Swnj }
1766442Swnj } while (readfrom);
1776442Swnj exit(0);
1786442Swnj }
1796442Swnj setpgrp(0, getpid());
1806442Swnj (void) close(s); (void)close(pv[0]);
1816442Swnj dup2(pv[1], 2);
1826442Swnj }
1836442Swnj if (*pwd->pw_shell == '\0')
18437288Sbostic pwd->pw_shell = _PATH_BSHELL;
18517184Sralph if (f > 2)
18617184Sralph (void) close(f);
18727902Slepreau (void) setgid((gid_t)pwd->pw_gid);
1889200Ssam initgroups(pwd->pw_name, pwd->pw_gid);
18927902Slepreau (void) setuid((uid_t)pwd->pw_uid);
19046676Sbostic (void)strcat(path, _PATH_DEFPATH);
1916442Swnj environ = envinit;
1926442Swnj strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
1936442Swnj strncat(shell, pwd->pw_shell, sizeof(shell)-7);
1946442Swnj strncat(username, pwd->pw_name, sizeof(username)-6);
19560099Sbostic cp = strrchr(pwd->pw_shell, '/');
1966442Swnj if (cp)
1976442Swnj cp++;
1986442Swnj else
1996442Swnj cp = pwd->pw_shell;
2006442Swnj execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
2016442Swnj perror(pwd->pw_shell);
2026442Swnj exit(1);
2036442Swnj }
2046442Swnj
20527902Slepreau /*VARARGS1*/
error(fmt,a1,a2,a3)2066442Swnj error(fmt, a1, a2, a3)
2076442Swnj char *fmt;
2086442Swnj int a1, a2, a3;
2096442Swnj {
2106442Swnj char buf[BUFSIZ];
2116442Swnj
2126442Swnj buf[0] = 1;
2136442Swnj (void) sprintf(buf+1, fmt, a1, a2, a3);
2146442Swnj (void) write(2, buf, strlen(buf));
2156442Swnj }
2166442Swnj
getstr(buf,cnt,err)2176442Swnj getstr(buf, cnt, err)
2186442Swnj char *buf;
2196442Swnj int cnt;
2206442Swnj char *err;
2216442Swnj {
2226442Swnj char c;
2236442Swnj
2246442Swnj do {
2256442Swnj if (read(0, &c, 1) != 1)
2266442Swnj exit(1);
2276442Swnj *buf++ = c;
2286442Swnj if (--cnt == 0) {
2296442Swnj error("%s too long\n", err);
2306442Swnj exit(1);
2316442Swnj }
2326442Swnj } while (c != 0);
2336442Swnj }
234