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
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
37 #ifdef HAVE_NETINET_IN_H
38 #include <netinet/in.h>
40 #ifdef HAVE_NETINET_IN6_H
41 #include <netinet/in6.h>
43 #ifdef HAVE_NETINET6_IN6_H
44 #include <netinet6/in6.h>
46 #ifdef HAVE_RPCSVC_YPCLNT_H
47 #include <rpcsvc/ypclnt.h>
52 int __check_rhosts_file = 1;
53 char *__rcmd_errstr = 0;
56 * Returns "true" if match, 0 if no match.
60 __icheckhost(unsigned raddr, const char *lhost)
66 /* Try for raw ip address first. */
67 if (isdigit((unsigned char)*lhost)
68 && (long)(laddr = inet_addr(lhost)) != -1)
69 return (raddr == laddr);
71 /* Better be a hostname. */
72 if ((hp = gethostbyname(lhost)) == NULL)
75 /* Spin through ip addresses. */
76 for (pp = hp->h_addr_list; *pp; ++pp)
77 if (memcmp(&raddr, *pp, sizeof(u_long)) == 0)
85 * Returns 0 if ok, -1 if not ok.
89 __ivaliduser(FILE *hostf, unsigned raddr, const char *luser,
94 char buf[MaxHostNameLen + 128]; /* host + login */
95 char hname[MaxHostNameLen];
97 /* Presumed guilty until proven innocent. */
98 int userok = 0, hostok = 0;
99 #ifdef HAVE_YP_GET_DEFAULT_DOMAIN
102 if (yp_get_default_domain(&ypdomain))
105 #define ypdomain NULL
107 /* We need to get the damn hostname back for netgroup matching. */
108 if ((hp = gethostbyaddr((char *)&raddr,
112 strlcpy(hname, hp->h_name, sizeof(hname));
114 while (fgets(buf, sizeof(buf), hostf)) {
116 /* Skip lines that are too long. */
117 if (strchr(p, '\n') == NULL) {
118 while ((ch = getc(hostf)) != '\n' && ch != EOF);
121 if (*p == '\n' || *p == '#') {
125 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
126 if (isupper((unsigned char)*p))
127 *p = tolower((unsigned char)*p);
130 if (*p == ' ' || *p == '\t') {
132 while (*p == ' ' || *p == '\t')
135 while (*p != '\n' && *p != ' ' &&
136 *p != '\t' && *p != '\0')
142 * Do +/- and +@/-@ checking. This looks really nasty,
143 * but it matches SunOS's behavior so far as I can tell.
147 if (!buf[1]) { /* '+' matches all hosts */
151 if (buf[1] == '@') /* match a host by netgroup */
152 hostok = innetgr((char *)&buf[2],
153 (char *)&hname, NULL, ypdomain);
154 else /* match a host by addr */
155 hostok = __icheckhost(raddr,(char *)&buf[1]);
157 case '-': /* reject '-' hosts and all their users */
159 if (innetgr((char *)&buf[2],
160 (char *)&hname, NULL, ypdomain))
163 if (__icheckhost(raddr,(char *)&buf[1]))
167 default: /* if no '+' or '-', do a simple match */
168 hostok = __icheckhost(raddr, buf);
173 if (!*(user+1)) { /* '+' matches all users */
177 if (*(user+1) == '@') /* match a user by netgroup */
178 userok = innetgr(user+2, NULL, (char *)ruser,
180 else /* match a user by direct specification */
181 userok = !(strcmp(ruser, user+1));
183 case '-': /* if we matched a hostname, */
184 if (hostok) { /* check for user field rejections */
187 if (*(user+1) == '@') {
188 if (innetgr(user+2, NULL,
189 (char *)ruser, ypdomain))
192 if (!strcmp(ruser, user+1))
197 default: /* no rejections: try to match the user */
199 userok = !(strcmp(ruser,*user ? user : luser));
202 if (hostok && userok)
209 * New .rhosts strategy: We are passed an ip address. We spin through
210 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
211 * has ip addresses, we don't have to trust a nameserver. When it
212 * contains hostnames, we spin through the list of addresses the nameserver
213 * gives us and look for a match.
215 * Returns 0 if ok, -1 if not ok.
217 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
218 iruserok(unsigned raddr, int superuser, const char *ruser, const char *luser)
226 char pbuf[MaxPathLen];
229 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
232 if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
238 if (first == 1 && (__check_rhosts_file || superuser)) {
240 if ((pwd = k_getpwnam((char*)luser)) == NULL)
242 snprintf (pbuf, sizeof(pbuf), "%s/.rhosts", pwd->pw_dir);
245 * Change effective uid while opening .rhosts. If root and
246 * reading an NFS mounted file system, can't read files that
247 * are protected read/write owner only.
250 if (seteuid(pwd->pw_uid) < 0)
252 hostf = fopen(pbuf, "r");
258 * If not a regular file, or is owned by someone other than
259 * user or root or if writeable by anyone but the owner, quit.
262 if (lstat(pbuf, &sbuf) < 0)
263 cp = ".rhosts lstat failed";
264 else if (!S_ISREG(sbuf.st_mode))
265 cp = ".rhosts not regular file";
266 else if (fstat(fileno(hostf), &sbuf) < 0)
267 cp = ".rhosts fstat failed";
268 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
269 cp = "bad .rhosts owner";
270 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
271 cp = ".rhosts writeable by other than owner";
272 /* If there were any problems, quit. */