]>
Commit | Line | Data |
---|---|---|
94439e4e | 1 | /* |
486bf0fb | 2 | * $Id: auth_basic.cc,v 1.53 2008/02/12 23:17:50 rousskov Exp $ |
94439e4e | 3 | * |
4 | * DEBUG: section 29 Authenticator | |
5 | * AUTHOR: Duane Wessels | |
6 | * | |
2b6662ba | 7 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
94439e4e | 8 | * ---------------------------------------------------------- |
9 | * | |
2b6662ba | 10 | * Squid is the result of efforts by numerous individuals from |
11 | * the Internet community; see the CONTRIBUTORS file for full | |
12 | * details. Many organizations have provided support for Squid's | |
13 | * development; see the SPONSORS file for full details. Squid is | |
14 | * Copyrighted (C) 2001 by the Regents of the University of | |
15 | * California; see the COPYRIGHT file for full details. Squid | |
16 | * incorporates software developed and/or copyrighted by other | |
17 | * sources; see the CREDITS file for full details. | |
94439e4e | 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 "auth_basic.h" | |
e6ccf245 | 42 | #include "authenticate.h" |
62ee09ca | 43 | #include "CacheManager.h" |
e6ccf245 | 44 | #include "Store.h" |
924f73bc | 45 | #include "HttpReply.h" |
f5691f9c | 46 | #include "basicScheme.h" |
d295d770 | 47 | #include "wordlist.h" |
985c86bc | 48 | #include "SquidTime.h" |
94439e4e | 49 | |
50 | static void | |
e6ccf245 | 51 | authenticateStateFree(AuthenticateStateData * r) |
94439e4e | 52 | { |
53 | cbdataFree(r); | |
54 | } | |
55 | ||
56 | /* Basic Scheme */ | |
57 | ||
58 | static HLPCB authenticateBasicHandleReply; | |
94439e4e | 59 | static AUTHSSTATS authenticateBasicStats; |
94439e4e | 60 | |
61 | static helper *basicauthenticators = NULL; | |
62 | ||
f5691f9c | 63 | static AuthBasicConfig basicConfig; |
94439e4e | 64 | |
65 | static int authbasic_initialised = 0; | |
94439e4e | 66 | |
2d72d4fd | 67 | |
94439e4e | 68 | /* |
69 | * | |
2d72d4fd | 70 | * Public Functions |
94439e4e | 71 | * |
72 | */ | |
73 | ||
2d72d4fd | 74 | /* internal functions */ |
75 | ||
f5691f9c | 76 | /* TODO: move to basicScheme.cc - after all per request and user functions are moved out */ |
77 | void | |
78 | basicScheme::done() | |
2d72d4fd | 79 | { |
f5691f9c | 80 | /* TODO: this should be a Config call. */ |
81 | ||
2d72d4fd | 82 | if (basicauthenticators) |
62e76326 | 83 | helperShutdown(basicauthenticators); |
84 | ||
2d72d4fd | 85 | authbasic_initialised = 0; |
62e76326 | 86 | |
2d72d4fd | 87 | if (!shutting_down) |
62e76326 | 88 | return; |
89 | ||
2d72d4fd | 90 | if (basicauthenticators) |
62e76326 | 91 | helperFree(basicauthenticators); |
92 | ||
2d72d4fd | 93 | basicauthenticators = NULL; |
62e76326 | 94 | |
f5691f9c | 95 | /* XXX Reinstate auth shutdown for dynamic schemes? */ |
bf8fe701 | 96 | debugs(29, 2, "authBasicDone: Basic authentication Shutdown."); |
2d72d4fd | 97 | } |
98 | ||
f5691f9c | 99 | bool |
100 | AuthBasicConfig::active() const | |
2d70df72 | 101 | { |
f5691f9c | 102 | return authbasic_initialised == 1; |
2d70df72 | 103 | } |
104 | ||
f5691f9c | 105 | bool |
106 | AuthBasicConfig::configured() const | |
94439e4e | 107 | { |
f5691f9c | 108 | if ((authenticate != NULL) && (authenticateChildren != 0) && |
109 | (basicAuthRealm != NULL)) { | |
bf8fe701 | 110 | debugs(29, 9, "authBasicConfigured: returning configured"); |
f5691f9c | 111 | return true; |
2d70df72 | 112 | } |
62e76326 | 113 | |
bf8fe701 | 114 | debugs(29, 9, "authBasicConfigured: returning unconfigured"); |
f5691f9c | 115 | return false; |
94439e4e | 116 | } |
117 | ||
f5691f9c | 118 | const char * |
119 | AuthBasicConfig::type() const | |
94439e4e | 120 | { |
f5691f9c | 121 | return basicScheme::GetInstance().type(); |
122 | } | |
62e76326 | 123 | |
f5691f9c | 124 | AuthBasicUserRequest::AuthBasicUserRequest() : _theUser(NULL) |
125 | {} | |
126 | ||
127 | AuthBasicUserRequest::~AuthBasicUserRequest() | |
128 | {} | |
129 | ||
130 | ||
131 | bool | |
132 | BasicUser::authenticated() const | |
94439e4e | 133 | { |
f5691f9c | 134 | if ((flags.credentials_ok == 1) && (credentials_checkedtime + basicConfig.credentialsTTL > squid_curtime)) |
135 | return true; | |
136 | ||
bf8fe701 | 137 | debugs(29, 4, "User not authenticated or credentials need rechecking."); |
f5691f9c | 138 | |
139 | return false; | |
94439e4e | 140 | } |
62e76326 | 141 | |
f5691f9c | 142 | int |
143 | AuthBasicUserRequest::authenticated() const | |
144 | { | |
145 | BasicUser const *basic_auth = dynamic_cast<BasicUser const *>(user()); | |
7e6e1767 | 146 | assert (basic_auth != NULL); |
f5691f9c | 147 | |
148 | if (basic_auth->authenticated()) | |
149 | return 1; | |
150 | ||
151 | return 0; | |
152 | } | |
94439e4e | 153 | |
154 | /* log a basic user in | |
155 | */ | |
f5691f9c | 156 | void |
486bf0fb | 157 | AuthBasicUserRequest::authenticate(HttpRequest * request, ConnStateData * conn, http_hdr_type type) |
94439e4e | 158 | { |
f5691f9c | 159 | assert(user() != NULL); |
94439e4e | 160 | |
f5691f9c | 161 | basic_data *basic_auth = dynamic_cast<BasicUser *>(user()); |
9bea1d5b | 162 | |
bd507204 | 163 | /* if the password is not ok, do an identity */ |
62e76326 | 164 | |
17729788 | 165 | if (!basic_auth || basic_auth->flags.credentials_ok != 1) |
62e76326 | 166 | return; |
94439e4e | 167 | |
168 | /* are we about to recheck the credentials externally? */ | |
f5691f9c | 169 | if ((basic_auth->credentials_checkedtime + basicConfig.credentialsTTL) <= squid_curtime) { |
bf8fe701 | 170 | debugs(29, 4, "authBasicAuthenticate: credentials expired - rechecking"); |
62e76326 | 171 | return; |
94439e4e | 172 | } |
62e76326 | 173 | |
94439e4e | 174 | /* we have been through the external helper, and the credentials haven't expired */ |
bf8fe701 | 175 | debugs(29, 9, "authenticateBasicAuthenticateuser: user '" << basic_auth->username() << "' authenticated"); |
94439e4e | 176 | |
f5691f9c | 177 | /* Decode now takes care of finding the AuthUser struct in the cache */ |
94439e4e | 178 | /* after external auth occurs anyway */ |
f5691f9c | 179 | basic_auth->expiretime = current_time.tv_sec; |
62e76326 | 180 | |
94439e4e | 181 | return; |
182 | } | |
183 | ||
184 | int | |
f5691f9c | 185 | AuthBasicUserRequest::module_direction() |
94439e4e | 186 | { |
62e76326 | 187 | /* null auth_user is checked for by authenticateDirection */ |
f5691f9c | 188 | basic_data *basic_auth = dynamic_cast<BasicUser *>(user()); |
189 | assert (basic_auth); | |
62e76326 | 190 | |
bd507204 | 191 | switch (basic_auth->flags.credentials_ok) { |
62e76326 | 192 | |
94439e4e | 193 | case 0: /* not checked */ |
62e76326 | 194 | return -1; |
195 | ||
94439e4e | 196 | case 1: /* checked & ok */ |
62e76326 | 197 | |
f5691f9c | 198 | if (basic_auth->credentials_checkedtime + basicConfig.credentialsTTL <= squid_curtime) |
62e76326 | 199 | return -1; |
200 | ||
201 | return 0; | |
202 | ||
94439e4e | 203 | case 2: /* paused while waiting for a username:password check on another request */ |
62e76326 | 204 | return -1; |
205 | ||
94439e4e | 206 | case 3: /* authentication process failed. */ |
88d3f890 | 207 | return 0; |
94439e4e | 208 | } |
62e76326 | 209 | |
94439e4e | 210 | return -2; |
211 | } | |
212 | ||
213 | void | |
76f142cd | 214 | AuthBasicConfig::fixHeader(AuthUserRequest *auth_user_request, HttpReply *rep, http_hdr_type type, HttpRequest * request) |
94439e4e | 215 | { |
f5691f9c | 216 | if (authenticate) { |
bf8fe701 | 217 | debugs(29, 9, "authenticateFixErrorHeader: Sending type:" << type << " header: 'Basic realm=\"" << basicAuthRealm << "\"'"); |
f5691f9c | 218 | httpHeaderPutStrf(&rep->header, type, "Basic realm=\"%s\"", basicAuthRealm); |
94439e4e | 219 | } |
220 | } | |
221 | ||
222 | /* free any allocated configuration details */ | |
223 | void | |
f5691f9c | 224 | AuthBasicConfig::done() |
94439e4e | 225 | { |
f5691f9c | 226 | if (authenticate) |
227 | wordlistDestroy(&authenticate); | |
62e76326 | 228 | |
f5691f9c | 229 | if (basicAuthRealm) |
230 | safe_free(basicAuthRealm); | |
94439e4e | 231 | } |
232 | ||
f5691f9c | 233 | BasicUser::~BasicUser() |
94439e4e | 234 | { |
f5691f9c | 235 | if (passwd) |
236 | xfree(passwd); | |
62e76326 | 237 | |
f5691f9c | 238 | safe_free (cleartext); |
94439e4e | 239 | } |
240 | ||
241 | static void | |
242 | authenticateBasicHandleReply(void *data, char *reply) | |
243 | { | |
e6ccf245 | 244 | AuthenticateStateData *r = static_cast<AuthenticateStateData *>(data); |
e6ccf245 | 245 | BasicAuthQueueNode *tmpnode; |
94439e4e | 246 | char *t = NULL; |
fa80a8ef | 247 | void *cbdata; |
bf8fe701 | 248 | debugs(29, 9, "authenticateBasicHandleReply: {" << (reply ? reply : "<NULL>") << "}"); |
62e76326 | 249 | |
94439e4e | 250 | if (reply) { |
62e76326 | 251 | if ((t = strchr(reply, ' '))) |
0a0c70cd | 252 | *t++ = '\0'; |
62e76326 | 253 | |
254 | if (*reply == '\0') | |
255 | reply = NULL; | |
94439e4e | 256 | } |
62e76326 | 257 | |
94439e4e | 258 | assert(r->auth_user_request != NULL); |
f5691f9c | 259 | assert(r->auth_user_request->user()->auth_type == AUTH_BASIC); |
260 | basic_data *basic_auth = dynamic_cast<basic_data *>(r->auth_user_request->user()); | |
62e76326 | 261 | |
2948b229 | 262 | assert(basic_auth != NULL); |
263 | ||
94439e4e | 264 | if (reply && (strncasecmp(reply, "OK", 2) == 0)) |
62e76326 | 265 | basic_auth->flags.credentials_ok = 1; |
0a0c70cd | 266 | else { |
62e76326 | 267 | basic_auth->flags.credentials_ok = 3; |
268 | ||
0a0c70cd | 269 | if (t && *t) |
270 | r->auth_user_request->setDenyMessage(t); | |
271 | } | |
272 | ||
94439e4e | 273 | basic_auth->credentials_checkedtime = squid_curtime; |
62e76326 | 274 | |
fa80a8ef | 275 | if (cbdataReferenceValidDone(r->data, &cbdata)) |
62e76326 | 276 | r->handler(cbdata, NULL); |
277 | ||
fa80a8ef | 278 | cbdataReferenceDone(r->data); |
62e76326 | 279 | |
f2afa96a | 280 | while (basic_auth->auth_queue) { |
62e76326 | 281 | tmpnode = basic_auth->auth_queue->next; |
282 | ||
283 | if (cbdataReferenceValidDone(basic_auth->auth_queue->data, &cbdata)) | |
284 | basic_auth->auth_queue->handler(cbdata, NULL); | |
285 | ||
286 | xfree(basic_auth->auth_queue); | |
287 | ||
288 | basic_auth->auth_queue = tmpnode; | |
94439e4e | 289 | } |
62e76326 | 290 | |
94439e4e | 291 | authenticateStateFree(r); |
292 | } | |
293 | ||
f5691f9c | 294 | void |
295 | AuthBasicConfig::dump(StoreEntry * entry, const char *name, AuthConfig * scheme) | |
94439e4e | 296 | { |
f5691f9c | 297 | wordlist *list = authenticate; |
94439e4e | 298 | storeAppendPrintf(entry, "%s %s", name, "basic"); |
62e76326 | 299 | |
94439e4e | 300 | while (list != NULL) { |
62e76326 | 301 | storeAppendPrintf(entry, " %s", list->key); |
302 | list = list->next; | |
94439e4e | 303 | } |
62e76326 | 304 | |
07eca7e0 | 305 | storeAppendPrintf(entry, "\n"); |
306 | ||
f5691f9c | 307 | storeAppendPrintf(entry, "%s basic realm %s\n", name, basicAuthRealm); |
308 | storeAppendPrintf(entry, "%s basic children %d\n", name, authenticateChildren); | |
309 | storeAppendPrintf(entry, "%s basic concurrency %d\n", name, authenticateConcurrency); | |
310 | storeAppendPrintf(entry, "%s basic credentialsttl %d seconds\n", name, (int) credentialsTTL); | |
64658378 | 311 | storeAppendPrintf(entry, "%s basic casesensitive %s\n", name, casesensitive ? "on" : "off"); |
94439e4e | 312 | |
313 | } | |
314 | ||
f5691f9c | 315 | AuthBasicConfig::AuthBasicConfig() |
316 | { | |
317 | /* TODO: move into initialisation list */ | |
318 | authenticateChildren = 5; | |
319 | credentialsTTL = 2 * 60 * 60; /* two hours */ | |
75126633 | 320 | basicAuthRealm = xstrdup("Squid proxy-caching web server"); |
f5691f9c | 321 | } |
62e76326 | 322 | |
3845e4c8 | 323 | AuthBasicConfig::~AuthBasicConfig() |
324 | { | |
acde4327 | 325 | safe_free(basicAuthRealm); |
3845e4c8 | 326 | } |
327 | ||
f5691f9c | 328 | void |
329 | AuthBasicConfig::parse(AuthConfig * scheme, int n_configured, char *param_str) | |
330 | { | |
94439e4e | 331 | if (strcasecmp(param_str, "program") == 0) { |
f5691f9c | 332 | if (authenticate) |
333 | wordlistDestroy(&authenticate); | |
62e76326 | 334 | |
f5691f9c | 335 | parse_wordlist(&authenticate); |
62e76326 | 336 | |
f5691f9c | 337 | requirePathnameExists("authparam basic program", authenticate->key); |
94439e4e | 338 | } else if (strcasecmp(param_str, "children") == 0) { |
f5691f9c | 339 | parse_int(&authenticateChildren); |
07eca7e0 | 340 | } else if (strcasecmp(param_str, "concurrency") == 0) { |
f5691f9c | 341 | parse_int(&authenticateConcurrency); |
94439e4e | 342 | } else if (strcasecmp(param_str, "realm") == 0) { |
f5691f9c | 343 | parse_eol(&basicAuthRealm); |
94439e4e | 344 | } else if (strcasecmp(param_str, "credentialsttl") == 0) { |
f5691f9c | 345 | parse_time_t(&credentialsTTL); |
64658378 | 346 | } else if (strcasecmp(param_str, "casesensitive") == 0) { |
347 | parse_onoff(&casesensitive); | |
94439e4e | 348 | } else { |
bf8fe701 | 349 | debugs(29, 0, "unrecognised basic auth scheme parameter '" << param_str << "'"); |
94439e4e | 350 | } |
351 | } | |
352 | ||
353 | static void | |
354 | authenticateBasicStats(StoreEntry * sentry) | |
355 | { | |
9522b380 | 356 | helperStats(sentry, basicauthenticators, "Basic Authenticator Statistics"); |
94439e4e | 357 | } |
358 | ||
e6ccf245 | 359 | CBDATA_TYPE(AuthenticateStateData); |
94439e4e | 360 | |
e1f7507e | 361 | static AuthUser * |
94439e4e | 362 | authBasicAuthUserFindUsername(const char *username) |
363 | { | |
e6ccf245 | 364 | AuthUserHashPointer *usernamehash; |
e1f7507e | 365 | debugs(29, 9, HERE << "Looking for user '" << username << "'"); |
62e76326 | 366 | |
e6ccf245 | 367 | if (username && (usernamehash = static_cast<AuthUserHashPointer *>(hash_lookup(proxy_auth_username_cache, username)))) { |
62e76326 | 368 | while (usernamehash) { |
f5691f9c | 369 | if ((usernamehash->user()->auth_type == AUTH_BASIC) && |
62e76326 | 370 | !strcmp(username, (char const *)usernamehash->key)) |
f5691f9c | 371 | return usernamehash->user(); |
62e76326 | 372 | |
373 | usernamehash = static_cast<AuthUserHashPointer *>(usernamehash->next); | |
374 | } | |
94439e4e | 375 | } |
62e76326 | 376 | |
94439e4e | 377 | return NULL; |
378 | } | |
379 | ||
f5691f9c | 380 | void |
381 | BasicUser::deleteSelf() const | |
382 | { | |
383 | delete this; | |
384 | } | |
94439e4e | 385 | |
f5691f9c | 386 | BasicUser::BasicUser(AuthConfig *config) : AuthUser (config) , passwd (NULL), credentials_checkedtime(0), auth_queue(NULL), cleartext (NULL), currentRequest (NULL), httpAuthHeader (NULL) |
387 | { | |
388 | flags.credentials_ok = 0; | |
389 | } | |
62e76326 | 390 | |
35b3bc89 | 391 | bool |
f5691f9c | 392 | BasicUser::decodeCleartext() |
393 | { | |
acde4327 AJ |
394 | char *sent_auth = NULL; |
395 | ||
94439e4e | 396 | /* username and password */ |
f5691f9c | 397 | sent_auth = xstrdup(httpAuthHeader); |
acde4327 | 398 | |
94439e4e | 399 | /* Trim trailing \n before decoding */ |
400 | strtok(sent_auth, "\n"); | |
62e76326 | 401 | |
94439e4e | 402 | cleartext = uudecode(sent_auth); |
62e76326 | 403 | |
acde4327 AJ |
404 | safe_free(sent_auth); |
405 | ||
406 | if (!cleartext) | |
407 | return false; | |
62e76326 | 408 | |
94439e4e | 409 | /* |
410 | * Don't allow NL or CR in the credentials. | |
411 | * Oezguer Kesim <oec@codeblau.de> | |
412 | */ | |
bf8fe701 | 413 | debugs(29, 9, "BasicUser::decodeCleartext: '" << cleartext << "'"); |
62e76326 | 414 | |
36a04c15 | 415 | if (strcspn(cleartext, "\r\n") != strlen(cleartext)) { |
bf8fe701 | 416 | debugs(29, 1, "BasicUser::decodeCleartext: bad characters in authorization header '" << httpAuthHeader << "'"); |
147c7544 | 417 | safe_free(cleartext); |
418 | return false; | |
36a04c15 | 419 | } |
35b3bc89 | 420 | return true; |
421 | } | |
36a04c15 | 422 | |
35b3bc89 | 423 | void |
424 | BasicUser::extractUsername() | |
425 | { | |
acde4327 | 426 | char * seperator = strchr(cleartext, ':'); |
62e76326 | 427 | |
acde4327 AJ |
428 | if (seperator == NULL) { |
429 | username(cleartext); | |
430 | } else { | |
431 | /* terminate the username */ | |
432 | *seperator = '\0'; | |
433 | ||
434 | username(cleartext); | |
64658378 | 435 | |
acde4327 AJ |
436 | /* replace the colon so we can find the password */ |
437 | *seperator = ':'; | |
438 | } | |
411c6ea3 | 439 | |
64658378 | 440 | if (!basicConfig.casesensitive) |
441 | Tolower((char *)username()); | |
f5691f9c | 442 | } |
62e76326 | 443 | |
f5691f9c | 444 | void |
445 | BasicUser::extractPassword() | |
446 | { | |
acde4327 | 447 | passwd = strchr(cleartext, ':'); |
62e76326 | 448 | |
acde4327 | 449 | if (passwd == NULL) { |
bf8fe701 | 450 | debugs(29, 4, "authenticateBasicDecodeAuth: no password in proxy authorization header '" << httpAuthHeader << "'"); |
f5691f9c | 451 | passwd = NULL; |
452 | currentRequest->setDenyMessage ("no password was present in the HTTP [proxy-]authorization header. This is most likely a browser bug"); | |
acde4327 AJ |
453 | } else { |
454 | ++passwd; | |
455 | if (*passwd == '\0') { | |
456 | debugs(29, 4, "authenticateBasicDecodeAuth: Disallowing empty password,user is '" << username() << "'"); | |
457 | passwd = NULL; | |
458 | currentRequest->setDenyMessage ("Request denied because you provided an empty password. Users MUST have a password."); | |
459 | } else { | |
460 | passwd = xstrndup(passwd, USER_IDENT_SZ); | |
461 | } | |
94439e4e | 462 | } |
f5691f9c | 463 | } |
94439e4e | 464 | |
f5691f9c | 465 | void |
466 | BasicUser::decode(char const *proxy_auth, AuthUserRequest *auth_user_request) | |
467 | { | |
468 | currentRequest = auth_user_request; | |
469 | httpAuthHeader = proxy_auth; | |
35b3bc89 | 470 | if (decodeCleartext ()) { |
471 | extractUsername(); | |
147c7544 | 472 | extractPassword(); |
35b3bc89 | 473 | } |
f5691f9c | 474 | currentRequest = NULL; |
475 | httpAuthHeader = NULL; | |
476 | } | |
477 | ||
478 | bool | |
479 | BasicUser::valid() const | |
480 | { | |
147c7544 | 481 | if (username() == NULL) |
482 | return false; | |
483 | if (passwd == NULL) | |
484 | return false; | |
485 | return true; | |
f5691f9c | 486 | } |
94439e4e | 487 | |
f5691f9c | 488 | void |
489 | BasicUser::makeLoggingInstance(AuthBasicUserRequest *auth_user_request) | |
490 | { | |
491 | if (username()) { | |
492 | /* log the username */ | |
bf8fe701 | 493 | debugs(29, 9, "authBasicDecodeAuth: Creating new user for logging '" << username() << "'"); |
62e76326 | 494 | /* new scheme data */ |
f5691f9c | 495 | BasicUser *basic_auth = new BasicUser(& basicConfig); |
496 | auth_user_request->user(basic_auth); | |
62e76326 | 497 | /* save the credentials */ |
f5691f9c | 498 | basic_auth->username(username()); |
499 | username(NULL); | |
62e76326 | 500 | /* set the auth_user type */ |
f5691f9c | 501 | basic_auth->auth_type = AUTH_BROKEN; |
502 | /* link the request to the user */ | |
503 | basic_auth->addRequest(auth_user_request); | |
504 | } | |
505 | } | |
506 | ||
507 | AuthUser * | |
508 | BasicUser::makeCachedFrom() | |
509 | { | |
510 | /* the user doesn't exist in the username cache yet */ | |
bf8fe701 | 511 | debugs(29, 9, "authBasicDecodeAuth: Creating new user '" << username() << "'"); |
f5691f9c | 512 | BasicUser *basic_user = new BasicUser(&basicConfig); |
513 | /* save the credentials */ | |
514 | basic_user->username(username()); | |
515 | username(NULL); | |
516 | basic_user->passwd = passwd; | |
517 | passwd = NULL; | |
518 | /* set the auth_user type */ | |
519 | basic_user->auth_type = AUTH_BASIC; | |
520 | /* current time for timeouts */ | |
521 | basic_user->expiretime = current_time.tv_sec; | |
522 | ||
523 | /* this basic_user struct is the 'lucky one' to get added to the username cache */ | |
524 | /* the requests after this link to the basic_user */ | |
525 | /* store user in hash */ | |
526 | basic_user->addToNameCache(); | |
527 | return basic_user; | |
528 | } | |
529 | ||
530 | void | |
531 | BasicUser::updateCached(BasicUser *from) | |
532 | { | |
bf8fe701 | 533 | debugs(29, 9, "authBasicDecodeAuth: Found user '" << from->username() << "' in the user cache as '" << this << "'"); |
f5691f9c | 534 | |
535 | if (strcmp(from->passwd, passwd)) { | |
bf8fe701 | 536 | debugs(29, 4, "authBasicDecodeAuth: new password found. Updating in user master record and resetting auth state to unchecked"); |
f5691f9c | 537 | flags.credentials_ok = 0; |
538 | xfree(passwd); | |
539 | passwd = from->passwd; | |
540 | from->passwd = NULL; | |
541 | } | |
542 | ||
543 | if (flags.credentials_ok == 3) { | |
bf8fe701 | 544 | debugs(29, 4, "authBasicDecodeAuth: last attempt to authenticate this user failed, resetting auth state to unchecked"); |
f5691f9c | 545 | flags.credentials_ok = 0; |
546 | } | |
547 | } | |
548 | ||
549 | /* | |
550 | * Decode a Basic [Proxy-]Auth string, linking the passed | |
551 | * auth_user_request structure to any existing user structure or creating one | |
552 | * if needed. Note that just returning will be treated as | |
553 | * "cannot decode credentials". Use the message field to return a | |
554 | * descriptive message to the user. | |
555 | */ | |
556 | AuthUserRequest * | |
557 | AuthBasicConfig::decode(char const *proxy_auth) | |
558 | { | |
559 | AuthBasicUserRequest *auth_user_request = new AuthBasicUserRequest(); | |
560 | /* decode the username */ | |
561 | /* trim BASIC from string */ | |
562 | ||
ba53f4b8 | 563 | while (xisgraph(*proxy_auth)) |
f5691f9c | 564 | proxy_auth++; |
565 | ||
566 | BasicUser *basic_auth, local_basic(&basicConfig); | |
567 | ||
568 | /* Trim leading whitespace before decoding */ | |
569 | while (xisspace(*proxy_auth)) | |
570 | proxy_auth++; | |
571 | ||
572 | local_basic.decode(proxy_auth, auth_user_request); | |
573 | ||
574 | if (!local_basic.valid()) { | |
575 | local_basic.makeLoggingInstance(auth_user_request); | |
576 | return auth_user_request; | |
94439e4e | 577 | } |
62e76326 | 578 | |
f5691f9c | 579 | /* now lookup and see if we have a matching auth_user structure in |
580 | * memory. */ | |
62e76326 | 581 | |
e1f7507e | 582 | AuthUser *auth_user; |
62e76326 | 583 | |
f5691f9c | 584 | if ((auth_user = authBasicAuthUserFindUsername(local_basic.username())) == NULL) { |
585 | auth_user = local_basic.makeCachedFrom(); | |
586 | basic_auth = dynamic_cast<BasicUser *>(auth_user); | |
587 | assert (basic_auth); | |
588 | } else { | |
589 | basic_auth = dynamic_cast<BasicUser *>(auth_user); | |
590 | assert (basic_auth); | |
591 | basic_auth->updateCached (&local_basic); | |
592 | } | |
62e76326 | 593 | |
f5691f9c | 594 | /* link the request to the in-cache user */ |
595 | auth_user_request->user(basic_auth); | |
62e76326 | 596 | |
f5691f9c | 597 | basic_auth->addRequest(auth_user_request); |
598 | ||
599 | return auth_user_request; | |
94439e4e | 600 | } |
601 | ||
602 | /* Initialize helpers and the like for this auth scheme. Called AFTER parsing the | |
603 | * config file */ | |
f5691f9c | 604 | void |
605 | AuthBasicConfig::init(AuthConfig * scheme) | |
94439e4e | 606 | { |
f5691f9c | 607 | if (authenticate) { |
62e76326 | 608 | authbasic_initialised = 1; |
609 | ||
610 | if (basicauthenticators == NULL) | |
611 | basicauthenticators = helperCreate("basicauthenticator"); | |
612 | ||
f5691f9c | 613 | basicauthenticators->cmdline = authenticate; |
62e76326 | 614 | |
f5691f9c | 615 | basicauthenticators->n_to_start = authenticateChildren; |
62e76326 | 616 | |
f5691f9c | 617 | basicauthenticators->concurrency = authenticateConcurrency; |
07eca7e0 | 618 | |
62e76326 | 619 | basicauthenticators->ipc_type = IPC_STREAM; |
620 | ||
621 | helperOpenServers(basicauthenticators); | |
622 | ||
62e76326 | 623 | CBDATA_INIT_TYPE(AuthenticateStateData); |
94439e4e | 624 | } |
625 | } | |
626 | ||
62ee09ca | 627 | void |
628 | AuthBasicConfig::registerWithCacheManager(CacheManager & manager) | |
629 | { | |
630 | manager.registerAction("basicauthenticator", | |
631 | "Basic User Authenticator Stats", | |
632 | authenticateBasicStats, 0, 1); | |
633 | } | |
634 | ||
f5691f9c | 635 | void |
76f142cd | 636 | BasicUser::queueRequest(AuthUserRequest * auth_user_request, RH * handler, void *data) |
f5691f9c | 637 | { |
638 | BasicAuthQueueNode *node; | |
639 | node = static_cast<BasicAuthQueueNode *>(xmalloc(sizeof(BasicAuthQueueNode))); | |
640 | assert(node); | |
641 | /* save the details */ | |
642 | node->next = auth_queue; | |
643 | auth_queue = node; | |
644 | node->auth_user_request = auth_user_request; | |
645 | node->handler = handler; | |
646 | node->data = cbdataReference(data); | |
647 | } | |
648 | ||
94439e4e | 649 | /* send the initial data to a basic authenticator module */ |
f5691f9c | 650 | void |
651 | AuthBasicUserRequest::module_start(RH * handler, void *data) | |
94439e4e | 652 | { |
94439e4e | 653 | basic_data *basic_auth; |
f5691f9c | 654 | assert(user()->auth_type == AUTH_BASIC); |
655 | basic_auth = dynamic_cast<basic_data *>(user()); | |
2948b229 | 656 | assert(basic_auth != NULL); |
bf8fe701 | 657 | debugs(29, 9, "AuthBasicUserRequest::start: '" << basic_auth->username() << ":" << basic_auth->passwd << "'"); |
62e76326 | 658 | |
f5691f9c | 659 | if (basicConfig.authenticate == NULL) { |
62e76326 | 660 | handler(data, NULL); |
661 | return; | |
94439e4e | 662 | } |
62e76326 | 663 | |
94439e4e | 664 | /* check to see if the auth_user already has a request outstanding */ |
bd507204 | 665 | if (basic_auth->flags.credentials_ok == 2) { |
62e76326 | 666 | /* there is a request with the same credentials already being verified */ |
f5691f9c | 667 | basic_auth->queueRequest(this, handler, data); |
62e76326 | 668 | return; |
94439e4e | 669 | } |
f5691f9c | 670 | |
671 | basic_auth->submitRequest (this, handler, data); | |
94439e4e | 672 | } |
f5691f9c | 673 | |
674 | void | |
76f142cd | 675 | BasicUser::submitRequest (AuthUserRequest * auth_user_request, RH * handler, void *data) |
f5691f9c | 676 | { |
677 | /* mark the user as haveing verification in progress */ | |
678 | flags.credentials_ok = 2; | |
679 | AuthenticateStateData *r = NULL; | |
680 | char buf[8192]; | |
681 | char user[1024], pass[1024]; | |
682 | r = cbdataAlloc(AuthenticateStateData); | |
683 | r->handler = handler; | |
684 | r->data = cbdataReference(data); | |
685 | r->auth_user_request = auth_user_request; | |
686 | xstrncpy(user, rfc1738_escape(username()), sizeof(user)); | |
687 | xstrncpy(pass, rfc1738_escape(passwd), sizeof(pass)); | |
688 | snprintf(buf, sizeof(buf), "%s %s\n", user, pass); | |
689 | helperSubmit(basicauthenticators, buf, authenticateBasicHandleReply, r); | |
690 | } | |
691 | ||
692 | AuthConfig * | |
693 | basicScheme::createConfig() | |
694 | { | |
695 | return &basicConfig; | |
696 | } | |
697 |