]>
git.saurik.com Git - apple/network_cmds.git/blob - rexecd.tproj/rexecd.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1983, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
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.
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
58 static char copyright
[] =
59 "@(#) Copyright (c) 1983, 1993\n\
60 The Regents of the University of California. All rights reserved.\n";
64 static char sccsid
[] = "@(#)rexecd.c 8.1 (Berkeley) 6/4/93";
67 #include <sys/param.h>
68 #include <sys/ioctl.h>
69 #include <sys/socket.h>
72 #include <netinet/in.h>
88 * remote execute server:
99 struct sockaddr_in from
;
102 fromlen
= sizeof (from
);
103 if (getpeername(0, (struct sockaddr
*)&from
, &fromlen
) < 0) {
104 (void)fprintf(stderr
,
105 "rexecd: getpeername: %s\n", strerror(errno
));
111 char username
[20] = "USER=";
112 char homedir
[64] = "HOME=";
113 char shell
[64] = "SHELL=";
114 char path
[sizeof(_PATH_DEFPATH
) + sizeof("PATH=")] = "PATH=";
116 {homedir
, shell
, path
, username
, 0};
122 struct sockaddr_in asin
= { AF_INET
};
126 struct sockaddr_in
*fromp
;
128 char cmdbuf
[NCARGS
+1], *cp
, *namep
;
129 char user
[16], pass
[16];
133 int pv
[2], pid
, ready
, readfrom
, cc
;
134 char buf
[BUFSIZ
], sig
;
137 (void) signal(SIGINT
, SIG_DFL
);
138 (void) signal(SIGQUIT
, SIG_DFL
);
139 (void) signal(SIGTERM
, SIG_DFL
);
141 { int t
= open(_PATH_TTY
, 2);
143 ioctl(t
, TIOCNOTTY
, (char *)0);
155 if (read(f
, &c
, 1) != 1)
159 port
= port
* 10 + c
- '0';
163 s
= socket(AF_INET
, SOCK_STREAM
, 0);
166 if (bind(s
, (struct sockaddr
*)&asin
, sizeof (asin
)) < 0)
169 fromp
->sin_port
= htons(port
);
170 if (connect(s
, (struct sockaddr
*)fromp
, sizeof (*fromp
)) < 0)
174 getstr(user
, sizeof(user
), "username");
175 getstr(pass
, sizeof(pass
), "password");
176 getstr(cmdbuf
, sizeof(cmdbuf
), "command");
178 pwd
= getpwnam(user
);
180 error("Login incorrect.\n");
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");
191 if (chdir(pwd
->pw_dir
) < 0) {
192 error("No remote directory.\n");
195 (void) write(2, "\0", 1);
200 error("Try again.\n");
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! */
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)
220 if (ready
& (1<<pv
[0])) {
221 cc
= read(pv
[0], buf
, sizeof (buf
));
224 readfrom
&= ~(1<<pv
[0]);
226 (void) write(s
, buf
, cc
);
231 setpgrp(0, getpid());
232 (void) close(s
); (void)close(pv
[0]);
235 if (*pwd
->pw_shell
== '\0')
236 pwd
->pw_shell
= _PATH_BSHELL
;
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
);
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
, '/');
252 execl(pwd
->pw_shell
, cp
, "-c", cmdbuf
, 0);
253 perror(pwd
->pw_shell
);
258 error(fmt
, a1
, a2
, a3
)
265 (void) sprintf(buf
+1, fmt
, a1
, a2
, a3
);
266 (void) write(2, buf
, strlen(buf
));
269 getstr(buf
, cnt
, err
)
277 if (read(0, &c
, 1) != 1)
281 error("%s too long\n", err
);