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