]> git.ipfire.org Git - thirdparty/squid.git/blob - src/auth/ntlm/auth_ntlm.cc
Major rewrite of proxy authentication to support other schemes than
[thirdparty/squid.git] / src / auth / ntlm / auth_ntlm.cc
1
2 /*
3 * $Id: auth_ntlm.cc,v 1.1 2001/01/07 23:36:48 hno Exp $
4 *
5 * DEBUG: section 29 NTLM Authenticator
6 * AUTHOR: Robert Collins
7 *
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
13 * National Laboratory for Applied Network Research and funded by the
14 * National Science Foundation. Squid is Copyrighted (C) 1998 by
15 * the Regents of the University of California. Please see the
16 * COPYRIGHT file for full details. Squid incorporates software
17 * developed and/or copyrighted by other sources. Please see the
18 * CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 /* The functions in this file handle authentication.
37 * They DO NOT perform access control or auditing.
38 * See acl.c for access control and client_side.c for auditing */
39
40
41 #include "squid.h"
42 #include "auth_ntlm.h"
43
44 static void
45 authenticateStateFree(authenticateStateData * r)
46 {
47 cbdataFree(r);
48 }
49
50 /* NTLM Scheme */
51 static HLPSCB authenticateNTLMHandleReply;
52 static HLPSCB authenticateNTLMHandleplaceholder;
53 static AUTHSACTIVE authenticateNTLMActive;
54 static AUTHSAUTHED authNTLMAuthenticated;
55 static AUTHSAUTHUSER authenticateNTLMAuthenticateUser;
56 static AUTHSFIXERR authenticateNTLMFixErrorHeader;
57 static AUTHSFREE authenticateNTLMFreeUser;
58 static AUTHSDIRECTION authenticateNTLMDirection;
59 static AUTHSDECODE authenticateDecodeNTLMAuth;
60 static AUTHSDUMP authNTLMCfgDump;
61 static AUTHSFREECONFIG authNTLMFreeConfig;
62 static AUTHSINIT authNTLMInit;
63 static AUTHSONCLOSEC authenticateNTLMOnCloseConnection;
64 static AUTHSUSERNAME authenticateNTLMUsername;
65 static AUTHSREQFREE authNTLMAURequestFree;
66 static AUTHSPARSE authNTLMParse;
67 static AUTHSSTART authenticateNTLMStart;
68 static AUTHSSTATS authenticateNTLMStats;
69 static AUTHSSHUTDOWN authNTLMDone;
70
71 /* helper callbacks to handle per server state data */
72 static HLPSAVAIL authenticateNTLMHelperServerAvailable;
73 static HLPSONEQ authenticateNTLMHelperServerOnEmpty;
74
75 static statefulhelper *ntlmauthenticators = NULL;
76
77 CBDATA_TYPE(authenticateStateData);
78
79 static int authntlm_initialised = 0;
80
81 MemPool *ntlm_helper_state_pool = NULL;
82 MemPool *ntlm_user_pool = NULL;
83 MemPool *ntlm_request_pool = NULL;
84 static auth_ntlm_config *ntlmConfig = NULL;
85
86 static hash_table *proxy_auth_cache = NULL;
87
88 /*
89 *
90 * Private Functions
91 *
92 */
93
94 void
95 authNTLMDone(void)
96 {
97 // memPoolDestroy(ufs_state_pool);
98
99 if (ntlmauthenticators)
100 helperStatefulShutdown(ntlmauthenticators);
101 authntlm_initialised = 0;
102 if (!shutting_down)
103 return;
104 helperStatefulFree(ntlmauthenticators);
105 ntlmauthenticators = NULL;
106 memPoolDestroy(ntlm_helper_state_pool);
107 memPoolDestroy(ntlm_request_pool);
108 memPoolDestroy(ntlm_user_pool);
109
110 }
111
112 /* free any allocated configuration details */
113 void
114 authNTLMFreeConfig(authScheme * scheme)
115 {
116 if (ntlmConfig == NULL)
117 return;
118 assert(ntlmConfig == scheme->scheme_data);
119 if (ntlmConfig->authenticate)
120 wordlistDestroy(&ntlmConfig->authenticate);
121 xfree(ntlmConfig);
122 ntlmConfig = NULL;
123 }
124
125 static void
126 authNTLMCfgDump(StoreEntry * entry, const char *name, authScheme * scheme)
127 {
128 auth_ntlm_config *config = scheme->scheme_data;
129 wordlist *list = config->authenticate;
130 storeAppendPrintf(entry, "%s %s", name, "ntlm");
131 while (list != NULL) {
132 storeAppendPrintf(entry, " %s", list->key);
133 list = list->next;
134 }
135 storeAppendPrintf(entry, "\n%s %s children %d\n%s %s max_challenge_reuses %d\n%s %s max_challenge_lifetime %d seconds\n",
136 name, "ntlm", config->authenticateChildren,
137 name, "ntlm", config->challengeuses,
138 name, "ntlm", config->challengelifetime);
139
140 }
141
142 static void
143 authNTLMParse(authScheme * scheme, int n_configured, char *param_str)
144 {
145 if (scheme->scheme_data == NULL) {
146 assert(ntlmConfig == NULL);
147 /* this is the first param to be found */
148 scheme->scheme_data = xmalloc(sizeof(auth_ntlm_config));
149 memset(scheme->scheme_data, 0, sizeof(auth_ntlm_config));
150 ntlmConfig = scheme->scheme_data;
151 ntlmConfig->authenticateChildren = 5;
152 ntlmConfig->challengeuses = 0;
153 ntlmConfig->challengelifetime = 60;
154 }
155 ntlmConfig = scheme->scheme_data;
156 if (strcasecmp(param_str, "program") == 0) {
157 parse_wordlist(&ntlmConfig->authenticate);
158 requirePathnameExists("authparam ntlm program", ntlmConfig->authenticate->key);
159 } else if (strcasecmp(param_str, "children") == 0) {
160 parse_int(&ntlmConfig->authenticateChildren);
161 } else if (strcasecmp(param_str, "max_challenge_reuses") == 0) {
162 parse_int(&ntlmConfig->challengeuses);
163 } else if (strcasecmp(param_str, "max_challenge_lifetime") == 0) {
164 parse_time_t(&ntlmConfig->challengelifetime);
165 } else {
166 debug(28, 0) ("unrecognised ntlm auth scheme parameter '%s'\n", param_str);
167 }
168 }
169
170
171 void
172 authSchemeSetup_ntlm(authscheme_entry_t * authscheme)
173 {
174 #if 0
175 static int ntlminit = 0;
176 #endif
177 assert(!authntlm_initialised);
178 authscheme->Active = authenticateNTLMActive;
179 authscheme->parse = authNTLMParse;
180 authscheme->dump = authNTLMCfgDump;
181 authscheme->requestFree = authNTLMAURequestFree;
182 authscheme->freeconfig = authNTLMFreeConfig;
183 authscheme->init = authNTLMInit;
184 authscheme->authAuthenticate = authenticateNTLMAuthenticateUser;
185 authscheme->authenticated = authNTLMAuthenticated;
186 authscheme->authFixHeader = authenticateNTLMFixErrorHeader;
187 authscheme->FreeUser = authenticateNTLMFreeUser;
188 authscheme->authStart = authenticateNTLMStart;
189 authscheme->authStats = authenticateNTLMStats;
190 authscheme->authUserUsername = authenticateNTLMUsername;
191 authscheme->getdirection = authenticateNTLMDirection;
192 authscheme->decodeauth = authenticateDecodeNTLMAuth;
193 authscheme->donefunc = authNTLMDone;
194 authscheme->oncloseconnection = authenticateNTLMOnCloseConnection;
195 }
196
197 /* Initialize helpers and the like for this auth scheme. Called AFTER parsing the
198 * config file */
199 static void
200 authNTLMInit(authScheme * scheme)
201 {
202 static int ntlminit = 0;
203 if (ntlmConfig->authenticate) {
204 if (!ntlm_helper_state_pool)
205 ntlm_helper_state_pool = memPoolCreate("NTLM Helper State data", sizeof(ntlm_helper_state_t));
206 if (!ntlm_user_pool)
207 ntlm_user_pool = memPoolCreate("NTLM Scheme User Data", sizeof(ntlm_user_t));
208 if (!ntlm_request_pool)
209 ntlm_request_pool = memPoolCreate("NTLM Scheme Request Data", sizeof(ntlm_request_t));
210 authntlm_initialised = 1;
211 if (ntlmauthenticators == NULL)
212 ntlmauthenticators = helperStatefulCreate("ntlmauthenticator");
213 if (!proxy_auth_cache)
214 proxy_auth_cache = hash_create((HASHCMP *) strcmp, 7921, hash_string);
215 assert(proxy_auth_cache);
216 ntlmauthenticators->cmdline = ntlmConfig->authenticate;
217 ntlmauthenticators->n_to_start = ntlmConfig->authenticateChildren;
218 ntlmauthenticators->ipc_type = IPC_TCP_SOCKET;
219 ntlmauthenticators->datapool = ntlm_helper_state_pool;
220 ntlmauthenticators->IsAvailable = authenticateNTLMHelperServerAvailable;
221 ntlmauthenticators->OnEmptyQueue = authenticateNTLMHelperServerOnEmpty;
222 helperStatefulOpenServers(ntlmauthenticators);
223 /* TODO: In here send the initial YR to preinitialise the challenge cache */
224 /* Think about this... currently we ask when the challenge is needed. Better? */
225 if (!ntlminit) {
226 cachemgrRegister("ntlmauthenticator",
227 "User NTLM Authenticator Stats",
228 authenticateNTLMStats, 0, 1);
229 ntlminit++;
230 }
231 CBDATA_INIT_TYPE(authenticateStateData);
232 }
233 }
234
235 int
236 authenticateNTLMActive()
237 {
238 if (authntlm_initialised)
239 return 1;
240 else
241 return 0;
242 }
243
244 /* NTLM Scheme */
245
246 int
247 authenticateNTLMDirection(auth_user_request_t * auth_user_request)
248 {
249 ntlm_request_t *ntlm_request = auth_user_request->scheme_data;
250 /* null auth_user is checked for by authenticateDirection */
251 switch (ntlm_request->auth_state) {
252 case AUTHENTICATE_STATE_NONE: /* no progress at all. */
253 debug(28, 1) ("authenticateNTLMDirection: called before NTLM Authenticate!. Report a bug to squid-dev.\n");
254 return -2;
255 case AUTHENTICATE_STATE_NEGOTIATE: /* send to helper */
256 case AUTHENTICATE_STATE_RESPONSE: /*send to helper */
257 return -1;
258 case AUTHENTICATE_STATE_CHALLENGE: /* send to client */
259 return 1;
260 case AUTHENTICATE_STATE_DONE: /* do nothing.. */
261 return 0;
262 }
263 return -2;
264 }
265
266 /*
267 * Send the authenticate error header(s). Note: IE has a bug and the NTLM header
268 * must be first. To ensure that, the configure use --enable-auth=ntlm, anything
269 * else.
270 */
271 void
272 authenticateNTLMFixErrorHeader(auth_user_request_t * auth_user_request, HttpReply * rep, http_hdr_type type, request_t * request)
273 {
274 ntlm_request_t *ntlm_request;
275 if (ntlmConfig->authenticate) {
276 /* New request, no user details */
277 if (auth_user_request == NULL) {
278 debug(29, 9) ("authenticateNTLMFixErrorHeader: Sending type:%d header: 'NTLM'\n", type);
279 httpHeaderPutStrf(&rep->header, type, "NTLM");
280 /* drop the connection */
281 httpHeaderDelByName(&rep->header, "keep-alive");
282 /* NTLM has problems if the initial connection is not dropped
283 * I haven't checked the RFC compliance of this hack - RBCollins */
284 request->flags.proxy_keepalive = 0;
285 } else {
286 ntlm_request = auth_user_request->scheme_data;
287 switch (ntlm_request->auth_state) {
288 case AUTHENTICATE_STATE_NONE:
289 debug(29, 9) ("authenticateNTLMFixErrorHeader: Sending type:%d header: 'NTLM'\n", type);
290 httpHeaderPutStrf(&rep->header, type, "NTLM");
291 /* drop the connection */
292 httpHeaderDelByName(&rep->header, "keep-alive");
293 /* NTLM has problems if the initial connection is not dropped
294 * I haven't checked the RFC compliance of this hack - RBCollins */
295 request->flags.proxy_keepalive = 0;
296 break;
297 case AUTHENTICATE_STATE_CHALLENGE:
298 /* we are 'waiting' for a response */
299 /* pass the challenge to the client */
300 debug(29, 9) ("authenticateNTLMFixErrorHeader: Sending type:%d header: 'NTLM %s'\n", type, ntlm_request->authchallenge);
301 httpHeaderPutStrf(&rep->header, type, "NTLM %s", ntlm_request->authchallenge);
302 break;
303 default:
304 debug(29, 0) ("authenticateNTLMFixErrorHeader: state %d.\n", ntlm_request->auth_state);
305 fatal("unexpected state in AuthenticateNTLMFixErrorHeader.\n");
306 }
307 }
308 }
309 }
310
311 void
312 authNTLMRequestFree(ntlm_request_t * ntlm_request)
313 {
314 if (!ntlm_request)
315 return;
316 if (ntlm_request->ntlmnegotiate)
317 xfree(ntlm_request->ntlmnegotiate);
318 if (ntlm_request->authchallenge)
319 xfree(ntlm_request->authchallenge);
320 if (ntlm_request->ntlmauthenticate)
321 xfree(ntlm_request->ntlmauthenticate);
322 memPoolFree(ntlm_request_pool, ntlm_request);
323 }
324
325 void
326 authNTLMAURequestFree(auth_user_request_t * auth_user_request)
327 {
328 if (auth_user_request->scheme_data)
329 authNTLMRequestFree((ntlm_request_t *) auth_user_request->scheme_data);
330 auth_user_request->scheme_data = NULL;
331 }
332
333 void
334 authenticateNTLMFreeUser(auth_user_t * auth_user)
335 {
336 dlink_node *link, *tmplink;
337 ntlm_user_t *ntlm_user = auth_user->scheme_data;
338 auth_user_hash_pointer *proxy_auth_hash;
339
340 debug(29, 5) ("authenticateNTLMFreeUser: Clearing NTLM scheme data\n");
341 if (ntlm_user->username)
342 xfree(ntlm_user->username);
343 /* were they linked in by one or more proxy-authenticate headers */
344 link = ntlm_user->proxy_auth_list.head;
345 while (link) {
346 debug(29, 9) ("authenticateFreeProxyAuthUser: removing proxy_auth hash entry '%d'\n", link->data);
347 proxy_auth_hash = link->data;
348 tmplink = link;
349 link = link->next;
350 dlinkDelete(tmplink, &ntlm_user->proxy_auth_list);
351 hash_remove_link(proxy_auth_cache, (hash_link *) proxy_auth_hash);
352 /* free the key (usually the proxy_auth header) */
353 xfree(proxy_auth_hash->key);
354 memFree(proxy_auth_hash, MEM_AUTH_USER_HASH);
355 }
356 memPoolFree(ntlm_user_pool, ntlm_user);
357 auth_user->scheme_data = NULL;
358 }
359
360 static stateful_helper_callback_t
361 authenticateNTLMHandleplaceholder(void *data, void *lastserver, char *reply)
362 {
363 authenticateStateData *r = data;
364 stateful_helper_callback_t result = S_HELPER_UNKNOWN;
365 int valid;
366 /* we should only be called for placeholder requests - which have no reply string */
367 assert(reply == NULL);
368 assert(r->auth_user_request);
369 /* standard callback stuff */
370 valid = cbdataValid(r->data);
371 cbdataUnlock(r->data);
372 /* call authenticateNTLMStart to retry this request */
373 debug(29, 9) ("authenticateNTLMHandleplaceholder: calling authenticateNTLMStart\n");
374 authenticateNTLMStart(r->auth_user_request, r->handler, r->data);
375 authenticateStateFree(r);
376 return result;
377 }
378
379 static stateful_helper_callback_t
380 authenticateNTLMHandleReply(void *data, void *lastserver, char *reply)
381 {
382 #if 0
383 authenticateStatefulStateData *r = data;
384 #endif
385 authenticateStateData *r = data;
386 ntlm_helper_state_t *helperstate;
387 int valid;
388 stateful_helper_callback_t result = S_HELPER_UNKNOWN;
389 #if 0
390 void *nextserver = NULL;
391 #endif
392 char *t = NULL;
393 auth_user_request_t *auth_user_request;
394 auth_user_t *auth_user;
395 ntlm_user_t *ntlm_user;
396 ntlm_request_t *ntlm_request;
397 debug(29, 9) ("authenticateNTLMHandleReply: Helper: '%d' {%s}\n", lastserver, reply ? reply : "<NULL>");
398 valid = cbdataValid(r->data);
399 cbdataUnlock(r->data);
400 if (valid) {
401 if (reply) {
402 /* seperate out the useful data */
403 if (strncasecmp(reply, "TT ", 3) == 0) {
404 reply += 3;
405 /* we have been given a Challenge */
406 /* we should check we weren't given an empty challenge */
407 #if 0
408 result = S_HELPER_RESERVE;
409 #endif
410 /* copy the challenge to the state data */
411 helperstate = helperStatefulServerGetData(lastserver);
412 if (helperstate == NULL)
413 fatal("lost NTLm helper state! quitting\n");
414 helperstate->challenge = xstrndup(reply, NTLM_CHALLENGE_SZ + 5);
415 helperstate->challengeuses = 0;
416 helperstate->renewed = squid_curtime;
417 /* and we satisfy the request that happended on the refresh boundary */
418 /* note this code is now in two places FIXME */
419 assert(r->auth_user_request != NULL);
420 assert(r->auth_user_request->auth_user->auth_type == AUTH_NTLM);
421 auth_user_request = r->auth_user_request;
422 ntlm_request = auth_user_request->scheme_data;
423 assert(ntlm_request != NULL);
424 result = S_HELPER_DEFER;
425 #if 0
426 nextserver = lastserver;
427 #endif
428 debug(29, 9) ("authenticateNTLMHandleReply: helper '%d'\n", lastserver);
429 assert(ntlm_request->auth_state == AUTHENTICATE_STATE_NEGOTIATE);
430 // auth_user->auth_data.ntlm_auth.auth_state = AUTHENTICATE_STATE_CHALLENGE;
431 ntlm_request->authhelper = lastserver;
432 ntlm_request->authchallenge = xstrndup(reply, NTLM_CHALLENGE_SZ + 5);
433 } else if (strncasecmp(reply, "AF ", 3) == 0) {
434 /* we're finished, release the helper */
435 reply += 3;
436 assert(r->auth_user_request != NULL);
437 assert(r->auth_user_request->auth_user->auth_type == AUTH_NTLM);
438 auth_user_request = r->auth_user_request;
439 assert(auth_user_request->scheme_data != NULL);
440 ntlm_request = auth_user_request->scheme_data;
441 auth_user = auth_user_request->auth_user;
442 ntlm_user = auth_user_request->auth_user->scheme_data;
443 assert(ntlm_user != NULL);
444 result = S_HELPER_RELEASE;
445 /* we only expect OK when finishing the handshake */
446 assert(ntlm_request->auth_state == AUTHENTICATE_STATE_RESPONSE);
447 ntlm_user->username = xstrndup(reply, MAX_LOGIN_SZ);
448 ntlm_request->authhelper = NULL;
449 auth_user->flags.credentials_ok = 1; /* login ok */
450 } else if (strncasecmp(reply, "NA ", 3) == 0) {
451 /* TODO: only work with auth_user here if it exists */
452 assert(r->auth_user_request != NULL);
453 assert(r->auth_user_request->auth_user->auth_type == AUTH_NTLM);
454 auth_user_request = r->auth_user_request;
455 auth_user = auth_user_request->auth_user;
456 assert(auth_user != NULL);
457 ntlm_user = auth_user->scheme_data;
458 ntlm_request = auth_user_request->scheme_data;
459 assert((ntlm_user != NULL) && (ntlm_request != NULL));
460 /* todo: action of Negotiate state on error */
461 result = S_HELPER_RELEASE; /*some error has occured. no more requests */
462 ntlm_request->authhelper = NULL;
463 auth_user->flags.credentials_ok = 2; /* Login/Usercode failed */
464 debug(29, 4) ("authenticateNTLMHandleReply: Error validating user via NTLM.\n");
465 ntlm_request->auth_state = AUTHENTICATE_STATE_NONE;
466 if ((t = strchr(reply, ' '))) /* strip after a space */
467 *t = '\0';
468 } else if (strncasecmp(reply, "BH ", 3) == 0) {
469 /* TODO kick off a refresh process. This can occur after a YR or after
470 * a KK. If after a YR release the helper and resubmit the request via
471 * Authenticate NTLM start.
472 * If after a KK deny the user's request w/ 407 and mark the helper as
473 * Needing YR. */
474 assert(r->auth_user_request != NULL);
475 assert(r->auth_user_request->auth_user->auth_type == AUTH_NTLM);
476 auth_user_request = r->auth_user_request;
477 auth_user = auth_user_request->auth_user;
478 assert(auth_user != NULL);
479 ntlm_user = auth_user->scheme_data;
480 ntlm_request = auth_user_request->scheme_data;
481 assert((ntlm_user != NULL) && (ntlm_request != NULL));
482 result = S_HELPER_RELEASE; /*some error has occured. no more requests for
483 * this helper */
484 helperstate = helperStatefulServerGetData(ntlm_request->authhelper);
485 ntlm_request->authhelper = NULL;
486 if (ntlm_request->auth_state == AUTHENTICATE_STATE_NEGOTIATE) {
487 /* The helper broke on YR. It automatically
488 * resets */
489 auth_user->flags.credentials_ok = 3; /* cannot process */
490 debug(29, 1) ("authenticateNTLMHandleReply: Error obtaining challenge from helper: %d.\n", lastserver);
491 /* mark it for starving */
492 helperstate->starve = 1;
493 /* resubmit the request. This helper is currently busy, so we will get
494 * a different one. */
495 authenticateNTLMStart(auth_user_request, r->handler, r->data);
496 } else {
497 /* the helper broke on a KK */
498 /* first the standard KK stuff */
499 auth_user->flags.credentials_ok = 2; /* Login/Usercode failed */
500 debug(29, 4) ("authenticateNTLMHandleReply: Error validating user via NTLM.\n");
501 ntlm_request->auth_state = AUTHENTICATE_STATE_NONE;
502 if ((t = strchr(reply, ' '))) /* strip after a space */
503 *t = '\0';
504 /* now we mark the helper for resetting. */
505 helperstate->starve = 1;
506 }
507 ntlm_request->auth_state = AUTHENTICATE_STATE_NONE;
508 } else {
509 /* TODO: only work with auth_user here if it exists */
510 assert(r->auth_user_request != NULL);
511 assert(r->auth_user_request->auth_user->auth_type == AUTH_NTLM);
512 auth_user_request = r->auth_user_request;
513 auth_user = auth_user_request->auth_user;
514 assert(auth_user != NULL);
515 ntlm_user = auth_user->scheme_data;
516 ntlm_request = auth_user_request->scheme_data;
517 assert((ntlm_user != NULL) && (ntlm_request != NULL));
518 debug(29, 1) ("authenticateNTLMHandleReply: Unsupported helper response, '%s'\n", reply);
519 /* restart the authentication process */
520 ntlm_request->auth_state = AUTHENTICATE_STATE_NONE;
521 auth_user->flags.credentials_ok = 3; /* cannot process */
522 ntlm_request->authhelper = NULL;
523 }
524 } else {
525 fatal("authenticateNTLMHandleReply: called with no result string\n");
526 }
527 r->handler(r->data, NULL);
528 } else {
529 debug(29, 1) ("AuthenticateNTLMHandleReply: invalid callback data. Releasing helper '%d'.\n", lastserver);
530 result = S_HELPER_RELEASE;
531 }
532 authenticateStateFree(r);
533 debug(29, 9) ("NTLM HandleReply, telling stateful helper : %d\n", result);
534 return result;
535 }
536
537 #if 0
538 static void
539 authenticateNTLMStateFree(authenticateNTLMStateData * r)
540 {
541 cbdataFree(r);
542 }
543
544 #endif
545
546 static void
547 authenticateNTLMStats(StoreEntry * sentry)
548 {
549 storeAppendPrintf(sentry, "NTLM Authenticator Statistics:\n");
550 helperStatefulStats(sentry, ntlmauthenticators);
551 }
552
553 /* is a particular challenge still valid ? */
554 int
555 authenticateNTLMValidChallenge(ntlm_helper_state_t * helperstate)
556 {
557 if (helperstate->challenge == NULL)
558 return 0;
559 return 1;
560 }
561
562 /* does our policy call for changing the challenge now? */
563 int
564 authenticateNTLMChangeChallenge(ntlm_helper_state_t * helperstate)
565 {
566 /* don't check for invalid challenges just for expiry choices */
567 /* this is needed because we have to starve the helper until all old
568 * requests have been satisfied */
569 if (helperstate->challengeuses > ntlmConfig->challengeuses)
570 return 1;
571 if (helperstate->renewed + ntlmConfig->challengelifetime >= squid_curtime)
572 return 1;
573 return 0;
574 }
575
576 /* send the initial data to a stateful ntlm authenticator module */
577 static void
578 authenticateNTLMStart(auth_user_request_t * auth_user_request, RH * handler, void *data)
579 {
580 #if 0
581 authenticateStatefulStateData *r = NULL;
582 #endif
583 authenticateStateData *r = NULL;
584 helper_stateful_server *server;
585 ntlm_helper_state_t *helperstate;
586 char buf[8192];
587 char *sent_string = NULL;
588 ntlm_user_t *ntlm_user;
589 ntlm_request_t *ntlm_request;
590 auth_user_t *auth_user;
591
592 assert(auth_user_request);
593 auth_user = auth_user_request->auth_user;
594 ntlm_user = auth_user->scheme_data;
595 ntlm_request = auth_user_request->scheme_data;
596 assert(ntlm_user);
597 assert(ntlm_request);
598 assert(handler);
599 assert(data);
600 assert(auth_user->auth_type = AUTH_NTLM);
601 debug(29, 9) ("authenticateNTLMStart: auth state '%d'\n", ntlm_request->auth_state);
602 switch (ntlm_request->auth_state) {
603 case AUTHENTICATE_STATE_NEGOTIATE:
604 sent_string = xstrdup(ntlm_request->ntlmnegotiate);
605 break;
606 case AUTHENTICATE_STATE_RESPONSE:
607 sent_string = xstrdup(ntlm_request->ntlmauthenticate);
608 assert(ntlm_request->authhelper);
609 debug(29, 9) ("authenticateNTLMStart: Asking NTLMauthenticator '%d'.\n", ntlm_request->authhelper);
610 break;
611 default:
612 fatal("Invalid authenticate state for NTLMStart");
613 }
614
615 while (!xisspace(*sent_string)) /*trim NTLM */
616 sent_string++;
617
618 while (xisspace(*sent_string)) /*trim leading spaces */
619 sent_string++;
620
621 debug(29, 9) ("authenticateNTLMStart: state '%d'\n", ntlm_request->auth_state);
622 debug(29, 9) ("authenticateNTLMStart: '%s'\n", sent_string);
623 if (ntlmConfig->authenticate == NULL) {
624 debug(29, 0) ("authenticateNTLMStart: no NTLM program specified:'%s'\n", sent_string);
625 // handler(data,0, NULL);
626 handler(data, NULL);
627 return;
628 }
629 #ifdef NTLMHELPPROTOCOLV2
630 r = CBDATA_ALLOC(authenticateStateData, NULL);
631 r->handler = handler;
632 cbdataLock(data);
633 r->data = data;
634 r->auth_user_request = auth_user_request;
635 snprintf(buf, 8192, "%s\n", sent_string);
636 helperStatefulSubmit(ntlmauthenticators, buf, authenticateNTLMHandleReply, r, ntlm_request->authhelper);
637 debug(29, 9) ("authenticateNTLMstart: finished\n");
638 #else
639 /* this is ugly TODO: move the challenge generation routines to their own function and
640 * tidy the logic up to make use of the efficiency we now have */
641 switch (ntlm_request->auth_state) {
642 case AUTHENTICATE_STATE_NEGOTIATE:
643 /*
644 * 1: get a helper server
645 * 2: does it have a challenge?
646 * 3: tell it to get a challenge, or give ntlmauthdone the challenge
647 */
648 server = helperStatefulDefer(ntlmauthenticators);
649 helperstate = server ? helperStatefulServerGetData(server) : NULL;
650 while ((server != NULL) &&
651 authenticateNTLMChangeChallenge(helperstate)) {
652 /* flag this helper for challenge changing */
653 helperstate->starve = 1;
654 /* and release the deferred request */
655 helperStatefulReleaseServer(server);
656 server = helperStatefulDefer(ntlmauthenticators);
657 if (server != NULL)
658 helperstate = helperStatefulServerGetData(server);
659 }
660 if (server == NULL)
661 debug(29, 9) ("unable to get a deferred ntlm helper... all helpers are refreshing challenges. Queuing as a placeholder request.\n");
662
663 ntlm_request->authhelper = server;
664 /* tell the log what helper we have been given */
665 debug(29, 9) ("authenticateNTLMStart: helper '%d' assigned\n", server);
666 /* valid challenge? */
667 if ((server == NULL) || !authenticateNTLMValidChallenge(helperstate)) {
668 r = CBDATA_ALLOC(authenticateStateData, NULL);
669 r->handler = handler;
670 cbdataLock(data);
671 r->data = data;
672 r->auth_user_request = auth_user_request;
673 if (server == NULL) {
674 helperStatefulSubmit(ntlmauthenticators, NULL, authenticateNTLMHandleplaceholder, r, ntlm_request->authhelper);
675 } else {
676 snprintf(buf, 8192, "YR\n");
677 helperStatefulSubmit(ntlmauthenticators, buf, authenticateNTLMHandleReply, r, ntlm_request->authhelper);
678 }
679 } else {
680 /* we have a valid challenge */
681 /* TODO: turn the below into a function and call from here and handlereply */
682 /* increment the challenge uses */
683 helperstate->challengeuses++;
684 /* assign the challenge */
685 ntlm_request->authchallenge =
686 xstrndup(helperstate->challenge, NTLM_CHALLENGE_SZ + 5);
687 handler(data, NULL);
688 }
689
690 break;
691 case AUTHENTICATE_STATE_RESPONSE:
692 r = CBDATA_ALLOC(authenticateStateData, NULL);
693 r->handler = handler;
694 cbdataLock(data);
695 r->data = data;
696 r->auth_user_request = auth_user_request;
697 snprintf(buf, 8192, "KK %s\n", sent_string);
698 helperStatefulSubmit(ntlmauthenticators, buf, authenticateNTLMHandleReply, r, ntlm_request->authhelper);
699 debug(29, 9) ("authenticateNTLMstart: finished\n");
700 break;
701 default:
702 fatal("Invalid authenticate state for NTLMStart");
703 }
704 #endif
705 }
706
707 /* callback used by stateful helper routines */
708 int
709 authenticateNTLMHelperServerAvailable(void *data)
710 {
711 ntlm_helper_state_t *statedata = data;
712 if (statedata != NULL) {
713 if (statedata->starve) {
714 debug(29, 4) ("authenticateNTLMHelperServerAvailable: starving - returning 0\n");
715 return 0;
716 } else {
717 debug(29, 4) ("authenticateNTLMHelperServerAvailable: not starving - returning 1\n");
718 return 1;
719 }
720 }
721 debug(29, 4) ("authenticateNTLMHelperServerAvailable: no state data - returning 0\n");
722 return 0;
723 }
724
725 void
726 authenticateNTLMHelperServerOnEmpty(void *data)
727 {
728 ntlm_helper_state_t *statedata = data;
729 if (statedata == NULL)
730 return;
731 if (statedata->starve) {
732 /* we have been starving the helper */
733 debug(29, 9) ("authenticateNTLMHelperServerOnEmpty: resetting challenge details\n");
734 statedata->starve = 0;
735 statedata->challengeuses = 0;
736 statedata->renewed = 0;
737 xfree(statedata->challenge);
738 statedata->challenge = NULL;
739 }
740 }
741
742
743 /* clear the NTLM helper of being reserved for future requests */
744 void
745 authenticateNTLMReleasehelper(auth_user_request_t * auth_user_request)
746 {
747 ntlm_request_t *ntlm_request;
748 assert(auth_user_request->auth_user->auth_type == AUTH_NTLM);
749 assert(auth_user_request->scheme_data != NULL);
750 ntlm_request = auth_user_request->scheme_data;
751 debug(29, 9) ("authenticateNTLMReleasehelper: releasing helper '%d'\n", ntlm_request->authhelper);
752 helperStatefulReleaseServer(ntlm_request->authhelper);
753 ntlm_request->authhelper = NULL;
754 }
755
756 /* clear any connection related authentication details */
757 void
758 authenticateNTLMOnCloseConnection(ConnStateData * conn)
759 {
760 ntlm_request_t *ntlm_request;
761 assert(conn != NULL);
762 if (conn->auth_user_request != NULL) {
763 assert(conn->auth_user_request->scheme_data != NULL);
764 ntlm_request = conn->auth_user_request->scheme_data;
765 if (ntlm_request->authhelper != NULL)
766 authenticateNTLMReleasehelper(conn->auth_user_request);
767 /* unlock the connection based lock */
768 debug(29, 9) ("authenticateNTLMOnCloseConnection: Unlocking auth_user from the connection.\n");
769 authenticateAuthUserRequestUnlock(conn->auth_user_request);
770 conn->auth_user_request = NULL;
771 }
772 }
773
774 /* authenticateUserUsername: return a pointer to the username in the */
775 char *
776 authenticateNTLMUsername(auth_user_t * auth_user)
777 {
778 ntlm_user_t *ntlm_user = auth_user->scheme_data;
779 if (ntlm_user)
780 return ntlm_user->username;
781 return NULL;
782 }
783
784
785 /*
786 * Decode an NTLM [Proxy-]Auth string, placing the results in the passed
787 * Auth_user structure.
788 */
789
790 void
791 authenticateDecodeNTLMAuth(auth_user_request_t * auth_user_request, const char *proxy_auth)
792 {
793 dlink_node *node;
794 assert(auth_user_request->auth_user == NULL);
795 auth_user_request->auth_user = authenticateAuthUserNew("ntlm");
796 auth_user_request->auth_user->auth_type = AUTH_NTLM;
797 auth_user_request->auth_user->scheme_data = memPoolAlloc(ntlm_user_pool);
798 auth_user_request->scheme_data = memPoolAlloc(ntlm_request_pool);
799 /* lock for the auth_user_request link */
800 authenticateAuthUserLock(auth_user_request->auth_user);
801 node = dlinkNodeNew();
802 dlinkAdd(auth_user_request, node, &auth_user_request->auth_user->requests);
803
804 /* all we have to do is identify that it's NTLM - the helper does the rest */
805 debug(29, 9) ("authenticateDecodeNTLMAuth: NTLM authentication\n");
806 return;
807 }
808
809 int
810 authenticateNTLMcmpUsername(ntlm_user_t * u1, ntlm_user_t * u2)
811 {
812 return strcmp(u1->username, u2->username);
813 }
814
815 void
816 authenticateProxyAuthCacheAddLink(const char *key, auth_user_t * auth_user)
817 {
818 auth_user_hash_pointer *proxy_auth_hash;
819 ntlm_user_t *ntlm_user;
820 proxy_auth_hash =
821 memAllocate(MEM_AUTH_USER_HASH);
822 proxy_auth_hash->key = xstrdup(key);
823 proxy_auth_hash->auth_user = auth_user;
824 ntlm_user = auth_user->scheme_data;
825 dlinkAddTail(proxy_auth_hash, &proxy_auth_hash->link,
826 &ntlm_user->proxy_auth_list);
827 hash_join(proxy_auth_cache, (hash_link *) proxy_auth_hash);
828 }
829
830
831 int
832 authNTLMAuthenticated(auth_user_request_t * auth_user_request)
833 {
834 ntlm_request_t *ntlm_request = auth_user_request->scheme_data;
835 if (ntlm_request->auth_state == AUTHENTICATE_STATE_DONE)
836 return 1;
837 debug(29, 9) ("User not fully authenticated.\n");
838 return 0;
839 }
840
841 #if 0
842 static acl_proxy_auth_user *
843 authenticateNTLMAuthenticateUser(void *data, const char *proxy_auth, ConnStateData * conn)
844 #else
845 static void
846 authenticateNTLMAuthenticateUser(auth_user_request_t * auth_user_request, request_t * request, ConnStateData * conn, http_hdr_type type)
847 #endif
848 {
849 const char *proxy_auth;
850 auth_user_hash_pointer *usernamehash, *proxy_auth_hash = NULL;
851 auth_user_t *auth_user;
852 ntlm_request_t *ntlm_request;
853 ntlm_user_t *ntlm_user;
854 LOCAL_ARRAY(char, ntlmhash, NTLM_CHALLENGE_SZ * 2);
855 /* get header */
856 proxy_auth = httpHeaderGetStr(&request->header, type);
857
858 auth_user = auth_user_request->auth_user;
859 assert(auth_user);
860 assert(auth_user->auth_type == AUTH_NTLM);
861 assert(auth_user->scheme_data != NULL);
862 assert(auth_user_request->scheme_data != NULL);
863 ntlm_user = auth_user->scheme_data;
864 ntlm_request = auth_user_request->scheme_data;
865 switch (ntlm_request->auth_state) {
866 case AUTHENTICATE_STATE_NONE:
867 /* we've recieved a negotiate request. pass to a helper */
868 debug(29, 9) ("authenticateNTLMAuthenticateUser: auth state ntlm none. %s\n",
869 proxy_auth);
870 ntlm_request->auth_state = AUTHENTICATE_STATE_NEGOTIATE;
871 ntlm_request->ntlmnegotiate = xstrndup(proxy_auth, NTLM_CHALLENGE_SZ + 5);
872 conn->auth_type = AUTH_NTLM;
873 conn->auth_user_request = auth_user_request;
874 /* and lock for the connection duration */
875 debug(29, 9) ("authenticateNTLMAuthenticateUser: Locking auth_user from the connection.\n");
876 authenticateAuthUserRequestLock(auth_user_request);
877 return;
878 break;
879 case AUTHENTICATE_STATE_NEGOTIATE:
880 ntlm_request->auth_state = AUTHENTICATE_STATE_CHALLENGE;
881 return;
882 break;
883 case AUTHENTICATE_STATE_CHALLENGE:
884 /* we should have recieved a NTLM challenge. pass it to the same
885 * helper process */
886 debug(29, 9) ("authenticateNTLMAuthenticateUser: auth state challenge with header %s.\n", proxy_auth);
887 /* do a cache lookup here. If it matches it's a successful ntlm
888 * challenge - release the helper and use the existing auth_user
889 * details. */
890 if (strncmp("NTLM ", proxy_auth, 5) == 0) {
891 ntlm_request->ntlmauthenticate = xstrdup(proxy_auth);
892 } else {
893 fatal("Incorrect scheme in auth header\n");
894 /* TODO: more fault tolerance.. reset the auth scheme here */
895 }
896 /* cache entries have authenticateauthheaderchallengestring */
897 snprintf(ntlmhash, sizeof(ntlmhash) - 1, "%s%s",
898 ntlm_request->ntlmauthenticate,
899 ntlm_request->authchallenge);
900 /* see if we already know this user's authenticate */
901 debug(29, 9) ("aclMatchProxyAuth: cache lookup with key '%s'\n", ntlmhash);
902 assert(proxy_auth_cache != NULL);
903 proxy_auth_hash = hash_lookup(proxy_auth_cache, ntlmhash);
904 if (!proxy_auth_hash) { /* not in the hash table */
905 debug(29, 4) ("authenticateNTLMAuthenticateUser: proxy-auth cache miss.\n");
906 ntlm_request->auth_state = AUTHENTICATE_STATE_RESPONSE;
907 /* verify with the ntlm helper */
908 } else {
909 debug(29, 4) ("authenticateNTLMAuthenticateUser: ntlm proxy-auth cache hit\n");
910 /* throw away the temporary entry */
911 authenticateNTLMReleasehelper(auth_user_request);
912 authenticateAuthUserMerge(auth_user, proxy_auth_hash->auth_user);
913 auth_user = proxy_auth_hash->auth_user;
914 auth_user_request->auth_user = auth_user;
915 ntlm_request->auth_state = AUTHENTICATE_STATE_DONE;
916 /* we found one */
917 debug(29, 9) ("found matching cache entry\n");
918 assert(auth_user->auth_type == AUTH_NTLM);
919 /* get the existing entries details */
920 ntlm_user = auth_user->scheme_data;
921 debug(29, 9) ("Username to be used is %s\n",
922 ntlm_user->username);
923 auth_user->flags.credentials_ok = 1; /* authenticated ok */
924 /* on ntlm auth we do not unlock the auth_user until the
925 * connection is dropped. Thank MS for this quirk */
926 auth_user->expiretime = current_time.tv_sec;
927 auth_user->ip_expiretime = squid_curtime;
928 }
929 return;
930 break;
931 case AUTHENTICATE_STATE_RESPONSE:
932 /* auth-challenge pair cache miss. We've just got the response */
933 /*add to cache and let them through */
934 ntlm_request->auth_state = AUTHENTICATE_STATE_DONE;
935 /* this connection is authenticated */
936 debug(29, 4) ("authenticated\nch %s\nauth %s\nauthuser %s\n",
937 ntlm_request->authchallenge,
938 ntlm_request->ntlmauthenticate,
939 ntlm_user->username);
940 /* cache entries have authenticateauthheaderchallengestring */
941 snprintf(ntlmhash, sizeof(ntlmhash) - 1, "%s%s",
942 ntlm_request->ntlmauthenticate,
943 ntlm_request->authchallenge);
944 /* see if this is an existing user with a different proxy_auth
945 * string */
946 if ((usernamehash = hash_lookup(proxy_auth_username_cache,
947 ntlm_user->username))) {
948 while ((usernamehash->auth_user->auth_type !=
949 auth_user->auth_type) && (usernamehash->next) &&
950 !authenticateNTLMcmpUsername(usernamehash->auth_user->scheme_data, ntlm_user))
951 usernamehash = usernamehash->next;
952 if (usernamehash->auth_user->auth_type == auth_user->auth_type) {
953 /*
954 * add another link from the new proxy_auth to the
955 * auth_user structure and update the information */
956 assert(proxy_auth_hash == NULL);
957 authenticateProxyAuthCacheAddLink(ntlmhash, usernamehash->auth_user);
958 /* we can't seamlessly recheck the username due to the
959 * challenge nature of the protocol. Just free the
960 * temporary auth_user */
961 authenticateAuthUserMerge(auth_user, usernamehash->auth_user);
962 auth_user = usernamehash->auth_user;
963 auth_user_request->auth_user = auth_user;
964 #if 0
965 conn->auth_user = auth_user;
966 #endif
967 }
968 } else {
969 /* store user in hash's */
970 authenticateUserNameCacheAdd(auth_user);
971 authenticateProxyAuthCacheAddLink(ntlmhash, auth_user);
972 }
973 /* set these to now because this is either a new login from an
974 * existing user or a new user */
975 auth_user->expiretime = current_time.tv_sec;
976 auth_user->ip_expiretime = squid_curtime;
977 auth_user->flags.credentials_ok = 1; /*authenticated ok */
978 return;
979 break;
980 case AUTHENTICATE_STATE_DONE:
981 fatal("authenticateNTLMAuthenticateUser: unexpect auth state DONE! Report a bug to the squid developers.\n");
982 #if 0 /* done in acl.c */
983 case AUTHENTICATE_STATE_DONE:
984 debug(28, 5) ("aclMatchProxyAuth: connection in state Done. using connection credentials for the request. \n");
985 /* is it working right? */
986 assert(checklist->auth_user == NULL);
987 assert(checklist->conn->auth_user != NULL);
988 /* we have a valid username. */
989 auth_user = checklist->conn->auth_user;
990 /* store the username in the request for logging */
991 xstrncpy(checklist->request->authuser,
992 auth_user->auth_data.ntlm_auth.username,
993 USER_IDENT_SZ);
994 if (auth_user->expiretime + Config.authenticateTTL > current_time.tv_sec
995 ) {
996 auth_user->expiretime = current_time.tv_sec;
997 } else {
998 //user passed externa; authentication in every case to get here. f.
999 Let it through
1000 } /* we don't unlock the auth_user until the connection is dropped. Thank
1001 * MS for this quirk. */ if (authenticateCheckAuthUserIP(checklist->src_addr, auth_user)) {
1002 /* Once the match is completed we have finished with the
1003 * auth_user structure */
1004 /* check to see if we have matched the user-acl before */
1005 return aclCacheMatchAcl(&auth_user->proxy_match_cache, acltype,
1006 data, auth_user->auth_data.ntlm_auth.username);
1007 } else {
1008 return 0;
1009 }
1010 break;
1011 #endif
1012 }
1013
1014 return;
1015 }