Home | History | Annotate | Line # | Download | only in rlogind
rlogind.c revision 1.2
      1 /*-
      2  * Copyright (c) 1983, 1988, 1989 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      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.
     20  *
     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
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 char copyright[] =
     36 "@(#) Copyright (c) 1983, 1988, 1989 The Regents of the University of California.\n\
     37  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 /*static char sccsid[] = "from: @(#)rlogind.c	5.53 (Berkeley) 4/20/91";*/
     42 static char rcsid[] = "$Id: rlogind.c,v 1.2 1993/08/01 18:29:55 mycroft Exp $";
     43 #endif /* not lint */
     44 
     45 #ifdef KERBEROS
     46 /* From:
     47  *	$Source: /tank/opengrok/rsync2/NetBSD/src/libexec/rlogind/rlogind.c,v $
     48  *	$Header: /tank/opengrok/rsync2/NetBSD/src/libexec/rlogind/rlogind.c,v 1.2 1993/08/01 18:29:55 mycroft Exp $
     49  */
     50 #endif
     51 
     52 /*
     53  * remote login server:
     54  *	\0
     55  *	remuser\0
     56  *	locuser\0
     57  *	terminal_type/speed\0
     58  *	data
     59  */
     60 
     61 #define	FD_SETSIZE	16		/* don't need many bits for select */
     62 #include <sys/param.h>
     63 #include <sys/stat.h>
     64 #include <sys/ioctl.h>
     65 #include <signal.h>
     66 #include <termios.h>
     67 
     68 #include <sys/socket.h>
     69 #include <netinet/in.h>
     70 #include <netinet/in_systm.h>
     71 #include <netinet/ip.h>
     72 #include <arpa/inet.h>
     73 #include <netdb.h>
     74 
     75 #include <pwd.h>
     76 #include <syslog.h>
     77 #include <errno.h>
     78 #include <stdio.h>
     79 #include <unistd.h>
     80 #include <stdlib.h>
     81 #include <string.h>
     82 #include "pathnames.h"
     83 
     84 #ifndef TIOCPKT_WINDOW
     85 #define TIOCPKT_WINDOW 0x80
     86 #endif
     87 
     88 #ifdef	KERBEROS
     89 #include <kerberosIV/des.h>
     90 #include <kerberosIV/krb.h>
     91 #define	SECURE_MESSAGE "This rlogin session is using DES encryption for all transmissions.\r\n"
     92 
     93 AUTH_DAT	*kdata;
     94 KTEXT		ticket;
     95 u_char		auth_buf[sizeof(AUTH_DAT)];
     96 u_char		tick_buf[sizeof(KTEXT_ST)];
     97 Key_schedule	schedule;
     98 int		doencrypt, retval, use_kerberos, vacuous;
     99 
    100 #define		ARGSTR			"alnkvx"
    101 #else
    102 #define		ARGSTR			"aln"
    103 #endif	/* KERBEROS */
    104 
    105 char	*env[2];
    106 #define	NMAX 30
    107 char	lusername[NMAX+1], rusername[NMAX+1];
    108 static	char term[64] = "TERM=";
    109 #define	ENVSIZE	(sizeof("TERM=")-1)	/* skip null for concatenation */
    110 int	keepalive = 1;
    111 int	check_all = 0;
    112 
    113 struct	passwd *pwd;
    114 
    115 main(argc, argv)
    116 	int argc;
    117 	char **argv;
    118 {
    119 	extern int opterr, optind;
    120 	extern int _check_rhosts_file;
    121 	int ch;
    122 	int on = 1, fromlen;
    123 	struct sockaddr_in from;
    124 
    125 	openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH);
    126 
    127 	opterr = 0;
    128 	while ((ch = getopt(argc, argv, ARGSTR)) != EOF)
    129 		switch (ch) {
    130 		case 'a':
    131 			check_all = 1;
    132 			break;
    133 		case 'l':
    134 			_check_rhosts_file = 0;
    135 			break;
    136 		case 'n':
    137 			keepalive = 0;
    138 			break;
    139 #ifdef KERBEROS
    140 		case 'k':
    141 			use_kerberos = 1;
    142 			break;
    143 		case 'v':
    144 			vacuous = 1;
    145 			break;
    146 #ifdef CRYPT
    147 		case 'x':
    148 			doencrypt = 1;
    149 			break;
    150 #endif
    151 #endif
    152 		case '?':
    153 		default:
    154 			usage();
    155 			break;
    156 		}
    157 	argc -= optind;
    158 	argv += optind;
    159 
    160 #ifdef	KERBEROS
    161 	if (use_kerberos && vacuous) {
    162 		usage();
    163 		fatal(STDERR_FILENO, "only one of -k and -v allowed", 0);
    164 	}
    165 #endif
    166 	fromlen = sizeof (from);
    167 	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
    168 		syslog(LOG_ERR,"Can't get peer name of remote host: %m");
    169 		fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
    170 	}
    171 	if (keepalive &&
    172 	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
    173 		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
    174 	on = IPTOS_LOWDELAY;
    175 	if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
    176 		syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
    177 	doit(0, &from);
    178 }
    179 
    180 int	child;
    181 void	cleanup();
    182 int	netf;
    183 char	line[MAXPATHLEN];
    184 int	confirmed;
    185 extern	char	*inet_ntoa();
    186 
    187 struct winsize win = { 0, 0, 0, 0 };
    188 
    189 
    190 doit(f, fromp)
    191 	int f;
    192 	struct sockaddr_in *fromp;
    193 {
    194 	int i, master, pid, on = 1;
    195 	int authenticated = 0, hostok = 0;
    196 	register struct hostent *hp;
    197 	char remotehost[2 * MAXHOSTNAMELEN + 1];
    198 	struct hostent hostent;
    199 	char c;
    200 
    201 	alarm(60);
    202 	read(f, &c, 1);
    203 
    204 	if (c != 0)
    205 		exit(1);
    206 #ifdef	KERBEROS
    207 	if (vacuous)
    208 		fatal(f, "Remote host requires Kerberos authentication", 0);
    209 #endif
    210 
    211 	alarm(0);
    212 	fromp->sin_port = ntohs((u_short)fromp->sin_port);
    213 	hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof(struct in_addr),
    214 		fromp->sin_family);
    215 	if (hp == 0) {
    216 		/*
    217 		 * Only the name is used below.
    218 		 */
    219 		hp = &hostent;
    220 		hp->h_name = inet_ntoa(fromp->sin_addr);
    221 		hostok++;
    222 	} else if (check_all || local_domain(hp->h_name)) {
    223 		/*
    224 		 * If name returned by gethostbyaddr is in our domain,
    225 		 * attempt to verify that we haven't been fooled by someone
    226 		 * in a remote net; look up the name and check that this
    227 		 * address corresponds to the name.
    228 		 */
    229 		strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1);
    230 		remotehost[sizeof(remotehost) - 1] = 0;
    231 		hp = gethostbyname(remotehost);
    232 		if (hp)
    233 		    for (; hp->h_addr_list[0]; hp->h_addr_list++)
    234 			if (!bcmp(hp->h_addr_list[0], (caddr_t)&fromp->sin_addr,
    235 			    sizeof(fromp->sin_addr))) {
    236 				hostok++;
    237 				break;
    238 			}
    239 	} else
    240 		hostok++;
    241 
    242 #ifdef	KERBEROS
    243 	if (use_kerberos) {
    244 		if (!hostok)
    245 			fatal(f, "rlogind: Host address mismatch.", 0);
    246 		retval = do_krb_login(hp->h_name, fromp);
    247 		if (retval == 0)
    248 			authenticated++;
    249 		else if (retval > 0)
    250 			fatal(f, krb_err_txt[retval], 0);
    251 		write(f, &c, 1);
    252 		confirmed = 1;		/* we sent the null! */
    253 	} else
    254 #endif
    255 	{
    256 	    if (fromp->sin_family != AF_INET ||
    257 	        fromp->sin_port >= IPPORT_RESERVED ||
    258 	        fromp->sin_port < IPPORT_RESERVED/2) {
    259 		    syslog(LOG_NOTICE, "Connection from %s on illegal port",
    260 			    inet_ntoa(fromp->sin_addr));
    261 		    fatal(f, "Permission denied", 0);
    262 	    }
    263 #ifdef IP_OPTIONS
    264 	    {
    265 	    u_char optbuf[BUFSIZ/3], *cp;
    266 	    char lbuf[BUFSIZ], *lp;
    267 	    int optsize = sizeof(optbuf), ipproto;
    268 	    struct protoent *ip;
    269 
    270 	    if ((ip = getprotobyname("ip")) != NULL)
    271 		    ipproto = ip->p_proto;
    272 	    else
    273 		    ipproto = IPPROTO_IP;
    274 	    if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
    275 		&optsize) == 0 && optsize != 0) {
    276 		    lp = lbuf;
    277 		    for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
    278 			    sprintf(lp, " %2.2x", *cp);
    279 		    syslog(LOG_NOTICE,
    280 			"Connection received using IP options (ignored):%s",
    281 			lbuf);
    282 		    if (setsockopt(0, ipproto, IP_OPTIONS,
    283 			(char *)NULL, optsize) != 0) {
    284 			    syslog(LOG_ERR, "setsockopt IP_OPTIONS NULL: %m");
    285 			    exit(1);
    286 		    }
    287 	        }
    288 	    }
    289 #endif
    290 	    if (do_rlogin(hp->h_name) == 0 && hostok)
    291 		    authenticated++;
    292 	}
    293 	if (confirmed == 0) {
    294 		write(f, "", 1);
    295 		confirmed = 1;		/* we sent the null! */
    296 	}
    297 #ifdef	KERBEROS
    298 #ifdef	CRYPT
    299 	if (doencrypt)
    300 		(void) des_write(f, SECURE_MESSAGE, sizeof(SECURE_MESSAGE));
    301 #endif
    302 	if (use_kerberos == 0)
    303 #endif
    304 	   if (!authenticated && !hostok)
    305 		write(f, "rlogind: Host address mismatch.\r\n",
    306 		    sizeof("rlogind: Host address mismatch.\r\n") - 1);
    307 
    308 	netf = f;
    309 
    310 	pid = forkpty(&master, line, NULL, &win);
    311 	if (pid < 0) {
    312 		if (errno == ENOENT)
    313 			fatal(f, "Out of ptys", 0);
    314 		else
    315 			fatal(f, "Forkpty", 1);
    316 	}
    317 	if (pid == 0) {
    318 		if (f > 2)	/* f should always be 0, but... */
    319 			(void) close(f);
    320 		setup_term(0);
    321 		if (authenticated) {
    322 #ifdef	KERBEROS
    323 			if (use_kerberos && (pwd->pw_uid == 0))
    324 				syslog(LOG_INFO|LOG_AUTH,
    325 				    "ROOT Kerberos login from %s.%s@%s on %s\n",
    326 				    kdata->pname, kdata->pinst, kdata->prealm,
    327 				    hp->h_name);
    328 #endif
    329 
    330 			execl(_PATH_LOGIN, "login", "-p",
    331 			    "-h", hp->h_name, "-f", lusername, 0);
    332 		} else
    333 			execl(_PATH_LOGIN, "login", "-p",
    334 			    "-h", hp->h_name, lusername, 0);
    335 		fatal(STDERR_FILENO, _PATH_LOGIN, 1);
    336 		/*NOTREACHED*/
    337 	}
    338 #ifdef	CRYPT
    339 #ifdef	KERBEROS
    340 	/*
    341 	 * If encrypted, don't turn on NBIO or the des read/write
    342 	 * routines will croak.
    343 	 */
    344 
    345 	if (!doencrypt)
    346 #endif
    347 #endif
    348 		ioctl(f, FIONBIO, &on);
    349 	ioctl(master, FIONBIO, &on);
    350 	ioctl(master, TIOCPKT, &on);
    351 	signal(SIGCHLD, cleanup);
    352 	protocol(f, master);
    353 	signal(SIGCHLD, SIG_IGN);
    354 	cleanup();
    355 }
    356 
    357 char	magic[2] = { 0377, 0377 };
    358 char	oobdata[] = {TIOCPKT_WINDOW};
    359 
    360 /*
    361  * Handle a "control" request (signaled by magic being present)
    362  * in the data stream.  For now, we are only willing to handle
    363  * window size changes.
    364  */
    365 control(pty, cp, n)
    366 	int pty;
    367 	char *cp;
    368 	int n;
    369 {
    370 	struct winsize w;
    371 
    372 	if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's')
    373 		return (0);
    374 	oobdata[0] &= ~TIOCPKT_WINDOW;	/* we know he heard */
    375 	bcopy(cp+4, (char *)&w, sizeof(w));
    376 	w.ws_row = ntohs(w.ws_row);
    377 	w.ws_col = ntohs(w.ws_col);
    378 	w.ws_xpixel = ntohs(w.ws_xpixel);
    379 	w.ws_ypixel = ntohs(w.ws_ypixel);
    380 	(void)ioctl(pty, TIOCSWINSZ, &w);
    381 	return (4+sizeof (w));
    382 }
    383 
    384 /*
    385  * rlogin "protocol" machine.
    386  */
    387 protocol(f, p)
    388 	register int f, p;
    389 {
    390 	char pibuf[1024+1], fibuf[1024], *pbp, *fbp;
    391 	register pcc = 0, fcc = 0;
    392 	int cc, nfd, n;
    393 	char cntl;
    394 
    395 	/*
    396 	 * Must ignore SIGTTOU, otherwise we'll stop
    397 	 * when we try and set slave pty's window shape
    398 	 * (our controlling tty is the master pty).
    399 	 */
    400 	(void) signal(SIGTTOU, SIG_IGN);
    401 	send(f, oobdata, 1, MSG_OOB);	/* indicate new rlogin */
    402 	if (f > p)
    403 		nfd = f + 1;
    404 	else
    405 		nfd = p + 1;
    406 	if (nfd > FD_SETSIZE) {
    407 		syslog(LOG_ERR, "select mask too small, increase FD_SETSIZE");
    408 		fatal(f, "internal error (select mask too small)", 0);
    409 	}
    410 	for (;;) {
    411 		fd_set ibits, obits, ebits, *omask;
    412 
    413 		FD_ZERO(&ebits);
    414 		FD_ZERO(&ibits);
    415 		FD_ZERO(&obits);
    416 		omask = (fd_set *)NULL;
    417 		if (fcc) {
    418 			FD_SET(p, &obits);
    419 			omask = &obits;
    420 		} else
    421 			FD_SET(f, &ibits);
    422 		if (pcc >= 0)
    423 			if (pcc) {
    424 				FD_SET(f, &obits);
    425 				omask = &obits;
    426 			} else
    427 				FD_SET(p, &ibits);
    428 		FD_SET(p, &ebits);
    429 		if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) {
    430 			if (errno == EINTR)
    431 				continue;
    432 			fatal(f, "select", 1);
    433 		}
    434 		if (n == 0) {
    435 			/* shouldn't happen... */
    436 			sleep(5);
    437 			continue;
    438 		}
    439 #define	pkcontrol(c)	((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
    440 		if (FD_ISSET(p, &ebits)) {
    441 			cc = read(p, &cntl, 1);
    442 			if (cc == 1 && pkcontrol(cntl)) {
    443 				cntl |= oobdata[0];
    444 				send(f, &cntl, 1, MSG_OOB);
    445 				if (cntl & TIOCPKT_FLUSHWRITE) {
    446 					pcc = 0;
    447 					FD_CLR(p, &ibits);
    448 				}
    449 			}
    450 		}
    451 		if (FD_ISSET(f, &ibits)) {
    452 #ifdef	CRYPT
    453 #ifdef	KERBEROS
    454 			if (doencrypt)
    455 				fcc = des_read(f, fibuf, sizeof(fibuf));
    456 			else
    457 #endif
    458 #endif
    459 				fcc = read(f, fibuf, sizeof(fibuf));
    460 			if (fcc < 0 && errno == EWOULDBLOCK)
    461 				fcc = 0;
    462 			else {
    463 				register char *cp;
    464 				int left, n;
    465 
    466 				if (fcc <= 0)
    467 					break;
    468 				fbp = fibuf;
    469 
    470 			top:
    471 				for (cp = fibuf; cp < fibuf+fcc-1; cp++)
    472 					if (cp[0] == magic[0] &&
    473 					    cp[1] == magic[1]) {
    474 						left = fcc - (cp-fibuf);
    475 						n = control(p, cp, left);
    476 						if (n) {
    477 							left -= n;
    478 							if (left > 0)
    479 								bcopy(cp+n, cp, left);
    480 							fcc -= n;
    481 							goto top; /* n^2 */
    482 						}
    483 					}
    484 				FD_SET(p, &obits);		/* try write */
    485 			}
    486 		}
    487 
    488 		if (FD_ISSET(p, &obits) && fcc > 0) {
    489 			cc = write(p, fbp, fcc);
    490 			if (cc > 0) {
    491 				fcc -= cc;
    492 				fbp += cc;
    493 			}
    494 		}
    495 
    496 		if (FD_ISSET(p, &ibits)) {
    497 			pcc = read(p, pibuf, sizeof (pibuf));
    498 			pbp = pibuf;
    499 			if (pcc < 0 && errno == EWOULDBLOCK)
    500 				pcc = 0;
    501 			else if (pcc <= 0)
    502 				break;
    503 			else if (pibuf[0] == 0) {
    504 				pbp++, pcc--;
    505 #ifdef	CRYPT
    506 #ifdef	KERBEROS
    507 				if (!doencrypt)
    508 #endif
    509 #endif
    510 					FD_SET(f, &obits);	/* try write */
    511 			} else {
    512 				if (pkcontrol(pibuf[0])) {
    513 					pibuf[0] |= oobdata[0];
    514 					send(f, &pibuf[0], 1, MSG_OOB);
    515 				}
    516 				pcc = 0;
    517 			}
    518 		}
    519 		if ((FD_ISSET(f, &obits)) && pcc > 0) {
    520 #ifdef	CRYPT
    521 #ifdef	KERBEROS
    522 			if (doencrypt)
    523 				cc = des_write(f, pbp, pcc);
    524 			else
    525 #endif
    526 #endif
    527 				cc = write(f, pbp, pcc);
    528 			if (cc < 0 && errno == EWOULDBLOCK) {
    529 				/*
    530 				 * This happens when we try write after read
    531 				 * from p, but some old kernels balk at large
    532 				 * writes even when select returns true.
    533 				 */
    534 				if (!FD_ISSET(p, &ibits))
    535 					sleep(5);
    536 				continue;
    537 			}
    538 			if (cc > 0) {
    539 				pcc -= cc;
    540 				pbp += cc;
    541 			}
    542 		}
    543 	}
    544 }
    545 
    546 void
    547 cleanup()
    548 {
    549 	char *p;
    550 
    551 	p = line + sizeof(_PATH_DEV) - 1;
    552 	if (logout(p))
    553 		logwtmp(p, "", "");
    554 	(void)chmod(line, 0666);
    555 	(void)chown(line, 0, 0);
    556 	*p = 'p';
    557 	(void)chmod(line, 0666);
    558 	(void)chown(line, 0, 0);
    559 	shutdown(netf, 2);
    560 	exit(1);
    561 }
    562 
    563 fatal(f, msg, syserr)
    564 	int f, syserr;
    565 	char *msg;
    566 {
    567 	int len;
    568 	char buf[BUFSIZ], *bp = buf;
    569 
    570 	/*
    571 	 * Prepend binary one to message if we haven't sent
    572 	 * the magic null as confirmation.
    573 	 */
    574 	if (!confirmed)
    575 		*bp++ = '\01';		/* error indicator */
    576 	if (syserr)
    577 		len = sprintf(bp, "rlogind: %s: %s.\r\n",
    578 		    msg, strerror(errno));
    579 	else
    580 		len = sprintf(bp, "rlogind: %s.\r\n", msg);
    581 	(void) write(f, buf, bp + len - buf);
    582 	exit(1);
    583 }
    584 
    585 do_rlogin(host)
    586 	char *host;
    587 {
    588 	getstr(rusername, sizeof(rusername), "remuser too long");
    589 	getstr(lusername, sizeof(lusername), "locuser too long");
    590 	getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
    591 
    592 	pwd = getpwnam(lusername);
    593 	if (pwd == NULL)
    594 		return(-1);
    595 	if (pwd->pw_uid == 0)
    596 		return(-1);
    597 	return(ruserok(host, 0, rusername, lusername));
    598 }
    599 
    600 
    601 getstr(buf, cnt, errmsg)
    602 	char *buf;
    603 	int cnt;
    604 	char *errmsg;
    605 {
    606 	char c;
    607 
    608 	do {
    609 		if (read(0, &c, 1) != 1)
    610 			exit(1);
    611 		if (--cnt < 0)
    612 			fatal(STDOUT_FILENO, errmsg, 0);
    613 		*buf++ = c;
    614 	} while (c != 0);
    615 }
    616 
    617 extern	char **environ;
    618 
    619 setup_term(fd)
    620 	int fd;
    621 {
    622 	register char *cp = index(term+ENVSIZE, '/');
    623 	char *speed;
    624 	struct termios tt;
    625 
    626 #ifndef notyet
    627 	tcgetattr(fd, &tt);
    628 	if (cp) {
    629 		*cp++ = '\0';
    630 		speed = cp;
    631 		cp = index(speed, '/');
    632 		if (cp)
    633 			*cp++ = '\0';
    634 		cfsetspeed(&tt, atoi(speed));
    635 	}
    636 
    637 	tt.c_iflag = TTYDEF_IFLAG;
    638 	tt.c_oflag = TTYDEF_OFLAG;
    639 	tt.c_lflag = TTYDEF_LFLAG;
    640 	tcsetattr(fd, TCSAFLUSH, &tt);
    641 #else
    642 	if (cp) {
    643 		*cp++ = '\0';
    644 		speed = cp;
    645 		cp = index(speed, '/');
    646 		if (cp)
    647 			*cp++ = '\0';
    648 		tcgetattr(fd, &tt);
    649 		cfsetspeed(&tt, atoi(speed));
    650 		tcsetattr(fd, TCSAFLUSH, &tt);
    651 	}
    652 #endif
    653 
    654 	env[0] = term;
    655 	env[1] = 0;
    656 	environ = env;
    657 }
    658 
    659 #ifdef	KERBEROS
    660 #define	VERSION_SIZE	9
    661 
    662 /*
    663  * Do the remote kerberos login to the named host with the
    664  * given inet address
    665  *
    666  * Return 0 on valid authorization
    667  * Return -1 on valid authentication, no authorization
    668  * Return >0 for error conditions
    669  */
    670 do_krb_login(host, dest)
    671 	char *host;
    672 	struct sockaddr_in *dest;
    673 {
    674 	int rc;
    675 	char instance[INST_SZ], version[VERSION_SIZE];
    676 	long authopts = 0L;	/* !mutual */
    677 	struct sockaddr_in faddr;
    678 
    679 	kdata = (AUTH_DAT *) auth_buf;
    680 	ticket = (KTEXT) tick_buf;
    681 
    682 	instance[0] = '*';
    683 	instance[1] = '\0';
    684 
    685 #ifdef	CRYPT
    686 	if (doencrypt) {
    687 		rc = sizeof(faddr);
    688 		if (getsockname(0, (struct sockaddr *)&faddr, &rc))
    689 			return(-1);
    690 		authopts = KOPT_DO_MUTUAL;
    691 		rc = krb_recvauth(
    692 			authopts, 0,
    693 			ticket, "rcmd",
    694 			instance, dest, &faddr,
    695 			kdata, "", schedule, version);
    696 		 des_set_key(kdata->session, schedule);
    697 
    698 	} else
    699 #endif
    700 		rc = krb_recvauth(
    701 			authopts, 0,
    702 			ticket, "rcmd",
    703 			instance, dest, (struct sockaddr_in *) 0,
    704 			kdata, "", (bit_64 *) 0, version);
    705 
    706 	if (rc != KSUCCESS)
    707 		return(rc);
    708 
    709 	getstr(lusername, sizeof(lusername), "locuser");
    710 	/* get the "cmd" in the rcmd protocol */
    711 	getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type");
    712 
    713 	pwd = getpwnam(lusername);
    714 	if (pwd == NULL)
    715 		return(-1);
    716 
    717 	/* returns nonzero for no access */
    718 	if (kuserok(kdata,lusername) != 0)
    719 		return(-1);
    720 
    721 	return(0);
    722 
    723 }
    724 #endif /* KERBEROS */
    725 
    726 usage()
    727 {
    728 #ifdef KERBEROS
    729 	syslog(LOG_ERR, "usage: rlogind [-aln] [-k | -v]");
    730 #else
    731 	syslog(LOG_ERR, "usage: rlogind [-aln]");
    732 #endif
    733 }
    734 
    735 /*
    736  * Check whether host h is in our local domain,
    737  * defined as sharing the last two components of the domain part,
    738  * or the entire domain part if the local domain has only one component.
    739  * If either name is unqualified (contains no '.'),
    740  * assume that the host is local, as it will be
    741  * interpreted as such.
    742  */
    743 local_domain(h)
    744 	char *h;
    745 {
    746 	char localhost[MAXHOSTNAMELEN];
    747 	char *p1, *p2, *topdomain();
    748 
    749 	localhost[0] = 0;
    750 	(void) gethostname(localhost, sizeof(localhost));
    751 	p1 = topdomain(localhost);
    752 	p2 = topdomain(h);
    753 	if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
    754 		return(1);
    755 	return(0);
    756 }
    757 
    758 char *
    759 topdomain(h)
    760 	char *h;
    761 {
    762 	register char *p;
    763 	char *maybe = NULL;
    764 	int dots = 0;
    765 
    766 	for (p = h + strlen(h); p >= h; p--) {
    767 		if (*p == '.') {
    768 			if (++dots == 2)
    769 				return (p);
    770 			maybe = p;
    771 		}
    772 	}
    773 	return (maybe);
    774 }
    775