121163Sdist /* 235734Sbostic * Copyright (c) 1983 The Regents of the University of California. 335734Sbostic * All rights reserved. 435734Sbostic * 542670Sbostic * %sccs.include.redist.c% 621163Sdist */ 721163Sdist 86442Swnj #ifndef lint 921163Sdist char copyright[] = 1035734Sbostic "@(#) Copyright (c) 1983 The Regents of the University of California.\n\ 1121163Sdist All rights reserved.\n"; 1235734Sbostic #endif /* not lint */ 136442Swnj 1421163Sdist #ifndef lint 15*46674Sbostic static char sccsid[] = "@(#)rexecd.c 5.11 (Berkeley) 02/25/91"; 1635734Sbostic #endif /* not lint */ 1721163Sdist 18*46674Sbostic #include <sys/param.h> 196442Swnj #include <sys/ioctl.h> 206442Swnj #include <sys/socket.h> 2127902Slepreau #include <sys/time.h> 229200Ssam #include <netinet/in.h> 236442Swnj #include <signal.h> 249200Ssam #include <netdb.h> 25*46674Sbostic #include <pwd.h> 26*46674Sbostic #include <errno.h> 27*46674Sbostic #include <unistd.h> 28*46674Sbostic #include <stdio.h> 29*46674Sbostic #include <stdlib.h> 30*46674Sbostic #include <string.h> 31*46674Sbostic #include <paths.h> 326442Swnj 3327902Slepreau /*VARARGS1*/ 34*46674Sbostic int error(); 3527902Slepreau 366442Swnj /* 376442Swnj * remote execute server: 386442Swnj * username\0 396442Swnj * password\0 406442Swnj * command\0 416442Swnj * data 426442Swnj */ 4327902Slepreau /*ARGSUSED*/ 446442Swnj main(argc, argv) 456442Swnj int argc; 466442Swnj char **argv; 476442Swnj { 486442Swnj struct sockaddr_in from; 4916368Skarels int fromlen; 506442Swnj 5116368Skarels fromlen = sizeof (from); 52*46674Sbostic if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) { 53*46674Sbostic (void)fprintf(stderr, 54*46674Sbostic "rexecd: getpeername: %s\n", strerror(errno)); 559200Ssam exit(1); 569200Ssam } 5716368Skarels doit(0, &from); 586442Swnj } 596442Swnj 606442Swnj char username[20] = "USER="; 616442Swnj char homedir[64] = "HOME="; 626442Swnj char shell[64] = "SHELL="; 636442Swnj char *envinit[] = 6437288Sbostic {homedir, shell, _PATH_DEFPATH, username, 0}; 656442Swnj char **environ; 666442Swnj 676442Swnj struct sockaddr_in asin = { AF_INET }; 686442Swnj 696442Swnj doit(f, fromp) 706442Swnj int f; 716442Swnj struct sockaddr_in *fromp; 726442Swnj { 736442Swnj char cmdbuf[NCARGS+1], *cp, *namep; 746442Swnj char user[16], pass[16]; 756442Swnj struct passwd *pwd; 766442Swnj int s; 7736502Sbostic u_short port; 786442Swnj int pv[2], pid, ready, readfrom, cc; 796442Swnj char buf[BUFSIZ], sig; 806442Swnj int one = 1; 816442Swnj 826442Swnj (void) signal(SIGINT, SIG_DFL); 836442Swnj (void) signal(SIGQUIT, SIG_DFL); 846442Swnj (void) signal(SIGTERM, SIG_DFL); 856442Swnj #ifdef DEBUG 8637989Sbostic { int t = open(_PATH_TTY, 2); 876442Swnj if (t >= 0) { 886442Swnj ioctl(t, TIOCNOTTY, (char *)0); 896442Swnj (void) close(t); 906442Swnj } 916442Swnj } 926442Swnj #endif 936442Swnj dup2(f, 0); 946442Swnj dup2(f, 1); 956442Swnj dup2(f, 2); 966442Swnj (void) alarm(60); 976442Swnj port = 0; 986442Swnj for (;;) { 996442Swnj char c; 1006442Swnj if (read(f, &c, 1) != 1) 1016442Swnj exit(1); 1026442Swnj if (c == 0) 1036442Swnj break; 1046442Swnj port = port * 10 + c - '0'; 1056442Swnj } 1066442Swnj (void) alarm(0); 1076442Swnj if (port != 0) { 10825294Sbloom s = socket(AF_INET, SOCK_STREAM, 0); 1096442Swnj if (s < 0) 1106442Swnj exit(1); 111*46674Sbostic if (bind(s, (struct sockaddr *)&asin, sizeof (asin)) < 0) 1129257Ssam exit(1); 1136442Swnj (void) alarm(60); 11436502Sbostic fromp->sin_port = htons(port); 115*46674Sbostic if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0) 1166442Swnj exit(1); 1176442Swnj (void) alarm(0); 1186442Swnj } 1196442Swnj getstr(user, sizeof(user), "username"); 1206442Swnj getstr(pass, sizeof(pass), "password"); 1216442Swnj getstr(cmdbuf, sizeof(cmdbuf), "command"); 1226442Swnj setpwent(); 1236442Swnj pwd = getpwnam(user); 1246442Swnj if (pwd == NULL) { 1256442Swnj error("Login incorrect.\n"); 1266442Swnj exit(1); 1276442Swnj } 1286442Swnj endpwent(); 1296442Swnj if (*pwd->pw_passwd != '\0') { 1306442Swnj namep = crypt(pass, pwd->pw_passwd); 1316442Swnj if (strcmp(namep, pwd->pw_passwd)) { 1326442Swnj error("Password incorrect.\n"); 1336442Swnj exit(1); 1346442Swnj } 1356442Swnj } 1366442Swnj if (chdir(pwd->pw_dir) < 0) { 1376442Swnj error("No remote directory.\n"); 1386442Swnj exit(1); 1396442Swnj } 1406442Swnj (void) write(2, "\0", 1); 1416442Swnj if (port) { 1426442Swnj (void) pipe(pv); 1436442Swnj pid = fork(); 1446442Swnj if (pid == -1) { 1456442Swnj error("Try again.\n"); 1466442Swnj exit(1); 1476442Swnj } 1486442Swnj if (pid) { 1496442Swnj (void) close(0); (void) close(1); (void) close(2); 1506442Swnj (void) close(f); (void) close(pv[1]); 1516442Swnj readfrom = (1<<s) | (1<<pv[0]); 1526442Swnj ioctl(pv[1], FIONBIO, (char *)&one); 1536442Swnj /* should set s nbio! */ 1546442Swnj do { 1556442Swnj ready = readfrom; 156*46674Sbostic (void) select(16, (fd_set *)&ready, 157*46674Sbostic (fd_set *)NULL, (fd_set *)NULL, 158*46674Sbostic (struct timeval *)NULL); 1596442Swnj if (ready & (1<<s)) { 1606442Swnj if (read(s, &sig, 1) <= 0) 1616442Swnj readfrom &= ~(1<<s); 1626442Swnj else 1636442Swnj killpg(pid, sig); 1646442Swnj } 1656442Swnj if (ready & (1<<pv[0])) { 1666442Swnj cc = read(pv[0], buf, sizeof (buf)); 1676442Swnj if (cc <= 0) { 16810193Ssam shutdown(s, 1+1); 1696442Swnj readfrom &= ~(1<<pv[0]); 1706442Swnj } else 1716442Swnj (void) write(s, buf, cc); 1726442Swnj } 1736442Swnj } while (readfrom); 1746442Swnj exit(0); 1756442Swnj } 1766442Swnj setpgrp(0, getpid()); 1776442Swnj (void) close(s); (void)close(pv[0]); 1786442Swnj dup2(pv[1], 2); 1796442Swnj } 1806442Swnj if (*pwd->pw_shell == '\0') 18137288Sbostic pwd->pw_shell = _PATH_BSHELL; 18217184Sralph if (f > 2) 18317184Sralph (void) close(f); 18427902Slepreau (void) setgid((gid_t)pwd->pw_gid); 1859200Ssam initgroups(pwd->pw_name, pwd->pw_gid); 18627902Slepreau (void) setuid((uid_t)pwd->pw_uid); 1876442Swnj environ = envinit; 1886442Swnj strncat(homedir, pwd->pw_dir, sizeof(homedir)-6); 1896442Swnj strncat(shell, pwd->pw_shell, sizeof(shell)-7); 1906442Swnj strncat(username, pwd->pw_name, sizeof(username)-6); 1916442Swnj cp = rindex(pwd->pw_shell, '/'); 1926442Swnj if (cp) 1936442Swnj cp++; 1946442Swnj else 1956442Swnj cp = pwd->pw_shell; 1966442Swnj execl(pwd->pw_shell, cp, "-c", cmdbuf, 0); 1976442Swnj perror(pwd->pw_shell); 1986442Swnj exit(1); 1996442Swnj } 2006442Swnj 20127902Slepreau /*VARARGS1*/ 2026442Swnj error(fmt, a1, a2, a3) 2036442Swnj char *fmt; 2046442Swnj int a1, a2, a3; 2056442Swnj { 2066442Swnj char buf[BUFSIZ]; 2076442Swnj 2086442Swnj buf[0] = 1; 2096442Swnj (void) sprintf(buf+1, fmt, a1, a2, a3); 2106442Swnj (void) write(2, buf, strlen(buf)); 2116442Swnj } 2126442Swnj 2136442Swnj getstr(buf, cnt, err) 2146442Swnj char *buf; 2156442Swnj int cnt; 2166442Swnj char *err; 2176442Swnj { 2186442Swnj char c; 2196442Swnj 2206442Swnj do { 2216442Swnj if (read(0, &c, 1) != 1) 2226442Swnj exit(1); 2236442Swnj *buf++ = c; 2246442Swnj if (--cnt == 0) { 2256442Swnj error("%s too long\n", err); 2266442Swnj exit(1); 2276442Swnj } 2286442Swnj } while (c != 0); 2296442Swnj } 230