Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / faithd / prefix.c
1 /*      $KAME: prefix.c,v 1.9 2001/07/02 14:36:49 itojun Exp $  */
2 /*      $FreeBSD: src/usr.sbin/faithd/prefix.c,v 1.1.2.2 2002/04/28 05:40:29 suz Exp $  */
3 /*      $DragonFly: src/usr.sbin/faithd/prefix.c,v 1.2 2003/06/17 04:29:53 dillon Exp $ */
4
5 /*
6  * Copyright (C) 2000 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <stdio.h>
38 #include <netdb.h>
39 #include <string.h>
40 #include <stddef.h>
41 #include <stdlib.h>
42 #include <limits.h>
43
44 #ifndef offsetof
45 #define offsetof(type, member)  ((size_t)(u_long)(&((type *)0)->member))
46 #endif
47
48 #include "faithd.h"
49 #include "prefix.h"
50
51 static int prefix_set __P((const char *, struct prefix *, int));
52 static struct config *config_load1 __P((const char *));
53 #if 0
54 static void config_show1 __P((const struct config *));
55 static void config_show __P((void));
56 #endif
57
58 struct config *config_list = NULL;
59 #ifdef NI_WITHSCOPEID
60 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
61 #else
62 const int niflags = NI_NUMERICHOST;
63 #endif
64
65 static int
66 prefix_set(s, prefix, slash)
67         const char *s;
68         struct prefix *prefix;
69         int slash;
70 {
71         char *p, *q, *r;
72         struct addrinfo hints, *res = NULL;
73         int max;
74         char *a;
75
76         p = strdup(s);
77         q = strchr(p, '/');
78         if (q) {
79                 if (!slash)
80                         goto fail;
81                 *q++ = '\0';
82         }
83
84         memset(&hints, 0, sizeof(hints));
85         hints.ai_family = PF_UNSPEC;
86         hints.ai_socktype = SOCK_DGRAM; /*dummy*/
87         hints.ai_flags = AI_NUMERICHOST;
88         if (getaddrinfo(p, "0", &hints, &res))
89                 goto fail;
90         if (res->ai_next || res->ai_addrlen > sizeof(prefix->a))
91                 goto fail;
92         memcpy(&prefix->a, res->ai_addr, res->ai_addrlen);
93
94         switch (prefix->a.ss_family) {
95         case AF_INET:
96                 max = 32;
97                 a = (char *)&((struct sockaddr_in *)&prefix->a)->sin_addr;
98                 break;
99         case AF_INET6:
100                 max = 128;
101                 a = (char *)&((struct sockaddr_in6 *)&prefix->a)->sin6_addr;
102                 break;
103         default:
104                 a = NULL;
105                 max = -1;
106                 break;
107         }
108
109         if (q) {
110                 r = NULL;
111                 prefix->l = (int)strtoul(q, &r, 10);
112                 if (!*q || *r)
113                         goto fail;
114                 if (prefix->l < 0 || prefix->l > max)
115                         goto fail;
116         } else
117                 prefix->l = max;
118
119         if (p)
120                 free(p);
121         if (res)
122                 freeaddrinfo(res);
123         return 0;
124
125 fail:
126         if (p)
127                 free(p);
128         if (res)
129                 freeaddrinfo(res);
130         return -1;
131 }
132
133 const char *
134 prefix_string(prefix)
135         const struct prefix *prefix;
136 {
137         static char buf[NI_MAXHOST + 20];
138         char hbuf[NI_MAXHOST];
139
140         if (getnameinfo((const struct sockaddr *)&prefix->a, prefix->a.ss_len,
141             hbuf, sizeof(hbuf), NULL, 0, niflags))
142                 return NULL;
143         snprintf(buf, sizeof(buf), "%s/%d", hbuf, prefix->l);
144         return buf;
145 }
146
147 int
148 prefix_match(prefix, sa)
149         const struct prefix *prefix;
150         const struct sockaddr *sa;
151 {
152         struct sockaddr_storage a, b;
153         char *pa, *pb;
154         int off, l;
155
156         if (prefix->a.ss_family != sa->sa_family ||
157             prefix->a.ss_len != sa->sa_len)
158                 return 0;
159
160         if (prefix->a.ss_len > sizeof(a) || sa->sa_len > sizeof(b))
161                 return 0;
162
163         switch (prefix->a.ss_family) {
164         case AF_INET:
165                 off = offsetof(struct sockaddr_in, sin_addr);
166                 break;
167         case AF_INET6:
168                 off = offsetof(struct sockaddr_in6, sin6_addr);
169                 break;
170         default:
171                 if (memcmp(&prefix->a, sa, prefix->a.ss_len) != 0)
172                         return 0;
173                 else
174                         return 1;
175         }
176
177         memcpy(&a, &prefix->a, prefix->a.ss_len);
178         memcpy(&b, sa, sa->sa_len);
179         l = prefix->l / 8 + (prefix->l % 8 ? 1 : 0);
180
181         /* overrun check */
182         if (off + l > a.ss_len)
183                 return 0;
184
185         pa = ((char *)&a) + off;
186         pb = ((char *)&b) + off;
187         if (prefix->l % 8) {
188                 pa[prefix->l / 8] &= 0xff00 >> (prefix->l % 8);
189                 pb[prefix->l / 8] &= 0xff00 >> (prefix->l % 8);
190         }
191         if (memcmp(pa, pb, l) != 0)
192                 return 0;
193         else
194                 return 1;
195 }
196
197 /*
198  * prefix/prefixlen permit/deny prefix/prefixlen [srcaddr]
199  * 3ffe::/16 permit 10.0.0.0/8 10.1.1.1
200  */
201 static struct config *
202 config_load1(line)
203         const char *line;
204 {
205         struct config *conf;
206         char buf[BUFSIZ];
207         char *p;
208         char *token[4];
209         int i;
210
211         if (strlen(line) + 1 > sizeof(buf))
212                 return NULL;
213         strlcpy(buf, line, sizeof(buf));
214
215         p = strchr(buf, '\n');
216         if (!p)
217                 return NULL;
218         *p = '\0';
219         p = strchr(buf, '#');
220         if (p)
221                 *p = '\0';
222         if (strlen(buf) == 0)
223                 return NULL;
224
225         p = buf;
226         memset(token, 0, sizeof(token));
227         for (i = 0; i < sizeof(token) / sizeof(token[0]); i++) {
228                 token[i] = strtok(p, "\t ");
229                 p = NULL;
230                 if (token[i] == NULL)
231                         break;
232         }
233         /* extra tokens? */
234         if (strtok(p, "\t ") != NULL)
235                 return NULL;
236         /* insufficient tokens */
237         switch (i) {
238         case 3:
239         case 4:
240                 break;
241         default:
242                 return NULL;
243         }
244
245         conf = (struct config *)malloc(sizeof(*conf));
246         if (conf == NULL)
247                 return NULL;
248         memset(conf, 0, sizeof(*conf));
249
250         if (strcasecmp(token[1], "permit") == 0)
251                 conf->permit = 1;
252         else if (strcasecmp(token[1], "deny") == 0)
253                 conf->permit = 0;
254         else {
255                 /* invalid keyword is considered as "deny" */
256                 conf->permit = 0;
257         }
258
259         if (prefix_set(token[0], &conf->match, 1) < 0)
260                 goto fail;
261         if (prefix_set(token[2], &conf->dest, 1) < 0)
262                 goto fail;
263         if (token[3]) {
264                 if (prefix_set(token[3], &conf->src, 0) < 0)
265                         goto fail;
266         }
267
268         return conf;
269
270 fail:
271         free(conf);
272         return NULL;
273 }
274
275 int
276 config_load(configfile)
277         const char *configfile;
278 {
279         FILE *fp;
280         char buf[BUFSIZ];
281         struct config *conf, *p;
282         struct config sentinel;
283
284         config_list = NULL;
285
286         if (!configfile)
287                 configfile = _PATH_PREFIX_CONF;
288         fp = fopen(configfile, "r");
289         if (fp == NULL)
290                 return -1;
291
292         p = &sentinel;
293         while (fgets(buf, sizeof(buf), fp) != NULL) {
294                 conf = config_load1(buf);
295                 if (conf) {
296                         p->next = conf;
297                         p = p->next;
298                 }
299         }
300         config_list = sentinel.next;
301
302         fclose(fp);
303         return 0;
304 }
305
306 #if 0
307 static void
308 config_show1(conf)
309         const struct config *conf;
310 {
311         const char *p;
312
313         p = prefix_string(&conf->match);
314         printf("%s", p ? p : "?");
315
316         if (conf->permit)
317                 printf(" permit");
318         else
319                 printf(" deny");
320
321         p = prefix_string(&conf->dest);
322         printf(" %s", p ? p : "?");
323
324         printf("\n");
325 }
326
327 static void
328 config_show()
329 {
330         struct config *conf;
331
332         for (conf = config_list; conf; conf = conf->next)
333                 config_show1(conf);
334 }
335 #endif
336
337 const struct config *
338 config_match(sa1, sa2)
339         struct sockaddr *sa1, *sa2;
340 {
341         static struct config conf;
342         const struct config *p;
343
344         if (sa1->sa_len > sizeof(conf.match.a) ||
345             sa2->sa_len > sizeof(conf.dest.a))
346                 return NULL;
347
348         memset(&conf, 0, sizeof(conf));
349         if (!config_list) {
350                 conf.permit = 1;
351                 memcpy(&conf.match.a, sa1, sa1->sa_len);
352                 memcpy(&conf.dest.a, sa2, sa2->sa_len);
353                 return &conf;
354         }
355
356         for (p = config_list; p; p = p->next)
357                 if (prefix_match(&p->match, sa1) && prefix_match(&p->dest, sa2))
358                         return p;
359
360         return NULL;
361 }