1 /* $NetBSD: iruserok.c,v 1.1.1.1 2011/04/13 18:15:42 elric Exp $ */
4 * Copyright (c) 1983, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #ifdef HAVE_SYS_TYPES_H
37 #include <sys/types.h>
39 #ifdef HAVE_NETINET_IN_H
40 #include <netinet/in.h>
42 #ifdef HAVE_NETINET_IN6_H
43 #include <netinet/in6.h>
45 #ifdef HAVE_NETINET6_IN6_H
46 #include <netinet6/in6.h>
48 #ifdef HAVE_RPCSVC_YPCLNT_H
49 #include <rpcsvc/ypclnt.h>
52 #include <krb5/roken.h>
54 int __check_rhosts_file
= 1;
55 char *__rcmd_errstr
= 0;
58 * Returns "true" if match, 0 if no match.
62 __icheckhost(unsigned raddr
, const char *lhost
)
68 /* Try for raw ip address first. */
69 if (isdigit((unsigned char)*lhost
)
70 && (long)(laddr
= inet_addr(lhost
)) != -1)
71 return (raddr
== laddr
);
73 /* Better be a hostname. */
74 if ((hp
= gethostbyname(lhost
)) == NULL
)
77 /* Spin through ip addresses. */
78 for (pp
= hp
->h_addr_list
; *pp
; ++pp
)
79 if (memcmp(&raddr
, *pp
, sizeof(u_long
)) == 0)
87 * Returns 0 if ok, -1 if not ok.
91 __ivaliduser(FILE *hostf
, unsigned raddr
, const char *luser
,
96 char buf
[MaxHostNameLen
+ 128]; /* host + login */
97 char hname
[MaxHostNameLen
];
99 /* Presumed guilty until proven innocent. */
100 int userok
= 0, hostok
= 0;
101 #ifdef HAVE_YP_GET_DEFAULT_DOMAIN
104 if (yp_get_default_domain(&ypdomain
))
107 #define ypdomain NULL
109 /* We need to get the damn hostname back for netgroup matching. */
110 if ((hp
= gethostbyaddr((char *)&raddr
,
114 strlcpy(hname
, hp
->h_name
, sizeof(hname
));
116 while (fgets(buf
, sizeof(buf
), hostf
)) {
118 /* Skip lines that are too long. */
119 if (strchr(p
, '\n') == NULL
) {
120 while ((ch
= getc(hostf
)) != '\n' && ch
!= EOF
);
123 if (*p
== '\n' || *p
== '#') {
127 while (*p
!= '\n' && *p
!= ' ' && *p
!= '\t' && *p
!= '\0') {
128 if (isupper((unsigned char)*p
))
129 *p
= tolower((unsigned char)*p
);
132 if (*p
== ' ' || *p
== '\t') {
134 while (*p
== ' ' || *p
== '\t')
137 while (*p
!= '\n' && *p
!= ' ' &&
138 *p
!= '\t' && *p
!= '\0')
144 * Do +/- and +@/-@ checking. This looks really nasty,
145 * but it matches SunOS's behavior so far as I can tell.
149 if (!buf
[1]) { /* '+' matches all hosts */
153 if (buf
[1] == '@') /* match a host by netgroup */
154 hostok
= innetgr((char *)&buf
[2],
155 (char *)&hname
, NULL
, ypdomain
);
156 else /* match a host by addr */
157 hostok
= __icheckhost(raddr
,(char *)&buf
[1]);
159 case '-': /* reject '-' hosts and all their users */
161 if (innetgr((char *)&buf
[2],
162 (char *)&hname
, NULL
, ypdomain
))
165 if (__icheckhost(raddr
,(char *)&buf
[1]))
169 default: /* if no '+' or '-', do a simple match */
170 hostok
= __icheckhost(raddr
, buf
);
175 if (!*(user
+1)) { /* '+' matches all users */
179 if (*(user
+1) == '@') /* match a user by netgroup */
180 userok
= innetgr(user
+2, NULL
, (char *)ruser
,
182 else /* match a user by direct specification */
183 userok
= !(strcmp(ruser
, user
+1));
185 case '-': /* if we matched a hostname, */
186 if (hostok
) { /* check for user field rejections */
189 if (*(user
+1) == '@') {
190 if (innetgr(user
+2, NULL
,
191 (char *)ruser
, ypdomain
))
194 if (!strcmp(ruser
, user
+1))
199 default: /* no rejections: try to match the user */
201 userok
= !(strcmp(ruser
,*user
? user
: luser
));
204 if (hostok
&& userok
)
211 * New .rhosts strategy: We are passed an ip address. We spin through
212 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
213 * has ip addresses, we don't have to trust a nameserver. When it
214 * contains hostnames, we spin through the list of addresses the nameserver
215 * gives us and look for a match.
217 * Returns 0 if ok, -1 if not ok.
219 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
220 iruserok(unsigned raddr
, int superuser
, const char *ruser
, const char *luser
)
228 char pbuf
[MaxPathLen
];
231 hostf
= superuser
? NULL
: fopen(_PATH_HEQUIV
, "r");
234 if (__ivaliduser(hostf
, raddr
, luser
, ruser
) == 0) {
240 if (first
== 1 && (__check_rhosts_file
|| superuser
)) {
242 if ((pwd
= k_getpwnam((char*)luser
)) == NULL
)
244 snprintf (pbuf
, sizeof(pbuf
), "%s/.rhosts", pwd
->pw_dir
);
247 * Change effective uid while opening .rhosts. If root and
248 * reading an NFS mounted file system, can't read files that
249 * are protected read/write owner only.
252 if (seteuid(pwd
->pw_uid
) < 0)
254 hostf
= fopen(pbuf
, "r");
260 * If not a regular file, or is owned by someone other than
261 * user or root or if writeable by anyone but the owner, quit.
264 if (lstat(pbuf
, &sbuf
) < 0)
265 cp
= ".rhosts lstat failed";
266 else if (!S_ISREG(sbuf
.st_mode
))
267 cp
= ".rhosts not regular file";
268 else if (fstat(fileno(hostf
), &sbuf
) < 0)
269 cp
= ".rhosts fstat failed";
270 else if (sbuf
.st_uid
&& sbuf
.st_uid
!= pwd
->pw_uid
)
271 cp
= "bad .rhosts owner";
272 else if (sbuf
.st_mode
& (S_IWGRP
|S_IWOTH
))
273 cp
= ".rhosts writeable by other than owner";
274 /* If there were any problems, quit. */