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