Commit | Line | Data |
---|---|---|
c018628f | 1 | /* |
df119935 KB |
2 | * Copyright (c) 1983 The Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
6 | * provided that the above copyright notice and this paragraph are | |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
c018628f DF |
16 | */ |
17 | ||
18 | #ifndef lint | |
19 | char copyright[] = | |
df119935 | 20 | "@(#) Copyright (c) 1983 The Regents of the University of California.\n\ |
c018628f | 21 | All rights reserved.\n"; |
df119935 | 22 | #endif /* not lint */ |
c018628f | 23 | |
f0d0adca | 24 | #ifndef lint |
7abf8d65 | 25 | static char sccsid[] = "@(#)rexecd.c 5.9 (Berkeley) %G%"; |
df119935 | 26 | #endif /* not lint */ |
f0d0adca | 27 | |
f0d0adca BJ |
28 | #include <sys/ioctl.h> |
29 | #include <sys/param.h> | |
30 | #include <sys/socket.h> | |
2478cefb | 31 | #include <sys/time.h> |
94a2d2a7 SL |
32 | |
33 | #include <netinet/in.h> | |
34 | ||
35 | #include <stdio.h> | |
f0d0adca BJ |
36 | #include <errno.h> |
37 | #include <pwd.h> | |
f0d0adca | 38 | #include <signal.h> |
94a2d2a7 | 39 | #include <netdb.h> |
d20be4ab | 40 | #include "pathnames.h" |
f0d0adca | 41 | |
d20be4ab | 42 | extern int errno; |
f0d0adca | 43 | struct passwd *getpwnam(); |
9bd38ba8 | 44 | char *crypt(), *rindex(), *strncat(); |
2478cefb | 45 | /*VARARGS1*/ |
f0d0adca | 46 | int error(); |
2478cefb | 47 | |
f0d0adca BJ |
48 | /* |
49 | * remote execute server: | |
50 | * username\0 | |
51 | * password\0 | |
52 | * command\0 | |
53 | * data | |
54 | */ | |
2478cefb | 55 | /*ARGSUSED*/ |
f0d0adca BJ |
56 | main(argc, argv) |
57 | int argc; | |
58 | char **argv; | |
59 | { | |
f0d0adca | 60 | struct sockaddr_in from; |
bb933cc2 | 61 | int fromlen; |
f0d0adca | 62 | |
bb933cc2 MK |
63 | fromlen = sizeof (from); |
64 | if (getpeername(0, &from, &fromlen) < 0) { | |
65 | fprintf(stderr, "%s: ", argv[0]); | |
66 | perror("getpeername"); | |
94a2d2a7 SL |
67 | exit(1); |
68 | } | |
bb933cc2 | 69 | doit(0, &from); |
f0d0adca BJ |
70 | } |
71 | ||
72 | char username[20] = "USER="; | |
73 | char homedir[64] = "HOME="; | |
74 | char shell[64] = "SHELL="; | |
75 | char *envinit[] = | |
d20be4ab | 76 | {homedir, shell, _PATH_DEFPATH, username, 0}; |
f0d0adca BJ |
77 | char **environ; |
78 | ||
79 | struct sockaddr_in asin = { AF_INET }; | |
80 | ||
81 | doit(f, fromp) | |
82 | int f; | |
83 | struct sockaddr_in *fromp; | |
84 | { | |
85 | char cmdbuf[NCARGS+1], *cp, *namep; | |
86 | char user[16], pass[16]; | |
87 | struct passwd *pwd; | |
88 | int s; | |
4ce69e43 | 89 | u_short port; |
f0d0adca BJ |
90 | int pv[2], pid, ready, readfrom, cc; |
91 | char buf[BUFSIZ], sig; | |
92 | int one = 1; | |
93 | ||
94 | (void) signal(SIGINT, SIG_DFL); | |
95 | (void) signal(SIGQUIT, SIG_DFL); | |
96 | (void) signal(SIGTERM, SIG_DFL); | |
97 | #ifdef DEBUG | |
7abf8d65 | 98 | { int t = open(_PATH_TTY, 2); |
f0d0adca BJ |
99 | if (t >= 0) { |
100 | ioctl(t, TIOCNOTTY, (char *)0); | |
101 | (void) close(t); | |
102 | } | |
103 | } | |
104 | #endif | |
105 | dup2(f, 0); | |
106 | dup2(f, 1); | |
107 | dup2(f, 2); | |
f0d0adca BJ |
108 | (void) alarm(60); |
109 | port = 0; | |
110 | for (;;) { | |
111 | char c; | |
112 | if (read(f, &c, 1) != 1) | |
113 | exit(1); | |
114 | if (c == 0) | |
115 | break; | |
116 | port = port * 10 + c - '0'; | |
117 | } | |
118 | (void) alarm(0); | |
119 | if (port != 0) { | |
eebf51cf | 120 | s = socket(AF_INET, SOCK_STREAM, 0); |
f0d0adca BJ |
121 | if (s < 0) |
122 | exit(1); | |
eebf51cf | 123 | if (bind(s, &asin, sizeof (asin)) < 0) |
36a969c5 | 124 | exit(1); |
f0d0adca | 125 | (void) alarm(60); |
4ce69e43 | 126 | fromp->sin_port = htons(port); |
2478cefb | 127 | if (connect(s, fromp, sizeof (*fromp)) < 0) |
f0d0adca BJ |
128 | exit(1); |
129 | (void) alarm(0); | |
130 | } | |
131 | getstr(user, sizeof(user), "username"); | |
132 | getstr(pass, sizeof(pass), "password"); | |
133 | getstr(cmdbuf, sizeof(cmdbuf), "command"); | |
134 | setpwent(); | |
135 | pwd = getpwnam(user); | |
136 | if (pwd == NULL) { | |
137 | error("Login incorrect.\n"); | |
138 | exit(1); | |
139 | } | |
140 | endpwent(); | |
141 | if (*pwd->pw_passwd != '\0') { | |
142 | namep = crypt(pass, pwd->pw_passwd); | |
143 | if (strcmp(namep, pwd->pw_passwd)) { | |
144 | error("Password incorrect.\n"); | |
145 | exit(1); | |
146 | } | |
147 | } | |
148 | if (chdir(pwd->pw_dir) < 0) { | |
149 | error("No remote directory.\n"); | |
150 | exit(1); | |
151 | } | |
152 | (void) write(2, "\0", 1); | |
153 | if (port) { | |
154 | (void) pipe(pv); | |
155 | pid = fork(); | |
156 | if (pid == -1) { | |
157 | error("Try again.\n"); | |
158 | exit(1); | |
159 | } | |
160 | if (pid) { | |
161 | (void) close(0); (void) close(1); (void) close(2); | |
162 | (void) close(f); (void) close(pv[1]); | |
163 | readfrom = (1<<s) | (1<<pv[0]); | |
164 | ioctl(pv[1], FIONBIO, (char *)&one); | |
165 | /* should set s nbio! */ | |
166 | do { | |
167 | ready = readfrom; | |
2478cefb JL |
168 | (void) select(16, &ready, (fd_set *)0, |
169 | (fd_set *)0, (struct timeval *)0); | |
f0d0adca BJ |
170 | if (ready & (1<<s)) { |
171 | if (read(s, &sig, 1) <= 0) | |
172 | readfrom &= ~(1<<s); | |
173 | else | |
174 | killpg(pid, sig); | |
175 | } | |
176 | if (ready & (1<<pv[0])) { | |
177 | cc = read(pv[0], buf, sizeof (buf)); | |
178 | if (cc <= 0) { | |
ff24c640 | 179 | shutdown(s, 1+1); |
f0d0adca BJ |
180 | readfrom &= ~(1<<pv[0]); |
181 | } else | |
182 | (void) write(s, buf, cc); | |
183 | } | |
184 | } while (readfrom); | |
185 | exit(0); | |
186 | } | |
187 | setpgrp(0, getpid()); | |
188 | (void) close(s); (void)close(pv[0]); | |
189 | dup2(pv[1], 2); | |
190 | } | |
191 | if (*pwd->pw_shell == '\0') | |
d20be4ab | 192 | pwd->pw_shell = _PATH_BSHELL; |
1d799a50 RC |
193 | if (f > 2) |
194 | (void) close(f); | |
2478cefb | 195 | (void) setgid((gid_t)pwd->pw_gid); |
94a2d2a7 | 196 | initgroups(pwd->pw_name, pwd->pw_gid); |
2478cefb | 197 | (void) setuid((uid_t)pwd->pw_uid); |
f0d0adca BJ |
198 | environ = envinit; |
199 | strncat(homedir, pwd->pw_dir, sizeof(homedir)-6); | |
200 | strncat(shell, pwd->pw_shell, sizeof(shell)-7); | |
201 | strncat(username, pwd->pw_name, sizeof(username)-6); | |
202 | cp = rindex(pwd->pw_shell, '/'); | |
203 | if (cp) | |
204 | cp++; | |
205 | else | |
206 | cp = pwd->pw_shell; | |
207 | execl(pwd->pw_shell, cp, "-c", cmdbuf, 0); | |
208 | perror(pwd->pw_shell); | |
209 | exit(1); | |
210 | } | |
211 | ||
2478cefb | 212 | /*VARARGS1*/ |
f0d0adca BJ |
213 | error(fmt, a1, a2, a3) |
214 | char *fmt; | |
215 | int a1, a2, a3; | |
216 | { | |
217 | char buf[BUFSIZ]; | |
218 | ||
219 | buf[0] = 1; | |
220 | (void) sprintf(buf+1, fmt, a1, a2, a3); | |
221 | (void) write(2, buf, strlen(buf)); | |
222 | } | |
223 | ||
224 | getstr(buf, cnt, err) | |
225 | char *buf; | |
226 | int cnt; | |
227 | char *err; | |
228 | { | |
229 | char c; | |
230 | ||
231 | do { | |
232 | if (read(0, &c, 1) != 1) | |
233 | exit(1); | |
234 | *buf++ = c; | |
235 | if (--cnt == 0) { | |
236 | error("%s too long\n", err); | |
237 | exit(1); | |
238 | } | |
239 | } while (c != 0); | |
240 | } |