]> git.ipfire.org Git - thirdparty/squid.git/blob - src/acl/UserData.cc
Use SBufList instead of wordlist to collect data for mgr:config
[thirdparty/squid.git] / src / acl / UserData.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 #include "acl/Checklist.h"
37 #include "acl/UserData.h"
38 #include "ConfigParser.h"
39 #include "Debug.h"
40
41 template<class T>
42 inline void
43 xRefFree(T &thing)
44 {
45 xfree (thing);
46 }
47
48 ACLUserData::~ACLUserData()
49 {
50 if (names)
51 names->destroy(xRefFree);
52 }
53
54 static int
55 splaystrcasecmp (char * const &l, char * const &r)
56 {
57 return strcasecmp ((char *)l,(char *)r);
58 }
59
60 static int
61 splaystrcmp (char * const &l, char * const &r)
62 {
63 return strcmp ((char *)l,(char *)r);
64 }
65
66 bool
67 ACLUserData::match(char const *user)
68 {
69 SplayNode<char *> *Top = names;
70
71 debugs(28, 7, "aclMatchUser: user is " << user << ", case_insensitive is " << flags.case_insensitive);
72 debugs(28, 8, "Top is " << Top << ", Top->data is " << ((char *) (Top != NULL ? (Top)->data : "Unavailable")));
73
74 if (user == NULL || strcmp(user, "-") == 0)
75 return 0;
76
77 if (flags.required) {
78 debugs(28, 7, "aclMatchUser: user REQUIRED and auth-info present.");
79 return 1;
80 }
81
82 if (flags.case_insensitive)
83 Top = Top->splay((char *)user, splaystrcasecmp);
84 else
85 Top = Top->splay((char *)user, splaystrcmp);
86
87 /* Top=splay_splay(user,Top,(splayNode::SPLAYCMP *)dumping_strcmp); */
88 debugs(28, 7, "aclMatchUser: returning " << !splayLastResult << ",Top is " <<
89 Top << ", Top->data is " << ((char *) (Top ? Top->data : "Unavailable")));
90
91 names = Top;
92
93 return !splayLastResult;
94 }
95
96 static void
97 aclDumpUserListWalkee(char * const & node_data, void *outlist)
98 {
99 /* outlist is really a SBufList* */
100 static_cast<SBufList *>(outlist)->push_back(SBuf(node_data));
101 }
102
103 SBufList
104 ACLUserData::dump() const
105 {
106 SBufList sl;
107
108 if (flags.case_insensitive)
109 sl.push_back(SBuf("-i"));
110
111 /* damn this is VERY inefficient for long ACL lists... filling
112 * a SBufList this way costs Sum(1,N) iterations. For instance
113 * a 1000-elements list will be filled in 499500 iterations.
114 */
115 if (flags.required)
116 sl.push_back(SBuf("REQUIRED"));
117 else if (names)
118 names->walk(aclDumpUserListWalkee, &sl);
119
120 return sl;
121 }
122
123 void
124 ACLUserData::parse()
125 {
126 debugs(28, 2, "aclParseUserList: parsing user list");
127 char *t = NULL;
128
129 if ((t = ConfigParser::strtokFile())) {
130 debugs(28, 5, "aclParseUserList: First token is " << t);
131
132 if (strcmp("-i", t) == 0) {
133 debugs(28, 5, "aclParseUserList: Going case-insensitive");
134 flags.case_insensitive = true;
135 } else if (strcmp("REQUIRED", t) == 0) {
136 debugs(28, 5, "aclParseUserList: REQUIRED-type enabled");
137 flags.required = true;
138 } else {
139 if (flags.case_insensitive)
140 Tolower(t);
141
142 names = names->insert(xstrdup(t), splaystrcmp);
143 }
144 }
145
146 debugs(28, 3, "aclParseUserList: Case-insensitive-switch is " << flags.case_insensitive);
147 /* we might inherit from a previous declaration */
148
149 debugs(28, 4, "aclParseUserList: parsing user list");
150
151 while ((t = ConfigParser::strtokFile())) {
152 debugs(28, 6, "aclParseUserList: Got token: " << t);
153
154 if (flags.case_insensitive)
155 Tolower(t);
156
157 names = names->insert(xstrdup(t), splaystrcmp);
158 }
159 }
160
161 bool
162 ACLUserData::empty() const
163 {
164 return names->empty() && !flags.required;
165 }
166
167 ACLData<char const *> *
168 ACLUserData::clone() const
169 {
170 /* Splay trees don't clone yet. */
171 assert (!names);
172 return new ACLUserData;
173 }