1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1983, 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
2002-05-08 00:47:01 +00:00
|
|
|
* Copyright (c) 2002 Networks Associates Technology, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Portions of this software were developed for the FreeBSD Project by
|
|
|
|
* ThinkSec AS and NAI Labs, the Security Research Division of Network
|
|
|
|
* Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
|
|
|
|
* ("CBOSS"), as part of the DARPA CHATS research program.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-08-05 06:46:46 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1983, 1990, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
2002-10-16 12:33:14 +00:00
|
|
|
#if 0
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifndef lint
|
1997-09-28 08:57:24 +00:00
|
|
|
static const char sccsid[] = "@(#)rlogin.c 8.1 (Berkeley) 6/6/93";
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif /* not lint */
|
2002-10-16 12:33:14 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* rlogin - remote login
|
|
|
|
*/
|
2002-04-28 11:16:43 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <sys/param.h>
|
2003-09-28 08:54:56 +00:00
|
|
|
#include <sys/ioctl.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
1995-05-03 06:25:56 +00:00
|
|
|
#include <netinet/tcp.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1997-08-05 06:46:46 +00:00
|
|
|
#include <err.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
1998-10-09 06:47:57 +00:00
|
|
|
#include <libutil.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <netdb.h>
|
2002-05-08 00:47:02 +00:00
|
|
|
#include <paths.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <setjmp.h>
|
2003-09-28 08:54:56 +00:00
|
|
|
#include <termios.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#ifndef TIOCPKT_WINDOW
|
|
|
|
#define TIOCPKT_WINDOW 0x80
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* concession to Sun */
|
|
|
|
#ifndef SIGUSR1
|
|
|
|
#define SIGUSR1 30
|
|
|
|
#endif
|
|
|
|
|
2003-09-28 08:54:56 +00:00
|
|
|
int eight, rem;
|
|
|
|
struct termios deftty;
|
|
|
|
|
2000-02-15 15:11:40 +00:00
|
|
|
int family = PF_UNSPEC;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
int noescape;
|
|
|
|
u_char escapechar = '~';
|
|
|
|
|
|
|
|
#define get_window_size(fd, wp) ioctl(fd, TIOCGWINSZ, wp)
|
|
|
|
struct winsize winsize;
|
|
|
|
|
2002-03-22 01:33:25 +00:00
|
|
|
void catch_child(int);
|
|
|
|
void copytochild(int);
|
|
|
|
void doit(long) __dead2;
|
|
|
|
void done(int) __dead2;
|
|
|
|
void echo(char);
|
2002-10-16 16:09:05 +00:00
|
|
|
u_int getescape(const char *);
|
2002-03-22 01:33:25 +00:00
|
|
|
void lostpeer(int);
|
|
|
|
void mode(int);
|
2002-04-28 11:16:43 +00:00
|
|
|
void msg(const char *);
|
2002-03-22 01:33:25 +00:00
|
|
|
void oob(int);
|
|
|
|
int reader(int);
|
|
|
|
void sendwindow(void);
|
|
|
|
void setsignal(int);
|
|
|
|
void sigwinch(int);
|
|
|
|
void stop(char);
|
|
|
|
void usage(void) __dead2;
|
|
|
|
void writer(void);
|
|
|
|
void writeroob(int);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
int
|
2002-03-23 23:35:42 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
struct passwd *pw;
|
|
|
|
struct servent *sp;
|
2003-09-28 08:54:56 +00:00
|
|
|
struct termios tty;
|
1994-05-27 12:33:43 +00:00
|
|
|
long omask;
|
2002-10-16 16:09:05 +00:00
|
|
|
int argoff, ch, dflag, Dflag, one;
|
|
|
|
uid_t uid;
|
1999-05-27 12:58:41 +00:00
|
|
|
char *host, *localname, *p, *user, term[1024];
|
2003-09-28 08:54:56 +00:00
|
|
|
speed_t ospeed;
|
2002-05-08 00:47:01 +00:00
|
|
|
struct sockaddr_storage ss;
|
2005-08-05 09:58:49 +00:00
|
|
|
socklen_t sslen;
|
2005-11-13 21:03:56 +00:00
|
|
|
size_t len, len2;
|
|
|
|
int i;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1995-05-03 06:25:56 +00:00
|
|
|
argoff = dflag = Dflag = 0;
|
1994-05-27 12:33:43 +00:00
|
|
|
one = 1;
|
1999-05-27 12:58:41 +00:00
|
|
|
host = localname = user = NULL;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1997-09-28 08:57:24 +00:00
|
|
|
if ((p = rindex(argv[0], '/')))
|
1994-05-27 12:33:43 +00:00
|
|
|
++p;
|
|
|
|
else
|
|
|
|
p = argv[0];
|
|
|
|
|
|
|
|
if (strcmp(p, "rlogin"))
|
|
|
|
host = p;
|
|
|
|
|
|
|
|
/* handle "rlogin host flags" */
|
|
|
|
if (!host && argc > 2 && argv[1][0] != '-') {
|
|
|
|
host = argv[1];
|
|
|
|
argoff = 1;
|
|
|
|
}
|
|
|
|
|
2005-01-21 11:09:40 +00:00
|
|
|
#define OPTIONS "468DEde:i:l:"
|
1997-03-29 04:34:07 +00:00
|
|
|
while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
|
1994-05-27 12:33:43 +00:00
|
|
|
switch(ch) {
|
2000-02-15 15:11:40 +00:00
|
|
|
case '4':
|
|
|
|
family = PF_INET;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '6':
|
|
|
|
family = PF_INET6;
|
|
|
|
break;
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
case '8':
|
|
|
|
eight = 1;
|
|
|
|
break;
|
1995-05-03 06:25:56 +00:00
|
|
|
case 'D':
|
|
|
|
Dflag = 1;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'E':
|
|
|
|
noescape = 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
noescape = 0;
|
|
|
|
escapechar = getescape(optarg);
|
|
|
|
break;
|
1999-05-25 11:14:33 +00:00
|
|
|
case 'i':
|
1999-05-27 12:58:41 +00:00
|
|
|
if (getuid() != 0)
|
|
|
|
errx(1, "-i user: permission denied");
|
1999-05-25 11:14:33 +00:00
|
|
|
localname = optarg;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'l':
|
|
|
|
user = optarg;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
optind += argoff;
|
|
|
|
|
|
|
|
/* if haven't gotten a host yet, do so */
|
1998-03-26 18:03:41 +00:00
|
|
|
if (!host && !(host = argv[optind++]))
|
1994-05-27 12:33:43 +00:00
|
|
|
usage();
|
|
|
|
|
1998-03-26 18:03:41 +00:00
|
|
|
if (argv[optind])
|
1994-05-27 12:33:43 +00:00
|
|
|
usage();
|
|
|
|
|
1997-08-05 06:46:46 +00:00
|
|
|
if (!(pw = getpwuid(uid = getuid())))
|
|
|
|
errx(1, "unknown user id");
|
1994-05-27 12:33:43 +00:00
|
|
|
if (!user)
|
|
|
|
user = pw->pw_name;
|
1999-05-25 11:14:33 +00:00
|
|
|
if (!localname)
|
|
|
|
localname = pw->pw_name;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
sp = NULL;
|
2002-10-16 16:09:05 +00:00
|
|
|
sp = getservbyname("login", "tcp");
|
1997-08-05 06:46:46 +00:00
|
|
|
if (sp == NULL)
|
|
|
|
errx(1, "login/tcp: unknown service");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2003-09-28 08:54:56 +00:00
|
|
|
if ((p = getenv("TERM")) != NULL)
|
|
|
|
(void)strlcpy(term, p, sizeof(term));
|
|
|
|
len = strlen(term);
|
|
|
|
if (len < (sizeof(term) - 1) && tcgetattr(0, &tty) == 0) {
|
|
|
|
/* start at 2 to include the / */
|
|
|
|
for (ospeed = i = cfgetospeed(&tty), len2 = 2; i > 9; len2++)
|
|
|
|
i /= 10;
|
|
|
|
if (len + len2 < sizeof(term))
|
|
|
|
(void)snprintf(term + len, len2 + 1, "/%d", ospeed);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(void)get_window_size(0, &winsize);
|
|
|
|
|
|
|
|
(void)signal(SIGPIPE, lostpeer);
|
|
|
|
/* will use SIGUSR1 for window size hack, so hold it off */
|
|
|
|
omask = sigblock(sigmask(SIGURG) | sigmask(SIGUSR1));
|
|
|
|
/*
|
|
|
|
* We set SIGURG and SIGUSR1 below so that an
|
|
|
|
* incoming signal will be held pending rather than being
|
|
|
|
* discarded. Note that these routines will be ready to get
|
|
|
|
* a signal by the time that they are unblocked below.
|
|
|
|
*/
|
|
|
|
(void)signal(SIGURG, copytochild);
|
|
|
|
(void)signal(SIGUSR1, writeroob);
|
|
|
|
|
2000-02-15 15:11:40 +00:00
|
|
|
rem = rcmd_af(&host, sp->s_port, localname, user, term, 0, family);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (rem < 0)
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
if (dflag &&
|
|
|
|
setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one, sizeof(one)) < 0)
|
1997-08-05 06:46:46 +00:00
|
|
|
warn("setsockopt");
|
1995-05-03 06:25:56 +00:00
|
|
|
if (Dflag &&
|
|
|
|
setsockopt(rem, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) < 0)
|
1997-08-05 06:46:46 +00:00
|
|
|
warn("setsockopt NODELAY (ignored)");
|
1995-05-03 06:25:56 +00:00
|
|
|
|
2000-01-25 14:52:10 +00:00
|
|
|
sslen = sizeof(ss);
|
1994-05-27 12:33:43 +00:00
|
|
|
one = IPTOS_LOWDELAY;
|
2000-01-25 14:52:10 +00:00
|
|
|
if (getsockname(rem, (struct sockaddr *)&ss, &sslen) == 0 &&
|
|
|
|
ss.ss_family == AF_INET) {
|
|
|
|
if (setsockopt(rem, IPPROTO_IP, IP_TOS, (char *)&one,
|
|
|
|
sizeof(int)) < 0)
|
|
|
|
warn("setsockopt TOS (ignored)");
|
|
|
|
} else
|
|
|
|
if (ss.ss_family == AF_INET)
|
|
|
|
warn("setsockopt getsockname failed");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
(void)setuid(uid);
|
|
|
|
doit(omask);
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
2003-09-28 08:54:56 +00:00
|
|
|
int child;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
void
|
2002-03-23 23:35:42 +00:00
|
|
|
doit(long omask)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2003-09-28 08:54:56 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)signal(SIGINT, SIG_IGN);
|
|
|
|
setsignal(SIGHUP);
|
|
|
|
setsignal(SIGQUIT);
|
2003-09-28 08:54:56 +00:00
|
|
|
mode(1);
|
1994-05-27 12:33:43 +00:00
|
|
|
child = fork();
|
|
|
|
if (child == -1) {
|
1997-08-05 06:46:46 +00:00
|
|
|
warn("fork");
|
1994-05-27 12:33:43 +00:00
|
|
|
done(1);
|
|
|
|
}
|
|
|
|
if (child == 0) {
|
|
|
|
if (reader(omask) == 0) {
|
2002-10-16 12:33:14 +00:00
|
|
|
msg("connection closed");
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
sleep(1);
|
2002-10-16 12:33:14 +00:00
|
|
|
msg("\007connection closed");
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We may still own the socket, and may have a pending SIGURG (or might
|
|
|
|
* receive one soon) that we really want to send to the reader. When
|
|
|
|
* one of these comes in, the trap copytochild simply copies such
|
|
|
|
* signals to the child. We can now unblock SIGURG and SIGUSR1
|
|
|
|
* that were set above.
|
|
|
|
*/
|
|
|
|
(void)sigsetmask(omask);
|
|
|
|
(void)signal(SIGCHLD, catch_child);
|
|
|
|
writer();
|
2002-10-16 12:33:14 +00:00
|
|
|
msg("closed connection");
|
1994-05-27 12:33:43 +00:00
|
|
|
done(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* trap a signal, unless it is being ignored. */
|
|
|
|
void
|
2002-03-23 23:35:42 +00:00
|
|
|
setsignal(int sig)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
int omask = sigblock(sigmask(sig));
|
|
|
|
|
|
|
|
if (signal(sig, exit) == SIG_IGN)
|
|
|
|
(void)signal(sig, SIG_IGN);
|
|
|
|
(void)sigsetmask(omask);
|
|
|
|
}
|
|
|
|
|
1996-09-14 03:00:32 +00:00
|
|
|
void
|
2002-03-23 23:35:42 +00:00
|
|
|
done(int status)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
int w, wstatus;
|
|
|
|
|
|
|
|
mode(0);
|
|
|
|
if (child > 0) {
|
|
|
|
/* make sure catch_child does not snap it up */
|
|
|
|
(void)signal(SIGCHLD, SIG_DFL);
|
|
|
|
if (kill(child, SIGKILL) >= 0)
|
|
|
|
while ((w = wait(&wstatus)) > 0 && w != child);
|
|
|
|
}
|
|
|
|
exit(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
int dosigwinch;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is called when the reader process gets the out-of-band (urgent)
|
|
|
|
* request to turn on the window-changing protocol.
|
|
|
|
*/
|
2002-10-16 16:09:05 +00:00
|
|
|
/* ARGSUSED */
|
1994-05-27 12:33:43 +00:00
|
|
|
void
|
2002-04-28 11:16:43 +00:00
|
|
|
writeroob(int signo __unused)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
if (dosigwinch == 0) {
|
|
|
|
sendwindow();
|
|
|
|
(void)signal(SIGWINCH, sigwinch);
|
|
|
|
}
|
|
|
|
dosigwinch = 1;
|
|
|
|
}
|
|
|
|
|
2002-10-16 16:09:05 +00:00
|
|
|
/* ARGSUSED */
|
1994-05-27 12:33:43 +00:00
|
|
|
void
|
2002-04-28 11:16:43 +00:00
|
|
|
catch_child(int signo __unused)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-06-03 23:13:11 +00:00
|
|
|
pid_t pid;
|
|
|
|
int status;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
for (;;) {
|
2002-06-03 23:13:11 +00:00
|
|
|
pid = wait3(&status, WNOHANG|WUNTRACED, NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (pid == 0)
|
|
|
|
return;
|
|
|
|
/* if the child (reader) dies, just quit */
|
|
|
|
if (pid < 0 || (pid == child && !WIFSTOPPED(status)))
|
2002-06-03 23:13:11 +00:00
|
|
|
done(WTERMSIG(status) | WEXITSTATUS(status));
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* writer: write to remote: 0 -> line.
|
|
|
|
* ~. terminate
|
|
|
|
* ~^Z suspend rlogin process.
|
|
|
|
* ~<delayed-suspend char> suspend rlogin process, but leave reader alone.
|
|
|
|
*/
|
|
|
|
void
|
2002-03-23 23:35:42 +00:00
|
|
|
writer(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-03-23 23:35:42 +00:00
|
|
|
int bol, local, n;
|
1994-05-27 12:33:43 +00:00
|
|
|
char c;
|
|
|
|
|
|
|
|
bol = 1; /* beginning of line */
|
|
|
|
local = 0;
|
|
|
|
for (;;) {
|
|
|
|
n = read(STDIN_FILENO, &c, 1);
|
|
|
|
if (n <= 0) {
|
|
|
|
if (n < 0 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If we're at the beginning of the line and recognize a
|
|
|
|
* command character, then we echo locally. Otherwise,
|
|
|
|
* characters are echo'd remotely. If the command character
|
|
|
|
* is doubled, this acts as a force and local echo is
|
|
|
|
* suppressed.
|
|
|
|
*/
|
|
|
|
if (bol) {
|
|
|
|
bol = 0;
|
|
|
|
if (!noescape && c == escapechar) {
|
|
|
|
local = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (local) {
|
|
|
|
local = 0;
|
2003-09-28 08:54:56 +00:00
|
|
|
if (c == '.' || CCEQ(deftty.c_cc[VEOF], c)) {
|
1994-05-27 12:33:43 +00:00
|
|
|
echo(c);
|
|
|
|
break;
|
|
|
|
}
|
2003-09-28 08:54:56 +00:00
|
|
|
if (CCEQ(deftty.c_cc[VSUSP], c) ||
|
|
|
|
CCEQ(deftty.c_cc[VDSUSP], c)) {
|
1994-05-27 12:33:43 +00:00
|
|
|
bol = 1;
|
|
|
|
echo(c);
|
|
|
|
stop(c);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c != escapechar)
|
2002-10-16 16:09:05 +00:00
|
|
|
(void)write(rem, &escapechar, 1);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2002-10-16 16:09:05 +00:00
|
|
|
if (write(rem, &c, 1) == 0) {
|
|
|
|
msg("line gone");
|
|
|
|
break;
|
|
|
|
}
|
2003-09-28 08:54:56 +00:00
|
|
|
bol = CCEQ(deftty.c_cc[VKILL], c) ||
|
|
|
|
CCEQ(deftty.c_cc[VEOF], c) ||
|
|
|
|
CCEQ(deftty.c_cc[VINTR], c) ||
|
|
|
|
CCEQ(deftty.c_cc[VSUSP], c) ||
|
1994-05-27 12:33:43 +00:00
|
|
|
c == '\r' || c == '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-03-23 23:35:42 +00:00
|
|
|
echo(char c)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-03-23 23:35:42 +00:00
|
|
|
char *p;
|
1994-05-27 12:33:43 +00:00
|
|
|
char buf[8];
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
c &= 0177;
|
|
|
|
*p++ = escapechar;
|
|
|
|
if (c < ' ') {
|
|
|
|
*p++ = '^';
|
|
|
|
*p++ = c + '@';
|
|
|
|
} else if (c == 0177) {
|
|
|
|
*p++ = '^';
|
|
|
|
*p++ = '?';
|
|
|
|
} else
|
|
|
|
*p++ = c;
|
|
|
|
*p++ = '\r';
|
|
|
|
*p++ = '\n';
|
|
|
|
(void)write(STDOUT_FILENO, buf, p - buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
stop(char cmdc)
|
|
|
|
{
|
|
|
|
mode(0);
|
|
|
|
(void)signal(SIGCHLD, SIG_IGN);
|
2003-09-28 08:54:56 +00:00
|
|
|
(void)kill(CCEQ(deftty.c_cc[VSUSP], cmdc) ? 0 : getpid(), SIGTSTP);
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)signal(SIGCHLD, catch_child);
|
|
|
|
mode(1);
|
|
|
|
sigwinch(0); /* check for size changes */
|
|
|
|
}
|
|
|
|
|
2002-10-16 16:09:05 +00:00
|
|
|
/* ARGSUSED */
|
1994-05-27 12:33:43 +00:00
|
|
|
void
|
2002-04-28 11:16:43 +00:00
|
|
|
sigwinch(int signo __unused)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
struct winsize ws;
|
|
|
|
|
|
|
|
if (dosigwinch && get_window_size(0, &ws) == 0 &&
|
|
|
|
bcmp(&ws, &winsize, sizeof(ws))) {
|
|
|
|
winsize = ws;
|
|
|
|
sendwindow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send the window size to the server via the magic escape
|
|
|
|
*/
|
|
|
|
void
|
2002-03-23 23:35:42 +00:00
|
|
|
sendwindow(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
struct winsize *wp;
|
|
|
|
char obuf[4 + sizeof (struct winsize)];
|
|
|
|
|
|
|
|
wp = (struct winsize *)(obuf+4);
|
|
|
|
obuf[0] = 0377;
|
|
|
|
obuf[1] = 0377;
|
|
|
|
obuf[2] = 's';
|
|
|
|
obuf[3] = 's';
|
|
|
|
wp->ws_row = htons(winsize.ws_row);
|
|
|
|
wp->ws_col = htons(winsize.ws_col);
|
|
|
|
wp->ws_xpixel = htons(winsize.ws_xpixel);
|
|
|
|
wp->ws_ypixel = htons(winsize.ws_ypixel);
|
|
|
|
|
2002-10-16 16:09:05 +00:00
|
|
|
(void)write(rem, obuf, sizeof(obuf));
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* reader: read from remote: line -> 1
|
|
|
|
*/
|
|
|
|
#define READING 1
|
|
|
|
#define WRITING 2
|
|
|
|
|
|
|
|
jmp_buf rcvtop;
|
2002-10-16 16:09:05 +00:00
|
|
|
int rcvcnt, rcvstate;
|
|
|
|
pid_t ppid;
|
1994-05-27 12:33:43 +00:00
|
|
|
char rcvbuf[8 * 1024];
|
|
|
|
|
2002-10-16 16:09:05 +00:00
|
|
|
/* ARGSUSED */
|
1994-05-27 12:33:43 +00:00
|
|
|
void
|
2002-04-28 11:16:43 +00:00
|
|
|
oob(int signo __unused)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2003-09-28 08:54:56 +00:00
|
|
|
struct termios tty;
|
|
|
|
int atmark, n, rcvd;
|
1994-05-27 12:33:43 +00:00
|
|
|
char waste[BUFSIZ], mark;
|
|
|
|
|
|
|
|
rcvd = 0;
|
|
|
|
while (recv(rem, &mark, 1, MSG_OOB) < 0) {
|
|
|
|
switch (errno) {
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
/*
|
|
|
|
* Urgent data not here yet. It may not be possible
|
|
|
|
* to send it yet if we are blocked for output and
|
|
|
|
* our input buffer is full.
|
|
|
|
*/
|
2002-04-28 11:16:43 +00:00
|
|
|
if (rcvcnt < (int)sizeof(rcvbuf)) {
|
1994-05-27 12:33:43 +00:00
|
|
|
n = read(rem, rcvbuf + rcvcnt,
|
|
|
|
sizeof(rcvbuf) - rcvcnt);
|
|
|
|
if (n <= 0)
|
|
|
|
return;
|
|
|
|
rcvd += n;
|
|
|
|
} else {
|
|
|
|
n = read(rem, waste, sizeof(waste));
|
|
|
|
if (n <= 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mark & TIOCPKT_WINDOW) {
|
|
|
|
/* Let server know about window size changes */
|
|
|
|
(void)kill(ppid, SIGUSR1);
|
|
|
|
}
|
|
|
|
if (!eight && (mark & TIOCPKT_NOSTOP)) {
|
2003-09-28 08:54:56 +00:00
|
|
|
(void)tcgetattr(0, &tty);
|
|
|
|
tty.c_iflag &= ~IXON;
|
|
|
|
(void)tcsetattr(0, TCSANOW, &tty);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
if (!eight && (mark & TIOCPKT_DOSTOP)) {
|
2003-09-28 08:54:56 +00:00
|
|
|
(void)tcgetattr(0, &tty);
|
|
|
|
tty.c_iflag |= (deftty.c_iflag & IXON);
|
|
|
|
(void)tcsetattr(0, TCSANOW, &tty);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
if (mark & TIOCPKT_FLUSHWRITE) {
|
2003-09-28 08:54:56 +00:00
|
|
|
(void)tcflush(1, TCIOFLUSH);
|
1994-05-27 12:33:43 +00:00
|
|
|
for (;;) {
|
|
|
|
if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
|
1997-08-05 06:46:46 +00:00
|
|
|
warn("ioctl");
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (atmark)
|
|
|
|
break;
|
|
|
|
n = read(rem, waste, sizeof (waste));
|
|
|
|
if (n <= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Don't want any pending data to be output, so clear the recv
|
|
|
|
* buffer. If we were hanging on a write when interrupted,
|
|
|
|
* don't want it to restart. If we were reading, restart
|
|
|
|
* anyway.
|
|
|
|
*/
|
|
|
|
rcvcnt = 0;
|
|
|
|
longjmp(rcvtop, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* oob does not do FLUSHREAD (alas!) */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we filled the receive buffer while a read was pending, longjmp
|
|
|
|
* to the top to restart appropriately. Don't abort a pending write,
|
|
|
|
* however, or we won't know how much was written.
|
|
|
|
*/
|
|
|
|
if (rcvd && rcvstate == READING)
|
|
|
|
longjmp(rcvtop, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reader: read from remote: line -> 1 */
|
|
|
|
int
|
2002-03-23 23:35:42 +00:00
|
|
|
reader(int omask)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-10-16 16:09:05 +00:00
|
|
|
int n, remaining;
|
1994-05-27 12:33:43 +00:00
|
|
|
char *bufp;
|
2002-10-16 16:09:05 +00:00
|
|
|
pid_t pid;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-10-16 16:09:05 +00:00
|
|
|
pid = getpid();
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)signal(SIGTTOU, SIG_IGN);
|
|
|
|
(void)signal(SIGURG, oob);
|
2002-02-27 22:45:10 +00:00
|
|
|
(void)signal(SIGUSR1, oob); /* When propogating SIGURG from parent */
|
1994-05-27 12:33:43 +00:00
|
|
|
ppid = getppid();
|
|
|
|
(void)fcntl(rem, F_SETOWN, pid);
|
|
|
|
(void)setjmp(rcvtop);
|
|
|
|
(void)sigsetmask(omask);
|
|
|
|
bufp = rcvbuf;
|
|
|
|
for (;;) {
|
|
|
|
while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
|
|
|
|
rcvstate = WRITING;
|
|
|
|
n = write(STDOUT_FILENO, bufp, remaining);
|
|
|
|
if (n < 0) {
|
|
|
|
if (errno != EINTR)
|
|
|
|
return (-1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bufp += n;
|
|
|
|
}
|
|
|
|
bufp = rcvbuf;
|
|
|
|
rcvcnt = 0;
|
|
|
|
rcvstate = READING;
|
|
|
|
|
2002-10-16 16:09:05 +00:00
|
|
|
rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
|
1994-05-27 12:33:43 +00:00
|
|
|
if (rcvcnt == 0)
|
|
|
|
return (0);
|
|
|
|
if (rcvcnt < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
1997-08-05 06:46:46 +00:00
|
|
|
warn("read");
|
1994-05-27 12:33:43 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-03-23 23:35:42 +00:00
|
|
|
mode(int f)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2003-09-28 08:54:56 +00:00
|
|
|
struct termios tty;
|
|
|
|
|
|
|
|
switch (f) {
|
1994-05-27 12:33:43 +00:00
|
|
|
case 0:
|
2003-09-28 08:54:56 +00:00
|
|
|
(void)tcsetattr(0, TCSANOW, &deftty);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2003-09-28 08:54:56 +00:00
|
|
|
(void)tcgetattr(0, &deftty);
|
|
|
|
tty = deftty;
|
|
|
|
/* This is loosely derived from sys/kern/tty_compat.c. */
|
|
|
|
tty.c_lflag &= ~(ECHO|ICANON|ISIG|IEXTEN);
|
|
|
|
tty.c_iflag &= ~ICRNL;
|
|
|
|
tty.c_oflag &= ~OPOST;
|
|
|
|
tty.c_cc[VMIN] = 1;
|
|
|
|
tty.c_cc[VTIME] = 0;
|
|
|
|
if (eight) {
|
|
|
|
tty.c_iflag &= IXOFF;
|
|
|
|
tty.c_cflag &= ~(CSIZE|PARENB);
|
|
|
|
tty.c_cflag |= CS8;
|
|
|
|
}
|
|
|
|
(void)tcsetattr(0, TCSANOW, &tty);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-16 16:09:05 +00:00
|
|
|
/* ARGSUSED */
|
1994-05-27 12:33:43 +00:00
|
|
|
void
|
2002-04-28 11:16:43 +00:00
|
|
|
lostpeer(int signo __unused)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
(void)signal(SIGPIPE, SIG_IGN);
|
2002-10-16 12:33:14 +00:00
|
|
|
msg("\007connection closed");
|
1994-05-27 12:33:43 +00:00
|
|
|
done(1);
|
|
|
|
}
|
|
|
|
|
2002-02-27 22:45:10 +00:00
|
|
|
/* copy SIGURGs to the child process via SIGUSR1. */
|
2002-10-16 16:09:05 +00:00
|
|
|
/* ARGSUSED */
|
1994-05-27 12:33:43 +00:00
|
|
|
void
|
2002-04-28 11:16:43 +00:00
|
|
|
copytochild(int signo __unused)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-02-27 22:45:10 +00:00
|
|
|
(void)kill(child, SIGUSR1);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-04-28 11:16:43 +00:00
|
|
|
msg(const char *str)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
(void)fprintf(stderr, "rlogin: %s\r\n", str);
|
|
|
|
}
|
|
|
|
|
1996-09-14 03:00:32 +00:00
|
|
|
void
|
2002-03-23 23:35:42 +00:00
|
|
|
usage(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
(void)fprintf(stderr,
|
2000-02-15 15:11:40 +00:00
|
|
|
"usage: rlogin [-46%s]%s[-e char] [-i localname] [-l username] host\n",
|
2003-09-28 08:54:56 +00:00
|
|
|
"8DEd", " ");
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
u_int
|
2002-10-16 16:09:05 +00:00
|
|
|
getescape(const char *p)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
long val;
|
2002-10-16 16:09:05 +00:00
|
|
|
size_t len;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if ((len = strlen(p)) == 1) /* use any single char, including '\' */
|
|
|
|
return ((u_int)*p);
|
|
|
|
/* otherwise, \nnn */
|
|
|
|
if (*p == '\\' && len >= 2 && len <= 4) {
|
|
|
|
val = strtol(++p, NULL, 8);
|
|
|
|
for (;;) {
|
|
|
|
if (!*++p)
|
|
|
|
return ((u_int)val);
|
|
|
|
if (*p < '0' || *p > '8')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msg("illegal option value -- e");
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|