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