2 * Copyright (C) 1996-2015 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/negotiate/Config.h"
13 #include "auth/negotiate/User.h"
14 #include "auth/negotiate/UserRequest.h"
15 #include "auth/State.h"
16 #include "auth/User.h"
17 #include "client_side.h"
19 #include "format/Format.h"
22 #include "helper/Reply.h"
23 #include "HttpHeaderTools.h"
24 #include "HttpReply.h"
25 #include "HttpRequest.h"
27 #include "SquidTime.h"
29 Auth::Negotiate::UserRequest::UserRequest() :
37 Auth::Negotiate::UserRequest::~UserRequest()
39 assert(LockCount()==0);
40 safe_free(server_blob
);
41 safe_free(client_blob
);
46 HTTPMSGUNLOCK(request
);
52 Auth::Negotiate::UserRequest::connLastHeader()
58 Auth::Negotiate::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::Negotiate::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
); //CHECKME: can ever client_blob be 0 here?
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 negotiate authentication credentials.");
84 } else if (printResult
>= (int)sizeof(buf
))
85 debugs(29, 2, "Negotiate authentication credentials truncated.");
91 Auth::Negotiate::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_NEGOTIATE
)
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: Negotiate Authentication in unexpected state: " << user()->credentials());
115 return Auth::CRED_ERROR
;
120 Auth::Negotiate::UserRequest::startHelperLookup(HttpRequest
*, AccessLogEntry::Pointer
&al
, AUTHCB
* handler
, void *data
)
122 static char buf
[MAX_AUTHTOKEN_LEN
];
127 assert(user() != NULL
);
128 assert(user()->auth_type
== Auth::AUTH_NEGOTIATE
);
130 if (static_cast<Auth::Negotiate::Config
*>(Auth::Config::Find("negotiate"))->authenticateProgram
== NULL
) {
131 debugs(29, DBG_CRITICAL
, "ERROR: No Negotiate authentication program configured.");
136 debugs(29, 8, HERE
<< "credentials state is '" << user()->credentials() << "'");
138 const char *keyExtras
= helperRequestKeyExtras(request
, al
);
140 if (user()->credentials() == Auth::Pending
) {
142 printResult
= snprintf(buf
, sizeof(buf
), "YR %s %s\n", client_blob
, keyExtras
);
144 printResult
= snprintf(buf
, sizeof(buf
), "YR %s\n", client_blob
); //CHECKME: can ever client_blob be 0 here?
147 printResult
= snprintf(buf
, sizeof(buf
), "KK %s %s\n", client_blob
, keyExtras
);
149 printResult
= snprintf(buf
, sizeof(buf
), "KK %s\n", client_blob
);
152 if (printResult
< 0 || printResult
>= (int)sizeof(buf
)) {
154 debugs(29, DBG_CRITICAL
, "ERROR: Can not build negotiate authentication helper request");
156 debugs(29, DBG_CRITICAL
, "ERROR: Negotiate authentication helper request too big for the " << sizeof(buf
) << "-byte buffer");
163 safe_free(client_blob
);
165 helperStatefulSubmit(negotiateauthenticators
, buf
, Auth::Negotiate::UserRequest::HandleReply
,
166 new Auth::StateData(this, handler
, data
), authserver
);
170 * Atomic action: properly release the Negotiate auth helpers which may have been reserved
171 * for this request connections use.
174 Auth::Negotiate::UserRequest::releaseAuthServer()
177 debugs(29, 6, HERE
<< "releasing Negotiate auth server '" << authserver
<< "'");
178 helperStatefulReleaseServer(authserver
);
181 debugs(29, 6, HERE
<< "No Negotiate auth server to release.");
185 Auth::Negotiate::UserRequest::authenticate(HttpRequest
* aRequest
, ConnStateData
* conn
, Http::HdrType type
)
187 /* Check that we are in the client side, where we can generate
190 if (conn
== NULL
|| !cbdataReferenceValid(conn
)) {
191 user()->credentials(Auth::Failed
);
192 debugs(29, DBG_IMPORTANT
, "WARNING: Negotiate Authentication attempt to perform authentication without a connection!");
197 debugs(29, DBG_IMPORTANT
, "WARNING: Negotiate Authentication waiting for helper reply!");
202 debugs(29, 2, HERE
<< "need to challenge client '" << server_blob
<< "'!");
207 const char *proxy_auth
= aRequest
->header
.getStr(type
);
209 /* locate second word */
210 const char *blob
= proxy_auth
;
213 while (xisspace(*blob
) && *blob
)
216 while (!xisspace(*blob
) && *blob
)
219 while (xisspace(*blob
) && *blob
)
223 switch (user()->credentials()) {
225 case Auth::Unchecked
:
226 /* we've received a negotiate request. pass to a helper */
227 debugs(29, 9, HERE
<< "auth state negotiate none. Received blob: '" << proxy_auth
<< "'");
228 user()->credentials(Auth::Pending
);
229 safe_free(client_blob
);
230 client_blob
=xstrdup(blob
);
231 assert(conn
->getAuth() == NULL
);
232 conn
->setAuth(this, "new Negotiate handshake request");
234 HTTPMSGLOCK(request
);
238 debugs(29, DBG_IMPORTANT
, HERE
<< "need to ask helper");
241 case Auth::Handshake
:
242 /* we should have received a blob from the client. Hand it off to
244 safe_free(client_blob
);
245 client_blob
= xstrdup(blob
);
247 HTTPMSGUNLOCK(request
);
249 HTTPMSGLOCK(request
);
253 fatal("Auth::Negotiate::UserRequest::authenticate: unexpected auth state DONE! Report a bug to the squid developers.\n");
257 /* we've failed somewhere in authentication */
258 debugs(29, 9, HERE
<< "auth state negotiate failed. " << proxy_auth
);
264 Auth::Negotiate::UserRequest::HandleReply(void *data
, const Helper::Reply
&reply
)
266 Auth::StateData
*r
= static_cast<Auth::StateData
*>(data
);
268 debugs(29, 8, HERE
<< "helper: '" << reply
.whichServer
<< "' sent us reply=" << reply
);
270 if (!cbdataReferenceValid(r
->data
)) {
271 debugs(29, DBG_IMPORTANT
, "ERROR: Negotiate Authentication invalid callback data. helper '" << reply
.whichServer
<< "'.");
276 Auth::UserRequest::Pointer auth_user_request
= r
->auth_user_request
;
277 assert(auth_user_request
!= NULL
);
279 // add new helper kv-pair notes to the credentials object
280 // so that any transaction using those credentials can access them
281 auth_user_request
->user()->notes
.appendNewOnly(&reply
.notes
);
282 // remove any private credentials detail which got added.
283 auth_user_request
->user()->notes
.remove("token");
285 Auth::Negotiate::UserRequest
*lm_request
= dynamic_cast<Auth::Negotiate::UserRequest
*>(auth_user_request
.getRaw());
286 assert(lm_request
!= NULL
);
287 assert(lm_request
->waiting
);
289 lm_request
->waiting
= 0;
290 safe_free(lm_request
->client_blob
);
292 assert(auth_user_request
->user() != NULL
);
293 assert(auth_user_request
->user()->auth_type
== Auth::AUTH_NEGOTIATE
);
295 if (lm_request
->authserver
== NULL
)
296 lm_request
->authserver
= reply
.whichServer
.get(); // XXX: no locking?
298 assert(reply
.whichServer
== lm_request
->authserver
);
300 switch (reply
.result
) {
302 /* we have been given a blob to send to the client */
303 safe_free(lm_request
->server_blob
);
304 lm_request
->request
->flags
.mustKeepalive
= true;
305 if (lm_request
->request
->flags
.proxyKeepalive
) {
306 const char *tokenNote
= reply
.notes
.findFirst("token");
307 lm_request
->server_blob
= xstrdup(tokenNote
);
308 auth_user_request
->user()->credentials(Auth::Handshake
);
309 auth_user_request
->denyMessage("Authentication in progress");
310 debugs(29, 4, HERE
<< "Need to challenge the client with a server token: '" << tokenNote
<< "'");
312 auth_user_request
->user()->credentials(Auth::Failed
);
313 auth_user_request
->denyMessage("Negotiate authentication requires a persistent connection");
318 const char *userNote
= reply
.notes
.findFirst("user");
319 const char *tokenNote
= reply
.notes
.findFirst("token");
320 if (userNote
== NULL
|| tokenNote
== NULL
) {
321 // XXX: handle a success with no username better
323 fatalf("authenticateNegotiateHandleReply: *** Unsupported helper response ***, '%s'\n", reply
.other().content());
327 /* we're finished, release the helper */
328 auth_user_request
->user()->username(userNote
);
329 auth_user_request
->denyMessage("Login successful");
330 safe_free(lm_request
->server_blob
);
331 lm_request
->server_blob
= xstrdup(tokenNote
);
332 lm_request
->releaseAuthServer();
334 /* connection is authenticated */
335 debugs(29, 4, HERE
<< "authenticated user " << auth_user_request
->user()->username());
336 auto local_auth_user
= lm_request
->user();
337 auto cached_user
= Auth::Negotiate::User::Cache()->lookup(auth_user_request
->user()->userKey());
339 local_auth_user
->addToNameCache();
341 /* we can't seamlessly recheck the username due to the
342 * challenge-response nature of the protocol.
343 * Just free the temporary auth_user after merging as
344 * much of it new state into the existing one as possible */
345 cached_user
->absorb(local_auth_user
);
346 /* from here on we are working with the original cached credentials. */
347 local_auth_user
= cached_user
;
348 auth_user_request
->user(local_auth_user
);
350 /* set these to now because this is either a new login from an
351 * existing user or a new user */
352 local_auth_user
->expiretime
= current_time
.tv_sec
;
353 auth_user_request
->user()->credentials(Auth::Ok
);
354 debugs(29, 4, HERE
<< "Successfully validated user via Negotiate. Username '" << auth_user_request
->user()->username() << "'");
358 case Helper::Error
: {
359 const char *messageNote
= reply
.notes
.find("message");
360 const char *tokenNote
= reply
.notes
.findFirst("token");
362 /* authentication failure (wrong password, etc.) */
363 if (messageNote
!= NULL
)
364 auth_user_request
->denyMessage(messageNote
);
366 auth_user_request
->denyMessage("Negotiate Authentication denied with no reason given");
367 auth_user_request
->user()->credentials(Auth::Failed
);
368 safe_free(lm_request
->server_blob
);
369 if (tokenNote
!= NULL
)
370 lm_request
->server_blob
= xstrdup(tokenNote
);
371 lm_request
->releaseAuthServer();
372 debugs(29, 4, "Failed validating user via Negotiate. Result: " << reply
);
376 case Helper::Unknown
:
377 debugs(29, DBG_IMPORTANT
, "ERROR: Negotiate Authentication Helper '" << reply
.whichServer
<< "' crashed!.");
378 /* continue to the next case */
380 case Helper::TimedOut
:
381 case Helper::BrokenHelper
: {
382 /* TODO kick off a refresh process. This can occur after a YR or after
383 * a KK. If after a YR release the helper and resubmit the request via
384 * Authenticate Negotiate start.
385 * If after a KK deny the user's request w/ 407 and mark the helper as
387 const char *errNote
= reply
.notes
.find("message");
388 if (reply
.result
== Helper::Unknown
)
389 auth_user_request
->denyMessage("Internal Error");
390 else if (errNote
!= NULL
)
391 auth_user_request
->denyMessage(errNote
);
393 auth_user_request
->denyMessage("Negotiate Authentication failed with no reason given");
394 auth_user_request
->user()->credentials(Auth::Failed
);
395 safe_free(lm_request
->server_blob
);
396 lm_request
->releaseAuthServer();
397 debugs(29, DBG_IMPORTANT
, "ERROR: Negotiate Authentication validating user. Result: " << reply
);
401 if (lm_request
->request
) {
402 HTTPMSGUNLOCK(lm_request
->request
);
403 lm_request
->request
= NULL
;