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