]> git.ipfire.org Git - thirdparty/squid.git/blob - src/auth/digest/auth_digest.cc
Starting to rework digest attribute parser
[thirdparty/squid.git] / src / auth / digest / auth_digest.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 29 Authenticator
5 * AUTHOR: Robert Collins
6 *
7 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
8 * ----------------------------------------------------------
9 *
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.
18 *
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.
23 *
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.
28 *
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.
32 *
33 */
34
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 */
38
39
40 #include "squid.h"
41 #include "rfc2617.h"
42 #include "auth_digest.h"
43 #include "auth/Gadgets.h"
44 #include "event.h"
45 #include "CacheManager.h"
46 #include "Store.h"
47 #include "HttpRequest.h"
48 #include "HttpReply.h"
49 #include "wordlist.h"
50 #include "SquidTime.h"
51 /* TODO don't include this */
52 #include "digestScheme.h"
53
54 /* Digest Scheme */
55
56 static HLPCB authenticateDigestHandleReply;
57 static AUTHSSTATS authenticateDigestStats;
58
59 static helper *digestauthenticators = NULL;
60
61 static hash_table *digest_nonce_cache;
62
63 static AuthDigestConfig digestConfig;
64
65 static int authdigest_initialised = 0;
66 static MemAllocator *digest_nonce_pool = NULL;
67
68 CBDATA_TYPE(DigestAuthenticateStateData);
69
70 enum {
71 DIGEST_USERNAME=1,
72 DIGEST_REALM,
73 DIGEST_QOP,
74 DIGEST_ALGORITHM,
75 DIGEST_URI,
76 DIGEST_NONCE,
77 DIGEST_NC,
78 DIGEST_CNONCE,
79 DIGEST_RESPONSE,
80 DIGEST_ENUM_END
81 };
82
83 static const HttpHeaderFieldAttrs DigestAttrs[DIGEST_ENUM_END] = {
84 {"username", (http_hdr_type)DIGEST_USERNAME},
85 {"realm", (http_hdr_type)DIGEST_REALM},
86 {"qop", (http_hdr_type)DIGEST_QOP},
87 {"algorithm", (http_hdr_type)DIGEST_ALGORITHM},
88 {"uri", (http_hdr_type)DIGEST_URI},
89 {"nonce", (http_hdr_type)DIGEST_NONCE},
90 {"nc", (http_hdr_type)DIGEST_NC},
91 {"cnonce", (http_hdr_type)DIGEST_CNONCE},
92 {"response", (http_hdr_type)DIGEST_RESPONSE},
93 };
94
95 static HttpHeaderFieldInfo *DigestFieldsInfo = NULL;
96
97 /*
98 *
99 * Nonce Functions
100 *
101 */
102
103 static void authenticateDigestNonceCacheCleanup(void *data);
104 static digest_nonce_h *authenticateDigestNonceFindNonce(const char *nonceb64);
105 static digest_nonce_h *authenticateDigestNonceNew(void);
106 static void authenticateDigestNonceDelete(digest_nonce_h * nonce);
107 static void authenticateDigestNonceSetup(void);
108 static void authenticateDigestNonceShutdown(void);
109 static void authenticateDigestNonceReconfigure(void);
110 static const char *authenticateDigestNonceNonceb64(digest_nonce_h * nonce);
111 static int authDigestNonceIsValid(digest_nonce_h * nonce, char nc[9]);
112 static int authDigestNonceIsStale(digest_nonce_h * nonce);
113 static void authDigestNonceEncode(digest_nonce_h * nonce);
114 static int authDigestNonceLastRequest(digest_nonce_h * nonce);
115 static void authDigestNonceLink(digest_nonce_h * nonce);
116 static void authDigestNonceUnlink(digest_nonce_h * nonce);
117 #if NOT_USED
118 static int authDigestNonceLinks(digest_nonce_h * nonce);
119 #endif
120 static void authDigestNonceUserUnlink(digest_nonce_h * nonce);
121 static void authDigestNoncePurge(digest_nonce_h * nonce);
122
123 static void
124 authDigestNonceEncode(digest_nonce_h * nonce)
125 {
126 if (!nonce)
127 return;
128
129 if (nonce->key)
130 xfree(nonce->key);
131
132 nonce->key = xstrdup(base64_encode_bin((char *) &(nonce->noncedata), sizeof(digest_nonce_data)));
133 }
134
135 static digest_nonce_h *
136 authenticateDigestNonceNew(void)
137 {
138 digest_nonce_h *newnonce = static_cast < digest_nonce_h * >(digest_nonce_pool->alloc());
139 digest_nonce_h *temp;
140
141 /* NONCE CREATION - NOTES AND REASONING. RBC 20010108
142 * === EXCERPT FROM RFC 2617 ===
143 * The contents of the nonce are implementation dependent. The quality
144 * of the implementation depends on a good choice. A nonce might, for
145 * example, be constructed as the base 64 encoding of
146 *
147 * time-stamp H(time-stamp ":" ETag ":" private-key)
148 *
149 * where time-stamp is a server-generated time or other non-repeating
150 * value, ETag is the value of the HTTP ETag header associated with
151 * the requested entity, and private-key is data known only to the
152 * server. With a nonce of this form a server would recalculate the
153 * hash portion after receiving the client authentication header and
154 * reject the request if it did not match the nonce from that header
155 * or if the time-stamp value is not recent enough. In this way the
156 * server can limit the time of the nonce's validity. The inclusion of
157 * the ETag prevents a replay request for an updated version of the
158 * resource. (Note: including the IP address of the client in the
159 * nonce would appear to offer the server the ability to limit the
160 * reuse of the nonce to the same client that originally got it.
161 * However, that would break proxy farms, where requests from a single
162 * user often go through different proxies in the farm. Also, IP
163 * address spoofing is not that hard.)
164 * ====
165 *
166 * Now for my reasoning:
167 * We will not accept a unrecognised nonce->we have all recognisable
168 * nonces stored. If we send out unique base64 encodings we guarantee
169 * that a given nonce applies to only one user (barring attacks or
170 * really bad timing with expiry and creation). Using a random
171 * component in the nonce allows us to loop to find a unique nonce.
172 * We use H(nonce_data) so the nonce is meaningless to the reciever.
173 * So our nonce looks like base64(H(timestamp,pointertohash,randomdata))
174 * And even if our randomness is not very random (probably due to
175 * bad coding on my part) we don't really care - the timestamp and
176 * memory pointer also guarantee local uniqueness in the input to the hash
177 * function.
178 */
179
180 /* create a new nonce */
181 newnonce->nc = 0;
182 newnonce->flags.valid = 1;
183 newnonce->noncedata.self = newnonce;
184 newnonce->noncedata.creationtime = current_time.tv_sec;
185 newnonce->noncedata.randomdata = squid_random();
186
187 authDigestNonceEncode(newnonce);
188 /*
189 * loop until we get a unique nonce. The nonce creation must
190 * have a random factor
191 */
192
193 while ((temp = authenticateDigestNonceFindNonce((char const *) (newnonce->key)))) {
194 /* create a new nonce */
195 newnonce->noncedata.randomdata = squid_random();
196 authDigestNonceEncode(newnonce);
197 }
198
199 hash_join(digest_nonce_cache, newnonce);
200 /* the cache's link */
201 authDigestNonceLink(newnonce);
202 newnonce->flags.incache = 1;
203 debugs(29, 5, "authenticateDigestNonceNew: created nonce " << newnonce << " at " << newnonce->noncedata.creationtime);
204 return newnonce;
205 }
206
207 static void
208 authenticateDigestNonceDelete(digest_nonce_h * nonce)
209 {
210 if (nonce) {
211 assert(nonce->references == 0);
212 #if UNREACHABLECODE
213
214 if (nonce->flags.incache)
215 hash_remove_link(digest_nonce_cache, nonce);
216
217 #endif
218
219 assert(nonce->flags.incache == 0);
220
221 safe_free(nonce->key);
222
223 digest_nonce_pool->free(nonce);
224 }
225 }
226
227 static void
228 authenticateDigestNonceSetup(void)
229 {
230 if (!digest_nonce_pool)
231 digest_nonce_pool = memPoolCreate("Digest Scheme nonce's", sizeof(digest_nonce_h));
232
233 if (!digest_nonce_cache) {
234 digest_nonce_cache = hash_create((HASHCMP *) strcmp, 7921, hash_string);
235 assert(digest_nonce_cache);
236 eventAdd("Digest none cache maintenance", authenticateDigestNonceCacheCleanup, NULL, digestConfig.nonceGCInterval, 1);
237 }
238 }
239
240 static void
241 authenticateDigestNonceShutdown(void)
242 {
243 /*
244 * We empty the cache of any nonces left in there.
245 */
246 digest_nonce_h *nonce;
247
248 if (digest_nonce_cache) {
249 debugs(29, 2, "authenticateDigestNonceShutdown: Shutting down nonce cache ");
250 hash_first(digest_nonce_cache);
251
252 while ((nonce = ((digest_nonce_h *) hash_next(digest_nonce_cache)))) {
253 assert(nonce->flags.incache);
254 authDigestNoncePurge(nonce);
255 }
256 }
257
258 #if DEBUGSHUTDOWN
259 if (digest_nonce_pool) {
260 delete digest_nonce_pool;
261 digest_nonce_pool = NULL;
262 }
263
264 #endif
265 debugs(29, 2, "authenticateDigestNonceShutdown: Nonce cache shutdown");
266 }
267
268 static void
269 authenticateDigestNonceReconfigure(void)
270 {}
271
272 static void
273 authenticateDigestNonceCacheCleanup(void *data)
274 {
275 /*
276 * We walk the hash by nonceb64 as that is the unique key we
277 * use. For big hash tables we could consider stepping through
278 * the cache, 100/200 entries at a time. Lets see how it flies
279 * first.
280 */
281 digest_nonce_h *nonce;
282 debugs(29, 3, "authenticateDigestNonceCacheCleanup: Cleaning the nonce cache now");
283 debugs(29, 3, "authenticateDigestNonceCacheCleanup: Current time: " << current_time.tv_sec);
284 hash_first(digest_nonce_cache);
285
286 while ((nonce = ((digest_nonce_h *) hash_next(digest_nonce_cache)))) {
287 debugs(29, 3, "authenticateDigestNonceCacheCleanup: nonce entry : " << nonce << " '" << (char *) nonce->key << "'");
288 debugs(29, 4, "authenticateDigestNonceCacheCleanup: Creation time: " << nonce->noncedata.creationtime);
289
290 if (authDigestNonceIsStale(nonce)) {
291 debugs(29, 4, "authenticateDigestNonceCacheCleanup: Removing nonce " << (char *) nonce->key << " from cache due to timeout.");
292 assert(nonce->flags.incache);
293 /* invalidate nonce so future requests fail */
294 nonce->flags.valid = 0;
295 /* if it is tied to a auth_user, remove the tie */
296 authDigestNonceUserUnlink(nonce);
297 authDigestNoncePurge(nonce);
298 }
299 }
300
301 debugs(29, 3, "authenticateDigestNonceCacheCleanup: Finished cleaning the nonce cache.");
302
303 if (digestConfig.active())
304 eventAdd("Digest none cache maintenance", authenticateDigestNonceCacheCleanup, NULL, digestConfig.nonceGCInterval, 1);
305 }
306
307 static void
308 authDigestNonceLink(digest_nonce_h * nonce)
309 {
310 assert(nonce != NULL);
311 nonce->references++;
312 debugs(29, 9, "authDigestNonceLink: nonce '" << nonce << "' now at '" << nonce->references << "'.");
313 }
314
315 #if NOT_USED
316 static int
317 authDigestNonceLinks(digest_nonce_h * nonce)
318 {
319 if (!nonce)
320 return -1;
321
322 return nonce->references;
323 }
324
325 #endif
326
327 static void
328 authDigestNonceUnlink(digest_nonce_h * nonce)
329 {
330 assert(nonce != NULL);
331
332 if (nonce->references > 0) {
333 nonce->references--;
334 } else {
335 debugs(29, 1, "authDigestNonceUnlink; Attempt to lower nonce " << nonce << " refcount below 0!");
336 }
337
338 debugs(29, 9, "authDigestNonceUnlink: nonce '" << nonce << "' now at '" << nonce->references << "'.");
339
340 if (nonce->references == 0)
341 authenticateDigestNonceDelete(nonce);
342 }
343
344 static const char *
345 authenticateDigestNonceNonceb64(digest_nonce_h * nonce)
346 {
347 if (!nonce)
348 return NULL;
349
350 return (char const *) nonce->key;
351 }
352
353 static digest_nonce_h *
354 authenticateDigestNonceFindNonce(const char *nonceb64)
355 {
356 digest_nonce_h *nonce = NULL;
357
358 if (nonceb64 == NULL)
359 return NULL;
360
361 debugs(29, 9, "authDigestNonceFindNonce:looking for nonceb64 '" << nonceb64 << "' in the nonce cache.");
362
363 nonce = static_cast < digest_nonce_h * >(hash_lookup(digest_nonce_cache, nonceb64));
364
365 if ((nonce == NULL) || (strcmp(authenticateDigestNonceNonceb64(nonce), nonceb64)))
366 return NULL;
367
368 debugs(29, 9, "authDigestNonceFindNonce: Found nonce '" << nonce << "'");
369
370 return nonce;
371 }
372
373 static int
374 authDigestNonceIsValid(digest_nonce_h * nonce, char nc[9])
375 {
376 unsigned long intnc;
377 /* do we have a nonce ? */
378
379 if (!nonce)
380 return 0;
381
382 intnc = strtol(nc, NULL, 16);
383
384 /* has it already been invalidated ? */
385 if (!nonce->flags.valid) {
386 debugs(29, 4, "authDigestNonceIsValid: Nonce already invalidated");
387 return 0;
388 }
389
390 /* is the nonce-count ok ? */
391 if (!digestConfig.CheckNonceCount) {
392 nonce->nc++;
393 return -1; /* forced OK by configuration */
394 }
395
396 if ((digestConfig.NonceStrictness && intnc != nonce->nc + 1) ||
397 intnc < nonce->nc + 1) {
398 debugs(29, 4, "authDigestNonceIsValid: Nonce count doesn't match");
399 nonce->flags.valid = 0;
400 return 0;
401 }
402
403 /* seems ok */
404 /* increment the nonce count - we've already checked that intnc is a
405 * valid representation for us, so we don't need the test here.
406 */
407 nonce->nc = intnc;
408
409 return -1;
410 }
411
412 static int
413 authDigestNonceIsStale(digest_nonce_h * nonce)
414 {
415 /* do we have a nonce ? */
416
417 if (!nonce)
418 return -1;
419
420 /* has it's max duration expired? */
421 if (nonce->noncedata.creationtime + digestConfig.noncemaxduration < current_time.tv_sec) {
422 debugs(29, 4, "authDigestNonceIsStale: Nonce is too old. " <<
423 nonce->noncedata.creationtime << " " <<
424 digestConfig.noncemaxduration << " " <<
425 current_time.tv_sec);
426
427 nonce->flags.valid = 0;
428 return -1;
429 }
430
431 if (nonce->nc > 99999998) {
432 debugs(29, 4, "authDigestNonceIsStale: Nonce count overflow");
433 nonce->flags.valid = 0;
434 return -1;
435 }
436
437 if (nonce->nc > digestConfig.noncemaxuses) {
438 debugs(29, 4, "authDigestNoncelastRequest: Nonce count over user limit");
439 nonce->flags.valid = 0;
440 return -1;
441 }
442
443 /* seems ok */
444 return 0;
445 }
446
447 /* return -1 if the digest will be stale on the next request */
448 static int
449 authDigestNonceLastRequest(digest_nonce_h * nonce)
450 {
451 if (!nonce)
452 return -1;
453
454 if (nonce->nc == 99999997) {
455 debugs(29, 4, "authDigestNoncelastRequest: Nonce count about to overflow");
456 return -1;
457 }
458
459 if (nonce->nc >= digestConfig.noncemaxuses - 1) {
460 debugs(29, 4, "authDigestNoncelastRequest: Nonce count about to hit user limit");
461 return -1;
462 }
463
464 /* and other tests are possible. */
465 return 0;
466 }
467
468 static void
469 authDigestNoncePurge(digest_nonce_h * nonce)
470 {
471 if (!nonce)
472 return;
473
474 if (!nonce->flags.incache)
475 return;
476
477 hash_remove_link(digest_nonce_cache, nonce);
478
479 nonce->flags.incache = 0;
480
481 /* the cache's link */
482 authDigestNonceUnlink(nonce);
483 }
484
485 /* USER related functions */
486 static AuthUser *
487 authDigestUserFindUsername(const char *username)
488 {
489 AuthUserHashPointer *usernamehash;
490 AuthUser *auth_user;
491 debugs(29, 9, HERE << "Looking for user '" << username << "'");
492
493 if (username && (usernamehash = static_cast < auth_user_hash_pointer * >(hash_lookup(proxy_auth_username_cache, username)))) {
494 while ((usernamehash->user()->auth_type != AUTH_DIGEST) &&
495 (usernamehash->next))
496 usernamehash = static_cast < auth_user_hash_pointer * >(usernamehash->next);
497
498 auth_user = NULL;
499
500 if (usernamehash->user()->auth_type == AUTH_DIGEST) {
501 auth_user = usernamehash->user();
502 }
503
504 return auth_user;
505 }
506
507 return NULL;
508 }
509
510 static void
511 authDigestUserShutdown(void)
512 {
513 /** \todo Future work: the auth framework could flush it's cache */
514 AuthUserHashPointer *usernamehash;
515 AuthUser *auth_user;
516 hash_first(proxy_auth_username_cache);
517
518 while ((usernamehash = ((auth_user_hash_pointer *) hash_next(proxy_auth_username_cache)))) {
519 auth_user = usernamehash->user();
520
521 if (strcmp(auth_user->config->type(), "digest") == 0)
522 auth_user->unlock();
523 }
524 }
525
526 /** delete the digest request structure. Does NOT delete related structures */
527 void
528 digestScheme::done()
529 {
530 /** \todo this should be a Config call. */
531
532 if (digestauthenticators)
533 helperShutdown(digestauthenticators);
534
535 httpHeaderDestroyFieldsInfo(DigestFieldsInfo, DIGEST_ENUM_END);
536 DigestFieldsInfo = NULL;
537
538 authdigest_initialised = 0;
539
540 if (!shutting_down) {
541 authenticateDigestNonceReconfigure();
542 return;
543 }
544
545 delete digestauthenticators;
546 digestauthenticators = NULL;
547
548 authDigestUserShutdown();
549 authenticateDigestNonceShutdown();
550 debugs(29, 2, "authenticateDigestDone: Digest authentication shut down.");
551 }
552
553 void
554 AuthDigestConfig::dump(StoreEntry * entry, const char *name, AuthConfig * scheme)
555 {
556 wordlist *list = authenticate;
557 debugs(29, 9, "authDigestCfgDump: Dumping configuration");
558 storeAppendPrintf(entry, "%s %s", name, "digest");
559
560 while (list != NULL) {
561 storeAppendPrintf(entry, " %s", list->key);
562 list = list->next;
563 }
564
565 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",
566 name, "digest", digestAuthRealm,
567 name, "digest", authenticateChildren.n_max, authenticateChildren.n_startup, authenticateChildren.n_idle, authenticateChildren.concurrency,
568 name, "digest", noncemaxuses,
569 name, "digest", (int) noncemaxduration,
570 name, "digest", (int) nonceGCInterval);
571 }
572
573 bool
574 AuthDigestConfig::active() const
575 {
576 return authdigest_initialised == 1;
577 }
578
579 bool
580 AuthDigestConfig::configured() const
581 {
582 if ((authenticate != NULL) &&
583 (authenticateChildren.n_max != 0) &&
584 (digestAuthRealm != NULL) && (noncemaxduration > -1))
585 return true;
586
587 return false;
588 }
589
590 int
591 AuthDigestUserRequest::authenticated() const
592 {
593 if (credentials() == Ok)
594 return 1;
595
596 return 0;
597 }
598
599 /** log a digest user in
600 */
601 void
602 AuthDigestUserRequest::authenticate(HttpRequest * request, ConnStateData * conn, http_hdr_type type)
603 {
604 AuthUser *auth_user;
605 AuthDigestUserRequest *digest_request;
606 digest_user_h *digest_user;
607
608 HASHHEX SESSIONKEY;
609 HASHHEX HA2 = "";
610 HASHHEX Response;
611
612 assert(authUser() != NULL);
613 auth_user = authUser();
614
615 digest_user = dynamic_cast < digest_user_h * >(auth_user);
616
617 assert(digest_user != NULL);
618
619 /* if the check has corrupted the user, just return */
620
621 if (credentials() == Failed) {
622 return;
623 }
624
625 digest_request = this;
626
627 /* do we have the HA1 */
628
629 if (!digest_user->HA1created) {
630 credentials(Pending);
631 return;
632 }
633
634 if (digest_request->nonce == NULL) {
635 /* this isn't a nonce we issued */
636 credentials(Failed);
637 return;
638 }
639
640 DigestCalcHA1(digest_request->algorithm, NULL, NULL, NULL,
641 authenticateDigestNonceNonceb64(digest_request->nonce),
642 digest_request->cnonce,
643 digest_user->HA1, SESSIONKEY);
644 DigestCalcResponse(SESSIONKEY, authenticateDigestNonceNonceb64(digest_request->nonce),
645 digest_request->nc, digest_request->cnonce, digest_request->qop,
646 RequestMethodStr(request->method), digest_request->uri, HA2, Response);
647
648 debugs(29, 9, "\nResponse = '" << digest_request->response << "'\nsquid is = '" << Response << "'");
649
650 if (strcasecmp(digest_request->response, Response) != 0) {
651 if (!digest_request->flags.helper_queried) {
652 /* Query the helper in case the password has changed */
653 digest_request->flags.helper_queried = 1;
654 digest_request->credentials_ok = Pending;
655 return;
656 }
657
658 if (digestConfig.PostWorkaround && request->method != METHOD_GET) {
659 /* Ugly workaround for certain very broken browsers using the
660 * wrong method to calculate the request-digest on POST request.
661 * This should be deleted once Digest authentication becomes more
662 * widespread and such broken browsers no longer are commonly
663 * used.
664 */
665 DigestCalcResponse(SESSIONKEY, authenticateDigestNonceNonceb64(digest_request->nonce),
666 digest_request->nc, digest_request->cnonce, digest_request->qop,
667 RequestMethodStr(METHOD_GET), digest_request->uri, HA2, Response);
668
669 if (strcasecmp(digest_request->response, Response)) {
670 credentials(Failed);
671 digest_request->setDenyMessage("Incorrect password");
672 return;
673 } else {
674 const char *useragent = request->header.getStr(HDR_USER_AGENT);
675
676 static IpAddress last_broken_addr;
677 static int seen_broken_client = 0;
678
679 if (!seen_broken_client) {
680 last_broken_addr.SetNoAddr();
681 seen_broken_client = 1;
682 }
683
684 if (last_broken_addr != request->client_addr) {
685 debugs(29, 1, "\nDigest POST bug detected from " <<
686 request->client_addr << " using '" <<
687 (useragent ? useragent : "-") <<
688 "'. Please upgrade browser. See Bug #630 for details.");
689
690 last_broken_addr = request->client_addr;
691 }
692 }
693 } else {
694 credentials(Failed);
695 digest_request->flags.invalid_password = 1;
696 digest_request->setDenyMessage("Incorrect password");
697 return;
698 }
699
700 /* check for stale nonce */
701 if (!authDigestNonceIsValid(digest_request->nonce, digest_request->nc)) {
702 debugs(29, 3, "authenticateDigestAuthenticateuser: user '" << digest_user->username() << "' validated OK but nonce stale");
703 credentials(Failed);
704 digest_request->setDenyMessage("Stale nonce");
705 return;
706 }
707 }
708
709 credentials(Ok);
710
711 /* password was checked and did match */
712 debugs(29, 4, "authenticateDigestAuthenticateuser: user '" << digest_user->username() << "' validated OK");
713
714 /* auth_user is now linked, we reset these values
715 * after external auth occurs anyway */
716 auth_user->expiretime = current_time.tv_sec;
717 return;
718 }
719
720 int
721 AuthDigestUserRequest::module_direction()
722 {
723 switch (credentials()) {
724
725 case Unchecked:
726 return -1;
727
728 case Ok:
729
730 return 0;
731
732 case Pending:
733 return -1;
734
735 case Failed:
736
737 /* send new challenge */
738 return 1;
739 }
740
741 return -2;
742 }
743
744 /* add the [proxy]authorisation header */
745 void
746 AuthDigestUserRequest::addHeader(HttpReply * rep, int accel)
747 {
748 http_hdr_type type;
749
750 /* don't add to authentication error pages */
751
752 if ((!accel && rep->sline.status == HTTP_PROXY_AUTHENTICATION_REQUIRED)
753 || (accel && rep->sline.status == HTTP_UNAUTHORIZED))
754 return;
755
756 type = accel ? HDR_AUTHENTICATION_INFO : HDR_PROXY_AUTHENTICATION_INFO;
757
758 #if WAITING_FOR_TE
759 /* test for http/1.1 transfer chunked encoding */
760 if (chunkedtest)
761 return;
762
763 #endif
764
765 if ((digestConfig.authenticate) && authDigestNonceLastRequest(nonce)) {
766 flags.authinfo_sent = 1;
767 debugs(29, 9, "authDigestAddHead: Sending type:" << type << " header: 'nextnonce=\"" << authenticateDigestNonceNonceb64(nonce) << "\"");
768 httpHeaderPutStrf(&rep->header, type, "nextnonce=\"%s\"", authenticateDigestNonceNonceb64(nonce));
769 }
770 }
771
772 #if WAITING_FOR_TE
773 /* add the [proxy]authorisation header */
774 void
775 AuthDigestUserRequest::addTrailer(HttpReply * rep, int accel)
776 {
777 int type;
778
779 if (!auth_user_request)
780 return;
781
782
783 /* has the header already been send? */
784 if (flags.authinfo_sent)
785 return;
786
787 /* don't add to authentication error pages */
788 if ((!accel && rep->sline.status == HTTP_PROXY_AUTHENTICATION_REQUIRED)
789 || (accel && rep->sline.status == HTTP_UNAUTHORIZED))
790 return;
791
792 type = accel ? HDR_AUTHENTICATION_INFO : HDR_PROXY_AUTHENTICATION_INFO;
793
794 if ((digestConfig.authenticate) && authDigestNonceLastRequest(nonce)) {
795 debugs(29, 9, "authDigestAddTrailer: Sending type:" << type << " header: 'nextnonce=\"" << authenticateDigestNonceNonceb64(nonce) << "\"");
796 httpTrailerPutStrf(&rep->header, type, "nextnonce=\"%s\"", authenticateDigestNonceNonceb64(nonce));
797 }
798 }
799
800 #endif
801
802 /* add the [www-|Proxy-]authenticate header on a 407 or 401 reply */
803 void
804 AuthDigestConfig::fixHeader(AuthUserRequest *auth_user_request, HttpReply *rep, http_hdr_type hdrType, HttpRequest * request)
805 {
806 if (!authenticate)
807 return;
808
809 int stale = 0;
810
811 if (auth_user_request) {
812 AuthDigestUserRequest *digest_request;
813 digest_request = dynamic_cast < AuthDigestUserRequest * >(auth_user_request);
814 assert (digest_request != NULL);
815
816 stale = !digest_request->flags.invalid_password;
817 }
818
819 /* on a 407 or 401 we always use a new nonce */
820 digest_nonce_h *nonce = authenticateDigestNonceNew();
821
822 debugs(29, 9, "authenticateFixHeader: Sending type:" << hdrType <<
823 " header: 'Digest realm=\"" << digestAuthRealm << "\", nonce=\"" <<
824 authenticateDigestNonceNonceb64(nonce) << "\", qop=\"" << QOP_AUTH <<
825 "\", stale=" << (stale ? "true" : "false"));
826
827 /* in the future, for WWW auth we may want to support the domain entry */
828 httpHeaderPutStrf(&rep->header, hdrType, "Digest realm=\"%s\", nonce=\"%s\", qop=\"%s\", stale=%s", digestAuthRealm, authenticateDigestNonceNonceb64(nonce), QOP_AUTH, stale ? "true" : "false");
829 }
830
831 DigestUser::~DigestUser()
832 {
833
834 dlink_node *link, *tmplink;
835 link = nonces.head;
836
837 while (link) {
838 tmplink = link;
839 link = link->next;
840 dlinkDelete(tmplink, &nonces);
841 authDigestNoncePurge(static_cast < digest_nonce_h * >(tmplink->data));
842 authDigestNonceUnlink(static_cast < digest_nonce_h * >(tmplink->data));
843 dlinkNodeDelete(tmplink);
844 }
845 }
846
847 static void
848 authenticateDigestHandleReply(void *data, char *reply)
849 {
850 DigestAuthenticateStateData *replyData = static_cast < DigestAuthenticateStateData * >(data);
851 AuthUserRequest *auth_user_request;
852 AuthDigestUserRequest *digest_request;
853 digest_user_h *digest_user;
854 char *t = NULL;
855 void *cbdata;
856 debugs(29, 9, "authenticateDigestHandleReply: {" << (reply ? reply : "<NULL>") << "}");
857
858 if (reply) {
859 if ((t = strchr(reply, ' ')))
860 *t++ = '\0';
861
862 if (*reply == '\0' || *reply == '\n')
863 reply = NULL;
864 }
865
866 assert(replyData->auth_user_request != NULL);
867 auth_user_request = replyData->auth_user_request;
868 digest_request = dynamic_cast < AuthDigestUserRequest * >(auth_user_request);
869 assert(digest_request);
870
871 digest_user = dynamic_cast < digest_user_h * >(auth_user_request->user());
872 assert(digest_user != NULL);
873
874 if (reply && (strncasecmp(reply, "ERR", 3) == 0)) {
875 digest_request->credentials(AuthDigestUserRequest::Failed);
876 digest_request->flags.invalid_password = 1;
877
878 if (t && *t)
879 digest_request->setDenyMessage(t);
880 } else if (reply) {
881 CvtBin(reply, digest_user->HA1);
882 digest_user->HA1created = 1;
883 }
884
885 if (cbdataReferenceValidDone(replyData->data, &cbdata))
886 replyData->handler(cbdata, NULL);
887
888 //we know replyData->auth_user_request != NULL, or we'd have asserted
889 AUTHUSERREQUESTUNLOCK(replyData->auth_user_request, "replyData");
890
891 cbdataFree(replyData);
892 }
893
894 /* Initialize helpers and the like for this auth scheme. Called AFTER parsing the
895 * config file */
896 void
897 AuthDigestConfig::init(AuthConfig * scheme)
898 {
899 if (authenticate) {
900 DigestFieldsInfo = httpHeaderBuildFieldsInfo(DigestAttrs, DIGEST_ENUM_END);
901 authenticateDigestNonceSetup();
902 authdigest_initialised = 1;
903
904 if (digestauthenticators == NULL)
905 digestauthenticators = new helper("digestauthenticator");
906
907 digestauthenticators->cmdline = authenticate;
908
909 digestauthenticators->childs = authenticateChildren;
910
911 digestauthenticators->ipc_type = IPC_STREAM;
912
913 helperOpenServers(digestauthenticators);
914
915 CBDATA_INIT_TYPE(DigestAuthenticateStateData);
916 }
917 }
918
919 void
920 AuthDigestConfig::registerWithCacheManager(void)
921 {
922 CacheManager::GetInstance()->
923 registerAction("digestauthenticator",
924 "Digest User Authenticator Stats",
925 authenticateDigestStats, 0, 1);
926 }
927
928 /* free any allocated configuration details */
929 void
930 AuthDigestConfig::done()
931 {
932 if (authenticate)
933 wordlistDestroy(&authenticate);
934
935 safe_free(digestAuthRealm);
936 }
937
938 AuthDigestConfig::AuthDigestConfig() : authenticateChildren(20)
939 {
940 /* TODO: move into initialisation list */
941 /* 5 minutes */
942 nonceGCInterval = 5 * 60;
943 /* 30 minutes */
944 noncemaxduration = 30 * 60;
945 /* 50 requests */
946 noncemaxuses = 50;
947 /* Not strict nonce count behaviour */
948 NonceStrictness = 0;
949 /* Verify nonce count */
950 CheckNonceCount = 1;
951 }
952
953 void
954 AuthDigestConfig::parse(AuthConfig * scheme, int n_configured, char *param_str)
955 {
956 if (strcasecmp(param_str, "program") == 0) {
957 if (authenticate)
958 wordlistDestroy(&authenticate);
959
960 parse_wordlist(&authenticate);
961
962 requirePathnameExists("auth_param digest program", authenticate->key);
963 } else if (strcasecmp(param_str, "children") == 0) {
964 authenticateChildren.parseConfig();
965 } else if (strcasecmp(param_str, "realm") == 0) {
966 parse_eol(&digestAuthRealm);
967 } else if (strcasecmp(param_str, "nonce_garbage_interval") == 0) {
968 parse_time_t(&nonceGCInterval);
969 } else if (strcasecmp(param_str, "nonce_max_duration") == 0) {
970 parse_time_t(&noncemaxduration);
971 } else if (strcasecmp(param_str, "nonce_max_count") == 0) {
972 parse_int((int *) &noncemaxuses);
973 } else if (strcasecmp(param_str, "nonce_strictness") == 0) {
974 parse_onoff(&NonceStrictness);
975 } else if (strcasecmp(param_str, "check_nonce_count") == 0) {
976 parse_onoff(&CheckNonceCount);
977 } else if (strcasecmp(param_str, "post_workaround") == 0) {
978 parse_onoff(&PostWorkaround);
979 } else if (strcasecmp(param_str, "utf8") == 0) {
980 parse_onoff(&utf8);
981 } else {
982 debugs(29, 0, "unrecognised digest auth scheme parameter '" << param_str << "'");
983 }
984 }
985
986 const char *
987 AuthDigestConfig::type() const
988 {
989 return digestScheme::GetInstance().type();
990 }
991
992
993 static void
994 authenticateDigestStats(StoreEntry * sentry)
995 {
996 helperStats(sentry, digestauthenticators, "Digest Authenticator Statistics");
997 }
998
999 /* NonceUserUnlink: remove the reference to auth_user and unlink the node from the list */
1000
1001 static void
1002 authDigestNonceUserUnlink(digest_nonce_h * nonce)
1003 {
1004 digest_user_h *digest_user;
1005 dlink_node *link, *tmplink;
1006
1007 if (!nonce)
1008 return;
1009
1010 if (!nonce->user)
1011 return;
1012
1013 digest_user = nonce->user;
1014
1015 /* unlink from the user list. Yes we're crossing structures but this is the only
1016 * time this code is needed
1017 */
1018 link = digest_user->nonces.head;
1019
1020 while (link) {
1021 tmplink = link;
1022 link = link->next;
1023
1024 if (tmplink->data == nonce) {
1025 dlinkDelete(tmplink, &digest_user->nonces);
1026 authDigestNonceUnlink(static_cast < digest_nonce_h * >(tmplink->data));
1027 dlinkNodeDelete(tmplink);
1028 link = NULL;
1029 }
1030 }
1031
1032 /* this reference to user was not locked because freeeing the user frees
1033 * the nonce too.
1034 */
1035 nonce->user = NULL;
1036 }
1037
1038 /* authDigestUserLinkNonce: add a nonce to a given user's struct */
1039
1040 static void
1041 authDigestUserLinkNonce(DigestUser * user, digest_nonce_h * nonce)
1042 {
1043 dlink_node *node;
1044 digest_user_h *digest_user;
1045
1046 if (!user || !nonce)
1047 return;
1048
1049 digest_user = user;
1050
1051 node = digest_user->nonces.head;
1052
1053 while (node && (node->data != nonce))
1054 node = node->next;
1055
1056 if (node)
1057 return;
1058
1059 node = dlinkNodeNew();
1060
1061 dlinkAddTail(nonce, node, &digest_user->nonces);
1062
1063 authDigestNonceLink(nonce);
1064
1065 /* ping this nonce to this auth user */
1066 assert((nonce->user == NULL) || (nonce->user == user));
1067
1068 /* we don't lock this reference because removing the user removes the
1069 * hash too. Of course if that changes we're stuffed so read the code huh?
1070 */
1071 nonce->user = user;
1072 }
1073
1074 /* setup the necessary info to log the username */
1075 static AuthUserRequest *
1076 authDigestLogUsername(char *username, AuthDigestUserRequest *auth_user_request)
1077 {
1078 assert(auth_user_request != NULL);
1079
1080 /* log the username */
1081 debugs(29, 9, "authDigestLogUsername: Creating new user for logging '" << username << "'");
1082 digest_user_h *digest_user = new DigestUser(&digestConfig);
1083 /* save the credentials */
1084 digest_user->username(username);
1085 /* set the auth_user type */
1086 digest_user->auth_type = AUTH_BROKEN;
1087 /* link the request to the user */
1088 auth_user_request->authUser(digest_user);
1089 auth_user_request->user(digest_user);
1090 digest_user->addRequest (auth_user_request);
1091 return auth_user_request;
1092 }
1093
1094 /*
1095 * Decode a Digest [Proxy-]Auth string, placing the results in the passed
1096 * Auth_user structure.
1097 */
1098 AuthUserRequest *
1099 AuthDigestConfig::decode(char const *proxy_auth)
1100 {
1101 const char *item;
1102 const char *p;
1103 const char *pos = NULL;
1104 char *username = NULL;
1105 digest_nonce_h *nonce;
1106 int ilen;
1107
1108 debugs(29, 9, "authenticateDigestDecodeAuth: beginning");
1109
1110 AuthDigestUserRequest *digest_request = new AuthDigestUserRequest();
1111
1112 /* trim DIGEST from string */
1113
1114 while (xisgraph(*proxy_auth))
1115 proxy_auth++;
1116
1117 /* Trim leading whitespace before decoding */
1118 while (xisspace(*proxy_auth))
1119 proxy_auth++;
1120
1121 String temp(proxy_auth);
1122
1123 while (strListGetItem(&temp, ',', &item, &ilen, &pos)) {
1124 if ((p = strchr(item, '=')) && (p - item < ilen))
1125 ilen = p++ - item;
1126
1127 if (!strncmp(item, "username", ilen)) {
1128 /* white space */
1129
1130 while (xisspace(*p))
1131 p++;
1132
1133 /* quote mark */
1134 p++;
1135
1136 safe_free(username);
1137 username = xstrndup(p, strchr(p, '"') + 1 - p);
1138
1139 debugs(29, 9, "authDigestDecodeAuth: Found Username '" << username << "'");
1140 } else if (!strncmp(item, "realm", ilen)) {
1141 /* white space */
1142
1143 while (xisspace(*p))
1144 p++;
1145
1146 /* quote mark */
1147 p++;
1148
1149 safe_free(digest_request->realm);
1150 digest_request->realm = xstrndup(p, strchr(p, '"') + 1 - p);
1151
1152 debugs(29, 9, "authDigestDecodeAuth: Found realm '" << digest_request->realm << "'");
1153 } else if (!strncmp(item, "qop", ilen)) {
1154 /* white space */
1155
1156 while (xisspace(*p))
1157 p++;
1158
1159 if (*p == '\"')
1160 /* quote mark */
1161 p++;
1162
1163 safe_free(digest_request->qop);
1164 digest_request->qop = xstrndup(p, strcspn(p, "\" \t\r\n()<>@,;:\\/[]?={}") + 1);
1165
1166 debugs(29, 9, "authDigestDecodeAuth: Found qop '" << digest_request->qop << "'");
1167 } else if (!strncmp(item, "algorithm", ilen)) {
1168 /* white space */
1169
1170 while (xisspace(*p))
1171 p++;
1172
1173 if (*p == '\"')
1174 /* quote mark */
1175 p++;
1176
1177 safe_free(digest_request->algorithm);
1178 digest_request->algorithm = xstrndup(p, strcspn(p, "\" \t\r\n()<>@,;:\\/[]?={}") + 1);
1179
1180 debugs(29, 9, "authDigestDecodeAuth: Found algorithm '" << digest_request->algorithm << "'");
1181 } else if (!strncmp(item, "uri", ilen)) {
1182 /* white space */
1183
1184 while (xisspace(*p))
1185 p++;
1186
1187 /* quote mark */
1188 p++;
1189
1190 safe_free(digest_request->uri);
1191 digest_request->uri = xstrndup(p, strchr(p, '"') + 1 - p);
1192
1193 debugs(29, 9, "authDigestDecodeAuth: Found uri '" << digest_request->uri << "'");
1194 } else if (!strncmp(item, "nonce", ilen)) {
1195 /* white space */
1196
1197 while (xisspace(*p))
1198 p++;
1199
1200 /* quote mark */
1201 p++;
1202
1203 safe_free(digest_request->nonceb64);
1204 digest_request->nonceb64 = xstrndup(p, strchr(p, '"') + 1 - p);
1205
1206 debugs(29, 9, "authDigestDecodeAuth: Found nonce '" << digest_request->nonceb64 << "'");
1207 } else if (!strncmp(item, "nc", ilen)) {
1208 /* white space */
1209
1210 while (xisspace(*p))
1211 p++;
1212
1213 xstrncpy(digest_request->nc, p, 9);
1214
1215 debugs(29, 9, "authDigestDecodeAuth: Found noncecount '" << digest_request->nc << "'");
1216 } else if (!strncmp(item, "cnonce", ilen)) {
1217 /* white space */
1218
1219 while (xisspace(*p))
1220 p++;
1221
1222 /* quote mark */
1223 p++;
1224
1225 safe_free(digest_request->cnonce);
1226 digest_request->cnonce = xstrndup(p, strchr(p, '"') + 1 - p);
1227
1228 debugs(29, 9, "authDigestDecodeAuth: Found cnonce '" << digest_request->cnonce << "'");
1229 } else if (!strncmp(item, "response", ilen)) {
1230 /* white space */
1231
1232 while (xisspace(*p))
1233 p++;
1234
1235 /* quote mark */
1236 p++;
1237
1238 safe_free(digest_request->response);
1239 digest_request->response = xstrndup(p, strchr(p, '"') + 1 - p);
1240
1241 debugs(29, 9, "authDigestDecodeAuth: Found response '" << digest_request->response << "'");
1242 }
1243 }
1244
1245 temp.clean();
1246
1247
1248 /* now we validate the data given to us */
1249
1250 /*
1251 * TODO: on invalid parameters we should return 400, not 407.
1252 * Find some clean way of doing this. perhaps return a valid
1253 * struct, and set the direction to clientwards combined with
1254 * a change to the clientwards handling code (ie let the
1255 * clientwards call set the error type (but limited to known
1256 * correct values - 400/401/407
1257 */
1258
1259 /* first the NONCE count */
1260
1261 if (digest_request->cnonce && strlen(digest_request->nc) != 8) {
1262 debugs(29, 4, "authenticateDigestDecode: nonce count length invalid");
1263 return authDigestLogUsername(username, digest_request);
1264 }
1265
1266 /* now the nonce */
1267 nonce = authenticateDigestNonceFindNonce(digest_request->nonceb64);
1268
1269 if (!nonce) {
1270 /* we couldn't find a matching nonce! */
1271 debugs(29, 4, "authenticateDigestDecode: Unexpected or invalid nonce received");
1272 return authDigestLogUsername(username, digest_request);
1273 }
1274
1275 digest_request->nonce = nonce;
1276 authDigestNonceLink(nonce);
1277
1278 /* check the qop is what we expected. Note that for compatability with
1279 * RFC 2069 we should support a missing qop. Tough. */
1280
1281 if (digest_request->qop && strcmp(digest_request->qop, QOP_AUTH) != 0) {
1282 /* we received a qop option we didn't send */
1283 debugs(29, 4, "authenticateDigestDecode: Invalid qop option received");
1284 return authDigestLogUsername(username, digest_request);
1285 }
1286
1287 /* we can't check the URI just yet. We'll check it in the
1288 * authenticate phase, but needs to be given */
1289 if (!digest_request->uri) {
1290 debugs(29, 4, "authenticateDigestDecode: Missing URI field");
1291 return authDigestLogUsername(username, digest_request);
1292 }
1293
1294 /* is the response the correct length? */
1295
1296 if (!digest_request->response || strlen(digest_request->response) != 32) {
1297 debugs(29, 4, "authenticateDigestDecode: Response length invalid");
1298 return authDigestLogUsername(username, digest_request);
1299 }
1300
1301 /* do we have a username ? */
1302 if (!username || username[0] == '\0') {
1303 debugs(29, 4, "authenticateDigestDecode: Empty or not present username");
1304 return authDigestLogUsername(username, digest_request);
1305 }
1306
1307 /* check that we're not being hacked / the username hasn't changed */
1308 if (nonce->user && strcmp(username, nonce->user->username())) {
1309 debugs(29, 4, "authenticateDigestDecode: Username for the nonce does not equal the username for the request");
1310 return authDigestLogUsername(username, digest_request);
1311 }
1312
1313 /* if we got a qop, did we get a cnonce or did we get a cnonce wihtout a qop? */
1314 if ((digest_request->qop && !digest_request->cnonce)
1315 || (!digest_request->qop && digest_request->cnonce)) {
1316 debugs(29, 4, "authenticateDigestDecode: qop without cnonce, or vice versa!");
1317 return authDigestLogUsername(username, digest_request);
1318 }
1319
1320 /* check the algorithm is present and supported */
1321 if (!digest_request->algorithm)
1322 digest_request->algorithm = xstrndup("MD5", 4);
1323 else if (strcmp(digest_request->algorithm, "MD5")
1324 && strcmp(digest_request->algorithm, "MD5-sess")) {
1325 debugs(29, 4, "authenticateDigestDecode: invalid algorithm specified!");
1326 return authDigestLogUsername(username, digest_request);
1327 }
1328
1329 /* the method we'll check at the authenticate step as well */
1330
1331
1332 /* we don't send or parse opaques. Ok so we're flexable ... */
1333
1334 /* find the user */
1335 digest_user_h *digest_user;
1336
1337 AuthUser *auth_user;
1338
1339 if ((auth_user = authDigestUserFindUsername(username)) == NULL) {
1340 /* the user doesn't exist in the username cache yet */
1341 debugs(29, 9, "authDigestDecodeAuth: Creating new digest user '" << username << "'");
1342 digest_user = new DigestUser (&digestConfig);
1343 /* auth_user is a parent */
1344 auth_user = digest_user;
1345 /* save the username */
1346 digest_user->username(username);
1347 /* set the user type */
1348 digest_user->auth_type = AUTH_DIGEST;
1349 /* this auth_user struct is the one to get added to the
1350 * username cache */
1351 /* store user in hash's */
1352 digest_user->addToNameCache();
1353
1354 /*
1355 * Add the digest to the user so we can tell if a hacking
1356 * or spoofing attack is taking place. We do this by assuming
1357 * the user agent won't change user name without warning.
1358 */
1359 authDigestUserLinkNonce(digest_user, nonce);
1360 } else {
1361 debugs(29, 9, "authDigestDecodeAuth: Found user '" << username << "' in the user cache as '" << auth_user << "'");
1362 digest_user = static_cast < digest_user_h * >(auth_user);
1363 xfree(username);
1364 }
1365
1366 /*link the request and the user */
1367 assert(digest_request != NULL);
1368
1369 digest_request->authUser (digest_user);
1370
1371 digest_request->user(digest_user);
1372
1373 digest_user->addRequest (digest_request);
1374
1375 debugs(29, 9, "username = '" << digest_user->username() << "'\nrealm = '" <<
1376 digest_request->realm << "'\nqop = '" << digest_request->qop <<
1377 "'\nalgorithm = '" << digest_request->algorithm << "'\nuri = '" <<
1378 digest_request->uri << "'\nnonce = '" << digest_request->nonceb64 <<
1379 "'\nnc = '" << digest_request->nc << "'\ncnonce = '" <<
1380 digest_request->cnonce << "'\nresponse = '" <<
1381 digest_request->response << "'\ndigestnonce = '" << nonce << "'");
1382
1383 return digest_request;
1384 }
1385
1386 /* send the initial data to a digest authenticator module */
1387 void
1388 AuthDigestUserRequest::module_start(RH * handler, void *data)
1389 {
1390 DigestAuthenticateStateData *r = NULL;
1391 char buf[8192];
1392 digest_user_h *digest_user;
1393 assert(user()->auth_type == AUTH_DIGEST);
1394 digest_user = dynamic_cast < digest_user_h * >(user());
1395 assert(digest_user != NULL);
1396 debugs(29, 9, "authenticateStart: '\"" << digest_user->username() << "\":\"" << realm << "\"'");
1397
1398 if (digestConfig.authenticate == NULL) {
1399 handler(data, NULL);
1400 return;
1401 }
1402
1403 r = cbdataAlloc(DigestAuthenticateStateData);
1404 r->handler = handler;
1405 r->data = cbdataReference(data);
1406 r->auth_user_request = this;
1407 AUTHUSERREQUESTLOCK(r->auth_user_request, "r");
1408 if (digestConfig.utf8) {
1409 char userstr[1024];
1410 latin1_to_utf8(userstr, sizeof(userstr), digest_user->username());
1411 snprintf(buf, 8192, "\"%s\":\"%s\"\n", userstr, realm);
1412 } else {
1413 snprintf(buf, 8192, "\"%s\":\"%s\"\n", digest_user->username(), realm);
1414 }
1415
1416 helperSubmit(digestauthenticators, buf, authenticateDigestHandleReply, r);
1417 }
1418
1419 DigestUser::DigestUser (AuthConfig *aConfig) : AuthUser (aConfig), HA1created (0)
1420 {}
1421
1422 AuthUser *
1423 AuthDigestUserRequest::authUser() const
1424 {
1425 return const_cast<AuthUser *>(user());
1426 }
1427
1428 void
1429 AuthDigestUserRequest::authUser(AuthUser *aUser)
1430 {
1431 assert(!authUser());
1432 user(aUser);
1433 user()->lock();
1434 }
1435
1436 AuthDigestUserRequest::CredentialsState
1437 AuthDigestUserRequest::credentials() const
1438 {
1439 return credentials_ok;
1440 }
1441
1442 void
1443 AuthDigestUserRequest::credentials(CredentialsState newCreds)
1444 {
1445 credentials_ok = newCreds;
1446 }
1447
1448 AuthDigestUserRequest::AuthDigestUserRequest() : nonceb64(NULL) ,cnonce(NULL) ,realm(NULL),
1449 pszPass(NULL) ,algorithm(NULL) ,pszMethod(NULL),
1450 qop(NULL) ,uri(NULL) ,response(NULL),
1451 nonce(NULL), _theUser (NULL) ,
1452 credentials_ok (Unchecked)
1453 {}
1454
1455 /** delete the digest request structure. Does NOT delete related structures */
1456 AuthDigestUserRequest::~AuthDigestUserRequest()
1457 {
1458 safe_free (nonceb64);
1459 safe_free (cnonce);
1460 safe_free (realm);
1461 safe_free (pszPass);
1462 safe_free (algorithm);
1463 safe_free (pszMethod);
1464 safe_free (qop);
1465 safe_free (uri);
1466 safe_free (response);
1467
1468 if (nonce)
1469 authDigestNonceUnlink(nonce);
1470 }
1471
1472 AuthConfig *
1473 digestScheme::createConfig()
1474 {
1475 return &digestConfig;
1476 }
1477