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