]> git.saurik.com Git - apple/network_cmds.git/blob - rexecd.tproj/rexecd.c
7d3af6c3876e5d57d5344fc00f1c6b6e4f3d8420
[apple/network_cmds.git] / rexecd.tproj / rexecd.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright (c) 1983, 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 */
55
56 #ifndef lint
57 static char copyright[] =
58 "@(#) Copyright (c) 1983, 1993\n\
59 The Regents of the University of California. All rights reserved.\n";
60 #endif /* not lint */
61
62 #ifndef lint
63 static char sccsid[] = "@(#)rexecd.c 8.1 (Berkeley) 6/4/93";
64 #endif /* not lint */
65
66 #include <sys/param.h>
67 #include <sys/ioctl.h>
68 #include <sys/socket.h>
69 #include <sys/time.h>
70
71 #include <netinet/in.h>
72
73 #include <errno.h>
74 #include <netdb.h>
75 #include <paths.h>
76 #include <pwd.h>
77 #include <signal.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <unistd.h>
82
83 /*VARARGS1*/
84 int error();
85
86 /*
87 * remote execute server:
88 * username\0
89 * password\0
90 * command\0
91 * data
92 */
93 /*ARGSUSED*/
94 main(argc, argv)
95 int argc;
96 char **argv;
97 {
98 struct sockaddr_in from;
99 int fromlen;
100
101 fromlen = sizeof (from);
102 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
103 (void)fprintf(stderr,
104 "rexecd: getpeername: %s\n", strerror(errno));
105 exit(1);
106 }
107 doit(0, &from);
108 }
109
110 char username[20] = "USER=";
111 char homedir[64] = "HOME=";
112 char shell[64] = "SHELL=";
113 char path[sizeof(_PATH_DEFPATH) + sizeof("PATH=")] = "PATH=";
114 char *envinit[] =
115 {homedir, shell, path, username, 0};
116 #ifdef __APPLE__
117 extern
118 #endif
119 char **environ;
120
121 struct sockaddr_in asin = { AF_INET };
122
123 doit(f, fromp)
124 int f;
125 struct sockaddr_in *fromp;
126 {
127 char cmdbuf[NCARGS+1], *cp, *namep;
128 char user[16], pass[16];
129 struct passwd *pwd;
130 int s;
131 u_short port;
132 int pv[2], pid, ready, readfrom, cc;
133 char buf[BUFSIZ], sig;
134 int one = 1;
135
136 (void) signal(SIGINT, SIG_DFL);
137 (void) signal(SIGQUIT, SIG_DFL);
138 (void) signal(SIGTERM, SIG_DFL);
139 #ifdef DEBUG
140 { int t = open(_PATH_TTY, 2);
141 if (t >= 0) {
142 ioctl(t, TIOCNOTTY, (char *)0);
143 (void) close(t);
144 }
145 }
146 #endif
147 dup2(f, 0);
148 dup2(f, 1);
149 dup2(f, 2);
150 (void) alarm(60);
151 port = 0;
152 for (;;) {
153 char c;
154 if (read(f, &c, 1) != 1)
155 exit(1);
156 if (c == 0)
157 break;
158 port = port * 10 + c - '0';
159 }
160 (void) alarm(0);
161 if (port != 0) {
162 s = socket(AF_INET, SOCK_STREAM, 0);
163 if (s < 0)
164 exit(1);
165 if (bind(s, (struct sockaddr *)&asin, sizeof (asin)) < 0)
166 exit(1);
167 (void) alarm(60);
168 fromp->sin_port = htons(port);
169 if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0)
170 exit(1);
171 (void) alarm(0);
172 }
173 getstr(user, sizeof(user), "username");
174 getstr(pass, sizeof(pass), "password");
175 getstr(cmdbuf, sizeof(cmdbuf), "command");
176 setpwent();
177 pwd = getpwnam(user);
178 if (pwd == NULL) {
179 error("Login incorrect.\n");
180 exit(1);
181 }
182 endpwent();
183 if (*pwd->pw_passwd != '\0') {
184 namep = crypt(pass, pwd->pw_passwd);
185 if (strcmp(namep, pwd->pw_passwd)) {
186 error("Password incorrect.\n");
187 exit(1);
188 }
189 }
190 if (chdir(pwd->pw_dir) < 0) {
191 error("No remote directory.\n");
192 exit(1);
193 }
194 (void) write(2, "\0", 1);
195 if (port) {
196 (void) pipe(pv);
197 pid = fork();
198 if (pid == -1) {
199 error("Try again.\n");
200 exit(1);
201 }
202 if (pid) {
203 (void) close(0); (void) close(1); (void) close(2);
204 (void) close(f); (void) close(pv[1]);
205 readfrom = (1<<s) | (1<<pv[0]);
206 ioctl(pv[1], FIONBIO, (char *)&one);
207 /* should set s nbio! */
208 do {
209 ready = readfrom;
210 (void) select(16, (fd_set *)&ready,
211 (fd_set *)NULL, (fd_set *)NULL,
212 (struct timeval *)NULL);
213 if (ready & (1<<s)) {
214 if (read(s, &sig, 1) <= 0)
215 readfrom &= ~(1<<s);
216 else
217 killpg(pid, sig);
218 }
219 if (ready & (1<<pv[0])) {
220 cc = read(pv[0], buf, sizeof (buf));
221 if (cc <= 0) {
222 shutdown(s, 1+1);
223 readfrom &= ~(1<<pv[0]);
224 } else
225 (void) write(s, buf, cc);
226 }
227 } while (readfrom);
228 exit(0);
229 }
230 setpgrp(0, getpid());
231 (void) close(s); (void)close(pv[0]);
232 dup2(pv[1], 2);
233 }
234 if (*pwd->pw_shell == '\0')
235 pwd->pw_shell = _PATH_BSHELL;
236 if (f > 2)
237 (void) close(f);
238 (void) setgid((gid_t)pwd->pw_gid);
239 initgroups(pwd->pw_name, pwd->pw_gid);
240 (void) setuid((uid_t)pwd->pw_uid);
241 (void)strcat(path, _PATH_DEFPATH);
242 environ = envinit;
243 strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
244 strncat(shell, pwd->pw_shell, sizeof(shell)-7);
245 strncat(username, pwd->pw_name, sizeof(username)-6);
246 cp = strrchr(pwd->pw_shell, '/');
247 if (cp)
248 cp++;
249 else
250 cp = pwd->pw_shell;
251 execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
252 perror(pwd->pw_shell);
253 exit(1);
254 }
255
256 /*VARARGS1*/
257 error(fmt, a1, a2, a3)
258 char *fmt;
259 int a1, a2, a3;
260 {
261 char buf[BUFSIZ];
262
263 buf[0] = 1;
264 (void) sprintf(buf+1, fmt, a1, a2, a3);
265 (void) write(2, buf, strlen(buf));
266 }
267
268 getstr(buf, cnt, err)
269 char *buf;
270 int cnt;
271 char *err;
272 {
273 char c;
274
275 do {
276 if (read(0, &c, 1) != 1)
277 exit(1);
278 *buf++ = c;
279 if (--cnt == 0) {
280 error("%s too long\n", err);
281 exit(1);
282 }
283 } while (c != 0);
284 }