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. 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.
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
36 RCSID("$Id: iruserok.c,v 1.22 1999/09/16 20:06:06 assar Exp $");
41 #ifdef HAVE_SYS_TYPES_H
42 #include <sys/types.h>
44 #ifdef HAVE_NETINET_IN_H
45 #include <netinet/in.h>
47 #ifdef HAVE_NETINET_IN6_H
48 #include <netinet/in6.h>
50 #ifdef HAVE_NETINET6_IN6_H
51 #include <netinet6/in6.h>
53 #ifdef HAVE_RPCSVC_YPCLNT_H
54 #include <rpcsvc/ypclnt.h>
60 #ifdef HAVE_ARPA_INET_H
61 #include <arpa/inet.h>
66 int __check_rhosts_file = 1;
67 char *__rcmd_errstr = 0;
70 * Returns "true" if match, 0 if no match.
74 __icheckhost(unsigned raddr, const char *lhost)
80 /* Try for raw ip address first. */
81 if (isdigit((unsigned char)*lhost)
82 && (long)(laddr = inet_addr(lhost)) != -1)
83 return (raddr == laddr);
85 /* Better be a hostname. */
86 if ((hp = gethostbyname(lhost)) == NULL)
89 /* Spin through ip addresses. */
90 for (pp = hp->h_addr_list; *pp; ++pp)
91 if (memcmp(&raddr, *pp, sizeof(u_long)) == 0)
99 * Returns 0 if ok, -1 if not ok.
103 __ivaliduser(FILE *hostf, unsigned raddr, const char *luser,
108 char buf[MaxHostNameLen + 128]; /* host + login */
109 char hname[MaxHostNameLen];
111 /* Presumed guilty until proven innocent. */
112 int userok = 0, hostok = 0;
113 #ifdef HAVE_YP_GET_DEFAULT_DOMAIN
116 if (yp_get_default_domain(&ypdomain))
119 #define ypdomain NULL
121 /* We need to get the damn hostname back for netgroup matching. */
122 if ((hp = gethostbyaddr((char *)&raddr,
126 strlcpy(hname, hp->h_name, sizeof(hname));
128 while (fgets(buf, sizeof(buf), hostf)) {
130 /* Skip lines that are too long. */
131 if (strchr(p, '\n') == NULL) {
132 while ((ch = getc(hostf)) != '\n' && ch != EOF);
135 if (*p == '\n' || *p == '#') {
139 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
140 if (isupper((unsigned char)*p))
141 *p = tolower((unsigned char)*p);
144 if (*p == ' ' || *p == '\t') {
146 while (*p == ' ' || *p == '\t')
149 while (*p != '\n' && *p != ' ' &&
150 *p != '\t' && *p != '\0')
156 * Do +/- and +@/-@ checking. This looks really nasty,
157 * but it matches SunOS's behavior so far as I can tell.
161 if (!buf[1]) { /* '+' matches all hosts */
165 if (buf[1] == '@') /* match a host by netgroup */
166 hostok = innetgr((char *)&buf[2],
167 (char *)&hname, NULL, ypdomain);
168 else /* match a host by addr */
169 hostok = __icheckhost(raddr,(char *)&buf[1]);
171 case '-': /* reject '-' hosts and all their users */
173 if (innetgr((char *)&buf[2],
174 (char *)&hname, NULL, ypdomain))
177 if (__icheckhost(raddr,(char *)&buf[1]))
181 default: /* if no '+' or '-', do a simple match */
182 hostok = __icheckhost(raddr, buf);
187 if (!*(user+1)) { /* '+' matches all users */
191 if (*(user+1) == '@') /* match a user by netgroup */
192 userok = innetgr(user+2, NULL, (char *)ruser,
194 else /* match a user by direct specification */
195 userok = !(strcmp(ruser, user+1));
197 case '-': /* if we matched a hostname, */
198 if (hostok) { /* check for user field rejections */
201 if (*(user+1) == '@') {
202 if (innetgr(user+2, NULL,
203 (char *)ruser, ypdomain))
206 if (!strcmp(ruser, user+1))
211 default: /* no rejections: try to match the user */
213 userok = !(strcmp(ruser,*user ? user : luser));
216 if (hostok && userok)
223 * New .rhosts strategy: We are passed an ip address. We spin through
224 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
225 * has ip addresses, we don't have to trust a nameserver. When it
226 * contains hostnames, we spin through the list of addresses the nameserver
227 * gives us and look for a match.
229 * Returns 0 if ok, -1 if not ok.
232 iruserok(unsigned raddr, int superuser, const char *ruser, const char *luser)
240 char pbuf[MaxPathLen];
243 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
246 if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
252 if (first == 1 && (__check_rhosts_file || superuser)) {
254 if ((pwd = k_getpwnam((char*)luser)) == NULL)
256 snprintf (pbuf, sizeof(pbuf), "%s/.rhosts", pwd->pw_dir);
259 * Change effective uid while opening .rhosts. If root and
260 * reading an NFS mounted file system, can't read files that
261 * are protected read/write owner only.
264 seteuid(pwd->pw_uid);
265 hostf = fopen(pbuf, "r");
271 * If not a regular file, or is owned by someone other than
272 * user or root or if writeable by anyone but the owner, quit.
275 if (lstat(pbuf, &sbuf) < 0)
276 cp = ".rhosts lstat failed";
277 else if (!S_ISREG(sbuf.st_mode))
278 cp = ".rhosts not regular file";
279 else if (fstat(fileno(hostf), &sbuf) < 0)
280 cp = ".rhosts fstat failed";
281 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
282 cp = "bad .rhosts owner";
283 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
284 cp = ".rhosts writeable by other than owner";
285 /* If there were any problems, quit. */