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
33 * @(#) Copyright (c) 1983, 1990, 1993, 1994 The Regents of the University of California. All rights reserved.
34 * @(#)rsh.c 8.3 (Berkeley) 4/6/94
35 * $FreeBSD: src/usr.bin/rsh/rsh.c,v 1.21.2.4 2002/09/17 15:34:41 nectar Exp $
36 * $DragonFly: src/usr.bin/rsh/rsh.c,v 1.3 2003/11/03 19:31:32 eirikn Exp $
39 #include <sys/param.h>
40 #include <sys/signal.h>
41 #include <sys/socket.h>
42 #include <sys/ioctl.h>
46 #include <netinet/in.h>
60 #include "pathnames.h"
63 #include <openssl/des.h>
68 Key_schedule schedule;
69 int use_kerberos = 1, doencrypt;
70 char dst_realm_buf[REALM_SZ], *dest_realm;
71 extern char *krb_realmofhost();
79 int family = PF_UNSPEC;
81 void connect_timeout(int);
82 char *copyargs(char **);
84 void talk(int, long, pid_t, int, int);
95 int argoff, asrsh, ch, dflag, nflag, one, rem;
98 char *args, *host, *p, *user;
104 argoff = asrsh = dflag = nflag = 0;
108 /* if called as something other than "rsh", use it as the host name */
109 if ((p = strrchr(argv[0], '/')))
113 if (strcmp(p, "rsh"))
118 /* handle "rsh host flags" */
119 if (!host && argc > 2 && argv[1][0] != '-') {
126 #define OPTIONS "468KLde:k:l:nt:wx"
128 #define OPTIONS "468KLde:k:l:nt:w"
131 #define OPTIONS "468KLde:l:nt:w"
133 while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
148 case 'L': /* -8Lew are ignored to allow rlogin aliases */
161 dest_realm = dst_realm_buf;
162 strncpy(dest_realm, optarg, REALM_SZ);
176 timeout = atoi(optarg);
184 /* if haven't gotten a host yet, do so */
185 if (!host && !(host = argv[optind++]))
188 /* if no further arguments, must have been called as rlogin. */
192 execv(_PATH_RLOGIN, argv);
193 err(1, "can't exec %s", _PATH_RLOGIN);
199 if (!(pw = getpwuid(uid = getuid())))
200 errx(1, "unknown user id");
206 /* -x turns off -n */
212 args = copyargs(argv);
216 k = auth_getval("auth_list");
217 if (k && !strstr(k, "kerberos"))
220 sp = getservbyname((doencrypt ? "ekshell" : "kshell"), "tcp");
224 "warning, using standard rsh: can't get entry for %s/tcp service",
225 doencrypt ? "ekshell" : "kshell");
230 sp = getservbyname("shell", "tcp");
232 errx(1, "shell/tcp: unknown service");
239 /* fully qualify hostname (needed for krb_realmofhost) */
240 hp = gethostbyname(host);
241 if (hp != NULL && !(host = strdup(hp->h_name)))
246 if (dest_realm == NULL)
247 dest_realm = krb_realmofhost(host);
251 rem = krcmd_mutual(&host, sp->s_port, user, args,
252 &rfd2, dest_realm, &cred, schedule);
253 des_set_key(&cred.session, schedule);
256 rem = krcmd(&host, sp->s_port, user, args, &rfd2,
260 sp = getservbyname("shell", "tcp");
262 errx(1, "shell/tcp: unknown service");
263 if (errno == ECONNREFUSED)
265 "warning, using standard rsh: remote host doesn't support Kerberos");
268 "warning, using standard rsh: can't provide Kerberos auth data");
273 errx(1, "the -x flag requires Kerberos authentication");
274 rem = rcmd_af(&host, sp->s_port, pw->pw_name, user, args,
279 signal(SIGALRM, connect_timeout);
282 rem = rcmd_af(&host, sp->s_port, pw->pw_name, user, args, &rfd2,
285 signal(SIGALRM, SIG_DFL);
294 errx(1, "can't establish stderr");
296 if (setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one,
299 if (setsockopt(rfd2, SOL_SOCKET, SO_DEBUG, &one,
305 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGTERM));
306 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
307 (void)signal(SIGINT, sendsig);
308 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
309 (void)signal(SIGQUIT, sendsig);
310 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
311 (void)signal(SIGTERM, sendsig);
319 (void)shutdown(rem, 1);
327 (void)ioctl(rfd2, FIONBIO, &one);
328 (void)ioctl(rem, FIONBIO, &one);
331 talk(nflag, omask, pid, rem, timeout);
334 (void)kill(pid, SIGKILL);
339 talk(nflag, omask, pid, rem, timeout)
346 fd_set readfrom, ready, rembits;
347 char *bp, buf[BUFSIZ];
348 struct timeval tvtimeout;
351 if (!nflag && pid == 0) {
355 if ((cc = read(0, buf, sizeof buf)) <= 0)
360 if (rem >= FD_SETSIZE)
361 errx(1, "descriptor too big");
363 FD_SET(rem, &rembits);
365 if (select(nfds, 0, &rembits, 0, 0) < 0) {
370 if (!FD_ISSET(rem, &rembits))
375 wc = des_enc_write(rem, bp, cc, schedule, &cred.session);
379 wc = write(rem, bp, cc);
381 if (errno == EWOULDBLOCK)
391 (void)shutdown(rem, 1);
395 tvtimeout.tv_sec = timeout;
396 tvtimeout.tv_usec = 0;
398 (void)sigsetmask(omask);
399 if (rfd2 >= FD_SETSIZE || rem >= FD_SETSIZE)
400 errx(1, "descriptor too big");
402 FD_SET(rfd2, &readfrom);
403 FD_SET(rem, &readfrom);
404 nfds = MAX(rfd2+1, rem+1);
408 srval = select(nfds, &ready, 0, 0, &tvtimeout);
410 srval = select(nfds, &ready, 0, 0, 0);
419 errx(1, "timeout reached (%d seconds)\n", timeout);
420 if (FD_ISSET(rfd2, &ready)) {
425 cc = des_enc_read(rfd2, buf, sizeof buf, schedule, &cred.session);
429 cc = read(rfd2, buf, sizeof buf);
431 if (errno != EWOULDBLOCK)
432 FD_CLR(rfd2, &readfrom);
434 (void)write(2, buf, cc);
436 if (FD_ISSET(rem, &ready)) {
441 cc = des_enc_read(rem, buf, sizeof buf, schedule, &cred.session);
445 cc = read(rem, buf, sizeof buf);
447 if (errno != EWOULDBLOCK)
448 FD_CLR(rem, &readfrom);
450 (void)write(1, buf, cc);
452 } while (FD_ISSET(rfd2, &readfrom) || FD_ISSET(rem, &readfrom));
456 connect_timeout(int sig)
458 char message[] = "timeout reached before connection completed.\n";
460 write(STDERR_FILENO, message, sizeof(message) - 1);
474 (void)des_enc_write(rfd2, &signo, 1, schedule, &cred.session);
478 (void)write(rfd2, &signo, 1);
486 char **ap, *args, *p;
489 for (ap = argv; *ap; ++ap)
490 cc += strlen(*ap) + 1;
491 if (!(args = malloc((u_int)cc)))
493 for (p = args, ap = argv; *ap; ++ap) {
494 (void)strcpy(p, *ap);
495 for (p = strcpy(p, *ap); *p; ++p);
506 (void)fprintf(stderr,
507 "usage: rsh [-46] [-ndK%s]%s[-l login] [-t timeout] host [command]\n",
510 "x", " [-k realm] ");