121163Sdist /* 235734Sbostic * Copyright (c) 1983 The Regents of the University of California. 335734Sbostic * All rights reserved. 435734Sbostic * 5*42670Sbostic * %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*42670Sbostic static char sccsid[] = "@(#)rexecd.c 5.10 (Berkeley) 06/01/90"; 1635734Sbostic #endif /* not lint */ 1721163Sdist 186442Swnj #include <sys/ioctl.h> 196442Swnj #include <sys/param.h> 206442Swnj #include <sys/socket.h> 2127902Slepreau #include <sys/time.h> 229200Ssam 239200Ssam #include <netinet/in.h> 249200Ssam 259200Ssam #include <stdio.h> 266442Swnj #include <errno.h> 276442Swnj #include <pwd.h> 286442Swnj #include <signal.h> 299200Ssam #include <netdb.h> 3037288Sbostic #include "pathnames.h" 316442Swnj 3237288Sbostic extern int errno; 336442Swnj struct passwd *getpwnam(); 3432446Sbostic char *crypt(), *rindex(), *strncat(); 3527902Slepreau /*VARARGS1*/ 366442Swnj int error(); 3727902Slepreau 386442Swnj /* 396442Swnj * remote execute server: 406442Swnj * username\0 416442Swnj * password\0 426442Swnj * command\0 436442Swnj * data 446442Swnj */ 4527902Slepreau /*ARGSUSED*/ 466442Swnj main(argc, argv) 476442Swnj int argc; 486442Swnj char **argv; 496442Swnj { 506442Swnj struct sockaddr_in from; 5116368Skarels int fromlen; 526442Swnj 5316368Skarels fromlen = sizeof (from); 5416368Skarels if (getpeername(0, &from, &fromlen) < 0) { 5516368Skarels fprintf(stderr, "%s: ", argv[0]); 5616368Skarels perror("getpeername"); 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="; 656442Swnj char *envinit[] = 6637288Sbostic {homedir, shell, _PATH_DEFPATH, username, 0}; 676442Swnj char **environ; 686442Swnj 696442Swnj struct sockaddr_in asin = { AF_INET }; 706442Swnj 716442Swnj doit(f, fromp) 726442Swnj int f; 736442Swnj struct sockaddr_in *fromp; 746442Swnj { 756442Swnj char cmdbuf[NCARGS+1], *cp, *namep; 766442Swnj char user[16], pass[16]; 776442Swnj struct passwd *pwd; 786442Swnj int s; 7936502Sbostic u_short port; 806442Swnj int pv[2], pid, ready, readfrom, cc; 816442Swnj char buf[BUFSIZ], sig; 826442Swnj int one = 1; 836442Swnj 846442Swnj (void) signal(SIGINT, SIG_DFL); 856442Swnj (void) signal(SIGQUIT, SIG_DFL); 866442Swnj (void) signal(SIGTERM, SIG_DFL); 876442Swnj #ifdef DEBUG 8837989Sbostic { int t = open(_PATH_TTY, 2); 896442Swnj if (t >= 0) { 906442Swnj ioctl(t, TIOCNOTTY, (char *)0); 916442Swnj (void) close(t); 926442Swnj } 936442Swnj } 946442Swnj #endif 956442Swnj dup2(f, 0); 966442Swnj dup2(f, 1); 976442Swnj dup2(f, 2); 986442Swnj (void) alarm(60); 996442Swnj port = 0; 1006442Swnj for (;;) { 1016442Swnj char c; 1026442Swnj if (read(f, &c, 1) != 1) 1036442Swnj exit(1); 1046442Swnj if (c == 0) 1056442Swnj break; 1066442Swnj port = port * 10 + c - '0'; 1076442Swnj } 1086442Swnj (void) alarm(0); 1096442Swnj if (port != 0) { 11025294Sbloom s = socket(AF_INET, SOCK_STREAM, 0); 1116442Swnj if (s < 0) 1126442Swnj exit(1); 11325294Sbloom if (bind(s, &asin, sizeof (asin)) < 0) 1149257Ssam exit(1); 1156442Swnj (void) alarm(60); 11636502Sbostic fromp->sin_port = htons(port); 11727902Slepreau if (connect(s, fromp, sizeof (*fromp)) < 0) 1186442Swnj exit(1); 1196442Swnj (void) alarm(0); 1206442Swnj } 1216442Swnj getstr(user, sizeof(user), "username"); 1226442Swnj getstr(pass, sizeof(pass), "password"); 1236442Swnj getstr(cmdbuf, sizeof(cmdbuf), "command"); 1246442Swnj setpwent(); 1256442Swnj pwd = getpwnam(user); 1266442Swnj if (pwd == NULL) { 1276442Swnj error("Login incorrect.\n"); 1286442Swnj exit(1); 1296442Swnj } 1306442Swnj endpwent(); 1316442Swnj if (*pwd->pw_passwd != '\0') { 1326442Swnj namep = crypt(pass, pwd->pw_passwd); 1336442Swnj if (strcmp(namep, pwd->pw_passwd)) { 1346442Swnj error("Password incorrect.\n"); 1356442Swnj exit(1); 1366442Swnj } 1376442Swnj } 1386442Swnj if (chdir(pwd->pw_dir) < 0) { 1396442Swnj error("No remote directory.\n"); 1406442Swnj exit(1); 1416442Swnj } 1426442Swnj (void) write(2, "\0", 1); 1436442Swnj if (port) { 1446442Swnj (void) pipe(pv); 1456442Swnj pid = fork(); 1466442Swnj if (pid == -1) { 1476442Swnj error("Try again.\n"); 1486442Swnj exit(1); 1496442Swnj } 1506442Swnj if (pid) { 1516442Swnj (void) close(0); (void) close(1); (void) close(2); 1526442Swnj (void) close(f); (void) close(pv[1]); 1536442Swnj readfrom = (1<<s) | (1<<pv[0]); 1546442Swnj ioctl(pv[1], FIONBIO, (char *)&one); 1556442Swnj /* should set s nbio! */ 1566442Swnj do { 1576442Swnj ready = readfrom; 15827902Slepreau (void) select(16, &ready, (fd_set *)0, 15927902Slepreau (fd_set *)0, (struct timeval *)0); 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); 1886442Swnj environ = envinit; 1896442Swnj strncat(homedir, pwd->pw_dir, sizeof(homedir)-6); 1906442Swnj strncat(shell, pwd->pw_shell, sizeof(shell)-7); 1916442Swnj strncat(username, pwd->pw_name, sizeof(username)-6); 1926442Swnj cp = rindex(pwd->pw_shell, '/'); 1936442Swnj if (cp) 1946442Swnj cp++; 1956442Swnj else 1966442Swnj cp = pwd->pw_shell; 1976442Swnj execl(pwd->pw_shell, cp, "-c", cmdbuf, 0); 1986442Swnj perror(pwd->pw_shell); 1996442Swnj exit(1); 2006442Swnj } 2016442Swnj 20227902Slepreau /*VARARGS1*/ 2036442Swnj error(fmt, a1, a2, a3) 2046442Swnj char *fmt; 2056442Swnj int a1, a2, a3; 2066442Swnj { 2076442Swnj char buf[BUFSIZ]; 2086442Swnj 2096442Swnj buf[0] = 1; 2106442Swnj (void) sprintf(buf+1, fmt, a1, a2, a3); 2116442Swnj (void) write(2, buf, strlen(buf)); 2126442Swnj } 2136442Swnj 2146442Swnj getstr(buf, cnt, err) 2156442Swnj char *buf; 2166442Swnj int cnt; 2176442Swnj char *err; 2186442Swnj { 2196442Swnj char c; 2206442Swnj 2216442Swnj do { 2226442Swnj if (read(0, &c, 1) != 1) 2236442Swnj exit(1); 2246442Swnj *buf++ = c; 2256442Swnj if (--cnt == 0) { 2266442Swnj error("%s too long\n", err); 2276442Swnj exit(1); 2286442Swnj } 2296442Swnj } while (c != 0); 2306442Swnj } 231