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