1 #ifndef lint 2 static char sccsid[] = "@(#)rexecd.c 4.4 82/11/15"; 3 #endif 4 5 #include <sys/ioctl.h> 6 #include <sys/param.h> 7 #include <sys/socket.h> 8 9 #include <netinet/in.h> 10 11 #include <stdio.h> 12 #include <errno.h> 13 #include <pwd.h> 14 #include <wait.h> 15 #include <signal.h> 16 #include <netdb.h> 17 18 extern errno; 19 struct sockaddr_in sin = { AF_INET }; 20 struct passwd *getpwnam(); 21 char *crypt(), *rindex(), *sprintf(); 22 /* VARARGS 1 */ 23 int error(); 24 /* 25 * remote execute server: 26 * username\0 27 * password\0 28 * command\0 29 * data 30 */ 31 main(argc, argv) 32 int argc; 33 char **argv; 34 { 35 union wait status; 36 int f; 37 struct sockaddr_in from; 38 struct servent *sp; 39 40 sp = getservbyname("exec", "tcp"); 41 if (sp == 0) { 42 fprintf(stderr, "tcp/exec: unknown service\n"); 43 exit(1); 44 } 45 sin.sin_port = htons((u_short)sp->s_port); 46 #ifndef DEBUG 47 if (fork()) 48 exit(0); 49 for (f = 0; f < 10; f++) 50 (void) close(f); 51 (void) open("/", 0); 52 (void) dup2(0, 1); 53 (void) dup2(0, 2); 54 { int t = open("/dev/tty", 2); 55 if (t >= 0) { 56 ioctl(t, TIOCNOTTY, (char *)0); 57 (void) close(t); 58 } 59 } 60 #endif 61 argc--, argv++; 62 f = socket(AF_INET, SOCK_STREAM, 0, 0); 63 if (f < 0) { 64 perror("rexecd: socket"); 65 exit(1); 66 } 67 if (bind(f, &sin, sizeof (sin), 0) < 0) { 68 perror("rexecd: bind:"); 69 exit(1); 70 } 71 listen(f, 10); 72 for (;;) { 73 int s, len = sizeof (from); 74 75 s = accept(f, &from, &len, 0); 76 if (s < 0) { 77 perror("rexecd: accept"); 78 sleep(1); 79 continue; 80 } 81 if (fork() == 0) 82 doit(s, &from); 83 (void) close(s); 84 while (wait3(status, WNOHANG, 0) > 0) 85 continue; 86 } 87 } 88 89 char username[20] = "USER="; 90 char homedir[64] = "HOME="; 91 char shell[64] = "SHELL="; 92 char *envinit[] = 93 {homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", username, 0}; 94 char **environ; 95 96 struct sockaddr_in asin = { AF_INET }; 97 98 doit(f, fromp) 99 int f; 100 struct sockaddr_in *fromp; 101 { 102 char cmdbuf[NCARGS+1], *cp, *namep; 103 char user[16], pass[16]; 104 struct passwd *pwd; 105 int s; 106 short port; 107 int pv[2], pid, ready, readfrom, cc; 108 char buf[BUFSIZ], sig; 109 int one = 1; 110 111 (void) signal(SIGINT, SIG_DFL); 112 (void) signal(SIGQUIT, SIG_DFL); 113 (void) signal(SIGTERM, SIG_DFL); 114 #ifdef DEBUG 115 { int t = open("/dev/tty", 2); 116 if (t >= 0) { 117 ioctl(t, TIOCNOTTY, (char *)0); 118 (void) close(t); 119 } 120 } 121 #endif 122 dup2(f, 0); 123 dup2(f, 1); 124 dup2(f, 2); 125 (void) alarm(60); 126 port = 0; 127 for (;;) { 128 char c; 129 if (read(f, &c, 1) != 1) 130 exit(1); 131 if (c == 0) 132 break; 133 port = port * 10 + c - '0'; 134 } 135 (void) alarm(0); 136 if (port != 0) { 137 s = socket(AF_INET, SOCK_STREAM, 0, 0); 138 if (s < 0) 139 exit(1); 140 if (bind(s, &asin, sizeof (asin), 0) < 0) 141 exit(1); 142 (void) alarm(60); 143 fromp->sin_port = htons((u_short)port); 144 if (connect(s, fromp, sizeof (*fromp), 0) < 0) 145 exit(1); 146 (void) alarm(0); 147 } 148 getstr(user, sizeof(user), "username"); 149 getstr(pass, sizeof(pass), "password"); 150 getstr(cmdbuf, sizeof(cmdbuf), "command"); 151 setpwent(); 152 pwd = getpwnam(user); 153 if (pwd == NULL) { 154 error("Login incorrect.\n"); 155 exit(1); 156 } 157 endpwent(); 158 if (*pwd->pw_passwd != '\0') { 159 namep = crypt(pass, pwd->pw_passwd); 160 if (strcmp(namep, pwd->pw_passwd)) { 161 error("Password incorrect.\n"); 162 exit(1); 163 } 164 } 165 if (chdir(pwd->pw_dir) < 0) { 166 error("No remote directory.\n"); 167 exit(1); 168 } 169 (void) write(2, "\0", 1); 170 if (port) { 171 (void) pipe(pv); 172 pid = fork(); 173 if (pid == -1) { 174 error("Try again.\n"); 175 exit(1); 176 } 177 if (pid) { 178 (void) close(0); (void) close(1); (void) close(2); 179 (void) close(f); (void) close(pv[1]); 180 readfrom = (1<<s) | (1<<pv[0]); 181 ioctl(pv[1], FIONBIO, (char *)&one); 182 /* should set s nbio! */ 183 do { 184 ready = readfrom; 185 (void) select(16, &ready, 0, 0, 0); 186 if (ready & (1<<s)) { 187 if (read(s, &sig, 1) <= 0) 188 readfrom &= ~(1<<s); 189 else 190 killpg(pid, sig); 191 } 192 if (ready & (1<<pv[0])) { 193 cc = read(pv[0], buf, sizeof (buf)); 194 if (cc <= 0) { 195 int done = 1+1; 196 ioctl(s, SIOCDONE, (char *)&done); 197 readfrom &= ~(1<<pv[0]); 198 } else 199 (void) write(s, buf, cc); 200 } 201 } while (readfrom); 202 exit(0); 203 } 204 setpgrp(0, getpid()); 205 (void) close(s); (void)close(pv[0]); 206 dup2(pv[1], 2); 207 } 208 if (*pwd->pw_shell == '\0') 209 pwd->pw_shell = "/bin/sh"; 210 (void) close(f); 211 initgroups(pwd->pw_name, pwd->pw_gid); 212 (void) setuid(pwd->pw_uid); 213 (void) setgid(pwd->pw_gid); 214 environ = envinit; 215 strncat(homedir, pwd->pw_dir, sizeof(homedir)-6); 216 strncat(shell, pwd->pw_shell, sizeof(shell)-7); 217 strncat(username, pwd->pw_name, sizeof(username)-6); 218 cp = rindex(pwd->pw_shell, '/'); 219 if (cp) 220 cp++; 221 else 222 cp = pwd->pw_shell; 223 execl(pwd->pw_shell, cp, "-c", cmdbuf, 0); 224 perror(pwd->pw_shell); 225 exit(1); 226 } 227 228 /* VARARGS 1 */ 229 error(fmt, a1, a2, a3) 230 char *fmt; 231 int a1, a2, a3; 232 { 233 char buf[BUFSIZ]; 234 235 buf[0] = 1; 236 (void) sprintf(buf+1, fmt, a1, a2, a3); 237 (void) write(2, buf, strlen(buf)); 238 } 239 240 getstr(buf, cnt, err) 241 char *buf; 242 int cnt; 243 char *err; 244 { 245 char c; 246 247 do { 248 if (read(0, &c, 1) != 1) 249 exit(1); 250 *buf++ = c; 251 if (--cnt == 0) { 252 error("%s too long\n", err); 253 exit(1); 254 } 255 } while (c != 0); 256 } 257