]> git.ipfire.org Git - thirdparty/squid.git/blob - src/acl/UserData.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / acl / UserData.cc
1 /*
2 * Copyright (C) 1996-2017 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 "sbuf/Algorithms.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: " <<
54 JoinContainerToSBuf(userDataNames.begin(), userDataNames.end(),
55 SBuf(" ")));
56 return sl;
57 }
58
59 static bool
60 CaseSensitiveSBufCompare(const SBuf &lhs, const SBuf &rhs)
61 {
62 return (lhs.cmp(rhs) < 0);
63 }
64
65 static bool
66 CaseInsensitveSBufCompare(const SBuf &lhs, const SBuf &rhs)
67 {
68 return (lhs.caseCmp(rhs) < 0);
69 }
70
71 ACLUserData::ACLUserData() :
72 userDataNames(CaseSensitiveSBufCompare)
73 {
74 flags.case_insensitive = false;
75 flags.required = false;
76 }
77
78 void
79 ACLUserData::parse()
80 {
81 debugs(28, 2, "parsing user list");
82
83 char *t = NULL;
84 if ((t = ConfigParser::strtokFile())) {
85 SBuf s(t);
86 debugs(28, 5, "first token is " << s);
87
88 if (s.cmp("-i",2) == 0) {
89 debugs(28, 5, "Going case-insensitive");
90 flags.case_insensitive = true;
91 // due to how the std::set API work, if we want to change
92 // the comparison function we have to create a new std::set
93 UserDataNames_t newUdn(CaseInsensitveSBufCompare);
94 newUdn.insert(userDataNames.begin(), userDataNames.end());
95 swap(userDataNames,newUdn);
96 } else if (s.cmp("REQUIRED") == 0) {
97 debugs(28, 5, "REQUIRED-type enabled");
98 flags.required = true;
99 } else {
100 if (flags.case_insensitive)
101 s.toLower();
102
103 debugs(28, 6, "Adding user " << s);
104 userDataNames.insert(s);
105 }
106 }
107
108 debugs(28, 3, "Case-insensitive-switch is " << flags.case_insensitive);
109 /* we might inherit from a previous declaration */
110
111 debugs(28, 4, "parsing following tokens");
112
113 while ((t = ConfigParser::strtokFile())) {
114 SBuf s(t);
115 debugs(28, 6, "Got token: " << s);
116
117 if (flags.case_insensitive)
118 s.toLower();
119
120 debugs(28, 6, "Adding user " << s);
121 userDataNames.insert(s);
122 }
123
124 if (flags.required && !userDataNames.empty()) {
125 debugs(28, DBG_PARSE_NOTE(1), "WARNING: detected attempt to add usernames to an acl of type REQUIRED");
126 userDataNames.clear();
127 }
128
129 debugs(28,4, "ACL contains " << userDataNames.size() << " users");
130 }
131
132 bool
133 ACLUserData::empty() const
134 {
135 debugs(28,6,"required: " << flags.required << ", number of users: " << userDataNames.size());
136 if (flags.required)
137 return false;
138 return userDataNames.empty();
139 }
140
141 ACLData<char const *> *
142 ACLUserData::clone() const
143 {
144 return new ACLUserData;
145 }
146