rshd.c revision 19108
1/*-
2 * Copyright (c) 1988, 1989, 1992, 1993, 1994
3 *	The Regents of the University of California.  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 *	$Id: rshd.c,v 1.8 1996/09/22 21:55:08 wosch Exp $
34 */
35
36#ifndef lint
37static char copyright[] =
38"@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\n\
39	The Regents of the University of California.  All rights reserved.\n";
40#endif /* not lint */
41
42#ifndef lint
43static char sccsid[] = "@(#)rshd.c	8.2 (Berkeley) 4/6/94";
44#endif /* not lint */
45
46/*
47 * remote shell server:
48 *	[port]\0
49 *	remuser\0
50 *	locuser\0
51 *	command\0
52 *	data
53 */
54#include <sys/param.h>
55#include <sys/ioctl.h>
56#include <sys/time.h>
57#include <sys/socket.h>
58
59#include <netinet/in.h>
60#include <arpa/inet.h>
61#include <netdb.h>
62
63#include <errno.h>
64#include <fcntl.h>
65#include <paths.h>
66#include <pwd.h>
67#include <signal.h>
68#include <stdio.h>
69#include <stdlib.h>
70#include <string.h>
71#include <syslog.h>
72#include <unistd.h>
73
74int	keepalive = 1;
75int	check_all;
76int	log_success;		/* If TRUE, log all successful accesses */
77int	sent_null;
78
79void	 doit __P((struct sockaddr_in *));
80void	 error __P((const char *, ...));
81void	 getstr __P((char *, int, char *));
82int	 local_domain __P((char *));
83char	*topdomain __P((char *));
84void	 usage __P((void));
85
86#ifdef	KERBEROS
87#include <des.h>
88#include <kerberosIV/krb.h>
89#define	VERSION_SIZE	9
90#define SECURE_MESSAGE  "This rsh session is using DES encryption for all transmissions.\r\n"
91#define	OPTIONS		"alnkvxL"
92char	authbuf[sizeof(AUTH_DAT)];
93char	tickbuf[sizeof(KTEXT_ST)];
94int	doencrypt, use_kerberos, vacuous;
95Key_schedule	schedule;
96#else
97#define	OPTIONS	"alnL"
98#endif
99
100int
101main(argc, argv)
102	int argc;
103	char *argv[];
104{
105	extern int __check_rhosts_file;
106	struct linger linger;
107	int ch, on = 1, fromlen;
108	struct sockaddr_in from;
109
110	openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
111
112	opterr = 0;
113	while ((ch = getopt(argc, argv, OPTIONS)) != EOF)
114		switch (ch) {
115		case 'a':
116			check_all = 1;
117			break;
118		case 'l':
119			__check_rhosts_file = 0;
120			break;
121		case 'n':
122			keepalive = 0;
123			break;
124#ifdef	KERBEROS
125		case 'k':
126			use_kerberos = 1;
127			break;
128
129		case 'v':
130			vacuous = 1;
131			break;
132
133#ifdef CRYPT
134		case 'x':
135			doencrypt = 1;
136			break;
137#endif
138#endif
139		case 'L':
140			log_success = 1;
141			break;
142		case '?':
143		default:
144			usage();
145			break;
146		}
147
148	argc -= optind;
149	argv += optind;
150
151#ifdef	KERBEROS
152	if (use_kerberos && vacuous) {
153		syslog(LOG_ERR, "only one of -k and -v allowed");
154		exit(2);
155	}
156#ifdef CRYPT
157	if (doencrypt && !use_kerberos) {
158		syslog(LOG_ERR, "-k is required for -x");
159		exit(2);
160	}
161#endif
162#endif
163
164	fromlen = sizeof (from);
165	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
166		syslog(LOG_ERR, "getpeername: %m");
167		_exit(1);
168	}
169	if (keepalive &&
170	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
171	    sizeof(on)) < 0)
172		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
173	linger.l_onoff = 1;
174	linger.l_linger = 60;			/* XXX */
175	if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger,
176	    sizeof (linger)) < 0)
177		syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
178	doit(&from);
179	/* NOTREACHED */
180}
181
182char	username[20] = "USER=";
183char	homedir[64] = "HOME=";
184char	shell[64] = "SHELL=";
185char	path[100] = "PATH=";
186char	*envinit[] =
187	    {homedir, shell, path, username, 0};
188char	**environ;
189
190void
191doit(fromp)
192	struct sockaddr_in *fromp;
193{
194	extern char *__rcmd_errstr;	/* syslog hook from libc/net/rcmd.c. */
195	struct hostent *hp;
196	struct passwd *pwd;
197	u_short port;
198	fd_set ready, readfrom;
199	int cc, nfd, pv[2], pid, s;
200	int one = 1;
201	char *hostname, *errorstr, *errorhost;
202	char *cp, sig, buf[BUFSIZ];
203	char cmdbuf[NCARGS+1], locuser[16], remuser[16];
204	char remotehost[2 * MAXHOSTNAMELEN + 1];
205
206#ifdef	KERBEROS
207	AUTH_DAT	*kdata = (AUTH_DAT *) NULL;
208	KTEXT		ticket = (KTEXT) NULL;
209	char		instance[INST_SZ], version[VERSION_SIZE];
210	struct		sockaddr_in	fromaddr;
211	int		rc;
212	long		authopts;
213	int		pv1[2], pv2[2];
214	fd_set		wready, writeto;
215
216	fromaddr = *fromp;
217#endif
218
219	(void) signal(SIGINT, SIG_DFL);
220	(void) signal(SIGQUIT, SIG_DFL);
221	(void) signal(SIGTERM, SIG_DFL);
222#ifdef DEBUG
223	{ int t = open(_PATH_TTY, 2);
224	  if (t >= 0) {
225		ioctl(t, TIOCNOTTY, (char *)0);
226		(void) close(t);
227	  }
228	}
229#endif
230	fromp->sin_port = ntohs((u_short)fromp->sin_port);
231	if (fromp->sin_family != AF_INET) {
232		syslog(LOG_ERR, "malformed \"from\" address (af %d)\n",
233		    fromp->sin_family);
234		exit(1);
235	}
236#ifdef IP_OPTIONS
237      {
238	u_char optbuf[BUFSIZ/3], *cp;
239	char lbuf[BUFSIZ], *lp;
240	int optsize = sizeof(optbuf), ipproto;
241	struct protoent *ip;
242
243	if ((ip = getprotobyname("ip")) != NULL)
244		ipproto = ip->p_proto;
245	else
246		ipproto = IPPROTO_IP;
247	if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) &&
248	    optsize != 0) {
249		lp = lbuf;
250		for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
251			sprintf(lp, " %2.2x", *cp);
252		syslog(LOG_NOTICE,
253		    "Connection received from %s using IP options (ignored):%s",
254		    inet_ntoa(fromp->sin_addr), lbuf);
255		if (setsockopt(0, ipproto, IP_OPTIONS,
256		    (char *)NULL, optsize) != 0) {
257			syslog(LOG_ERR, "setsockopt IP_OPTIONS NULL: %m");
258			exit(1);
259		}
260	}
261      }
262#endif
263
264#ifdef	KERBEROS
265	if (!use_kerberos)
266#endif
267		if (fromp->sin_port >= IPPORT_RESERVED ||
268		    fromp->sin_port < IPPORT_RESERVED/2) {
269			syslog(LOG_NOTICE|LOG_AUTH,
270			    "Connection from %s on illegal port %u",
271			    inet_ntoa(fromp->sin_addr),
272			    fromp->sin_port);
273			exit(1);
274		}
275
276	(void) alarm(60);
277	port = 0;
278	for (;;) {
279		char c;
280		if ((cc = read(STDIN_FILENO, &c, 1)) != 1) {
281			if (cc < 0)
282				syslog(LOG_NOTICE, "read: %m");
283			shutdown(0, 1+1);
284			exit(1);
285		}
286		if (c== 0)
287			break;
288		port = port * 10 + c - '0';
289	}
290
291	(void) alarm(0);
292	if (port != 0) {
293		int lport = IPPORT_RESERVED - 1;
294		s = rresvport(&lport);
295		if (s < 0) {
296			syslog(LOG_ERR, "can't get stderr port: %m");
297			exit(1);
298		}
299#ifdef	KERBEROS
300		if (!use_kerberos)
301#endif
302			if (port >= IPPORT_RESERVED) {
303				syslog(LOG_ERR, "2nd port not reserved\n");
304				exit(1);
305			}
306		fromp->sin_port = htons(port);
307		if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0) {
308			syslog(LOG_INFO, "connect second port %d: %m", port);
309			exit(1);
310		}
311	}
312
313#ifdef	KERBEROS
314	if (vacuous) {
315		error("rshd: remote host requires Kerberos authentication\n");
316		exit(1);
317	}
318#endif
319
320#ifdef notdef
321	/* from inetd, socket is already on 0, 1, 2 */
322	dup2(f, 0);
323	dup2(f, 1);
324	dup2(f, 2);
325#endif
326	errorstr = NULL;
327	hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof (struct in_addr),
328		fromp->sin_family);
329	if (hp) {
330		/*
331		 * If name returned by gethostbyaddr is in our domain,
332		 * attempt to verify that we haven't been fooled by someone
333		 * in a remote net; look up the name and check that this
334		 * address corresponds to the name.
335		 */
336		hostname = hp->h_name;
337#ifdef	KERBEROS
338		if (!use_kerberos)
339#endif
340		if (check_all || local_domain(hp->h_name)) {
341			strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1);
342			remotehost[sizeof(remotehost) - 1] = 0;
343			errorhost = remotehost;
344			hp = gethostbyname(remotehost);
345			if (hp == NULL) {
346				syslog(LOG_INFO,
347				    "Couldn't look up address for %s",
348				    remotehost);
349				errorstr =
350				"Couldn't look up address for your host (%s)\n";
351				hostname = inet_ntoa(fromp->sin_addr);
352			} else for (; ; hp->h_addr_list++) {
353				if (hp->h_addr_list[0] == NULL) {
354					syslog(LOG_NOTICE,
355					  "Host addr %s not listed for host %s",
356					    inet_ntoa(fromp->sin_addr),
357					    hp->h_name);
358					errorstr =
359					    "Host address mismatch for %s\n";
360					hostname = inet_ntoa(fromp->sin_addr);
361					break;
362				}
363				if (!bcmp(hp->h_addr_list[0],
364				    (caddr_t)&fromp->sin_addr,
365				    sizeof(fromp->sin_addr))) {
366					hostname = remotehost;
367					break;
368				}
369			}
370		}
371	} else
372		errorhost = hostname = inet_ntoa(fromp->sin_addr);
373
374#ifdef	KERBEROS
375	if (use_kerberos) {
376		kdata = (AUTH_DAT *) authbuf;
377		ticket = (KTEXT) tickbuf;
378		authopts = 0L;
379		strcpy(instance, "*");
380		version[VERSION_SIZE - 1] = '\0';
381#ifdef CRYPT
382		if (doencrypt) {
383			struct sockaddr_in local_addr;
384			rc = sizeof(local_addr);
385			if (getsockname(0, (struct sockaddr *)&local_addr,
386			    &rc) < 0) {
387				syslog(LOG_ERR, "getsockname: %m");
388				error("rlogind: getsockname: %m");
389				exit(1);
390			}
391			authopts = KOPT_DO_MUTUAL;
392			rc = krb_recvauth(authopts, 0, ticket,
393				"rcmd", instance, &fromaddr,
394				&local_addr, kdata, "", schedule,
395				version);
396			des_set_key_krb(&kdata->session, schedule);
397		} else
398#endif
399			rc = krb_recvauth(authopts, 0, ticket, "rcmd",
400				instance, &fromaddr,
401				(struct sockaddr_in *) 0,
402				kdata, "", NULL, version);
403		if (rc != KSUCCESS) {
404			error("Kerberos authentication failure: %s\n",
405				  krb_err_txt[rc]);
406			exit(1);
407		}
408	} else
409#endif
410		getstr(remuser, sizeof(remuser), "remuser");
411
412	getstr(locuser, sizeof(locuser), "locuser");
413	getstr(cmdbuf, sizeof(cmdbuf), "command");
414	setpwent();
415	pwd = getpwnam(locuser);
416	if (pwd == NULL) {
417		syslog(LOG_INFO|LOG_AUTH,
418		    "%s@%s as %s: unknown login. cmd='%.80s'",
419		    remuser, hostname, locuser, cmdbuf);
420		if (errorstr == NULL)
421			errorstr = "Login incorrect.\n";
422		goto fail;
423	}
424	if (chdir(pwd->pw_dir) < 0) {
425		(void) chdir("/");
426#ifdef notdef
427		syslog(LOG_INFO|LOG_AUTH,
428		    "%s@%s as %s: no home directory. cmd='%.80s'",
429		    remuser, hostname, locuser, cmdbuf);
430		error("No remote directory.\n");
431		exit(1);
432#endif
433	}
434
435#ifdef	KERBEROS
436	if (use_kerberos) {
437		if (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0') {
438			if (kuserok(kdata, locuser) != 0) {
439				syslog(LOG_INFO|LOG_AUTH,
440				    "Kerberos rsh denied to %s.%s@%s",
441				    kdata->pname, kdata->pinst, kdata->prealm);
442				error("Permission denied.\n");
443				exit(1);
444			}
445		}
446	} else
447#endif
448
449		if (errorstr ||
450		    (pwd->pw_expire && time(NULL) >= pwd->pw_expire) ||
451		    (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' &&
452		    iruserok(fromp->sin_addr.s_addr, pwd->pw_uid == 0,
453		    remuser, locuser) < 0)) {
454			if (__rcmd_errstr)
455				syslog(LOG_INFO|LOG_AUTH,
456			    "%s@%s as %s: permission denied (%s). cmd='%.80s'",
457				    remuser, hostname, locuser, __rcmd_errstr,
458				    cmdbuf);
459			else
460				syslog(LOG_INFO|LOG_AUTH,
461			    "%s@%s as %s: permission denied. cmd='%.80s'",
462				    remuser, hostname, locuser, cmdbuf);
463fail:
464			if (errorstr == NULL)
465				errorstr = "Permission denied.\n";
466			error(errorstr, errorhost);
467			exit(1);
468		}
469
470	if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) {
471		error("Logins currently disabled.\n");
472		exit(1);
473	}
474#if	BSD > 43
475	/* before fork, while we're session leader */
476	if (setlogin(pwd->pw_name) < 0)
477		syslog(LOG_ERR, "setlogin() failed: %m");
478#endif
479
480	(void) write(STDERR_FILENO, "\0", 1);
481	sent_null = 1;
482
483	if (port) {
484		if (pipe(pv) < 0) {
485			error("Can't make pipe.\n");
486			exit(1);
487		}
488#ifdef CRYPT
489#ifdef KERBEROS
490		if (doencrypt) {
491			if (pipe(pv1) < 0) {
492				error("Can't make 2nd pipe.\n");
493				exit(1);
494			}
495			if (pipe(pv2) < 0) {
496				error("Can't make 3rd pipe.\n");
497				exit(1);
498			}
499		}
500#endif
501#endif
502		pid = fork();
503		if (pid == -1)  {
504			error("Can't fork; try again.\n");
505			exit(1);
506		}
507		if (pid) {
508#ifdef CRYPT
509#ifdef KERBEROS
510			if (doencrypt) {
511				static char msg[] = SECURE_MESSAGE;
512				(void) close(pv1[1]);
513				(void) close(pv2[1]);
514				des_write(s, msg, sizeof(msg) - 1);
515
516			} else
517#endif
518#endif
519			{
520				(void) close(0);
521				(void) close(1);
522			}
523			(void) close(2);
524			(void) close(pv[1]);
525
526			FD_ZERO(&readfrom);
527			FD_SET(s, &readfrom);
528			FD_SET(pv[0], &readfrom);
529			if (pv[0] > s)
530				nfd = pv[0];
531			else
532				nfd = s;
533#ifdef CRYPT
534#ifdef KERBEROS
535			if (doencrypt) {
536				FD_ZERO(&writeto);
537				FD_SET(pv2[0], &writeto);
538				FD_SET(pv1[0], &readfrom);
539
540				nfd = MAX(nfd, pv2[0]);
541				nfd = MAX(nfd, pv1[0]);
542			} else
543#endif
544#endif
545				ioctl(pv[0], FIONBIO, (char *)&one);
546
547			/* should set s nbio! */
548			nfd++;
549			do {
550				ready = readfrom;
551#ifdef CRYPT
552#ifdef KERBEROS
553				if (doencrypt) {
554					wready = writeto;
555					if (select(nfd, &ready,
556					    &wready, (fd_set *) 0,
557					    (struct timeval *) 0) < 0)
558						break;
559				} else
560#endif
561#endif
562					if (select(nfd, &ready, (fd_set *)0,
563					  (fd_set *)0, (struct timeval *)0) < 0)
564						break;
565				if (FD_ISSET(s, &ready)) {
566					int	ret;
567#ifdef CRYPT
568#ifdef KERBEROS
569					if (doencrypt)
570						ret = des_read(s, &sig, 1);
571					else
572#endif
573#endif
574						ret = read(s, &sig, 1);
575					if (ret <= 0)
576						FD_CLR(s, &readfrom);
577					else
578						killpg(pid, sig);
579				}
580				if (FD_ISSET(pv[0], &ready)) {
581					errno = 0;
582					cc = read(pv[0], buf, sizeof(buf));
583					if (cc <= 0) {
584						shutdown(s, 1+1);
585						FD_CLR(pv[0], &readfrom);
586					} else {
587#ifdef CRYPT
588#ifdef KERBEROS
589						if (doencrypt)
590							(void)
591							  des_write(s, buf, cc);
592						else
593#endif
594#endif
595							(void)
596							  write(s, buf, cc);
597					}
598				}
599#ifdef CRYPT
600#ifdef KERBEROS
601				if (doencrypt && FD_ISSET(pv1[0], &ready)) {
602					errno = 0;
603					cc = read(pv1[0], buf, sizeof(buf));
604					if (cc <= 0) {
605						shutdown(pv1[0], 1+1);
606						FD_CLR(pv1[0], &readfrom);
607					} else
608						(void) des_write(STDOUT_FILENO,
609						    buf, cc);
610				}
611
612				if (doencrypt && FD_ISSET(pv2[0], &wready)) {
613					errno = 0;
614					cc = des_read(STDIN_FILENO,
615					    buf, sizeof(buf));
616					if (cc <= 0) {
617						shutdown(pv2[0], 1+1);
618						FD_CLR(pv2[0], &writeto);
619					} else
620						(void) write(pv2[0], buf, cc);
621				}
622#endif
623#endif
624
625			} while (FD_ISSET(s, &readfrom) ||
626#ifdef CRYPT
627#ifdef KERBEROS
628			    (doencrypt && FD_ISSET(pv1[0], &readfrom)) ||
629#endif
630#endif
631			    FD_ISSET(pv[0], &readfrom));
632			exit(0);
633		}
634		setpgrp(0, getpid());
635		(void) close(s);
636		(void) close(pv[0]);
637#ifdef CRYPT
638#ifdef KERBEROS
639		if (doencrypt) {
640			close(pv1[0]); close(pv2[0]);
641			dup2(pv1[1], 1);
642			dup2(pv2[1], 0);
643			close(pv1[1]);
644			close(pv2[1]);
645		}
646#endif
647#endif
648		dup2(pv[1], 2);
649		close(pv[1]);
650	}
651	if (*pwd->pw_shell == '\0')
652		pwd->pw_shell = _PATH_BSHELL;
653	(void) setgid((gid_t)pwd->pw_gid);
654	initgroups(pwd->pw_name, pwd->pw_gid);
655	(void) setuid((uid_t)pwd->pw_uid);
656	environ = envinit;
657	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
658	strcat(path, _PATH_DEFPATH);
659	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
660	strncat(username, pwd->pw_name, sizeof(username)-6);
661	cp = strrchr(pwd->pw_shell, '/');
662	if (cp)
663		cp++;
664	else
665		cp = pwd->pw_shell;
666	endpwent();
667	if (log_success || pwd->pw_uid == 0) {
668#ifdef	KERBEROS
669		if (use_kerberos)
670		    syslog(LOG_INFO|LOG_AUTH,
671			"Kerberos shell from %s.%s@%s on %s as %s, cmd='%.80s'",
672			kdata->pname, kdata->pinst, kdata->prealm,
673			hostname, locuser, cmdbuf);
674		else
675#endif
676		    syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
677			remuser, hostname, locuser, cmdbuf);
678	}
679	execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
680	perror(pwd->pw_shell);
681	exit(1);
682}
683
684/*
685 * Report error to client.  Note: can't be used until second socket has
686 * connected to client, or older clients will hang waiting for that
687 * connection first.
688 */
689#if __STDC__
690#include <stdarg.h>
691#else
692#include <varargs.h>
693#endif
694
695void
696#if __STDC__
697error(const char *fmt, ...)
698#else
699error(fmt, va_alist)
700	char *fmt;
701        va_dcl
702#endif
703{
704	va_list ap;
705	int len;
706	char *bp, buf[BUFSIZ];
707#if __STDC__
708	va_start(ap, fmt);
709#else
710	va_start(ap);
711#endif
712	bp = buf;
713	if (sent_null == 0) {
714		*bp++ = 1;
715		len = 1;
716	} else
717		len = 0;
718	(void)vsnprintf(bp, sizeof(buf) - 1, fmt, ap);
719	(void)write(STDERR_FILENO, buf, len + strlen(bp));
720}
721
722void
723getstr(buf, cnt, err)
724	char *buf, *err;
725	int cnt;
726{
727	char c;
728
729	do {
730		if (read(STDIN_FILENO, &c, 1) != 1)
731			exit(1);
732		*buf++ = c;
733		if (--cnt == 0) {
734			error("%s too long\n", err);
735			exit(1);
736		}
737	} while (c != 0);
738}
739
740/*
741 * Check whether host h is in our local domain,
742 * defined as sharing the last two components of the domain part,
743 * or the entire domain part if the local domain has only one component.
744 * If either name is unqualified (contains no '.'),
745 * assume that the host is local, as it will be
746 * interpreted as such.
747 */
748int
749local_domain(h)
750	char *h;
751{
752	char localhost[MAXHOSTNAMELEN];
753	char *p1, *p2;
754
755	localhost[0] = 0;
756	(void) gethostname(localhost, sizeof(localhost));
757	p1 = topdomain(localhost);
758	p2 = topdomain(h);
759	if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
760		return (1);
761	return (0);
762}
763
764char *
765topdomain(h)
766	char *h;
767{
768	char *p, *maybe = NULL;
769	int dots = 0;
770
771	for (p = h + strlen(h); p >= h; p--) {
772		if (*p == '.') {
773			if (++dots == 2)
774				return (p);
775			maybe = p;
776		}
777	}
778	return (maybe);
779}
780
781void
782usage()
783{
784
785	syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);
786	exit(2);
787}
788