]>
git.saurik.com Git - apple/network_cmds.git/blob - rsh.tproj/rsh.c
2 * Copyright (c) 1983, 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2002 Networks Associates Technology, Inc.
7 * Portions of this software were developed for the FreeBSD Project by
8 * ThinkSec AS and NAI Labs, the Security Research Division of Network
9 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
10 * ("CBOSS"), as part of the DARPA CHATS research program.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 static const char copyright
[] =
43 "@(#) Copyright (c) 1983, 1990, 1993, 1994\n\
44 The Regents of the University of California. All rights reserved.\n";
48 static const char sccsid
[] = "From: @(#)rsh.c 8.3 (Berkeley) 4/6/94";
51 #include <sys/cdefs.h>
53 #include <sys/param.h>
54 #include <sys/signal.h>
55 #include <sys/socket.h>
56 #include <sys/ioctl.h>
60 #include <netinet/in.h>
79 int family
= PF_UNSPEC
;
80 char rlogin
[] = "rlogin";
82 void connect_timeout(int);
83 char *copyargs(char * const *);
85 void talk(int, long, pid_t
, int, int);
89 main(int argc
, char *argv
[])
91 struct passwd
const *pw
;
92 struct servent
const *sp
;
94 int argoff
, asrsh
, ch
, dflag
, nflag
, one
, rem
;
97 char *args
, *host
, *p
, *user
;
100 argoff
= asrsh
= dflag
= nflag
= 0;
104 /* if called as something other than "rsh", use it as the host name */
105 if ((p
= strrchr(argv
[0], '/')))
109 if (strcmp(p
, "rsh"))
114 /* handle "rsh host flags" */
115 if (!host
&& argc
> 2 && argv
[1][0] != '-') {
120 #define OPTIONS "468KLde:l:nt:w"
121 while ((ch
= getopt(argc
- argoff
, argv
+ argoff
, OPTIONS
)) != -1)
131 case 'L': /* -8Lew are ignored to allow rlogin aliases */
146 timeout
= atoi(optarg
);
154 /* if haven't gotten a host yet, do so */
155 if (!host
&& !(host
= argv
[optind
++]))
158 /* if no further arguments, must have been called as rlogin. */
162 execv(_PATH_RLOGIN
, argv
);
163 err(1, "can't exec %s", _PATH_RLOGIN
);
169 if (!(pw
= getpwuid(uid
= getuid())))
170 errx(1, "unknown user id");
174 args
= copyargs(argv
);
178 sp
= getservbyname("shell", "tcp");
180 errx(1, "shell/tcp: unknown service");
183 signal(SIGALRM
, connect_timeout
);
186 rem
= rcmd_af(&host
, sp
->s_port
, pw
->pw_name
, user
, args
, &rfd2
,
189 signal(SIGALRM
, SIG_DFL
);
197 errx(1, "can't establish stderr");
199 if (setsockopt(rem
, SOL_SOCKET
, SO_DEBUG
, &one
,
202 if (setsockopt(rfd2
, SOL_SOCKET
, SO_DEBUG
, &one
,
208 omask
= sigblock(sigmask(SIGINT
)|sigmask(SIGQUIT
)|sigmask(SIGTERM
));
209 if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
210 (void)signal(SIGINT
, sendsig
);
211 if (signal(SIGQUIT
, SIG_IGN
) != SIG_IGN
)
212 (void)signal(SIGQUIT
, sendsig
);
213 if (signal(SIGTERM
, SIG_IGN
) != SIG_IGN
)
214 (void)signal(SIGTERM
, sendsig
);
222 (void)shutdown(rem
, 1);
224 (void)ioctl(rfd2
, FIONBIO
, &one
);
225 (void)ioctl(rem
, FIONBIO
, &one
);
227 talk(nflag
, omask
, pid
, rem
, timeout
);
230 (void)kill(pid
, SIGKILL
);
235 talk(int nflag
, long omask
, pid_t pid
, int rem
, int timeout
)
238 fd_set readfrom
, ready
, rembits
;
241 struct timeval tvtimeout
;
244 if (!nflag
&& pid
== 0) {
248 if ((cc
= read(0, buf
, sizeof buf
)) <= 0)
253 if (rem
>= FD_SETSIZE
)
254 errx(1, "descriptor too big");
256 FD_SET(rem
, &rembits
);
258 if (select(nfds
, 0, &rembits
, 0, 0) < 0) {
263 if (!FD_ISSET(rem
, &rembits
))
265 wc
= write(rem
, bp
, cc
);
267 if (errno
== EWOULDBLOCK
)
277 (void)shutdown(rem
, 1);
281 tvtimeout
.tv_sec
= timeout
;
282 tvtimeout
.tv_usec
= 0;
284 (void)sigsetmask(omask
);
285 if (rfd2
>= FD_SETSIZE
|| rem
>= FD_SETSIZE
)
286 errx(1, "descriptor too big");
288 FD_SET(rfd2
, &readfrom
);
289 FD_SET(rem
, &readfrom
);
290 nfds
= MAX(rfd2
+1, rem
+1);
294 srval
= select(nfds
, &ready
, 0, 0, &tvtimeout
);
296 srval
= select(nfds
, &ready
, 0, 0, 0);
305 errx(1, "timeout reached (%d seconds)\n", timeout
);
306 if (FD_ISSET(rfd2
, &ready
)) {
308 cc
= read(rfd2
, buf
, sizeof buf
);
310 if (errno
!= EWOULDBLOCK
)
311 FD_CLR(rfd2
, &readfrom
);
313 (void)write(STDERR_FILENO
, buf
, cc
);
315 if (FD_ISSET(rem
, &ready
)) {
317 cc
= read(rem
, buf
, sizeof buf
);
319 if (errno
!= EWOULDBLOCK
)
320 FD_CLR(rem
, &readfrom
);
322 (void)write(STDOUT_FILENO
, buf
, cc
);
324 } while (FD_ISSET(rfd2
, &readfrom
) || FD_ISSET(rem
, &readfrom
));
328 connect_timeout(int sig
)
330 char message
[] = "timeout reached before connection completed.\n";
332 write(STDERR_FILENO
, message
, sizeof(message
) - 1);
342 (void)write(rfd2
, &signo
, 1);
346 copyargs(char * const *argv
)
353 for (ap
= argv
; *ap
; ++ap
)
354 cc
+= strlen(*ap
) + 1;
355 if (!(args
= malloc((u_int
)cc
)))
357 for (p
= args
, ap
= argv
; *ap
; ++ap
) {
358 (void)strcpy(p
, *ap
);
359 for (p
= strcpy(p
, *ap
); *p
; ++p
);
370 (void)fprintf(stderr
,
371 "usage: rsh [-46] [-nd] [-l login] [-t timeout] host [command]\n");