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/negotiate/Config.h"
12 #include "auth/negotiate/UserRequest.h"
13 #include "auth/State.h"
14 #include "auth/User.h"
15 #include "client_side.h"
17 #include "format/Format.h"
20 #include "helper/Reply.h"
21 #include "HttpHeaderTools.h"
22 #include "HttpReply.h"
23 #include "HttpRequest.h"
25 #include "SquidTime.h"
27 Auth::Negotiate::UserRequest::UserRequest()
36 Auth::Negotiate::UserRequest::~UserRequest()
38 assert(LockCount()==0);
39 safe_free(server_blob
);
40 safe_free(client_blob
);
45 HTTPMSGUNLOCK(request
);
51 Auth::Negotiate::UserRequest::connLastHeader()
57 Auth::Negotiate::UserRequest::authenticated() const
59 if (user() != NULL
&& user()->credentials() == Auth::Ok
) {
60 debugs(29, 9, HERE
<< "user authenticated.");
64 debugs(29, 9, HERE
<< "user not fully authenticated.");
69 Auth::Negotiate::UserRequest::credentialsStr()
71 static char buf
[MAX_AUTHTOKEN_LEN
];
73 if (user()->credentials() == Auth::Pending
) {
74 printResult
= snprintf(buf
, sizeof(buf
), "YR %s\n", client_blob
); //CHECKME: can ever client_blob be 0 here?
76 printResult
= snprintf(buf
, sizeof(buf
), "KK %s\n", client_blob
);
79 // truncation is OK because we are used only for logging
80 if (printResult
< 0) {
81 debugs(29, 2, "Can not build negotiate authentication credentials.");
83 } else if (printResult
>= (int)sizeof(buf
))
84 debugs(29, 2, "Negotiate authentication credentials truncated.");
90 Auth::Negotiate::UserRequest::module_direction()
92 /* null auth_user is checked for by Auth::UserRequest::direction() */
94 if (waiting
|| client_blob
)
95 return Auth::CRED_LOOKUP
; /* need helper response to continue */
97 if (user()->auth_type
!= Auth::AUTH_NEGOTIATE
)
98 return Auth::CRED_ERROR
;
100 switch (user()->credentials()) {
102 case Auth::Handshake
:
104 return Auth::CRED_CHALLENGE
;
107 return Auth::CRED_VALID
;
110 return Auth::CRED_ERROR
; // XXX: really? not VALID or CHALLENGE?
113 debugs(29, DBG_IMPORTANT
, "WARNING: Negotiate Authentication in unexpected state: " << user()->credentials());
114 return Auth::CRED_ERROR
;
119 Auth::Negotiate::UserRequest::startHelperLookup(HttpRequest
*, AccessLogEntry::Pointer
&al
, AUTHCB
* handler
, void *data
)
121 static char buf
[MAX_AUTHTOKEN_LEN
];
126 assert(user() != NULL
);
127 assert(user()->auth_type
== Auth::AUTH_NEGOTIATE
);
129 if (static_cast<Auth::Negotiate::Config
*>(Auth::Config::Find("negotiate"))->authenticateProgram
== NULL
) {
130 debugs(29, DBG_CRITICAL
, "ERROR: No Negotiate authentication program configured.");
135 debugs(29, 8, HERE
<< "credentials state is '" << user()->credentials() << "'");
137 const char *keyExtras
= helperRequestKeyExtras(request
, al
);
139 if (user()->credentials() == Auth::Pending
) {
141 printResult
= snprintf(buf
, sizeof(buf
), "YR %s %s\n", client_blob
, keyExtras
);
143 printResult
= snprintf(buf
, sizeof(buf
), "YR %s\n", client_blob
); //CHECKME: can ever client_blob be 0 here?
146 printResult
= snprintf(buf
, sizeof(buf
), "KK %s %s\n", client_blob
, keyExtras
);
148 printResult
= snprintf(buf
, sizeof(buf
), "KK %s\n", client_blob
);
151 if (printResult
< 0 || printResult
>= (int)sizeof(buf
)) {
153 debugs(29, DBG_CRITICAL
, "ERROR: Can not build negotiate authentication helper request");
155 debugs(29, DBG_CRITICAL
, "ERROR: Negotiate authentication helper request too big for the " << sizeof(buf
) << "-byte buffer");
162 safe_free(client_blob
);
164 helperStatefulSubmit(negotiateauthenticators
, buf
, Auth::Negotiate::UserRequest::HandleReply
,
165 new Auth::StateData(this, handler
, data
), authserver
);
169 * Atomic action: properly release the Negotiate auth helpers which may have been reserved
170 * for this request connections use.
173 Auth::Negotiate::UserRequest::releaseAuthServer()
176 debugs(29, 6, HERE
<< "releasing Negotiate auth server '" << authserver
<< "'");
177 helperStatefulReleaseServer(authserver
);
180 debugs(29, 6, HERE
<< "No Negotiate auth server to release.");
184 Auth::Negotiate::UserRequest::authenticate(HttpRequest
* aRequest
, ConnStateData
* conn
, http_hdr_type type
)
186 /* Check that we are in the client side, where we can generate
189 if (conn
== NULL
|| !cbdataReferenceValid(conn
)) {
190 user()->credentials(Auth::Failed
);
191 debugs(29, DBG_IMPORTANT
, "WARNING: Negotiate Authentication attempt to perform authentication without a connection!");
196 debugs(29, DBG_IMPORTANT
, "WARNING: Negotiate Authentication waiting for helper reply!");
201 debugs(29, 2, HERE
<< "need to challenge client '" << server_blob
<< "'!");
206 const char *proxy_auth
= aRequest
->header
.getStr(type
);
208 /* locate second word */
209 const char *blob
= proxy_auth
;
212 while (xisspace(*blob
) && *blob
)
215 while (!xisspace(*blob
) && *blob
)
218 while (xisspace(*blob
) && *blob
)
222 switch (user()->credentials()) {
224 case Auth::Unchecked
:
225 /* we've received a negotiate request. pass to a helper */
226 debugs(29, 9, HERE
<< "auth state negotiate none. Received blob: '" << proxy_auth
<< "'");
227 user()->credentials(Auth::Pending
);
228 safe_free(client_blob
);
229 client_blob
=xstrdup(blob
);
230 assert(conn
->getAuth() == NULL
);
231 conn
->setAuth(this, "new Negotiate handshake request");
233 HTTPMSGLOCK(request
);
237 debugs(29, DBG_IMPORTANT
, HERE
<< "need to ask helper");
240 case Auth::Handshake
:
241 /* we should have received a blob from the client. Hand it off to
243 safe_free(client_blob
);
244 client_blob
= xstrdup(blob
);
246 HTTPMSGUNLOCK(request
);
248 HTTPMSGLOCK(request
);
252 fatal("Auth::Negotiate::UserRequest::authenticate: unexpected auth state DONE! Report a bug to the squid developers.\n");
256 /* we've failed somewhere in authentication */
257 debugs(29, 9, HERE
<< "auth state negotiate failed. " << proxy_auth
);
263 Auth::Negotiate::UserRequest::HandleReply(void *data
, const Helper::Reply
&reply
)
265 Auth::StateData
*r
= static_cast<Auth::StateData
*>(data
);
267 debugs(29, 8, HERE
<< "helper: '" << reply
.whichServer
<< "' sent us reply=" << reply
);
269 if (!cbdataReferenceValid(r
->data
)) {
270 debugs(29, DBG_IMPORTANT
, "ERROR: Negotiate Authentication invalid callback data. helper '" << reply
.whichServer
<< "'.");
275 Auth::UserRequest::Pointer auth_user_request
= r
->auth_user_request
;
276 assert(auth_user_request
!= NULL
);
278 // add new helper kv-pair notes to the credentials object
279 // so that any transaction using those credentials can access them
280 auth_user_request
->user()->notes
.appendNewOnly(&reply
.notes
);
281 // remove any private credentials detail which got added.
282 auth_user_request
->user()->notes
.remove("token");
284 Auth::Negotiate::UserRequest
*lm_request
= dynamic_cast<Auth::Negotiate::UserRequest
*>(auth_user_request
.getRaw());
285 assert(lm_request
!= NULL
);
286 assert(lm_request
->waiting
);
288 lm_request
->waiting
= 0;
289 safe_free(lm_request
->client_blob
);
291 assert(auth_user_request
->user() != NULL
);
292 assert(auth_user_request
->user()->auth_type
== Auth::AUTH_NEGOTIATE
);
294 if (lm_request
->authserver
== NULL
)
295 lm_request
->authserver
= reply
.whichServer
.get(); // XXX: no locking?
297 assert(reply
.whichServer
== lm_request
->authserver
);
299 switch (reply
.result
) {
301 /* we have been given a blob to send to the client */
302 safe_free(lm_request
->server_blob
);
303 lm_request
->request
->flags
.mustKeepalive
= true;
304 if (lm_request
->request
->flags
.proxyKeepalive
) {
305 const char *tokenNote
= reply
.notes
.findFirst("token");
306 lm_request
->server_blob
= xstrdup(tokenNote
);
307 auth_user_request
->user()->credentials(Auth::Handshake
);
308 auth_user_request
->denyMessage("Authentication in progress");
309 debugs(29, 4, HERE
<< "Need to challenge the client with a server token: '" << tokenNote
<< "'");
311 auth_user_request
->user()->credentials(Auth::Failed
);
312 auth_user_request
->denyMessage("Negotiate authentication requires a persistent connection");
317 const char *userNote
= reply
.notes
.findFirst("user");
318 const char *tokenNote
= reply
.notes
.findFirst("token");
319 if (userNote
== NULL
|| tokenNote
== NULL
) {
320 // XXX: handle a success with no username better
322 fatalf("authenticateNegotiateHandleReply: *** Unsupported helper response ***, '%s'\n", reply
.other().content());
326 /* we're finished, release the helper */
327 auth_user_request
->user()->username(userNote
);
328 auth_user_request
->denyMessage("Login successful");
329 safe_free(lm_request
->server_blob
);
330 lm_request
->server_blob
= xstrdup(tokenNote
);
331 lm_request
->releaseAuthServer();
333 /* connection is authenticated */
334 debugs(29, 4, HERE
<< "authenticated user " << auth_user_request
->user()->username());
335 /* see if this is an existing user */
336 AuthUserHashPointer
*usernamehash
= static_cast<AuthUserHashPointer
*>(hash_lookup(proxy_auth_username_cache
, auth_user_request
->user()->userKey()));
337 Auth::User::Pointer local_auth_user
= lm_request
->user();
338 while (usernamehash
&& (usernamehash
->user()->auth_type
!= Auth::AUTH_NEGOTIATE
||
339 strcmp(usernamehash
->user()->userKey(), auth_user_request
->user()->userKey()) != 0))
340 usernamehash
= static_cast<AuthUserHashPointer
*>(usernamehash
->next
);
342 /* we can't seamlessly recheck the username due to the
343 * challenge-response nature of the protocol.
344 * Just free the temporary auth_user after merging as
345 * much of it new state into the existing one as possible */
346 usernamehash
->user()->absorb(local_auth_user
);
347 /* from here on we are working with the original cached credentials. */
348 local_auth_user
= usernamehash
->user();
349 auth_user_request
->user(local_auth_user
);
351 /* store user in hash's */
352 local_auth_user
->addToNameCache();
354 /* set these to now because this is either a new login from an
355 * existing user or a new user */
356 local_auth_user
->expiretime
= current_time
.tv_sec
;
357 auth_user_request
->user()->credentials(Auth::Ok
);
358 debugs(29, 4, HERE
<< "Successfully validated user via Negotiate. Username '" << auth_user_request
->user()->username() << "'");
362 case Helper::Error
: {
363 const char *messageNote
= reply
.notes
.find("message");
364 const char *tokenNote
= reply
.notes
.findFirst("token");
366 /* authentication failure (wrong password, etc.) */
367 if (messageNote
!= NULL
)
368 auth_user_request
->denyMessage(messageNote
);
370 auth_user_request
->denyMessage("Negotiate Authentication denied with no reason given");
371 auth_user_request
->user()->credentials(Auth::Failed
);
372 safe_free(lm_request
->server_blob
);
373 if (tokenNote
!= NULL
)
374 lm_request
->server_blob
= xstrdup(tokenNote
);
375 lm_request
->releaseAuthServer();
376 debugs(29, 4, "Failed validating user via Negotiate. Result: " << reply
);
380 case Helper::Unknown
:
381 debugs(29, DBG_IMPORTANT
, "ERROR: Negotiate Authentication Helper '" << reply
.whichServer
<< "' crashed!.");
382 /* continue to the next case */
384 case Helper::TimedOut
:
385 case Helper::BrokenHelper
: {
386 /* TODO kick off a refresh process. This can occur after a YR or after
387 * a KK. If after a YR release the helper and resubmit the request via
388 * Authenticate Negotiate start.
389 * If after a KK deny the user's request w/ 407 and mark the helper as
391 const char *errNote
= reply
.notes
.find("message");
392 if (reply
.result
== Helper::Unknown
)
393 auth_user_request
->denyMessage("Internal Error");
394 else if (errNote
!= NULL
)
395 auth_user_request
->denyMessage(errNote
);
397 auth_user_request
->denyMessage("Negotiate Authentication failed with no reason given");
398 auth_user_request
->user()->credentials(Auth::Failed
);
399 safe_free(lm_request
->server_blob
);
400 lm_request
->releaseAuthServer();
401 debugs(29, DBG_IMPORTANT
, "ERROR: Negotiate Authentication validating user. Result: " << reply
);
405 if (lm_request
->request
) {
406 HTTPMSGUNLOCK(lm_request
->request
);
407 lm_request
->request
= NULL
;