NetBSD/usr.bin/rlogin/rlogin.c

1053 lines
22 KiB
C
Raw Normal View History

2001-02-20 02:03:42 +03:00
/* $NetBSD: rlogin.c,v 1.25 2001/02/19 23:03:51 cgd Exp $ */
1995-03-21 10:58:28 +03:00
1993-03-21 12:45:37 +03:00
/*
1995-03-21 10:58:28 +03:00
* Copyright (c) 1983, 1990, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03: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.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1983, 1990, 1993\n\
The Regents of the University of California. All rights reserved.\n");
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#ifndef lint
1995-03-21 10:58:28 +03:00
#if 0
1997-01-09 09:57:45 +03:00
static char sccsid[] = "@(#)rlogin.c 8.4 (Berkeley) 4/29/95";
1995-03-21 10:58:28 +03:00
#else
2001-02-20 02:03:42 +03:00
__RCSID("$NetBSD: rlogin.c,v 1.25 2001/02/19 23:03:51 cgd Exp $");
1995-03-21 10:58:28 +03:00
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
/*
* rlogin - remote login
*/
#include <sys/param.h>
1995-05-03 11:13:51 +04:00
#include <sys/ioctl.h>
1993-03-21 12:45:37 +03:00
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
1997-01-09 09:57:45 +03:00
#include <sys/ioctl.h>
1993-03-21 12:45:37 +03:00
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <err.h>
1993-03-21 12:45:37 +03:00
#include <errno.h>
1995-03-21 10:58:28 +03:00
#include <fcntl.h>
#include <netdb.h>
1993-03-21 12:45:37 +03:00
#include <pwd.h>
1995-03-21 10:58:28 +03:00
#include <setjmp.h>
1995-05-03 11:13:51 +04:00
#include <termios.h>
1995-03-21 10:58:28 +03:00
#include <signal.h>
1993-03-21 12:45:37 +03:00
#include <stdio.h>
1995-03-21 10:58:28 +03:00
#include <stdlib.h>
1993-03-21 12:45:37 +03:00
#include <string.h>
1995-03-21 10:58:28 +03:00
#include <unistd.h>
#ifdef __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
1993-03-21 12:45:37 +03:00
#ifdef KERBEROS
#include <kerberosIV/des.h>
#include <kerberosIV/krb.h>
1997-01-09 23:18:21 +03:00
#include <kerberosIV/kstream.h>
1993-03-21 12:45:37 +03:00
1995-03-21 10:58:28 +03:00
#include "krb.h"
1993-03-21 12:45:37 +03:00
CREDENTIALS cred;
Key_schedule schedule;
1997-01-09 23:18:21 +03:00
MSG_DAT msg_data;
struct sockaddr_in local, foreign;
1993-03-21 12:45:37 +03:00
int use_kerberos = 1, doencrypt;
1997-01-09 23:18:21 +03:00
kstream krem;
1993-03-21 12:45:37 +03:00
#endif
#ifndef TIOCPKT_WINDOW
#define TIOCPKT_WINDOW 0x80
#endif
/* concession to Sun */
#ifndef SIGUSR1
#define SIGUSR1 30
#endif
1995-05-03 11:13:51 +04:00
#ifndef CCEQ
#define CCEQ(val, c) (c == val ? val != _POSIX_VDISABLE : 0)
#endif
int eight, rem;
struct termios deftty;
1993-03-21 12:45:37 +03:00
int noescape;
u_char escapechar = '~';
1995-03-21 10:58:28 +03:00
#ifdef OLDSUN
1993-03-21 12:45:37 +03:00
struct winsize {
unsigned short ws_row, ws_col;
unsigned short ws_xpixel, ws_ypixel;
};
1995-03-21 10:58:28 +03:00
#else
#define get_window_size(fd, wp) ioctl(fd, TIOCGWINSZ, wp)
1993-03-21 12:45:37 +03:00
#endif
struct winsize winsize;
1995-03-21 10:58:28 +03:00
void catch_child __P((int));
void copytochild __P((int));
void doit __P((sigset_t *));
void done __P((int));
void echo __P((int));
1995-03-21 10:58:28 +03:00
u_int getescape __P((char *));
void lostpeer __P((int));
int main __P((int, char **));
1995-03-21 10:58:28 +03:00
void mode __P((int));
void msg __P((char *));
void oob __P((int));
1997-01-09 09:57:45 +03:00
int reader __P((sigset_t *));
1995-03-21 10:58:28 +03:00
void sendwindow __P((void));
void setsignal __P((int));
1997-01-09 09:57:45 +03:00
int speed __P((int));
1995-03-21 10:58:28 +03:00
void sigwinch __P((int));
1995-05-03 11:36:59 +04:00
void stop __P((int));
void usage __P((void));
1995-03-21 10:58:28 +03:00
void writer __P((void));
void writeroob __P((int));
#ifdef KERBEROS
void warning __P((const char *, ...));
#endif
#ifdef OLDSUN
int get_window_size __P((int, struct winsize *));
1993-03-21 12:45:37 +03:00
#endif
1995-03-21 10:58:28 +03:00
int
1993-03-21 12:45:37 +03:00
main(argc, argv)
int argc;
1995-03-21 10:58:28 +03:00
char *argv[];
1993-03-21 12:45:37 +03:00
{
struct passwd *pw;
struct servent *sp;
1995-05-03 11:13:51 +04:00
struct termios tty;
1997-01-09 09:57:45 +03:00
sigset_t smask;
1993-03-21 12:45:37 +03:00
int argoff, ch, dflag, one, uid;
int i, len, len2;
char *host, *p, *user, *name, term[1024] = "network";
speed_t ospeed;
1997-01-09 09:57:45 +03:00
struct sigaction sa;
1997-02-17 09:14:25 +03:00
struct rlimit rlim;
1997-01-09 23:18:21 +03:00
#ifdef KERBEROS
KTEXT_ST ticket;
int sock;
long authopts;
int through_once = 0;
extern int _kstream_des_debug_OOB;
char *dest_realm = NULL;
1997-01-09 23:18:21 +03:00
#endif
1993-03-21 12:45:37 +03:00
argoff = dflag = 0;
one = 1;
host = user = NULL;
2001-02-20 02:03:42 +03:00
if (strcmp(getprogname(), "rlogin") != 0) {
host = strdup(getprogname());
if (host == NULL)
err(1, NULL);
}
1993-03-21 12:45:37 +03:00
/* handle "rlogin host flags" */
if (!host && argc > 2 && argv[1][0] != '-') {
host = argv[1];
argoff = 1;
}
#ifdef KERBEROS
#define OPTIONS "8EKLde:k:l:x"
#else
#define OPTIONS "8EKLde:l:"
#endif
while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
1993-03-21 12:45:37 +03:00
switch(ch) {
case '8':
eight = 1;
break;
case 'E':
noescape = 1;
break;
#ifdef KERBEROS
case 'K':
1993-03-21 12:45:37 +03:00
use_kerberos = 0;
break;
#endif
1993-03-21 12:45:37 +03:00
case 'd':
1997-01-09 23:18:21 +03:00
#ifdef KERBEROS
_kstream_des_debug_OOB = 1;
#endif
1993-03-21 12:45:37 +03:00
dflag = 1;
break;
case 'e':
1995-03-21 10:58:28 +03:00
noescape = 0;
1993-03-21 12:45:37 +03:00
escapechar = getescape(optarg);
break;
#ifdef KERBEROS
case 'k':
dest_realm = optarg;
1993-03-21 12:45:37 +03:00
break;
#endif
case 'l':
user = optarg;
break;
#ifdef CRYPT
#ifdef KERBEROS
case 'x':
doencrypt = 1;
break;
#endif
#endif
case '?':
default:
usage();
}
optind += argoff;
argc -= optind;
argv += optind;
/* if haven't gotten a host yet, do so */
if (!host && !(host = *argv++))
usage();
if (*argv)
usage();
1997-01-09 09:57:45 +03:00
if (!(pw = getpwuid(uid = getuid())))
errx(1, "unknown user id.");
/* Accept user1@host format, though "-l user2" overrides user1 */
p = strchr(host, '@');
if (p) {
*p = '\0';
if (!user && p > host)
user = host;
host = p + 1;
if (*host == '\0')
usage();
1993-03-21 12:45:37 +03:00
}
if ((name = strdup(pw->pw_name)) == NULL)
err(1, "malloc");
1993-03-21 12:45:37 +03:00
if (!user)
user = name;
1993-03-21 12:45:37 +03:00
#ifdef KERBEROS
sp = NULL;
1993-03-21 12:45:37 +03:00
if (use_kerberos) {
sp = getservbyname((doencrypt ? "eklogin" : "klogin"), "tcp");
if (sp == NULL) {
use_kerberos = 0;
warning("can't get entry for %s/tcp service",
doencrypt ? "eklogin" : "klogin");
}
}
if (sp == NULL)
#endif
1993-03-21 12:45:37 +03:00
sp = getservbyname("login", "tcp");
1997-01-09 09:57:45 +03:00
if (sp == NULL)
errx(1, "login/tcp: unknown service.");
1993-03-21 12:45:37 +03:00
if ((p = getenv("TERM")) != NULL) {
(void)strncpy(term, p, sizeof(term) - 1);
term[sizeof(term) - 1] = '\0';
}
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;
1996-08-10 23:47:32 +04:00
if (len + len2 < sizeof(term))
(void)snprintf(term + len, len2 + 1, "/%d", ospeed);
1993-03-21 12:45:37 +03:00
}
(void)get_window_size(0, &winsize);
1997-01-09 09:57:45 +03:00
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sa.sa_handler = lostpeer;
(void)sigaction(SIGPIPE, &sa, (struct sigaction *)0);
1993-03-21 12:45:37 +03:00
/* will use SIGUSR1 for window size hack, so hold it off */
1997-01-09 09:57:45 +03:00
sigemptyset(&smask);
sigaddset(&smask, SIGURG);
sigaddset(&smask, SIGUSR1);
(void)sigprocmask(SIG_SETMASK, &smask, &smask);
1995-03-21 10:58:28 +03:00
/*
* 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.;
1995-03-21 10:58:28 +03:00
*/
1997-01-09 09:57:45 +03:00
sa.sa_handler = copytochild;
(void)sigaction(SIGURG, &sa, (struct sigaction *) 0);
sa.sa_handler = writeroob;
(void)sigaction(SIGUSR1, &sa, (struct sigaction *) 0);
1997-02-17 09:14:25 +03:00
/* don't dump core */
rlim.rlim_cur = rlim.rlim_max = 0;
if (setrlimit(RLIMIT_CORE, &rlim) < 0)
warn("setrlimit");
1993-03-21 12:45:37 +03:00
#ifdef KERBEROS
try_connect:
if (use_kerberos) {
1995-03-21 10:58:28 +03:00
struct hostent *hp;
/* Fully qualify hostname (needed for krb_realmofhost). */
hp = gethostbyname(host);
1997-01-09 09:57:45 +03:00
if (hp != NULL && !(host = strdup(hp->h_name)))
errx(1, "%s", strerror(ENOMEM));
1995-03-21 10:58:28 +03:00
1993-03-21 12:45:37 +03:00
rem = KSUCCESS;
errno = 0;
#ifdef CRYPT
if (doencrypt)
1997-01-09 23:18:21 +03:00
authopts = KOPT_DO_MUTUAL;
1993-03-21 12:45:37 +03:00
else
#endif /* CRYPT */
1997-01-09 23:18:21 +03:00
authopts = 0L;
if (dest_realm == NULL) {
/* default this now, once. */
if (!(dest_realm = krb_realmofhost (host))) {
warnx("Unknown realm for host %s.", host);
use_kerberos = 0;
sp = getservbyname("login", "tcp");
goto try_connect;
}
1993-03-21 12:45:37 +03:00
}
1997-01-09 23:18:21 +03:00
rem = kcmd(&sock, &host, sp->s_port, name, user,
1997-01-09 23:18:21 +03:00
term, 0, &ticket, "rcmd", dest_realm,
&cred, schedule, &msg_data, &local, &foreign,
authopts);
if (rem != KSUCCESS) {
switch(rem) {
case KDC_PR_UNKNOWN:
warnx("Host %s not registered for %s",
host, "Kerberos rlogin service");
use_kerberos = 0;
sp = getservbyname("login", "tcp");
1997-01-09 23:18:21 +03:00
goto try_connect;
case NO_TKT_FIL:
if (through_once++) {
use_kerberos = 0;
sp = getservbyname("login", "tcp");
1997-01-09 23:18:21 +03:00
goto try_connect;
}
#ifdef notyet
krb_get_pw_in_tkt(user, krb_realm, "krbtgt",
krb_realm,
DEFAULT_TKT_LIFE/5, 0);
goto try_connect;
#endif
default:
warnx("Kerberos rcmd failed: %s",
(rem == -1) ? "rcmd protocol failure" :
krb_err_txt[rem]);
use_kerberos = 0;
sp = getservbyname("login", "tcp");
goto try_connect;
}
1997-01-09 23:18:21 +03:00
}
rem = sock;
if (doencrypt)
krem = kstream_create_rlogin_from_fd(rem, &schedule,
&cred.session);
else
krem = kstream_create_from_fd(rem, 0, 0);
kstream_set_buffer_mode(krem, 0);
1993-03-21 12:45:37 +03:00
} else {
#ifdef CRYPT
1997-01-09 09:57:45 +03:00
if (doencrypt)
errx(1, "the -x flag requires Kerberos authentication.");
1993-03-21 12:45:37 +03:00
#endif /* CRYPT */
rem = rcmd_af(&host, sp->s_port, name, user, term, 0,
PF_UNSPEC);
if (rem < 0)
1997-01-09 23:18:21 +03:00
exit(1);
1993-03-21 12:45:37 +03:00
}
#else
rem = rcmd_af(&host, sp->s_port, name, user, term, 0, PF_UNSPEC);
1997-01-09 23:18:21 +03:00
1993-03-21 12:45:37 +03:00
#endif /* KERBEROS */
if (rem < 0)
exit(1);
if (dflag &&
setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one, sizeof(one)) < 0)
1997-01-09 09:57:45 +03:00
warn("setsockopt DEBUG (ignored)");
{
struct sockaddr_storage ss;
int sslen;
sslen = sizeof(ss);
if (getsockname(rem, (struct sockaddr *)&ss, &sslen) == 0
&& ((struct sockaddr *)&ss)->sa_family == AF_INET) {
one = IPTOS_LOWDELAY;
if (setsockopt(rem, IPPROTO_IP, IP_TOS, (char *)&one,
sizeof(int)) < 0) {
warn("setsockopt TOS (ignored)");
}
}
}
1993-03-21 12:45:37 +03:00
(void)setuid(uid);
1997-01-09 09:57:45 +03:00
doit(&smask);
1993-03-21 12:45:37 +03:00
/*NOTREACHED*/
return (0);
1993-03-21 12:45:37 +03:00
}
1997-01-09 09:57:45 +03:00
int
speed(fd)
int fd;
{
struct termios tt;
1993-03-21 12:45:37 +03:00
1997-01-09 09:57:45 +03:00
(void)tcgetattr(fd, &tt);
return ((int)cfgetispeed(&tt));
1997-01-09 09:57:45 +03:00
}
pid_t child;
struct termios deftt;
struct termios nott;
void
doit(smask)
sigset_t *smask;
{
int i;
struct sigaction sa;
for (i = 0; i < NCCS; i++)
nott.c_cc[i] = _POSIX_VDISABLE;
tcgetattr(0, &deftt);
nott.c_cc[VSTART] = deftt.c_cc[VSTART];
nott.c_cc[VSTOP] = deftt.c_cc[VSTOP];
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sa.sa_handler = SIG_IGN;
(void)sigaction(SIGINT, &sa, (struct sigaction *) 0);
1995-03-21 10:58:28 +03:00
setsignal(SIGHUP);
setsignal(SIGQUIT);
1995-05-03 11:13:51 +04:00
mode(1);
1993-03-21 12:45:37 +03:00
child = fork();
if (child == -1) {
1997-01-09 09:57:45 +03:00
warn("fork");
1993-03-21 12:45:37 +03:00
done(1);
}
if (child == 0) {
1997-01-09 09:57:45 +03:00
mode(1);
if (reader(smask) == 0) {
1993-03-21 12:45:37 +03:00
msg("connection closed.");
exit(0);
}
sleep(1);
1995-05-03 11:13:51 +04:00
msg("\aconnection closed.");
1993-03-21 12:45:37 +03:00
exit(1);
}
/*
* We may still own the socket, and may have a pending SIGURG (or might
1995-03-21 10:58:28 +03:00
* 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.
1993-03-21 12:45:37 +03:00
*/
1997-01-09 09:57:45 +03:00
(void)sigprocmask(SIG_SETMASK, smask, (sigset_t *) 0);
sa.sa_handler = catch_child;
(void)sigaction(SIGCHLD, &sa, (struct sigaction *) 0);
1993-03-21 12:45:37 +03:00
writer();
msg("closed connection.");
done(0);
}
/* trap a signal, unless it is being ignored. */
1995-03-21 10:58:28 +03:00
void
setsignal(sig)
1993-03-21 12:45:37 +03:00
int sig;
{
1997-01-09 09:57:45 +03:00
struct sigaction sa;
sigset_t sigs;
1993-03-21 12:45:37 +03:00
1997-01-09 09:57:45 +03:00
sigemptyset(&sigs);
sigaddset(&sigs, sig);
sigprocmask(SIG_BLOCK, &sigs, &sigs);
sigemptyset(&sa.sa_mask);
sa.sa_handler = exit;
sa.sa_flags = SA_RESTART;
(void)sigaction(sig, &sa, &sa);
if (sa.sa_handler == SIG_IGN)
(void)sigaction(sig, &sa, (struct sigaction *) 0);
(void)sigprocmask(SIG_SETMASK, &sigs, (sigset_t *) 0);
1993-03-21 12:45:37 +03:00
}
void
1993-03-21 12:45:37 +03:00
done(status)
int status;
{
1997-01-09 09:57:45 +03:00
pid_t w;
int wstatus;
struct sigaction sa;
1993-03-21 12:45:37 +03:00
mode(0);
if (child > 0) {
/* make sure catch_child does not snap it up */
1997-01-09 09:57:45 +03:00
sigemptyset(&sa.sa_mask);
sa.sa_handler = SIG_DFL;
sa.sa_flags = 0;
(void)sigaction(SIGCHLD, &sa, (struct sigaction *) 0);
1993-03-21 12:45:37 +03:00
if (kill(child, SIGKILL) >= 0)
1997-01-09 09:57:45 +03:00
while ((w = wait(&wstatus)) > 0 && w != child)
continue;
1993-03-21 12:45:37 +03:00
}
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.
*/
void
1995-03-21 10:58:28 +03:00
writeroob(signo)
int signo;
1993-03-21 12:45:37 +03:00
{
1997-01-09 09:57:45 +03:00
struct sigaction sa;
1993-03-21 12:45:37 +03:00
if (dosigwinch == 0) {
sendwindow();
1997-01-09 09:57:45 +03:00
sigemptyset(&sa.sa_mask);
sa.sa_handler = sigwinch;
sa.sa_flags = SA_RESTART;
(void)sigaction(SIGWINCH, &sa, (struct sigaction *) 0);
1993-03-21 12:45:37 +03:00
}
dosigwinch = 1;
}
void
1995-03-21 10:58:28 +03:00
catch_child(signo)
int signo;
1993-03-21 12:45:37 +03:00
{
1997-01-09 09:57:45 +03:00
int status;
pid_t pid;
1993-03-21 12:45:37 +03:00
for (;;) {
1997-01-09 09:57:45 +03:00
pid = waitpid(-1, &status, WNOHANG|WUNTRACED);
1993-03-21 12:45:37 +03:00
if (pid == 0)
return;
/* if the child (reader) dies, just quit */
1995-03-21 10:58:28 +03:00
if (pid < 0 || (pid == child && !WIFSTOPPED(status)))
1997-01-09 09:57:45 +03:00
done(WEXITSTATUS(status) | WTERMSIG(status));
1993-03-21 12:45:37 +03:00
}
/* NOTREACHED */
}
/*
* writer: write to remote: 0 -> line.
* ~. terminate
* ~^Z suspend rlogin process.
* ~<delayed-suspend char> suspend rlogin process, but leave reader alone.
*/
1995-03-21 10:58:28 +03:00
void
1993-03-21 12:45:37 +03:00
writer()
{
int bol, local, n;
1993-03-21 12:45:37 +03: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;
1995-05-03 11:13:51 +04:00
if (c == '.' || CCEQ(deftty.c_cc[VEOF], c)) {
echo((int)c);
1993-03-21 12:45:37 +03:00
break;
}
1995-05-03 11:36:59 +04:00
if (CCEQ(deftty.c_cc[VSUSP], c)) {
1993-03-21 12:45:37 +03:00
bol = 1;
echo((int)c);
1995-05-03 11:36:59 +04:00
stop(1);
continue;
}
if (CCEQ(deftty.c_cc[VDSUSP], c)) {
bol = 1;
echo((int)c);
1995-05-03 11:36:59 +04:00
stop(0);
1993-03-21 12:45:37 +03:00
continue;
}
if (c != escapechar) {
1993-03-21 12:45:37 +03:00
#ifdef KERBEROS
if (use_kerberos)
(void)kstream_write(krem,
(char *)&escapechar, 1);
else
1993-03-21 12:45:37 +03:00
#endif
(void)write(rem, &escapechar, 1);
}
1993-03-21 12:45:37 +03:00
}
#ifdef KERBEROS
if (use_kerberos) {
if (kstream_write(krem, &c, 1) == 0) {
msg("line gone");
break;
}
1997-01-09 23:18:21 +03:00
}
else
1993-03-21 12:45:37 +03:00
#endif
if (write(rem, &c, 1) == 0) {
msg("line gone");
break;
}
1995-05-03 11:13:51 +04: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) ||
1993-03-21 12:45:37 +03:00
c == '\r' || c == '\n';
}
}
1995-03-21 10:58:28 +03:00
void
echo(i)
int i;
1993-03-21 12:45:37 +03:00
{
char c = (char)i;
char *p;
1993-03-21 12:45:37 +03: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);
}
1995-03-21 10:58:28 +03:00
void
1995-05-03 11:36:59 +04:00
stop(all)
int all;
1993-03-21 12:45:37 +03:00
{
1997-01-09 09:57:45 +03:00
struct sigaction sa;
1993-03-21 12:45:37 +03:00
mode(0);
1997-01-09 09:57:45 +03:00
sigemptyset(&sa.sa_mask);
sa.sa_handler = SIG_IGN;
sa.sa_flags = SA_RESTART;
(void)sigaction(SIGCHLD, &sa, (struct sigaction *) 0);
1995-05-03 11:36:59 +04:00
(void)kill(all ? 0 : getpid(), SIGTSTP);
1997-01-09 09:57:45 +03:00
sa.sa_handler = catch_child;
(void)sigaction(SIGCHLD, &sa, (struct sigaction *) 0);
1993-03-21 12:45:37 +03:00
mode(1);
1995-03-21 10:58:28 +03:00
sigwinch(0); /* check for size changes */
1993-03-21 12:45:37 +03:00
}
void
1995-03-21 10:58:28 +03:00
sigwinch(signo)
int signo;
1993-03-21 12:45:37 +03:00
{
struct winsize ws;
if (dosigwinch && get_window_size(0, &ws) == 0 &&
1997-01-09 09:57:45 +03:00
memcmp(&ws, &winsize, sizeof(ws))) {
1993-03-21 12:45:37 +03:00
winsize = ws;
sendwindow();
}
}
/*
* Send the window size to the server via the magic escape
*/
1995-03-21 10:58:28 +03:00
void
1993-03-21 12:45:37 +03:00
sendwindow()
{
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);
#ifdef KERBEROS
if (use_kerberos)
(void)kstream_write(krem, obuf, sizeof(obuf));
else
1993-03-21 12:45:37 +03:00
#endif
(void)write(rem, obuf, sizeof(obuf));
}
/*
* reader: read from remote: line -> 1
*/
#define READING 1
#define WRITING 2
jmp_buf rcvtop;
1997-01-09 09:57:45 +03:00
pid_t ppid;
int rcvcnt, rcvstate;
1993-03-21 12:45:37 +03:00
char rcvbuf[8 * 1024];
void
1995-03-21 10:58:28 +03:00
oob(signo)
int signo;
1993-03-21 12:45:37 +03:00
{
1995-05-03 11:13:51 +04:00
struct termios tty;
int atmark, n, rcvd;
1993-03-21 12:45:37 +03:00
char waste[BUFSIZ], mark;
rcvd = 0;
1995-03-21 10:58:28 +03:00
while (recv(rem, &mark, 1, MSG_OOB) < 0) {
1993-03-21 12:45:37 +03:00
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.
*/
if (rcvcnt < sizeof(rcvbuf)) {
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;
1995-03-21 10:58:28 +03:00
}
1993-03-21 12:45:37 +03:00
}
if (mark & TIOCPKT_WINDOW) {
/* Let server know about window size changes */
(void)kill(ppid, SIGUSR1);
}
if (!eight && (mark & TIOCPKT_NOSTOP)) {
1995-05-03 11:13:51 +04:00
(void)tcgetattr(0, &tty);
tty.c_iflag &= ~IXON;
(void)tcsetattr(0, TCSANOW, &tty);
1993-03-21 12:45:37 +03:00
}
if (!eight && (mark & TIOCPKT_DOSTOP)) {
1995-05-03 11:13:51 +04:00
(void)tcgetattr(0, &tty);
tty.c_iflag |= (deftty.c_iflag & IXON);
(void)tcsetattr(0, TCSANOW, &tty);
1993-03-21 12:45:37 +03:00
}
if (mark & TIOCPKT_FLUSHWRITE) {
(void)tcflush(1, TCIOFLUSH);
1993-03-21 12:45:37 +03:00
for (;;) {
if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
1997-01-09 09:57:45 +03:00
warn("ioctl SIOCATMARK (ignored)");
1993-03-21 12:45:37 +03: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 */
1995-03-21 10:58:28 +03:00
int
1997-01-09 09:57:45 +03:00
reader(smask)
sigset_t *smask;
1993-03-21 12:45:37 +03:00
{
1997-01-09 09:57:45 +03:00
pid_t pid;
int n, remaining;
1995-03-21 10:58:28 +03:00
char *bufp;
1997-01-09 09:57:45 +03:00
struct sigaction sa;
1993-03-21 12:45:37 +03:00
1995-03-21 10:58:28 +03:00
pid = getpid(); /* modern systems use positives for pid */
1997-01-09 09:57:45 +03:00
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sa.sa_handler = SIG_IGN;
(void)sigaction(SIGTTOU, &sa, (struct sigaction *) 0);
sa.sa_handler = oob;
(void)sigaction(SIGURG, &sa, (struct sigaction *) 0);
1993-03-21 12:45:37 +03:00
ppid = getppid();
(void)fcntl(rem, F_SETOWN, pid);
(void)setjmp(rcvtop);
1997-01-09 09:57:45 +03:00
(void)sigprocmask(SIG_SETMASK, smask, (sigset_t *) 0);
1995-03-21 10:58:28 +03:00
bufp = rcvbuf;
1993-03-21 12:45:37 +03:00
for (;;) {
while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
rcvstate = WRITING;
n = write(STDOUT_FILENO, bufp, remaining);
if (n < 0) {
if (errno != EINTR)
1995-03-21 10:58:28 +03:00
return (-1);
1993-03-21 12:45:37 +03:00
continue;
}
bufp += n;
}
bufp = rcvbuf;
rcvcnt = 0;
rcvstate = READING;
#ifdef KERBEROS
if (use_kerberos)
rcvcnt = kstream_read(krem, rcvbuf, sizeof(rcvbuf));
else
1993-03-21 12:45:37 +03:00
#endif
rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
1993-03-21 12:45:37 +03:00
if (rcvcnt == 0)
return (0);
if (rcvcnt < 0) {
if (errno == EINTR)
continue;
1997-01-09 09:57:45 +03:00
warn("read");
1995-03-21 10:58:28 +03:00
return (-1);
1993-03-21 12:45:37 +03:00
}
}
}
1995-03-21 10:58:28 +03:00
void
1993-03-21 12:45:37 +03:00
mode(f)
1995-03-21 10:58:28 +03:00
int f;
1993-03-21 12:45:37 +03:00
{
1995-05-03 11:13:51 +04:00
struct termios tty;
switch (f) {
1993-03-21 12:45:37 +03:00
case 0:
1995-05-03 11:13:51 +04:00
(void)tcsetattr(0, TCSANOW, &deftty);
1993-03-21 12:45:37 +03:00
break;
case 1:
1995-05-03 11:13:51 +04:00
(void)tcgetattr(0, &deftty);
tty = deftty;
1995-05-17 17:35:50 +04:00
/* This is loosely derived from sys/compat/tty_compat.c. */
tty.c_lflag &= ~(ECHO|ICANON|ISIG|IEXTEN);
1995-05-03 11:13:51 +04:00
tty.c_iflag &= ~ICRNL;
tty.c_oflag &= ~OPOST;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 0;
1995-05-03 11:13:51 +04:00
if (eight) {
tty.c_iflag &= IXOFF;
tty.c_cflag &= ~(CSIZE|PARENB);
tty.c_cflag |= CS8;
}
(void)tcsetattr(0, TCSANOW, &tty);
1993-03-21 12:45:37 +03:00
break;
1997-01-09 09:57:45 +03:00
1993-03-21 12:45:37 +03:00
default:
return;
}
}
void
1995-03-21 10:58:28 +03:00
lostpeer(signo)
int signo;
1993-03-21 12:45:37 +03:00
{
1997-01-09 09:57:45 +03:00
struct sigaction sa;
sa.sa_flags = SA_RESTART;
sa.sa_handler = SIG_IGN;
(void)sigaction(SIGPIPE, &sa, (struct sigaction *)0);
1995-05-03 11:13:51 +04:00
msg("\aconnection closed.");
1993-03-21 12:45:37 +03:00
done(1);
}
/* copy SIGURGs to the child process. */
void
1995-03-21 10:58:28 +03:00
copytochild(signo)
int signo;
1993-03-21 12:45:37 +03:00
{
1997-01-09 09:57:45 +03:00
1993-03-21 12:45:37 +03:00
(void)kill(child, SIGURG);
}
1995-03-21 10:58:28 +03:00
void
1993-03-21 12:45:37 +03:00
msg(str)
char *str;
{
1997-01-09 09:57:45 +03:00
1993-03-21 12:45:37 +03:00
(void)fprintf(stderr, "rlogin: %s\r\n", str);
}
#ifdef KERBEROS
/* VARARGS */
1995-03-21 10:58:28 +03:00
void
#if __STDC__
warning(const char *fmt, ...)
#else
warning(fmt, va_alist)
char *fmt;
va_dcl
#endif
1993-03-21 12:45:37 +03:00
{
va_list ap;
(void)fprintf(stderr, "rlogin: warning, using standard rlogin: ");
1995-03-21 10:58:28 +03:00
#ifdef __STDC__
va_start(ap, fmt);
#else
1993-03-21 12:45:37 +03:00
va_start(ap);
1995-03-21 10:58:28 +03:00
#endif
1993-03-21 12:45:37 +03:00
vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fprintf(stderr, ".\n");
}
#endif
void
1993-03-21 12:45:37 +03:00
usage()
{
(void)fprintf(stderr,
1997-01-09 09:57:45 +03:00
"usage: rlogin [ -%s]%s[-e char] [ -l username ] [username@]host\n",
1993-03-21 12:45:37 +03:00
#ifdef KERBEROS
#ifdef CRYPT
"8EKLdx", " [-k realm] ");
1993-03-21 12:45:37 +03:00
#else
"8EKLd", " [-k realm] ");
1993-03-21 12:45:37 +03:00
#endif
#else
"8ELd", " ");
1993-03-21 12:45:37 +03:00
#endif
exit(1);
}
/*
1995-03-21 10:58:28 +03:00
* The following routine provides compatibility (such as it is) between older
1993-03-21 12:45:37 +03:00
* Suns and others. Suns have only a `ttysize', so we convert it to a winsize.
*/
1995-03-21 10:58:28 +03:00
#ifdef OLDSUN
int
1993-03-21 12:45:37 +03:00
get_window_size(fd, wp)
int fd;
struct winsize *wp;
{
struct ttysize ts;
int error;
if ((error = ioctl(0, TIOCGSIZE, &ts)) != 0)
1995-03-21 10:58:28 +03:00
return (error);
1993-03-21 12:45:37 +03:00
wp->ws_row = ts.ts_lines;
wp->ws_col = ts.ts_cols;
wp->ws_xpixel = 0;
wp->ws_ypixel = 0;
1995-03-21 10:58:28 +03:00
return (0);
1993-03-21 12:45:37 +03:00
}
#endif
1995-03-21 10:58:28 +03:00
u_int
1993-03-21 12:45:37 +03:00
getescape(p)
char *p;
1993-03-21 12:45:37 +03:00
{
long val;
int len;
if ((len = strlen(p)) == 1) /* use any single char, including '\' */
1995-03-21 10:58:28 +03:00
return ((u_int)*p);
1993-03-21 12:45:37 +03:00
/* otherwise, \nnn */
if (*p == '\\' && len >= 2 && len <= 4) {
1995-03-21 10:58:28 +03:00
val = strtol(++p, NULL, 8);
1993-03-21 12:45:37 +03:00
for (;;) {
if (!*++p)
1995-03-21 10:58:28 +03:00
return ((u_int)val);
1993-03-21 12:45:37 +03:00
if (*p < '0' || *p > '8')
break;
}
}
msg("illegal option value -- e");
usage();
/* NOTREACHED */
return (0);
1993-03-21 12:45:37 +03:00
}