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