]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ypserv/yp_access.c
Followup to r347996
[FreeBSD/FreeBSD.git] / usr.sbin / ypserv / yp_access.c
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1995
5  *      Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Bill Paul.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
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 static const char *yp_procs[] = {
63         /* NIS v1 */
64         "ypoldproc_null",
65         "ypoldproc_domain",
66         "ypoldproc_domain_nonack",
67         "ypoldproc_match",
68         "ypoldproc_first",
69         "ypoldproc_next",
70         "ypoldproc_poll",
71         "ypoldproc_push",
72         "ypoldproc_get",
73         "badproc1", /* placeholder */
74         "badproc2", /* placeholder */
75         "badproc3", /* placeholder */
76
77         /* NIS v2 */
78         "ypproc_null",
79         "ypproc_domain",
80         "ypproc_domain_nonack",
81         "ypproc_match",
82         "ypproc_first",
83         "ypproc_next",
84         "ypproc_xfr",
85         "ypproc_clear",
86         "ypproc_all",
87         "ypproc_master",
88         "ypproc_order",
89         "ypproc_maplist"
90 };
91
92 struct securenet {
93         struct in_addr net;
94         struct in_addr mask;
95         struct securenet *next;
96 };
97
98 static struct securenet *securenets;
99
100 #define LINEBUFSZ 1024
101
102 #ifdef TCP_WRAPPER
103 int hosts_ctl(char *, char *, char *, char *);
104 #endif
105
106 /*
107  * Read /var/yp/securenets file and initialize the securenets
108  * list. If the file doesn't exist, we set up a dummy entry that
109  * allows all hosts to connect.
110  */
111 void
112 load_securenets(void)
113 {
114         FILE *fp;
115         char path[MAXPATHLEN + 2];
116         char linebuf[1024 + 2];
117         struct securenet *tmp;
118
119         /*
120          * If securenets is not NULL, we are being called to reload
121          * the list; free the existing list before re-reading the
122          * securenets file.
123          */
124         while (securenets) {
125                 tmp = securenets->next;
126                 free(securenets);
127                 securenets = tmp;
128         }
129
130         snprintf(path, MAXPATHLEN, "%s/securenets", yp_dir);
131
132         if ((fp = fopen(path, "r")) == NULL) {
133                 if (errno == ENOENT) {
134                         securenets = malloc(sizeof(struct securenet));
135                         securenets->net.s_addr = INADDR_ANY;
136                         securenets->mask.s_addr = INADDR_ANY;
137                         securenets->next = NULL;
138                         return;
139                 } else {
140                         yp_error("fopen(%s) failed: %s", path, strerror(errno));
141                         exit(1);
142                 }
143         }
144
145         securenets = NULL;
146
147         while (fgets(linebuf, LINEBUFSZ, fp)) {
148                 char addr1[20], addr2[20];
149
150                 if ((linebuf[0] == '#')
151                     || (strspn(linebuf, " \t\r\n") == strlen(linebuf)))
152                         continue;
153                 if (sscanf(linebuf, "%s %s", addr1, addr2) < 2) {
154                         yp_error("badly formatted securenets entry: %s",
155                                                         linebuf);
156                         continue;
157                 }
158
159                 tmp = malloc(sizeof(struct securenet));
160
161                 if (!inet_aton((char *)&addr1, (struct in_addr *)&tmp->net)) {
162                         yp_error("badly formatted securenets entry: %s", addr1);
163                         free(tmp);
164                         continue;
165                 }
166
167                 if (!inet_aton((char *)&addr2, (struct in_addr *)&tmp->mask)) {
168                         yp_error("badly formatted securenets entry: %s", addr2);
169                         free(tmp);
170                         continue;
171                 }
172
173                 tmp->next = securenets;
174                 securenets = tmp;
175         }
176
177         fclose(fp);
178
179 }
180
181 /*
182  * Access control functions.
183  *
184  * yp_access() checks the mapname and client host address and watches for
185  * the following things:
186  *
187  * - If the client is referencing one of the master.passwd.* or shadow.* maps,
188  *   it must be using a privileged port to make its RPC to us. If it is, then
189  *   we can assume that the caller is root and allow the RPC to succeed. If it
190  *   isn't access is denied.
191  *
192  * - The client's IP address is checked against the securenets rules.
193  *   There are two kinds of securenets support: the built-in support,
194  *   which is very simple and depends on the presence of a
195  *   /var/yp/securenets file, and tcp-wrapper support, which requires
196  *   Wietse Venema's libwrap.a and tcpd.h. (Since the tcp-wrapper
197  *   package does not ship with FreeBSD, we use the built-in support
198  *   by default. Users can recompile the server with the tcp-wrapper library
199  *   if they already have it installed and want to use hosts.allow and
200  *   hosts.deny to control access instead of having a separate securenets
201  *   file.)
202  *
203  *   If no /var/yp/securenets file is present, the host access checks
204  *   are bypassed and all hosts are allowed to connect.
205  *
206  * The yp_validdomain() function checks the domain specified by the caller
207  * to make sure it's actually served by this server. This is more a sanity
208  * check than an a security check, but this seems to be the best place for
209  * it.
210  */
211
212 #ifdef DB_CACHE
213 int
214 yp_access(const char *map, const char *domain, const struct svc_req *rqstp)
215 #else
216 int
217 yp_access(const char *map, const struct svc_req *rqstp)
218 #endif
219 {
220         struct sockaddr_in *rqhost;
221         int status_securenets = 0;
222 #ifdef TCP_WRAPPER
223         int status_tcpwrap;
224 #endif
225         static unsigned long oldaddr = 0;
226         struct securenet *tmp;
227         const char *yp_procedure = NULL;
228         char procbuf[50];
229
230         if (rqstp->rq_prog != YPPASSWDPROG && rqstp->rq_prog != YPPROG) {
231                 snprintf(procbuf, sizeof(procbuf), "#%lu/#%lu",
232                     (unsigned long)rqstp->rq_prog,
233                     (unsigned long)rqstp->rq_proc);
234                 yp_procedure = (char *)&procbuf;
235         } else {
236                 yp_procedure = rqstp->rq_prog == YPPASSWDPROG ?
237                 "yppasswdprog_update" :
238                 yp_procs[rqstp->rq_proc + (12 * (rqstp->rq_vers - 1))];
239         }
240
241         rqhost = svc_getcaller(rqstp->rq_xprt);
242
243         if (debug) {
244                 yp_error("procedure %s called from %s:%d", yp_procedure,
245                         inet_ntoa(rqhost->sin_addr),
246                         ntohs(rqhost->sin_port));
247                 if (map != NULL)
248                         yp_error("client is referencing map \"%s\".", map);
249         }
250
251         /* Check the map name if one was supplied. */
252         if (map != NULL) {
253                 if (strchr(map, '/')) {
254                         yp_error("embedded slash in map name \"%s\" -- \
255 possible spoof attempt from %s:%d",
256                                 map, inet_ntoa(rqhost->sin_addr),
257                                 ntohs(rqhost->sin_port));
258                         return(1);
259                 }
260 #ifdef DB_CACHE
261                 if ((yp_testflag((char *)map, (char *)domain, YP_SECURE) ||
262 #else
263                 if ((strstr(map, "master.passwd.") || strstr(map, "shadow.") ||
264 #endif
265                     (rqstp->rq_prog == YPPROG &&
266                      rqstp->rq_proc == YPPROC_XFR) ||
267                     (rqstp->rq_prog == YPXFRD_FREEBSD_PROG &&
268                      rqstp->rq_proc == YPXFRD_GETMAP)) &&
269                      ntohs(rqhost->sin_port) >= IPPORT_RESERVED) {
270                         yp_error("access to %s denied -- client %s:%d \
271 not privileged", map, inet_ntoa(rqhost->sin_addr), ntohs(rqhost->sin_port));
272                         return(1);
273                 }
274         }
275
276 #ifdef TCP_WRAPPER
277         status_tcpwrap = hosts_ctl("ypserv", STRING_UNKNOWN,
278                            inet_ntoa(rqhost->sin_addr), "");
279 #endif
280         tmp = securenets;
281         while (tmp) {
282                 if (((rqhost->sin_addr.s_addr & ~tmp->mask.s_addr)
283                     | tmp->net.s_addr) == rqhost->sin_addr.s_addr) {
284                         status_securenets = 1;
285                         break;
286                 }
287                 tmp = tmp->next;
288         }
289
290 #ifdef TCP_WRAPPER
291         if (status_securenets == 0 || status_tcpwrap == 0) {
292 #else
293         if (status_securenets == 0) {
294 #endif
295         /*
296          * One of the following two events occurred:
297          *
298          * (1) The /var/yp/securenets exists and the remote host does not
299          *     match any of the networks specified in it.
300          * (2) The hosts.allow file has denied access and TCP_WRAPPER is
301          *     defined.
302          *
303          * In either case deny access.
304          */
305                 if (rqhost->sin_addr.s_addr != oldaddr) {
306                         yp_error("connect from %s:%d to procedure %s refused",
307                                         inet_ntoa(rqhost->sin_addr),
308                                         ntohs(rqhost->sin_port),
309                                         yp_procedure);
310                         oldaddr = rqhost->sin_addr.s_addr;
311                 }
312                 return(1);
313         }
314         return(0);
315
316 }
317
318 int
319 yp_validdomain(const char *domain)
320 {
321         struct stat statbuf;
322         char dompath[MAXPATHLEN + 2];
323
324         if (domain == NULL || strstr(domain, "binding") ||
325             !strcmp(domain, ".") || !strcmp(domain, "..") ||
326             strchr(domain, '/') || strlen(domain) > YPMAXDOMAIN)
327                 return(1);
328
329         snprintf(dompath, sizeof(dompath), "%s/%s", yp_dir, domain);
330
331         if (stat(dompath, &statbuf) < 0 || !S_ISDIR(statbuf.st_mode))
332                 return(1);
333
334
335         return(0);
336 }