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*46676Sbostic static char sccsid[] = "@(#)rexecd.c 5.12 (Berkeley) 02/25/91"; 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> 229200Ssam #include <netinet/in.h> 236442Swnj #include <signal.h> 249200Ssam #include <netdb.h> 2546674Sbostic #include <pwd.h> 2646674Sbostic #include <errno.h> 2746674Sbostic #include <unistd.h> 2846674Sbostic #include <stdio.h> 2946674Sbostic #include <stdlib.h> 3046674Sbostic #include <string.h> 3146674Sbostic #include <paths.h> 326442Swnj 3327902Slepreau /*VARARGS1*/ 3446674Sbostic 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); 5246674Sbostic if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) { 5346674Sbostic (void)fprintf(stderr, 5446674Sbostic "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="; 63*46676Sbostic char path[sizeof(_PATH_DEFPATH) + sizeof("PATH=")] = "PATH="; 646442Swnj char *envinit[] = 65*46676Sbostic {homedir, shell, path, username, 0}; 666442Swnj char **environ; 676442Swnj 686442Swnj struct sockaddr_in asin = { AF_INET }; 696442Swnj 706442Swnj doit(f, fromp) 716442Swnj int f; 726442Swnj struct sockaddr_in *fromp; 736442Swnj { 746442Swnj char cmdbuf[NCARGS+1], *cp, *namep; 756442Swnj char user[16], pass[16]; 766442Swnj struct passwd *pwd; 776442Swnj int s; 7836502Sbostic u_short port; 796442Swnj int pv[2], pid, ready, readfrom, cc; 806442Swnj char buf[BUFSIZ], sig; 816442Swnj int one = 1; 826442Swnj 836442Swnj (void) signal(SIGINT, SIG_DFL); 846442Swnj (void) signal(SIGQUIT, SIG_DFL); 856442Swnj (void) signal(SIGTERM, SIG_DFL); 866442Swnj #ifdef DEBUG 8737989Sbostic { int t = open(_PATH_TTY, 2); 886442Swnj if (t >= 0) { 896442Swnj ioctl(t, TIOCNOTTY, (char *)0); 906442Swnj (void) close(t); 916442Swnj } 926442Swnj } 936442Swnj #endif 946442Swnj dup2(f, 0); 956442Swnj dup2(f, 1); 966442Swnj dup2(f, 2); 976442Swnj (void) alarm(60); 986442Swnj port = 0; 996442Swnj for (;;) { 1006442Swnj char c; 1016442Swnj if (read(f, &c, 1) != 1) 1026442Swnj exit(1); 1036442Swnj if (c == 0) 1046442Swnj break; 1056442Swnj port = port * 10 + c - '0'; 1066442Swnj } 1076442Swnj (void) alarm(0); 1086442Swnj if (port != 0) { 10925294Sbloom s = socket(AF_INET, SOCK_STREAM, 0); 1106442Swnj if (s < 0) 1116442Swnj exit(1); 11246674Sbostic if (bind(s, (struct sockaddr *)&asin, sizeof (asin)) < 0) 1139257Ssam exit(1); 1146442Swnj (void) alarm(60); 11536502Sbostic fromp->sin_port = htons(port); 11646674Sbostic if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0) 1176442Swnj exit(1); 1186442Swnj (void) alarm(0); 1196442Swnj } 1206442Swnj getstr(user, sizeof(user), "username"); 1216442Swnj getstr(pass, sizeof(pass), "password"); 1226442Swnj getstr(cmdbuf, sizeof(cmdbuf), "command"); 1236442Swnj setpwent(); 1246442Swnj pwd = getpwnam(user); 1256442Swnj if (pwd == NULL) { 1266442Swnj error("Login incorrect.\n"); 1276442Swnj exit(1); 1286442Swnj } 1296442Swnj endpwent(); 1306442Swnj if (*pwd->pw_passwd != '\0') { 1316442Swnj namep = crypt(pass, pwd->pw_passwd); 1326442Swnj if (strcmp(namep, pwd->pw_passwd)) { 1336442Swnj error("Password incorrect.\n"); 1346442Swnj exit(1); 1356442Swnj } 1366442Swnj } 1376442Swnj if (chdir(pwd->pw_dir) < 0) { 1386442Swnj error("No remote directory.\n"); 1396442Swnj exit(1); 1406442Swnj } 1416442Swnj (void) write(2, "\0", 1); 1426442Swnj if (port) { 1436442Swnj (void) pipe(pv); 1446442Swnj pid = fork(); 1456442Swnj if (pid == -1) { 1466442Swnj error("Try again.\n"); 1476442Swnj exit(1); 1486442Swnj } 1496442Swnj if (pid) { 1506442Swnj (void) close(0); (void) close(1); (void) close(2); 1516442Swnj (void) close(f); (void) close(pv[1]); 1526442Swnj readfrom = (1<<s) | (1<<pv[0]); 1536442Swnj ioctl(pv[1], FIONBIO, (char *)&one); 1546442Swnj /* should set s nbio! */ 1556442Swnj do { 1566442Swnj ready = readfrom; 15746674Sbostic (void) select(16, (fd_set *)&ready, 15846674Sbostic (fd_set *)NULL, (fd_set *)NULL, 15946674Sbostic (struct timeval *)NULL); 1606442Swnj if (ready & (1<<s)) { 1616442Swnj if (read(s, &sig, 1) <= 0) 1626442Swnj readfrom &= ~(1<<s); 1636442Swnj else 1646442Swnj killpg(pid, sig); 1656442Swnj } 1666442Swnj if (ready & (1<<pv[0])) { 1676442Swnj cc = read(pv[0], buf, sizeof (buf)); 1686442Swnj if (cc <= 0) { 16910193Ssam shutdown(s, 1+1); 1706442Swnj readfrom &= ~(1<<pv[0]); 1716442Swnj } else 1726442Swnj (void) write(s, buf, cc); 1736442Swnj } 1746442Swnj } while (readfrom); 1756442Swnj exit(0); 1766442Swnj } 1776442Swnj setpgrp(0, getpid()); 1786442Swnj (void) close(s); (void)close(pv[0]); 1796442Swnj dup2(pv[1], 2); 1806442Swnj } 1816442Swnj if (*pwd->pw_shell == '\0') 18237288Sbostic pwd->pw_shell = _PATH_BSHELL; 18317184Sralph if (f > 2) 18417184Sralph (void) close(f); 18527902Slepreau (void) setgid((gid_t)pwd->pw_gid); 1869200Ssam initgroups(pwd->pw_name, pwd->pw_gid); 18727902Slepreau (void) setuid((uid_t)pwd->pw_uid); 188*46676Sbostic (void)strcat(path, _PATH_DEFPATH); 1896442Swnj environ = envinit; 1906442Swnj strncat(homedir, pwd->pw_dir, sizeof(homedir)-6); 1916442Swnj strncat(shell, pwd->pw_shell, sizeof(shell)-7); 1926442Swnj strncat(username, pwd->pw_name, sizeof(username)-6); 1936442Swnj cp = rindex(pwd->pw_shell, '/'); 1946442Swnj if (cp) 1956442Swnj cp++; 1966442Swnj else 1976442Swnj cp = pwd->pw_shell; 1986442Swnj execl(pwd->pw_shell, cp, "-c", cmdbuf, 0); 1996442Swnj perror(pwd->pw_shell); 2006442Swnj exit(1); 2016442Swnj } 2026442Swnj 20327902Slepreau /*VARARGS1*/ 2046442Swnj error(fmt, a1, a2, a3) 2056442Swnj char *fmt; 2066442Swnj int a1, a2, a3; 2076442Swnj { 2086442Swnj char buf[BUFSIZ]; 2096442Swnj 2106442Swnj buf[0] = 1; 2116442Swnj (void) sprintf(buf+1, fmt, a1, a2, a3); 2126442Swnj (void) write(2, buf, strlen(buf)); 2136442Swnj } 2146442Swnj 2156442Swnj getstr(buf, cnt, err) 2166442Swnj char *buf; 2176442Swnj int cnt; 2186442Swnj char *err; 2196442Swnj { 2206442Swnj char c; 2216442Swnj 2226442Swnj do { 2236442Swnj if (read(0, &c, 1) != 1) 2246442Swnj exit(1); 2256442Swnj *buf++ = c; 2266442Swnj if (--cnt == 0) { 2276442Swnj error("%s too long\n", err); 2286442Swnj exit(1); 2296442Swnj } 2306442Swnj } while (c != 0); 2316442Swnj } 232