2 * Copyright (c) 1983, 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1990, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "From: @(#)rsh.c 8.3 (Berkeley) 4/6/94";
44 static const char rcsid[] =
45 "$FreeBSD: src/usr.bin/rsh/rsh.c,v 1.21.2.4 2002/09/17 15:34:41 nectar Exp $";
48 #include <sys/param.h>
49 #include <sys/signal.h>
50 #include <sys/socket.h>
51 #include <sys/ioctl.h>
55 #include <netinet/in.h>
69 #include "pathnames.h"
72 #include <openssl/des.h>
77 Key_schedule schedule;
78 int use_kerberos = 1, doencrypt;
79 char dst_realm_buf[REALM_SZ], *dest_realm;
80 extern char *krb_realmofhost();
88 int family = PF_UNSPEC;
90 void connect_timeout __P((int));
91 char *copyargs __P((char **));
92 void sendsig __P((int));
93 void talk __P((int, long, pid_t, int, int));
94 void usage __P((void));
104 int argoff, asrsh, ch, dflag, nflag, one, rem;
107 char *args, *host, *p, *user;
113 argoff = asrsh = dflag = nflag = 0;
117 /* if called as something other than "rsh", use it as the host name */
118 if ((p = strrchr(argv[0], '/')))
122 if (strcmp(p, "rsh"))
127 /* handle "rsh host flags" */
128 if (!host && argc > 2 && argv[1][0] != '-') {
135 #define OPTIONS "468KLde:k:l:nt:wx"
137 #define OPTIONS "468KLde:k:l:nt:w"
140 #define OPTIONS "468KLde:l:nt:w"
142 while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
157 case 'L': /* -8Lew are ignored to allow rlogin aliases */
170 dest_realm = dst_realm_buf;
171 strncpy(dest_realm, optarg, REALM_SZ);
185 timeout = atoi(optarg);
193 /* if haven't gotten a host yet, do so */
194 if (!host && !(host = argv[optind++]))
197 /* if no further arguments, must have been called as rlogin. */
201 execv(_PATH_RLOGIN, argv);
202 err(1, "can't exec %s", _PATH_RLOGIN);
208 if (!(pw = getpwuid(uid = getuid())))
209 errx(1, "unknown user id");
215 /* -x turns off -n */
221 args = copyargs(argv);
225 k = auth_getval("auth_list");
226 if (k && !strstr(k, "kerberos"))
229 sp = getservbyname((doencrypt ? "ekshell" : "kshell"), "tcp");
233 "warning, using standard rsh: can't get entry for %s/tcp service",
234 doencrypt ? "ekshell" : "kshell");
239 sp = getservbyname("shell", "tcp");
241 errx(1, "shell/tcp: unknown service");
248 /* fully qualify hostname (needed for krb_realmofhost) */
249 hp = gethostbyname(host);
250 if (hp != NULL && !(host = strdup(hp->h_name)))
255 if (dest_realm == NULL)
256 dest_realm = krb_realmofhost(host);
260 rem = krcmd_mutual(&host, sp->s_port, user, args,
261 &rfd2, dest_realm, &cred, schedule);
262 des_set_key(&cred.session, schedule);
265 rem = krcmd(&host, sp->s_port, user, args, &rfd2,
269 sp = getservbyname("shell", "tcp");
271 errx(1, "shell/tcp: unknown service");
272 if (errno == ECONNREFUSED)
274 "warning, using standard rsh: remote host doesn't support Kerberos");
277 "warning, using standard rsh: can't provide Kerberos auth data");
282 errx(1, "the -x flag requires Kerberos authentication");
283 rem = rcmd_af(&host, sp->s_port, pw->pw_name, user, args,
288 signal(SIGALRM, connect_timeout);
291 rem = rcmd_af(&host, sp->s_port, pw->pw_name, user, args, &rfd2,
294 signal(SIGALRM, SIG_DFL);
303 errx(1, "can't establish stderr");
305 if (setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one,
308 if (setsockopt(rfd2, SOL_SOCKET, SO_DEBUG, &one,
314 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGTERM));
315 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
316 (void)signal(SIGINT, sendsig);
317 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
318 (void)signal(SIGQUIT, sendsig);
319 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
320 (void)signal(SIGTERM, sendsig);
328 (void)shutdown(rem, 1);
336 (void)ioctl(rfd2, FIONBIO, &one);
337 (void)ioctl(rem, FIONBIO, &one);
340 talk(nflag, omask, pid, rem, timeout);
343 (void)kill(pid, SIGKILL);
348 talk(nflag, omask, pid, rem, timeout)
355 fd_set readfrom, ready, rembits;
356 char *bp, buf[BUFSIZ];
357 struct timeval tvtimeout;
360 if (!nflag && pid == 0) {
364 if ((cc = read(0, buf, sizeof buf)) <= 0)
369 if (rem >= FD_SETSIZE)
370 errx(1, "descriptor too big");
372 FD_SET(rem, &rembits);
374 if (select(nfds, 0, &rembits, 0, 0) < 0) {
379 if (!FD_ISSET(rem, &rembits))
384 wc = des_enc_write(rem, bp, cc, schedule, &cred.session);
388 wc = write(rem, bp, cc);
390 if (errno == EWOULDBLOCK)
400 (void)shutdown(rem, 1);
404 tvtimeout.tv_sec = timeout;
405 tvtimeout.tv_usec = 0;
407 (void)sigsetmask(omask);
408 if (rfd2 >= FD_SETSIZE || rem >= FD_SETSIZE)
409 errx(1, "descriptor too big");
411 FD_SET(rfd2, &readfrom);
412 FD_SET(rem, &readfrom);
413 nfds = MAX(rfd2+1, rem+1);
417 srval = select(nfds, &ready, 0, 0, &tvtimeout);
419 srval = select(nfds, &ready, 0, 0, 0);
428 errx(1, "timeout reached (%d seconds)\n", timeout);
429 if (FD_ISSET(rfd2, &ready)) {
434 cc = des_enc_read(rfd2, buf, sizeof buf, schedule, &cred.session);
438 cc = read(rfd2, buf, sizeof buf);
440 if (errno != EWOULDBLOCK)
441 FD_CLR(rfd2, &readfrom);
443 (void)write(2, buf, cc);
445 if (FD_ISSET(rem, &ready)) {
450 cc = des_enc_read(rem, buf, sizeof buf, schedule, &cred.session);
454 cc = read(rem, buf, sizeof buf);
456 if (errno != EWOULDBLOCK)
457 FD_CLR(rem, &readfrom);
459 (void)write(1, buf, cc);
461 } while (FD_ISSET(rfd2, &readfrom) || FD_ISSET(rem, &readfrom));
465 connect_timeout(int sig)
467 char message[] = "timeout reached before connection completed.\n";
469 write(STDERR_FILENO, message, sizeof(message) - 1);
483 (void)des_enc_write(rfd2, &signo, 1, schedule, &cred.session);
487 (void)write(rfd2, &signo, 1);
495 char **ap, *args, *p;
498 for (ap = argv; *ap; ++ap)
499 cc += strlen(*ap) + 1;
500 if (!(args = malloc((u_int)cc)))
502 for (p = args, ap = argv; *ap; ++ap) {
503 (void)strcpy(p, *ap);
504 for (p = strcpy(p, *ap); *p; ++p);
515 (void)fprintf(stderr,
516 "usage: rsh [-46] [-ndK%s]%s[-l login] [-t timeout] host [command]\n",
519 "x", " [-k realm] ");