]>
git.saurik.com Git - apple/network_cmds.git/blob - rsh.tproj/rsh.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, 1990, 1993, 1994
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
59 * $Source: /cvs/Darwin/Commands/NeXT/network_cmds/rsh.tproj/rsh.c,v $
60 * $Header: /cvs/Darwin/Commands/NeXT/network_cmds/rsh.tproj/rsh.c,v 1.1.1.1 1999/05/02 03:58:17 wsanchez Exp $
63 #include <sys/types.h>
64 #include <sys/signal.h>
65 #include <sys/socket.h>
66 #include <sys/ioctl.h>
69 #include <netinet/in.h>
82 #include "pathnames.h"
85 #include <kerberosIV/des.h>
86 #include <kerberosIV/krb.h>
89 Key_schedule schedule
;
90 int use_kerberos
= 1, doencrypt
;
91 char dst_realm_buf
[REALM_SZ
], *dest_realm
;
92 extern char *krb_realmofhost();
100 char *copyargs
__P((char **));
101 void sendsig
__P((int));
102 void talk
__P((int, long, pid_t
, int));
103 void usage
__P((void));
104 void warning
__P(());
114 int argoff
, asrsh
, ch
, dflag
, nflag
, one
, rem
;
117 char *args
, *host
, *p
, *user
;
119 argoff
= asrsh
= dflag
= nflag
= 0;
123 /* if called as something other than "rsh", use it as the host name */
124 if (p
= strrchr(argv
[0], '/'))
128 if (strcmp(p
, "rsh"))
133 /* handle "rsh host flags" */
134 if (!host
&& argc
> 2 && argv
[1][0] != '-') {
141 #define OPTIONS "8KLdek:l:nwx"
143 #define OPTIONS "8KLdek:l:nw"
146 #define OPTIONS "8KLdel:nw"
148 while ((ch
= getopt(argc
- argoff
, argv
+ argoff
, OPTIONS
)) != EOF
)
155 case 'L': /* -8Lew are ignored to allow rlogin aliases */
168 dest_realm
= dst_realm_buf
;
169 strncpy(dest_realm
, optarg
, REALM_SZ
);
179 des_set_key(cred
.session
, schedule
);
189 /* if haven't gotten a host yet, do so */
190 if (!host
&& !(host
= argv
[optind
++]))
193 /* if no further arguments, must have been called as rlogin. */
197 execv(_PATH_RLOGIN
, argv
);
198 err(1, "can't exec %s", _PATH_RLOGIN
);
204 if (!(pw
= getpwuid(uid
= getuid())))
205 errx(1, "unknown user id");
206 /* Accept user1@host format, though "-l user2" overrides user1 */
207 p
= strchr(host
, '@');
210 if (!user
&& p
> host
)
221 /* -x turns off -n */
227 args
= copyargs(argv
);
232 sp
= getservbyname((doencrypt
? "ekshell" : "kshell"), "tcp");
235 warning("can't get entry for %s/tcp service",
236 doencrypt
? "ekshell" : "kshell");
241 sp
= getservbyname("shell", "tcp");
243 errx(1, "shell/tcp: unknown service");
250 /* fully qualify hostname (needed for krb_realmofhost) */
251 hp
= gethostbyname(host
);
252 if (hp
!= NULL
&& !(host
= strdup(hp
->h_name
)))
257 if (dest_realm
== NULL
)
258 dest_realm
= krb_realmofhost(host
);
262 rem
= krcmd_mutual(&host
, sp
->s_port
, user
, args
,
263 &rfd2
, dest_realm
, &cred
, schedule
);
266 rem
= krcmd(&host
, sp
->s_port
, user
, args
, &rfd2
,
270 sp
= getservbyname("shell", "tcp");
272 errx(1, "shell/tcp: unknown service");
273 if (errno
== ECONNREFUSED
)
274 warning("remote host doesn't support Kerberos");
276 warning("can't provide Kerberos auth data");
281 errx(1, "the -x flag requires Kerberos authentication");
282 rem
= rcmd(&host
, sp
->s_port
, pw
->pw_name
, user
, args
, &rfd2
);
285 rem
= rcmd(&host
, sp
->s_port
, pw
->pw_name
, user
, args
, &rfd2
);
292 errx(1, "can't establish stderr");
294 if (setsockopt(rem
, SOL_SOCKET
, SO_DEBUG
, &one
,
297 if (setsockopt(rfd2
, SOL_SOCKET
, SO_DEBUG
, &one
,
303 omask
= sigblock(sigmask(SIGINT
)|sigmask(SIGQUIT
)|sigmask(SIGTERM
));
304 if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
305 (void)signal(SIGINT
, sendsig
);
306 if (signal(SIGQUIT
, SIG_IGN
) != SIG_IGN
)
307 (void)signal(SIGQUIT
, sendsig
);
308 if (signal(SIGTERM
, SIG_IGN
) != SIG_IGN
)
309 (void)signal(SIGTERM
, sendsig
);
323 (void)ioctl(rfd2
, FIONBIO
, &one
);
324 (void)ioctl(rem
, FIONBIO
, &one
);
327 talk(nflag
, omask
, pid
, rem
);
330 (void)kill(pid
, SIGKILL
);
335 talk(nflag
, omask
, pid
, rem
)
342 fd_set readfrom
, ready
, rembits
;
343 char *bp
, buf
[BUFSIZ
];
345 if (!nflag
&& pid
== 0) {
349 if ((cc
= read(0, buf
, sizeof buf
)) <= 0)
355 FD_SET(rem
, &rembits
);
356 if (select(16, 0, &rembits
, 0, 0) < 0) {
361 if (!FD_ISSET(rem
, &rembits
))
366 wc
= des_write(rem
, bp
, cc
);
370 wc
= write(rem
, bp
, cc
);
372 if (errno
== EWOULDBLOCK
)
382 (void)shutdown(rem
, 1);
386 (void)sigsetmask(omask
);
388 FD_SET(rfd2
, &readfrom
);
389 FD_SET(rem
, &readfrom
);
392 if (select(16, &ready
, 0, 0, 0) < 0) {
397 if (FD_ISSET(rfd2
, &ready
)) {
402 cc
= des_read(rfd2
, buf
, sizeof buf
);
406 cc
= read(rfd2
, buf
, sizeof buf
);
408 if (errno
!= EWOULDBLOCK
)
409 FD_CLR(rfd2
, &readfrom
);
411 (void)write(2, buf
, cc
);
413 if (FD_ISSET(rem
, &ready
)) {
418 cc
= des_read(rem
, buf
, sizeof buf
);
422 cc
= read(rem
, buf
, sizeof buf
);
424 if (errno
!= EWOULDBLOCK
)
425 FD_CLR(rem
, &readfrom
);
427 (void)write(1, buf
, cc
);
429 } while (FD_ISSET(rfd2
, &readfrom
) || FD_ISSET(rem
, &readfrom
));
442 (void)des_write(rfd2
, &signo
, 1);
446 (void)write(rfd2
, &signo
, 1);
458 (void)fprintf(stderr
, "rsh: warning, using standard rsh: ");
460 fmt
= va_arg(ap
, char *);
461 vfprintf(stderr
, fmt
, ap
);
463 (void)fprintf(stderr
, ".\n");
472 char **ap
, *args
, *p
;
475 for (ap
= argv
; *ap
; ++ap
)
476 cc
+= strlen(*ap
) + 1;
477 if (!(args
= malloc((u_int
)cc
)))
479 for (p
= args
, ap
= argv
; *ap
; ++ap
) {
480 (void)strcpy(p
, *ap
);
481 for (p
= strcpy(p
, *ap
); *p
; ++p
);
492 (void)fprintf(stderr
,
493 "usage: rsh [-nd%s]%s[-l login] [login@]host [command]\n",
496 "x", " [-k realm] ");