2 * Copyright (c) 1983, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 RCSID("$Id: iruserok.c 17879 2006-08-08 21:50:40Z lha $");
37 #ifdef HAVE_SYS_TYPES_H
38 #include <sys/types.h>
40 #ifdef HAVE_NETINET_IN_H
41 #include <netinet/in.h>
43 #ifdef HAVE_NETINET_IN6_H
44 #include <netinet/in6.h>
46 #ifdef HAVE_NETINET6_IN6_H
47 #include <netinet6/in6.h>
49 #ifdef HAVE_RPCSVC_YPCLNT_H
50 #include <rpcsvc/ypclnt.h>
55 int __check_rhosts_file = 1;
56 char *__rcmd_errstr = 0;
59 * Returns "true" if match, 0 if no match.
63 __icheckhost(unsigned raddr, const char *lhost)
69 /* Try for raw ip address first. */
70 if (isdigit((unsigned char)*lhost)
71 && (long)(laddr = inet_addr(lhost)) != -1)
72 return (raddr == laddr);
74 /* Better be a hostname. */
75 if ((hp = gethostbyname(lhost)) == NULL)
78 /* Spin through ip addresses. */
79 for (pp = hp->h_addr_list; *pp; ++pp)
80 if (memcmp(&raddr, *pp, sizeof(u_long)) == 0)
88 * Returns 0 if ok, -1 if not ok.
92 __ivaliduser(FILE *hostf, unsigned raddr, const char *luser,
97 char buf[MaxHostNameLen + 128]; /* host + login */
98 char hname[MaxHostNameLen];
100 /* Presumed guilty until proven innocent. */
101 int userok = 0, hostok = 0;
102 #ifdef HAVE_YP_GET_DEFAULT_DOMAIN
105 if (yp_get_default_domain(&ypdomain))
108 #define ypdomain NULL
110 /* We need to get the damn hostname back for netgroup matching. */
111 if ((hp = gethostbyaddr((char *)&raddr,
115 strlcpy(hname, hp->h_name, sizeof(hname));
117 while (fgets(buf, sizeof(buf), hostf)) {
119 /* Skip lines that are too long. */
120 if (strchr(p, '\n') == NULL) {
121 while ((ch = getc(hostf)) != '\n' && ch != EOF);
124 if (*p == '\n' || *p == '#') {
128 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
129 if (isupper((unsigned char)*p))
130 *p = tolower((unsigned char)*p);
133 if (*p == ' ' || *p == '\t') {
135 while (*p == ' ' || *p == '\t')
138 while (*p != '\n' && *p != ' ' &&
139 *p != '\t' && *p != '\0')
145 * Do +/- and +@/-@ checking. This looks really nasty,
146 * but it matches SunOS's behavior so far as I can tell.
150 if (!buf[1]) { /* '+' matches all hosts */
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]);
160 case '-': /* reject '-' hosts and all their users */
162 if (innetgr((char *)&buf[2],
163 (char *)&hname, NULL, ypdomain))
166 if (__icheckhost(raddr,(char *)&buf[1]))
170 default: /* if no '+' or '-', do a simple match */
171 hostok = __icheckhost(raddr, buf);
176 if (!*(user+1)) { /* '+' matches all users */
180 if (*(user+1) == '@') /* match a user by netgroup */
181 userok = innetgr(user+2, NULL, (char *)ruser,
183 else /* match a user by direct specification */
184 userok = !(strcmp(ruser, user+1));
186 case '-': /* if we matched a hostname, */
187 if (hostok) { /* check for user field rejections */
190 if (*(user+1) == '@') {
191 if (innetgr(user+2, NULL,
192 (char *)ruser, ypdomain))
195 if (!strcmp(ruser, user+1))
200 default: /* no rejections: try to match the user */
202 userok = !(strcmp(ruser,*user ? user : luser));
205 if (hostok && userok)
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.
218 * Returns 0 if ok, -1 if not ok.
220 int ROKEN_LIB_FUNCTION
221 iruserok(unsigned raddr, int superuser, const char *ruser, const char *luser)
229 char pbuf[MaxPathLen];
232 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
235 if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
241 if (first == 1 && (__check_rhosts_file || superuser)) {
243 if ((pwd = k_getpwnam((char*)luser)) == NULL)
245 snprintf (pbuf, sizeof(pbuf), "%s/.rhosts", pwd->pw_dir);
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.
253 if (seteuid(pwd->pw_uid) < 0)
255 hostf = fopen(pbuf, "r");
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.
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. */