-
Notifications
You must be signed in to change notification settings - Fork 32
/
Access.cs
322 lines (285 loc) · 7.21 KB
/
Access.cs
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**
* Copyright (C) 2006 Alex Pedenko
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
using System;
using System.Net;
using System.Net.Sockets;
namespace NetSync
{
public class Access
{
static int NS_INT16SZ = 2;
static int NS_INADDRSZ = 4;
static int NS_IN6ADDRSZ = 16;
public Access()
{
}
public bool AllowAccess(string addr, string host, string allowList, string denyList)
{
if(allowList == null || allowList.CompareTo("") == 0)
allowList = null;
if(denyList == null || denyList.CompareTo("") == 0)
denyList = null;
/* if theres no deny list and no allow list then allow access */
if (denyList == null && allowList == null)
return true;
/* if there is an allow list but no deny list then allow only hosts
on the allow list */
if (denyList == null)
return(AccessMatch(allowList, addr, host));
/* if theres a deny list but no allow list then allow
all hosts not on the deny list */
if (allowList == null)
return(!AccessMatch(denyList, addr, host));
/* if there are both type of list then allow all hosts on the
allow list */
if (AccessMatch(allowList,addr,host))
return true;
/* if there are both type of list and it's not on the allow then
allow it if its not on the deny */
if (AccessMatch(denyList,addr,host))
return false;
return true;
}
protected bool AccessMatch(string list, string addr, string host)
{
char[] separators = {' ',',','\t'};
string[] list2 = list.ToLower().Split(separators);
if (host != null) host = host.ToLower();
for (int i = 0; i < list2.Length; i++)
{
if(list2[i].CompareTo("") == 0)
continue;
if (MatchHostname(host, list2[i]) || MatchAddress(addr, list2[i]))
{
return true;
}
}
return false;
}
protected bool MatchHostname(string host, string tok)
{
if (host == null) return false;
return WildMatch.CheckWildMatch(tok, host);
}
protected bool MatchAddress(string addr, string tok)
{
IPAddress ipaddr = null, iptok = null;
string p = "";
int len = 0, addrlen = 0;
byte[] mask = new byte[16];
Int64 bits = 0;
if (addr == null || addr.CompareTo("") == 0) return false;
int pos = tok.IndexOf('/');
if (pos > 0)
{
p = tok.Substring(0, pos);
len = p.Length;
}
else
{
len = tok.Length;
}
try
{
ipaddr = IPAddress.Parse(addr);
iptok = IPAddress.Parse(p);
} catch(Exception)
{
return false;
}
if (ipaddr.AddressFamily != iptok.AddressFamily) {
return false;
}
if(iptok.AddressFamily == AddressFamily.InterNetwork)
addrlen = 4;
else
addrlen = 16;
bits = -1;
if(pos > 0)
{
if((mask = InetPton(iptok.AddressFamily, tok.Substring(pos + 1, tok.Length - pos - 1))) == null)
{
mask = new byte[16];
bits = Convert.ToInt64(tok.Substring(pos + 1));
if(bits == 0)
return true;
if (bits < 0 || bits > (addrlen << 3))
{
Log.Write("malformed mask in " + tok);
return false;
}
}
}
else
{
bits = 128;
}
if (bits >= 0)
MakeMask(mask, (int)bits, addrlen);
return MatchBinary(ipaddr.ToString(), iptok.ToString(), mask, addrlen);
}
protected void MakeMask(byte[] mask, int plen, int addrlen)
{
int w, b, i;
w = plen >> 3;
b = plen & 0x7;
if (w > 0)
for(i=0; i<w; i++)
mask[i] = 0xff;
if (w < addrlen)
mask[w] = (byte)(0xff & (0xff<<(8-b)));
if (w+1 < addrlen)
for(i=0; i < addrlen-w-1; i++)
mask[w + 1 + i] = 0;
return;
}
protected bool MatchBinary(string b1, string b2, byte[] mask, int addrlen)
{
int i;
for (i=0; i<addrlen; i++)
{
if(((b1[i]^b2[i]) & mask[i]) > 0)
{
return false;
}
}
return true;
}
protected byte[] InetPton(AddressFamily af, string src)
{
if(af == AddressFamily.InterNetwork)
return InetPton4(src);
else
return InetPton6(src);
}
protected byte[] InetPton4(string src)
{
string digits = "0123456789";
char ch;
byte[] res = new byte[16];
int pos = 0, octets = 0;
bool saw_digit;
saw_digit = false;
octets = 0;
int i = 0;
while (i < src.Length) {
ch = src[i++];
int pch;
if ((pch = digits.IndexOf(ch)) >= 0) {
byte dig = (byte)(res[pos] * 10 + pch);
if (dig > 255)
return null;
res[pos] = dig;
if (!saw_digit) {
if (++octets > 4)
return null;
saw_digit = true;
}
} else if (ch == '.' && saw_digit) {
if (octets == 4)
return null;
pos++;
saw_digit = false;
} else
return null;
}
if (octets < 4)
return null;
return res;
}
protected byte[] InetPton6(string src)
{
string xdigits = "0123456789abcdef";
byte[] res = new byte[NS_IN6ADDRSZ];
string curtok;
bool saw_xdigit;
int pos = 0, colonp = -1, respos = 0;
char ch;
int val;
src = src.ToLower();
/* Leading :: requires some special handling. */
if (src[pos] == ':')
if (src[++pos] != ':')
return null;
curtok = src;
saw_xdigit = false;
val = 0;
while (pos < src.Length - 1) {
ch = src[++pos];
int pch;
if ((pch = xdigits.IndexOf(ch)) >= 0) {
val <<= 4;
val |= pch;
if (val > 0xffff)
return null;
saw_xdigit = true;
continue;
}
if (ch == ':') {
curtok = src;
if (!saw_xdigit) {
if (colonp > 0)
return null;
colonp = respos;
continue;
}
if (respos + NS_INT16SZ > NS_IN6ADDRSZ)
return null;
res[respos++] = (byte)((val >> 8) & 0xff);
res[respos++] = (byte)(val & 0xff);
saw_xdigit = false;
val = 0;
continue;
}
if (ch == '.' && ((respos + NS_INADDRSZ) <= NS_IN6ADDRSZ))
{
byte[] tp = InetPton4(curtok);
if( tp != null )
{
respos += NS_INADDRSZ;
saw_xdigit = false;
break; /* '\0' was seen by inet_pton4(). */
}
}
return null;
}
if (saw_xdigit) {
if (respos + NS_INT16SZ > NS_IN6ADDRSZ)
return null;
res[respos++] = (byte)((val >> 8) & 0xff);
res[respos++] = (byte)(val & 0xff);
}
if (colonp > 0) {
/*
* Since some memmove()'s erroneously fail to handle
* overlapping regions, we'll do the shift by hand.
*/
int n = respos - colonp;
int i;
for (i = 1; i <= n; i++) {
res[res.Length - i] = res[colonp + n - i];
res[colonp + n - i] = 0;
}
respos = NS_IN6ADDRSZ;
}
// if (respos != NS_IN6ADDRSZ)
// return null;
return res;
}
}
}