xref: /netbsd/libexec/rlogind/rlogind.c (revision c4a72b64)
1 /*	$NetBSD: rlogind.c,v 1.30 2002/09/23 12:48:03 mycroft Exp $	*/
2 
3 /*
4  * Copyright (C) 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *    This product includes software developed by WIDE Project and
18  *    its contributors.
19  * 4. Neither the name of the project nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 /*-
37  * Copyright (c) 1983, 1988, 1989, 1993
38  *	The Regents of the University of California.  All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. All advertising materials mentioning features or use of this software
49  *    must display the following acknowledgement:
50  *	This product includes software developed by the University of
51  *	California, Berkeley and its contributors.
52  * 4. Neither the name of the University nor the names of its contributors
53  *    may be used to endorse or promote products derived from this software
54  *    without specific prior written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66  * SUCH DAMAGE.
67  */
68 
69 #include <sys/cdefs.h>
70 #ifndef lint
71 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1989, 1993\n\
72 	The Regents of the University of California.  All rights reserved.\n");
73 #if 0
74 static char sccsid[] = "@(#)rlogind.c	8.2 (Berkeley) 4/28/95";
75 #else
76 __RCSID("$NetBSD: rlogind.c,v 1.30 2002/09/23 12:48:03 mycroft Exp $");
77 #endif
78 #endif /* not lint */
79 
80 /*
81  * remote login server:
82  *	\0
83  *	remuser\0
84  *	locuser\0
85  *	terminal_type/speed\0
86  *	data
87  */
88 
89 #include <sys/param.h>
90 #include <sys/stat.h>
91 #include <sys/ioctl.h>
92 #include <signal.h>
93 #include <termios.h>
94 #include <poll.h>
95 
96 #include <sys/socket.h>
97 #include <netinet/in.h>
98 #include <netinet/in_systm.h>
99 #include <netinet/ip.h>
100 #include <arpa/inet.h>
101 #include <netdb.h>
102 
103 #include <pwd.h>
104 #include <syslog.h>
105 #include <errno.h>
106 #include <stdio.h>
107 #include <unistd.h>
108 #include <stdlib.h>
109 #include <string.h>
110 #include <util.h>
111 #include "pathnames.h"
112 
113 #ifndef TIOCPKT_WINDOW
114 #define TIOCPKT_WINDOW 0x80
115 #endif
116 
117 #define		OPTIONS			"alnL"
118 
119 char	*env[2];
120 #define	NMAX 30
121 char	lusername[NMAX+1], rusername[NMAX+1];
122 static	char term[64] = "TERM=";
123 #define	ENVSIZE	(sizeof("TERM=")-1)	/* skip null for concatenation */
124 int	keepalive = 1;
125 int	check_all = 0;
126 int	log_success = 0;
127 
128 struct	passwd *pwd;
129 
130 void	doit __P((int, struct sockaddr *));
131 int	control __P((int, char *, int));
132 void	protocol __P((int, int));
133 void	cleanup __P((int));
134 void	fatal __P((int, char *, int));
135 int	do_rlogin __P((struct sockaddr *, char *));
136 void	getstr __P((char *, int, char *));
137 void	setup_term __P((int));
138 #if 0
139 int	do_krb_login __P((union sockunion *));
140 #endif
141 void	usage __P((void));
142 int	local_domain __P((char *));
143 char	*topdomain __P((char *));
144 int	main __P((int, char *[]));
145 
146 extern int __check_rhosts_file;
147 extern char *__rcmd_errstr;	/* syslog hook from libc/net/rcmd.c */
148 extern char **environ;
149 
150 int
151 main(argc, argv)
152 	int argc;
153 	char *argv[];
154 {
155 	struct sockaddr_storage from;
156 	int ch, fromlen, on;
157 
158 	openlog("rlogind", LOG_PID, LOG_AUTH);
159 
160 	opterr = 0;
161 	while ((ch = getopt(argc, argv, OPTIONS)) != -1)
162 		switch (ch) {
163 		case 'a':
164 			check_all = 1;
165 			break;
166 		case 'l':
167 			__check_rhosts_file = 0;
168 			break;
169 		case 'n':
170 			keepalive = 0;
171 			break;
172 		case 'L':
173 			log_success = 1;
174 			break;
175 		case '?':
176 		default:
177 			usage();
178 			break;
179 		}
180 	argc -= optind;
181 	argv += optind;
182 
183 	fromlen = sizeof (from); /* xxx */
184 	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
185 		syslog(LOG_ERR,"Can't get peer name of remote host: %m");
186 		fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
187 	}
188 #ifdef INET6
189 	if (((struct sockaddr *)&from)->sa_family == AF_INET6 &&
190 	    IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr) &&
191 	    sizeof(struct sockaddr_in) <= sizeof(from)) {
192 		struct sockaddr_in sin;
193 		struct sockaddr_in6 *sin6;
194 		const int off = sizeof(struct sockaddr_in6) -
195 		    sizeof(struct sockaddr_in);
196 
197 		sin6 = (struct sockaddr_in6 *)&from;
198 		memset(&sin, 0, sizeof(sin));
199 		sin.sin_family = AF_INET;
200 		sin.sin_len = sizeof(struct sockaddr_in);
201 		memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[off],
202 		    sizeof(sin.sin_addr));
203 		memcpy(&from, &sin, sizeof(sin));
204 		fromlen = sin.sin_len;
205 	}
206 #else
207 	if (((struct sockaddr *)&from)->sa_family == AF_INET6 &&
208 	    IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) {
209 		char hbuf[NI_MAXHOST];
210 		if (getnameinfo((struct sockaddr *)&from, fromlen, hbuf,
211 				sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) {
212 			strncpy(hbuf, "invalid", sizeof(hbuf));
213 		}
214 		syslog(LOG_ERR, "malformed \"from\" address (v4 mapped, %s)\n",
215 		    hbuf);
216 		exit(1);
217 	}
218 #endif
219 	on = 1;
220 	if (keepalive &&
221 	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
222 		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
223 #if defined(IP_TOS)
224 	if (((struct sockaddr *)&from)->sa_family == AF_INET) {
225 		on = IPTOS_LOWDELAY;
226 		if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
227 			syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
228 	}
229 #endif
230 	doit(0, (struct sockaddr *)&from);
231 	/* NOTREACHED */
232 #ifdef __GNUC__
233 	exit(0);
234 #endif
235 }
236 
237 int	child;
238 int	netf;
239 char	line[MAXPATHLEN];
240 int	confirmed;
241 
242 struct winsize win = { 0, 0, 0, 0 };
243 
244 
245 void
246 doit(f, fromp)
247 	int f;
248 	struct sockaddr *fromp;
249 {
250 	int master, pid, on = 1;
251 	int authenticated = 0;
252 	char *hostname;
253 	char hostnamebuf[2 * MAXHOSTNAMELEN + 1];
254 	char c;
255 	char naddr[NI_MAXHOST];
256 	char saddr[NI_MAXHOST];
257 	char raddr[NI_MAXHOST];
258 	int af = fromp->sa_family;
259 	u_int16_t *portp;
260 	struct addrinfo hints, *res, *res0;
261 	int gaierror;
262 #ifdef NI_WITHSCOPEID
263 	const int niflags = NI_NUMERICHOST | NI_NUMERICSERV | NI_WITHSCOPEID;
264 #else
265 	const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
266 #endif
267 
268 	alarm(60);
269 	read(f, &c, 1);
270 
271 	if (c != 0)
272 		exit(1);
273 
274 	alarm(0);
275 	switch (af) {
276 	case AF_INET:
277 		portp = &((struct sockaddr_in *)fromp)->sin_port;
278 		break;
279 #ifdef INET6
280 	case AF_INET6:
281 		portp = &((struct sockaddr_in6 *)fromp)->sin6_port;
282 		break;
283 #endif
284 	default:
285 		syslog(LOG_ERR, "malformed \"from\" address (af %d)\n", af);
286 		exit(1);
287 	}
288 	if (getnameinfo((struct sockaddr *)fromp, fromp->sa_len,
289 		    naddr, sizeof(naddr), NULL, 0, niflags) != 0) {
290 		syslog(LOG_ERR, "malformed \"from\" address (af %d)\n", af);
291 		exit(1);
292 	}
293 
294 	if (getnameinfo((struct sockaddr *)fromp, fromp->sa_len,
295 		    saddr, sizeof(saddr), NULL, 0, NI_NAMEREQD) == 0) {
296 		/*
297 		 * If name returned by gethostbyaddr is in our domain,
298 		 * attempt to verify that we haven't been fooled by someone
299 		 * in a remote net; look up the name and check that this
300 		 * address corresponds to the name.
301 		 */
302 		hostname = saddr;
303 		res0 = NULL;
304 		if (check_all || local_domain(saddr)) {
305 			strncpy(hostnamebuf, saddr, sizeof(hostnamebuf) - 1);
306 			hostnamebuf[sizeof(hostnamebuf) - 1] = 0;
307 			memset(&hints, 0, sizeof(hints));
308 			hints.ai_family = fromp->sa_family;
309 			hints.ai_socktype = SOCK_STREAM;
310 			hints.ai_flags = AI_CANONNAME;
311 			gaierror = getaddrinfo(hostnamebuf, "0", &hints, &res0);
312 			if (gaierror) {
313 				syslog(LOG_NOTICE,
314 				    "Couldn't look up address for %s: %s",
315 				    hostnamebuf, gai_strerror(gaierror));
316 				hostname = naddr;
317 			} else {
318 				for (res = res0; res; res = res->ai_next) {
319 					if (res->ai_family != fromp->sa_family)
320 						continue;
321 					if (res->ai_addrlen != fromp->sa_len)
322 						continue;
323 					if (getnameinfo(res->ai_addr,
324 						res->ai_addrlen,
325 						raddr, sizeof(raddr), NULL, 0,
326 						niflags) == 0
327 					 && strcmp(naddr, raddr) == 0) {
328 						hostname = res->ai_canonname
329 							? res->ai_canonname
330 							: saddr;
331 						break;
332 					}
333 				}
334 				if (res == NULL) {
335 					syslog(LOG_NOTICE,
336 					  "Host addr %s not listed for host %s",
337 					    naddr, res0->ai_canonname
338 						    ? res0->ai_canonname
339 						    : saddr);
340 					hostname = naddr;
341 				}
342 			}
343 		}
344 		hostname = strncpy(hostnamebuf, hostname,
345 				   sizeof(hostnamebuf) - 1);
346 		if (res0)
347 			freeaddrinfo(res0);
348 	} else
349 		hostname = strncpy(hostnamebuf, naddr,
350 				   sizeof(hostnamebuf) - 1);
351 
352 	hostnamebuf[sizeof(hostnamebuf) - 1] = '\0';
353 
354 	if (ntohs(*portp) >= IPPORT_RESERVED ||
355 	    ntohs(*portp) < IPPORT_RESERVED/2) {
356 		syslog(LOG_NOTICE, "Connection from %s on illegal port",
357 		       naddr);
358 		fatal(f, "Permission denied", 0);
359 	}
360 #ifdef IP_OPTIONS
361 	if (fromp->sa_family == AF_INET) {
362 		u_char optbuf[BUFSIZ/3], *cp;
363 		char lbuf[BUFSIZ], *lp;
364 		int optsize = sizeof(optbuf), ipproto;
365 		struct protoent *ip;
366 
367 		if ((ip = getprotobyname("ip")) != NULL)
368 			ipproto = ip->p_proto;
369 		else
370 			ipproto = IPPROTO_IP;
371 		if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
372 		    &optsize) == 0 && optsize != 0) {
373 			lp = lbuf;
374 			for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
375 				sprintf(lp, " %2.2x", *cp);
376 			syslog(LOG_NOTICE,
377 			    "Connection received using IP options (ignored):%s",
378 			    lbuf);
379 			if (setsockopt(0, ipproto, IP_OPTIONS,
380 			    (char *)NULL, optsize) != 0) {
381 				syslog(LOG_ERR,
382 				    "setsockopt IP_OPTIONS NULL: %m");
383 				exit(1);
384 			}
385 		}
386 	}
387 #endif
388 	if (do_rlogin(fromp, hostname) == 0)
389 		authenticated++;
390 	if (confirmed == 0) {
391 		write(f, "", 1);
392 		confirmed = 1;		/* we sent the null! */
393 	}
394 	netf = f;
395 
396 	pid = forkpty(&master, line, NULL, &win);
397 	if (pid < 0) {
398 		if (errno == ENOENT)
399 			fatal(f, "Out of ptys", 0);
400 		else
401 			fatal(f, "Forkpty", 1);
402 	}
403 	if (pid == 0) {
404 		if (f > 2)	/* f should always be 0, but... */
405 			(void) close(f);
406 		setup_term(0);
407 		if (authenticated)
408 			execl(_PATH_LOGIN, "login", "-p",
409 			    "-h", hostname, "-f", "--", lusername, (char *)0);
410 		else
411 			execl(_PATH_LOGIN, "login", "-p",
412 			    "-h", hostname, "--", lusername, (char *)0);
413 		fatal(STDERR_FILENO, _PATH_LOGIN, 1);
414 		/*NOTREACHED*/
415 	}
416 	ioctl(f, FIONBIO, &on);
417 	ioctl(master, FIONBIO, &on);
418 	ioctl(master, TIOCPKT, &on);
419 	signal(SIGCHLD, cleanup);
420 	protocol(f, master);
421 	signal(SIGCHLD, SIG_IGN);
422 	cleanup(0);
423 }
424 
425 char	magic[2] = { 0377, 0377 };
426 char	oobdata[] = {TIOCPKT_WINDOW};
427 
428 /*
429  * Handle a "control" request (signaled by magic being present)
430  * in the data stream.  For now, we are only willing to handle
431  * window size changes.
432  */
433 int
434 control(pty, cp, n)
435 	int pty;
436 	char *cp;
437 	int n;
438 {
439 	struct winsize w;
440 
441 	if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's')
442 		return (0);
443 	oobdata[0] &= ~TIOCPKT_WINDOW;	/* we know he heard */
444 	memmove(&w, cp+4, sizeof(w));
445 	w.ws_row = ntohs(w.ws_row);
446 	w.ws_col = ntohs(w.ws_col);
447 	w.ws_xpixel = ntohs(w.ws_xpixel);
448 	w.ws_ypixel = ntohs(w.ws_ypixel);
449 	(void)ioctl(pty, TIOCSWINSZ, &w);
450 	return (4+sizeof (w));
451 }
452 
453 /*
454  * rlogin "protocol" machine.
455  */
456 void
457 protocol(f, p)
458 	int f, p;
459 {
460 	char pibuf[1024+1], fibuf[1024], *pbp = NULL, *fbp = NULL;
461 					/* XXX gcc above */
462 	int pcc = 0, fcc = 0;
463 	int cc, n;
464 	char cntl;
465 	struct pollfd set[2];
466 
467 	/*
468 	 * Must ignore SIGTTOU, otherwise we'll stop
469 	 * when we try and set slave pty's window shape
470 	 * (our controlling tty is the master pty).
471 	 */
472 	(void) signal(SIGTTOU, SIG_IGN);
473 	send(f, oobdata, 1, MSG_OOB);	/* indicate new rlogin */
474 	set[0].fd = p;
475 	set[1].fd = f;
476 	for (;;) {
477 		set[0].events = POLLPRI;
478 		set[1].events = 0;
479 		if (fcc)
480 			set[0].events |= POLLOUT;
481 		else
482 			set[1].events |= POLLIN;
483 		if (pcc >= 0) {
484 			if (pcc)
485 				set[1].events |= POLLOUT;
486 			else
487 				set[0].events |= POLLIN;
488 		}
489 		if ((n = poll(set, 2, INFTIM)) < 0) {
490 			if (errno == EINTR)
491 				continue;
492 			fatal(f, "poll", 1);
493 		}
494 		if (n == 0) {
495 			/* shouldn't happen... */
496 			sleep(5);
497 			continue;
498 		}
499 #define	pkcontrol(c)	((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
500 		if (set[0].revents & POLLPRI) {
501 			cc = read(p, &cntl, 1);
502 			if (cc == 1 && pkcontrol(cntl)) {
503 				cntl |= oobdata[0];
504 				send(f, &cntl, 1, MSG_OOB);
505 				if (cntl & TIOCPKT_FLUSHWRITE)
506 					pcc = 0;
507 			}
508 		}
509 		if (set[1].revents & POLLIN) {
510 			fcc = read(f, fibuf, sizeof(fibuf));
511 			if (fcc < 0 && errno == EWOULDBLOCK)
512 				fcc = 0;
513 			else {
514 				char *cp;
515 				int left, n;
516 
517 				if (fcc <= 0)
518 					break;
519 				fbp = fibuf;
520 
521 			top:
522 				for (cp = fibuf; cp < fibuf+fcc-1; cp++)
523 					if (cp[0] == magic[0] &&
524 					    cp[1] == magic[1]) {
525 						left = fcc - (cp-fibuf);
526 						n = control(p, cp, left);
527 						if (n) {
528 							left -= n;
529 							if (left > 0)
530 								memmove(cp,
531 								    cp+n,
532 								    left);
533 							fcc -= n;
534 							goto top; /* n^2 */
535 						}
536 					}
537 			}
538 		}
539 
540 		if (set[0].revents & POLLOUT && fcc > 0) {
541 			cc = write(p, fbp, fcc);
542 			if (cc > 0) {
543 				fcc -= cc;
544 				fbp += cc;
545 			}
546 		}
547 
548 		if (set[0].revents & POLLIN) {
549 			pcc = read(p, pibuf, sizeof (pibuf));
550 			pbp = pibuf;
551 			if (pcc < 0 && errno == EWOULDBLOCK)
552 				pcc = 0;
553 			else if (pcc <= 0)
554 				break;
555 			else if (pibuf[0] == 0) {
556 				pbp++, pcc--;
557 			} else {
558 				if (pkcontrol(pibuf[0])) {
559 					pibuf[0] |= oobdata[0];
560 					send(f, &pibuf[0], 1, MSG_OOB);
561 				}
562 				pcc = 0;
563 			}
564 		}
565 		if (set[1].revents & POLLOUT && pcc > 0) {
566 			cc = write(f, pbp, pcc);
567 			if (cc > 0) {
568 				pcc -= cc;
569 				pbp += cc;
570 			}
571 		}
572 	}
573 }
574 
575 void
576 cleanup(signo)
577 	int signo;
578 {
579 	char *p, c;
580 
581 	p = line + sizeof(_PATH_DEV) - 1;
582 #ifdef SUPPORT_UTMP
583 	if (logout(p))
584 		logwtmp(p, "", "");
585 #endif
586 #ifdef SUPPORT_UTMPX
587 	if (logoutx(p, 0, DEAD_PROCESS))
588 		logwtmpx(p, "", "", 0, DEAD_PROCESS);
589 #endif
590 	(void)chmod(line, 0666);
591 	(void)chown(line, 0, 0);
592 	c = *p; *p = 'p';
593 	(void)chmod(line, 0666);
594 	(void)chown(line, 0, 0);
595 	*p = c;
596 	if (ttyaction(line, "rlogind", "root"))
597 		syslog(LOG_ERR, "%s: ttyaction failed", line);
598 	shutdown(netf, 2);
599 	exit(1);
600 }
601 
602 void
603 fatal(f, msg, syserr)
604 	int f;
605 	char *msg;
606 	int syserr;
607 {
608 	int len;
609 	char buf[BUFSIZ], *bp = buf;
610 
611 	/*
612 	 * Prepend binary one to message if we haven't sent
613 	 * the magic null as confirmation.
614 	 */
615 	if (!confirmed)
616 		*bp++ = '\01';		/* error indicator */
617 	if (syserr)
618 		len = sprintf(bp, "rlogind: %s: %s.\r\n",
619 		    msg, strerror(errno));
620 	else
621 		len = sprintf(bp, "rlogind: %s.\r\n", msg);
622 	(void) write(f, buf, bp + len - buf);
623 	exit(1);
624 }
625 
626 int
627 do_rlogin(dest, host)
628 	struct sockaddr *dest;
629 	char *host;
630 {
631 	int retval;
632 
633 	getstr(rusername, sizeof(rusername), "remuser too long");
634 	getstr(lusername, sizeof(lusername), "locuser too long");
635 	getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
636 
637 	pwd = getpwnam(lusername);
638 	if (pwd == NULL) {
639 		syslog(LOG_INFO,
640 		    "%s@%s as %s: unknown login.", rusername, host, lusername);
641 		return (-1);
642 	}
643 
644 	retval = iruserok_sa(dest, dest->sa_len, pwd->pw_uid == 0, rusername,
645 		lusername);
646 /* XXX put inet_ntoa(dest->sin_addr.s_addr) into all messages below */
647 	if (retval == 0) {
648 		if (log_success)
649 			syslog(LOG_INFO, "%s@%s as %s: iruserok succeeded",
650 			    rusername, host, lusername);
651 	} else {
652 		if (__rcmd_errstr)
653 			syslog(LOG_INFO, "%s@%s as %s: iruserok failed (%s)",
654 			    rusername, host, lusername, __rcmd_errstr);
655 		else
656 			syslog(LOG_INFO, "%s@%s as %s: iruserok failed",
657 			    rusername, host, lusername);
658 	}
659 	return(retval);
660 }
661 
662 void
663 getstr(buf, cnt, errmsg)
664 	char *buf;
665 	int cnt;
666 	char *errmsg;
667 {
668 	char c;
669 
670 	do {
671 		if (read(0, &c, 1) != 1)
672 			exit(1);
673 		if (--cnt < 0)
674 			fatal(STDOUT_FILENO, errmsg, 0);
675 		*buf++ = c;
676 	} while (c != 0);
677 }
678 
679 
680 void
681 setup_term(fd)
682 	int fd;
683 {
684 	char *cp = index(term+ENVSIZE, '/');
685 	char *speed;
686 	struct termios tt;
687 
688 #ifndef notyet
689 	tcgetattr(fd, &tt);
690 	if (cp) {
691 		*cp++ = '\0';
692 		speed = cp;
693 		cp = index(speed, '/');
694 		if (cp)
695 			*cp++ = '\0';
696 		cfsetspeed(&tt, atoi(speed));
697 	}
698 
699 	tt.c_iflag = TTYDEF_IFLAG;
700 	tt.c_oflag = TTYDEF_OFLAG;
701 	tt.c_lflag = TTYDEF_LFLAG;
702 	tcsetattr(fd, TCSAFLUSH, &tt);
703 #else
704 	if (cp) {
705 		*cp++ = '\0';
706 		speed = cp;
707 		cp = index(speed, '/');
708 		if (cp)
709 			*cp++ = '\0';
710 		tcgetattr(fd, &tt);
711 		cfsetspeed(&tt, atoi(speed));
712 		tcsetattr(fd, TCSAFLUSH, &tt);
713 	}
714 #endif
715 
716 	env[0] = term;
717 	env[1] = 0;
718 	environ = env;
719 }
720 
721 
722 void
723 usage()
724 {
725 	syslog(LOG_ERR, "usage: rlogind [-alnL]");
726 }
727 
728 /*
729  * Check whether host h is in our local domain,
730  * defined as sharing the last two components of the domain part,
731  * or the entire domain part if the local domain has only one component.
732  * If either name is unqualified (contains no '.'),
733  * assume that the host is local, as it will be
734  * interpreted as such.
735  */
736 int
737 local_domain(h)
738 	char *h;
739 {
740 	char localhost[MAXHOSTNAMELEN + 1];
741 	char *p1, *p2;
742 
743 	localhost[0] = 0;
744 	(void) gethostname(localhost, sizeof(localhost));
745 	localhost[sizeof(localhost) - 1] = '\0';
746 	p1 = topdomain(localhost);
747 	p2 = topdomain(h);
748 	if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
749 		return (1);
750 	return (0);
751 }
752 
753 char *
754 topdomain(h)
755 	char *h;
756 {
757 	char *p;
758 	char *maybe = NULL;
759 	int dots = 0;
760 
761 	for (p = h + strlen(h); p >= h; p--) {
762 		if (*p == '.') {
763 			if (++dots == 2)
764 				return (p);
765 			maybe = p;
766 		}
767 	}
768 	return (maybe);
769 }
770