]> git.ipfire.org Git - thirdparty/squid.git/blame - src/acl/RegexData.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / acl / RegexData.cc
CommitLineData
225b7b10 1/*
262a0e14 2 * $Id$
225b7b10 3 *
4 * DEBUG: section 28 Access Control
5 * AUTHOR: Duane Wessels
6 *
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
26ac0430 23 *
225b7b10 24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
26ac0430 28 *
225b7b10 29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 *
33 *
34 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
35 */
36
37#include "squid.h"
3ad63615
AR
38#include "acl/RegexData.h"
39#include "acl/Checklist.h"
40#include "acl/Acl.h"
d295d770 41#include "wordlist.h"
42#include "ConfigParser.h"
225b7b10 43
48071869 44static void aclDestroyRegexList(relist * data);
45void
46aclDestroyRegexList(relist * data)
47{
48 relist *next = NULL;
49
50 for (; data; data = next) {
51 next = data->next;
52 regfree(&data->regex);
53 safe_free(data->pattern);
54 memFree(data, MEM_RELIST);
55 }
56}
57
225b7b10 58ACLRegexData::~ACLRegexData()
59{
60 aclDestroyRegexList(data);
62e76326 61}
225b7b10 62
63bool
48071869 64ACLRegexData::match(char const *word)
225b7b10 65{
48071869 66 if (word == NULL)
67 return 0;
68
bf8fe701 69 debugs(28, 3, "aclRegexData::match: checking '" << word << "'");
48071869 70
71 relist *first, *prev;
72
73 first = data;
74
75 prev = NULL;
76
77 relist *current = first;
78
79 while (current) {
bf8fe701 80 debugs(28, 3, "aclRegexData::match: looking for '" << current->pattern << "'");
48071869 81
82 if (regexec(&current->regex, word, 0, 0, 0) == 0) {
83 if (prev != NULL) {
84 /* shift the element just found to the second position
85 * in the list */
86 prev->next = current->next;
87 current->next = first->next;
88 first->next = current;
89 }
90
bf8fe701 91 debugs(28, 2, "aclRegexData::match: match '" << current->pattern << "' found in '" << word << "'");
48071869 92 return 1;
93 }
94
95 prev = current;
96 current = current->next;
97 }
98
99 return 0;
225b7b10 100}
101
102wordlist *
103ACLRegexData::dump()
104{
48071869 105 wordlist *W = NULL;
106 relist *temp = data;
ae315d9c 107 int flags = REG_EXTENDED | REG_NOSUB;
48071869 108
109 while (temp != NULL) {
ae315d9c 110 if (temp->flags != flags) {
37a8ae40 111 if ((temp->flags&REG_ICASE) != 0) {
ae315d9c
AJ
112 wordlistAdd(&W, "-i");
113 } else {
114 wordlistAdd(&W, "+i");
115 }
116 flags = temp->flags;
117 }
118
48071869 119 wordlistAdd(&W, temp->pattern);
120 temp = temp->next;
121 }
122
123 return W;
124}
125
126static void aclParseRegexList(relist **curlist);
127void
128aclParseRegexList(relist **curlist)
129{
130 relist **Tail;
131 relist *q = NULL;
132 char *t = NULL;
133 regex_t comp;
134 int errcode;
135 int flags = REG_EXTENDED | REG_NOSUB;
136
3d0ac046 137 for (Tail = (relist **)curlist; *Tail; Tail = &((*Tail)->next));
d295d770 138 while ((t = ConfigParser::strtokFile())) {
48071869 139 if (strcmp(t, "-i") == 0) {
140 flags |= REG_ICASE;
141 continue;
142 }
143
144 if (strcmp(t, "+i") == 0) {
145 flags &= ~REG_ICASE;
146 continue;
147 }
148
149 if ((errcode = regcomp(&comp, t, flags)) != 0) {
150 char errbuf[256];
151 regerror(errcode, &comp, errbuf, sizeof errbuf);
bf8fe701 152 debugs(28, 0, "" << cfg_filename << " line " << config_lineno << ": " << config_input_line);
153 debugs(28, 0, "aclParseRegexList: Invalid regular expression '" << t << "': " << errbuf);
48071869 154 continue;
155 }
156
157 q = (relist *)memAllocate(MEM_RELIST);
ae315d9c 158 q->flags = flags;
48071869 159 q->pattern = xstrdup(t);
160 q->regex = comp;
161 *(Tail) = q;
162 Tail = &q->next;
163 }
225b7b10 164}
165
166void
167ACLRegexData::parse()
168{
169 aclParseRegexList(&data);
170}
171
65092baf 172bool
173ACLRegexData::empty() const
174{
175 return data == NULL;
176}
225b7b10 177
5dee515e 178ACLData<char const *> *
225b7b10 179ACLRegexData::clone() const
180{
181 /* Regex's don't clone yet. */
182 assert (!data);
183 return new ACLRegexData;
184}