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