Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / kerberosIV / lib / roken / iruserok.c
1 /*
2  * Copyright (c) 1983, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
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. 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.
20  *
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
31  * SUCH DAMAGE.
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 RCSID("$Id: iruserok.c,v 1.22 1999/09/16 20:06:06 assar Exp $");
37 #endif
38
39 #include <stdio.h>
40 #include <ctype.h>
41 #ifdef HAVE_SYS_TYPES_H
42 #include <sys/types.h>
43 #endif
44 #ifdef HAVE_NETINET_IN_H
45 #include <netinet/in.h>
46 #endif
47 #ifdef HAVE_NETINET_IN6_H
48 #include <netinet/in6.h>
49 #endif
50 #ifdef HAVE_NETINET6_IN6_H
51 #include <netinet6/in6.h>
52 #endif
53 #ifdef HAVE_RPCSVC_YPCLNT_H
54 #include <rpcsvc/ypclnt.h>
55 #endif
56
57 #ifdef HAVE_NETDB_H
58 #include <netdb.h>
59 #endif
60 #ifdef HAVE_ARPA_INET_H
61 #include <arpa/inet.h>
62 #endif
63
64 #include "roken.h"
65
66 int     __check_rhosts_file = 1;
67 char    *__rcmd_errstr = 0;
68
69 /*
70  * Returns "true" if match, 0 if no match.
71  */
72 static
73 int
74 __icheckhost(unsigned raddr, const char *lhost)
75 {
76         struct hostent *hp;
77         u_long laddr;
78         char **pp;
79
80         /* Try for raw ip address first. */
81         if (isdigit((unsigned char)*lhost)
82             && (long)(laddr = inet_addr(lhost)) != -1)
83                 return (raddr == laddr);
84
85         /* Better be a hostname. */
86         if ((hp = gethostbyname(lhost)) == NULL)
87                 return (0);
88
89         /* Spin through ip addresses. */
90         for (pp = hp->h_addr_list; *pp; ++pp)
91                 if (memcmp(&raddr, *pp, sizeof(u_long)) == 0)
92                         return (1);
93
94         /* No match. */
95         return (0);
96 }
97
98 /*
99  * Returns 0 if ok, -1 if not ok.
100  */
101 static
102 int
103 __ivaliduser(FILE *hostf, unsigned raddr, const char *luser,
104              const char *ruser)
105 {
106         char *user, *p;
107         int ch;
108         char buf[MaxHostNameLen + 128];         /* host + login */
109         char hname[MaxHostNameLen];
110         struct hostent *hp;
111         /* Presumed guilty until proven innocent. */
112         int userok = 0, hostok = 0;
113 #ifdef HAVE_YP_GET_DEFAULT_DOMAIN
114         char *ypdomain;
115
116         if (yp_get_default_domain(&ypdomain))
117                 ypdomain = NULL;
118 #else
119 #define ypdomain NULL
120 #endif
121         /* We need to get the damn hostname back for netgroup matching. */
122         if ((hp = gethostbyaddr((char *)&raddr,
123                                 sizeof(u_long),
124                                 AF_INET)) == NULL)
125                 return (-1);
126         strlcpy(hname, hp->h_name, sizeof(hname));
127
128         while (fgets(buf, sizeof(buf), hostf)) {
129                 p = buf;
130                 /* Skip lines that are too long. */
131                 if (strchr(p, '\n') == NULL) {
132                         while ((ch = getc(hostf)) != '\n' && ch != EOF);
133                         continue;
134                 }
135                 if (*p == '\n' || *p == '#') {
136                         /* comment... */
137                         continue;
138                 }
139                 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
140                         if (isupper((unsigned char)*p))
141                             *p = tolower((unsigned char)*p);
142                         p++;
143                 }
144                 if (*p == ' ' || *p == '\t') {
145                         *p++ = '\0';
146                         while (*p == ' ' || *p == '\t')
147                                 p++;
148                         user = p;
149                         while (*p != '\n' && *p != ' ' &&
150                             *p != '\t' && *p != '\0')
151                                 p++;
152                 } else
153                         user = p;
154                 *p = '\0';
155                 /*
156                  * Do +/- and +@/-@ checking. This looks really nasty,
157                  * but it matches SunOS's behavior so far as I can tell.
158                  */
159                 switch(buf[0]) {
160                 case '+':
161                         if (!buf[1]) {     /* '+' matches all hosts */
162                                 hostok = 1;
163                                 break;
164                         }
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]);
170                         break;
171                 case '-':     /* reject '-' hosts and all their users */
172                         if (buf[1] == '@') {
173                                 if (innetgr((char *)&buf[2],
174                                               (char *)&hname, NULL, ypdomain))
175                                         return(-1);
176                         } else {
177                                 if (__icheckhost(raddr,(char *)&buf[1]))
178                                         return(-1);
179                         }
180                         break;
181                 default:  /* if no '+' or '-', do a simple match */
182                         hostok = __icheckhost(raddr, buf);
183                         break;
184                 }
185                 switch(*user) {
186                 case '+':
187                         if (!*(user+1)) {      /* '+' matches all users */
188                                 userok = 1;
189                                 break;
190                         }
191                         if (*(user+1) == '@')  /* match a user by netgroup */
192                                 userok = innetgr(user+2, NULL, (char *)ruser,
193                                                  ypdomain);
194                         else       /* match a user by direct specification */
195                                 userok = !(strcmp(ruser, user+1));
196                         break;
197                 case '-':               /* if we matched a hostname, */
198                         if (hostok) {   /* check for user field rejections */
199                                 if (!*(user+1))
200                                         return(-1);
201                                 if (*(user+1) == '@') {
202                                         if (innetgr(user+2, NULL,
203                                                     (char *)ruser, ypdomain))
204                                                 return(-1);
205                                 } else {
206                                         if (!strcmp(ruser, user+1))
207                                                 return(-1);
208                                 }
209                         }
210                         break;
211                 default:        /* no rejections: try to match the user */
212                         if (hostok)
213                                 userok = !(strcmp(ruser,*user ? user : luser));
214                         break;
215                 }
216                 if (hostok && userok)
217                         return(0);
218         }
219         return (-1);
220 }
221
222 /*
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.
228  *
229  * Returns 0 if ok, -1 if not ok.
230  */
231 int
232 iruserok(unsigned raddr, int superuser, const char *ruser, const char *luser)
233 {
234         char *cp;
235         struct stat sbuf;
236         struct passwd *pwd;
237         FILE *hostf;
238         uid_t uid;
239         int first;
240         char pbuf[MaxPathLen];
241
242         first = 1;
243         hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
244 again:
245         if (hostf) {
246                 if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
247                         fclose(hostf);
248                         return (0);
249                 }
250                 fclose(hostf);
251         }
252         if (first == 1 && (__check_rhosts_file || superuser)) {
253                 first = 0;
254                 if ((pwd = k_getpwnam((char*)luser)) == NULL)
255                         return (-1);
256                 snprintf (pbuf, sizeof(pbuf), "%s/.rhosts", pwd->pw_dir);
257
258                 /*
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.
262                  */
263                 uid = geteuid();
264                 seteuid(pwd->pw_uid);
265                 hostf = fopen(pbuf, "r");
266                 seteuid(uid);
267
268                 if (hostf == NULL)
269                         return (-1);
270                 /*
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.
273                  */
274                 cp = NULL;
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. */
286                 if (cp) {
287                         __rcmd_errstr = cp;
288                         fclose(hostf);
289                         return (-1);
290                 }
291                 goto again;
292         }
293         return (-1);
294 }