]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/auth/ntlm/auth_ntlm.cc
Cleanup: zap CVS Id tags
[thirdparty/squid.git] / src / auth / ntlm / auth_ntlm.cc
index 33c77928aa469f5f9bf577b324e973213cb945b1..6b0caeb363da4df8df745fdeb0e3d565991676e2 100644 (file)
@@ -1,9 +1,8 @@
-
 /*
- * $Id: auth_ntlm.cc,v 1.44 2004/12/20 16:30:40 robertc Exp $
+ * $Id$
  *
  * DEBUG: section 29    NTLM Authenticator
- * AUTHOR: Robert Collins
+ * AUTHOR: Robert Collins, Henrik Nordstrom, Francesco Chemolli
  *
  * SQUID Web Proxy Cache          http://www.squid-cache.org/
  * ----------------------------------------------------------
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
  *  (at your option) any later version.
- *  
+ *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
- *  
+ *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 #include "squid.h"
 #include "auth_ntlm.h"
 #include "authenticate.h"
+#include "CacheManager.h"
 #include "Store.h"
 #include "client_side.h"
 #include "HttpReply.h"
 #include "HttpRequest.h"
 /* TODO remove this include */
 #include "ntlmScheme.h"
+#include "wordlist.h"
+#include "SquidTime.h"
+
+static void
+authenticateNTLMReleaseServer(AuthUserRequest * auth_user_request);
+
 
 static void
 authenticateStateFree(authenticateStateData * r)
 {
+    AUTHUSERREQUESTUNLOCK(r->auth_user_request, "r");
     cbdataFree(r);
 }
 
 /* NTLM Scheme */
 static HLPSCB authenticateNTLMHandleReply;
-static HLPSCB authenticateNTLMHandleplaceholder;
 static AUTHSSTATS authenticateNTLMStats;
 
-/* helper callbacks to handle per server state data */
-static HLPSAVAIL authenticateNTLMHelperServerAvailable;
-static HLPSONEQ authenticateNTLMHelperServerOnEmpty;
-
 static statefulhelper *ntlmauthenticators = NULL;
 
 CBDATA_TYPE(authenticateStateData);
 
 static int authntlm_initialised = 0;
 
-static MemAllocatorProxy *ntlm_helper_state_pool = NULL;
-static MemAllocatorProxy *ntlm_user_hash_pool = NULL;
-
 static auth_ntlm_config ntlmConfig;
 
 static hash_table *proxy_auth_cache = NULL;
@@ -87,7 +86,7 @@ void
 ntlmScheme::done()
 {
     /* TODO: this should be a Config call. */
-    debug(29, 2) ("authNTLMDone: shutting down NTLM authentication.\n");
+    debugs(29, 2, "ntlmScheme::done: shutting down NTLM authentication.");
 
     if (ntlmauthenticators)
         helperStatefulShutdown(ntlmauthenticators);
@@ -102,21 +101,7 @@ ntlmScheme::done()
 
     ntlmauthenticators = NULL;
 
-#if DEBUGSHUTDOWN
-
-    if (ntlm_helper_state_pool) {
-        delete ntlm_helper_state_pool;
-        ntlm_helper_state_pool = NULL;
-    }
-
-    /* Removed for some reason..
-        if (ntlm_user_pool) {
-       delete ntlm_user_pool;ntlm_user_pool = NULL;
-        }
-        */
-
-#endif
-    debug(29, 2) ("authNTLMDone: NTLM authentication Shutdown.\n");
+    debugs(29, 2, "ntlmScheme::done: NTLM authentication Shutdown.");
 }
 
 /* free any allocated configuration details */
@@ -138,20 +123,14 @@ AuthNTLMConfig::dump(StoreEntry * entry, const char *name, AuthConfig * scheme)
         list = list->next;
     }
 
-    storeAppendPrintf(entry, "\n%s %s children %d\n%s %s max_challenge_reuses %d\n%s %s max_challenge_lifetime %d seconds\n",
-                      name, "ntlm", authenticateChildren,
-                      name, "ntlm", challengeuses,
-                      name, "ntlm", (int) challengelifetime);
+    storeAppendPrintf(entry, "\n%s ntlm children %d\n",
+                      name, authenticateChildren);
+    storeAppendPrintf(entry, "%s %s keep_alive %s\n", name, "ntlm", keep_alive ? "on" : "off");
 
 }
 
-AuthNTLMConfig::AuthNTLMConfig()
-{
-    /* TODO Move into initialisation list */
-    authenticateChildren = 5;
-    challengeuses = 0;
-    challengelifetime = 60;
-}
+AuthNTLMConfig::AuthNTLMConfig() : authenticateChildren(5), keep_alive(1)
+{ }
 
 void
 AuthNTLMConfig::parse(AuthConfig * scheme, int n_configured, char *param_str)
@@ -165,12 +144,10 @@ AuthNTLMConfig::parse(AuthConfig * scheme, int n_configured, char *param_str)
         requirePathnameExists("authparam ntlm program", authenticate->key);
     } else if (strcasecmp(param_str, "children") == 0) {
         parse_int(&authenticateChildren);
-    } else if (strcasecmp(param_str, "max_challenge_reuses") == 0) {
-        parse_int(&challengeuses);
-    } else if (strcasecmp(param_str, "max_challenge_lifetime") == 0) {
-        parse_time_t(&challengelifetime);
+    } else if (strcasecmp(param_str, "keep_alive") == 0) {
+        parse_onoff(&keep_alive);
     } else {
-        debug(28, 0) ("unrecognised ntlm auth scheme parameter '%s'\n", param_str);
+        debugs(29, 0, "AuthNTLMConfig::parse: unrecognised ntlm auth scheme parameter '" << param_str << "'");
     }
 
     /*
@@ -196,15 +173,7 @@ AuthNTLMConfig::type() const
 void
 AuthNTLMConfig::init(AuthConfig * scheme)
 {
-    static int ntlminit = 0;
-
     if (authenticate) {
-        if (!ntlm_helper_state_pool)
-            ntlm_helper_state_pool = new MemAllocatorProxy("NTLM Helper State data", sizeof(ntlm_helper_state_t));
-
-        if (!ntlm_user_hash_pool)
-
-            ntlm_user_hash_pool = new MemAllocatorProxy("NTLM Header Hash Data", sizeof(struct ProxyAuthCachePointer));
 
         authntlm_initialised = 1;
 
@@ -222,33 +191,21 @@ AuthNTLMConfig::init(AuthConfig * scheme)
 
         ntlmauthenticators->ipc_type = IPC_STREAM;
 
-        ntlmauthenticators->datapool = ntlm_helper_state_pool;
-
-        ntlmauthenticators->IsAvailable = authenticateNTLMHelperServerAvailable;
-
-        ntlmauthenticators->OnEmptyQueue = authenticateNTLMHelperServerOnEmpty;
-
         helperStatefulOpenServers(ntlmauthenticators);
 
-        /*
-         * TODO: In here send the initial YR to preinitialise the
-         * challenge cache
-         */
-        /*
-         * Think about this... currently we ask when the challenge
-         * is needed. Better?
-         */
-        if (!ntlminit) {
-            cachemgrRegister("ntlmauthenticator",
-                             "NTLM User Authenticator Stats",
-                             authenticateNTLMStats, 0, 1);
-            ntlminit++;
-        }
-
         CBDATA_INIT_TYPE(authenticateStateData);
     }
 }
 
+void
+AuthNTLMConfig::registerWithCacheManager(void)
+{
+    CacheManager::GetInstance()->
+    registerAction("ntlmauthenticator",
+                   "NTLM User Authenticator Stats",
+                   authenticateNTLMStats, 0, 1);
+}
+
 bool
 AuthNTLMConfig::active() const
 {
@@ -258,601 +215,329 @@ AuthNTLMConfig::active() const
 bool
 AuthNTLMConfig::configured() const
 {
-    if ((authenticate != NULL) && (authenticateChildren != 0) && (challengeuses > -1) && (challengelifetime > -1)) {
-        debug(29, 9) ("authNTLMConfigured: returning configured\n");
+    if ((authenticate != NULL) && (authenticateChildren != 0)) {
+        debugs(29, 9, "AuthNTLMConfig::configured: returning configured");
         return true;
     }
 
-    debug(29, 9) ("authNTLMConfigured: returning unconfigured\n");
+    debugs(29, 9, "AuthNTLMConfig::configured: returning unconfigured");
     return false;
 }
 
 /* NTLM Scheme */
+/* See AuthUserRequest.cc::authenticateDirection for return values */
 int
 AuthNTLMUserRequest::module_direction()
 {
     /* null auth_user is checked for by authenticateDirection */
 
+    if (waiting || client_blob)
+        return -1; /* need helper response to continue */
+
     switch (auth_state) {
 
         /* no progress at all. */
 
     case AUTHENTICATE_STATE_NONE:
-        debug(29, 1) ("AuthNTLMUserRequest::direction: called before NTLM Authenticate!. Report a bug to squid-dev.\n");
-        /* fall thru */
+        debugs(29, 1, "AuthNTLMUserRequest::direction: called before NTLM Authenticate for request " << this << "!. Report a bug to squid-dev.");
+        return -2; /* error */
 
     case AUTHENTICATE_STATE_FAILED:
-        return -2;
+        return -2; /* error */
 
-        /* send to helper */
 
-    case AUTHENTICATE_STATE_NEGOTIATE:
-
-        /*send to helper */
-
-    case AUTHENTICATE_STATE_RESPONSE:
-        return -1;
-
-        /* send to client */
-
-    case AUTHENTICATE_STATE_CHALLENGE:
-        return 1;
-
-        /* do nothing.. */
+    case AUTHENTICATE_STATE_IN_PROGRESS:
+        assert(server_blob);
+        return 1; /* send to client */
 
     case AUTHENTICATE_STATE_DONE:
-        return 0;
+        return 0; /* do nothing */
+
+    case AUTHENTICATE_STATE_INITIAL:
+        debugs(29, 1, "AuthNTLMUserRequest::direction: Unexpected AUTHENTICATE_STATE_INITIAL");
+        return -2;
     }
 
     return -2;
 }
 
-/*
- * Send the authenticate error header(s). Note: IE has a bug and the NTLM header
- * must be first. To ensure that, the configure use --enable-auth=ntlm, anything
- * else.
- */
 void
-AuthNTLMConfig::fixHeader(auth_user_request_t *auth_user_request, HttpReply *rep, http_hdr_type type, HttpRequest * request)
+AuthNTLMConfig::fixHeader(AuthUserRequest *auth_user_request, HttpReply *rep, http_hdr_type type, HttpRequest * request)
 {
     AuthNTLMUserRequest *ntlm_request;
 
-    if (authenticate) {
-        /* New request, no user details */
+    if (!authenticate)
+        return;
 
-        if (auth_user_request == NULL) {
-            debug(29, 9) ("authenticateNTLMFixErrorHeader: Sending type:%d header: 'NTLM'\n", type);
-            httpHeaderPutStrf(&rep->header, type, "NTLM");
+    /* Need keep-alive */
+    if (!request->flags.proxy_keepalive && request->flags.must_keepalive)
+        return;
+
+    /* New request, no user details */
+    if (auth_user_request == NULL) {
+        debugs(29, 9, "AuthNTLMConfig::fixHeader: Sending type:" << type << " header: 'NTLM'");
+        httpHeaderPutStrf(&rep->header, type, "NTLM");
+
+        if (!keep_alive) {
             /* drop the connection */
-            httpHeaderDelByName(&rep->header, "keep-alive");
-            /* NTLM has problems if the initial connection is not dropped
-             * I haven't checked the RFC compliance of this hack - RBCollins */
             request->flags.proxy_keepalive = 0;
-        } else {
-            ntlm_request = dynamic_cast< AuthNTLMUserRequest *>(auth_user_request);
-            assert (ntlm_request);
-
-            switch (ntlm_request->auth_state) {
-
-            case AUTHENTICATE_STATE_NONE:
-
-            case AUTHENTICATE_STATE_FAILED:
-                debug(29, 9) ("authenticateNTLMFixErrorHeader: Sending type:%d header: 'NTLM'\n", type);
-                httpHeaderPutStrf(&rep->header, type, "NTLM");
-                /* drop the connection */
-                httpHeaderDelByName(&rep->header, "keep-alive");
-                /* NTLM has problems if the initial connection is not dropped
-                 * I haven't checked the RFC compliance of this hack - RBCollins */
-                request->flags.proxy_keepalive = 0;
-                break;
-
-            case AUTHENTICATE_STATE_CHALLENGE:
-                /* we are 'waiting' for a response */
-                /* pass the challenge to the client */
-                debug(29, 9) ("authenticateNTLMFixErrorHeader: Sending type:%d header: 'NTLM %s'\n", type, ntlm_request->authchallenge);
-                httpHeaderPutStrf(&rep->header, type, "NTLM %s", ntlm_request->authchallenge);
-                break;
-
-            default:
-                debug(29, 0) ("authenticateNTLMFixErrorHeader: state %d.\n", ntlm_request->auth_state);
-                fatal("unexpected state in AuthenticateNTLMFixErrorHeader.\n");
-            }
         }
-    }
-}
+    } else {
+        ntlm_request = dynamic_cast<AuthNTLMUserRequest *>(auth_user_request);
 
-NTLMUser::~NTLMUser()
-{
-    dlink_node *link, *tmplink;
-    ProxyAuthCachePointer *proxy_auth_hash;
-    debug(29, 5) ("NTLMUser::~NTLMUser: Clearing NTLM scheme data\n");
-
-    /* were they linked in by one or more proxy-authenticate headers */
-    link = proxy_auth_list.head;
-
-    while (link) {
-        debug(29, 9) ("authenticateFreeProxyAuthUser: removing proxy_auth hash entry '%p'\n", link->data);
-        proxy_auth_hash = static_cast<ProxyAuthCachePointer *>(link->data);
-        tmplink = link;
-        link = link->next;
-        dlinkDelete(tmplink, &proxy_auth_list);
-        hash_remove_link(proxy_auth_cache, (hash_link *) proxy_auth_hash);
-        /* free the key (usually the proxy_auth header) */
-        xfree(proxy_auth_hash->key);
-        ntlm_user_hash_pool->free(proxy_auth_hash);
-    }
+        assert(ntlm_request != NULL);
 
-}
+        switch (ntlm_request->auth_state) {
 
-static stateful_helper_callback_t
-authenticateNTLMHandleplaceholder(void *data, void *lastserver, char *reply)
-{
-    authenticateStateData *r = static_cast<authenticateStateData *>(data);
-    stateful_helper_callback_t result = S_HELPER_UNKNOWN;
-    /* we should only be called for placeholder requests - which have no reply string */
-    assert(reply == NULL);
-    assert(r->auth_user_request);
-    /* standard callback stuff */
-
-    if (!cbdataReferenceValid(r->data)) {
-        debug(29, 1) ("AuthenticateNTLMHandlePlacheholder: invalid callback data.\n");
-        return result;
-    }
+        case AUTHENTICATE_STATE_FAILED:
+            /* here it makes sense to drop the connection, as auth is
+             * tied to it, even if MAYBE the client could handle it - Kinkie */
+            request->flags.proxy_keepalive = 0;
+            /* fall through */
 
-    /* call authenticateNTLMStart to retry this request */
-    debug(29, 9) ("authenticateNTLMHandleplaceholder: calling authenticateNTLMStart\n");
+        case AUTHENTICATE_STATE_DONE:
+            /* Special case: authentication finished OK but disallowed by ACL.
+             * Need to start over to give the client another chance.
+             */
+            /* fall through */
 
-    r->auth_user_request->start(r->handler, r->data);
+        case AUTHENTICATE_STATE_NONE:
+            /* semantic change: do not drop the connection.
+             * 2.5 implementation used to keep it open - Kinkie */
+            debugs(29, 9, "AuthNTLMConfig::fixHeader: Sending type:" << type << " header: 'NTLM'");
+            httpHeaderPutStrf(&rep->header, type, "NTLM");
+            break;
 
-    cbdataReferenceDone(r->data);
+        case AUTHENTICATE_STATE_IN_PROGRESS:
+            /* we're waiting for a response from the client. Pass it the blob */
+            debugs(29, 9, "AuthNTLMConfig::fixHeader: Sending type:" << type << " header: 'NTLM " << ntlm_request->server_blob << "'");
+            httpHeaderPutStrf(&rep->header, type, "NTLM %s", ntlm_request->server_blob);
+            safe_free(ntlm_request->server_blob);
+            break;
 
-    authenticateStateFree(r);
 
-    return result;
+        default:
+            debugs(29, 0, "AuthNTLMConfig::fixHeader: state " << ntlm_request->auth_state << ".");
+            fatal("unexpected state in AuthenticateNTLMFixErrorHeader.\n");
+        }
+    }
+}
+
+NTLMUser::~NTLMUser()
+{
+    debugs(29, 5, "NTLMUser::~NTLMUser: doing nothing to clearNTLM scheme data for '" << this << "'");
 }
 
 static stateful_helper_callback_t
 authenticateNTLMHandleReply(void *data, void *lastserver, char *reply)
 {
     authenticateStateData *r = static_cast<authenticateStateData *>(data);
-    ntlm_helper_state_t *helperstate;
+
+    int valid;
     stateful_helper_callback_t result = S_HELPER_UNKNOWN;
-    char *t = NULL;
-    auth_user_request_t *auth_user_request;
-    auth_user_t *auth_user;
-    ntlm_user_t *ntlm_user;
+    char *blob;
+
+    AuthUserRequest *auth_user_request;
+    AuthUser *auth_user;
+    NTLMUser *ntlm_user;
     AuthNTLMUserRequest *ntlm_request;
-    debug(29, 9) ("authenticateNTLMHandleReply: Helper: '%p' {%s}\n", lastserver, reply ? reply : "<NULL>");
 
-    if (!cbdataReferenceValid(r->data)) {
-        debug(29, 1) ("AuthenticateNTLMHandleReply: invalid callback data. Releasing helper '%p'.\n", lastserver);
+    debugs(29, 8, "authenticateNTLMHandleReply: helper: '" << lastserver << "' sent us '" << (reply ? reply : "<NULL>") << "'");
+    valid = cbdataReferenceValid(r->data);
+
+    if (!valid) {
+        debugs(29, 1, "authenticateNTLMHandleReply: invalid callback data. Releasing helper '" << lastserver << "'.");
         cbdataReferenceDone(r->data);
         authenticateStateFree(r);
-        debug(29, 9) ("NTLM HandleReply, telling stateful helper : %d\n", S_HELPER_RELEASE);
+        debugs(29, 9, "authenticateNTLMHandleReply: telling stateful helper : " << S_HELPER_RELEASE);
         return S_HELPER_RELEASE;
     }
 
     if (!reply) {
-        /*
-         * TODO: this occurs when a helper crashes. We should clean
-         * up that helpers resources and queued requests.
-         */
-        fatal("authenticateNTLMHandleReply: called with no result string\n");
+        debugs(29, 1, "authenticateNTLMHandleReply: Helper '" << lastserver << "' crashed!.");
+        reply = (char *)"BH Internal error";
     }
 
-    /* seperate out the useful data */
-    if (strncasecmp(reply, "TT ", 3) == 0) {
-        reply += 3;
-        /* we have been given a Challenge */
-        /* we should check we weren't given an empty challenge */
-        /* copy the challenge to the state data */
-        helperstate = static_cast<ntlm_helper_state_t *>(helperStatefulServerGetData(static_cast<helper_stateful_server *>(lastserver)));
-
-        if (helperstate == NULL)
-            fatal("lost NTLM helper state! quitting\n");
-
-        helperstate->challenge = xstrdup(reply);
-
-        helperstate->challengeuses = 0;
-
-        helperstate->renewed = squid_curtime;
+    auth_user_request = r->auth_user_request;
+    assert(auth_user_request != NULL);
+    ntlm_request = dynamic_cast<AuthNTLMUserRequest *>(auth_user_request);
 
-        /* and we satisfy the request that happended on the refresh boundary */
-        /* note this code is now in two places FIXME */
-        assert(r->auth_user_request != NULL);
+    assert(ntlm_request != NULL);
+    assert(ntlm_request->waiting);
+    ntlm_request->waiting = 0;
+    safe_free(ntlm_request->client_blob);
 
-        assert(r->auth_user_request->user()->auth_type == AUTH_NTLM);
-
-        auth_user_request = r->auth_user_request;
-
-        ntlm_request = dynamic_cast< AuthNTLMUserRequest *>(auth_user_request);
-
-        assert(ntlm_request != NULL);
-
-        result = S_HELPER_DEFER;
+    auth_user = ntlm_request->user();
+    assert(auth_user != NULL);
+    assert(auth_user->auth_type == AUTH_NTLM);
+    ntlm_user = dynamic_cast<ntlm_user_t *>(auth_user_request->user());
 
-        /* reserve the server for future authentication */
-        ntlm_request->authserver_deferred = 1;
+    assert(ntlm_user != NULL);
 
-        debug(29, 9) ("authenticateNTLMHandleReply: helper '%p'\n", lastserver);
+    if (ntlm_request->authserver == NULL)
+        ntlm_request->authserver = static_cast<helper_stateful_server*>(lastserver);
+    else
+        assert(ntlm_request->authserver == lastserver);
 
-        assert(ntlm_request->auth_state == AUTHENTICATE_STATE_NEGOTIATE);
+    /* seperate out the useful data */
+    blob = strchr(reply, ' ');
 
-        ntlm_request->authserver = static_cast<helper_stateful_server *>(lastserver);
+    if (blob)
+        blob++;
 
-        ntlm_request->authchallenge = xstrdup(reply);
+    if (strncasecmp(reply, "TT ", 3) == 0) {
+        /* we have been given a blob to send to the client */
+        safe_free(ntlm_request->server_blob);
+        ntlm_request->request->flags.must_keepalive = 1;
+        if (ntlm_request->request->flags.proxy_keepalive) {
+            ntlm_request->server_blob = xstrdup(blob);
+            ntlm_request->auth_state = AUTHENTICATE_STATE_IN_PROGRESS;
+            auth_user_request->denyMessage("Authentication in progress");
+            debugs(29, 4, "authenticateNTLMHandleReply: Need to challenge the client with a server blob '" << blob << "'");
+            result = S_HELPER_RESERVE;
+        } else {
+            ntlm_request->auth_state = AUTHENTICATE_STATE_FAILED;
+            auth_user_request->denyMessage("NTLM authentication requires a persistent connection");
+            result = S_HELPER_RELEASE;
+        }
     } else if (strncasecmp(reply, "AF ", 3) == 0) {
         /* we're finished, release the helper */
-        reply += 3;
-        assert(r->auth_user_request != NULL);
-        assert(r->auth_user_request->user()->auth_type == AUTH_NTLM);
-        auth_user_request = r->auth_user_request;
-        ntlm_request = dynamic_cast< AuthNTLMUserRequest *>(auth_user_request);
-        assert(ntlm_request);
-        auth_user = auth_user_request->user();
-        ntlm_user = dynamic_cast<ntlm_user_t *>(auth_user_request->user());
-        assert(ntlm_user != NULL);
-        result = S_HELPER_RELEASE;
-        /* we only expect OK when finishing the handshake */
-        assert(ntlm_request->auth_state == AUTHENTICATE_STATE_RESPONSE);
-        ntlm_user->username(xstrdup(reply));
-        ntlm_request->authserver = NULL;
-#ifdef NTLM_FAIL_OPEN
-
-    } else if (strncasecmp(reply, "LD ", 3) == 0) {
-        /* This is a variant of BH, which rather than deny access
-         * allows the user through. The helper is starved and then refreshed
-         * via YR, all pending authentications are likely to fail also.
-         * It is meant for those helpers which occasionally fail for
-         * no reason at all (casus belli, NTLMSSP helper on NT domain,
-         * failing about 1 auth out of 1k.
-         * The code is a merge from the BH case with snippets of the AF
-         * case */
-        /* AF code: mark user as authenticated */
-        reply += 3;
-        assert(r->auth_user_request != NULL);
-        assert(r->auth_user_request->user()->auth_type == AUTH_NTLM);
-        auth_user_request = r->auth_user_request;
-        ntlm_request = dynamic_cast< AuthNTLMUserRequest *>(auth_user_request);
-        assert(ntlm_request);
-        auth_user = auth_user_request->user();
-        ntlm_user = dynamic_cast<ntlm_user_t *>(auth_user_request->user());
-        assert(ntlm_user != NULL);
-        result = S_HELPER_RELEASE;
-        /* we only expect LD when finishing the handshake */
-        assert(ntlm_request->auth_state == AUTHENTICATE_STATE_RESPONSE);
-        ntlm_user->username_ = xstrdup(reply);
-        helperstate = static_cast<ntlm_helper_state_t *>(helperStatefulServerGetData(ntlm_request->authserver));
-        ntlm_request->authserver = NULL;
-        /* BH code: mark helper as broken */
-        /* mark it for starving */
-        helperstate->starve = 1;
-#endif
+        ntlm_user->username(blob);
+        auth_user_request->denyMessage("Login successful");
+        safe_free(ntlm_request->server_blob);
 
+        result = S_HELPER_RELEASE;
+        debugs(29, 4, "authenticateNTLMHandleReply: Successfully validated user via NTLM. Username '" << blob << "'");
+        /* connection is authenticated */
+        debugs(29, 4, "AuthNTLMUserRequest::authenticate: authenticated user " << ntlm_user->username());
+        /* see if this is an existing user with a different proxy_auth
+         * string */
+        auth_user_hash_pointer *usernamehash = static_cast<AuthUserHashPointer *>(hash_lookup(proxy_auth_username_cache, ntlm_user->username()));
+        AuthUser *local_auth_user = ntlm_request->user();
+        while (usernamehash && (usernamehash->user()->auth_type != AUTH_NTLM || strcmp(usernamehash->user()->username(), ntlm_user->username()) != 0))
+            usernamehash = static_cast<AuthUserHashPointer *>(usernamehash->next);
+        if (usernamehash) {
+            /* we can't seamlessly recheck the username due to the
+             * challenge-response nature of the protocol.
+             * Just free the temporary auth_user */
+            usernamehash->user()->absorb(local_auth_user);
+            //authenticateAuthUserMerge(local_auth_user, usernamehash->user());
+            local_auth_user = usernamehash->user();
+            ntlm_request->_auth_user = local_auth_user;
+        } else {
+            /* store user in hash's */
+            local_auth_user->addToNameCache();
+            // authenticateUserNameCacheAdd(local_auth_user);
+        }
+        /* set these to now because this is either a new login from an
+         * existing user or a new user */
+        local_auth_user->expiretime = current_time.tv_sec;
+        authenticateNTLMReleaseServer(ntlm_request);
+        ntlm_request->auth_state = AUTHENTICATE_STATE_DONE;
     } else if (strncasecmp(reply, "NA ", 3) == 0) {
-        /* TODO: only work with auth_user here if it exists */
-        assert(r->auth_user_request != NULL);
-        assert(r->auth_user_request->user()->auth_type == AUTH_NTLM);
-        auth_user_request = r->auth_user_request;
-        auth_user = auth_user_request->user();
-        assert(auth_user != NULL);
-        ntlm_user = dynamic_cast<ntlm_user_t *>(auth_user);
-        ntlm_request = dynamic_cast< AuthNTLMUserRequest *>(auth_user_request);
-        assert((ntlm_user != NULL) && (ntlm_request != NULL));
-        /* todo: action of Negotiate state on error */
-        result = S_HELPER_RELEASE;     /*some error has occured. no more requests */
-        ntlm_request->authserver = NULL;
-        debug(29, 4) ("authenticateNTLMHandleReply: Error validating user via NTLM. Error returned '%s'\n", reply);
+        /* authentication failure (wrong password, etc.) */
+        auth_user_request->denyMessage(blob);
         ntlm_request->auth_state = AUTHENTICATE_STATE_FAILED;
-
-        if ((t = strchr(reply, ' ')))  /* strip after a space */
-            *t = '\0';
-    } else if (strncasecmp(reply, "NA", 2) == 0) {
-        /* NTLM Helper protocol violation! */
-        fatal("NTLM Helper returned invalid response \"NA\" - a error message MUST be attached\n");
+        safe_free(ntlm_request->server_blob);
+        authenticateNTLMReleaseServer(ntlm_request);
+        result = S_HELPER_RELEASE;
+        debugs(29, 4, "authenticateNTLMHandleReply: Failed validating user via NTLM. Error returned '" << blob << "'");
     } else if (strncasecmp(reply, "BH ", 3) == 0) {
         /* TODO kick off a refresh process. This can occur after a YR or after
-         * a KK. If after a YR release the helper and resubmit the request via 
-         * Authenticate NTLM start. 
-         * If after a KK deny the user's request w/ 407 and mark the helper as 
+         * a KK. If after a YR release the helper and resubmit the request via
+         * Authenticate NTLM start.
+         * If after a KK deny the user's request w/ 407 and mark the helper as
          * Needing YR. */
-        assert(r->auth_user_request != NULL);
-        assert(r->auth_user_request->user()->auth_type == AUTH_NTLM);
-        auth_user_request = r->auth_user_request;
-        auth_user = auth_user_request->user();
-        assert(auth_user != NULL);
-        ntlm_user = dynamic_cast<ntlm_user_t *>(auth_user);
-        ntlm_request = dynamic_cast< AuthNTLMUserRequest *>(auth_user_request);
-        assert((ntlm_user != NULL) && (ntlm_request != NULL));
-        /*some error has occured. no more requests for
-                                                                                                                                        * this helper */
-        result = S_HELPER_RELEASE;
-        assert(ntlm_request->authserver ? ntlm_request->authserver == lastserver : 1);
-        helperstate = static_cast<ntlm_helper_state_t *>(helperStatefulServerGetData(ntlm_request->authserver));
-        ntlm_request->authserver = NULL;
-
-        if (ntlm_request->auth_state == AUTHENTICATE_STATE_NEGOTIATE) {
-            /* The helper broke on YR. It automatically
-             * resets */
-            debug(29, 1) ("authenticateNTLMHandleReply: Error obtaining challenge from helper: %p. Error returned '%s'\n", lastserver, reply);
-            /* mark it for starving */
-            helperstate->starve = 1;
-            /* resubmit the request. This helper is currently busy, so we will get
-             * a different one. Our auth state stays the same */
-            auth_user_request->start(r->handler, r->data);
-            /* don't call the callback */
-            cbdataReferenceDone(r->data);
-            authenticateStateFree(r);
-            debug(29, 9) ("NTLM HandleReply, telling stateful helper : %d\n", result);
-            return result;
-        }
-
-        /* the helper broke on a KK */
-        /* first the standard KK stuff */
-        debug(29, 4) ("authenticateNTLMHandleReply: Error validating user via NTLM. Error returned '%s'\n", reply);
-
-        if ((t = strchr(reply, ' ')))  /* strip after a space */
-            *t = '\0';
-
-        /* now we mark the helper for resetting. */
-        helperstate->starve = 1;
-
+        auth_user_request->denyMessage(blob);
         ntlm_request->auth_state = AUTHENTICATE_STATE_FAILED;
+        safe_free(ntlm_request->server_blob);
+        authenticateNTLMReleaseServer(ntlm_request);
+        result = S_HELPER_RELEASE;
+        debugs(29, 1, "authenticateNTLMHandleReply: Error validating user via NTLM. Error returned '" << reply << "'");
     } else {
-        /* TODO: only work with auth_user here if it exists */
-        /* TODO: take the request state into consideration */
-        assert(r->auth_user_request != NULL);
-        assert(r->auth_user_request->user()->auth_type == AUTH_NTLM);
-        auth_user_request = r->auth_user_request;
-        auth_user = auth_user_request->user();
-        assert(auth_user != NULL);
-        ntlm_user = dynamic_cast<ntlm_user_t *>(auth_user);
-        ntlm_request = dynamic_cast< AuthNTLMUserRequest *>(auth_user_request);
-        assert((ntlm_user != NULL) && (ntlm_request != NULL));
-        debug(29, 1) ("authenticateNTLMHandleReply: *** Unsupported helper response ***, '%s'\n", reply);
-        /* **** NOTE THIS CODE IS EFFECTIVELY UNTESTED **** */
-        /* restart the authentication process */
-        ntlm_request->auth_state = AUTHENTICATE_STATE_NONE;
-        assert(ntlm_request->authserver ? ntlm_request->authserver == lastserver : 1);
-        ntlm_request->authserver = NULL;
+        /* protocol error */
+        fatalf("authenticateNTLMHandleReply: *** Unsupported helper response ***, '%s'\n", reply);
     }
 
+    if (ntlm_request->request) {
+        HTTPMSGUNLOCK(ntlm_request->request);
+        ntlm_request->request = NULL;
+    }
     r->handler(r->data, NULL);
     cbdataReferenceDone(r->data);
     authenticateStateFree(r);
-    debug(29, 9) ("NTLM HandleReply, telling stateful helper : %d\n", result);
+    debugs(29, 9, "authenticateNTLMHandleReply: telling stateful helper : " << result);
     return result;
 }
 
 static void
 authenticateNTLMStats(StoreEntry * sentry)
 {
-    storeAppendPrintf(sentry, "NTLM Authenticator Statistics:\n");
-    helperStatefulStats(sentry, ntlmauthenticators);
-}
-
-/* is a particular challenge still valid ? */
-static int
-authenticateNTLMValidChallenge(ntlm_helper_state_t * helperstate)
-{
-    debug(29, 9) ("authenticateNTLMValidChallenge: Challenge is %s\n", helperstate->challenge ? "Valid" : "Invalid");
-
-    if (helperstate->challenge == NULL)
-        return 0;
-
-    return 1;
+    helperStatefulStats(sentry, ntlmauthenticators, "NTLM Authenticator Statistics");
 }
 
-/* does our policy call for changing the challenge now? */
-static int
-authenticateNTLMChangeChallenge_p(ntlm_helper_state_t * helperstate)
-{
-    /* don't check for invalid challenges just for expiry choices */
-    /* this is needed because we have to starve the helper until all old
-     * requests have been satisfied */
-
-    if (!helperstate->renewed) {
-        /* first use, no challenge has been set. Without this check, it will
-         * loop forever */
-        debug(29, 5) ("authenticateNTLMChangeChallenge_p: first use\n");
-        return 0;
-    }
-
-    if (helperstate->challengeuses > ntlmConfig.challengeuses) {
-        debug(29, 4) ("authenticateNTLMChangeChallenge_p: Challenge uses (%d) exceeded max uses (%d)\n", helperstate->challengeuses, ntlmConfig.challengeuses);
-        return 1;
-    }
-
-    if (helperstate->renewed + ntlmConfig.challengelifetime < squid_curtime) {
-        debug(29, 4) ("authenticateNTLMChangeChallenge_p: Challenge exceeded max lifetime by %d seconds\n", (int) (squid_curtime - (helperstate->renewed + ntlmConfig.challengelifetime)));
-        return 1;
-    }
-
-    debug(29, 9) ("Challenge is to be reused\n");
-    return 0;
-}
 
 /* send the initial data to a stateful ntlm authenticator module */
 void
 AuthNTLMUserRequest::module_start(RH * handler, void *data)
 {
     authenticateStateData *r = NULL;
-    helper_stateful_server *server;
-    ntlm_helper_state_t *helperstate;
-    char buf[8192];
-    char *sent_string = NULL;
+    static char buf[8192];
     ntlm_user_t *ntlm_user;
-    auth_user_t *auth_user;
+    AuthUser *auth_user = user();
 
-    auth_user = this->user();
-    ntlm_user = dynamic_cast<ntlm_user_t *>(auth_user);
-    assert(ntlm_user);
     assert(data);
+    assert(handler);
+    assert(auth_user);
     assert(auth_user->auth_type == AUTH_NTLM);
-    debug(29, 9) ("authenticateNTLMStart: auth state '%d'\n", auth_state);
-
-    switch (auth_state) {
-
-    case AUTHENTICATE_STATE_NEGOTIATE:
-        sent_string = ntlmnegotiate;
-        break;
-
-    case AUTHENTICATE_STATE_RESPONSE:
-        sent_string = ntlmauthenticate;
-        assert(authserver);
-        debug(29, 9) ("authenticateNTLMStart: Asking NTLMauthenticator '%p'.\n", authserver);
-        break;
-
-    default:
-        fatal("Invalid authenticate state for NTLMStart");
-    }
-
-    while (!xisspace(*sent_string))    /*trim NTLM */
-        sent_string++;
-
-    while (xisspace(*sent_string))     /*trim leading spaces */
-        sent_string++;
 
-    debug(29, 9) ("authenticateNTLMStart: state '%d'\n", auth_state);
+    ntlm_user = dynamic_cast<ntlm_user_t *>(user());
 
-    debug(29, 9) ("authenticateNTLMStart: '%s'\n", sent_string);
+    debugs(29, 8, "AuthNTLMUserRequest::module_start: auth state is '" << auth_state << "'");
 
     if (ntlmConfig.authenticate == NULL) {
-        debug(29, 0) ("authenticateNTLMStart: no NTLM program specified:'%s'\n", sent_string);
+        debugs(29, 0, "AuthNTLMUserRequest::module_start: no NTLM program specified.");
         handler(data, NULL);
         return;
     }
 
-    /* this is ugly TODO: move the challenge generation routines to their own function and
-     * tidy the logic up to make use of the efficiency we now have */
-    switch (auth_state) {
-
-    case AUTHENTICATE_STATE_NEGOTIATE:
-        /*
-         * 1: get a helper server
-         * 2: does it have a challenge?
-         * 3: tell it to get a challenge, or give ntlmauthdone the challenge
-         */
-        server = helperStatefulDefer(ntlmauthenticators);
-        helperstate = server ? static_cast<ntlm_helper_state_t *>(helperStatefulServerGetData(server)) : NULL;
-
-        while ((server != NULL) && authenticateNTLMChangeChallenge_p(helperstate)) {
-            /* flag this helper for challenge changing */
-            helperstate->starve = 1;
-            /* and release the deferred request */
-            helperStatefulReleaseServer(server);
-            /* Get another deferrable server */
-            server = helperStatefulDefer(ntlmauthenticators);
-            helperstate = server ? static_cast<ntlm_helper_state_t *>(helperStatefulServerGetData(server)) : NULL;
-        }
-
-        if (server == NULL)
-            debug(29, 9) ("unable to get a deferred ntlm helper... all helpers are refreshing challenges. Queuing as a placeholder request.\n");
-
-        authserver = server;
-
-        /* tell the log what helper we have been given */
-        debug(29, 9) ("authenticateNTLMStart: helper '%p' assigned\n", server);
-
-        /* server and valid challenge? */
-        if ((server == NULL) || !authenticateNTLMValidChallenge(helperstate)) {
-            /* No server, or server with invalid challenge */
-            r = cbdataAlloc(authenticateStateData);
-            r->handler = handler;
-            r->data = cbdataReference(data);
-            r->auth_user_request = this;
-
-            if (server == NULL) {
-                helperStatefulSubmit(ntlmauthenticators, NULL, authenticateNTLMHandleplaceholder, r, NULL);
-            } else {
-                /* Server with invalid challenge */
-                snprintf(buf, 8192, "YR\n");
-                helperStatefulSubmit(ntlmauthenticators, buf, authenticateNTLMHandleReply, r, authserver);
-            }
-        } else {
-            /* (server != NULL and we have a valid challenge) */
-            /* TODO: turn the below into a function and call from here and handlereply */
-            /* increment the challenge uses */
-            helperstate->challengeuses++;
-            /* assign the challenge */
-            authchallenge = xstrdup(helperstate->challenge);
-            /* we're not actually submitting a request, so we need to release the helper
-             * should the connection close unexpectedly
-             */
-            authserver_deferred = 1;
-            handler(data, NULL);
-        }
-
-        break;
-
-    case AUTHENTICATE_STATE_RESPONSE:
-        r = cbdataAlloc(authenticateStateData);
-        r->handler = handler;
-        r->data = cbdataReference(data);
-        r->auth_user_request = this;
-        snprintf(buf, 8192, "KK %s\n", sent_string);
-        /* getting rid of deferred request status */
-        authserver_deferred = 0;
-        helperStatefulSubmit(ntlmauthenticators, buf, authenticateNTLMHandleReply, r, authserver);
-        debug(29, 9) ("authenticateNTLMstart: finished\n");
-        break;
-
-    default:
-        fatal("Invalid authenticate state for NTLMStart");
-    }
-}
-
-/* callback used by stateful helper routines */
-static int
-authenticateNTLMHelperServerAvailable(void *data)
-{
-    ntlm_helper_state_t *statedata = static_cast<ntlm_helper_state_t *>(data);
+    r = cbdataAlloc(authenticateStateData);
+    r->handler = handler;
+    r->data = cbdataReference(data);
+    r->auth_user_request = this;
+    AUTHUSERREQUESTLOCK(r->auth_user_request, "r");
 
-    if (statedata != NULL) {
-        if (statedata->starve) {
-            debug(29, 4) ("authenticateNTLMHelperServerAvailable: starving - returning 0\n");
-            return 0;
-        } else {
-            debug(29, 4) ("authenticateNTLMHelperServerAvailable: not starving - returning 1\n");
-            return 1;
-        }
+    if (auth_state == AUTHENTICATE_STATE_INITIAL) {
+        snprintf(buf, 8192, "YR %s\n", client_blob); //CHECKME: can ever client_blob be 0 here?
+    } else {
+        snprintf(buf, 8192, "KK %s\n", client_blob);
     }
 
-    debug(29, 4) ("authenticateNTLMHelperServerAvailable: no state data - returning 0\n");
-    return 0;
-}
-
-static void
-authenticateNTLMHelperServerOnEmpty(void *data)
-{
-    ntlm_helper_state_t *statedata = static_cast<ntlm_helper_state_t *>(data);
-
-    if (statedata == NULL)
-        return;
+    waiting = 1;
 
-    if (statedata->starve) {
-        /* we have been starving the helper */
-        debug(29, 9) ("authenticateNTLMHelperServerOnEmpty: resetting challenge details\n");
-        statedata->starve = 0;
-        statedata->challengeuses = 0;
-        statedata->renewed = 0;
-        xfree(statedata->challenge);
-        statedata->challenge = NULL;
-    }
+    safe_free(client_blob);
+    helperStatefulSubmit(ntlmauthenticators, buf, authenticateNTLMHandleReply, r, authserver);
 }
 
-
 /* clear the NTLM helper of being reserved for future requests */
 static void
-authenticateNTLMReleaseServer(auth_user_request_t * auth_user_request)
+authenticateNTLMReleaseServer(AuthUserRequest * auth_user_request)
 {
     AuthNTLMUserRequest *ntlm_request;
     assert(auth_user_request->user()->auth_type == AUTH_NTLM);
     ntlm_request = dynamic_cast< AuthNTLMUserRequest *>(auth_user_request);
-    assert (ntlm_request);
-    debug(29, 9) ("authenticateNTLMReleaseServer: releasing server '%p'\n", ntlm_request->authserver);
-    helperStatefulReleaseServer(ntlm_request->authserver);
-    ntlm_request->authserver = NULL;
+    debugs(29, 9, "authenticateNTLMReleaseServer: releasing server '" << ntlm_request->authserver << "'");
+    /* is it possible for the server to be NULL? hno seems to think so.
+     * Let's see what happens, might segfault in helperStatefulReleaseServer
+     * if it does. I leave it like this not to cover possibly problematic
+     * code-paths. Kinkie */
+    /* DPW 2007-05-07
+     * yes, it is possible */
+    assert(ntlm_request != NULL);
+    if (ntlm_request->authserver) {
+        helperStatefulReleaseServer(ntlm_request->authserver);
+        ntlm_request->authserver = NULL;
+    }
 }
 
 /* clear any connection related authentication details */
@@ -861,37 +546,24 @@ AuthNTLMUserRequest::onConnectionClose(ConnStateData *conn)
 {
     assert(conn != NULL);
 
-    if (conn->auth_user_request != NULL) {
-        assert (conn->auth_user_request == this);
-        assert(this->conn == conn);
+    debugs(29, 8, "AuthNTLMUserRequest::onConnectionClose: closing connection '" << conn << "' (this is '" << this << "')");
 
-        if (authserver != NULL && authserver_deferred)
-            authenticateNTLMReleaseServer(this);
-
-        /* unlock the connection based lock */
-        debug(29, 9) ("authenticateNTLMOnCloseConnection: Unlocking auth_user from the connection.\n");
+    if (conn->auth_user_request == NULL) {
+        debugs(29, 8, "AuthNTLMUserRequest::onConnectionClose: no auth_user_request");
+        return;
+    }
 
-        /* This still breaks the abstraction, but is at least read only now.
-        * If needed, this could be ignored, as the conn deletion will also unlock
-        * the auth user request.
-        */
-        this->unlock();
+    if (authserver != NULL)
+        authenticateNTLMReleaseServer(this);
 
-        conn->auth_user_request = NULL;
-    }
-}
+    /* unlock the connection based lock */
+    debugs(29, 9, "AuthNTLMUserRequest::onConnectionClose: Unlocking auth_user from the connection '" << conn << "'.");
 
-/* NTLMLastHeader: return a pointer to the last header used in authenticating
- * the request/conneciton
- */
-const char *
-AuthNTLMUserRequest::connLastHeader()
-{
-    return ntlmauthenticate;
+    AUTHUSERREQUESTUNLOCK(conn->auth_user_request, "conn");
 }
 
 /*
- * Decode an NTLM [Proxy-]Auth string, placing the results in the passed
+ * Decode a NTLM [Proxy-]Auth string, placing the results in the passed
  * Auth_user structure.
  */
 AuthUserRequest *
@@ -905,253 +577,158 @@ AuthNTLMConfig::decode(char const *proxy_auth)
     auth_user_request->user()->addRequest(auth_user_request);
 
     /* all we have to do is identify that it's NTLM - the helper does the rest */
-    debug(29, 9) ("authenticateDecodeNTLMAuth: NTLM authentication\n");
+    debugs(29, 9, "AuthNTLMConfig::decode: NTLM authentication");
     return auth_user_request;
 }
 
-static int
-authenticateNTLMcmpUsername(ntlm_user_t * u1, ntlm_user_t * u2)
-{
-    return strcmp(u1->username(), u2->username());
-}
-
-
-/* there is a known race where a single client recieves the same challenge
- * and sends the same response to squid on a single select cycle.
- * Check for this and if found ignore the new link 
- */
-static void
-authenticateProxyAuthCacheAddLink(const char *key, auth_user_t * auth_user)
-{
-
-    struct ProxyAuthCachePointer *proxy_auth_hash;
-    dlink_node *node;
-    ntlm_user_t *ntlm_user;
-    ntlm_user = dynamic_cast<ntlm_user_t *>(auth_user);
-    node = ntlm_user->proxy_auth_list.head;
-    /* prevent duplicates */
-
-    while (node) {
-
-        if (!strcmp(key, (char const *)((struct ProxyAuthCachePointer *) node->data)->key))
-            return;
-
-        node = node->next;
-    }
-
-    proxy_auth_hash = static_cast<ProxyAuthCachePointer *>(ntlm_user_hash_pool->alloc());
-    proxy_auth_hash->key = xstrdup(key);
-    proxy_auth_hash->auth_user = auth_user;
-    dlinkAddTail(proxy_auth_hash, &proxy_auth_hash->link, &ntlm_user->proxy_auth_list);
-    hash_join(proxy_auth_cache, (hash_link *) proxy_auth_hash);
-}
-
 int
 AuthNTLMUserRequest::authenticated() const
 {
-    if (auth_state == AUTHENTICATE_STATE_DONE)
+    if (auth_state == AUTHENTICATE_STATE_DONE) {
+        debugs(29, 9, "AuthNTLMUserRequest::authenticated: user authenticated.");
         return 1;
+    }
 
-    debug(29, 9) ("User not fully authenticated.\n");
+    debugs(29, 9, "AuthNTLMUserRequest::authenticated: user not fully authenticated.");
 
     return 0;
 }
 
 void
-AuthNTLMUserRequest::authenticate(HttpRequest * request, ConnStateData::Pointer conn, http_hdr_type type)
+AuthNTLMUserRequest::authenticate(HttpRequest * request, ConnStateData * conn, http_hdr_type type)
 {
-    const char *proxy_auth;
+    const char *proxy_auth, *blob;
 
-    struct ProxyAuthCachePointer *proxy_auth_hash = NULL;
-    auth_user_hash_pointer *usernamehash;
     /* TODO: rename this!! */
-    auth_user_t *auth_user;
-    AuthNTLMUserRequest *ntlm_request;
+    AuthUser *local_auth_user;
     ntlm_user_t *ntlm_user;
-    LOCAL_ARRAY(char, ntlmhash, NTLM_CHALLENGE_SZ * 2);
-    /* get header */
-    proxy_auth = httpHeaderGetStr(&request->header, type);
 
-    auth_user = user();
-    assert(auth_user);
-    assert(auth_user->auth_type == AUTH_NTLM);
-    ntlm_user = dynamic_cast<ntlm_user_t *>(auth_user);
-    ntlm_request = this;
-    assert (ntlm_request);
+    local_auth_user = user();
+    assert(local_auth_user);
+    assert(local_auth_user->auth_type == AUTH_NTLM);
+    ntlm_user = dynamic_cast<ntlm_user_t *>(local_auth_user);
+    assert (this);
+
     /* Check that we are in the client side, where we can generate
      * auth challenges */
 
-    if (conn.getRaw() == NULL) {
-        ntlm_request->auth_state = AUTHENTICATE_STATE_FAILED;
-        debug(29, 1) ("authenticateNTLMAuthenticateUser: attempt to perform authentication without a connection!\n");
+    if (conn == NULL || !cbdataReferenceValid(conn)) {
+        auth_state = AUTHENTICATE_STATE_FAILED;
+        debugs(29, 1, "AuthNTLMUserRequest::authenticate: attempt to perform authentication without a connection!");
         return;
     }
 
-    switch (ntlm_request->auth_state) {
-
-    case AUTHENTICATE_STATE_NONE:
-        /* we've recieved a negotiate request. pass to a helper */
-        debug(29, 9) ("authenticateNTLMAuthenticateUser: auth state ntlm none. %s\n", proxy_auth);
-        ntlm_request->auth_state = AUTHENTICATE_STATE_NEGOTIATE;
-        ntlm_request->ntlmnegotiate = xstrdup(proxy_auth);
-        conn->auth_type = AUTH_NTLM;
-        conn->auth_user_request = this;
-        ntlm_request->conn = conn;
-        /* and lock for the connection duration */
-        debug(29, 9) ("authenticateNTLMAuthenticateUser: Locking auth_user from the connection.\n");
-
-        this->lock()
-
-        ;
+    if (waiting) {
+        debugs(29, 1, "AuthNTLMUserRequest::authenticate: waiting for helper reply!");
         return;
+    }
 
-        break;
-
-    case AUTHENTICATE_STATE_NEGOTIATE:
-        ntlm_request->auth_state = AUTHENTICATE_STATE_CHALLENGE;
-
-        /* We _MUST_ have the auth challenge by now */
-        assert(ntlm_request->authchallenge);
-
+    if (server_blob) {
+        debugs(29, 2, "AuthNTLMUserRequest::authenticate: need to challenge client '" << server_blob << "'!");
         return;
+    }
 
-        break;
+    /* get header */
+    proxy_auth = request->header.getStr(type);
 
-    case AUTHENTICATE_STATE_CHALLENGE:
-        /* we should have recieved a NTLM challenge. pass it to the same
-         * helper process */
-        debug(29, 9) ("authenticateNTLMAuthenticateUser: auth state challenge with header %s.\n", proxy_auth);
+    /* locate second word */
+    blob = proxy_auth;
 
-        /* do a cache lookup here. If it matches it's a successful ntlm
-         * challenge - release the helper and use the existing auth_user 
-         * details. */
+    /* if proxy_auth is actually NULL, we'd better not manipulate it. */
+    if (blob) {
+        while (xisspace(*blob) && *blob)
+            blob++;
 
-        if (strncmp("NTLM ", proxy_auth, 5) == 0) {
-            ntlm_request->ntlmauthenticate = xstrdup(proxy_auth);
-        } else {
-            fatal("Incorrect scheme in auth header\n");
-            /* TODO: more fault tolerance.. reset the auth scheme here */
-        }
+        while (!xisspace(*blob) && *blob)
+            blob++;
 
-        /* cache entries have authenticateauthheaderchallengestring */
-        snprintf(ntlmhash, sizeof(ntlmhash) - 1, "%s%s",
-                 ntlm_request->ntlmauthenticate,
-                 ntlm_request->authchallenge);
+        while (xisspace(*blob) && *blob)
+            blob++;
+    }
 
-        /* see if we already know this user's authenticate */
-        debug(29, 9) ("aclMatchProxyAuth: cache lookup with key '%s'\n", ntlmhash);
+    switch (auth_state) {
 
-        assert(proxy_auth_cache != NULL);
+    case AUTHENTICATE_STATE_NONE:
+        /* we've received a ntlm request. pass to a helper */
+        debugs(29, 9, "AuthNTLMUserRequest::authenticate: auth state ntlm none. Received blob: '" << proxy_auth << "'");
+        auth_state = AUTHENTICATE_STATE_INITIAL;
+        safe_free(client_blob);
+        client_blob=xstrdup(blob);
+        conn->auth_type = AUTH_NTLM;
+        assert(conn->auth_user_request == NULL);
+        conn->auth_user_request = this;
+        AUTHUSERREQUESTLOCK(conn->auth_user_request, "conn");
+        this->request = request;
+        HTTPMSGLOCK(this->request);
+        return;
 
-        proxy_auth_hash = static_cast<ProxyAuthCachePointer *>(hash_lookup(proxy_auth_cache, ntlmhash));
+        break;
 
-        if (!proxy_auth_hash) {        /* not in the hash table */
-            debug(29, 4) ("authenticateNTLMAuthenticateUser: proxy-auth cache miss.\n");
-            ntlm_request->auth_state = AUTHENTICATE_STATE_RESPONSE;
-            /* verify with the ntlm helper */
-        } else {
-            debug(29, 4) ("authenticateNTLMAuthenticateUser: ntlm proxy-auth cache hit\n");
-            /* throw away the temporary entry */
-            ntlm_request->authserver_deferred = 0;
-            authenticateNTLMReleaseServer(this);
-            authenticateAuthUserMerge(auth_user, proxy_auth_hash->auth_user);
-            auth_user = proxy_auth_hash->auth_user;
-            this->user(auth_user);
-            ntlm_request->auth_state = AUTHENTICATE_STATE_DONE;
-            /* we found one */
-            debug(29, 9) ("found matching cache entry\n");
-            assert(auth_user->auth_type == AUTH_NTLM);
-            /* get the existing entries details */
-            ntlm_user = dynamic_cast<ntlm_user_t *>(auth_user);
-            debug(29, 9) ("Username to be used is %s\n", ntlm_user->username());
-            /* on ntlm auth we do not unlock the auth_user until the
-             * connection is dropped. Thank MS for this quirk */
-            auth_user->expiretime = current_time.tv_sec;
-        }
+    case AUTHENTICATE_STATE_INITIAL:
+        debugs(29, 1, "AuthNTLMUserRequest::authenticate: need to ask helper");
 
         return;
+
         break;
 
-    case AUTHENTICATE_STATE_RESPONSE:
-        /* auth-challenge pair cache miss. We've just got the response from the helper */
-        /*add to cache and let them through */
-        ntlm_request->auth_state = AUTHENTICATE_STATE_DONE;
-        /* this connection is authenticated */
-        debug(29, 4) ("authenticated\nch    %s\nauth     %s\nauthuser %s\n",
-                      ntlm_request->authchallenge,
-                      ntlm_request->ntlmauthenticate,
-                      ntlm_user->username());
-        /* cache entries have authenticateauthheaderchallengestring */
-        snprintf(ntlmhash, sizeof(ntlmhash) - 1, "%s%s",
-                 ntlm_request->ntlmauthenticate,
-                 ntlm_request->authchallenge);
-        /* see if this is an existing user with a different proxy_auth
-         * string */
 
-        if ((usernamehash = static_cast<AuthUserHashPointer *>(hash_lookup(proxy_auth_username_cache, ntlm_user->username())))
-           ) {
-            while ((usernamehash->user()->auth_type != auth_user->auth_type) && (usernamehash->next) && !authenticateNTLMcmpUsername(dynamic_cast<ntlm_user_t *>(usernamehash->user()), ntlm_user)
-                  )
-                usernamehash = static_cast<AuthUserHashPointer*>(usernamehash->next);
-            if (usernamehash->user()->auth_type == auth_user->auth_type) {
-                /*
-                 * add another link from the new proxy_auth to the
-                 * auth_user structure and update the information */
-                assert(proxy_auth_hash == NULL);
-                authenticateProxyAuthCacheAddLink(ntlmhash, usernamehash->user());
-                /* we can't seamlessly recheck the username due to the
-                 * challenge nature of the protocol. Just free the 
-                 * temporary auth_user */
-                authenticateAuthUserMerge(auth_user, usernamehash->user());
-                auth_user = usernamehash->user();
-                this->user(auth_user);
-            }
-        } else {
-            /* store user in hash's */
-            auth_user->addToNameCache();
-            authenticateProxyAuthCacheAddLink(ntlmhash, auth_user);
-        }
+    case AUTHENTICATE_STATE_IN_PROGRESS:
+        /* we should have received a blob from the client. Hand it off to
+         * some helper */
+        safe_free(client_blob);
 
-        /* set these to now because this is either a new login from an
-         * existing user or a new user */
-        auth_user->expiretime = current_time.tv_sec;
+        client_blob = xstrdup (blob);
+
+        if (this->request)
+            HTTPMSGUNLOCK(this->request);
+        this->request = request;
+        HTTPMSGLOCK(this->request);
         return;
+
         break;
 
     case AUTHENTICATE_STATE_DONE:
-        fatal("authenticateNTLMAuthenticateUser: unexpect auth state DONE! Report a bug to the squid developers.\n");
+        fatal("AuthNTLMUserRequest::authenticate: unexpect auth state DONE! Report a bug to the squid developers.\n");
+
         break;
 
     case AUTHENTICATE_STATE_FAILED:
         /* we've failed somewhere in authentication */
-        debug(29, 9) ("authenticateNTLMAuthenticateUser: auth state ntlm failed. %s\n", proxy_auth);
+        debugs(29, 9, "AuthNTLMUserRequest::authenticate: auth state ntlm failed. " << proxy_auth);
+
         return;
+
+        break;
     }
 
     return;
 }
 
-AuthNTLMUserRequest::AuthNTLMUserRequest() : ntlmnegotiate(NULL), authchallenge(NULL), ntlmauthenticate(NULL),
-        authserver(NULL), auth_state(AUTHENTICATE_STATE_NONE),
-        authserver_deferred(0), conn(NULL), _theUser(NULL)
-{}
+AuthNTLMUserRequest::AuthNTLMUserRequest() :
+        /*conn(NULL),*/ auth_state(AUTHENTICATE_STATE_NONE),
+        _theUser(NULL)
+{
+    waiting=0;
+    client_blob=0;
+    server_blob=0;
+    authserver=NULL;
+    request = NULL;
+}
 
 AuthNTLMUserRequest::~AuthNTLMUserRequest()
 {
-    if (ntlmnegotiate)
-        xfree(ntlmnegotiate);
+    safe_free(server_blob);
+    safe_free(client_blob);
 
-    if (authchallenge)
-        xfree(authchallenge);
-
-    if (ntlmauthenticate)
-        xfree(ntlmauthenticate);
-
-    if (authserver != NULL && authserver_deferred) {
-        debug(29, 9) ("authenticateNTLMRequestFree: releasing server '%p'\n", authserver);
+    if (authserver != NULL) {
+        debugs(29, 9, "AuthNTLMUserRequest::~AuthNTLMUserRequest: releasing server '" << authserver << "'");
         helperStatefulReleaseServer(authserver);
         authserver = NULL;
     }
+    if (request) {
+        HTTPMSGUNLOCK(request);
+        request = NULL;
+    }
 }
 
 void
@@ -1171,3 +748,9 @@ ntlmScheme::createConfig()
     return &ntlmConfig;
 }
 
+const char *
+AuthNTLMUserRequest::connLastHeader()
+{
+    return NULL;
+}
+