Merge branch 'vendor/DHCPCD'
[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.23 2006/05/31 22:31:08 cperciva Exp $
33  */
34
35 #include <stdlib.h>
36 #include <rpc/rpc.h>
37 #include <rpcsvc/yp.h>
38 #include <rpcsvc/yppasswd.h>
39 #include <rpcsvc/ypxfrd.h>
40 #include <sys/types.h>
41 #include <limits.h>
42 #include <db.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
46 #include <sys/stat.h>
47 #include <sys/fcntl.h>
48 #include <paths.h>
49 #include <errno.h>
50 #include <sys/param.h>
51 #include "yp_extern.h"
52 #ifdef TCP_WRAPPER
53 #include "tcpd.h"
54 #endif
55
56 extern int debug;
57
58                         /* NIS v1 */
59 static const char *yp_procs[] = {
60         "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 struct securenet {
89         struct in_addr net;
90         struct in_addr mask;
91         struct securenet *next;
92 };
93
94 static struct securenet *securenets;
95
96 #define LINEBUFSZ 1024
97
98 /*
99  * Read /var/yp/securenets file and initialize the securenets
100  * list. If the file doesn't exist, we set up a dummy entry that
101  * allows all hosts to connect.
102  */
103 void
104 load_securenets(void)
105 {
106         FILE *fp;
107         char path[MAXPATHLEN + 2];
108         char linebuf[1024 + 2];
109         struct securenet *tmp;
110
111         /*
112          * If securenets is not NULL, we are being called to reload
113          * the list; free the existing list before re-reading the
114          * securenets file.
115          */
116         while (securenets) {
117                 tmp = securenets->next;
118                 free(securenets);
119                 securenets = tmp;
120         }
121
122         snprintf(path, MAXPATHLEN, "%s/securenets", yp_dir);
123
124         if ((fp = fopen(path, "r")) == NULL) {
125                 if (errno == ENOENT) {
126                         securenets = (struct securenet *)malloc(sizeof(struct securenet));
127                         securenets->net.s_addr = INADDR_ANY;
128                         securenets->mask.s_addr = INADDR_ANY;
129                         securenets->next = NULL;
130                         return;
131                 } else {
132                         yp_error("fopen(%s) failed: %s", path, strerror(errno));
133                         exit(1);
134                 }
135         }
136
137         securenets = NULL;
138
139         while (fgets(linebuf, LINEBUFSZ, fp)) {
140                 char addr1[20], addr2[20];
141
142                 if ((linebuf[0] == '#')
143                     || (strspn(linebuf, " \t\r\n") == strlen(linebuf)))
144                         continue;
145                 if (sscanf(linebuf, "%s %s", addr1, addr2) < 2) {
146                         yp_error("badly formatted securenets entry: %s",
147                                                         linebuf);
148                         continue;
149                 }
150
151                 tmp = (struct securenet *)malloc(sizeof(struct securenet));
152
153                 if (!inet_aton((char *)&addr1, (struct in_addr *)&tmp->net)) {
154                         yp_error("badly formatted securenets entry: %s", addr1);
155                         free(tmp);
156                         continue;
157                 }
158
159                 if (!inet_aton((char *)&addr2, (struct in_addr *)&tmp->mask)) {
160                         yp_error("badly formatted securenets entry: %s", addr2);
161                         free(tmp);
162                         continue;
163                 }
164
165                 tmp->next = securenets;
166                 securenets = tmp;
167         }
168
169         fclose(fp);
170
171 }
172
173 /*
174  * Access control functions.
175  *
176  * yp_access() checks the mapname and client host address and watches for
177  * the following things:
178  *
179  * - If the client is referencing one of the master.passwd.* maps, it must
180  *   be using a privileged port to make its RPC to us. If it is, then we can
181  *   assume that the caller is root and allow the RPC to succeed. If it
182  *   isn't access is denied.
183  *
184  * - The client's IP address is checked against the securenets rules.
185  *   There are two kinds of securenets support: the built-in support,
186  *   which is very simple and depends on the presence of a
187  *   /var/yp/securenets file, and tcp-wrapper support, which requires
188  *   Wietse Venema's libwrap.a and tcpd.h. (Since the tcp-wrapper
189  *   package does not ship with FreeBSD, we use the built-in support
190  *   by default. Users can recompile the server with the tcp-wrapper library
191  *   if they already have it installed and want to use hosts.allow and
192  *   hosts.deny to control access instead of having a separate securenets
193  *   file.)
194  *
195  *   If no /var/yp/securenets file is present, the host access checks
196  *   are bypassed and all hosts are allowed to connect.
197  *
198  * The yp_validdomain() function checks the domain specified by the caller
199  * to make sure it's actually served by this server. This is more a sanity
200  * check than an a security check, but this seems to be the best place for
201  * it.
202  */
203
204 #ifdef DB_CACHE
205 int
206 yp_access(const char *map, const char *domain, const struct svc_req *rqstp)
207 #else
208 int
209 yp_access(const char *map, const struct svc_req *rqstp)
210 #endif
211 {
212         struct sockaddr_in *rqhost;
213         int status_securenets = 0;
214 #ifdef TCP_WRAPPER
215         int status_tcpwrap;
216 #endif
217         static unsigned long oldaddr = 0;
218         struct securenet *tmp;
219         const char *yp_procedure = NULL;
220         char procbuf[50];
221
222         if (rqstp->rq_prog != YPPASSWDPROG && rqstp->rq_prog != YPPROG) {
223                 snprintf(procbuf, sizeof(procbuf), "#%lu/#%lu",
224                     (unsigned long)rqstp->rq_prog,
225                     (unsigned long)rqstp->rq_proc);
226                 yp_procedure = (char *)&procbuf;
227         } else {
228                 yp_procedure = rqstp->rq_prog == YPPASSWDPROG ?
229                 "yppasswdprog_update" :
230                 yp_procs[rqstp->rq_proc + (12 * (rqstp->rq_vers - 1))];
231         }
232
233         rqhost = svc_getcaller(rqstp->rq_xprt);
234
235         if (debug) {
236                 yp_error("procedure %s called from %s:%d", yp_procedure,
237                         inet_ntoa(rqhost->sin_addr),
238                         ntohs(rqhost->sin_port));
239                 if (map != NULL)
240                         yp_error("client is referencing map \"%s\".", map);
241         }
242
243         /* Check the map name if one was supplied. */
244         if (map != NULL) {
245                 if (strchr(map, '/')) {
246                         yp_error("embedded slash in map name \"%s\" -- \
247 possible spoof attempt from %s:%d",
248                                 map, inet_ntoa(rqhost->sin_addr),
249                                 ntohs(rqhost->sin_port));
250                         return(1);
251                 }
252 #ifdef DB_CACHE
253                 if ((yp_testflag((char *)map, (char *)domain, YP_SECURE) ||
254 #else
255                 if ((strstr(map, "master.passwd.") ||
256 #endif
257                     (rqstp->rq_prog == YPPROG &&
258                      rqstp->rq_proc == YPPROC_XFR) ||
259                     (rqstp->rq_prog == YPXFRD_FREEBSD_PROG &&
260                      rqstp->rq_proc == YPXFRD_GETMAP)) &&
261                      ntohs(rqhost->sin_port) >= IPPORT_RESERVED) {
262                         yp_error("access to %s denied -- client %s:%d \
263 not privileged", map, inet_ntoa(rqhost->sin_addr), ntohs(rqhost->sin_port));
264                         return(1);
265                 }
266         }
267
268 #ifdef TCP_WRAPPER
269         status_tcpwrap = hosts_ctl("ypserv", STRING_UNKNOWN,
270                            inet_ntoa(rqhost->sin_addr), "");
271 #endif
272         tmp = securenets;
273         while (tmp) {
274                 if (((rqhost->sin_addr.s_addr & ~tmp->mask.s_addr)
275                     | tmp->net.s_addr) == rqhost->sin_addr.s_addr) {
276                         status_securenets = 1;
277                         break;
278                 }
279                 tmp = tmp->next;
280         }
281
282 #ifdef TCP_WRAPPER
283         if (status_securenets == 0 || status_tcpwrap == 0) {
284 #else
285         if (status_securenets == 0) {
286 #endif
287         /*
288          * One of the following two events occurred:
289          *
290          * (1) The /var/yp/securenets exists and the remote host does not
291          *     match any of the networks specified in it.
292          * (2) The hosts.allow file has denied access and TCP_WRAPPER is
293          *     defined.
294          *
295          * In either case deny access.
296          */
297                 if (rqhost->sin_addr.s_addr != oldaddr) {
298                         yp_error("connect from %s:%d to procedure %s refused",
299                                         inet_ntoa(rqhost->sin_addr),
300                                         ntohs(rqhost->sin_port),
301                                         yp_procedure);
302                         oldaddr = rqhost->sin_addr.s_addr;
303                 }
304                 return(1);
305         }
306         return(0);
307
308 }
309
310 int
311 yp_validdomain(const char *domain)
312 {
313         struct stat statbuf;
314         char dompath[MAXPATHLEN + 2];
315
316         if (domain == NULL || strstr(domain, "binding") ||
317             !strcmp(domain, ".") || !strcmp(domain, "..") ||
318             strchr(domain, '/') || strlen(domain) > YPMAXDOMAIN)
319                 return(1);
320
321         snprintf(dompath, sizeof(dompath), "%s/%s", yp_dir, domain);
322
323         if (stat(dompath, &statbuf) < 0 || !S_ISDIR(statbuf.st_mode))
324                 return(1);
325
326
327         return(0);
328 }