2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 #include "AccessLogEntry.h"
11 #include "auth/CredentialsCache.h"
12 #include "auth/ntlm/Config.h"
13 #include "auth/ntlm/User.h"
14 #include "auth/ntlm/UserRequest.h"
15 #include "auth/State.h"
17 #include "client_side.h"
19 #include "format/Format.h"
22 #include "helper/Reply.h"
23 #include "http/Stream.h"
25 #include "HttpRequest.h"
27 #include "SquidTime.h"
29 Auth::Ntlm::UserRequest::UserRequest() :
37 Auth::Ntlm::UserRequest::~UserRequest()
39 assert(LockCount()==0);
40 safe_free(server_blob
);
41 safe_free(client_blob
);
46 HTTPMSGUNLOCK(request
);
52 Auth::Ntlm::UserRequest::connLastHeader()
58 Auth::Ntlm::UserRequest::authenticated() const
60 if (user() != NULL
&& user()->credentials() == Auth::Ok
) {
61 debugs(29, 9, HERE
<< "user authenticated.");
65 debugs(29, 9, HERE
<< "user not fully authenticated.");
70 Auth::Ntlm::UserRequest::credentialsStr()
72 static char buf
[MAX_AUTHTOKEN_LEN
];
74 if (user()->credentials() == Auth::Pending
) {
75 printResult
= snprintf(buf
, sizeof(buf
), "YR %s\n", client_blob
);
77 printResult
= snprintf(buf
, sizeof(buf
), "KK %s\n", client_blob
);
80 // truncation is OK because we are used only for logging
81 if (printResult
< 0) {
82 debugs(29, 2, "Can not build ntlm authentication credentials.");
84 } else if (printResult
>= (int)sizeof(buf
))
85 debugs(29, 2, "Ntlm authentication credentials truncated.");
91 Auth::Ntlm::UserRequest::module_direction()
93 /* null auth_user is checked for by Auth::UserRequest::direction() */
95 if (waiting
|| client_blob
)
96 return Auth::CRED_LOOKUP
; /* need helper response to continue */
98 if (user()->auth_type
!= Auth::AUTH_NTLM
)
99 return Auth::CRED_ERROR
;
101 switch (user()->credentials()) {
103 case Auth::Handshake
:
105 return Auth::CRED_CHALLENGE
;
108 return Auth::CRED_VALID
;
111 return Auth::CRED_ERROR
; // XXX: really? not VALID or CHALLENGE?
114 debugs(29, DBG_IMPORTANT
, "WARNING: NTLM Authentication in unexpected state: " << user()->credentials());
115 return Auth::CRED_ERROR
;
120 Auth::Ntlm::UserRequest::startHelperLookup(HttpRequest
*, AccessLogEntry::Pointer
&al
, AUTHCB
* handler
, void *data
)
122 static char buf
[MAX_AUTHTOKEN_LEN
];
127 if (static_cast<Auth::Ntlm::Config
*>(Auth::Config::Find("ntlm"))->authenticateProgram
== NULL
) {
128 debugs(29, DBG_CRITICAL
, "ERROR: NTLM Start: no NTLM program configured.");
133 debugs(29, 8, HERE
<< "credentials state is '" << user()->credentials() << "'");
135 const char *keyExtras
= helperRequestKeyExtras(request
, al
);
137 if (user()->credentials() == Auth::Pending
) {
139 printResult
= snprintf(buf
, sizeof(buf
), "YR %s %s\n", client_blob
, keyExtras
);
141 printResult
= snprintf(buf
, sizeof(buf
), "YR %s\n", client_blob
); //CHECKME: can ever client_blob be 0 here?
144 printResult
= snprintf(buf
, sizeof(buf
), "KK %s %s\n", client_blob
, keyExtras
);
146 printResult
= snprintf(buf
, sizeof(buf
), "KK %s\n", client_blob
);
150 if (printResult
< 0 || printResult
>= (int)sizeof(buf
)) {
152 debugs(29, DBG_CRITICAL
, "ERROR: Can not build ntlm authentication helper request");
154 debugs(29, DBG_CRITICAL
, "ERROR: Ntlm authentication helper request too big for the " << sizeof(buf
) << "-byte buffer.");
159 safe_free(client_blob
);
160 helperStatefulSubmit(ntlmauthenticators
, buf
, Auth::Ntlm::UserRequest::HandleReply
,
161 new Auth::StateData(this, handler
, data
), authserver
);
165 * Atomic action: properly release the NTLM auth helpers which may have been reserved
166 * for this request connections use.
169 Auth::Ntlm::UserRequest::releaseAuthServer()
172 debugs(29, 6, HERE
<< "releasing NTLM auth server '" << authserver
<< "'");
173 helperStatefulReleaseServer(authserver
);
176 debugs(29, 6, HERE
<< "No NTLM auth server to release.");
180 Auth::Ntlm::UserRequest::authenticate(HttpRequest
* aRequest
, ConnStateData
* conn
, Http::HdrType type
)
182 /* Check that we are in the client side, where we can generate
185 if (conn
== NULL
|| !cbdataReferenceValid(conn
)) {
186 user()->credentials(Auth::Failed
);
187 debugs(29, DBG_IMPORTANT
, "WARNING: NTLM Authentication attempt to perform authentication without a connection!");
192 debugs(29, DBG_IMPORTANT
, "WARNING: NTLM Authentication waiting for helper reply!");
197 debugs(29, 2, HERE
<< "need to challenge client '" << server_blob
<< "'!");
202 const char *proxy_auth
= aRequest
->header
.getStr(type
);
204 /* locate second word */
205 const char *blob
= proxy_auth
;
207 /* if proxy_auth is actually NULL, we'd better not manipulate it. */
209 while (xisspace(*blob
) && *blob
)
212 while (!xisspace(*blob
) && *blob
)
215 while (xisspace(*blob
) && *blob
)
219 switch (user()->credentials()) {
221 case Auth::Unchecked
:
222 /* we've received a ntlm request. pass to a helper */
223 debugs(29, 9, HERE
<< "auth state ntlm none. Received blob: '" << proxy_auth
<< "'");
224 user()->credentials(Auth::Pending
);
225 safe_free(client_blob
);
226 client_blob
=xstrdup(blob
);
227 assert(conn
->getAuth() == NULL
);
228 conn
->setAuth(this, "new NTLM handshake request");
230 HTTPMSGLOCK(request
);
234 debugs(29, DBG_IMPORTANT
, HERE
<< "need to ask helper");
237 case Auth::Handshake
:
238 /* we should have received a blob from the client. Hand it off to
240 safe_free(client_blob
);
241 client_blob
= xstrdup(blob
);
243 HTTPMSGUNLOCK(request
);
245 HTTPMSGLOCK(request
);
249 fatal("Auth::Ntlm::UserRequest::authenticate: unexpect auth state DONE! Report a bug to the squid developers.\n");
253 /* we've failed somewhere in authentication */
254 debugs(29, 9, HERE
<< "auth state ntlm failed. " << proxy_auth
);
260 Auth::Ntlm::UserRequest::HandleReply(void *data
, const Helper::Reply
&reply
)
262 Auth::StateData
*r
= static_cast<Auth::StateData
*>(data
);
264 debugs(29, 8, HERE
<< "helper: '" << reply
.whichServer
<< "' sent us reply=" << reply
);
266 if (!cbdataReferenceValid(r
->data
)) {
267 debugs(29, DBG_IMPORTANT
, "ERROR: NTLM Authentication invalid callback data. helper '" << reply
.whichServer
<< "'.");
272 Auth::UserRequest::Pointer auth_user_request
= r
->auth_user_request
;
273 assert(auth_user_request
!= NULL
);
275 // add new helper kv-pair notes to the credentials object
276 // so that any transaction using those credentials can access them
277 auth_user_request
->user()->notes
.appendNewOnly(&reply
.notes
);
278 // remove any private credentials detail which got added.
279 auth_user_request
->user()->notes
.remove("token");
281 Auth::Ntlm::UserRequest
*lm_request
= dynamic_cast<Auth::Ntlm::UserRequest
*>(auth_user_request
.getRaw());
282 assert(lm_request
!= NULL
);
283 assert(lm_request
->waiting
);
285 lm_request
->waiting
= 0;
286 safe_free(lm_request
->client_blob
);
288 assert(auth_user_request
->user() != NULL
);
289 assert(auth_user_request
->user()->auth_type
== Auth::AUTH_NTLM
);
291 if (lm_request
->authserver
== NULL
)
292 lm_request
->authserver
= reply
.whichServer
.get(); // XXX: no locking?
294 assert(reply
.whichServer
== lm_request
->authserver
);
296 switch (reply
.result
) {
298 /* we have been given a blob to send to the client */
299 safe_free(lm_request
->server_blob
);
300 lm_request
->request
->flags
.mustKeepalive
= true;
301 if (lm_request
->request
->flags
.proxyKeepalive
) {
302 const char *serverBlob
= reply
.notes
.findFirst("token");
303 lm_request
->server_blob
= xstrdup(serverBlob
);
304 auth_user_request
->user()->credentials(Auth::Handshake
);
305 auth_user_request
->denyMessage("Authentication in progress");
306 debugs(29, 4, HERE
<< "Need to challenge the client with a server token: '" << serverBlob
<< "'");
308 auth_user_request
->user()->credentials(Auth::Failed
);
309 auth_user_request
->denyMessage("NTLM authentication requires a persistent connection");
314 /* we're finished, release the helper */
315 const char *userLabel
= reply
.notes
.findFirst("user");
317 auth_user_request
->user()->credentials(Auth::Failed
);
318 safe_free(lm_request
->server_blob
);
319 lm_request
->releaseAuthServer();
320 debugs(29, DBG_CRITICAL
, "ERROR: NTLM Authentication helper returned no username. Result: " << reply
);
323 auth_user_request
->user()->username(userLabel
);
324 auth_user_request
->denyMessage("Login successful");
325 safe_free(lm_request
->server_blob
);
326 lm_request
->releaseAuthServer();
328 debugs(29, 4, HERE
<< "Successfully validated user via NTLM. Username '" << userLabel
<< "'");
329 /* connection is authenticated */
330 debugs(29, 4, HERE
<< "authenticated user " << auth_user_request
->user()->username());
331 /* see if this is an existing user */
332 auto local_auth_user
= lm_request
->user();
333 auto cached_user
= Auth::Ntlm::User::Cache()->lookup(auth_user_request
->user()->userKey());
335 local_auth_user
->addToNameCache();
337 /* we can't seamlessly recheck the username due to the
338 * challenge-response nature of the protocol.
339 * Just free the temporary auth_user after merging as
340 * much of it new state into the existing one as possible */
341 cached_user
->absorb(local_auth_user
);
342 /* from here on we are working with the original cached credentials. */
343 local_auth_user
= cached_user
;
344 auth_user_request
->user(local_auth_user
);
346 /* set these to now because this is either a new login from an
347 * existing user or a new user */
348 local_auth_user
->expiretime
= current_time
.tv_sec
;
349 auth_user_request
->user()->credentials(Auth::Ok
);
350 debugs(29, 4, HERE
<< "Successfully validated user via NTLM. Username '" << auth_user_request
->user()->username() << "'");
354 case Helper::Error
: {
355 /* authentication failure (wrong password, etc.) */
356 const char *errNote
= reply
.notes
.find("message");
358 auth_user_request
->denyMessage(errNote
);
360 auth_user_request
->denyMessage("NTLM Authentication denied with no reason given");
361 auth_user_request
->user()->credentials(Auth::Failed
);
362 safe_free(lm_request
->server_blob
);
363 lm_request
->releaseAuthServer();
364 debugs(29, 4, "Failed validating user via NTLM. Result: " << reply
);
368 case Helper::Unknown
:
369 debugs(29, DBG_IMPORTANT
, "ERROR: NTLM Authentication Helper '" << reply
.whichServer
<< "' crashed!.");
370 /* continue to the next case */
372 case Helper::TimedOut
:
373 case Helper::BrokenHelper
: {
374 /* TODO kick off a refresh process. This can occur after a YR or after
375 * a KK. If after a YR release the helper and resubmit the request via
376 * Authenticate NTLM start.
377 * If after a KK deny the user's request w/ 407 and mark the helper as
379 const char *errNote
= reply
.notes
.find("message");
380 if (reply
.result
== Helper::Unknown
)
381 auth_user_request
->denyMessage("Internal Error");
382 else if (errNote
!= NULL
)
383 auth_user_request
->denyMessage(errNote
);
385 auth_user_request
->denyMessage("NTLM Authentication failed with no reason given");
386 auth_user_request
->user()->credentials(Auth::Failed
);
387 safe_free(lm_request
->server_blob
);
388 lm_request
->releaseAuthServer();
389 debugs(29, DBG_IMPORTANT
, "ERROR: NTLM Authentication validating user. Result: " << reply
);
394 if (lm_request
->request
) {
395 HTTPMSGUNLOCK(lm_request
->request
);
396 lm_request
->request
= NULL
;