xref: /freebsd/usr.sbin/ypserv/yp_access.c (revision 1c05a6ea6b849ff95e539c31adea887c644a6a01)
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  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <stdlib.h>
38 #include <rpc/rpc.h>
39 #include <rpcsvc/yp.h>
40 #include <rpcsvc/yppasswd.h>
41 #include <rpcsvc/ypxfrd.h>
42 #include <sys/types.h>
43 #include <limits.h>
44 #include <db.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <sys/stat.h>
49 #include <sys/fcntl.h>
50 #include <paths.h>
51 #include <errno.h>
52 #include <sys/param.h>
53 #include "yp_extern.h"
54 #ifdef TCP_WRAPPER
55 #include "tcpd.h"
56 #endif
57 
58 extern int debug;
59 
60 static const char *yp_procs[] = {
61 	/* NIS v1 */
62 	"ypoldproc_null",
63 	"ypoldproc_domain",
64 	"ypoldproc_domain_nonack",
65 	"ypoldproc_match",
66 	"ypoldproc_first",
67 	"ypoldproc_next",
68 	"ypoldproc_poll",
69 	"ypoldproc_push",
70 	"ypoldproc_get",
71 	"badproc1", /* placeholder */
72 	"badproc2", /* placeholder */
73 	"badproc3", /* placeholder */
74 
75 	/* NIS v2 */
76 	"ypproc_null",
77 	"ypproc_domain",
78 	"ypproc_domain_nonack",
79 	"ypproc_match",
80 	"ypproc_first",
81 	"ypproc_next",
82 	"ypproc_xfr",
83 	"ypproc_clear",
84 	"ypproc_all",
85 	"ypproc_master",
86 	"ypproc_order",
87 	"ypproc_maplist"
88 };
89 
90 struct securenet {
91 	struct in_addr net;
92 	struct in_addr mask;
93 	struct securenet *next;
94 };
95 
96 static struct securenet *securenets;
97 
98 #define LINEBUFSZ 1024
99 
100 #ifdef TCP_WRAPPER
101 int hosts_ctl(char *, char *, char *, char *);
102 #endif
103 
104 /*
105  * Read /var/yp/securenets file and initialize the securenets
106  * list. If the file doesn't exist, we set up a dummy entry that
107  * allows all hosts to connect.
108  */
109 void
110 load_securenets(void)
111 {
112 	FILE *fp;
113 	char path[MAXPATHLEN + 2];
114 	char linebuf[1024 + 2];
115 	struct securenet *tmp;
116 
117 	/*
118 	 * If securenets is not NULL, we are being called to reload
119 	 * the list; free the existing list before re-reading the
120 	 * securenets file.
121 	 */
122 	while (securenets) {
123 		tmp = securenets->next;
124 		free(securenets);
125 		securenets = tmp;
126 	}
127 
128 	snprintf(path, MAXPATHLEN, "%s/securenets", yp_dir);
129 
130 	if ((fp = fopen(path, "r")) == NULL) {
131 		if (errno == ENOENT) {
132 			securenets = malloc(sizeof(struct securenet));
133 			securenets->net.s_addr = INADDR_ANY;
134 			securenets->mask.s_addr = INADDR_ANY;
135 			securenets->next = NULL;
136 			return;
137 		} else {
138 			yp_error("fopen(%s) failed: %s", path, strerror(errno));
139 			exit(1);
140 		}
141 	}
142 
143 	securenets = NULL;
144 
145 	while (fgets(linebuf, LINEBUFSZ, fp)) {
146 		char addr1[20], addr2[20];
147 
148 		if ((linebuf[0] == '#')
149 		    || (strspn(linebuf, " \t\r\n") == strlen(linebuf)))
150 			continue;
151 		if (sscanf(linebuf, "%s %s", addr1, addr2) < 2) {
152 			yp_error("badly formatted securenets entry: %s",
153 							linebuf);
154 			continue;
155 		}
156 
157 		tmp = malloc(sizeof(struct securenet));
158 
159 		if (!inet_aton((char *)&addr1, (struct in_addr *)&tmp->net)) {
160 			yp_error("badly formatted securenets entry: %s", addr1);
161 			free(tmp);
162 			continue;
163 		}
164 
165 		if (!inet_aton((char *)&addr2, (struct in_addr *)&tmp->mask)) {
166 			yp_error("badly formatted securenets entry: %s", addr2);
167 			free(tmp);
168 			continue;
169 		}
170 
171 		tmp->next = securenets;
172 		securenets = tmp;
173 	}
174 
175 	fclose(fp);
176 
177 }
178 
179 /*
180  * Access control functions.
181  *
182  * yp_access() checks the mapname and client host address and watches for
183  * the following things:
184  *
185  * - If the client is referencing one of the master.passwd.* or shadow.* maps,
186  *   it must be using a privileged port to make its RPC to us. If it is, then
187  *   we can assume that the caller is root and allow the RPC to succeed. If it
188  *   isn't access is denied.
189  *
190  * - The client's IP address is checked against the securenets rules.
191  *   There are two kinds of securenets support: the built-in support,
192  *   which is very simple and depends on the presence of a
193  *   /var/yp/securenets file, and tcp-wrapper support, which requires
194  *   Wietse Venema's libwrap.a and tcpd.h. (Since the tcp-wrapper
195  *   package does not ship with FreeBSD, we use the built-in support
196  *   by default. Users can recompile the server with the tcp-wrapper library
197  *   if they already have it installed and want to use hosts.allow and
198  *   hosts.deny to control access instead of having a separate securenets
199  *   file.)
200  *
201  *   If no /var/yp/securenets file is present, the host access checks
202  *   are bypassed and all hosts are allowed to connect.
203  *
204  * The yp_validdomain() function checks the domain specified by the caller
205  * to make sure it's actually served by this server. This is more a sanity
206  * check than an a security check, but this seems to be the best place for
207  * it.
208  */
209 
210 #ifdef DB_CACHE
211 int
212 yp_access(const char *map, const char *domain, const struct svc_req *rqstp)
213 #else
214 int
215 yp_access(const char *map, const struct svc_req *rqstp)
216 #endif
217 {
218 	struct sockaddr_in *rqhost;
219 	int status_securenets = 0;
220 #ifdef TCP_WRAPPER
221 	int status_tcpwrap;
222 #endif
223 	static unsigned long oldaddr = 0;
224 	struct securenet *tmp;
225 	const char *yp_procedure = NULL;
226 	char procbuf[50];
227 
228 	if (rqstp->rq_prog != YPPASSWDPROG && rqstp->rq_prog != YPPROG) {
229 		snprintf(procbuf, sizeof(procbuf), "#%lu/#%lu",
230 		    (unsigned long)rqstp->rq_prog,
231 		    (unsigned long)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.") || strstr(map, "shadow.") ||
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_tcpwrap = hosts_ctl("ypserv", STRING_UNKNOWN,
276 			   inet_ntoa(rqhost->sin_addr), "");
277 #endif
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_securenets = 1;
283 			break;
284 		}
285 		tmp = tmp->next;
286 	}
287 
288 #ifdef TCP_WRAPPER
289 	if (status_securenets == 0 || status_tcpwrap == 0) {
290 #else
291 	if (status_securenets == 0) {
292 #endif
293 	/*
294 	 * One of the following two events occurred:
295 	 *
296 	 * (1) The /var/yp/securenets exists and the remote host does not
297 	 *     match any of the networks specified in it.
298 	 * (2) The hosts.allow file has denied access and TCP_WRAPPER is
299 	 *     defined.
300 	 *
301 	 * In either case deny access.
302 	 */
303 		if (rqhost->sin_addr.s_addr != oldaddr) {
304 			yp_error("connect from %s:%d to procedure %s refused",
305 					inet_ntoa(rqhost->sin_addr),
306 					ntohs(rqhost->sin_port),
307 					yp_procedure);
308 			oldaddr = rqhost->sin_addr.s_addr;
309 		}
310 		return(1);
311 	}
312 	return(0);
313 
314 }
315 
316 int
317 yp_validdomain(const char *domain)
318 {
319 	struct stat statbuf;
320 	char dompath[MAXPATHLEN + 2];
321 
322 	if (domain == NULL || strstr(domain, "binding") ||
323 	    !strcmp(domain, ".") || !strcmp(domain, "..") ||
324 	    strchr(domain, '/') || strlen(domain) > YPMAXDOMAIN)
325 		return(1);
326 
327 	snprintf(dompath, sizeof(dompath), "%s/%s", yp_dir, domain);
328 
329 	if (stat(dompath, &statbuf) < 0 || !S_ISDIR(statbuf.st_mode))
330 		return(1);
331 
332 
333 	return(0);
334 }
335