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