2 #include "auth/negotiate/auth_negotiate.h"
3 #include "auth/negotiate/UserRequest.h"
4 #include "auth/State.h"
8 #include "HttpRequest.h"
11 Auth::Negotiate::UserRequest::UserRequest()
20 Auth::Negotiate::UserRequest::~UserRequest()
22 assert(RefCountCount()==0);
23 safe_free(server_blob
);
24 safe_free(client_blob
);
29 HTTPMSGUNLOCK(request
);
35 Auth::Negotiate::UserRequest::connLastHeader()
41 Auth::Negotiate::UserRequest::authenticated() const
43 if (user() != NULL
&& user()->credentials() == Auth::Ok
) {
44 debugs(29, 9, HERE
<< "user authenticated.");
48 debugs(29, 9, HERE
<< "user not fully authenticated.");
53 Auth::Negotiate::UserRequest::module_direction()
55 /* null auth_user is checked for by Auth::UserRequest::direction() */
57 if (waiting
|| client_blob
)
58 return Auth::CRED_LOOKUP
; /* need helper response to continue */
60 if (user()->auth_type
!= Auth::AUTH_NEGOTIATE
)
61 return Auth::CRED_ERROR
;
63 switch (user()->credentials()) {
67 return Auth::CRED_CHALLENGE
;
70 return Auth::CRED_VALID
;
73 return Auth::CRED_ERROR
; // XXX: really? not VALID or CHALLENGE?
76 debugs(29, DBG_IMPORTANT
, "WARNING: Negotiate Authentication in unexpected state: " << user()->credentials());
77 return Auth::CRED_ERROR
;
82 Auth::Negotiate::UserRequest::module_start(RH
* handler
, void *data
)
84 static char buf
[MAX_AUTHTOKEN_LEN
];
89 assert(user() != NULL
);
90 assert(user()->auth_type
== Auth::AUTH_NEGOTIATE
);
92 if (static_cast<Auth::Negotiate::Config
*>(Auth::Config::Find("negotiate"))->authenticateProgram
== NULL
) {
93 debugs(29, DBG_CRITICAL
, "ERROR: No Negotiate authentication program configured.");
98 debugs(29, 8, HERE
<< "credentials state is '" << user()->credentials() << "'");
100 if (user()->credentials() == Auth::Pending
) {
101 snprintf(buf
, sizeof(buf
), "YR %s\n", client_blob
); //CHECKME: can ever client_blob be 0 here?
103 snprintf(buf
, sizeof(buf
), "KK %s\n", client_blob
);
108 safe_free(client_blob
);
110 helperStatefulSubmit(negotiateauthenticators
, buf
, Auth::Negotiate::UserRequest::HandleReply
,
111 new Auth::StateData(this, handler
, data
), authserver
);
115 * Atomic action: properly release the Negotiate auth helpers which may have been reserved
116 * for this request connections use.
119 Auth::Negotiate::UserRequest::releaseAuthServer()
122 debugs(29, 6, HERE
<< "releasing Negotiate auth server '" << authserver
<< "'");
123 helperStatefulReleaseServer(authserver
);
126 debugs(29, 6, HERE
<< "No Negotiate auth server to release.");
129 /* clear any connection related authentication details */
131 Auth::Negotiate::UserRequest::onConnectionClose(ConnStateData
*conn
)
133 assert(conn
!= NULL
);
135 debugs(29, 8, HERE
<< "closing connection '" << conn
<< "' (this is '" << this << "')");
137 if (conn
->auth_user_request
== NULL
) {
138 debugs(29, 8, HERE
<< "no auth_user_request");
144 /* unlock the connection based lock */
145 debugs(29, 9, HERE
<< "Unlocking auth_user from the connection '" << conn
<< "'.");
147 conn
->auth_user_request
= NULL
;
151 Auth::Negotiate::UserRequest::authenticate(HttpRequest
* aRequest
, ConnStateData
* conn
, http_hdr_type type
)
155 /* Check that we are in the client side, where we can generate
158 if (conn
== NULL
|| !cbdataReferenceValid(conn
)) {
159 user()->credentials(Auth::Failed
);
160 debugs(29, DBG_IMPORTANT
, "WARNING: Negotiate Authentication attempt to perform authentication without a connection!");
165 debugs(29, DBG_IMPORTANT
, "WARNING: Negotiate Authentication waiting for helper reply!");
170 debugs(29, 2, HERE
<< "need to challenge client '" << server_blob
<< "'!");
175 const char *proxy_auth
= aRequest
->header
.getStr(type
);
177 /* locate second word */
178 const char *blob
= proxy_auth
;
181 while (xisspace(*blob
) && *blob
)
184 while (!xisspace(*blob
) && *blob
)
187 while (xisspace(*blob
) && *blob
)
191 switch (user()->credentials()) {
193 case Auth::Unchecked
:
194 /* we've received a negotiate request. pass to a helper */
195 debugs(29, 9, HERE
<< "auth state negotiate none. Received blob: '" << proxy_auth
<< "'");
196 user()->credentials(Auth::Pending
);
197 safe_free(client_blob
);
198 client_blob
=xstrdup(blob
);
199 assert(conn
->auth_user_request
== NULL
);
200 conn
->auth_user_request
= this;
202 HTTPMSGLOCK(request
);
206 debugs(29, 1, HERE
<< "need to ask helper");
209 case Auth::Handshake
:
210 /* we should have received a blob from the client. Hand it off to
212 safe_free(client_blob
);
213 client_blob
= xstrdup(blob
);
215 HTTPMSGUNLOCK(request
);
217 HTTPMSGLOCK(request
);
221 fatal("Auth::Negotiate::UserRequest::authenticate: unexpected auth state DONE! Report a bug to the squid developers.\n");
225 /* we've failed somewhere in authentication */
226 debugs(29, 9, HERE
<< "auth state negotiate failed. " << proxy_auth
);
232 Auth::Negotiate::UserRequest::HandleReply(void *data
, void *lastserver
, char *reply
)
234 Auth::StateData
*r
= static_cast<Auth::StateData
*>(data
);
236 char *blob
, *arg
= NULL
;
238 debugs(29, 8, HERE
<< "helper: '" << lastserver
<< "' sent us '" << (reply
? reply
: "<NULL>") << "'");
240 if (!cbdataReferenceValid(r
->data
)) {
241 debugs(29, DBG_IMPORTANT
, "ERROR: Negotiate Authentication invalid callback data. helper '" << lastserver
<< "'.");
247 debugs(29, DBG_IMPORTANT
, "ERROR: Negotiate Authentication Helper '" << lastserver
<< "' crashed!.");
248 reply
= (char *)"BH Internal error";
251 Auth::UserRequest::Pointer auth_user_request
= r
->auth_user_request
;
252 assert(auth_user_request
!= NULL
);
254 Auth::Negotiate::UserRequest
*lm_request
= dynamic_cast<Auth::Negotiate::UserRequest
*>(auth_user_request
.getRaw());
255 assert(lm_request
!= NULL
);
256 assert(lm_request
->waiting
);
258 lm_request
->waiting
= 0;
259 safe_free(lm_request
->client_blob
);
261 assert(auth_user_request
->user() != NULL
);
262 assert(auth_user_request
->user()->auth_type
== Auth::AUTH_NEGOTIATE
);
264 if (lm_request
->authserver
== NULL
)
265 lm_request
->authserver
= static_cast<helper_stateful_server
*>(lastserver
);
267 assert(lm_request
->authserver
== lastserver
);
269 /* seperate out the useful data */
270 blob
= strchr(reply
, ' ');
274 arg
= strchr(blob
+ 1, ' ');
279 if (strncasecmp(reply
, "TT ", 3) == 0) {
280 /* we have been given a blob to send to the client */
283 safe_free(lm_request
->server_blob
);
284 lm_request
->request
->flags
.must_keepalive
= 1;
285 if (lm_request
->request
->flags
.proxy_keepalive
) {
286 lm_request
->server_blob
= xstrdup(blob
);
287 auth_user_request
->user()->credentials(Auth::Handshake
);
288 auth_user_request
->denyMessage("Authentication in progress");
289 debugs(29, 4, HERE
<< "Need to challenge the client with a server blob '" << blob
<< "'");
291 auth_user_request
->user()->credentials(Auth::Failed
);
292 auth_user_request
->denyMessage("NTLM authentication requires a persistent connection");
294 } else if (strncasecmp(reply
, "AF ", 3) == 0 && arg
!= NULL
) {
295 /* we're finished, release the helper */
300 auth_user_request
->user()->username(arg
);
301 auth_user_request
->denyMessage("Login successful");
302 safe_free(lm_request
->server_blob
);
303 lm_request
->server_blob
= xstrdup(blob
);
304 lm_request
->releaseAuthServer();
306 /* connection is authenticated */
307 debugs(29, 4, HERE
<< "authenticated user " << auth_user_request
->user()->username());
308 /* see if this is an existing user with a different proxy_auth
310 AuthUserHashPointer
*usernamehash
= static_cast<AuthUserHashPointer
*>(hash_lookup(proxy_auth_username_cache
, auth_user_request
->user()->username()));
311 Auth::User::Pointer local_auth_user
= lm_request
->user();
312 while (usernamehash
&& (usernamehash
->user()->auth_type
!= Auth::AUTH_NEGOTIATE
||
313 strcmp(usernamehash
->user()->username(), auth_user_request
->user()->username()) != 0))
314 usernamehash
= static_cast<AuthUserHashPointer
*>(usernamehash
->next
);
316 /* we can't seamlessly recheck the username due to the
317 * challenge-response nature of the protocol.
318 * Just free the temporary auth_user after merging as
319 * much of it new state into the existing one as possible */
320 usernamehash
->user()->absorb(local_auth_user
);
321 /* from here on we are working with the original cached credentials. */
322 local_auth_user
= usernamehash
->user();
323 auth_user_request
->user(local_auth_user
);
325 /* store user in hash's */
326 local_auth_user
->addToNameCache();
328 /* set these to now because this is either a new login from an
329 * existing user or a new user */
330 local_auth_user
->expiretime
= current_time
.tv_sec
;
331 auth_user_request
->user()->credentials(Auth::Ok
);
332 debugs(29, 4, HERE
<< "Successfully validated user via Negotiate. Username '" << blob
<< "'");
334 } else if (strncasecmp(reply
, "NA ", 3) == 0 && arg
!= NULL
) {
335 /* authentication failure (wrong password, etc.) */
340 auth_user_request
->denyMessage(arg
);
341 auth_user_request
->user()->credentials(Auth::Failed
);
342 safe_free(lm_request
->server_blob
);
343 lm_request
->server_blob
= xstrdup(blob
);
344 lm_request
->releaseAuthServer();
345 debugs(29, 4, HERE
<< "Failed validating user via Negotiate. Error returned '" << blob
<< "'");
346 } else if (strncasecmp(reply
, "BH ", 3) == 0) {
347 /* TODO kick off a refresh process. This can occur after a YR or after
348 * a KK. If after a YR release the helper and resubmit the request via
349 * Authenticate Negotiate start.
350 * If after a KK deny the user's request w/ 407 and mark the helper as
352 auth_user_request
->denyMessage(blob
);
353 auth_user_request
->user()->credentials(Auth::Failed
);
354 safe_free(lm_request
->server_blob
);
355 lm_request
->releaseAuthServer();
356 debugs(29, DBG_IMPORTANT
, "ERROR: Negotiate Authentication validating user. Error returned '" << reply
<< "'");
359 fatalf("authenticateNegotiateHandleReply: *** Unsupported helper response ***, '%s'\n", reply
);
362 lm_request
->request
= NULL
;
363 r
->handler(r
->data
, NULL
);
368 Auth::Negotiate::UserRequest::addAuthenticationInfoHeader(HttpReply
* rep
, int accel
)
375 /* don't add to authentication error pages */
376 if ((!accel
&& rep
->sline
.status
== HTTP_PROXY_AUTHENTICATION_REQUIRED
)
377 || (accel
&& rep
->sline
.status
== HTTP_UNAUTHORIZED
))
380 type
= accel
? HDR_AUTHENTICATION_INFO
: HDR_PROXY_AUTHENTICATION_INFO
;
381 httpHeaderPutStrf(&rep
->header
, type
, "Negotiate %s", server_blob
);
383 safe_free(server_blob
);