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