2 #include "AccessLogEntry.h"
3 #include "auth/digest/auth_digest.h"
4 #include "auth/digest/User.h"
5 #include "auth/digest/UserRequest.h"
6 #include "auth/State.h"
8 #include "format/Format.h"
9 #include "HttpHeaderTools.h"
10 #include "HttpReply.h"
11 #include "HttpRequest.h"
13 #include "SquidTime.h"
15 Auth::Digest::UserRequest::UserRequest() :
27 memset(nc
, 0, sizeof(nc
));
28 memset(&flags
, 0, sizeof(flags
));
32 * Delete the digest request structure.
33 * Does NOT delete related AuthUser structures
35 Auth::Digest::UserRequest::~UserRequest()
37 assert(LockCount()==0);
50 authDigestNonceUnlink(nonce
);
54 Auth::Digest::UserRequest::authenticated() const
56 if (user() != NULL
&& user()->credentials() == Auth::Ok
)
63 Auth::Digest::UserRequest::credentialsStr()
68 /** log a digest user in
71 Auth::Digest::UserRequest::authenticate(HttpRequest
* request
, ConnStateData
* conn
, http_hdr_type type
)
77 /* if the check has corrupted the user, just return */
78 if (user() == NULL
|| user()->credentials() == Auth::Failed
) {
82 Auth::User::Pointer auth_user
= user();
84 Auth::Digest::User
*digest_user
= dynamic_cast<Auth::Digest::User
*>(auth_user
.getRaw());
85 assert(digest_user
!= NULL
);
87 Auth::Digest::UserRequest
*digest_request
= this;
89 /* do we have the HA1 */
90 if (!digest_user
->HA1created
) {
91 auth_user
->credentials(Auth::Pending
);
95 if (digest_request
->nonce
== NULL
) {
96 /* this isn't a nonce we issued */
97 auth_user
->credentials(Auth::Failed
);
101 DigestCalcHA1(digest_request
->algorithm
, NULL
, NULL
, NULL
,
102 authenticateDigestNonceNonceb64(digest_request
->nonce
),
103 digest_request
->cnonce
,
104 digest_user
->HA1
, SESSIONKEY
);
105 DigestCalcResponse(SESSIONKEY
, authenticateDigestNonceNonceb64(digest_request
->nonce
),
106 digest_request
->nc
, digest_request
->cnonce
, digest_request
->qop
,
107 RequestMethodStr(request
->method
), digest_request
->uri
, HA2
, Response
);
109 debugs(29, 9, "\nResponse = '" << digest_request
->response
<< "'\nsquid is = '" << Response
<< "'");
111 if (strcasecmp(digest_request
->response
, Response
) != 0) {
112 if (!digest_request
->flags
.helper_queried
) {
113 /* Query the helper in case the password has changed */
114 digest_request
->flags
.helper_queried
= true;
115 auth_user
->credentials(Auth::Pending
);
119 if (static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->PostWorkaround
&& request
->method
!= Http::METHOD_GET
) {
120 /* Ugly workaround for certain very broken browsers using the
121 * wrong method to calculate the request-digest on POST request.
122 * This should be deleted once Digest authentication becomes more
123 * widespread and such broken browsers no longer are commonly
126 DigestCalcResponse(SESSIONKEY
, authenticateDigestNonceNonceb64(digest_request
->nonce
),
127 digest_request
->nc
, digest_request
->cnonce
, digest_request
->qop
,
128 RequestMethodStr(Http::METHOD_GET
), digest_request
->uri
, HA2
, Response
);
130 if (strcasecmp(digest_request
->response
, Response
)) {
131 auth_user
->credentials(Auth::Failed
);
132 digest_request
->flags
.invalid_password
= true;
133 digest_request
->setDenyMessage("Incorrect password");
136 const char *useragent
= request
->header
.getStr(HDR_USER_AGENT
);
138 static Ip::Address last_broken_addr
;
139 static int seen_broken_client
= 0;
141 if (!seen_broken_client
) {
142 last_broken_addr
.setNoAddr();
143 seen_broken_client
= 1;
146 if (last_broken_addr
!= request
->client_addr
) {
147 debugs(29, DBG_IMPORTANT
, "Digest POST bug detected from " <<
148 request
->client_addr
<< " using '" <<
149 (useragent
? useragent
: "-") <<
150 "'. Please upgrade browser. See Bug #630 for details.");
152 last_broken_addr
= request
->client_addr
;
156 auth_user
->credentials(Auth::Failed
);
157 digest_request
->flags
.invalid_password
= true;
158 digest_request
->setDenyMessage("Incorrect password");
163 /* check for stale nonce */
164 if (!authDigestNonceIsValid(digest_request
->nonce
, digest_request
->nc
)) {
165 debugs(29, 3, "user '" << auth_user
->username() << "' validated OK but nonce stale");
166 auth_user
->credentials(Auth::Failed
);
167 digest_request
->setDenyMessage("Stale nonce");
171 auth_user
->credentials(Auth::Ok
);
173 /* password was checked and did match */
174 debugs(29, 4, HERE
<< "user '" << auth_user
->username() << "' validated OK");
176 /* auth_user is now linked, we reset these values
177 * after external auth occurs anyway */
178 auth_user
->expiretime
= current_time
.tv_sec
;
183 Auth::Digest::UserRequest::module_direction()
185 if (user()->auth_type
!= Auth::AUTH_DIGEST
)
186 return Auth::CRED_ERROR
;
188 switch (user()->credentials()) {
191 return Auth::CRED_VALID
;
194 /* send new challenge */
195 return Auth::CRED_CHALLENGE
;
197 case Auth::Unchecked
:
199 return Auth::CRED_LOOKUP
;
202 return Auth::CRED_ERROR
;
207 Auth::Digest::UserRequest::addAuthenticationInfoHeader(HttpReply
* rep
, int accel
)
211 /* don't add to authentication error pages */
212 if ((!accel
&& rep
->sline
.status() == Http::scProxyAuthenticationRequired
)
213 || (accel
&& rep
->sline
.status() == Http::scUnauthorized
))
216 type
= accel
? HDR_AUTHENTICATION_INFO
: HDR_PROXY_AUTHENTICATION_INFO
;
219 /* test for http/1.1 transfer chunked encoding */
224 if ((static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->authenticateProgram
) && authDigestNonceLastRequest(nonce
)) {
225 flags
.authinfo_sent
= true;
226 debugs(29, 9, HERE
<< "Sending type:" << type
<< " header: 'nextnonce=\"" << authenticateDigestNonceNonceb64(nonce
) << "\"");
227 httpHeaderPutStrf(&rep
->header
, type
, "nextnonce=\"%s\"", authenticateDigestNonceNonceb64(nonce
));
233 Auth::Digest::UserRequest::addAuthenticationInfoTrailer(HttpReply
* rep
, int accel
)
237 if (!auth_user_request
)
240 /* has the header already been send? */
241 if (flags
.authinfo_sent
)
244 /* don't add to authentication error pages */
245 if ((!accel
&& rep
->sline
.status() == Http::scProxyAuthenticationRequired
)
246 || (accel
&& rep
->sline
.status() == Http::scUnauthorized
))
249 type
= accel
? HDR_AUTHENTICATION_INFO
: HDR_PROXY_AUTHENTICATION_INFO
;
251 if ((static_cast<Auth::Digest::Config
*>(digestScheme::GetInstance()->getConfig())->authenticate
) && authDigestNonceLastRequest(nonce
)) {
252 debugs(29, 9, HERE
<< "Sending type:" << type
<< " header: 'nextnonce=\"" << authenticateDigestNonceNonceb64(nonce
) << "\"");
253 httpTrailerPutStrf(&rep
->header
, type
, "nextnonce=\"%s\"", authenticateDigestNonceNonceb64(nonce
));
258 /* send the initial data to a digest authenticator module */
260 Auth::Digest::UserRequest::module_start(HttpRequest
*request
, AccessLogEntry::Pointer
&al
, AUTHCB
* handler
, void *data
)
264 assert(user() != NULL
&& user()->auth_type
== Auth::AUTH_DIGEST
);
265 debugs(29, 9, HERE
<< "'\"" << user()->username() << "\":\"" << realm
<< "\"'");
267 if (static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->authenticateProgram
== NULL
) {
268 debugs(29, DBG_CRITICAL
, "ERROR: No Digest authentication program configured.");
273 const char *keyExtras
= helperRequestKeyExtras(request
, al
);
274 if (static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->utf8
) {
276 latin1_to_utf8(userstr
, sizeof(userstr
), user()->username());
278 snprintf(buf
, 8192, "\"%s\":\"%s\" %s\n", userstr
, realm
, keyExtras
);
280 snprintf(buf
, 8192, "\"%s\":\"%s\"\n", userstr
, realm
);
283 snprintf(buf
, 8192, "\"%s\":\"%s\" %s\n", user()->username(), realm
, keyExtras
);
285 snprintf(buf
, 8192, "\"%s\":\"%s\"\n", user()->username(), realm
);
288 helperSubmit(digestauthenticators
, buf
, Auth::Digest::UserRequest::HandleReply
,
289 new Auth::StateData(this, handler
, data
));
293 Auth::Digest::UserRequest::HandleReply(void *data
, const HelperReply
&reply
)
295 Auth::StateData
*replyData
= static_cast<Auth::StateData
*>(data
);
296 debugs(29, 9, HERE
<< "reply=" << reply
);
298 assert(replyData
->auth_user_request
!= NULL
);
299 Auth::UserRequest::Pointer auth_user_request
= replyData
->auth_user_request
;
301 // add new helper kv-pair notes to the credentials object
302 // so that any transaction using those credentials can access them
303 auth_user_request
->user()->notes
.appendNewOnly(&reply
.notes
);
305 static bool oldHelperWarningDone
= false;
306 switch (reply
.result
) {
307 case HelperReply::Unknown
: {
308 // Squid 3.3 and older the digest helper only returns a HA1 hash (no "OK")
309 // the HA1 will be found in content() for these responses.
310 if (!oldHelperWarningDone
) {
311 debugs(29, DBG_IMPORTANT
, "WARNING: Digest auth helper returned old format HA1 response. It needs to be upgraded.");
312 oldHelperWarningDone
=true;
315 /* allow this because the digest_request pointer is purely local */
316 Auth::Digest::User
*digest_user
= dynamic_cast<Auth::Digest::User
*>(auth_user_request
->user().getRaw());
317 assert(digest_user
!= NULL
);
319 CvtBin(reply
.other().content(), digest_user
->HA1
);
320 digest_user
->HA1created
= 1;
324 case HelperReply::Okay
: {
325 /* allow this because the digest_request pointer is purely local */
326 Auth::Digest::User
*digest_user
= dynamic_cast<Auth::Digest::User
*>(auth_user_request
->user().getRaw());
327 assert(digest_user
!= NULL
);
329 const char *ha1Note
= reply
.notes
.findFirst("ha1");
330 if (ha1Note
!= NULL
) {
331 CvtBin(ha1Note
, digest_user
->HA1
);
332 digest_user
->HA1created
= 1;
334 debugs(29, DBG_IMPORTANT
, "ERROR: Digest auth helper did not produce a HA1. Using the wrong helper program? received: " << reply
);
339 case HelperReply::TT
:
340 debugs(29, DBG_IMPORTANT
, "ERROR: Digest auth does not support the result code received. Using the wrong helper program? received: " << reply
);
341 // fall through to next case. Handle this as an ERR response.
343 case HelperReply::BrokenHelper
:
344 // TODO retry the broken lookup on another helper?
345 // fall through to next case for now. Handle this as an ERR response silently.
347 case HelperReply::Error
: {
348 /* allow this because the digest_request pointer is purely local */
349 Auth::Digest::UserRequest
*digest_request
= dynamic_cast<Auth::Digest::UserRequest
*>(auth_user_request
.getRaw());
350 assert(digest_request
);
352 digest_request
->user()->credentials(Auth::Failed
);
353 digest_request
->flags
.invalid_password
= true;
355 const char *msgNote
= reply
.notes
.find("message");
356 if (msgNote
!= NULL
) {
357 digest_request
->setDenyMessage(msgNote
);
358 } else if (reply
.other().hasContent()) {
359 // old helpers did send ERR result but a bare message string instead of message= key name.
360 digest_request
->setDenyMessage(reply
.other().content());
361 if (!oldHelperWarningDone
) {
362 debugs(29, DBG_IMPORTANT
, "WARNING: Digest auth helper returned old format ERR response. It needs to be upgraded.");
363 oldHelperWarningDone
=true;
371 if (cbdataReferenceValidDone(replyData
->data
, &cbdata
))
372 replyData
->handler(cbdata
);