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