4 * DEBUG: section 29 Authenticator
5 * AUTHOR: Robert Collins
7 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
8 * ----------------------------------------------------------
10 * Squid is the result of efforts by numerous individuals from the
11 * Internet community. Development is led by Duane Wessels of the
12 * National Laboratory for Applied Network Research and funded by the
13 * National Science Foundation. Squid is Copyrighted (C) 1998 by
14 * the Regents of the University of California. Please see the
15 * COPYRIGHT file for full details. Squid incorporates software
16 * developed and/or copyrighted by other sources. Please see the
17 * CREDITS file for full details.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
35 /* The functions in this file handle authentication.
36 * They DO NOT perform access control or auditing.
37 * See acl.c for access control and client_side.c for auditing */
42 #include "auth/digest/auth_digest.h"
43 #include "auth/Gadgets.h"
45 #include "mgr/Registration.h"
47 #include "HttpRequest.h"
48 #include "HttpReply.h"
50 #include "SquidTime.h"
51 /* TODO don't include this */
52 #include "auth/digest/digestScheme.h"
53 #include "auth/digest/digestUserRequest.h"
57 static AUTHSSTATS authenticateDigestStats
;
59 helper
*digestauthenticators
= NULL
;
61 static hash_table
*digest_nonce_cache
;
63 static int authdigest_initialised
= 0;
64 static MemAllocator
*digest_nonce_pool
= NULL
;
66 // CBDATA_TYPE(DigestAuthenticateStateData);
68 enum http_digest_attr_type
{
81 static const HttpHeaderFieldAttrs DigestAttrs
[DIGEST_ENUM_END
] = {
82 {"username", (http_hdr_type
)DIGEST_USERNAME
},
83 {"realm", (http_hdr_type
)DIGEST_REALM
},
84 {"qop", (http_hdr_type
)DIGEST_QOP
},
85 {"algorithm", (http_hdr_type
)DIGEST_ALGORITHM
},
86 {"uri", (http_hdr_type
)DIGEST_URI
},
87 {"nonce", (http_hdr_type
)DIGEST_NONCE
},
88 {"nc", (http_hdr_type
)DIGEST_NC
},
89 {"cnonce", (http_hdr_type
)DIGEST_CNONCE
},
90 {"response", (http_hdr_type
)DIGEST_RESPONSE
},
93 static HttpHeaderFieldInfo
*DigestFieldsInfo
= NULL
;
101 static void authenticateDigestNonceCacheCleanup(void *data
);
102 static digest_nonce_h
*authenticateDigestNonceFindNonce(const char *nonceb64
);
103 static digest_nonce_h
*authenticateDigestNonceNew(void);
104 static void authenticateDigestNonceDelete(digest_nonce_h
* nonce
);
105 static void authenticateDigestNonceSetup(void);
106 static void authenticateDigestNonceShutdown(void);
107 static void authenticateDigestNonceReconfigure(void);
108 static int authDigestNonceIsStale(digest_nonce_h
* nonce
);
109 static void authDigestNonceEncode(digest_nonce_h
* nonce
);
110 static void authDigestNonceLink(digest_nonce_h
* nonce
);
112 static int authDigestNonceLinks(digest_nonce_h
* nonce
);
114 static void authDigestNonceUserUnlink(digest_nonce_h
* nonce
);
115 static void authDigestNoncePurge(digest_nonce_h
* nonce
);
118 authDigestNonceEncode(digest_nonce_h
* nonce
)
126 nonce
->key
= xstrdup(base64_encode_bin((char *) &(nonce
->noncedata
), sizeof(digest_nonce_data
)));
129 static digest_nonce_h
*
130 authenticateDigestNonceNew(void)
132 digest_nonce_h
*newnonce
= static_cast < digest_nonce_h
* >(digest_nonce_pool
->alloc());
133 digest_nonce_h
*temp
;
135 /* NONCE CREATION - NOTES AND REASONING. RBC 20010108
136 * === EXCERPT FROM RFC 2617 ===
137 * The contents of the nonce are implementation dependent. The quality
138 * of the implementation depends on a good choice. A nonce might, for
139 * example, be constructed as the base 64 encoding of
141 * time-stamp H(time-stamp ":" ETag ":" private-key)
143 * where time-stamp is a server-generated time or other non-repeating
144 * value, ETag is the value of the HTTP ETag header associated with
145 * the requested entity, and private-key is data known only to the
146 * server. With a nonce of this form a server would recalculate the
147 * hash portion after receiving the client authentication header and
148 * reject the request if it did not match the nonce from that header
149 * or if the time-stamp value is not recent enough. In this way the
150 * server can limit the time of the nonce's validity. The inclusion of
151 * the ETag prevents a replay request for an updated version of the
152 * resource. (Note: including the IP address of the client in the
153 * nonce would appear to offer the server the ability to limit the
154 * reuse of the nonce to the same client that originally got it.
155 * However, that would break proxy farms, where requests from a single
156 * user often go through different proxies in the farm. Also, IP
157 * address spoofing is not that hard.)
160 * Now for my reasoning:
161 * We will not accept a unrecognised nonce->we have all recognisable
162 * nonces stored. If we send out unique base64 encodings we guarantee
163 * that a given nonce applies to only one user (barring attacks or
164 * really bad timing with expiry and creation). Using a random
165 * component in the nonce allows us to loop to find a unique nonce.
166 * We use H(nonce_data) so the nonce is meaningless to the reciever.
167 * So our nonce looks like base64(H(timestamp,pointertohash,randomdata))
168 * And even if our randomness is not very random (probably due to
169 * bad coding on my part) we don't really care - the timestamp and
170 * memory pointer also guarantee local uniqueness in the input to the hash
174 /* create a new nonce */
176 newnonce
->flags
.valid
= 1;
177 newnonce
->noncedata
.self
= newnonce
;
178 newnonce
->noncedata
.creationtime
= current_time
.tv_sec
;
179 newnonce
->noncedata
.randomdata
= squid_random();
181 authDigestNonceEncode(newnonce
);
183 * loop until we get a unique nonce. The nonce creation must
184 * have a random factor
187 while ((temp
= authenticateDigestNonceFindNonce((char const *) (newnonce
->key
)))) {
188 /* create a new nonce */
189 newnonce
->noncedata
.randomdata
= squid_random();
190 authDigestNonceEncode(newnonce
);
193 hash_join(digest_nonce_cache
, newnonce
);
194 /* the cache's link */
195 authDigestNonceLink(newnonce
);
196 newnonce
->flags
.incache
= 1;
197 debugs(29, 5, "authenticateDigestNonceNew: created nonce " << newnonce
<< " at " << newnonce
->noncedata
.creationtime
);
202 authenticateDigestNonceDelete(digest_nonce_h
* nonce
)
205 assert(nonce
->references
== 0);
208 if (nonce
->flags
.incache
)
209 hash_remove_link(digest_nonce_cache
, nonce
);
213 assert(nonce
->flags
.incache
== 0);
215 safe_free(nonce
->key
);
217 digest_nonce_pool
->freeOne(nonce
);
222 authenticateDigestNonceSetup(void)
224 if (!digest_nonce_pool
)
225 digest_nonce_pool
= memPoolCreate("Digest Scheme nonce's", sizeof(digest_nonce_h
));
227 if (!digest_nonce_cache
) {
228 digest_nonce_cache
= hash_create((HASHCMP
*) strcmp
, 7921, hash_string
);
229 assert(digest_nonce_cache
);
230 eventAdd("Digest none cache maintenance", authenticateDigestNonceCacheCleanup
, NULL
, static_cast<AuthDigestConfig
*>(AuthConfig::Find("digest"))->nonceGCInterval
, 1);
235 authenticateDigestNonceShutdown(void)
238 * We empty the cache of any nonces left in there.
240 digest_nonce_h
*nonce
;
242 if (digest_nonce_cache
) {
243 debugs(29, 2, "authenticateDigestNonceShutdown: Shutting down nonce cache ");
244 hash_first(digest_nonce_cache
);
246 while ((nonce
= ((digest_nonce_h
*) hash_next(digest_nonce_cache
)))) {
247 assert(nonce
->flags
.incache
);
248 authDigestNoncePurge(nonce
);
253 if (digest_nonce_pool
) {
254 delete digest_nonce_pool
;
255 digest_nonce_pool
= NULL
;
259 debugs(29, 2, "authenticateDigestNonceShutdown: Nonce cache shutdown");
263 authenticateDigestNonceReconfigure(void)
267 authenticateDigestNonceCacheCleanup(void *data
)
270 * We walk the hash by nonceb64 as that is the unique key we
271 * use. For big hash tables we could consider stepping through
272 * the cache, 100/200 entries at a time. Lets see how it flies
275 digest_nonce_h
*nonce
;
276 debugs(29, 3, "authenticateDigestNonceCacheCleanup: Cleaning the nonce cache now");
277 debugs(29, 3, "authenticateDigestNonceCacheCleanup: Current time: " << current_time
.tv_sec
);
278 hash_first(digest_nonce_cache
);
280 while ((nonce
= ((digest_nonce_h
*) hash_next(digest_nonce_cache
)))) {
281 debugs(29, 3, "authenticateDigestNonceCacheCleanup: nonce entry : " << nonce
<< " '" << (char *) nonce
->key
<< "'");
282 debugs(29, 4, "authenticateDigestNonceCacheCleanup: Creation time: " << nonce
->noncedata
.creationtime
);
284 if (authDigestNonceIsStale(nonce
)) {
285 debugs(29, 4, "authenticateDigestNonceCacheCleanup: Removing nonce " << (char *) nonce
->key
<< " from cache due to timeout.");
286 assert(nonce
->flags
.incache
);
287 /* invalidate nonce so future requests fail */
288 nonce
->flags
.valid
= 0;
289 /* if it is tied to a auth_user, remove the tie */
290 authDigestNonceUserUnlink(nonce
);
291 authDigestNoncePurge(nonce
);
295 debugs(29, 3, "authenticateDigestNonceCacheCleanup: Finished cleaning the nonce cache.");
297 if (static_cast<AuthDigestConfig
*>(AuthConfig::Find("digest"))->active())
298 eventAdd("Digest none cache maintenance", authenticateDigestNonceCacheCleanup
, NULL
, static_cast<AuthDigestConfig
*>(AuthConfig::Find("digest"))->nonceGCInterval
, 1);
302 authDigestNonceLink(digest_nonce_h
* nonce
)
304 assert(nonce
!= NULL
);
306 debugs(29, 9, "authDigestNonceLink: nonce '" << nonce
<< "' now at '" << nonce
->references
<< "'.");
311 authDigestNonceLinks(digest_nonce_h
* nonce
)
316 return nonce
->references
;
322 authDigestNonceUnlink(digest_nonce_h
* nonce
)
324 assert(nonce
!= NULL
);
326 if (nonce
->references
> 0) {
329 debugs(29, 1, "authDigestNonceUnlink; Attempt to lower nonce " << nonce
<< " refcount below 0!");
332 debugs(29, 9, "authDigestNonceUnlink: nonce '" << nonce
<< "' now at '" << nonce
->references
<< "'.");
334 if (nonce
->references
== 0)
335 authenticateDigestNonceDelete(nonce
);
339 authenticateDigestNonceNonceb64(const digest_nonce_h
* nonce
)
344 return (char const *) nonce
->key
;
347 static digest_nonce_h
*
348 authenticateDigestNonceFindNonce(const char *nonceb64
)
350 digest_nonce_h
*nonce
= NULL
;
352 if (nonceb64
== NULL
)
355 debugs(29, 9, "authDigestNonceFindNonce:looking for nonceb64 '" << nonceb64
<< "' in the nonce cache.");
357 nonce
= static_cast < digest_nonce_h
* >(hash_lookup(digest_nonce_cache
, nonceb64
));
359 if ((nonce
== NULL
) || (strcmp(authenticateDigestNonceNonceb64(nonce
), nonceb64
)))
362 debugs(29, 9, "authDigestNonceFindNonce: Found nonce '" << nonce
<< "'");
368 authDigestNonceIsValid(digest_nonce_h
* nonce
, char nc
[9])
371 /* do we have a nonce ? */
376 intnc
= strtol(nc
, NULL
, 16);
378 /* has it already been invalidated ? */
379 if (!nonce
->flags
.valid
) {
380 debugs(29, 4, "authDigestNonceIsValid: Nonce already invalidated");
384 /* is the nonce-count ok ? */
385 if (!static_cast<AuthDigestConfig
*>(AuthConfig::Find("digest"))->CheckNonceCount
) {
387 return -1; /* forced OK by configuration */
390 if ((static_cast<AuthDigestConfig
*>(AuthConfig::Find("digest"))->NonceStrictness
&& intnc
!= nonce
->nc
+ 1) ||
391 intnc
< nonce
->nc
+ 1) {
392 debugs(29, 4, "authDigestNonceIsValid: Nonce count doesn't match");
393 nonce
->flags
.valid
= 0;
398 /* increment the nonce count - we've already checked that intnc is a
399 * valid representation for us, so we don't need the test here.
407 authDigestNonceIsStale(digest_nonce_h
* nonce
)
409 /* do we have a nonce ? */
414 /* has it's max duration expired? */
415 if (nonce
->noncedata
.creationtime
+ static_cast<AuthDigestConfig
*>(AuthConfig::Find("digest"))->noncemaxduration
< current_time
.tv_sec
) {
416 debugs(29, 4, "authDigestNonceIsStale: Nonce is too old. " <<
417 nonce
->noncedata
.creationtime
<< " " <<
418 static_cast<AuthDigestConfig
*>(AuthConfig::Find("digest"))->noncemaxduration
<< " " <<
419 current_time
.tv_sec
);
421 nonce
->flags
.valid
= 0;
425 if (nonce
->nc
> 99999998) {
426 debugs(29, 4, "authDigestNonceIsStale: Nonce count overflow");
427 nonce
->flags
.valid
= 0;
431 if (nonce
->nc
> static_cast<AuthDigestConfig
*>(AuthConfig::Find("digest"))->noncemaxuses
) {
432 debugs(29, 4, "authDigestNoncelastRequest: Nonce count over user limit");
433 nonce
->flags
.valid
= 0;
442 * \retval 0 the digest is not stale yet
443 * \retval -1 the digest will be stale on the next request
446 authDigestNonceLastRequest(digest_nonce_h
* nonce
)
451 if (nonce
->nc
== 99999997) {
452 debugs(29, 4, "authDigestNoncelastRequest: Nonce count about to overflow");
456 if (nonce
->nc
>= static_cast<AuthDigestConfig
*>(AuthConfig::Find("digest"))->noncemaxuses
- 1) {
457 debugs(29, 4, "authDigestNoncelastRequest: Nonce count about to hit user limit");
461 /* and other tests are possible. */
466 authDigestNoncePurge(digest_nonce_h
* nonce
)
471 if (!nonce
->flags
.incache
)
474 hash_remove_link(digest_nonce_cache
, nonce
);
476 nonce
->flags
.incache
= 0;
478 /* the cache's link */
479 authDigestNonceUnlink(nonce
);
482 /* USER related functions */
483 static AuthUser::Pointer
484 authDigestUserFindUsername(const char *username
)
486 AuthUserHashPointer
*usernamehash
;
487 debugs(29, 9, HERE
<< "Looking for user '" << username
<< "'");
489 if (username
&& (usernamehash
= static_cast < auth_user_hash_pointer
* >(hash_lookup(proxy_auth_username_cache
, username
)))) {
490 while ((usernamehash
->user()->auth_type
!= AUTH_DIGEST
) && (usernamehash
->next
))
491 usernamehash
= static_cast<AuthUserHashPointer
*>(usernamehash
->next
);
493 if (usernamehash
->user()->auth_type
== AUTH_DIGEST
) {
494 return usernamehash
->user();
502 AuthDigestConfig::rotateHelpers()
504 /* schedule closure of existing helpers */
505 if (digestauthenticators
) {
506 helperShutdown(digestauthenticators
);
509 /* NP: dynamic helper restart will ensure they start up again as needed. */
512 /** delete the digest request structure. Does NOT delete related structures */
516 /** \todo this should be a Config call. */
518 if (digestauthenticators
)
519 helperShutdown(digestauthenticators
);
521 if (DigestFieldsInfo
) {
522 httpHeaderDestroyFieldsInfo(DigestFieldsInfo
, DIGEST_ENUM_END
);
523 DigestFieldsInfo
= NULL
;
526 authdigest_initialised
= 0;
528 if (!shutting_down
) {
529 authenticateDigestNonceReconfigure();
533 delete digestauthenticators
;
534 digestauthenticators
= NULL
;
536 PurgeCredentialsCache();
537 authenticateDigestNonceShutdown();
538 debugs(29, 2, "authenticateDigestDone: Digest authentication shut down.");
540 /* clear the global handle to this scheme. */
545 AuthDigestConfig::dump(StoreEntry
* entry
, const char *name
, AuthConfig
* scheme
)
547 wordlist
*list
= authenticate
;
548 debugs(29, 9, "authDigestCfgDump: Dumping configuration");
549 storeAppendPrintf(entry
, "%s %s", name
, "digest");
551 while (list
!= NULL
) {
552 storeAppendPrintf(entry
, " %s", list
->key
);
556 storeAppendPrintf(entry
, "\n%s %s realm %s\n%s %s children %d startup=%d idle=%d concurrency=%d\n%s %s nonce_max_count %d\n%s %s nonce_max_duration %d seconds\n%s %s nonce_garbage_interval %d seconds\n",
557 name
, "digest", digestAuthRealm
,
558 name
, "digest", authenticateChildren
.n_max
, authenticateChildren
.n_startup
, authenticateChildren
.n_idle
, authenticateChildren
.concurrency
,
559 name
, "digest", noncemaxuses
,
560 name
, "digest", (int) noncemaxduration
,
561 name
, "digest", (int) nonceGCInterval
);
565 AuthDigestConfig::active() const
567 return authdigest_initialised
== 1;
571 AuthDigestConfig::configured() const
573 if ((authenticate
!= NULL
) &&
574 (authenticateChildren
.n_max
!= 0) &&
575 (digestAuthRealm
!= NULL
) && (noncemaxduration
> -1))
581 /* add the [www-|Proxy-]authenticate header on a 407 or 401 reply */
583 AuthDigestConfig::fixHeader(AuthUserRequest::Pointer auth_user_request
, HttpReply
*rep
, http_hdr_type hdrType
, HttpRequest
* request
)
590 if (auth_user_request
!= NULL
) {
591 AuthDigestUserRequest
*digest_request
;
592 digest_request
= dynamic_cast<AuthDigestUserRequest
*>(auth_user_request
.getRaw());
593 assert (digest_request
!= NULL
);
595 stale
= !digest_request
->flags
.invalid_password
;
598 /* on a 407 or 401 we always use a new nonce */
599 digest_nonce_h
*nonce
= authenticateDigestNonceNew();
601 debugs(29, 9, "authenticateFixHeader: Sending type:" << hdrType
<<
602 " header: 'Digest realm=\"" << digestAuthRealm
<< "\", nonce=\"" <<
603 authenticateDigestNonceNonceb64(nonce
) << "\", qop=\"" << QOP_AUTH
<<
604 "\", stale=" << (stale
? "true" : "false"));
606 /* in the future, for WWW auth we may want to support the domain entry */
607 httpHeaderPutStrf(&rep
->header
, hdrType
, "Digest realm=\"%s\", nonce=\"%s\", qop=\"%s\", stale=%s", digestAuthRealm
, authenticateDigestNonceNonceb64(nonce
), QOP_AUTH
, stale
? "true" : "false");
610 DigestUser::~DigestUser()
612 dlink_node
*link
, *tmplink
;
618 dlinkDelete(tmplink
, &nonces
);
619 authDigestNoncePurge(static_cast < digest_nonce_h
* >(tmplink
->data
));
620 authDigestNonceUnlink(static_cast < digest_nonce_h
* >(tmplink
->data
));
621 dlinkNodeDelete(tmplink
);
626 DigestUser::ttl() const
628 int32_t global_ttl
= static_cast<int32_t>(expiretime
- squid_curtime
+ Config
.authenticateTTL
);
630 /* find the longest lasting nonce. */
631 int32_t latest_nonce
= -1;
632 dlink_node
*link
= nonces
.head
;
634 digest_nonce_h
*nonce
= static_cast<digest_nonce_h
*>(link
->data
);
635 if (nonce
->flags
.valid
&& nonce
->noncedata
.creationtime
> latest_nonce
)
636 latest_nonce
= nonce
->noncedata
.creationtime
;
640 if (latest_nonce
== -1)
641 return min(-1, global_ttl
);
643 int32_t nonce_ttl
= latest_nonce
- current_time
.tv_sec
+ static_cast<AuthDigestConfig
*>(AuthConfig::Find("digest"))->noncemaxduration
;
645 return min(nonce_ttl
, global_ttl
);
648 /* Initialize helpers and the like for this auth scheme. Called AFTER parsing the
651 AuthDigestConfig::init(AuthConfig
* scheme
)
654 DigestFieldsInfo
= httpHeaderBuildFieldsInfo(DigestAttrs
, DIGEST_ENUM_END
);
655 authenticateDigestNonceSetup();
656 authdigest_initialised
= 1;
658 if (digestauthenticators
== NULL
)
659 digestauthenticators
= new helper("digestauthenticator");
661 digestauthenticators
->cmdline
= authenticate
;
663 digestauthenticators
->childs
= authenticateChildren
;
665 digestauthenticators
->ipc_type
= IPC_STREAM
;
667 helperOpenServers(digestauthenticators
);
669 CBDATA_INIT_TYPE(authenticateStateData
);
674 AuthDigestConfig::registerWithCacheManager(void)
676 Mgr::RegisterAction("digestauthenticator",
677 "Digest User Authenticator Stats",
678 authenticateDigestStats
, 0, 1);
681 /* free any allocated configuration details */
683 AuthDigestConfig::done()
686 wordlistDestroy(&authenticate
);
688 safe_free(digestAuthRealm
);
691 AuthDigestConfig::AuthDigestConfig()
693 /* TODO: move into initialisation list */
695 nonceGCInterval
= 5 * 60;
697 noncemaxduration
= 30 * 60;
700 /* Not strict nonce count behaviour */
702 /* Verify nonce count */
707 AuthDigestConfig::parse(AuthConfig
* scheme
, int n_configured
, char *param_str
)
709 if (strcasecmp(param_str
, "program") == 0) {
711 wordlistDestroy(&authenticate
);
713 parse_wordlist(&authenticate
);
715 requirePathnameExists("auth_param digest program", authenticate
->key
);
716 } else if (strcasecmp(param_str
, "children") == 0) {
717 authenticateChildren
.parseConfig();
718 } else if (strcasecmp(param_str
, "realm") == 0) {
719 parse_eol(&digestAuthRealm
);
720 } else if (strcasecmp(param_str
, "nonce_garbage_interval") == 0) {
721 parse_time_t(&nonceGCInterval
);
722 } else if (strcasecmp(param_str
, "nonce_max_duration") == 0) {
723 parse_time_t(&noncemaxduration
);
724 } else if (strcasecmp(param_str
, "nonce_max_count") == 0) {
725 parse_int((int *) &noncemaxuses
);
726 } else if (strcasecmp(param_str
, "nonce_strictness") == 0) {
727 parse_onoff(&NonceStrictness
);
728 } else if (strcasecmp(param_str
, "check_nonce_count") == 0) {
729 parse_onoff(&CheckNonceCount
);
730 } else if (strcasecmp(param_str
, "post_workaround") == 0) {
731 parse_onoff(&PostWorkaround
);
732 } else if (strcasecmp(param_str
, "utf8") == 0) {
735 debugs(29, 0, "unrecognised digest auth scheme parameter '" << param_str
<< "'");
740 AuthDigestConfig::type() const
742 return digestScheme::GetInstance()->type();
747 authenticateDigestStats(StoreEntry
* sentry
)
749 helperStats(sentry
, digestauthenticators
, "Digest Authenticator Statistics");
752 /* NonceUserUnlink: remove the reference to auth_user and unlink the node from the list */
755 authDigestNonceUserUnlink(digest_nonce_h
* nonce
)
757 DigestUser
*digest_user
;
758 dlink_node
*link
, *tmplink
;
766 digest_user
= nonce
->user
;
768 /* unlink from the user list. Yes we're crossing structures but this is the only
769 * time this code is needed
771 link
= digest_user
->nonces
.head
;
777 if (tmplink
->data
== nonce
) {
778 dlinkDelete(tmplink
, &digest_user
->nonces
);
779 authDigestNonceUnlink(static_cast < digest_nonce_h
* >(tmplink
->data
));
780 dlinkNodeDelete(tmplink
);
785 /* this reference to user was not locked because freeeing the user frees
791 /* authDigestUserLinkNonce: add a nonce to a given user's struct */
794 authDigestUserLinkNonce(DigestUser
* user
, digest_nonce_h
* nonce
)
797 DigestUser
*digest_user
;
804 node
= digest_user
->nonces
.head
;
806 while (node
&& (node
->data
!= nonce
))
812 node
= dlinkNodeNew();
814 dlinkAddTail(nonce
, node
, &digest_user
->nonces
);
816 authDigestNonceLink(nonce
);
818 /* ping this nonce to this auth user */
819 assert((nonce
->user
== NULL
) || (nonce
->user
== user
));
821 /* we don't lock this reference because removing the user removes the
822 * hash too. Of course if that changes we're stuffed so read the code huh?
827 /* setup the necessary info to log the username */
828 static AuthUserRequest::Pointer
829 authDigestLogUsername(char *username
, AuthUserRequest::Pointer auth_user_request
)
831 assert(auth_user_request
!= NULL
);
833 /* log the username */
834 debugs(29, 9, "authDigestLogUsername: Creating new user for logging '" << username
<< "'");
835 AuthUser::Pointer digest_user
= new DigestUser(static_cast<AuthDigestConfig
*>(AuthConfig::Find("digest")));
836 /* save the credentials */
837 digest_user
->username(username
);
838 /* set the auth_user type */
839 digest_user
->auth_type
= AUTH_BROKEN
;
840 /* link the request to the user */
841 auth_user_request
->user(digest_user
);
842 return auth_user_request
;
846 * Decode a Digest [Proxy-]Auth string, placing the results in the passed
847 * Auth_user structure.
849 AuthUserRequest::Pointer
850 AuthDigestConfig::decode(char const *proxy_auth
)
854 const char *pos
= NULL
;
855 char *username
= NULL
;
856 digest_nonce_h
*nonce
;
859 debugs(29, 9, "authenticateDigestDecodeAuth: beginning");
861 AuthDigestUserRequest
*digest_request
= new AuthDigestUserRequest();
863 /* trim DIGEST from string */
865 while (xisgraph(*proxy_auth
))
868 /* Trim leading whitespace before decoding */
869 while (xisspace(*proxy_auth
))
872 String
temp(proxy_auth
);
874 while (strListGetItem(&temp
, ',', &item
, &ilen
, &pos
)) {
875 /* isolate directive name & value */
878 if ((p
= (const char *)memchr(item
, '=', ilen
)) && (p
- item
< ilen
)) {
880 vlen
= ilen
- (p
- item
);
886 /* parse value. auth-param = token "=" ( token | quoted-string ) */
890 if (!httpHeaderParseQuotedString(p
, vlen
, &value
)) {
891 debugs(29, 9, "authDigestDecodeAuth: Failed to parse attribute '" << item
<< "' in '" << temp
<< "'");
895 value
.limitInit(p
, vlen
);
898 debugs(29, 9, "authDigestDecodeAuth: Failed to parse attribute '" << item
<< "' in '" << temp
<< "'");
903 http_digest_attr_type type
= (http_digest_attr_type
)httpHeaderIdByName(item
, nlen
, DigestFieldsInfo
, DIGEST_ENUM_END
);
906 case DIGEST_USERNAME
:
908 username
= xstrndup(value
.rawBuf(), value
.size() + 1);
909 debugs(29, 9, "authDigestDecodeAuth: Found Username '" << username
<< "'");
913 safe_free(digest_request
->realm
);
914 digest_request
->realm
= xstrndup(value
.rawBuf(), value
.size() + 1);
915 debugs(29, 9, "authDigestDecodeAuth: Found realm '" << digest_request
->realm
<< "'");
919 safe_free(digest_request
->qop
);
920 digest_request
->qop
= xstrndup(value
.rawBuf(), value
.size() + 1);
921 debugs(29, 9, "authDigestDecodeAuth: Found qop '" << digest_request
->qop
<< "'");
924 case DIGEST_ALGORITHM
:
925 safe_free(digest_request
->algorithm
);
926 digest_request
->algorithm
= xstrndup(value
.rawBuf(), value
.size() + 1);
927 debugs(29, 9, "authDigestDecodeAuth: Found algorithm '" << digest_request
->algorithm
<< "'");
931 safe_free(digest_request
->uri
);
932 digest_request
->uri
= xstrndup(value
.rawBuf(), value
.size() + 1);
933 debugs(29, 9, "authDigestDecodeAuth: Found uri '" << digest_request
->uri
<< "'");
937 safe_free(digest_request
->nonceb64
);
938 digest_request
->nonceb64
= xstrndup(value
.rawBuf(), value
.size() + 1);
939 debugs(29, 9, "authDigestDecodeAuth: Found nonce '" << digest_request
->nonceb64
<< "'");
943 if (value
.size() != 8) {
944 debugs(29, 9, "authDigestDecodeAuth: Invalid nc '" << value
<< "' in '" << temp
<< "'");
946 xstrncpy(digest_request
->nc
, value
.rawBuf(), value
.size() + 1);
947 debugs(29, 9, "authDigestDecodeAuth: Found noncecount '" << digest_request
->nc
<< "'");
951 safe_free(digest_request
->cnonce
);
952 digest_request
->cnonce
= xstrndup(value
.rawBuf(), value
.size() + 1);
953 debugs(29, 9, "authDigestDecodeAuth: Found cnonce '" << digest_request
->cnonce
<< "'");
956 case DIGEST_RESPONSE
:
957 safe_free(digest_request
->response
);
958 digest_request
->response
= xstrndup(value
.rawBuf(), value
.size() + 1);
959 debugs(29, 9, "authDigestDecodeAuth: Found response '" << digest_request
->response
<< "'");
963 debugs(29, 3, "authDigestDecodeAuth: Unknown attribute '" << item
<< "' in '" << temp
<< "'");
971 /* now we validate the data given to us */
974 * TODO: on invalid parameters we should return 400, not 407.
975 * Find some clean way of doing this. perhaps return a valid
976 * struct, and set the direction to clientwards combined with
977 * a change to the clientwards handling code (ie let the
978 * clientwards call set the error type (but limited to known
979 * correct values - 400/401/407
982 /* 2069 requirements */
984 /* do we have a username ? */
985 if (!username
|| username
[0] == '\0') {
986 debugs(29, 2, "authenticateDigestDecode: Empty or not present username");
987 return authDigestLogUsername(username
, digest_request
);
990 /* Sanity check of the username.
991 * " can not be allowed in usernames until * the digest helper protocol
994 if (strchr(username
, '"')) {
995 debugs(29, 2, "authenticateDigestDecode: Unacceptable username '" << username
<< "'");
996 return authDigestLogUsername(username
, digest_request
);
999 /* do we have a realm ? */
1000 if (!digest_request
->realm
|| digest_request
->realm
[0] == '\0') {
1001 debugs(29, 2, "authenticateDigestDecode: Empty or not present realm");
1002 return authDigestLogUsername(username
, digest_request
);
1006 if (!digest_request
->nonceb64
|| digest_request
->nonceb64
[0] == '\0') {
1007 debugs(29, 2, "authenticateDigestDecode: Empty or not present nonce");
1008 return authDigestLogUsername(username
, digest_request
);
1011 /* we can't check the URI just yet. We'll check it in the
1012 * authenticate phase, but needs to be given */
1013 if (!digest_request
->uri
|| digest_request
->uri
[0] == '\0') {
1014 debugs(29, 2, "authenticateDigestDecode: Missing URI field");
1015 return authDigestLogUsername(username
, digest_request
);
1018 /* is the response the correct length? */
1019 if (!digest_request
->response
|| strlen(digest_request
->response
) != 32) {
1020 debugs(29, 2, "authenticateDigestDecode: Response length invalid");
1021 return authDigestLogUsername(username
, digest_request
);
1024 /* check the algorithm is present and supported */
1025 if (!digest_request
->algorithm
)
1026 digest_request
->algorithm
= xstrndup("MD5", 4);
1027 else if (strcmp(digest_request
->algorithm
, "MD5")
1028 && strcmp(digest_request
->algorithm
, "MD5-sess")) {
1029 debugs(29, 2, "authenticateDigestDecode: invalid algorithm specified!");
1030 return authDigestLogUsername(username
, digest_request
);
1033 /* 2617 requirements, indicated by qop */
1034 if (digest_request
->qop
) {
1036 /* check the qop is what we expected. */
1037 if (strcmp(digest_request
->qop
, QOP_AUTH
) != 0) {
1038 /* we received a qop option we didn't send */
1039 debugs(29, 2, "authenticateDigestDecode: Invalid qop option received");
1040 return authDigestLogUsername(username
, digest_request
);
1044 if (!digest_request
->cnonce
|| digest_request
->cnonce
[0] == '\0') {
1045 debugs(29, 2, "authenticateDigestDecode: Missing cnonce field");
1046 return authDigestLogUsername(username
, digest_request
);
1050 if (strlen(digest_request
->nc
) != 8 || strspn(digest_request
->nc
, "0123456789abcdefABCDEF") != 8) {
1051 debugs(29, 2, "authenticateDigestDecode: invalid nonce count");
1052 return authDigestLogUsername(username
, digest_request
);
1055 /* cnonce and nc both require qop */
1056 if (digest_request
->cnonce
|| digest_request
->nc
) {
1057 debugs(29, 2, "authenticateDigestDecode: missing qop!");
1058 return authDigestLogUsername(username
, digest_request
);
1062 /** below nonce state dependent **/
1065 nonce
= authenticateDigestNonceFindNonce(digest_request
->nonceb64
);
1067 /* we couldn't find a matching nonce! */
1068 debugs(29, 2, "authenticateDigestDecode: Unexpected or invalid nonce received");
1069 digest_request
->user()->credentials(AuthUser::Failed
);
1070 return authDigestLogUsername(username
, digest_request
);
1073 digest_request
->nonce
= nonce
;
1074 authDigestNonceLink(nonce
);
1076 /* check that we're not being hacked / the username hasn't changed */
1077 if (nonce
->user
&& strcmp(username
, nonce
->user
->username())) {
1078 debugs(29, 2, "authenticateDigestDecode: Username for the nonce does not equal the username for the request");
1079 return authDigestLogUsername(username
, digest_request
);
1082 /* the method we'll check at the authenticate step as well */
1085 /* we don't send or parse opaques. Ok so we're flexable ... */
1088 DigestUser
*digest_user
;
1090 AuthUser::Pointer auth_user
;
1092 if ((auth_user
= authDigestUserFindUsername(username
)) == NULL
) {
1093 /* the user doesn't exist in the username cache yet */
1094 debugs(29, 9, "authDigestDecodeAuth: Creating new digest user '" << username
<< "'");
1095 digest_user
= new DigestUser(this);
1096 /* auth_user is a parent */
1097 auth_user
= digest_user
;
1098 /* save the username */
1099 digest_user
->username(username
);
1100 /* set the user type */
1101 digest_user
->auth_type
= AUTH_DIGEST
;
1102 /* this auth_user struct is the one to get added to the
1104 /* store user in hash's */
1105 digest_user
->addToNameCache();
1108 * Add the digest to the user so we can tell if a hacking
1109 * or spoofing attack is taking place. We do this by assuming
1110 * the user agent won't change user name without warning.
1112 authDigestUserLinkNonce(digest_user
, nonce
);
1114 debugs(29, 9, "authDigestDecodeAuth: Found user '" << username
<< "' in the user cache as '" << auth_user
<< "'");
1115 digest_user
= static_cast<DigestUser
*>(auth_user
.getRaw());
1119 /*link the request and the user */
1120 assert(digest_request
!= NULL
);
1122 digest_request
->user(digest_user
);
1123 debugs(29, 9, "username = '" << digest_user
->username() << "'\nrealm = '" <<
1124 digest_request
->realm
<< "'\nqop = '" << digest_request
->qop
<<
1125 "'\nalgorithm = '" << digest_request
->algorithm
<< "'\nuri = '" <<
1126 digest_request
->uri
<< "'\nnonce = '" << digest_request
->nonceb64
<<
1127 "'\nnc = '" << digest_request
->nc
<< "'\ncnonce = '" <<
1128 digest_request
->cnonce
<< "'\nresponse = '" <<
1129 digest_request
->response
<< "'\ndigestnonce = '" << nonce
<< "'");
1131 return digest_request
;
1134 DigestUser::DigestUser(AuthConfig
*aConfig
) : AuthUser(aConfig
), HA1created (0)