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