xref: /minix3/crypto/external/bsd/heimdal/dist/lib/roken/iruserok.c (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc /*	$NetBSD: iruserok.c,v 1.1.1.1 2011/04/13 18:15:42 elric Exp $	*/
2*ebfedea0SLionel Sambuc 
3*ebfedea0SLionel Sambuc /*
4*ebfedea0SLionel Sambuc  * Copyright (c) 1983, 1993, 1994
5*ebfedea0SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
6*ebfedea0SLionel Sambuc  *
7*ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
9*ebfedea0SLionel Sambuc  * are met:
10*ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*ebfedea0SLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
16*ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
17*ebfedea0SLionel Sambuc  *    without specific prior written permission.
18*ebfedea0SLionel Sambuc  *
19*ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*ebfedea0SLionel Sambuc  * SUCH DAMAGE.
30*ebfedea0SLionel Sambuc  */
31*ebfedea0SLionel Sambuc 
32*ebfedea0SLionel Sambuc #include <config.h>
33*ebfedea0SLionel Sambuc 
34*ebfedea0SLionel Sambuc #include <stdio.h>
35*ebfedea0SLionel Sambuc #include <ctype.h>
36*ebfedea0SLionel Sambuc #ifdef HAVE_SYS_TYPES_H
37*ebfedea0SLionel Sambuc #include <sys/types.h>
38*ebfedea0SLionel Sambuc #endif
39*ebfedea0SLionel Sambuc #ifdef HAVE_NETINET_IN_H
40*ebfedea0SLionel Sambuc #include <netinet/in.h>
41*ebfedea0SLionel Sambuc #endif
42*ebfedea0SLionel Sambuc #ifdef HAVE_NETINET_IN6_H
43*ebfedea0SLionel Sambuc #include <netinet/in6.h>
44*ebfedea0SLionel Sambuc #endif
45*ebfedea0SLionel Sambuc #ifdef HAVE_NETINET6_IN6_H
46*ebfedea0SLionel Sambuc #include <netinet6/in6.h>
47*ebfedea0SLionel Sambuc #endif
48*ebfedea0SLionel Sambuc #ifdef HAVE_RPCSVC_YPCLNT_H
49*ebfedea0SLionel Sambuc #include <rpcsvc/ypclnt.h>
50*ebfedea0SLionel Sambuc #endif
51*ebfedea0SLionel Sambuc 
52*ebfedea0SLionel Sambuc #include <krb5/roken.h>
53*ebfedea0SLionel Sambuc 
54*ebfedea0SLionel Sambuc int     __check_rhosts_file = 1;
55*ebfedea0SLionel Sambuc char    *__rcmd_errstr = 0;
56*ebfedea0SLionel Sambuc 
57*ebfedea0SLionel Sambuc /*
58*ebfedea0SLionel Sambuc  * Returns "true" if match, 0 if no match.
59*ebfedea0SLionel Sambuc  */
60*ebfedea0SLionel Sambuc static
61*ebfedea0SLionel Sambuc int
__icheckhost(unsigned raddr,const char * lhost)62*ebfedea0SLionel Sambuc __icheckhost(unsigned raddr, const char *lhost)
63*ebfedea0SLionel Sambuc {
64*ebfedea0SLionel Sambuc 	struct hostent *hp;
65*ebfedea0SLionel Sambuc 	u_long laddr;
66*ebfedea0SLionel Sambuc 	char **pp;
67*ebfedea0SLionel Sambuc 
68*ebfedea0SLionel Sambuc 	/* Try for raw ip address first. */
69*ebfedea0SLionel Sambuc 	if (isdigit((unsigned char)*lhost)
70*ebfedea0SLionel Sambuc 	    && (long)(laddr = inet_addr(lhost)) != -1)
71*ebfedea0SLionel Sambuc 		return (raddr == laddr);
72*ebfedea0SLionel Sambuc 
73*ebfedea0SLionel Sambuc 	/* Better be a hostname. */
74*ebfedea0SLionel Sambuc 	if ((hp = gethostbyname(lhost)) == NULL)
75*ebfedea0SLionel Sambuc 		return (0);
76*ebfedea0SLionel Sambuc 
77*ebfedea0SLionel Sambuc 	/* Spin through ip addresses. */
78*ebfedea0SLionel Sambuc 	for (pp = hp->h_addr_list; *pp; ++pp)
79*ebfedea0SLionel Sambuc 	        if (memcmp(&raddr, *pp, sizeof(u_long)) == 0)
80*ebfedea0SLionel Sambuc 			return (1);
81*ebfedea0SLionel Sambuc 
82*ebfedea0SLionel Sambuc 	/* No match. */
83*ebfedea0SLionel Sambuc 	return (0);
84*ebfedea0SLionel Sambuc }
85*ebfedea0SLionel Sambuc 
86*ebfedea0SLionel Sambuc /*
87*ebfedea0SLionel Sambuc  * Returns 0 if ok, -1 if not ok.
88*ebfedea0SLionel Sambuc  */
89*ebfedea0SLionel Sambuc static
90*ebfedea0SLionel Sambuc int
__ivaliduser(FILE * hostf,unsigned raddr,const char * luser,const char * ruser)91*ebfedea0SLionel Sambuc __ivaliduser(FILE *hostf, unsigned raddr, const char *luser,
92*ebfedea0SLionel Sambuc 	     const char *ruser)
93*ebfedea0SLionel Sambuc {
94*ebfedea0SLionel Sambuc 	char *user, *p;
95*ebfedea0SLionel Sambuc 	int ch;
96*ebfedea0SLionel Sambuc 	char buf[MaxHostNameLen + 128];		/* host + login */
97*ebfedea0SLionel Sambuc 	char hname[MaxHostNameLen];
98*ebfedea0SLionel Sambuc 	struct hostent *hp;
99*ebfedea0SLionel Sambuc 	/* Presumed guilty until proven innocent. */
100*ebfedea0SLionel Sambuc 	int userok = 0, hostok = 0;
101*ebfedea0SLionel Sambuc #ifdef HAVE_YP_GET_DEFAULT_DOMAIN
102*ebfedea0SLionel Sambuc 	char *ypdomain;
103*ebfedea0SLionel Sambuc 
104*ebfedea0SLionel Sambuc 	if (yp_get_default_domain(&ypdomain))
105*ebfedea0SLionel Sambuc 		ypdomain = NULL;
106*ebfedea0SLionel Sambuc #else
107*ebfedea0SLionel Sambuc #define	ypdomain NULL
108*ebfedea0SLionel Sambuc #endif
109*ebfedea0SLionel Sambuc 	/* We need to get the damn hostname back for netgroup matching. */
110*ebfedea0SLionel Sambuc 	if ((hp = gethostbyaddr((char *)&raddr,
111*ebfedea0SLionel Sambuc 				sizeof(u_long),
112*ebfedea0SLionel Sambuc 				AF_INET)) == NULL)
113*ebfedea0SLionel Sambuc 		return (-1);
114*ebfedea0SLionel Sambuc 	strlcpy(hname, hp->h_name, sizeof(hname));
115*ebfedea0SLionel Sambuc 
116*ebfedea0SLionel Sambuc 	while (fgets(buf, sizeof(buf), hostf)) {
117*ebfedea0SLionel Sambuc 		p = buf;
118*ebfedea0SLionel Sambuc 		/* Skip lines that are too long. */
119*ebfedea0SLionel Sambuc 		if (strchr(p, '\n') == NULL) {
120*ebfedea0SLionel Sambuc 			while ((ch = getc(hostf)) != '\n' && ch != EOF);
121*ebfedea0SLionel Sambuc 			continue;
122*ebfedea0SLionel Sambuc 		}
123*ebfedea0SLionel Sambuc 		if (*p == '\n' || *p == '#') {
124*ebfedea0SLionel Sambuc 			/* comment... */
125*ebfedea0SLionel Sambuc 			continue;
126*ebfedea0SLionel Sambuc 		}
127*ebfedea0SLionel Sambuc 		while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
128*ebfedea0SLionel Sambuc 		        if (isupper((unsigned char)*p))
129*ebfedea0SLionel Sambuc 			    *p = tolower((unsigned char)*p);
130*ebfedea0SLionel Sambuc 			p++;
131*ebfedea0SLionel Sambuc 		}
132*ebfedea0SLionel Sambuc 		if (*p == ' ' || *p == '\t') {
133*ebfedea0SLionel Sambuc 			*p++ = '\0';
134*ebfedea0SLionel Sambuc 			while (*p == ' ' || *p == '\t')
135*ebfedea0SLionel Sambuc 				p++;
136*ebfedea0SLionel Sambuc 			user = p;
137*ebfedea0SLionel Sambuc 			while (*p != '\n' && *p != ' ' &&
138*ebfedea0SLionel Sambuc 			    *p != '\t' && *p != '\0')
139*ebfedea0SLionel Sambuc 				p++;
140*ebfedea0SLionel Sambuc 		} else
141*ebfedea0SLionel Sambuc 			user = p;
142*ebfedea0SLionel Sambuc 		*p = '\0';
143*ebfedea0SLionel Sambuc 		/*
144*ebfedea0SLionel Sambuc 		 * Do +/- and +@/-@ checking. This looks really nasty,
145*ebfedea0SLionel Sambuc 		 * but it matches SunOS's behavior so far as I can tell.
146*ebfedea0SLionel Sambuc 		 */
147*ebfedea0SLionel Sambuc 		switch(buf[0]) {
148*ebfedea0SLionel Sambuc 		case '+':
149*ebfedea0SLionel Sambuc 			if (!buf[1]) {     /* '+' matches all hosts */
150*ebfedea0SLionel Sambuc 				hostok = 1;
151*ebfedea0SLionel Sambuc 				break;
152*ebfedea0SLionel Sambuc 			}
153*ebfedea0SLionel Sambuc 			if (buf[1] == '@')  /* match a host by netgroup */
154*ebfedea0SLionel Sambuc 				hostok = innetgr((char *)&buf[2],
155*ebfedea0SLionel Sambuc 					(char *)&hname, NULL, ypdomain);
156*ebfedea0SLionel Sambuc 			else		/* match a host by addr */
157*ebfedea0SLionel Sambuc 				hostok = __icheckhost(raddr,(char *)&buf[1]);
158*ebfedea0SLionel Sambuc 			break;
159*ebfedea0SLionel Sambuc 		case '-':     /* reject '-' hosts and all their users */
160*ebfedea0SLionel Sambuc 			if (buf[1] == '@') {
161*ebfedea0SLionel Sambuc 				if (innetgr((char *)&buf[2],
162*ebfedea0SLionel Sambuc 					      (char *)&hname, NULL, ypdomain))
163*ebfedea0SLionel Sambuc 					return(-1);
164*ebfedea0SLionel Sambuc 			} else {
165*ebfedea0SLionel Sambuc 				if (__icheckhost(raddr,(char *)&buf[1]))
166*ebfedea0SLionel Sambuc 					return(-1);
167*ebfedea0SLionel Sambuc 			}
168*ebfedea0SLionel Sambuc 			break;
169*ebfedea0SLionel Sambuc 		default:  /* if no '+' or '-', do a simple match */
170*ebfedea0SLionel Sambuc 			hostok = __icheckhost(raddr, buf);
171*ebfedea0SLionel Sambuc 			break;
172*ebfedea0SLionel Sambuc 		}
173*ebfedea0SLionel Sambuc 		switch(*user) {
174*ebfedea0SLionel Sambuc 		case '+':
175*ebfedea0SLionel Sambuc 			if (!*(user+1)) {      /* '+' matches all users */
176*ebfedea0SLionel Sambuc 				userok = 1;
177*ebfedea0SLionel Sambuc 				break;
178*ebfedea0SLionel Sambuc 			}
179*ebfedea0SLionel Sambuc 			if (*(user+1) == '@')  /* match a user by netgroup */
180*ebfedea0SLionel Sambuc 				userok = innetgr(user+2, NULL, (char *)ruser,
181*ebfedea0SLionel Sambuc 						 ypdomain);
182*ebfedea0SLionel Sambuc 			else	   /* match a user by direct specification */
183*ebfedea0SLionel Sambuc 				userok = !(strcmp(ruser, user+1));
184*ebfedea0SLionel Sambuc 			break;
185*ebfedea0SLionel Sambuc 		case '-': 		/* if we matched a hostname, */
186*ebfedea0SLionel Sambuc 			if (hostok) {   /* check for user field rejections */
187*ebfedea0SLionel Sambuc 				if (!*(user+1))
188*ebfedea0SLionel Sambuc 					return(-1);
189*ebfedea0SLionel Sambuc 				if (*(user+1) == '@') {
190*ebfedea0SLionel Sambuc 					if (innetgr(user+2, NULL,
191*ebfedea0SLionel Sambuc 						    (char *)ruser, ypdomain))
192*ebfedea0SLionel Sambuc 						return(-1);
193*ebfedea0SLionel Sambuc 				} else {
194*ebfedea0SLionel Sambuc 					if (!strcmp(ruser, user+1))
195*ebfedea0SLionel Sambuc 						return(-1);
196*ebfedea0SLionel Sambuc 				}
197*ebfedea0SLionel Sambuc 			}
198*ebfedea0SLionel Sambuc 			break;
199*ebfedea0SLionel Sambuc 		default:	/* no rejections: try to match the user */
200*ebfedea0SLionel Sambuc 			if (hostok)
201*ebfedea0SLionel Sambuc 				userok = !(strcmp(ruser,*user ? user : luser));
202*ebfedea0SLionel Sambuc 			break;
203*ebfedea0SLionel Sambuc 		}
204*ebfedea0SLionel Sambuc 		if (hostok && userok)
205*ebfedea0SLionel Sambuc 			return(0);
206*ebfedea0SLionel Sambuc 	}
207*ebfedea0SLionel Sambuc 	return (-1);
208*ebfedea0SLionel Sambuc }
209*ebfedea0SLionel Sambuc 
210*ebfedea0SLionel Sambuc /*
211*ebfedea0SLionel Sambuc  * New .rhosts strategy: We are passed an ip address. We spin through
212*ebfedea0SLionel Sambuc  * hosts.equiv and .rhosts looking for a match. When the .rhosts only
213*ebfedea0SLionel Sambuc  * has ip addresses, we don't have to trust a nameserver.  When it
214*ebfedea0SLionel Sambuc  * contains hostnames, we spin through the list of addresses the nameserver
215*ebfedea0SLionel Sambuc  * gives us and look for a match.
216*ebfedea0SLionel Sambuc  *
217*ebfedea0SLionel Sambuc  * Returns 0 if ok, -1 if not ok.
218*ebfedea0SLionel Sambuc  */
219*ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
iruserok(unsigned raddr,int superuser,const char * ruser,const char * luser)220*ebfedea0SLionel Sambuc iruserok(unsigned raddr, int superuser, const char *ruser, const char *luser)
221*ebfedea0SLionel Sambuc {
222*ebfedea0SLionel Sambuc 	char *cp;
223*ebfedea0SLionel Sambuc 	struct stat sbuf;
224*ebfedea0SLionel Sambuc 	struct passwd *pwd;
225*ebfedea0SLionel Sambuc 	FILE *hostf;
226*ebfedea0SLionel Sambuc 	uid_t uid;
227*ebfedea0SLionel Sambuc 	int first;
228*ebfedea0SLionel Sambuc 	char pbuf[MaxPathLen];
229*ebfedea0SLionel Sambuc 
230*ebfedea0SLionel Sambuc 	first = 1;
231*ebfedea0SLionel Sambuc 	hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
232*ebfedea0SLionel Sambuc again:
233*ebfedea0SLionel Sambuc 	if (hostf) {
234*ebfedea0SLionel Sambuc 		if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
235*ebfedea0SLionel Sambuc 			fclose(hostf);
236*ebfedea0SLionel Sambuc 			return (0);
237*ebfedea0SLionel Sambuc 		}
238*ebfedea0SLionel Sambuc 		fclose(hostf);
239*ebfedea0SLionel Sambuc 	}
240*ebfedea0SLionel Sambuc 	if (first == 1 && (__check_rhosts_file || superuser)) {
241*ebfedea0SLionel Sambuc 		first = 0;
242*ebfedea0SLionel Sambuc 		if ((pwd = k_getpwnam((char*)luser)) == NULL)
243*ebfedea0SLionel Sambuc 			return (-1);
244*ebfedea0SLionel Sambuc 		snprintf (pbuf, sizeof(pbuf), "%s/.rhosts", pwd->pw_dir);
245*ebfedea0SLionel Sambuc 
246*ebfedea0SLionel Sambuc 		/*
247*ebfedea0SLionel Sambuc 		 * Change effective uid while opening .rhosts.  If root and
248*ebfedea0SLionel Sambuc 		 * reading an NFS mounted file system, can't read files that
249*ebfedea0SLionel Sambuc 		 * are protected read/write owner only.
250*ebfedea0SLionel Sambuc 		 */
251*ebfedea0SLionel Sambuc 		uid = geteuid();
252*ebfedea0SLionel Sambuc 		if (seteuid(pwd->pw_uid) < 0)
253*ebfedea0SLionel Sambuc 			return (-1);
254*ebfedea0SLionel Sambuc 		hostf = fopen(pbuf, "r");
255*ebfedea0SLionel Sambuc 		seteuid(uid);
256*ebfedea0SLionel Sambuc 
257*ebfedea0SLionel Sambuc 		if (hostf == NULL)
258*ebfedea0SLionel Sambuc 			return (-1);
259*ebfedea0SLionel Sambuc 		/*
260*ebfedea0SLionel Sambuc 		 * If not a regular file, or is owned by someone other than
261*ebfedea0SLionel Sambuc 		 * user or root or if writeable by anyone but the owner, quit.
262*ebfedea0SLionel Sambuc 		 */
263*ebfedea0SLionel Sambuc 		cp = NULL;
264*ebfedea0SLionel Sambuc 		if (lstat(pbuf, &sbuf) < 0)
265*ebfedea0SLionel Sambuc 			cp = ".rhosts lstat failed";
266*ebfedea0SLionel Sambuc 		else if (!S_ISREG(sbuf.st_mode))
267*ebfedea0SLionel Sambuc 			cp = ".rhosts not regular file";
268*ebfedea0SLionel Sambuc 		else if (fstat(fileno(hostf), &sbuf) < 0)
269*ebfedea0SLionel Sambuc 			cp = ".rhosts fstat failed";
270*ebfedea0SLionel Sambuc 		else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
271*ebfedea0SLionel Sambuc 			cp = "bad .rhosts owner";
272*ebfedea0SLionel Sambuc 		else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
273*ebfedea0SLionel Sambuc 			cp = ".rhosts writeable by other than owner";
274*ebfedea0SLionel Sambuc 		/* If there were any problems, quit. */
275*ebfedea0SLionel Sambuc 		if (cp) {
276*ebfedea0SLionel Sambuc 			__rcmd_errstr = cp;
277*ebfedea0SLionel Sambuc 			fclose(hostf);
278*ebfedea0SLionel Sambuc 			return (-1);
279*ebfedea0SLionel Sambuc 		}
280*ebfedea0SLionel Sambuc 		goto again;
281*ebfedea0SLionel Sambuc 	}
282*ebfedea0SLionel Sambuc 	return (-1);
283*ebfedea0SLionel Sambuc }
284