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.11 (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 *envinit[] = 64 {homedir, shell, _PATH_DEFPATH, username, 0}; 65 char **environ; 66 67 struct sockaddr_in asin = { AF_INET }; 68 69 doit(f, fromp) 70 int f; 71 struct sockaddr_in *fromp; 72 { 73 char cmdbuf[NCARGS+1], *cp, *namep; 74 char user[16], pass[16]; 75 struct passwd *pwd; 76 int s; 77 u_short port; 78 int pv[2], pid, ready, readfrom, cc; 79 char buf[BUFSIZ], sig; 80 int one = 1; 81 82 (void) signal(SIGINT, SIG_DFL); 83 (void) signal(SIGQUIT, SIG_DFL); 84 (void) signal(SIGTERM, SIG_DFL); 85 #ifdef DEBUG 86 { int t = open(_PATH_TTY, 2); 87 if (t >= 0) { 88 ioctl(t, TIOCNOTTY, (char *)0); 89 (void) close(t); 90 } 91 } 92 #endif 93 dup2(f, 0); 94 dup2(f, 1); 95 dup2(f, 2); 96 (void) alarm(60); 97 port = 0; 98 for (;;) { 99 char c; 100 if (read(f, &c, 1) != 1) 101 exit(1); 102 if (c == 0) 103 break; 104 port = port * 10 + c - '0'; 105 } 106 (void) alarm(0); 107 if (port != 0) { 108 s = socket(AF_INET, SOCK_STREAM, 0); 109 if (s < 0) 110 exit(1); 111 if (bind(s, (struct sockaddr *)&asin, sizeof (asin)) < 0) 112 exit(1); 113 (void) alarm(60); 114 fromp->sin_port = htons(port); 115 if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0) 116 exit(1); 117 (void) alarm(0); 118 } 119 getstr(user, sizeof(user), "username"); 120 getstr(pass, sizeof(pass), "password"); 121 getstr(cmdbuf, sizeof(cmdbuf), "command"); 122 setpwent(); 123 pwd = getpwnam(user); 124 if (pwd == NULL) { 125 error("Login incorrect.\n"); 126 exit(1); 127 } 128 endpwent(); 129 if (*pwd->pw_passwd != '\0') { 130 namep = crypt(pass, pwd->pw_passwd); 131 if (strcmp(namep, pwd->pw_passwd)) { 132 error("Password incorrect.\n"); 133 exit(1); 134 } 135 } 136 if (chdir(pwd->pw_dir) < 0) { 137 error("No remote directory.\n"); 138 exit(1); 139 } 140 (void) write(2, "\0", 1); 141 if (port) { 142 (void) pipe(pv); 143 pid = fork(); 144 if (pid == -1) { 145 error("Try again.\n"); 146 exit(1); 147 } 148 if (pid) { 149 (void) close(0); (void) close(1); (void) close(2); 150 (void) close(f); (void) close(pv[1]); 151 readfrom = (1<<s) | (1<<pv[0]); 152 ioctl(pv[1], FIONBIO, (char *)&one); 153 /* should set s nbio! */ 154 do { 155 ready = readfrom; 156 (void) select(16, (fd_set *)&ready, 157 (fd_set *)NULL, (fd_set *)NULL, 158 (struct timeval *)NULL); 159 if (ready & (1<<s)) { 160 if (read(s, &sig, 1) <= 0) 161 readfrom &= ~(1<<s); 162 else 163 killpg(pid, sig); 164 } 165 if (ready & (1<<pv[0])) { 166 cc = read(pv[0], buf, sizeof (buf)); 167 if (cc <= 0) { 168 shutdown(s, 1+1); 169 readfrom &= ~(1<<pv[0]); 170 } else 171 (void) write(s, buf, cc); 172 } 173 } while (readfrom); 174 exit(0); 175 } 176 setpgrp(0, getpid()); 177 (void) close(s); (void)close(pv[0]); 178 dup2(pv[1], 2); 179 } 180 if (*pwd->pw_shell == '\0') 181 pwd->pw_shell = _PATH_BSHELL; 182 if (f > 2) 183 (void) close(f); 184 (void) setgid((gid_t)pwd->pw_gid); 185 initgroups(pwd->pw_name, pwd->pw_gid); 186 (void) setuid((uid_t)pwd->pw_uid); 187 environ = envinit; 188 strncat(homedir, pwd->pw_dir, sizeof(homedir)-6); 189 strncat(shell, pwd->pw_shell, sizeof(shell)-7); 190 strncat(username, pwd->pw_name, sizeof(username)-6); 191 cp = rindex(pwd->pw_shell, '/'); 192 if (cp) 193 cp++; 194 else 195 cp = pwd->pw_shell; 196 execl(pwd->pw_shell, cp, "-c", cmdbuf, 0); 197 perror(pwd->pw_shell); 198 exit(1); 199 } 200 201 /*VARARGS1*/ 202 error(fmt, a1, a2, a3) 203 char *fmt; 204 int a1, a2, a3; 205 { 206 char buf[BUFSIZ]; 207 208 buf[0] = 1; 209 (void) sprintf(buf+1, fmt, a1, a2, a3); 210 (void) write(2, buf, strlen(buf)); 211 } 212 213 getstr(buf, cnt, err) 214 char *buf; 215 int cnt; 216 char *err; 217 { 218 char c; 219 220 do { 221 if (read(0, &c, 1) != 1) 222 exit(1); 223 *buf++ = c; 224 if (--cnt == 0) { 225 error("%s too long\n", err); 226 exit(1); 227 } 228 } while (c != 0); 229 } 230