3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 static const char rcsid[] =
36 "$FreeBSD: src/usr.sbin/ypserv/yp_access.c,v 1.17.2.1 2002/02/15 00:47:00 des Exp $";
41 #include <rpcsvc/yp.h>
42 #include <rpcsvc/yppasswd.h>
43 #include <rpcsvc/ypxfrd.h>
44 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
51 #include <sys/fcntl.h>
54 #include <sys/param.h>
55 #include "yp_extern.h"
63 char *yp_procs[] = { "ypoldproc_null",
65 "ypoldproc_domain_nonack",
72 "badproc1", /* placeholder */
73 "badproc2", /* placeholder */
74 "badproc3", /* placeholder */
79 "ypproc_domain_nonack",
93 void load_securenets()
100 struct securenet *next;
103 struct securenet *securenets;
105 #define LINEBUFSZ 1024
108 * Read /var/yp/securenets file and initialize the securenets
109 * list. If the file doesn't exist, we set up a dummy entry that
110 * allows all hosts to connect.
112 void load_securenets()
115 char path[MAXPATHLEN + 2];
116 char linebuf[1024 + 2];
117 struct securenet *tmp;
120 * If securenets is not NULL, we are being called to reload
121 * the list; free the existing list before re-reading the
125 tmp = securenets->next;
130 snprintf(path, MAXPATHLEN, "%s/securenets", yp_dir);
132 if ((fp = fopen(path, "r")) == NULL) {
133 if (errno == ENOENT) {
134 securenets = (struct securenet *)malloc(sizeof(struct securenet));
135 securenets->net.s_addr = INADDR_ANY;
136 securenets->mask.s_addr = INADDR_ANY;
137 securenets->next = NULL;
140 yp_error("fopen(%s) failed: %s", path, strerror(errno));
147 while (fgets(linebuf, LINEBUFSZ, fp)) {
148 char addr1[20], addr2[20];
150 if ((linebuf[0] == '#')
151 || (strspn(linebuf, " \t\r\n") == strlen(linebuf)))
153 if (sscanf(linebuf, "%s %s", addr1, addr2) < 2) {
154 yp_error("badly formatted securenets entry: %s",
159 tmp = (struct securenet *)malloc(sizeof(struct securenet));
161 if (!inet_aton((char *)&addr1, (struct in_addr *)&tmp->net)) {
162 yp_error("badly formatted securenets entry: %s", addr1);
167 if (!inet_aton((char *)&addr2, (struct in_addr *)&tmp->mask)) {
168 yp_error("badly formatted securenets entry: %s", addr2);
173 tmp->next = securenets;
183 * Access control functions.
185 * yp_access() checks the mapname and client host address and watches for
186 * the following things:
188 * - If the client is referencing one of the master.passwd.* maps, it must
189 * be using a privileged port to make its RPC to us. If it is, then we can
190 * assume that the caller is root and allow the RPC to succeed. If it
191 * isn't access is denied.
193 * - The client's IP address is checked against the securenets rules.
194 * There are two kinds of securenets support: the built-in support,
195 * which is very simple and depends on the presence of a
196 * /var/yp/securenets file, and tcp-wrapper support, which requires
197 * Wietse Venema's libwrap.a and tcpd.h. (Since the tcp-wrapper
198 * package does not ship with FreeBSD, we use the built-in support
199 * by default. Users can recompile the server with the tcp-wrapper library
200 * if they already have it installed and want to use hosts.allow and
201 * hosts.deny to control access instead of having a seperate securenets
204 * If no /var/yp/securenets file is present, the host access checks
205 * are bypassed and all hosts are allowed to connect.
207 * The yp_validdomain() function checks the domain specified by the caller
208 * to make sure it's actually served by this server. This is more a sanity
209 * check than an a security check, but this seems to be the best place for
214 int yp_access(map, domain, rqstp)
216 int yp_access(map, rqstp)
222 const struct svc_req *rqstp;
224 struct sockaddr_in *rqhost;
226 static unsigned long oldaddr = 0;
228 struct securenet *tmp;
230 char *yp_procedure = NULL;
233 if (rqstp->rq_prog != YPPASSWDPROG && rqstp->rq_prog != YPPROG) {
234 snprintf(procbuf, sizeof(procbuf), "#%lu/#%lu", rqstp->rq_prog,
236 yp_procedure = (char *)&procbuf;
238 yp_procedure = rqstp->rq_prog == YPPASSWDPROG ?
239 "yppasswdprog_update" :
240 yp_procs[rqstp->rq_proc + (12 * (rqstp->rq_vers - 1))];
243 rqhost = svc_getcaller(rqstp->rq_xprt);
246 yp_error("procedure %s called from %s:%d", yp_procedure,
247 inet_ntoa(rqhost->sin_addr),
248 ntohs(rqhost->sin_port));
250 yp_error("client is referencing map \"%s\".", map);
253 /* Check the map name if one was supplied. */
255 if (strchr(map, '/')) {
256 yp_error("embedded slash in map name \"%s\" -- \
257 possible spoof attempt from %s:%d",
258 map, inet_ntoa(rqhost->sin_addr),
259 ntohs(rqhost->sin_port));
263 if ((yp_testflag((char *)map, (char *)domain, YP_SECURE) ||
265 if ((strstr(map, "master.passwd.") ||
267 (rqstp->rq_prog == YPPROG &&
268 rqstp->rq_proc == YPPROC_XFR) ||
269 (rqstp->rq_prog == YPXFRD_FREEBSD_PROG &&
270 rqstp->rq_proc == YPXFRD_GETMAP)) &&
271 ntohs(rqhost->sin_port) >= IPPORT_RESERVED) {
272 yp_error("access to %s denied -- client %s:%d \
273 not privileged", map, inet_ntoa(rqhost->sin_addr), ntohs(rqhost->sin_port));
279 status = hosts_ctl("ypserv", STRING_UNKNOWN,
280 inet_ntoa(rqhost->sin_addr), "");
284 if (((rqhost->sin_addr.s_addr & ~tmp->mask.s_addr)
285 | tmp->net.s_addr) == rqhost->sin_addr.s_addr) {
294 if (rqhost->sin_addr.s_addr != oldaddr) {
295 yp_error("connect from %s:%d to procedure %s refused",
296 inet_ntoa(rqhost->sin_addr),
297 ntohs(rqhost->sin_port),
299 oldaddr = rqhost->sin_addr.s_addr;
307 int yp_validdomain(domain)
311 char dompath[MAXPATHLEN + 2];
313 if (domain == NULL || strstr(domain, "binding") ||
314 !strcmp(domain, ".") || !strcmp(domain, "..") ||
315 strchr(domain, '/') || strlen(domain) > YPMAXDOMAIN)
318 snprintf(dompath, sizeof(dompath), "%s/%s", yp_dir, domain);
320 if (stat(dompath, &statbuf) < 0 || !S_ISDIR(statbuf.st_mode))