This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpasslist.c
207 lines (175 loc) · 5.31 KB
/
passlist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* Copyright (c) 2014-2021 Peter J. Philipp <pjp@delphinusdns.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* this file is based on filter.c
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __linux__
#include <grp.h>
#define __USE_BSD 1
#include <endian.h>
#include <bsd/stdlib.h>
#include <bsd/string.h>
#include <bsd/sys/queue.h>
#define __unused
#include <bsd/sys/tree.h>
#include <bsd/sys/endian.h>
#else /* not linux */
#include <sys/queue.h>
#include <sys/tree.h>
#endif /* __linux__ */
#include "ddd-dns.h"
#include "ddd-db.h"
int find_passlist(struct sockaddr_storage *, int);
void init_passlist(void);
int insert_passlist(char *, char *);
extern void dolog(int, char *, ...);
extern in_addr_t getmask(int);
extern int getmask6(int, struct sockaddr_in6 *);
extern int debug, verbose;
int passlist = 0; /* passlist is off by default */
SLIST_HEAD(listhead, passlistentry) passlisthead;
static struct passlistentry {
char name[INET6_ADDRSTRLEN];
int family;
struct sockaddr_storage hostmask;
struct sockaddr_storage netmask;
uint8_t prefixlen;
SLIST_ENTRY(passlistentry) passlist_entry;
} *wln2, *wlnp;
/*
* INIT_PASSLIST - initialize the passlist singly linked list
*/
void
init_passlist(void)
{
SLIST_INIT(&passlisthead);
return;
}
/*
* INSERT_PASSLIST - insert an address and prefixlen into the passlist slist
*/
int
insert_passlist(char *address, char *prefixlen)
{
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
int pnum;
int ret;
pnum = atoi(prefixlen);
wln2 = malloc(sizeof(struct passlistentry)); /* Insert after. */
if (strchr(address, ':') != NULL) {
wln2->family = AF_INET6;
sin6 = (struct sockaddr_in6 *)&wln2->hostmask;
if ((ret = inet_pton(AF_INET6, address, &sin6->sin6_addr.s6_addr)) != 1)
return (-1);
sin6->sin6_family = AF_INET6;
sin6 = (struct sockaddr_in6 *)&wln2->netmask;
sin6->sin6_family = AF_INET6;
if (getmask6(pnum, sin6) < 0)
return(-1);
wln2->prefixlen = pnum;
} else {
wln2->family = AF_INET;
sin = (struct sockaddr_in *)&wln2->hostmask;
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = inet_addr(address);
sin = (struct sockaddr_in *)&wln2->netmask;
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = getmask(pnum);
wln2->prefixlen = pnum;
}
SLIST_INSERT_HEAD(&passlisthead, wln2, passlist_entry);
return (0);
}
/*
* FIND_PASSLIST - walk the passlist list and find the correponding network
* if a network matches return 1, if no match is found return
* 0.
*/
int
find_passlist(struct sockaddr_storage *sst, int family)
{
struct sockaddr_in *sin, *sin0;
struct sockaddr_in6 *sin6, *sin60, *sin61;
uint32_t hostmask, netmask;
uint32_t a;
#ifdef __amd64
uint64_t *hm[2], *nm[2], *a6[2];
#else
uint32_t *hm[4], *nm[4], *a6[4];
#endif
SLIST_FOREACH(wlnp, &passlisthead, passlist_entry) {
if (wlnp->family == AF_INET) {
if (family != AF_INET)
continue;
sin = (struct sockaddr_in *)sst;
a = sin->sin_addr.s_addr;
sin = (struct sockaddr_in *)&wlnp->hostmask;
sin0 = (struct sockaddr_in *)&wlnp->netmask;
hostmask = sin->sin_addr.s_addr;
netmask = sin0->sin_addr.s_addr;
if ((hostmask & netmask) == (a & netmask)) {
return (1);
} /* if hostmask */
} else if (wlnp->family == AF_INET6) {
if (family != AF_INET6)
continue;
sin6 = (struct sockaddr_in6 *)sst;
sin60 = (struct sockaddr_in6 *)&wlnp->hostmask;
sin61 = (struct sockaddr_in6 *)&wlnp->netmask;
#ifdef __amd64
/*
* If this is on a 64 bit machine, we'll benefit
* by using 64 bit registers, this should make it
* a tad faster...
*/
hm[0] = (uint64_t *)&sin60->sin6_addr.s6_addr;
hm[1] = (hm[0] + 1);
nm[0] = (uint64_t *)&sin61->sin6_addr.s6_addr;
nm[1] = (nm[0] + 1);
a6[0] = (uint64_t *)&sin6->sin6_addr.s6_addr;
a6[1] = (a6[0] + 1);
if ( ((*hm[0] & *nm[0]) == (*a6[0] & *nm[0]))&&
((*hm[1] & *nm[1]) == (*a6[1] & *nm[1]))) {
#else
hm[0] = (uint32_t *)&sin60->sin6_addr.s6_addr;
hm[1] = (hm[0] + 1); hm[2] = (hm[1] + 1);
hm[3] = (hm[2] + 1);
nm[0] = (uint32_t *)&sin61->sin6_addr.s6_addr;
nm[1] = (nm[0] + 1); nm[2] = (nm[1] + 1);
nm[3] = (nm[2] + 1);
a6[0] = (uint32_t *)&sin6->sin6_addr.s6_addr;
a6[1] = (a6[0] + 1); a6[2] = (a6[1] + 1);
a6[3] = (a6[2] + 1);
if ( ((*hm[0] & *nm[0]) == (*a6[0] & *nm[0]))&&
((*hm[1] & *nm[1]) == (*a6[1] & *nm[1]))&&
((*hm[2] & *nm[2]) == (*a6[2] & *nm[2]))&&
((*hm[3] & *nm[3]) == (*a6[3] & *nm[3]))) {
#endif
return (1);
} /* if ip6 address */
} /* if AF_INET6 */
} /* SLIST */
return (0);
}