]> git.ipfire.org Git - thirdparty/squid.git/blob - src/acl/Arp.cc
Merged from trunk
[thirdparty/squid.git] / src / acl / Arp.cc
1 /*
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* DEBUG: section 28 Access Control */
10
11 #include "squid.h"
12
13 #if USE_SQUID_EUI
14
15 #include "acl/Arp.h"
16 #include "acl/FilledChecklist.h"
17 #include "cache_cf.h"
18 #include "Debug.h"
19 #include "eui/Eui48.h"
20 #include "globals.h"
21 #include "ip/Address.h"
22
23 static void aclParseArpList(Splay<Eui::Eui48 *> **curlist);
24 static int aclMatchArp(Splay<Eui::Eui48 *> **dataptr, Ip::Address &c);
25 static Splay<Eui::Eui48 *>::SPLAYCMP aclArpCompare;
26
27 ACL *
28 ACLARP::clone() const
29 {
30 return new ACLARP(*this);
31 }
32
33 ACLARP::ACLARP (char const *theClass) : data (NULL), class_ (theClass)
34 {}
35
36 ACLARP::ACLARP (ACLARP const & old) : data (NULL), class_ (old.class_)
37 {
38 /* we don't have copy constructors for the data yet */
39 assert (!old.data);
40 }
41
42 ACLARP::~ACLARP()
43 {
44 if (data)
45 data->destroy();
46 }
47
48 char const *
49 ACLARP::typeString() const
50 {
51 return class_;
52 }
53
54 bool
55 ACLARP::empty () const
56 {
57 return data->empty();
58 }
59
60 /* ==== BEGIN ARP ACL SUPPORT ============================================= */
61
62 /*
63 * From: dale@server.ctam.bitmcnit.bryansk.su (Dale)
64 * To: wessels@nlanr.net
65 * Subject: Another Squid patch... :)
66 * Date: Thu, 04 Dec 1997 19:55:01 +0300
67 * ============================================================================
68 *
69 * Working on setting up a proper firewall for a network containing some
70 * Win'95 computers at our Univ, I've discovered that some smart students
71 * avoid the restrictions easily just changing their IP addresses in Win'95
72 * Contol Panel... It has been getting boring, so I took Squid-1.1.18
73 * sources and added a new acl type for hard-wired access control:
74 *
75 * acl <name> arp <Ethernet address> ...
76 *
77 * For example,
78 *
79 * acl students arp 00:00:21:55:ed:22 00:00:21:ff:55:38
80 *
81 * NOTE: Linux code by David Luyer <luyer@ucs.uwa.edu.au>.
82 * Original (BSD-specific) code no longer works.
83 * Solaris code by R. Gancarz <radekg@solaris.elektrownia-lagisza.com.pl>
84 */
85
86 Eui::Eui48 *
87 aclParseArpData(const char *t)
88 {
89 char buf[256];
90 Eui::Eui48 *q = new Eui::Eui48;
91 debugs(28, 5, "aclParseArpData: " << t);
92
93 if (sscanf(t, "%[0-9a-fA-F:]", buf) != 1) {
94 debugs(28, DBG_CRITICAL, "aclParseArpData: Bad ethernet address: '" << t << "'");
95 safe_free(q);
96 return NULL;
97 }
98
99 if (!q->decode(buf)) {
100 debugs(28, DBG_CRITICAL, "" << cfg_filename << " line " << config_lineno << ": " << config_input_line);
101 debugs(28, DBG_CRITICAL, "aclParseArpData: Ignoring invalid ARP acl entry: can't parse '" << buf << "'");
102 safe_free(q);
103 return NULL;
104 }
105
106 return q;
107 }
108
109 /*******************/
110 /* aclParseArpList */
111 /*******************/
112 void
113 ACLARP::parse()
114 {
115 aclParseArpList(&data);
116 }
117
118 void
119 aclParseArpList(Splay<Eui::Eui48 *> **curlist)
120 {
121 char *t = NULL;
122 Eui::Eui48 *q = NULL;
123
124 while ((t = strtokFile())) {
125 if ((q = aclParseArpData(t)) == NULL)
126 continue;
127
128 (*curlist)->insert(q, aclArpCompare);
129 }
130 }
131
132 int
133 ACLARP::match(ACLChecklist *cl)
134 {
135 ACLFilledChecklist *checklist = Filled(cl);
136
137 /* IPv6 does not do ARP */
138 if (!checklist->src_addr.isIPv4()) {
139 debugs(14, 3, "ACLARP::match: IPv4 Required for ARP Lookups. Skipping " << checklist->src_addr );
140 return 0;
141 }
142
143 return aclMatchArp(&data, checklist->src_addr);
144 }
145
146 /***************/
147 /* aclMatchArp */
148 /***************/
149 int
150 aclMatchArp(Splay<Eui::Eui48 *> **dataptr, Ip::Address &c)
151 {
152 Eui::Eui48 lookingFor;
153 if (lookingFor.lookup(c)) {
154 Eui::Eui48 * const* lookupResult;
155 lookupResult = (*dataptr)->find(&lookingFor,aclArpCompare);
156 debugs(28, 3, "aclMatchArp: '" << c << "' " << (lookupResult ? "found" : "NOT found"));
157 return (lookupResult != NULL);
158 }
159 debugs(28, 3, "aclMatchArp: " << c << " NOT found");
160 return 0;
161 }
162
163 static int
164 aclArpCompare(Eui::Eui48 * const &a, Eui::Eui48 * const &b)
165 {
166 return memcmp(a, b, sizeof(Eui::Eui48));
167 }
168
169 // visitor functor to collect the contents of the Arp Acl
170 struct ArpAclDumpVisitor {
171 SBufList contents;
172 void operator() (const Eui::Eui48 * v) {
173 static char buf[48];
174 v->encode(buf,48);
175 contents.push_back(SBuf(buf));
176 }
177 };
178
179 SBufList
180 ACLARP::dump() const
181 {
182 ArpAclDumpVisitor visitor;
183 data->visit(visitor);
184 return visitor.contents;
185 }
186
187 /* ==== END ARP ACL SUPPORT =============================================== */
188
189 #endif /* USE_SQUID_EUI */
190