]>
git.saurik.com Git - apple/network_cmds.git/blob - rexecd.tproj/rexecd.c
7d3af6c3876e5d57d5344fc00f1c6b6e4f3d8420
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1983, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
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.
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
57 static char copyright
[] =
58 "@(#) Copyright (c) 1983, 1993\n\
59 The Regents of the University of California. All rights reserved.\n";
63 static char sccsid
[] = "@(#)rexecd.c 8.1 (Berkeley) 6/4/93";
66 #include <sys/param.h>
67 #include <sys/ioctl.h>
68 #include <sys/socket.h>
71 #include <netinet/in.h>
87 * remote execute server:
98 struct sockaddr_in from
;
101 fromlen
= sizeof (from
);
102 if (getpeername(0, (struct sockaddr
*)&from
, &fromlen
) < 0) {
103 (void)fprintf(stderr
,
104 "rexecd: getpeername: %s\n", strerror(errno
));
110 char username
[20] = "USER=";
111 char homedir
[64] = "HOME=";
112 char shell
[64] = "SHELL=";
113 char path
[sizeof(_PATH_DEFPATH
) + sizeof("PATH=")] = "PATH=";
115 {homedir
, shell
, path
, username
, 0};
121 struct sockaddr_in asin
= { AF_INET
};
125 struct sockaddr_in
*fromp
;
127 char cmdbuf
[NCARGS
+1], *cp
, *namep
;
128 char user
[16], pass
[16];
132 int pv
[2], pid
, ready
, readfrom
, cc
;
133 char buf
[BUFSIZ
], sig
;
136 (void) signal(SIGINT
, SIG_DFL
);
137 (void) signal(SIGQUIT
, SIG_DFL
);
138 (void) signal(SIGTERM
, SIG_DFL
);
140 { int t
= open(_PATH_TTY
, 2);
142 ioctl(t
, TIOCNOTTY
, (char *)0);
154 if (read(f
, &c
, 1) != 1)
158 port
= port
* 10 + c
- '0';
162 s
= socket(AF_INET
, SOCK_STREAM
, 0);
165 if (bind(s
, (struct sockaddr
*)&asin
, sizeof (asin
)) < 0)
168 fromp
->sin_port
= htons(port
);
169 if (connect(s
, (struct sockaddr
*)fromp
, sizeof (*fromp
)) < 0)
173 getstr(user
, sizeof(user
), "username");
174 getstr(pass
, sizeof(pass
), "password");
175 getstr(cmdbuf
, sizeof(cmdbuf
), "command");
177 pwd
= getpwnam(user
);
179 error("Login incorrect.\n");
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");
190 if (chdir(pwd
->pw_dir
) < 0) {
191 error("No remote directory.\n");
194 (void) write(2, "\0", 1);
199 error("Try again.\n");
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! */
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)
219 if (ready
& (1<<pv
[0])) {
220 cc
= read(pv
[0], buf
, sizeof (buf
));
223 readfrom
&= ~(1<<pv
[0]);
225 (void) write(s
, buf
, cc
);
230 setpgrp(0, getpid());
231 (void) close(s
); (void)close(pv
[0]);
234 if (*pwd
->pw_shell
== '\0')
235 pwd
->pw_shell
= _PATH_BSHELL
;
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
);
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
, '/');
251 execl(pwd
->pw_shell
, cp
, "-c", cmdbuf
, 0);
252 perror(pwd
->pw_shell
);
257 error(fmt
, a1
, a2
, a3
)
264 (void) sprintf(buf
+1, fmt
, a1
, a2
, a3
);
265 (void) write(2, buf
, strlen(buf
));
268 getstr(buf
, cnt
, err
)
276 if (read(0, &c
, 1) != 1)
280 error("%s too long\n", err
);