]> git.ipfire.org Git - thirdparty/squid.git/blob - src/acl/UserData.cc
Merged from trunk
[thirdparty/squid.git] / src / acl / UserData.cc
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* DEBUG: section 28 Access Control */
10
11 #include "squid.h"
12 #include "acl/Checklist.h"
13 #include "acl/UserData.h"
14 #include "ConfigParser.h"
15 #include "Debug.h"
16 #include "globals.h"
17 #include "SBufAlgos.h"
18 #include "util.h"
19
20 bool
21 ACLUserData::match(char const *user)
22 {
23 debugs(28, 7, "user is " << user << ", case_insensitive is " << flags.case_insensitive);
24
25 if (user == NULL || strcmp(user, "-") == 0)
26 return 0;
27
28 if (flags.required) {
29 debugs(28, 7, "aclMatchUser: user REQUIRED and auth-info present.");
30 return 1;
31 }
32
33 bool result = (userDataNames.find(SBuf(user)) != userDataNames.end());
34 debugs(28, 7, "returning " << result);
35 return result;
36 }
37
38 SBufList
39 ACLUserData::dump() const
40 {
41 SBufList sl;
42
43 if (flags.required) {
44 sl.push_back(SBuf("REQUIRED"));
45 return sl;
46 }
47
48 if (flags.case_insensitive)
49 sl.push_back(SBuf("-i"));
50
51 sl.insert(sl.end(), userDataNames.begin(), userDataNames.end());
52
53 debugs(28,5, "ACLUserData dump output: " << SBufContainerJoin(userDataNames,SBuf(" ")));
54 return sl;
55 }
56
57 static bool
58 CaseInsensitveSBufCompare(const SBuf &lhs, const SBuf &rhs)
59 {
60 return (lhs.caseCmp(rhs) < 0);
61 }
62
63 static bool
64 CaseSensitveSBufCompare(const SBuf &lhs, const SBuf &rhs)
65 {
66 return (lhs < rhs);
67 }
68
69 ACLUserData::ACLUserData() : userDataNames(CaseSensitveSBufCompare)
70 {
71 }
72
73 void
74 ACLUserData::parse()
75 {
76 debugs(28, 2, "parsing user list");
77
78 char *t = NULL;
79 if ((t = ConfigParser::strtokFile())) {
80 SBuf s(t);
81 debugs(28, 5, "first token is " << s);
82
83 if (s.cmp("-i",2) == 0) {
84 debugs(28, 5, "Going case-insensitive");
85 flags.case_insensitive = true;
86 // due to how the std::set API work, if we want to change
87 // the comparison function we have to create a new std::set
88 UserDataNames_t newUdn(CaseInsensitveSBufCompare);
89 newUdn.insert(userDataNames.begin(), userDataNames.end());
90 swap(userDataNames,newUdn);
91 } else if (s.cmp("REQUIRED") == 0) {
92 debugs(28, 5, "REQUIRED-type enabled");
93 flags.required = true;
94 } else {
95 if (flags.case_insensitive)
96 s.toLower();
97
98 debugs(28, 6, "Adding user " << s);
99 userDataNames.insert(s);
100 }
101 }
102
103 debugs(28, 3, "Case-insensitive-switch is " << flags.case_insensitive);
104 /* we might inherit from a previous declaration */
105
106 debugs(28, 4, "parsing following tokens");
107
108 while ((t = ConfigParser::strtokFile())) {
109 SBuf s(t);
110 debugs(28, 6, "Got token: " << s);
111
112 if (flags.case_insensitive)
113 s.toLower();
114
115 debugs(28, 6, "Adding user " << s);
116 userDataNames.insert(s);
117 }
118
119 if (flags.required && !userDataNames.empty()) {
120 debugs(28, DBG_PARSE_NOTE(1), "WARNING: detected attempt to add usernames to an acl of type REQUIRED");
121 userDataNames.clear();
122 }
123
124 debugs(28,4, "ACL contains " << userDataNames.size() << " users");
125 }
126
127 bool
128 ACLUserData::empty() const
129 {
130 debugs(28,6,"required: " << flags.required << ", number of users: " << userDataNames.size());
131 if (flags.required)
132 return false;
133 return userDataNames.empty();
134 }
135
136 ACLData<char const *> *
137 ACLUserData::clone() const
138 {
139 return new ACLUserData;
140 }
141