#if 0
#ifndef lint
static const char copyright[] =
"@(#) Copyright (c) 1983, 1988, 1989, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif
#ifndef lint
static const char sccsid[] = "@(#)rlogind.c 8.1 (Berkeley) 6/4/93";
#endif
#endif
#include <sys/cdefs.h>
__FBSDID("$FreeBSD: src/libexec/rlogind/rlogind.c,v 1.44 2005/05/13 16:31:09 ume Exp $");
#define FD_SETSIZE 16
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <signal.h>
#ifdef __APPLE__
#include <sys/termios.h>
#else
#include <termios.h>
#endif
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#ifndef __APPLE__
#include <libutil.h>
#else
#include <util.h>
#endif
#include <paths.h>
#include <pwd.h>
#include <syslog.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef __APPLE__
#include "pathnames.h"
#endif
#ifndef TIOCPKT_WINDOW
#define TIOCPKT_WINDOW 0x80
#endif
#if defined(KERBEROS)
#include <kerberosIV/des.h>
#include <kerberosIV/krb.h>
#define SECURE_MESSAGE "This rlogin session is using DES encryption for all transmissions.\r\n"
AUTH_DAT *kdata;
KTEXT ticket;
u_char auth_buf[sizeof(AUTH_DAT)];
u_char tick_buf[sizeof(KTEXT_ST)];
Key_schedule schedule;
int doencrypt, retval, use_kerberos, vacuous;
#define ARGSTR "alnkvx"
#else
#define ARGSTR "Daln"
#endif
char *env[2];
#define NMAX 30
char lusername[NMAX+1], rusername[NMAX+1];
static char term[64] = "TERM=";
#define ENVSIZE (sizeof("TERM=")-1)
int keepalive = 1;
int check_all = 0;
int no_delay;
struct passwd *pwd;
union sockunion {
struct sockinet {
u_char si_len;
u_char si_family;
u_short si_port;
} su_si;
struct sockaddr_in su_sin;
struct sockaddr_in6 su_sin6;
};
#define su_len su_si.si_len
#define su_family su_si.si_family
#define su_port su_si.si_port
void doit(int, union sockunion *);
int control(int, char *, int);
void protocol(int, int);
void cleanup(int);
void fatal(int, char *, int);
int do_rlogin(union sockunion *);
void getstr(char *, int, char *);
void setup_term(int);
int do_krb_login(struct sockaddr_in *);
void usage(void);
#if defined(KERBEROS)
int local_domain(char *);
char *topdomain(char *);
#endif
int
main(int argc, char *argv[])
{
extern int __check_rhosts_file;
union sockunion from;
socklen_t fromlen;
int ch, on;
openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH);
opterr = 0;
while ((ch = getopt(argc, argv, ARGSTR)) != -1)
switch (ch) {
case 'D':
no_delay = 1;
break;
case 'a':
check_all = 1;
break;
case 'l':
__check_rhosts_file = 0;
break;
case 'n':
keepalive = 0;
break;
#if defined(KERBEROS)
case 'k':
use_kerberos = 1;
break;
case 'v':
vacuous = 1;
break;
#if defined(CRYPT)
case 'x':
doencrypt = 1;
break;
#endif
#endif
case '?':
default:
usage();
break;
}
argc -= optind;
argv += optind;
#if defined(KERBEROS)
if (use_kerberos && vacuous) {
usage();
fatal(STDERR_FILENO, "only one of -k and -v allowed", 0);
}
#endif
fromlen = sizeof (from);
if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
syslog(LOG_ERR,"Can't get peer name of remote host: %m");
fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
}
on = 1;
if (keepalive &&
setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
if (no_delay &&
setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
if (from.su_family == AF_INET)
{
on = IPTOS_LOWDELAY;
if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
}
doit(0, &from);
return 0;
}
int child;
int netf;
char line[MAXPATHLEN];
int confirmed;
struct winsize win = { 0, 0, 0, 0 };
void
doit(int f, union sockunion *fromp)
{
int master, pid, on = 1;
int authenticated = 0;
char hostname[2 * MAXHOSTNAMELEN + 1];
char nameinfo[2 * INET6_ADDRSTRLEN + 1];
char c;
alarm(60);
read(f, &c, 1);
if (c != 0)
exit(1);
#if defined(KERBEROS)
if (vacuous)
fatal(f, "Remote host requires Kerberos authentication", 0);
#endif
alarm(0);
#ifdef __APPLE__
struct hostent* hp;
fromp->su_port = ntohs((u_short)fromp->su_port);
hp = gethostbyaddr((char *)&fromp->su_sin.sin_addr,
sizeof(struct in_addr), fromp->su_family);
if (hp)
(void)strcpy(hostname, hp->h_name);
else
(void)strcpy(hostname, inet_ntoa(fromp->su_sin.sin_addr));
#else
realhostname_sa(hostname, sizeof(hostname) - 1,
(struct sockaddr *)fromp, fromp->su_len);
fromp->su_port = ntohs((u_short)fromp->su_port);
hostname[sizeof(hostname) - 1] = '\0';
#endif
#if defined(KERBEROS)
if (use_kerberos) {
retval = do_krb_login(fromp);
if (retval == 0)
authenticated++;
else if (retval > 0)
fatal(f, krb_err_txt[retval], 0);
write(f, &c, 1);
confirmed = 1;
} else
#endif
{
if ((fromp->su_family != AF_INET
&& fromp->su_family != AF_INET6
) ||
fromp->su_port >= IPPORT_RESERVED ||
fromp->su_port < IPPORT_RESERVED/2) {
getnameinfo((struct sockaddr *)fromp,
fromp->su_len,
nameinfo, sizeof(nameinfo), NULL, 0,
NI_NUMERICHOST);
syslog(LOG_NOTICE, "Connection from %s on illegal port",
nameinfo);
fatal(f, "Permission denied", 0);
}
#ifdef IP_OPTIONS
if (fromp->su_family == AF_INET)
{
u_char optbuf[BUFSIZ/3];
socklen_t optsize = sizeof(optbuf);
int ipproto, i;
struct protoent *ip;
if ((ip = getprotobyname("ip")) != NULL)
ipproto = ip->p_proto;
else
ipproto = IPPROTO_IP;
if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
&optsize) == 0 && optsize != 0) {
for (i = 0; i < optsize; ) {
u_char c = optbuf[i];
if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
syslog(LOG_NOTICE,
"Connection refused from %s with IP option %s",
inet_ntoa(fromp->su_sin.sin_addr),
c == IPOPT_LSRR ? "LSRR" : "SSRR");
exit(1);
}
if (c == IPOPT_EOL)
break;
i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
}
}
}
#endif
if (do_rlogin(fromp) == 0)
authenticated++;
}
if (confirmed == 0) {
write(f, "", 1);
confirmed = 1;
}
#if defined(KERBEROS) && defined(CRYPT)
if (doencrypt)
(void) des_write(f, SECURE_MESSAGE, sizeof(SECURE_MESSAGE) - 1);
#endif
netf = f;
pid = forkpty(&master, line, NULL, &win);
if (pid < 0) {
if (errno == ENOENT)
fatal(f, "Out of ptys", 0);
else
fatal(f, "Forkpty", 1);
}
if (pid == 0) {
if (f > 2)
(void) close(f);
setup_term(0);
#ifndef __APPLE__
if (*lusername=='-') {
syslog(LOG_ERR, "tried to pass user \"%s\" to login",
lusername);
fatal(STDERR_FILENO, "invalid user", 0);
}
#endif
if (authenticated) {
#if defined(KERBEROS)
if (use_kerberos && (pwd->pw_uid == 0))
syslog(LOG_INFO|LOG_AUTH,
"ROOT Kerberos login from %s.%s@%s on %s\n",
kdata->pname, kdata->pinst, kdata->prealm,
hostname);
#endif
#ifdef __APPLE__
execle(_PATH_LOGIN, "login", "-p",
"-h", hostname, "-f", "--", lusername, NULL, env);
#else
execl(_PATH_LOGIN, "login", "-p",
"-h", hostname, "-f", lusername, (char *)NULL);
#endif
} else
#ifdef __APPLE__
execle(_PATH_LOGIN, "login", "-p",
"-h", hostname, "--", lusername, NULL, env);
#else
execl(_PATH_LOGIN, "login", "-p",
"-h", hostname, lusername, (char *)NULL);
#endif
fatal(STDERR_FILENO, _PATH_LOGIN, 1);
}
#if defined(KERBEROS) && defined(CRYPT)
if (!doencrypt)
#endif
ioctl(f, FIONBIO, &on);
ioctl(master, FIONBIO, &on);
ioctl(master, TIOCPKT, &on);
signal(SIGCHLD, cleanup);
protocol(f, master);
signal(SIGCHLD, SIG_IGN);
cleanup(0);
}
char magic[2] = { 0377, 0377 };
char oobdata[] = {TIOCPKT_WINDOW};
int
control(int pty, char *cp, int n)
{
struct winsize w;
if (n < 4 + (int)sizeof(w) || cp[2] != 's' || cp[3] != 's')
return (0);
oobdata[0] &= ~TIOCPKT_WINDOW;
bcopy(cp+4, (char *)&w, sizeof(w));
w.ws_row = ntohs(w.ws_row);
w.ws_col = ntohs(w.ws_col);
w.ws_xpixel = ntohs(w.ws_xpixel);
w.ws_ypixel = ntohs(w.ws_ypixel);
(void)ioctl(pty, TIOCSWINSZ, &w);
return (4+sizeof (w));
}
void
protocol(int f, int p)
{
char pibuf[1024+1], fibuf[1024], *pbp = NULL, *fbp = NULL;
int pcc = 0, fcc = 0;
int cc, nfd, n;
char cntl;
(void) signal(SIGTTOU, SIG_IGN);
send(f, oobdata, 1, MSG_OOB);
if (f > p)
nfd = f + 1;
else
nfd = p + 1;
if (nfd > FD_SETSIZE) {
syslog(LOG_ERR, "select mask too small, increase FD_SETSIZE");
fatal(f, "internal error (select mask too small)", 0);
}
for (;;) {
fd_set ibits, obits, ebits, *omask;
FD_ZERO(&ebits);
FD_ZERO(&ibits);
FD_ZERO(&obits);
omask = (fd_set *)NULL;
if (fcc) {
FD_SET(p, &obits);
omask = &obits;
} else
FD_SET(f, &ibits);
if (pcc >= 0) {
if (pcc) {
FD_SET(f, &obits);
omask = &obits;
} else
FD_SET(p, &ibits);
}
FD_SET(p, &ebits);
if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) {
if (errno == EINTR)
continue;
fatal(f, "select", 1);
}
if (n == 0) {
sleep(5);
continue;
}
#define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
if (FD_ISSET(p, &ebits)) {
cc = read(p, &cntl, 1);
if (cc == 1 && pkcontrol(cntl)) {
cntl |= oobdata[0];
send(f, &cntl, 1, MSG_OOB);
if (cntl & TIOCPKT_FLUSHWRITE) {
pcc = 0;
FD_CLR(p, &ibits);
}
}
}
if (FD_ISSET(f, &ibits)) {
#if defined(KERBEROS) && defined(CRYPT)
if (doencrypt)
fcc = des_read(f, fibuf, sizeof(fibuf));
else
#endif
fcc = read(f, fibuf, sizeof(fibuf));
if (fcc < 0 && errno == EWOULDBLOCK)
fcc = 0;
else {
char *cp;
int left, n;
if (fcc <= 0)
break;
fbp = fibuf;
top:
for (cp = fibuf; cp < fibuf+fcc-1; cp++)
if (cp[0] == magic[0] &&
cp[1] == magic[1]) {
left = fcc - (cp-fibuf);
n = control(p, cp, left);
if (n) {
left -= n;
if (left > 0)
bcopy(cp+n, cp, left);
fcc -= n;
goto top;
}
}
FD_SET(p, &obits);
}
}
if (FD_ISSET(p, &obits) && fcc > 0) {
cc = write(p, fbp, fcc);
if (cc > 0) {
fcc -= cc;
fbp += cc;
}
}
if (FD_ISSET(p, &ibits)) {
pcc = read(p, pibuf, sizeof (pibuf));
pbp = pibuf;
if (pcc < 0 && errno == EWOULDBLOCK)
pcc = 0;
else if (pcc <= 0)
break;
else if (pibuf[0] == 0) {
pbp++, pcc--;
#if defined(KERBEROS) && defined(CRYPT)
if (!doencrypt)
#endif
FD_SET(f, &obits);
} else {
if (pkcontrol(pibuf[0])) {
pibuf[0] |= oobdata[0];
send(f, &pibuf[0], 1, MSG_OOB);
}
pcc = 0;
}
}
if ((FD_ISSET(f, &obits)) && pcc > 0) {
#if defined(KERBEROS) && defined(CRYPT)
if (doencrypt)
cc = des_write(f, pbp, pcc);
else
#endif
cc = write(f, pbp, pcc);
if (cc < 0 && errno == EWOULDBLOCK) {
if (!FD_ISSET(p, &ibits))
sleep(5);
continue;
}
if (cc > 0) {
pcc -= cc;
pbp += cc;
}
}
}
}
void
cleanup(int signo)
{
#ifndef __APPLE__
char *p;
p = line + sizeof(_PATH_DEV) - 1;
if (logout(p))
logwtmp(p, "", "");
(void)chflags(line, 0);
(void)chmod(line, 0666);
(void)chown(line, 0, 0);
*p = 'p';
(void)chflags(line, 0);
(void)chmod(line, 0666);
(void)chown(line, 0, 0);
#endif
shutdown(netf, SHUT_RDWR);
exit(1);
}
void
fatal(int f, char *msg, int syserr)
{
int len;
char buf[BUFSIZ], *bp = buf;
if (!confirmed)
*bp++ = '\01';
if (syserr)
len = snprintf(bp, sizeof(buf), "rlogind: %s: %s.\r\n",
msg, strerror(errno));
else
len = snprintf(bp, sizeof(buf), "rlogind: %s.\r\n", msg);
if (len < 0)
len = 0;
(void) write(f, buf, bp + len - buf);
exit(1);
}
int
do_rlogin(union sockunion *dest)
{
getstr(rusername, sizeof(rusername), "remuser too long");
getstr(lusername, sizeof(lusername), "locuser too long");
getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
pwd = getpwnam(lusername);
if (pwd == NULL)
#ifdef __APPLE__
{
syslog(LOG_ERR,
"rlogin denied for user %s: getpwnam() failed\n",
lusername);
return (-1);
}
#else
return (-1);
#endif
#ifdef __APPLE__
if (pwd->pw_uid == 0 && strcmp("root", lusername))
{
syslog(LOG_ALERT,
"rlogin denied for non-root user %s with uid 0\n",
lusername);
return (-1);
}
#endif
return (iruserok_sa(dest, dest->su_len, pwd->pw_uid == 0, rusername,
lusername));
}
void
getstr(char *buf, int cnt, char *errmsg)
{
char c;
do {
if (read(STDIN_FILENO, &c, 1) != 1)
exit(1);
if (--cnt < 0)
fatal(STDOUT_FILENO, errmsg, 0);
*buf++ = c;
} while (c != 0);
}
extern char **environ;
void
setup_term(int fd)
{
char *cp = index(term+ENVSIZE, '/');
char *speed;
struct termios tt;
#ifndef notyet
tcgetattr(fd, &tt);
if (cp) {
*cp++ = '\0';
speed = cp;
cp = index(speed, '/');
if (cp)
*cp++ = '\0';
cfsetspeed(&tt, atoi(speed));
}
tt.c_iflag = TTYDEF_IFLAG;
tt.c_oflag = TTYDEF_OFLAG;
tt.c_lflag = TTYDEF_LFLAG;
tcsetattr(fd, TCSAFLUSH, &tt);
#else
if (cp) {
*cp++ = '\0';
speed = cp;
cp = index(speed, '/');
if (cp)
*cp++ = '\0';
tcgetattr(fd, &tt);
cfsetspeed(&tt, atoi(speed));
tcsetattr(fd, TCSAFLUSH, &tt);
}
#endif
env[0] = term;
env[1] = 0;
environ = env;
}
#if defined(KERBEROS)
#define VERSION_SIZE 9
int
do_krb_login(dest)
struct sockaddr_in *dest;
{
int rc;
char instance[INST_SZ], version[VERSION_SIZE];
long authopts = 0L;
struct sockaddr_in faddr;
kdata = (AUTH_DAT *) auth_buf;
ticket = (KTEXT) tick_buf;
instance[0] = '*';
instance[1] = '\0';
#if defined(CRYPT)
if (doencrypt) {
rc = sizeof(faddr);
if (getsockname(0, (struct sockaddr *)&faddr, &rc))
return (-1);
authopts = KOPT_DO_MUTUAL;
rc = krb_recvauth(
authopts, 0,
ticket, "rcmd",
instance, dest, &faddr,
kdata, "", schedule, version);
des_set_key(kdata->session, schedule);
} else
#endif
rc = krb_recvauth(
authopts, 0,
ticket, "rcmd",
instance, dest, (struct sockaddr_in *) 0,
kdata, "", (bit_64 *) 0, version);
if (rc != KSUCCESS)
return (rc);
getstr(lusername, sizeof(lusername), "locuser");
getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type");
pwd = getpwnam(lusername);
if (pwd == NULL)
return (-1);
if (kuserok(kdata, lusername) != 0)
return (-1);
return (0);
}
#endif
void
usage(void)
{
#if defined(KERBEROS)
syslog(LOG_ERR, "usage: rlogind [-aln] [-k | -v]");
#else
syslog(LOG_ERR, "usage: rlogind [-" ARGSTR "]");
#endif
}
#if defined(KERBEROS)
int
local_domain(h)
char *h;
{
char localhost[MAXHOSTNAMELEN];
char *p1, *p2;
localhost[0] = 0;
(void) gethostname(localhost, sizeof(localhost));
p1 = topdomain(localhost);
p2 = topdomain(h);
if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
return (1);
return (0);
}
char *
topdomain(h)
char *h;
{
char *p;
char *maybe = NULL;
int dots = 0;
for (p = h + strlen(h); p >= h; p--) {
if (*p == '.') {
if (++dots == 2)
return (p);
maybe = p;
}
}
return (maybe);
}
#endif