2 #include "acl/FilledChecklist.h"
3 #include "adaptation/AccessCheck.h"
4 #include "adaptation/AccessRule.h"
5 #include "adaptation/Config.h"
6 #include "adaptation/Initiator.h"
7 #include "adaptation/Service.h"
8 #include "adaptation/ServiceGroups.h"
9 #include "base/AsyncJobCalls.h"
10 #include "base/TextException.h"
11 #include "ConfigParser.h"
13 #include "HttpReply.h"
14 #include "HttpRequest.h"
17 /** \cond AUTODOCS-IGNORE */
18 cbdata_type
Adaptation::AccessCheck::CBDATA_AccessCheck
= CBDATA_UNKNOWN
;
22 Adaptation::AccessCheck::Start(Method method
, VectPoint vp
,
23 HttpRequest
*req
, HttpReply
*rep
, Adaptation::Initiator
*initiator
)
26 if (Config::Enabled
) {
27 // the new check will call the callback and delete self, eventually
28 AsyncJob::Start(new AccessCheck( // we do not store so not a CbcPointer
29 ServiceFilter(method
, vp
, req
, rep
), initiator
));
33 debugs(83, 3, HERE
<< "adaptation off, skipping");
37 Adaptation::AccessCheck::AccessCheck(const ServiceFilter
&aFilter
,
38 Adaptation::Initiator
*initiator
):
39 AsyncJob("AccessCheck"), filter(aFilter
),
40 theInitiator(initiator
),
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();
63 Adaptation::AccessCheck::start()
67 if (!usedDynamicRules())
71 /// returns true if previous services configured dynamic chaining "rules"
73 Adaptation::AccessCheck::usedDynamicRules()
75 Adaptation::History::Pointer ah
= filter
.request
->adaptHistory();
77 return false; // dynamic rules not enabled or not triggered
79 DynamicGroupCfg services
;
80 if (!ah
->extractFutureServices(services
)) { // clears history
81 debugs(85,9, HERE
<< "no service-proposed rules stored");
82 return false; // earlier service did not plan for the future
85 debugs(85,3, HERE
<< "using stored service-proposed rules: " << services
);
87 ServiceGroupPointer g
= new DynamicServiceChain(services
, filter
);
93 /// Walk the access rules list to find rules with applicable service groups
95 Adaptation::AccessCheck::check()
97 debugs(93, 4, HERE
<< "start checking");
99 typedef AccessRules::iterator ARI
;
100 for (ARI i
= AllRules().begin(); i
!= AllRules().end(); ++i
) {
102 if (isCandidate(*r
)) {
103 debugs(93, 5, HERE
<< "check: rule '" << r
->id
<< "' is a candidate");
111 // XXX: Here and everywhere we call FindRule(topCandidate()):
112 // Once we identified the candidate, we should not just ignore it
113 // if reconfigure changes rules. We should either lock the rule to
114 // prevent reconfigure from stealing it or restart the check with
115 // new rules. Throwing an exception may also be appropriate.
117 Adaptation::AccessCheck::checkCandidates()
119 debugs(93, 4, HERE
<< "has " << candidates
.size() << " rules");
121 while (!candidates
.empty()) {
122 if (AccessRule
*r
= FindRule(topCandidate())) {
123 /* BUG 2526: what to do when r->acl is empty?? */
124 // XXX: we do not have access to conn->rfc931 here.
125 acl_checklist
= new ACLFilledChecklist(r
->acl
, filter
.request
, dash_str
);
126 acl_checklist
->reply
= filter
.reply
? HTTPMSGLOCK(filter
.reply
) : NULL
;
127 acl_checklist
->nonBlockingCheck(AccessCheckCallbackWrapper
, this);
131 candidates
.shift(); // the rule apparently went away (reconfigure)
134 debugs(93, 4, HERE
<< "NO candidates left");
140 Adaptation::AccessCheck::AccessCheckCallbackWrapper(allow_t answer
, void *data
)
142 debugs(93, 8, HERE
<< "callback answer=" << answer
);
143 AccessCheck
*ac
= (AccessCheck
*)data
;
145 /** \todo AYJ 2008-06-12: If answer == ACCESS_AUTH_REQUIRED
146 * we should be kicking off an authentication before continuing
147 * with this request. see bug 2400 for details.
150 // convert to async call to get async call protections and features
151 typedef UnaryMemFunT
<AccessCheck
, allow_t
> MyDialer
;
152 AsyncCall::Pointer call
=
153 asyncCall(93,7, "Adaptation::AccessCheck::noteAnswer",
154 MyDialer(ac
, &Adaptation::AccessCheck::noteAnswer
, answer
));
155 ScheduleCallHere(call
);
159 /// process the results of the ACL check
161 Adaptation::AccessCheck::noteAnswer(allow_t answer
)
163 Must(!candidates
.empty()); // the candidate we were checking must be there
164 debugs(93,5, HERE
<< topCandidate() << " answer=" << answer
);
166 if (answer
== ACCESS_ALLOWED
) { // the rule matched
167 ServiceGroupPointer g
= topGroup();
168 if (g
!= NULL
) { // the corresponding group found
175 // no match or the group disappeared during reconfiguration
180 /// call back with a possibly nil group; the job ends here because all failures
181 /// at this point are fatal to the access check process
183 Adaptation::AccessCheck::callBack(const ServiceGroupPointer
&g
)
185 debugs(93,3, HERE
<< g
);
186 CallJobHere1(93, 5, theInitiator
, Adaptation::Initiator
,
187 noteAdaptationAclCheckDone
, g
);
188 mustStop("done"); // called back or will never be able to call back
191 Adaptation::ServiceGroupPointer
192 Adaptation::AccessCheck::topGroup() const
194 ServiceGroupPointer g
;
195 if (candidates
.size()) {
196 if (AccessRule
*r
= FindRule(topCandidate())) {
197 g
= FindGroup(r
->groupId
);
198 debugs(93,5, HERE
<< "top group for " << r
->id
<< " is " << g
);
200 debugs(93,5, HERE
<< "no rule for " << topCandidate());
203 debugs(93,5, HERE
<< "no candidates"); // should not happen
209 /** Returns true iff the rule's service group will be used after ACL matches.
210 Used to detect rules worth ACl-checking. */
212 Adaptation::AccessCheck::isCandidate(AccessRule
&r
)
214 debugs(93,7,HERE
<< "checking candidacy of " << r
.id
<< ", group " <<
217 ServiceGroupPointer g
= FindGroup(r
.groupId
);
220 debugs(93,7,HERE
<< "lost " << r
.groupId
<< " group in rule" << r
.id
);
224 const bool wants
= g
->wants(filter
);
225 debugs(93,7,HERE
<< r
.groupId
<< (wants
? " wants" : " ignores"));