]> git.ipfire.org Git - thirdparty/squid.git/blob - src/acl/Arp.cc
Merged from trunk
[thirdparty/squid.git] / src / acl / Arp.cc
1 /*
2 * DEBUG: section 28 Access Control
3 * AUTHOR: Duane Wessels
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 *
32 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
33 */
34
35 #include "squid.h"
36
37 #if USE_SQUID_EUI
38
39 #include "acl/Arp.h"
40 #include "acl/FilledChecklist.h"
41 #include "eui/Eui48.h"
42 #include "ip/Address.h"
43 #include "wordlist.h"
44
45 static void aclParseArpList(SplayNode<Eui::Eui48 *> **curlist);
46 static int aclMatchArp(SplayNode<Eui::Eui48 *> **dataptr, Ip::Address &c);
47 static SplayNode<Eui::Eui48 *>::SPLAYCMP aclArpCompare;
48 static SplayNode<Eui::Eui48 *>::SPLAYWALKEE aclDumpArpListWalkee;
49
50
51 ACL *
52 ACLARP::clone() const
53 {
54 return new ACLARP(*this);
55 }
56
57 ACLARP::ACLARP (char const *theClass) : data (NULL), class_ (theClass)
58 {}
59
60 ACLARP::ACLARP (ACLARP const & old) : data (NULL), class_ (old.class_)
61 {
62 /* we don't have copy constructors for the data yet */
63 assert (!old.data);
64 }
65
66 ACLARP::~ACLARP()
67 {
68 if (data)
69 data->destroy(SplayNode<Eui::Eui48*>::DefaultFree);
70 }
71
72 char const *
73 ACLARP::typeString() const
74 {
75 return class_;
76 }
77
78 bool
79 ACLARP::empty () const
80 {
81 return data->empty();
82 }
83
84 /* ==== BEGIN ARP ACL SUPPORT ============================================= */
85
86 /*
87 * From: dale@server.ctam.bitmcnit.bryansk.su (Dale)
88 * To: wessels@nlanr.net
89 * Subject: Another Squid patch... :)
90 * Date: Thu, 04 Dec 1997 19:55:01 +0300
91 * ============================================================================
92 *
93 * Working on setting up a proper firewall for a network containing some
94 * Win'95 computers at our Univ, I've discovered that some smart students
95 * avoid the restrictions easily just changing their IP addresses in Win'95
96 * Contol Panel... It has been getting boring, so I took Squid-1.1.18
97 * sources and added a new acl type for hard-wired access control:
98 *
99 * acl <name> arp <Ethernet address> ...
100 *
101 * For example,
102 *
103 * acl students arp 00:00:21:55:ed:22 00:00:21:ff:55:38
104 *
105 * NOTE: Linux code by David Luyer <luyer@ucs.uwa.edu.au>.
106 * Original (BSD-specific) code no longer works.
107 * Solaris code by R. Gancarz <radekg@solaris.elektrownia-lagisza.com.pl>
108 */
109
110 Eui::Eui48 *
111 aclParseArpData(const char *t)
112 {
113 char buf[256];
114 Eui::Eui48 *q = new Eui::Eui48;
115 debugs(28, 5, "aclParseArpData: " << t);
116
117 if (sscanf(t, "%[0-9a-fA-F:]", buf) != 1) {
118 debugs(28, 0, "aclParseArpData: Bad ethernet address: '" << t << "'");
119 safe_free(q);
120 return NULL;
121 }
122
123 if (!q->decode(buf)) {
124 debugs(28, 0, "" << cfg_filename << " line " << config_lineno << ": " << config_input_line);
125 debugs(28, 0, "aclParseArpData: Ignoring invalid ARP acl entry: can't parse '" << buf << "'");
126 safe_free(q);
127 return NULL;
128 }
129
130 return q;
131 }
132
133
134 /*******************/
135 /* aclParseArpList */
136 /*******************/
137 void
138 ACLARP::parse()
139 {
140 aclParseArpList(&data);
141 }
142
143 void
144 aclParseArpList(SplayNode<Eui::Eui48 *> **curlist)
145 {
146 char *t = NULL;
147 SplayNode<Eui::Eui48*> **Top = curlist;
148 Eui::Eui48 *q = NULL;
149
150 while ((t = strtokFile())) {
151 if ((q = aclParseArpData(t)) == NULL)
152 continue;
153
154 *Top = (*Top)->insert(q, aclArpCompare);
155 }
156 }
157
158 int
159 ACLARP::match(ACLChecklist *cl)
160 {
161 ACLFilledChecklist *checklist = Filled(cl);
162
163 /* IPv6 does not do ARP */
164 if (!checklist->src_addr.IsIPv4()) {
165 debugs(14, 3, "ACLARP::match: IPv4 Required for ARP Lookups. Skipping " << checklist->src_addr );
166 return 0;
167 }
168
169 return aclMatchArp(&data, checklist->src_addr);
170 }
171
172 /***************/
173 /* aclMatchArp */
174 /***************/
175 int
176 aclMatchArp(SplayNode<Eui::Eui48 *> **dataptr, Ip::Address &c)
177 {
178 Eui::Eui48 result;
179 SplayNode<Eui::Eui48 *> **Top = dataptr;
180
181 if (result.lookup(c)) {
182 /* Do ACL match lookup */
183 *Top = (*Top)->splay(&result, aclArpCompare);
184 debugs(28, 3, "aclMatchArp: '" << c << "' " << (splayLastResult ? "NOT found" : "found"));
185 return (0 == splayLastResult);
186 }
187
188 /*
189 * Address was not found on any interface
190 */
191 debugs(28, 3, "aclMatchArp: " << c << " NOT found");
192 return 0;
193 }
194
195 static int
196 aclArpCompare(Eui::Eui48 * const &a, Eui::Eui48 * const &b)
197 {
198 return memcmp(a, b, sizeof(Eui::Eui48));
199 }
200
201 static void
202 aclDumpArpListWalkee(Eui::Eui48 * const &node, void *state)
203 {
204 static char buf[48];
205 node->encode(buf, 48);
206 wordlistAdd((wordlist **)state, buf);
207 }
208
209 wordlist *
210 ACLARP::dump() const
211 {
212 wordlist *w = NULL;
213 data->walk(aclDumpArpListWalkee, &w);
214 return w;
215 }
216
217 /* ==== END ARP ACL SUPPORT =============================================== */
218
219 #endif /* USE_SQUID_EUI */