]> git.ipfire.org Git - thirdparty/squid.git/blob - src/acl/Arp.cc
SourceFormat Enforcement
[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 "Debug.h"
42 #include "eui/Eui48.h"
43 #include "ip/Address.h"
44 #include "protos.h"
45 #include "wordlist.h"
46
47 static void aclParseArpList(SplayNode<Eui::Eui48 *> **curlist);
48 static int aclMatchArp(SplayNode<Eui::Eui48 *> **dataptr, Ip::Address &c);
49 static SplayNode<Eui::Eui48 *>::SPLAYCMP aclArpCompare;
50 static SplayNode<Eui::Eui48 *>::SPLAYWALKEE aclDumpArpListWalkee;
51
52 ACL *
53 ACLARP::clone() const
54 {
55 return new ACLARP(*this);
56 }
57
58 ACLARP::ACLARP (char const *theClass) : data (NULL), class_ (theClass)
59 {}
60
61 ACLARP::ACLARP (ACLARP const & old) : data (NULL), class_ (old.class_)
62 {
63 /* we don't have copy constructors for the data yet */
64 assert (!old.data);
65 }
66
67 ACLARP::~ACLARP()
68 {
69 if (data)
70 data->destroy(SplayNode<Eui::Eui48*>::DefaultFree);
71 }
72
73 char const *
74 ACLARP::typeString() const
75 {
76 return class_;
77 }
78
79 bool
80 ACLARP::empty () const
81 {
82 return data->empty();
83 }
84
85 /* ==== BEGIN ARP ACL SUPPORT ============================================= */
86
87 /*
88 * From: dale@server.ctam.bitmcnit.bryansk.su (Dale)
89 * To: wessels@nlanr.net
90 * Subject: Another Squid patch... :)
91 * Date: Thu, 04 Dec 1997 19:55:01 +0300
92 * ============================================================================
93 *
94 * Working on setting up a proper firewall for a network containing some
95 * Win'95 computers at our Univ, I've discovered that some smart students
96 * avoid the restrictions easily just changing their IP addresses in Win'95
97 * Contol Panel... It has been getting boring, so I took Squid-1.1.18
98 * sources and added a new acl type for hard-wired access control:
99 *
100 * acl <name> arp <Ethernet address> ...
101 *
102 * For example,
103 *
104 * acl students arp 00:00:21:55:ed:22 00:00:21:ff:55:38
105 *
106 * NOTE: Linux code by David Luyer <luyer@ucs.uwa.edu.au>.
107 * Original (BSD-specific) code no longer works.
108 * Solaris code by R. Gancarz <radekg@solaris.elektrownia-lagisza.com.pl>
109 */
110
111 Eui::Eui48 *
112 aclParseArpData(const char *t)
113 {
114 char buf[256];
115 Eui::Eui48 *q = new Eui::Eui48;
116 debugs(28, 5, "aclParseArpData: " << t);
117
118 if (sscanf(t, "%[0-9a-fA-F:]", buf) != 1) {
119 debugs(28, DBG_CRITICAL, "aclParseArpData: Bad ethernet address: '" << t << "'");
120 safe_free(q);
121 return NULL;
122 }
123
124 if (!q->decode(buf)) {
125 debugs(28, DBG_CRITICAL, "" << cfg_filename << " line " << config_lineno << ": " << config_input_line);
126 debugs(28, DBG_CRITICAL, "aclParseArpData: Ignoring invalid ARP acl entry: can't parse '" << buf << "'");
127 safe_free(q);
128 return NULL;
129 }
130
131 return q;
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 */