3 * $Id: auth_ntlm.cc,v 1.1 2001/01/07 23:36:48 hno Exp $
5 * DEBUG: section 29 NTLM Authenticator
6 * AUTHOR: Robert Collins
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * ----------------------------------------------------------
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.
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.
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.
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.
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 */
42 #include "auth_ntlm.h"
45 authenticateStateFree(authenticateStateData
* r
)
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
;
71 /* helper callbacks to handle per server state data */
72 static HLPSAVAIL authenticateNTLMHelperServerAvailable
;
73 static HLPSONEQ authenticateNTLMHelperServerOnEmpty
;
75 static statefulhelper
*ntlmauthenticators
= NULL
;
77 CBDATA_TYPE(authenticateStateData
);
79 static int authntlm_initialised
= 0;
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
;
86 static hash_table
*proxy_auth_cache
= NULL
;
97 // memPoolDestroy(ufs_state_pool);
99 if (ntlmauthenticators
)
100 helperStatefulShutdown(ntlmauthenticators
);
101 authntlm_initialised
= 0;
104 helperStatefulFree(ntlmauthenticators
);
105 ntlmauthenticators
= NULL
;
106 memPoolDestroy(ntlm_helper_state_pool
);
107 memPoolDestroy(ntlm_request_pool
);
108 memPoolDestroy(ntlm_user_pool
);
112 /* free any allocated configuration details */
114 authNTLMFreeConfig(authScheme
* scheme
)
116 if (ntlmConfig
== NULL
)
118 assert(ntlmConfig
== scheme
->scheme_data
);
119 if (ntlmConfig
->authenticate
)
120 wordlistDestroy(&ntlmConfig
->authenticate
);
126 authNTLMCfgDump(StoreEntry
* entry
, const char *name
, authScheme
* scheme
)
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
);
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
);
143 authNTLMParse(authScheme
* scheme
, int n_configured
, char *param_str
)
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;
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
);
166 debug(28, 0) ("unrecognised ntlm auth scheme parameter '%s'\n", param_str
);
172 authSchemeSetup_ntlm(authscheme_entry_t
* authscheme
)
175 static int ntlminit
= 0;
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
;
197 /* Initialize helpers and the like for this auth scheme. Called AFTER parsing the
200 authNTLMInit(authScheme
* scheme
)
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
));
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? */
226 cachemgrRegister("ntlmauthenticator",
227 "User NTLM Authenticator Stats",
228 authenticateNTLMStats
, 0, 1);
231 CBDATA_INIT_TYPE(authenticateStateData
);
236 authenticateNTLMActive()
238 if (authntlm_initialised
)
247 authenticateNTLMDirection(auth_user_request_t
* auth_user_request
)
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");
255 case AUTHENTICATE_STATE_NEGOTIATE
: /* send to helper */
256 case AUTHENTICATE_STATE_RESPONSE
: /*send to helper */
258 case AUTHENTICATE_STATE_CHALLENGE
: /* send to client */
260 case AUTHENTICATE_STATE_DONE
: /* do nothing.. */
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
272 authenticateNTLMFixErrorHeader(auth_user_request_t
* auth_user_request
, HttpReply
* rep
, http_hdr_type type
, request_t
* request
)
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;
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;
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
);
304 debug(29, 0) ("authenticateNTLMFixErrorHeader: state %d.\n", ntlm_request
->auth_state
);
305 fatal("unexpected state in AuthenticateNTLMFixErrorHeader.\n");
312 authNTLMRequestFree(ntlm_request_t
* ntlm_request
)
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
);
326 authNTLMAURequestFree(auth_user_request_t
* auth_user_request
)
328 if (auth_user_request
->scheme_data
)
329 authNTLMRequestFree((ntlm_request_t
*) auth_user_request
->scheme_data
);
330 auth_user_request
->scheme_data
= NULL
;
334 authenticateNTLMFreeUser(auth_user_t
* auth_user
)
336 dlink_node
*link
, *tmplink
;
337 ntlm_user_t
*ntlm_user
= auth_user
->scheme_data
;
338 auth_user_hash_pointer
*proxy_auth_hash
;
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
;
346 debug(29, 9) ("authenticateFreeProxyAuthUser: removing proxy_auth hash entry '%d'\n", link
->data
);
347 proxy_auth_hash
= link
->data
;
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
);
356 memPoolFree(ntlm_user_pool
, ntlm_user
);
357 auth_user
->scheme_data
= NULL
;
360 static stateful_helper_callback_t
361 authenticateNTLMHandleplaceholder(void *data
, void *lastserver
, char *reply
)
363 authenticateStateData
*r
= data
;
364 stateful_helper_callback_t result
= S_HELPER_UNKNOWN
;
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
);
379 static stateful_helper_callback_t
380 authenticateNTLMHandleReply(void *data
, void *lastserver
, char *reply
)
383 authenticateStatefulStateData
*r
= data
;
385 authenticateStateData
*r
= data
;
386 ntlm_helper_state_t
*helperstate
;
388 stateful_helper_callback_t result
= S_HELPER_UNKNOWN
;
390 void *nextserver
= 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
);
402 /* seperate out the useful data */
403 if (strncasecmp(reply
, "TT ", 3) == 0) {
405 /* we have been given a Challenge */
406 /* we should check we weren't given an empty challenge */
408 result
= S_HELPER_RESERVE
;
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
;
426 nextserver
= lastserver
;
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 */
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 */
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
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
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
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
);
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 */
504 /* now we mark the helper for resetting. */
505 helperstate
->starve
= 1;
507 ntlm_request
->auth_state
= AUTHENTICATE_STATE_NONE
;
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
;
525 fatal("authenticateNTLMHandleReply: called with no result string\n");
527 r
->handler(r
->data
, NULL
);
529 debug(29, 1) ("AuthenticateNTLMHandleReply: invalid callback data. Releasing helper '%d'.\n", lastserver
);
530 result
= S_HELPER_RELEASE
;
532 authenticateStateFree(r
);
533 debug(29, 9) ("NTLM HandleReply, telling stateful helper : %d\n", result
);
539 authenticateNTLMStateFree(authenticateNTLMStateData
* r
)
547 authenticateNTLMStats(StoreEntry
* sentry
)
549 storeAppendPrintf(sentry
, "NTLM Authenticator Statistics:\n");
550 helperStatefulStats(sentry
, ntlmauthenticators
);
553 /* is a particular challenge still valid ? */
555 authenticateNTLMValidChallenge(ntlm_helper_state_t
* helperstate
)
557 if (helperstate
->challenge
== NULL
)
562 /* does our policy call for changing the challenge now? */
564 authenticateNTLMChangeChallenge(ntlm_helper_state_t
* helperstate
)
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
)
571 if (helperstate
->renewed
+ ntlmConfig
->challengelifetime
>= squid_curtime
)
576 /* send the initial data to a stateful ntlm authenticator module */
578 authenticateNTLMStart(auth_user_request_t
* auth_user_request
, RH
* handler
, void *data
)
581 authenticateStatefulStateData
*r
= NULL
;
583 authenticateStateData
*r
= NULL
;
584 helper_stateful_server
*server
;
585 ntlm_helper_state_t
*helperstate
;
587 char *sent_string
= NULL
;
588 ntlm_user_t
*ntlm_user
;
589 ntlm_request_t
*ntlm_request
;
590 auth_user_t
*auth_user
;
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
;
597 assert(ntlm_request
);
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
);
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
);
612 fatal("Invalid authenticate state for NTLMStart");
615 while (!xisspace(*sent_string
)) /*trim NTLM */
618 while (xisspace(*sent_string
)) /*trim leading spaces */
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);
629 #ifdef NTLMHELPPROTOCOLV2
630 r
= CBDATA_ALLOC(authenticateStateData
, NULL
);
631 r
->handler
= handler
;
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");
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
:
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
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
);
658 helperstate
= helperStatefulServerGetData(server
);
661 debug(29, 9) ("unable to get a deferred ntlm helper... all helpers are refreshing challenges. Queuing as a placeholder request.\n");
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
;
672 r
->auth_user_request
= auth_user_request
;
673 if (server
== NULL
) {
674 helperStatefulSubmit(ntlmauthenticators
, NULL
, authenticateNTLMHandleplaceholder
, r
, ntlm_request
->authhelper
);
676 snprintf(buf
, 8192, "YR\n");
677 helperStatefulSubmit(ntlmauthenticators
, buf
, authenticateNTLMHandleReply
, r
, ntlm_request
->authhelper
);
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);
691 case AUTHENTICATE_STATE_RESPONSE
:
692 r
= CBDATA_ALLOC(authenticateStateData
, NULL
);
693 r
->handler
= handler
;
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");
702 fatal("Invalid authenticate state for NTLMStart");
707 /* callback used by stateful helper routines */
709 authenticateNTLMHelperServerAvailable(void *data
)
711 ntlm_helper_state_t
*statedata
= data
;
712 if (statedata
!= NULL
) {
713 if (statedata
->starve
) {
714 debug(29, 4) ("authenticateNTLMHelperServerAvailable: starving - returning 0\n");
717 debug(29, 4) ("authenticateNTLMHelperServerAvailable: not starving - returning 1\n");
721 debug(29, 4) ("authenticateNTLMHelperServerAvailable: no state data - returning 0\n");
726 authenticateNTLMHelperServerOnEmpty(void *data
)
728 ntlm_helper_state_t
*statedata
= data
;
729 if (statedata
== NULL
)
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
;
743 /* clear the NTLM helper of being reserved for future requests */
745 authenticateNTLMReleasehelper(auth_user_request_t
* auth_user_request
)
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
;
756 /* clear any connection related authentication details */
758 authenticateNTLMOnCloseConnection(ConnStateData
* conn
)
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
;
774 /* authenticateUserUsername: return a pointer to the username in the */
776 authenticateNTLMUsername(auth_user_t
* auth_user
)
778 ntlm_user_t
*ntlm_user
= auth_user
->scheme_data
;
780 return ntlm_user
->username
;
786 * Decode an NTLM [Proxy-]Auth string, placing the results in the passed
787 * Auth_user structure.
791 authenticateDecodeNTLMAuth(auth_user_request_t
* auth_user_request
, const char *proxy_auth
)
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
);
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");
810 authenticateNTLMcmpUsername(ntlm_user_t
* u1
, ntlm_user_t
* u2
)
812 return strcmp(u1
->username
, u2
->username
);
816 authenticateProxyAuthCacheAddLink(const char *key
, auth_user_t
* auth_user
)
818 auth_user_hash_pointer
*proxy_auth_hash
;
819 ntlm_user_t
*ntlm_user
;
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
);
832 authNTLMAuthenticated(auth_user_request_t
* auth_user_request
)
834 ntlm_request_t
*ntlm_request
= auth_user_request
->scheme_data
;
835 if (ntlm_request
->auth_state
== AUTHENTICATE_STATE_DONE
)
837 debug(29, 9) ("User not fully authenticated.\n");
842 static acl_proxy_auth_user
*
843 authenticateNTLMAuthenticateUser(void *data
, const char *proxy_auth
, ConnStateData
* conn
)
846 authenticateNTLMAuthenticateUser(auth_user_request_t
* auth_user_request
, request_t
* request
, ConnStateData
* conn
, http_hdr_type type
)
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);
856 proxy_auth
= httpHeaderGetStr(&request
->header
, type
);
858 auth_user
= auth_user_request
->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",
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
);
879 case AUTHENTICATE_STATE_NEGOTIATE
:
880 ntlm_request
->auth_state
= AUTHENTICATE_STATE_CHALLENGE
;
883 case AUTHENTICATE_STATE_CHALLENGE
:
884 /* we should have recieved a NTLM challenge. pass it to the same
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
890 if (strncmp("NTLM ", proxy_auth
, 5) == 0) {
891 ntlm_request
->ntlmauthenticate
= xstrdup(proxy_auth
);
893 fatal("Incorrect scheme in auth header\n");
894 /* TODO: more fault tolerance.. reset the auth scheme here */
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 */
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
;
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
;
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
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
) {
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
;
965 conn
->auth_user
= auth_user
;
969 /* store user in hash's */
970 authenticateUserNameCacheAdd(auth_user
);
971 authenticateProxyAuthCacheAddLink(ntlmhash
, auth_user
);
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 */
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
,
994 if (auth_user
->expiretime
+ Config
.authenticateTTL
> current_time
.tv_sec
996 auth_user
->expiretime
= current_time
.tv_sec
;
998 //user passed externa; authentication in every case to get here. f.
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
);