xref: /freebsd/crypto/heimdal/lib/roken/iruserok.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*
2  * Copyright (c) 1983, 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 
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 RCSID("$Id: iruserok.c,v 1.23 1999/12/05 13:27:05 assar Exp $");
37 #endif
38 
39 #include <stdio.h>
40 #include <ctype.h>
41 #ifdef HAVE_SYS_TYPES_H
42 #include <sys/types.h>
43 #endif
44 #ifdef HAVE_NETINET_IN_H
45 #include <netinet/in.h>
46 #endif
47 #ifdef HAVE_NETINET_IN6_H
48 #include <netinet/in6.h>
49 #endif
50 #ifdef HAVE_NETINET6_IN6_H
51 #include <netinet6/in6.h>
52 #endif
53 #ifdef HAVE_RPCSVC_YPCLNT_H
54 #include <rpcsvc/ypclnt.h>
55 #endif
56 
57 #include "roken.h"
58 
59 int     __check_rhosts_file = 1;
60 char    *__rcmd_errstr = 0;
61 
62 /*
63  * Returns "true" if match, 0 if no match.
64  */
65 static
66 int
67 __icheckhost(unsigned raddr, const char *lhost)
68 {
69 	struct hostent *hp;
70 	u_long laddr;
71 	char **pp;
72 
73 	/* Try for raw ip address first. */
74 	if (isdigit((unsigned char)*lhost)
75 	    && (long)(laddr = inet_addr(lhost)) != -1)
76 		return (raddr == laddr);
77 
78 	/* Better be a hostname. */
79 	if ((hp = gethostbyname(lhost)) == NULL)
80 		return (0);
81 
82 	/* Spin through ip addresses. */
83 	for (pp = hp->h_addr_list; *pp; ++pp)
84 	        if (memcmp(&raddr, *pp, sizeof(u_long)) == 0)
85 			return (1);
86 
87 	/* No match. */
88 	return (0);
89 }
90 
91 /*
92  * Returns 0 if ok, -1 if not ok.
93  */
94 static
95 int
96 __ivaliduser(FILE *hostf, unsigned raddr, const char *luser,
97 	     const char *ruser)
98 {
99 	char *user, *p;
100 	int ch;
101 	char buf[MaxHostNameLen + 128];		/* host + login */
102 	char hname[MaxHostNameLen];
103 	struct hostent *hp;
104 	/* Presumed guilty until proven innocent. */
105 	int userok = 0, hostok = 0;
106 #ifdef HAVE_YP_GET_DEFAULT_DOMAIN
107 	char *ypdomain;
108 
109 	if (yp_get_default_domain(&ypdomain))
110 		ypdomain = NULL;
111 #else
112 #define	ypdomain NULL
113 #endif
114 	/* We need to get the damn hostname back for netgroup matching. */
115 	if ((hp = gethostbyaddr((char *)&raddr,
116 				sizeof(u_long),
117 				AF_INET)) == NULL)
118 		return (-1);
119 	strlcpy(hname, hp->h_name, sizeof(hname));
120 
121 	while (fgets(buf, sizeof(buf), hostf)) {
122 		p = buf;
123 		/* Skip lines that are too long. */
124 		if (strchr(p, '\n') == NULL) {
125 			while ((ch = getc(hostf)) != '\n' && ch != EOF);
126 			continue;
127 		}
128 		if (*p == '\n' || *p == '#') {
129 			/* comment... */
130 			continue;
131 		}
132 		while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
133 		        if (isupper((unsigned char)*p))
134 			    *p = tolower((unsigned char)*p);
135 			p++;
136 		}
137 		if (*p == ' ' || *p == '\t') {
138 			*p++ = '\0';
139 			while (*p == ' ' || *p == '\t')
140 				p++;
141 			user = p;
142 			while (*p != '\n' && *p != ' ' &&
143 			    *p != '\t' && *p != '\0')
144 				p++;
145 		} else
146 			user = p;
147 		*p = '\0';
148 		/*
149 		 * Do +/- and +@/-@ checking. This looks really nasty,
150 		 * but it matches SunOS's behavior so far as I can tell.
151 		 */
152 		switch(buf[0]) {
153 		case '+':
154 			if (!buf[1]) {     /* '+' matches all hosts */
155 				hostok = 1;
156 				break;
157 			}
158 			if (buf[1] == '@')  /* match a host by netgroup */
159 				hostok = innetgr((char *)&buf[2],
160 					(char *)&hname, NULL, ypdomain);
161 			else		/* match a host by addr */
162 				hostok = __icheckhost(raddr,(char *)&buf[1]);
163 			break;
164 		case '-':     /* reject '-' hosts and all their users */
165 			if (buf[1] == '@') {
166 				if (innetgr((char *)&buf[2],
167 					      (char *)&hname, NULL, ypdomain))
168 					return(-1);
169 			} else {
170 				if (__icheckhost(raddr,(char *)&buf[1]))
171 					return(-1);
172 			}
173 			break;
174 		default:  /* if no '+' or '-', do a simple match */
175 			hostok = __icheckhost(raddr, buf);
176 			break;
177 		}
178 		switch(*user) {
179 		case '+':
180 			if (!*(user+1)) {      /* '+' matches all users */
181 				userok = 1;
182 				break;
183 			}
184 			if (*(user+1) == '@')  /* match a user by netgroup */
185 				userok = innetgr(user+2, NULL, (char *)ruser,
186 						 ypdomain);
187 			else	   /* match a user by direct specification */
188 				userok = !(strcmp(ruser, user+1));
189 			break;
190 		case '-': 		/* if we matched a hostname, */
191 			if (hostok) {   /* check for user field rejections */
192 				if (!*(user+1))
193 					return(-1);
194 				if (*(user+1) == '@') {
195 					if (innetgr(user+2, NULL,
196 						    (char *)ruser, ypdomain))
197 						return(-1);
198 				} else {
199 					if (!strcmp(ruser, user+1))
200 						return(-1);
201 				}
202 			}
203 			break;
204 		default:	/* no rejections: try to match the user */
205 			if (hostok)
206 				userok = !(strcmp(ruser,*user ? user : luser));
207 			break;
208 		}
209 		if (hostok && userok)
210 			return(0);
211 	}
212 	return (-1);
213 }
214 
215 /*
216  * New .rhosts strategy: We are passed an ip address. We spin through
217  * hosts.equiv and .rhosts looking for a match. When the .rhosts only
218  * has ip addresses, we don't have to trust a nameserver.  When it
219  * contains hostnames, we spin through the list of addresses the nameserver
220  * gives us and look for a match.
221  *
222  * Returns 0 if ok, -1 if not ok.
223  */
224 int
225 iruserok(unsigned raddr, int superuser, const char *ruser, const char *luser)
226 {
227 	char *cp;
228 	struct stat sbuf;
229 	struct passwd *pwd;
230 	FILE *hostf;
231 	uid_t uid;
232 	int first;
233 	char pbuf[MaxPathLen];
234 
235 	first = 1;
236 	hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
237 again:
238 	if (hostf) {
239 		if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
240 			fclose(hostf);
241 			return (0);
242 		}
243 		fclose(hostf);
244 	}
245 	if (first == 1 && (__check_rhosts_file || superuser)) {
246 		first = 0;
247 		if ((pwd = k_getpwnam((char*)luser)) == NULL)
248 			return (-1);
249 		snprintf (pbuf, sizeof(pbuf), "%s/.rhosts", pwd->pw_dir);
250 
251 		/*
252 		 * Change effective uid while opening .rhosts.  If root and
253 		 * reading an NFS mounted file system, can't read files that
254 		 * are protected read/write owner only.
255 		 */
256 		uid = geteuid();
257 		seteuid(pwd->pw_uid);
258 		hostf = fopen(pbuf, "r");
259 		seteuid(uid);
260 
261 		if (hostf == NULL)
262 			return (-1);
263 		/*
264 		 * If not a regular file, or is owned by someone other than
265 		 * user or root or if writeable by anyone but the owner, quit.
266 		 */
267 		cp = NULL;
268 		if (lstat(pbuf, &sbuf) < 0)
269 			cp = ".rhosts lstat failed";
270 		else if (!S_ISREG(sbuf.st_mode))
271 			cp = ".rhosts not regular file";
272 		else if (fstat(fileno(hostf), &sbuf) < 0)
273 			cp = ".rhosts fstat failed";
274 		else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
275 			cp = "bad .rhosts owner";
276 		else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
277 			cp = ".rhosts writeable by other than owner";
278 		/* If there were any problems, quit. */
279 		if (cp) {
280 			__rcmd_errstr = cp;
281 			fclose(hostf);
282 			return (-1);
283 		}
284 		goto again;
285 	}
286 	return (-1);
287 }
288