4 #include "ConfigParser.h"
5 #include "HttpRequest.h"
7 #include "acl/FilledChecklist.h"
8 #include "adaptation/Service.h"
9 #include "adaptation/ServiceGroups.h"
10 #include "adaptation/AccessRule.h"
11 #include "adaptation/Config.h"
12 #include "adaptation/AccessCheck.h"
13 #include "base/TextException.h"
15 /** \cond AUTODOCS-IGNORE */
16 cbdata_type
Adaptation::AccessCheck::CBDATA_AccessCheck
= CBDATA_UNKNOWN
;
20 Adaptation::AccessCheck::Start(Method method
, VectPoint vp
,
21 HttpRequest
*req
, HttpReply
*rep
, AccessCheckCallback
*cb
, void *cbdata
)
24 if (Config::Enabled
) {
25 // the new check will call the callback and delete self, eventually
26 AsyncJob::Start(new AccessCheck( // we do not store so not a CbcPointer
27 ServiceFilter(method
, vp
, req
, rep
), cb
, cbdata
));
31 debugs(83, 3, HERE
<< "adaptation off, skipping");
35 Adaptation::AccessCheck::AccessCheck(const ServiceFilter
&aFilter
,
36 AccessCheckCallback
*aCallback
,
38 AsyncJob("AccessCheck"), filter(aFilter
),
40 callback_data(cbdataReference(aCallbackData
)),
44 Adaptation::Icap::History::Pointer h
= filter
.request
->icapHistory();
49 debugs(93, 5, HERE
<< "AccessCheck constructed for " <<
50 methodStr(filter
.method
) << " " << vectPointStr(filter
.point
));
53 Adaptation::AccessCheck::~AccessCheck()
56 Adaptation::Icap::History::Pointer h
= filter
.request
->icapHistory();
61 cbdataReferenceDone(callback_data
);
65 Adaptation::AccessCheck::start()
71 /// Walk the access rules list to find rules with applicable service groups
73 Adaptation::AccessCheck::check()
75 debugs(93, 4, HERE
<< "start checking");
77 typedef AccessRules::iterator ARI
;
78 for (ARI i
= AllRules().begin(); i
!= AllRules().end(); ++i
) {
80 if (isCandidate(*r
)) {
81 debugs(93, 5, HERE
<< "check: rule '" << r
->id
<< "' is a candidate");
89 // XXX: Here and everywhere we call FindRule(topCandidate()):
90 // Once we identified the candidate, we should not just ignore it
91 // if reconfigure changes rules. We should either lock the rule to
92 // prevent reconfigure from stealing it or restart the check with
93 // new rules. Throwing an exception may also be appropriate.
95 Adaptation::AccessCheck::checkCandidates()
97 debugs(93, 4, HERE
<< "has " << candidates
.size() << " rules");
99 while (!candidates
.empty()) {
100 if (AccessRule
*r
= FindRule(topCandidate())) {
101 /* BUG 2526: what to do when r->acl is empty?? */
102 // XXX: we do not have access to conn->rfc931 here.
103 acl_checklist
= new ACLFilledChecklist(r
->acl
, filter
.request
, dash_str
);
104 acl_checklist
->reply
= filter
.reply
? HTTPMSGLOCK(filter
.reply
) : NULL
;
105 acl_checklist
->nonBlockingCheck(AccessCheckCallbackWrapper
, this);
109 candidates
.shift(); // the rule apparently went away (reconfigure)
112 debugs(93, 4, HERE
<< "NO candidates left");
118 Adaptation::AccessCheck::AccessCheckCallbackWrapper(int answer
, void *data
)
120 debugs(93, 8, HERE
<< "callback answer=" << answer
);
121 AccessCheck
*ac
= (AccessCheck
*)data
;
123 /** \todo AYJ 2008-06-12: If answer == ACCESS_REQ_PROXY_AUTH
124 * we should be kicking off an authentication before continuing
125 * with this request. see bug 2400 for details.
128 // convert to async call to get async call protections and features
129 typedef UnaryMemFunT
<AccessCheck
, int> MyDialer
;
130 AsyncCall::Pointer call
=
131 asyncCall(93,7, "Adaptation::AccessCheck::noteAnswer",
132 MyDialer(ac
, &Adaptation::AccessCheck::noteAnswer
,
133 answer
==ACCESS_ALLOWED
));
134 ScheduleCallHere(call
);
138 /// process the results of the ACL check
140 Adaptation::AccessCheck::noteAnswer(int answer
)
142 Must(!candidates
.empty()); // the candidate we were checking must be there
143 debugs(93,5, HERE
<< topCandidate() << " answer=" << answer
);
145 if (answer
) { // the rule matched
146 ServiceGroupPointer g
= topGroup();
147 if (g
!= NULL
) { // the corresponding group found
154 // no match or the group disappeared during reconfiguration
159 /// call back with a possibly nil group; the job ends here because all failures
160 /// at this point are fatal to the access check process
162 Adaptation::AccessCheck::callBack(const ServiceGroupPointer
&g
)
164 debugs(93,3, HERE
<< g
);
166 void *validated_cbdata
;
167 if (cbdataReferenceValidDone(callback_data
, &validated_cbdata
)) {
168 callback(g
, validated_cbdata
);
170 mustStop("done"); // called back or will never be able to call back
173 Adaptation::ServiceGroupPointer
174 Adaptation::AccessCheck::topGroup() const
176 ServiceGroupPointer g
;
177 if (candidates
.size()) {
178 if (AccessRule
*r
= FindRule(topCandidate())) {
179 g
= FindGroup(r
->groupId
);
180 debugs(93,5, HERE
<< "top group for " << r
->id
<< " is " << g
);
182 debugs(93,5, HERE
<< "no rule for " << topCandidate());
185 debugs(93,5, HERE
<< "no candidates"); // should not happen
191 /** Returns true iff the rule's service group will be used after ACL matches.
192 Used to detect rules worth ACl-checking. */
194 Adaptation::AccessCheck::isCandidate(AccessRule
&r
)
196 debugs(93,7,HERE
<< "checking candidacy of " << r
.id
<< ", group " <<
199 ServiceGroupPointer g
= FindGroup(r
.groupId
);
202 debugs(93,7,HERE
<< "lost " << r
.groupId
<< " group in rule" << r
.id
);
206 const bool wants
= g
->wants(filter
);
207 debugs(93,7,HERE
<< r
.groupId
<< (wants
? " wants" : " ignores"));