]>
Commit | Line | Data |
---|---|---|
94439e4e | 1 | /* |
2fca45f5 | 2 | * $Id: auth_basic.cc,v 1.43 2006/07/07 19:10:29 serassio 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? */ |
2d72d4fd | 96 | debug(29, 2) ("authBasicDone: Basic authentication Shutdown.\n"); |
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)) { | |
62e76326 | 110 | debug(29, 9) ("authBasicConfigured: returning configured\n"); |
f5691f9c | 111 | return true; |
2d70df72 | 112 | } |
62e76326 | 113 | |
2d70df72 | 114 | debug(29, 9) ("authBasicConfigured: returning unconfigured\n"); |
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 | ||
137 | debug(29, 4) ("User not authenticated or credentials need rechecking.\n"); | |
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()); | |
146 | assert (user()); | |
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 |
157 | AuthBasicUserRequest::authenticate(HttpRequest * request, ConnStateData::Pointer 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 | |
bd507204 | 165 | if (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) { |
62e76326 | 170 | debug(29, 4) ("authBasicAuthenticate: credentials expired - rechecking\n"); |
171 | return; | |
94439e4e | 172 | } |
62e76326 | 173 | |
94439e4e | 174 | /* we have been through the external helper, and the credentials haven't expired */ |
175 | debug(29, 9) ("authenticateBasicAuthenticateuser: user '%s' authenticated\n", | |
f5691f9c | 176 | basic_auth->username()); |
94439e4e | 177 | |
f5691f9c | 178 | /* Decode now takes care of finding the AuthUser struct in the cache */ |
94439e4e | 179 | /* after external auth occurs anyway */ |
f5691f9c | 180 | basic_auth->expiretime = current_time.tv_sec; |
62e76326 | 181 | |
94439e4e | 182 | return; |
183 | } | |
184 | ||
185 | int | |
f5691f9c | 186 | AuthBasicUserRequest::module_direction() |
94439e4e | 187 | { |
62e76326 | 188 | /* null auth_user is checked for by authenticateDirection */ |
f5691f9c | 189 | basic_data *basic_auth = dynamic_cast<BasicUser *>(user()); |
190 | assert (basic_auth); | |
62e76326 | 191 | |
bd507204 | 192 | switch (basic_auth->flags.credentials_ok) { |
62e76326 | 193 | |
94439e4e | 194 | case 0: /* not checked */ |
62e76326 | 195 | return -1; |
196 | ||
94439e4e | 197 | case 1: /* checked & ok */ |
62e76326 | 198 | |
f5691f9c | 199 | if (basic_auth->credentials_checkedtime + basicConfig.credentialsTTL <= squid_curtime) |
62e76326 | 200 | return -1; |
201 | ||
202 | return 0; | |
203 | ||
94439e4e | 204 | case 2: /* paused while waiting for a username:password check on another request */ |
62e76326 | 205 | return -1; |
206 | ||
94439e4e | 207 | case 3: /* authentication process failed. */ |
88d3f890 | 208 | return 0; |
94439e4e | 209 | } |
62e76326 | 210 | |
94439e4e | 211 | return -2; |
212 | } | |
213 | ||
214 | void | |
f5691f9c | 215 | AuthBasicConfig::fixHeader(auth_user_request_t *auth_user_request, HttpReply *rep, http_hdr_type type, HttpRequest * request) |
94439e4e | 216 | { |
f5691f9c | 217 | if (authenticate) { |
218 | debug(29, 9) ("authenticateFixErrorHeader: Sending type:%d header: 'Basic realm=\"%s\"'\n", type, basicAuthRealm); | |
219 | httpHeaderPutStrf(&rep->header, type, "Basic realm=\"%s\"", basicAuthRealm); | |
94439e4e | 220 | } |
221 | } | |
222 | ||
223 | /* free any allocated configuration details */ | |
224 | void | |
f5691f9c | 225 | AuthBasicConfig::done() |
94439e4e | 226 | { |
f5691f9c | 227 | if (authenticate) |
228 | wordlistDestroy(&authenticate); | |
62e76326 | 229 | |
f5691f9c | 230 | if (basicAuthRealm) |
231 | safe_free(basicAuthRealm); | |
94439e4e | 232 | } |
233 | ||
f5691f9c | 234 | BasicUser::~BasicUser() |
94439e4e | 235 | { |
f5691f9c | 236 | if (passwd) |
237 | xfree(passwd); | |
62e76326 | 238 | |
f5691f9c | 239 | safe_free (cleartext); |
94439e4e | 240 | } |
241 | ||
242 | static void | |
243 | authenticateBasicHandleReply(void *data, char *reply) | |
244 | { | |
e6ccf245 | 245 | AuthenticateStateData *r = static_cast<AuthenticateStateData *>(data); |
e6ccf245 | 246 | BasicAuthQueueNode *tmpnode; |
94439e4e | 247 | char *t = NULL; |
fa80a8ef | 248 | void *cbdata; |
94439e4e | 249 | debug(29, 9) ("authenticateBasicHandleReply: {%s}\n", reply ? reply : "<NULL>"); |
62e76326 | 250 | |
94439e4e | 251 | if (reply) { |
62e76326 | 252 | if ((t = strchr(reply, ' '))) |
0a0c70cd | 253 | *t++ = '\0'; |
62e76326 | 254 | |
255 | if (*reply == '\0') | |
256 | reply = NULL; | |
94439e4e | 257 | } |
62e76326 | 258 | |
94439e4e | 259 | assert(r->auth_user_request != NULL); |
f5691f9c | 260 | assert(r->auth_user_request->user()->auth_type == AUTH_BASIC); |
261 | basic_data *basic_auth = dynamic_cast<basic_data *>(r->auth_user_request->user()); | |
62e76326 | 262 | |
94439e4e | 263 | if (reply && (strncasecmp(reply, "OK", 2) == 0)) |
62e76326 | 264 | basic_auth->flags.credentials_ok = 1; |
0a0c70cd | 265 | else { |
62e76326 | 266 | basic_auth->flags.credentials_ok = 3; |
267 | ||
0a0c70cd | 268 | if (t && *t) |
269 | r->auth_user_request->setDenyMessage(t); | |
270 | } | |
271 | ||
94439e4e | 272 | basic_auth->credentials_checkedtime = squid_curtime; |
62e76326 | 273 | |
fa80a8ef | 274 | if (cbdataReferenceValidDone(r->data, &cbdata)) |
62e76326 | 275 | r->handler(cbdata, NULL); |
276 | ||
fa80a8ef | 277 | cbdataReferenceDone(r->data); |
62e76326 | 278 | |
f2afa96a | 279 | while (basic_auth->auth_queue) { |
62e76326 | 280 | tmpnode = basic_auth->auth_queue->next; |
281 | ||
282 | if (cbdataReferenceValidDone(basic_auth->auth_queue->data, &cbdata)) | |
283 | basic_auth->auth_queue->handler(cbdata, NULL); | |
284 | ||
285 | xfree(basic_auth->auth_queue); | |
286 | ||
287 | basic_auth->auth_queue = tmpnode; | |
94439e4e | 288 | } |
62e76326 | 289 | |
94439e4e | 290 | authenticateStateFree(r); |
291 | } | |
292 | ||
f5691f9c | 293 | void |
294 | AuthBasicConfig::dump(StoreEntry * entry, const char *name, AuthConfig * scheme) | |
94439e4e | 295 | { |
f5691f9c | 296 | wordlist *list = authenticate; |
94439e4e | 297 | storeAppendPrintf(entry, "%s %s", name, "basic"); |
62e76326 | 298 | |
94439e4e | 299 | while (list != NULL) { |
62e76326 | 300 | storeAppendPrintf(entry, " %s", list->key); |
301 | list = list->next; | |
94439e4e | 302 | } |
62e76326 | 303 | |
07eca7e0 | 304 | storeAppendPrintf(entry, "\n"); |
305 | ||
f5691f9c | 306 | storeAppendPrintf(entry, "%s basic realm %s\n", name, basicAuthRealm); |
307 | storeAppendPrintf(entry, "%s basic children %d\n", name, authenticateChildren); | |
308 | storeAppendPrintf(entry, "%s basic concurrency %d\n", name, authenticateConcurrency); | |
309 | storeAppendPrintf(entry, "%s basic credentialsttl %d seconds\n", name, (int) credentialsTTL); | |
64658378 | 310 | storeAppendPrintf(entry, "%s basic casesensitive %s\n", name, casesensitive ? "on" : "off"); |
94439e4e | 311 | |
312 | } | |
313 | ||
f5691f9c | 314 | AuthBasicConfig::AuthBasicConfig() |
315 | { | |
316 | /* TODO: move into initialisation list */ | |
317 | authenticateChildren = 5; | |
318 | credentialsTTL = 2 * 60 * 60; /* two hours */ | |
75126633 | 319 | basicAuthRealm = xstrdup("Squid proxy-caching web server"); |
f5691f9c | 320 | } |
62e76326 | 321 | |
f5691f9c | 322 | void |
323 | AuthBasicConfig::parse(AuthConfig * scheme, int n_configured, char *param_str) | |
324 | { | |
94439e4e | 325 | if (strcasecmp(param_str, "program") == 0) { |
f5691f9c | 326 | if (authenticate) |
327 | wordlistDestroy(&authenticate); | |
62e76326 | 328 | |
f5691f9c | 329 | parse_wordlist(&authenticate); |
62e76326 | 330 | |
f5691f9c | 331 | requirePathnameExists("authparam basic program", authenticate->key); |
94439e4e | 332 | } else if (strcasecmp(param_str, "children") == 0) { |
f5691f9c | 333 | parse_int(&authenticateChildren); |
07eca7e0 | 334 | } else if (strcasecmp(param_str, "concurrency") == 0) { |
f5691f9c | 335 | parse_int(&authenticateConcurrency); |
94439e4e | 336 | } else if (strcasecmp(param_str, "realm") == 0) { |
f5691f9c | 337 | parse_eol(&basicAuthRealm); |
94439e4e | 338 | } else if (strcasecmp(param_str, "credentialsttl") == 0) { |
f5691f9c | 339 | parse_time_t(&credentialsTTL); |
64658378 | 340 | } else if (strcasecmp(param_str, "casesensitive") == 0) { |
341 | parse_onoff(&casesensitive); | |
94439e4e | 342 | } else { |
2fca45f5 | 343 | debug(29, 0) ("unrecognised basic auth scheme parameter '%s'\n", param_str); |
94439e4e | 344 | } |
345 | } | |
346 | ||
347 | static void | |
348 | authenticateBasicStats(StoreEntry * sentry) | |
349 | { | |
350 | storeAppendPrintf(sentry, "Basic Authenticator Statistics:\n"); | |
351 | helperStats(sentry, basicauthenticators); | |
352 | } | |
353 | ||
e6ccf245 | 354 | CBDATA_TYPE(AuthenticateStateData); |
94439e4e | 355 | |
2d72d4fd | 356 | static auth_user_t * |
94439e4e | 357 | authBasicAuthUserFindUsername(const char *username) |
358 | { | |
e6ccf245 | 359 | AuthUserHashPointer *usernamehash; |
94439e4e | 360 | debug(29, 9) ("authBasicAuthUserFindUsername: Looking for user '%s'\n", username); |
62e76326 | 361 | |
e6ccf245 | 362 | if (username && (usernamehash = static_cast<AuthUserHashPointer *>(hash_lookup(proxy_auth_username_cache, username)))) { |
62e76326 | 363 | while (usernamehash) { |
f5691f9c | 364 | if ((usernamehash->user()->auth_type == AUTH_BASIC) && |
62e76326 | 365 | !strcmp(username, (char const *)usernamehash->key)) |
f5691f9c | 366 | return usernamehash->user(); |
62e76326 | 367 | |
368 | usernamehash = static_cast<AuthUserHashPointer *>(usernamehash->next); | |
369 | } | |
94439e4e | 370 | } |
62e76326 | 371 | |
94439e4e | 372 | return NULL; |
373 | } | |
374 | ||
f5691f9c | 375 | void |
376 | BasicUser::deleteSelf() const | |
377 | { | |
378 | delete this; | |
379 | } | |
94439e4e | 380 | |
f5691f9c | 381 | BasicUser::BasicUser(AuthConfig *config) : AuthUser (config) , passwd (NULL), credentials_checkedtime(0), auth_queue(NULL), cleartext (NULL), currentRequest (NULL), httpAuthHeader (NULL) |
382 | { | |
383 | flags.credentials_ok = 0; | |
384 | } | |
62e76326 | 385 | |
f5691f9c | 386 | void |
387 | BasicUser::decodeCleartext() | |
388 | { | |
389 | char *sent_auth; | |
94439e4e | 390 | /* username and password */ |
f5691f9c | 391 | sent_auth = xstrdup(httpAuthHeader); |
94439e4e | 392 | /* Trim trailing \n before decoding */ |
393 | strtok(sent_auth, "\n"); | |
62e76326 | 394 | |
94439e4e | 395 | cleartext = uudecode(sent_auth); |
62e76326 | 396 | |
94439e4e | 397 | xfree(sent_auth); |
f5691f9c | 398 | } |
62e76326 | 399 | |
f5691f9c | 400 | void |
401 | BasicUser::extractUsername() | |
402 | { | |
94439e4e | 403 | /* |
404 | * Don't allow NL or CR in the credentials. | |
405 | * Oezguer Kesim <oec@codeblau.de> | |
406 | */ | |
94439e4e | 407 | debug(29, 9) ("authenticateBasicDecodeAuth: cleartext = '%s'\n", cleartext); |
62e76326 | 408 | |
36a04c15 | 409 | if (strcspn(cleartext, "\r\n") != strlen(cleartext)) { |
410 | debug(29, 1) ("authenticateBasicDecodeAuth: bad characters in authorization header '%s'\n", | |
411 | httpAuthHeader); | |
412 | xfree(cleartext); | |
413 | return; | |
414 | } | |
415 | ||
416 | char * tempusername = cleartext; | |
f5691f9c | 417 | /* terminate the username string */ |
62e76326 | 418 | |
411c6ea3 | 419 | if ((cleartext = strchr(tempusername, ':')) != NULL) |
62e76326 | 420 | *(cleartext)++ = '\0'; |
64658378 | 421 | |
411c6ea3 | 422 | username (tempusername); |
423 | ||
64658378 | 424 | if (!basicConfig.casesensitive) |
425 | Tolower((char *)username()); | |
f5691f9c | 426 | } |
62e76326 | 427 | |
f5691f9c | 428 | void |
429 | BasicUser::extractPassword() | |
430 | { | |
431 | passwd = cleartext; | |
62e76326 | 432 | |
94439e4e | 433 | if (cleartext == NULL) { |
62e76326 | 434 | debug(29, 4) ("authenticateBasicDecodeAuth: no password in proxy authorization header '%s'\n", |
f5691f9c | 435 | httpAuthHeader); |
436 | passwd = NULL; | |
437 | currentRequest->setDenyMessage ("no password was present in the HTTP [proxy-]authorization header. This is most likely a browser bug"); | |
94439e4e | 438 | } else if (*cleartext == '\0') { |
62e76326 | 439 | debug(29, 4) ("authenticateBasicDecodeAuth: Disallowing empty password," |
f5691f9c | 440 | "user is '%s'\n", username()); |
441 | passwd = NULL; | |
442 | currentRequest->setDenyMessage ("Request denied because you provided an empty password. Users MUST have a password."); | |
94439e4e | 443 | } |
62e76326 | 444 | |
f5691f9c | 445 | if (passwd) |
446 | passwd = xstrndup(cleartext, USER_IDENT_SZ); | |
62e76326 | 447 | |
f5691f9c | 448 | cleartext = NULL; |
449 | } | |
94439e4e | 450 | |
f5691f9c | 451 | void |
452 | BasicUser::decode(char const *proxy_auth, AuthUserRequest *auth_user_request) | |
453 | { | |
454 | currentRequest = auth_user_request; | |
455 | httpAuthHeader = proxy_auth; | |
456 | decodeCleartext (); | |
457 | extractUsername(); | |
458 | extractPassword(); | |
459 | currentRequest = NULL; | |
460 | httpAuthHeader = NULL; | |
461 | } | |
462 | ||
463 | bool | |
464 | BasicUser::valid() const | |
465 | { | |
466 | return passwd != NULL; | |
467 | } | |
94439e4e | 468 | |
f5691f9c | 469 | void |
470 | BasicUser::makeLoggingInstance(AuthBasicUserRequest *auth_user_request) | |
471 | { | |
472 | if (username()) { | |
473 | /* log the username */ | |
474 | debug(29, 9) ("authBasicDecodeAuth: Creating new user for logging '%s'\n", username()); | |
62e76326 | 475 | /* new scheme data */ |
f5691f9c | 476 | BasicUser *basic_auth = new BasicUser(& basicConfig); |
477 | auth_user_request->user(basic_auth); | |
62e76326 | 478 | /* save the credentials */ |
f5691f9c | 479 | basic_auth->username(username()); |
480 | username(NULL); | |
62e76326 | 481 | /* set the auth_user type */ |
f5691f9c | 482 | basic_auth->auth_type = AUTH_BROKEN; |
483 | /* link the request to the user */ | |
484 | basic_auth->addRequest(auth_user_request); | |
485 | } | |
486 | } | |
487 | ||
488 | AuthUser * | |
489 | BasicUser::makeCachedFrom() | |
490 | { | |
491 | /* the user doesn't exist in the username cache yet */ | |
492 | debug(29, 9) ("authBasicDecodeAuth: Creating new user '%s'\n", username()); | |
493 | BasicUser *basic_user = new BasicUser(&basicConfig); | |
494 | /* save the credentials */ | |
495 | basic_user->username(username()); | |
496 | username(NULL); | |
497 | basic_user->passwd = passwd; | |
498 | passwd = NULL; | |
499 | /* set the auth_user type */ | |
500 | basic_user->auth_type = AUTH_BASIC; | |
501 | /* current time for timeouts */ | |
502 | basic_user->expiretime = current_time.tv_sec; | |
503 | ||
504 | /* this basic_user struct is the 'lucky one' to get added to the username cache */ | |
505 | /* the requests after this link to the basic_user */ | |
506 | /* store user in hash */ | |
507 | basic_user->addToNameCache(); | |
508 | return basic_user; | |
509 | } | |
510 | ||
511 | void | |
512 | BasicUser::updateCached(BasicUser *from) | |
513 | { | |
514 | debug(29, 9) ("authBasicDecodeAuth: Found user '%s' in the user cache as '%p'\n", from->username(), this); | |
515 | ||
516 | if (strcmp(from->passwd, passwd)) { | |
517 | debug(29, 4) ("authBasicDecodeAuth: new password found. Updating in user master record and resetting auth state to unchecked\n"); | |
518 | flags.credentials_ok = 0; | |
519 | xfree(passwd); | |
520 | passwd = from->passwd; | |
521 | from->passwd = NULL; | |
522 | } | |
523 | ||
524 | if (flags.credentials_ok == 3) { | |
525 | debug(29, 4) ("authBasicDecodeAuth: last attempt to authenticate this user failed, resetting auth state to unchecked\n"); | |
526 | flags.credentials_ok = 0; | |
527 | } | |
528 | } | |
529 | ||
530 | /* | |
531 | * Decode a Basic [Proxy-]Auth string, linking the passed | |
532 | * auth_user_request structure to any existing user structure or creating one | |
533 | * if needed. Note that just returning will be treated as | |
534 | * "cannot decode credentials". Use the message field to return a | |
535 | * descriptive message to the user. | |
536 | */ | |
537 | AuthUserRequest * | |
538 | AuthBasicConfig::decode(char const *proxy_auth) | |
539 | { | |
540 | AuthBasicUserRequest *auth_user_request = new AuthBasicUserRequest(); | |
541 | /* decode the username */ | |
542 | /* trim BASIC from string */ | |
543 | ||
ba53f4b8 | 544 | while (xisgraph(*proxy_auth)) |
f5691f9c | 545 | proxy_auth++; |
546 | ||
547 | BasicUser *basic_auth, local_basic(&basicConfig); | |
548 | ||
549 | /* Trim leading whitespace before decoding */ | |
550 | while (xisspace(*proxy_auth)) | |
551 | proxy_auth++; | |
552 | ||
553 | local_basic.decode(proxy_auth, auth_user_request); | |
554 | ||
555 | if (!local_basic.valid()) { | |
556 | local_basic.makeLoggingInstance(auth_user_request); | |
557 | return auth_user_request; | |
94439e4e | 558 | } |
62e76326 | 559 | |
f5691f9c | 560 | /* now lookup and see if we have a matching auth_user structure in |
561 | * memory. */ | |
62e76326 | 562 | |
f5691f9c | 563 | auth_user_t *auth_user; |
62e76326 | 564 | |
f5691f9c | 565 | if ((auth_user = authBasicAuthUserFindUsername(local_basic.username())) == NULL) { |
566 | auth_user = local_basic.makeCachedFrom(); | |
567 | basic_auth = dynamic_cast<BasicUser *>(auth_user); | |
568 | assert (basic_auth); | |
569 | } else { | |
570 | basic_auth = dynamic_cast<BasicUser *>(auth_user); | |
571 | assert (basic_auth); | |
572 | basic_auth->updateCached (&local_basic); | |
573 | } | |
62e76326 | 574 | |
f5691f9c | 575 | /* link the request to the in-cache user */ |
576 | auth_user_request->user(basic_auth); | |
62e76326 | 577 | |
f5691f9c | 578 | basic_auth->addRequest(auth_user_request); |
579 | ||
580 | return auth_user_request; | |
94439e4e | 581 | } |
582 | ||
583 | /* Initialize helpers and the like for this auth scheme. Called AFTER parsing the | |
584 | * config file */ | |
f5691f9c | 585 | void |
586 | AuthBasicConfig::init(AuthConfig * scheme) | |
94439e4e | 587 | { |
f5691f9c | 588 | if (authenticate) { |
62e76326 | 589 | authbasic_initialised = 1; |
590 | ||
591 | if (basicauthenticators == NULL) | |
592 | basicauthenticators = helperCreate("basicauthenticator"); | |
593 | ||
f5691f9c | 594 | basicauthenticators->cmdline = authenticate; |
62e76326 | 595 | |
f5691f9c | 596 | basicauthenticators->n_to_start = authenticateChildren; |
62e76326 | 597 | |
f5691f9c | 598 | basicauthenticators->concurrency = authenticateConcurrency; |
07eca7e0 | 599 | |
62e76326 | 600 | basicauthenticators->ipc_type = IPC_STREAM; |
601 | ||
602 | helperOpenServers(basicauthenticators); | |
603 | ||
62e76326 | 604 | CBDATA_INIT_TYPE(AuthenticateStateData); |
94439e4e | 605 | } |
606 | } | |
607 | ||
62ee09ca | 608 | void |
609 | AuthBasicConfig::registerWithCacheManager(CacheManager & manager) | |
610 | { | |
611 | manager.registerAction("basicauthenticator", | |
612 | "Basic User Authenticator Stats", | |
613 | authenticateBasicStats, 0, 1); | |
614 | } | |
615 | ||
f5691f9c | 616 | void |
617 | BasicUser::queueRequest(auth_user_request_t * auth_user_request, RH * handler, void *data) | |
618 | { | |
619 | BasicAuthQueueNode *node; | |
620 | node = static_cast<BasicAuthQueueNode *>(xmalloc(sizeof(BasicAuthQueueNode))); | |
621 | assert(node); | |
622 | /* save the details */ | |
623 | node->next = auth_queue; | |
624 | auth_queue = node; | |
625 | node->auth_user_request = auth_user_request; | |
626 | node->handler = handler; | |
627 | node->data = cbdataReference(data); | |
628 | } | |
629 | ||
94439e4e | 630 | /* send the initial data to a basic authenticator module */ |
f5691f9c | 631 | void |
632 | AuthBasicUserRequest::module_start(RH * handler, void *data) | |
94439e4e | 633 | { |
94439e4e | 634 | basic_data *basic_auth; |
f5691f9c | 635 | assert(user()->auth_type == AUTH_BASIC); |
636 | basic_auth = dynamic_cast<basic_data *>(user()); | |
637 | debug(29, 9) ("AuthBasicUserRequest::start: '%s:%s'\n", basic_auth->username(), | |
62e76326 | 638 | basic_auth->passwd); |
639 | ||
f5691f9c | 640 | if (basicConfig.authenticate == NULL) { |
62e76326 | 641 | handler(data, NULL); |
642 | return; | |
94439e4e | 643 | } |
62e76326 | 644 | |
94439e4e | 645 | /* check to see if the auth_user already has a request outstanding */ |
bd507204 | 646 | if (basic_auth->flags.credentials_ok == 2) { |
62e76326 | 647 | /* there is a request with the same credentials already being verified */ |
f5691f9c | 648 | basic_auth->queueRequest(this, handler, data); |
62e76326 | 649 | return; |
94439e4e | 650 | } |
f5691f9c | 651 | |
652 | basic_auth->submitRequest (this, handler, data); | |
94439e4e | 653 | } |
f5691f9c | 654 | |
655 | void | |
656 | BasicUser::submitRequest (auth_user_request_t * auth_user_request, RH * handler, void *data) | |
657 | { | |
658 | /* mark the user as haveing verification in progress */ | |
659 | flags.credentials_ok = 2; | |
660 | AuthenticateStateData *r = NULL; | |
661 | char buf[8192]; | |
662 | char user[1024], pass[1024]; | |
663 | r = cbdataAlloc(AuthenticateStateData); | |
664 | r->handler = handler; | |
665 | r->data = cbdataReference(data); | |
666 | r->auth_user_request = auth_user_request; | |
667 | xstrncpy(user, rfc1738_escape(username()), sizeof(user)); | |
668 | xstrncpy(pass, rfc1738_escape(passwd), sizeof(pass)); | |
669 | snprintf(buf, sizeof(buf), "%s %s\n", user, pass); | |
670 | helperSubmit(basicauthenticators, buf, authenticateBasicHandleReply, r); | |
671 | } | |
672 | ||
673 | AuthConfig * | |
674 | basicScheme::createConfig() | |
675 | { | |
676 | return &basicConfig; | |
677 | } | |
678 |