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