[HEIMDAL-646] malloc(0) checks for AIX
[heimdal.git] / lib / roken / iruserok.c
blob3f0b5a0a5630867ed0b395af4cb0443396be00a0
1 /*
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
7 * are met:
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
27 * SUCH DAMAGE.
30 #include <config.h>
32 #include <stdio.h>
33 #include <ctype.h>
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
36 #endif
37 #ifdef HAVE_NETINET_IN_H
38 #include <netinet/in.h>
39 #endif
40 #ifdef HAVE_NETINET_IN6_H
41 #include <netinet/in6.h>
42 #endif
43 #ifdef HAVE_NETINET6_IN6_H
44 #include <netinet6/in6.h>
45 #endif
46 #ifdef HAVE_RPCSVC_YPCLNT_H
47 #include <rpcsvc/ypclnt.h>
48 #endif
50 #include "roken.h"
52 int __check_rhosts_file = 1;
53 char *__rcmd_errstr = 0;
56 * Returns "true" if match, 0 if no match.
58 static
59 int
60 __icheckhost(unsigned raddr, const char *lhost)
62 struct hostent *hp;
63 u_long laddr;
64 char **pp;
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)
73 return (0);
75 /* Spin through ip addresses. */
76 for (pp = hp->h_addr_list; *pp; ++pp)
77 if (memcmp(&raddr, *pp, sizeof(u_long)) == 0)
78 return (1);
80 /* No match. */
81 return (0);
85 * Returns 0 if ok, -1 if not ok.
87 static
88 int
89 __ivaliduser(FILE *hostf, unsigned raddr, const char *luser,
90 const char *ruser)
92 char *user, *p;
93 int ch;
94 char buf[MaxHostNameLen + 128]; /* host + login */
95 char hname[MaxHostNameLen];
96 struct hostent *hp;
97 /* Presumed guilty until proven innocent. */
98 int userok = 0, hostok = 0;
99 #ifdef HAVE_YP_GET_DEFAULT_DOMAIN
100 char *ypdomain;
102 if (yp_get_default_domain(&ypdomain))
103 ypdomain = NULL;
104 #else
105 #define ypdomain NULL
106 #endif
107 /* We need to get the damn hostname back for netgroup matching. */
108 if ((hp = gethostbyaddr((char *)&raddr,
109 sizeof(u_long),
110 AF_INET)) == NULL)
111 return (-1);
112 strlcpy(hname, hp->h_name, sizeof(hname));
114 while (fgets(buf, sizeof(buf), hostf)) {
115 p = buf;
116 /* Skip lines that are too long. */
117 if (strchr(p, '\n') == NULL) {
118 while ((ch = getc(hostf)) != '\n' && ch != EOF);
119 continue;
121 if (*p == '\n' || *p == '#') {
122 /* comment... */
123 continue;
125 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
126 if (isupper((unsigned char)*p))
127 *p = tolower((unsigned char)*p);
128 p++;
130 if (*p == ' ' || *p == '\t') {
131 *p++ = '\0';
132 while (*p == ' ' || *p == '\t')
133 p++;
134 user = p;
135 while (*p != '\n' && *p != ' ' &&
136 *p != '\t' && *p != '\0')
137 p++;
138 } else
139 user = p;
140 *p = '\0';
142 * Do +/- and +@/-@ checking. This looks really nasty,
143 * but it matches SunOS's behavior so far as I can tell.
145 switch(buf[0]) {
146 case '+':
147 if (!buf[1]) { /* '+' matches all hosts */
148 hostok = 1;
149 break;
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]);
156 break;
157 case '-': /* reject '-' hosts and all their users */
158 if (buf[1] == '@') {
159 if (innetgr((char *)&buf[2],
160 (char *)&hname, NULL, ypdomain))
161 return(-1);
162 } else {
163 if (__icheckhost(raddr,(char *)&buf[1]))
164 return(-1);
166 break;
167 default: /* if no '+' or '-', do a simple match */
168 hostok = __icheckhost(raddr, buf);
169 break;
171 switch(*user) {
172 case '+':
173 if (!*(user+1)) { /* '+' matches all users */
174 userok = 1;
175 break;
177 if (*(user+1) == '@') /* match a user by netgroup */
178 userok = innetgr(user+2, NULL, (char *)ruser,
179 ypdomain);
180 else /* match a user by direct specification */
181 userok = !(strcmp(ruser, user+1));
182 break;
183 case '-': /* if we matched a hostname, */
184 if (hostok) { /* check for user field rejections */
185 if (!*(user+1))
186 return(-1);
187 if (*(user+1) == '@') {
188 if (innetgr(user+2, NULL,
189 (char *)ruser, ypdomain))
190 return(-1);
191 } else {
192 if (!strcmp(ruser, user+1))
193 return(-1);
196 break;
197 default: /* no rejections: try to match the user */
198 if (hostok)
199 userok = !(strcmp(ruser,*user ? user : luser));
200 break;
202 if (hostok && userok)
203 return(0);
205 return (-1);
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 int ROKEN_LIB_FUNCTION
218 iruserok(unsigned raddr, int superuser, const char *ruser, const char *luser)
220 char *cp;
221 struct stat sbuf;
222 struct passwd *pwd;
223 FILE *hostf;
224 uid_t uid;
225 int first;
226 char pbuf[MaxPathLen];
228 first = 1;
229 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
230 again:
231 if (hostf) {
232 if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
233 fclose(hostf);
234 return (0);
236 fclose(hostf);
238 if (first == 1 && (__check_rhosts_file || superuser)) {
239 first = 0;
240 if ((pwd = k_getpwnam((char*)luser)) == NULL)
241 return (-1);
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.
249 uid = geteuid();
250 if (seteuid(pwd->pw_uid) < 0)
251 return (-1);
252 hostf = fopen(pbuf, "r");
253 seteuid(uid);
255 if (hostf == NULL)
256 return (-1);
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.
261 cp = NULL;
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. */
273 if (cp) {
274 __rcmd_errstr = cp;
275 fclose(hostf);
276 return (-1);
278 goto again;
280 return (-1);