2 #include "auth/digest/auth_digest.h"
3 #include "auth/digest/User.h"
4 #include "auth/digest/UserRequest.h"
5 #include "auth/State.h"
7 #include "HttpHeaderTools.h"
9 #include "HttpRequest.h"
10 #include "SquidTime.h"
12 Auth::Digest::UserRequest::UserRequest() :
24 memset(nc
, 0, sizeof(nc
));
25 memset(&flags
, 0, sizeof(flags
));
29 * Delete the digest request structure.
30 * Does NOT delete related AuthUser structures
32 Auth::Digest::UserRequest::~UserRequest()
34 assert(LockCount()==0);
47 authDigestNonceUnlink(nonce
);
51 Auth::Digest::UserRequest::authenticated() const
53 if (user() != NULL
&& user()->credentials() == Auth::Ok
)
59 /** log a digest user in
62 Auth::Digest::UserRequest::authenticate(HttpRequest
* request
, ConnStateData
* conn
, http_hdr_type type
)
68 /* if the check has corrupted the user, just return */
69 if (user() == NULL
|| user()->credentials() == Auth::Failed
) {
73 Auth::User::Pointer auth_user
= user();
75 Auth::Digest::User
*digest_user
= dynamic_cast<Auth::Digest::User
*>(auth_user
.getRaw());
76 assert(digest_user
!= NULL
);
78 Auth::Digest::UserRequest
*digest_request
= this;
80 /* do we have the HA1 */
81 if (!digest_user
->HA1created
) {
82 auth_user
->credentials(Auth::Pending
);
86 if (digest_request
->nonce
== NULL
) {
87 /* this isn't a nonce we issued */
88 auth_user
->credentials(Auth::Failed
);
92 DigestCalcHA1(digest_request
->algorithm
, NULL
, NULL
, NULL
,
93 authenticateDigestNonceNonceb64(digest_request
->nonce
),
94 digest_request
->cnonce
,
95 digest_user
->HA1
, SESSIONKEY
);
96 DigestCalcResponse(SESSIONKEY
, authenticateDigestNonceNonceb64(digest_request
->nonce
),
97 digest_request
->nc
, digest_request
->cnonce
, digest_request
->qop
,
98 RequestMethodStr(request
->method
), digest_request
->uri
, HA2
, Response
);
100 debugs(29, 9, "\nResponse = '" << digest_request
->response
<< "'\nsquid is = '" << Response
<< "'");
102 if (strcasecmp(digest_request
->response
, Response
) != 0) {
103 if (!digest_request
->flags
.helper_queried
) {
104 /* Query the helper in case the password has changed */
105 digest_request
->flags
.helper_queried
= true;
106 auth_user
->credentials(Auth::Pending
);
110 if (static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->PostWorkaround
&& request
->method
!= Http::METHOD_GET
) {
111 /* Ugly workaround for certain very broken browsers using the
112 * wrong method to calculate the request-digest on POST request.
113 * This should be deleted once Digest authentication becomes more
114 * widespread and such broken browsers no longer are commonly
117 DigestCalcResponse(SESSIONKEY
, authenticateDigestNonceNonceb64(digest_request
->nonce
),
118 digest_request
->nc
, digest_request
->cnonce
, digest_request
->qop
,
119 RequestMethodStr(Http::METHOD_GET
), digest_request
->uri
, HA2
, Response
);
121 if (strcasecmp(digest_request
->response
, Response
)) {
122 auth_user
->credentials(Auth::Failed
);
123 digest_request
->flags
.invalid_password
= true;
124 digest_request
->setDenyMessage("Incorrect password");
127 const char *useragent
= request
->header
.getStr(HDR_USER_AGENT
);
129 static Ip::Address last_broken_addr
;
130 static int seen_broken_client
= 0;
132 if (!seen_broken_client
) {
133 last_broken_addr
.setNoAddr();
134 seen_broken_client
= 1;
137 if (last_broken_addr
!= request
->client_addr
) {
138 debugs(29, DBG_IMPORTANT
, "Digest POST bug detected from " <<
139 request
->client_addr
<< " using '" <<
140 (useragent
? useragent
: "-") <<
141 "'. Please upgrade browser. See Bug #630 for details.");
143 last_broken_addr
= request
->client_addr
;
147 auth_user
->credentials(Auth::Failed
);
148 digest_request
->flags
.invalid_password
= true;
149 digest_request
->setDenyMessage("Incorrect password");
154 /* check for stale nonce */
155 if (!authDigestNonceIsValid(digest_request
->nonce
, digest_request
->nc
)) {
156 debugs(29, 3, "user '" << auth_user
->username() << "' validated OK but nonce stale");
157 auth_user
->credentials(Auth::Failed
);
158 digest_request
->setDenyMessage("Stale nonce");
162 auth_user
->credentials(Auth::Ok
);
164 /* password was checked and did match */
165 debugs(29, 4, HERE
<< "user '" << auth_user
->username() << "' validated OK");
167 /* auth_user is now linked, we reset these values
168 * after external auth occurs anyway */
169 auth_user
->expiretime
= current_time
.tv_sec
;
174 Auth::Digest::UserRequest::module_direction()
176 if (user()->auth_type
!= Auth::AUTH_DIGEST
)
177 return Auth::CRED_ERROR
;
179 switch (user()->credentials()) {
182 return Auth::CRED_VALID
;
185 /* send new challenge */
186 return Auth::CRED_CHALLENGE
;
188 case Auth::Unchecked
:
190 return Auth::CRED_LOOKUP
;
193 return Auth::CRED_ERROR
;
198 Auth::Digest::UserRequest::addAuthenticationInfoHeader(HttpReply
* rep
, int accel
)
202 /* don't add to authentication error pages */
203 if ((!accel
&& rep
->sline
.status() == Http::scProxyAuthenticationRequired
)
204 || (accel
&& rep
->sline
.status() == Http::scUnauthorized
))
207 type
= accel
? HDR_AUTHENTICATION_INFO
: HDR_PROXY_AUTHENTICATION_INFO
;
210 /* test for http/1.1 transfer chunked encoding */
215 if ((static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->authenticateProgram
) && authDigestNonceLastRequest(nonce
)) {
216 flags
.authinfo_sent
= true;
217 debugs(29, 9, HERE
<< "Sending type:" << type
<< " header: 'nextnonce=\"" << authenticateDigestNonceNonceb64(nonce
) << "\"");
218 httpHeaderPutStrf(&rep
->header
, type
, "nextnonce=\"%s\"", authenticateDigestNonceNonceb64(nonce
));
224 Auth::Digest::UserRequest::addAuthenticationInfoTrailer(HttpReply
* rep
, int accel
)
228 if (!auth_user_request
)
231 /* has the header already been send? */
232 if (flags
.authinfo_sent
)
235 /* don't add to authentication error pages */
236 if ((!accel
&& rep
->sline
.status() == Http::scProxyAuthenticationRequired
)
237 || (accel
&& rep
->sline
.status() == Http::scUnauthorized
))
240 type
= accel
? HDR_AUTHENTICATION_INFO
: HDR_PROXY_AUTHENTICATION_INFO
;
242 if ((static_cast<Auth::Digest::Config
*>(digestScheme::GetInstance()->getConfig())->authenticate
) && authDigestNonceLastRequest(nonce
)) {
243 debugs(29, 9, HERE
<< "Sending type:" << type
<< " header: 'nextnonce=\"" << authenticateDigestNonceNonceb64(nonce
) << "\"");
244 httpTrailerPutStrf(&rep
->header
, type
, "nextnonce=\"%s\"", authenticateDigestNonceNonceb64(nonce
));
249 /* send the initial data to a digest authenticator module */
251 Auth::Digest::UserRequest::module_start(AUTHCB
* handler
, void *data
)
255 assert(user() != NULL
&& user()->auth_type
== Auth::AUTH_DIGEST
);
256 debugs(29, 9, HERE
<< "'\"" << user()->username() << "\":\"" << realm
<< "\"'");
258 if (static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->authenticateProgram
== NULL
) {
259 debugs(29, DBG_CRITICAL
, "ERROR: No Digest authentication program configured.");
264 if (static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->utf8
) {
266 latin1_to_utf8(userstr
, sizeof(userstr
), user()->username());
267 snprintf(buf
, 8192, "\"%s\":\"%s\"\n", userstr
, realm
);
269 snprintf(buf
, 8192, "\"%s\":\"%s\"\n", user()->username(), realm
);
272 helperSubmit(digestauthenticators
, buf
, Auth::Digest::UserRequest::HandleReply
,
273 new Auth::StateData(this, handler
, data
));
277 Auth::Digest::UserRequest::HandleReply(void *data
, const HelperReply
&reply
)
279 Auth::StateData
*replyData
= static_cast<Auth::StateData
*>(data
);
280 debugs(29, 9, HERE
<< "reply=" << reply
);
282 assert(replyData
->auth_user_request
!= NULL
);
283 Auth::UserRequest::Pointer auth_user_request
= replyData
->auth_user_request
;
285 // add new helper kv-pair notes to the credentials object
286 // so that any transaction using those credentials can access them
287 auth_user_request
->user()->notes
.appendNewOnly(&reply
.notes
);
289 static bool oldHelperWarningDone
= false;
290 switch (reply
.result
) {
291 case HelperReply::Unknown
: {
292 // Squid 3.3 and older the digest helper only returns a HA1 hash (no "OK")
293 // the HA1 will be found in content() for these responses.
294 if (!oldHelperWarningDone
) {
295 debugs(29, DBG_IMPORTANT
, "WARNING: Digest auth helper returned old format HA1 response. It needs to be upgraded.");
296 oldHelperWarningDone
=true;
299 /* allow this because the digest_request pointer is purely local */
300 Auth::Digest::User
*digest_user
= dynamic_cast<Auth::Digest::User
*>(auth_user_request
->user().getRaw());
301 assert(digest_user
!= NULL
);
303 CvtBin(reply
.other().content(), digest_user
->HA1
);
304 digest_user
->HA1created
= 1;
308 case HelperReply::Okay
: {
309 /* allow this because the digest_request pointer is purely local */
310 Auth::Digest::User
*digest_user
= dynamic_cast<Auth::Digest::User
*>(auth_user_request
->user().getRaw());
311 assert(digest_user
!= NULL
);
313 const char *ha1Note
= reply
.notes
.findFirst("ha1");
314 if (ha1Note
!= NULL
) {
315 CvtBin(ha1Note
, digest_user
->HA1
);
316 digest_user
->HA1created
= 1;
318 debugs(29, DBG_IMPORTANT
, "ERROR: Digest auth helper did not produce a HA1. Using the wrong helper program? received: " << reply
);
323 case HelperReply::TT
:
324 debugs(29, DBG_IMPORTANT
, "ERROR: Digest auth does not support the result code received. Using the wrong helper program? received: " << reply
);
325 // fall through to next case. Handle this as an ERR response.
327 case HelperReply::BrokenHelper
:
328 // TODO retry the broken lookup on another helper?
329 // fall through to next case for now. Handle this as an ERR response silently.
331 case HelperReply::Error
: {
332 /* allow this because the digest_request pointer is purely local */
333 Auth::Digest::UserRequest
*digest_request
= dynamic_cast<Auth::Digest::UserRequest
*>(auth_user_request
.getRaw());
334 assert(digest_request
);
336 digest_request
->user()->credentials(Auth::Failed
);
337 digest_request
->flags
.invalid_password
= true;
339 const char *msgNote
= reply
.notes
.find("message");
340 if (msgNote
!= NULL
) {
341 digest_request
->setDenyMessage(msgNote
);
342 } else if (reply
.other().hasContent()) {
343 // old helpers did send ERR result but a bare message string instead of message= key name.
344 digest_request
->setDenyMessage(reply
.other().content());
345 if (!oldHelperWarningDone
) {
346 debugs(29, DBG_IMPORTANT
, "WARNING: Digest auth helper returned old format ERR response. It needs to be upgraded.");
347 oldHelperWarningDone
=true;
355 if (cbdataReferenceValidDone(replyData
->data
, &cbdata
))
356 replyData
->handler(cbdata
);