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.23 1999/12/05 13:27:05 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>
59 int __check_rhosts_file = 1;
60 char *__rcmd_errstr = 0;
63 * Returns "true" if match, 0 if no match.
67 __icheckhost(unsigned raddr, const char *lhost)
73 /* Try for raw ip address first. */
74 if (isdigit((unsigned char)*lhost)
75 && (long)(laddr = inet_addr(lhost)) != -1)
76 return (raddr == laddr);
78 /* Better be a hostname. */
79 if ((hp = gethostbyname(lhost)) == NULL)
82 /* Spin through ip addresses. */
83 for (pp = hp->h_addr_list; *pp; ++pp)
84 if (memcmp(&raddr, *pp, sizeof(u_long)) == 0)
92 * Returns 0 if ok, -1 if not ok.
96 __ivaliduser(FILE *hostf, unsigned raddr, const char *luser,
101 char buf[MaxHostNameLen + 128]; /* host + login */
102 char hname[MaxHostNameLen];
104 /* Presumed guilty until proven innocent. */
105 int userok = 0, hostok = 0;
106 #ifdef HAVE_YP_GET_DEFAULT_DOMAIN
109 if (yp_get_default_domain(&ypdomain))
112 #define ypdomain NULL
114 /* We need to get the damn hostname back for netgroup matching. */
115 if ((hp = gethostbyaddr((char *)&raddr,
119 strlcpy(hname, hp->h_name, sizeof(hname));
121 while (fgets(buf, sizeof(buf), hostf)) {
123 /* Skip lines that are too long. */
124 if (strchr(p, '\n') == NULL) {
125 while ((ch = getc(hostf)) != '\n' && ch != EOF);
128 if (*p == '\n' || *p == '#') {
132 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
133 if (isupper((unsigned char)*p))
134 *p = tolower((unsigned char)*p);
137 if (*p == ' ' || *p == '\t') {
139 while (*p == ' ' || *p == '\t')
142 while (*p != '\n' && *p != ' ' &&
143 *p != '\t' && *p != '\0')
149 * Do +/- and +@/-@ checking. This looks really nasty,
150 * but it matches SunOS's behavior so far as I can tell.
154 if (!buf[1]) { /* '+' matches all hosts */
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]);
164 case '-': /* reject '-' hosts and all their users */
166 if (innetgr((char *)&buf[2],
167 (char *)&hname, NULL, ypdomain))
170 if (__icheckhost(raddr,(char *)&buf[1]))
174 default: /* if no '+' or '-', do a simple match */
175 hostok = __icheckhost(raddr, buf);
180 if (!*(user+1)) { /* '+' matches all users */
184 if (*(user+1) == '@') /* match a user by netgroup */
185 userok = innetgr(user+2, NULL, (char *)ruser,
187 else /* match a user by direct specification */
188 userok = !(strcmp(ruser, user+1));
190 case '-': /* if we matched a hostname, */
191 if (hostok) { /* check for user field rejections */
194 if (*(user+1) == '@') {
195 if (innetgr(user+2, NULL,
196 (char *)ruser, ypdomain))
199 if (!strcmp(ruser, user+1))
204 default: /* no rejections: try to match the user */
206 userok = !(strcmp(ruser,*user ? user : luser));
209 if (hostok && userok)
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.
222 * Returns 0 if ok, -1 if not ok.
225 iruserok(unsigned raddr, int superuser, const char *ruser, const char *luser)
233 char pbuf[MaxPathLen];
236 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
239 if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
245 if (first == 1 && (__check_rhosts_file || superuser)) {
247 if ((pwd = k_getpwnam((char*)luser)) == NULL)
249 snprintf (pbuf, sizeof(pbuf), "%s/.rhosts", pwd->pw_dir);
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.
257 seteuid(pwd->pw_uid);
258 hostf = fopen(pbuf, "r");
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.
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. */