The grouplist variable made local in the last commit was not being NULLed
[dragonfly.git] / usr.sbin / ypserv / yp_access.c
1 /*
2  * Copyright (c) 1995
3  *      Bill Paul <wpaul@ctr.columbia.edu>.  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 Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD: src/usr.sbin/ypserv/yp_access.c,v 1.17.2.1 2002/02/15 00:47:00 des Exp $
33  * $DragonFly: src/usr.sbin/ypserv/yp_access.c,v 1.4 2004/03/31 23:20:22 cpressey Exp $
34  */
35
36 #include <stdlib.h>
37 #include <rpc/rpc.h>
38 #include <rpcsvc/yp.h>
39 #include <rpcsvc/yppasswd.h>
40 #include <rpcsvc/ypxfrd.h>
41 #include <sys/types.h>
42 #include <limits.h>
43 #include <db.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #include <sys/stat.h>
48 #include <sys/fcntl.h>
49 #include <paths.h>
50 #include <errno.h>
51 #include <sys/param.h>
52 #include "yp_extern.h"
53 #ifdef TCP_WRAPPER
54 #include "tcpd.h"
55 #endif
56
57 extern int debug;
58
59                         /* NIS v1 */
60 char *yp_procs[] = {    "ypoldproc_null",
61                         "ypoldproc_domain",
62                         "ypoldproc_domain_nonack",
63                         "ypoldproc_match",
64                         "ypoldproc_first",
65                         "ypoldproc_next",
66                         "ypoldproc_poll",
67                         "ypoldproc_push",
68                         "ypoldproc_get",
69                         "badproc1", /* placeholder */
70                         "badproc2", /* placeholder */
71                         "badproc3", /* placeholder */
72
73                         /* NIS v2 */
74                         "ypproc_null",
75                         "ypproc_domain",
76                         "ypproc_domain_nonack",
77                         "ypproc_match",
78                         "ypproc_first",
79                         "ypproc_next",
80                         "ypproc_xfr",
81                         "ypproc_clear",
82                         "ypproc_all",
83                         "ypproc_master",
84                         "ypproc_order",
85                         "ypproc_maplist"
86                    };
87
88
89 #ifdef TCP_WRAPPER
90 void
91 load_securenets(void)
92 {
93 }
94 #else
95 struct securenet {
96         struct in_addr net;
97         struct in_addr mask;
98         struct securenet *next;
99 };
100
101 struct securenet *securenets;
102
103 #define LINEBUFSZ 1024
104
105 /*
106  * Read /var/yp/securenets file and initialize the securenets
107  * list. If the file doesn't exist, we set up a dummy entry that
108  * allows all hosts to connect.
109  */
110 void
111 load_securenets(void)
112 {
113         FILE *fp;
114         char path[MAXPATHLEN + 2];
115         char linebuf[1024 + 2];
116         struct securenet *tmp;
117
118         /*
119          * If securenets is not NULL, we are being called to reload
120          * the list; free the existing list before re-reading the
121          * securenets file.
122          */
123         while (securenets) {
124                 tmp = securenets->next;
125                 free(securenets);
126                 securenets = tmp;
127         }
128
129         snprintf(path, MAXPATHLEN, "%s/securenets", yp_dir);
130
131         if ((fp = fopen(path, "r")) == NULL) {
132                 if (errno == ENOENT) {
133                         securenets = (struct securenet *)malloc(sizeof(struct securenet));
134                         securenets->net.s_addr = INADDR_ANY;
135                         securenets->mask.s_addr = INADDR_ANY;
136                         securenets->next = NULL;
137                         return;
138                 } else {
139                         yp_error("fopen(%s) failed: %s", path, strerror(errno));
140                         exit(1);
141                 }
142         }
143
144         securenets = NULL;
145
146         while (fgets(linebuf, LINEBUFSZ, fp)) {
147                 char addr1[20], addr2[20];
148
149                 if ((linebuf[0] == '#')
150                     || (strspn(linebuf, " \t\r\n") == strlen(linebuf)))
151                         continue;
152                 if (sscanf(linebuf, "%s %s", addr1, addr2) < 2) {
153                         yp_error("badly formatted securenets entry: %s",
154                                                         linebuf);
155                         continue;
156                 }
157
158                 tmp = (struct securenet *)malloc(sizeof(struct securenet));
159
160                 if (!inet_aton((char *)&addr1, (struct in_addr *)&tmp->net)) {
161                         yp_error("badly formatted securenets entry: %s", addr1);
162                         free(tmp);
163                         continue;
164                 }
165
166                 if (!inet_aton((char *)&addr2, (struct in_addr *)&tmp->mask)) {
167                         yp_error("badly formatted securenets entry: %s", addr2);
168                         free(tmp);
169                         continue;
170                 }
171
172                 tmp->next = securenets;
173                 securenets = tmp;
174         }
175
176         fclose(fp);
177
178 }
179 #endif
180
181 /*
182  * Access control functions.
183  *
184  * yp_access() checks the mapname and client host address and watches for
185  * the following things:
186  *
187  * - If the client is referencing one of the master.passwd.* maps, it must
188  *   be using a privileged port to make its RPC to us. If it is, then we can
189  *   assume that the caller is root and allow the RPC to succeed. If it
190  *   isn't access is denied.
191  *
192  * - The client's IP address is checked against the securenets rules.
193  *   There are two kinds of securenets support: the built-in support,
194  *   which is very simple and depends on the presence of a
195  *   /var/yp/securenets file, and tcp-wrapper support, which requires
196  *   Wietse Venema's libwrap.a and tcpd.h. (Since the tcp-wrapper
197  *   package does not ship with FreeBSD, we use the built-in support
198  *   by default. Users can recompile the server with the tcp-wrapper library
199  *   if they already have it installed and want to use hosts.allow and
200  *   hosts.deny to control access instead of having a separate securenets
201  *   file.)
202  *
203  *   If no /var/yp/securenets file is present, the host access checks
204  *   are bypassed and all hosts are allowed to connect.
205  *
206  * The yp_validdomain() function checks the domain specified by the caller
207  * to make sure it's actually served by this server. This is more a sanity
208  * check than an a security check, but this seems to be the best place for
209  * it.
210  */
211
212 #ifdef DB_CACHE
213 int
214 yp_access(const char *map, const char *domain, const struct svc_req *rqstp)
215 #else
216 int
217 yp_access(const char *map, const struct svc_req *rqstp)
218 #endif
219 {
220         struct sockaddr_in *rqhost;
221         int status = 0;
222         static unsigned long oldaddr = 0;
223 #ifndef TCP_WRAPPER
224         struct securenet *tmp;
225 #endif
226         char *yp_procedure = NULL;
227         char procbuf[50];
228
229         if (rqstp->rq_prog != YPPASSWDPROG && rqstp->rq_prog != YPPROG) {
230                 snprintf(procbuf, sizeof(procbuf), "#%lu/#%lu", rqstp->rq_prog,
231                                                                 rqstp->rq_proc);
232                 yp_procedure = (char *)&procbuf;
233         } else {
234                 yp_procedure = rqstp->rq_prog == YPPASSWDPROG ?
235                 "yppasswdprog_update" :
236                 yp_procs[rqstp->rq_proc + (12 * (rqstp->rq_vers - 1))];
237         }
238
239         rqhost = svc_getcaller(rqstp->rq_xprt);
240
241         if (debug) {
242                 yp_error("procedure %s called from %s:%d", yp_procedure,
243                         inet_ntoa(rqhost->sin_addr),
244                         ntohs(rqhost->sin_port));
245                 if (map != NULL)
246                         yp_error("client is referencing map \"%s\".", map);
247         }
248
249         /* Check the map name if one was supplied. */
250         if (map != NULL) {
251                 if (strchr(map, '/')) {
252                         yp_error("embedded slash in map name \"%s\" -- \
253 possible spoof attempt from %s:%d",
254                                 map, inet_ntoa(rqhost->sin_addr),
255                                 ntohs(rqhost->sin_port));
256                         return(1);
257                 }
258 #ifdef DB_CACHE
259                 if ((yp_testflag((char *)map, (char *)domain, YP_SECURE) ||
260 #else
261                 if ((strstr(map, "master.passwd.") ||
262 #endif
263                     (rqstp->rq_prog == YPPROG &&
264                      rqstp->rq_proc == YPPROC_XFR) ||
265                     (rqstp->rq_prog == YPXFRD_FREEBSD_PROG &&
266                      rqstp->rq_proc == YPXFRD_GETMAP)) &&
267                      ntohs(rqhost->sin_port) >= IPPORT_RESERVED) {
268                         yp_error("access to %s denied -- client %s:%d \
269 not privileged", map, inet_ntoa(rqhost->sin_addr), ntohs(rqhost->sin_port));
270                         return(1);
271                 }
272         }
273
274 #ifdef TCP_WRAPPER
275         status = hosts_ctl("ypserv", STRING_UNKNOWN,
276                            inet_ntoa(rqhost->sin_addr), "");
277 #else
278         tmp = securenets;
279         while (tmp) {
280                 if (((rqhost->sin_addr.s_addr & ~tmp->mask.s_addr)
281                     | tmp->net.s_addr) == rqhost->sin_addr.s_addr) {
282                         status = 1;
283                         break;
284                 }
285                 tmp = tmp->next;
286         }
287 #endif
288
289         if (!status) {
290                 if (rqhost->sin_addr.s_addr != oldaddr) {
291                         yp_error("connect from %s:%d to procedure %s refused",
292                                         inet_ntoa(rqhost->sin_addr),
293                                         ntohs(rqhost->sin_port),
294                                         yp_procedure);
295                         oldaddr = rqhost->sin_addr.s_addr;
296                 }
297                 return(1);
298         }
299         return(0);
300
301 }
302
303 int
304 yp_validdomain(const char *domain)
305 {
306         struct stat statbuf;
307         char dompath[MAXPATHLEN + 2];
308
309         if (domain == NULL || strstr(domain, "binding") ||
310             !strcmp(domain, ".") || !strcmp(domain, "..") ||
311             strchr(domain, '/') || strlen(domain) > YPMAXDOMAIN)
312                 return(1);
313
314         snprintf(dompath, sizeof(dompath), "%s/%s", yp_dir, domain);
315
316         if (stat(dompath, &statbuf) < 0 || !S_ISDIR(statbuf.st_mode))
317                 return(1);
318
319
320         return(0);
321 }