2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
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.
10 #include "AccessLogEntry.h"
11 #include "auth/digest/auth_digest.h"
12 #include "auth/digest/User.h"
13 #include "auth/digest/UserRequest.h"
14 #include "auth/State.h"
16 #include "format/Format.h"
17 #include "HttpHeaderTools.h"
18 #include "HttpReply.h"
19 #include "HttpRequest.h"
21 #include "SquidTime.h"
23 Auth::Digest::UserRequest::UserRequest() :
35 memset(nc
, 0, sizeof(nc
));
36 memset(&flags
, 0, sizeof(flags
));
40 * Delete the digest request structure.
41 * Does NOT delete related AuthUser structures
43 Auth::Digest::UserRequest::~UserRequest()
45 assert(LockCount()==0);
58 authDigestNonceUnlink(nonce
);
62 Auth::Digest::UserRequest::authenticated() const
64 if (user() != NULL
&& user()->credentials() == Auth::Ok
)
71 Auth::Digest::UserRequest::credentialsStr()
76 /** log a digest user in
79 Auth::Digest::UserRequest::authenticate(HttpRequest
* request
, ConnStateData
* conn
, http_hdr_type type
)
85 /* if the check has corrupted the user, just return */
86 if (user() == NULL
|| user()->credentials() == Auth::Failed
) {
90 Auth::User::Pointer auth_user
= user();
92 Auth::Digest::User
*digest_user
= dynamic_cast<Auth::Digest::User
*>(auth_user
.getRaw());
93 assert(digest_user
!= NULL
);
95 Auth::Digest::UserRequest
*digest_request
= this;
97 /* do we have the HA1 */
98 if (!digest_user
->HA1created
) {
99 auth_user
->credentials(Auth::Pending
);
103 if (digest_request
->nonce
== NULL
) {
104 /* this isn't a nonce we issued */
105 auth_user
->credentials(Auth::Failed
);
109 DigestCalcHA1(digest_request
->algorithm
, NULL
, NULL
, NULL
,
110 authenticateDigestNonceNonceb64(digest_request
->nonce
),
111 digest_request
->cnonce
,
112 digest_user
->HA1
, SESSIONKEY
);
113 SBuf sTmp
= request
->method
.image();
114 DigestCalcResponse(SESSIONKEY
, authenticateDigestNonceNonceb64(digest_request
->nonce
),
115 digest_request
->nc
, digest_request
->cnonce
, digest_request
->qop
,
116 sTmp
.c_str(), digest_request
->uri
, HA2
, Response
);
118 debugs(29, 9, "\nResponse = '" << digest_request
->response
<< "'\nsquid is = '" << Response
<< "'");
120 if (strcasecmp(digest_request
->response
, Response
) != 0) {
121 if (!digest_request
->flags
.helper_queried
) {
122 /* Query the helper in case the password has changed */
123 digest_request
->flags
.helper_queried
= true;
124 auth_user
->credentials(Auth::Pending
);
128 if (static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->PostWorkaround
&& request
->method
!= Http::METHOD_GET
) {
129 /* Ugly workaround for certain very broken browsers using the
130 * wrong method to calculate the request-digest on POST request.
131 * This should be deleted once Digest authentication becomes more
132 * widespread and such broken browsers no longer are commonly
135 sTmp
= HttpRequestMethod(Http::METHOD_GET
).image();
136 DigestCalcResponse(SESSIONKEY
, authenticateDigestNonceNonceb64(digest_request
->nonce
),
137 digest_request
->nc
, digest_request
->cnonce
, digest_request
->qop
,
138 sTmp
.c_str(), digest_request
->uri
, HA2
, Response
);
140 if (strcasecmp(digest_request
->response
, Response
)) {
141 auth_user
->credentials(Auth::Failed
);
142 digest_request
->flags
.invalid_password
= true;
143 digest_request
->setDenyMessage("Incorrect password");
146 const char *useragent
= request
->header
.getStr(HDR_USER_AGENT
);
148 static Ip::Address last_broken_addr
;
149 static int seen_broken_client
= 0;
151 if (!seen_broken_client
) {
152 last_broken_addr
.setNoAddr();
153 seen_broken_client
= 1;
156 if (last_broken_addr
!= request
->client_addr
) {
157 debugs(29, DBG_IMPORTANT
, "Digest POST bug detected from " <<
158 request
->client_addr
<< " using '" <<
159 (useragent
? useragent
: "-") <<
160 "'. Please upgrade browser. See Bug #630 for details.");
162 last_broken_addr
= request
->client_addr
;
166 auth_user
->credentials(Auth::Failed
);
167 digest_request
->flags
.invalid_password
= true;
168 digest_request
->setDenyMessage("Incorrect password");
173 /* check for stale nonce */
174 if (!authDigestNonceIsValid(digest_request
->nonce
, digest_request
->nc
)) {
175 debugs(29, 3, "user '" << auth_user
->username() << "' validated OK but nonce stale");
176 auth_user
->credentials(Auth::Handshake
);
177 digest_request
->setDenyMessage("Stale nonce");
181 auth_user
->credentials(Auth::Ok
);
183 /* password was checked and did match */
184 debugs(29, 4, HERE
<< "user '" << auth_user
->username() << "' validated OK");
186 /* auth_user is now linked, we reset these values
187 * after external auth occurs anyway */
188 auth_user
->expiretime
= current_time
.tv_sec
;
193 Auth::Digest::UserRequest::module_direction()
195 if (user()->auth_type
!= Auth::AUTH_DIGEST
)
196 return Auth::CRED_ERROR
;
198 switch (user()->credentials()) {
201 return Auth::CRED_VALID
;
203 case Auth::Handshake
:
205 /* send new challenge */
206 return Auth::CRED_CHALLENGE
;
208 case Auth::Unchecked
:
210 return Auth::CRED_LOOKUP
;
213 return Auth::CRED_ERROR
;
218 Auth::Digest::UserRequest::addAuthenticationInfoHeader(HttpReply
* rep
, int accel
)
222 /* don't add to authentication error pages */
223 if ((!accel
&& rep
->sline
.status() == Http::scProxyAuthenticationRequired
)
224 || (accel
&& rep
->sline
.status() == Http::scUnauthorized
))
227 type
= accel
? HDR_AUTHENTICATION_INFO
: HDR_PROXY_AUTHENTICATION_INFO
;
230 /* test for http/1.1 transfer chunked encoding */
235 if ((static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->authenticateProgram
) && authDigestNonceLastRequest(nonce
)) {
236 flags
.authinfo_sent
= true;
237 Auth::Digest::User
*digest_user
= dynamic_cast<Auth::Digest::User
*>(user().getRaw());
238 digest_nonce_h
*nextnonce
= digest_user
->currentNonce();
239 if (!nextnonce
|| authDigestNonceLastRequest(nonce
)) {
240 nextnonce
= authenticateDigestNonceNew();
241 authDigestUserLinkNonce(digest_user
, nextnonce
);
243 debugs(29, 9, "Sending type:" << type
<< " header: 'nextnonce=\"" << authenticateDigestNonceNonceb64(nextnonce
) << "\"");
244 httpHeaderPutStrf(&rep
->header
, type
, "nextnonce=\"%s\"", authenticateDigestNonceNonceb64(nextnonce
));
250 Auth::Digest::UserRequest::addAuthenticationInfoTrailer(HttpReply
* rep
, int accel
)
254 if (!auth_user_request
)
257 /* has the header already been send? */
258 if (flags
.authinfo_sent
)
261 /* don't add to authentication error pages */
262 if ((!accel
&& rep
->sline
.status() == Http::scProxyAuthenticationRequired
)
263 || (accel
&& rep
->sline
.status() == Http::scUnauthorized
))
266 type
= accel
? HDR_AUTHENTICATION_INFO
: HDR_PROXY_AUTHENTICATION_INFO
;
268 if ((static_cast<Auth::Digest::Config
*>(digestScheme::GetInstance()->getConfig())->authenticate
) && authDigestNonceLastRequest(nonce
)) {
269 Auth::Digest::User
*digest_user
= dynamic_cast<Auth::Digest::User
*>(auth_user_request
->user().getRaw());
270 nonce
= digest_user
->currentNonce();
272 nonce
= authenticateDigestNonceNew();
273 authDigestUserLinkNonce(digest_user
, nonce
);
275 debugs(29, 9, "Sending type:" << type
<< " header: 'nextnonce=\"" << authenticateDigestNonceNonceb64(nonce
) << "\"");
276 httpTrailerPutStrf(&rep
->header
, type
, "nextnonce=\"%s\"", authenticateDigestNonceNonceb64(nonce
));
281 /* send the initial data to a digest authenticator module */
283 Auth::Digest::UserRequest::startHelperLookup(HttpRequest
*request
, AccessLogEntry::Pointer
&al
, AUTHCB
* handler
, void *data
)
287 assert(user() != NULL
&& user()->auth_type
== Auth::AUTH_DIGEST
);
288 debugs(29, 9, HERE
<< "'\"" << user()->username() << "\":\"" << realm
<< "\"'");
290 if (static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->authenticateProgram
== NULL
) {
291 debugs(29, DBG_CRITICAL
, "ERROR: No Digest authentication program configured.");
296 const char *keyExtras
= helperRequestKeyExtras(request
, al
);
297 if (static_cast<Auth::Digest::Config
*>(Auth::Config::Find("digest"))->utf8
) {
299 latin1_to_utf8(userstr
, sizeof(userstr
), user()->username());
301 snprintf(buf
, 8192, "\"%s\":\"%s\" %s\n", userstr
, realm
, keyExtras
);
303 snprintf(buf
, 8192, "\"%s\":\"%s\"\n", userstr
, realm
);
306 snprintf(buf
, 8192, "\"%s\":\"%s\" %s\n", user()->username(), realm
, keyExtras
);
308 snprintf(buf
, 8192, "\"%s\":\"%s\"\n", user()->username(), realm
);
311 helperSubmit(digestauthenticators
, buf
, Auth::Digest::UserRequest::HandleReply
,
312 new Auth::StateData(this, handler
, data
));
316 Auth::Digest::UserRequest::HandleReply(void *data
, const HelperReply
&reply
)
318 Auth::StateData
*replyData
= static_cast<Auth::StateData
*>(data
);
319 debugs(29, 9, HERE
<< "reply=" << reply
);
321 assert(replyData
->auth_user_request
!= NULL
);
322 Auth::UserRequest::Pointer auth_user_request
= replyData
->auth_user_request
;
324 // add new helper kv-pair notes to the credentials object
325 // so that any transaction using those credentials can access them
326 auth_user_request
->user()->notes
.appendNewOnly(&reply
.notes
);
328 static bool oldHelperWarningDone
= false;
329 switch (reply
.result
) {
330 case HelperReply::Unknown
: {
331 // Squid 3.3 and older the digest helper only returns a HA1 hash (no "OK")
332 // the HA1 will be found in content() for these responses.
333 if (!oldHelperWarningDone
) {
334 debugs(29, DBG_IMPORTANT
, "WARNING: Digest auth helper returned old format HA1 response. It needs to be upgraded.");
335 oldHelperWarningDone
=true;
338 /* allow this because the digest_request pointer is purely local */
339 Auth::Digest::User
*digest_user
= dynamic_cast<Auth::Digest::User
*>(auth_user_request
->user().getRaw());
340 assert(digest_user
!= NULL
);
342 CvtBin(reply
.other().content(), digest_user
->HA1
);
343 digest_user
->HA1created
= 1;
347 case HelperReply::Okay
: {
348 /* allow this because the digest_request pointer is purely local */
349 Auth::Digest::User
*digest_user
= dynamic_cast<Auth::Digest::User
*>(auth_user_request
->user().getRaw());
350 assert(digest_user
!= NULL
);
352 const char *ha1Note
= reply
.notes
.findFirst("ha1");
353 if (ha1Note
!= NULL
) {
354 CvtBin(ha1Note
, digest_user
->HA1
);
355 digest_user
->HA1created
= 1;
357 debugs(29, DBG_IMPORTANT
, "ERROR: Digest auth helper did not produce a HA1. Using the wrong helper program? received: " << reply
);
362 case HelperReply::TT
:
363 debugs(29, DBG_IMPORTANT
, "ERROR: Digest auth does not support the result code received. Using the wrong helper program? received: " << reply
);
364 // fall through to next case. Handle this as an ERR response.
366 case HelperReply::BrokenHelper
:
367 // TODO retry the broken lookup on another helper?
368 // fall through to next case for now. Handle this as an ERR response silently.
370 case HelperReply::Error
: {
371 /* allow this because the digest_request pointer is purely local */
372 Auth::Digest::UserRequest
*digest_request
= dynamic_cast<Auth::Digest::UserRequest
*>(auth_user_request
.getRaw());
373 assert(digest_request
);
375 digest_request
->user()->credentials(Auth::Failed
);
376 digest_request
->flags
.invalid_password
= true;
378 const char *msgNote
= reply
.notes
.find("message");
379 if (msgNote
!= NULL
) {
380 digest_request
->setDenyMessage(msgNote
);
381 } else if (reply
.other().hasContent()) {
382 // old helpers did send ERR result but a bare message string instead of message= key name.
383 digest_request
->setDenyMessage(reply
.other().content());
384 if (!oldHelperWarningDone
) {
385 debugs(29, DBG_IMPORTANT
, "WARNING: Digest auth helper returned old format ERR response. It needs to be upgraded.");
386 oldHelperWarningDone
=true;
394 if (cbdataReferenceValidDone(replyData
->data
, &cbdata
))
395 replyData
->handler(cbdata
);