2 * Copyright (C) 1996-2023 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 "http/Stream.h"
24 #include "HttpHeaderTools.h"
25 #include "HttpReply.h"
26 #include "HttpRequest.h"
29 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() != nullptr && user()->credentials() == Auth::Ok
) {
60 debugs(29, 9, "user authenticated.");
64 debugs(29, 9, "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() != nullptr);
127 assert(user()->auth_type
== Auth::AUTH_NEGOTIATE
);
129 if (static_cast<Auth::Negotiate::Config
*>(Auth::SchemeConfig::Find("negotiate"))->authenticateProgram
== nullptr) {
130 debugs(29, DBG_CRITICAL
, "ERROR: No Negotiate authentication program configured.");
135 debugs(29, 8, "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
), reservationId
);
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, reservationId
);
177 negotiateauthenticators
->cancelReservation(reservationId
);
178 reservationId
.clear();
180 debugs(29, 6, "No Negotiate auth server to release.");
184 Auth::Negotiate::UserRequest::authenticate(HttpRequest
* aRequest
, ConnStateData
* conn
, Http::HdrType type
)
186 /* Check that we are in the client side, where we can generate
189 if (conn
== nullptr || !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, "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, "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() == nullptr);
231 conn
->setAuth(this, "new Negotiate handshake request");
233 HTTPMSGLOCK(request
);
237 debugs(29, DBG_IMPORTANT
, "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, "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, reply
.reservationId
<< " got reply=" << reply
);
269 if (!cbdataReferenceValid(r
->data
)) {
270 debugs(29, DBG_IMPORTANT
, "ERROR: Negotiate Authentication invalid callback data (" << reply
.reservationId
<< ")");
275 Auth::UserRequest::Pointer auth_user_request
= r
->auth_user_request
;
276 assert(auth_user_request
!= nullptr);
278 // add new helper kv-pair notes to the credentials object
279 // so that any transaction using those credentials can access them
280 static const NotePairs::Names appendables
= { SBuf("group"), SBuf("tag") };
281 auth_user_request
->user()->notes
.replaceOrAddOrAppend(&reply
.notes
, appendables
);
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
!= nullptr);
287 assert(lm_request
->waiting
);
289 lm_request
->waiting
= 0;
290 safe_free(lm_request
->client_blob
);
292 assert(auth_user_request
->user() != nullptr);
293 assert(auth_user_request
->user()->auth_type
== Auth::AUTH_NEGOTIATE
);
295 if (!lm_request
->reservationId
)
296 lm_request
->reservationId
= reply
.reservationId
;
298 assert(reply
.reservationId
== lm_request
->reservationId
);
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
->setDenyMessage("Authentication in progress");
310 debugs(29, 4, "Need to challenge the client with a server token: '" << tokenNote
<< "'");
312 auth_user_request
->user()->credentials(Auth::Failed
);
313 auth_user_request
->setDenyMessage("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
== nullptr || tokenNote
== nullptr) {
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
->setDenyMessage("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, "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, "Successfully validated user via Negotiate. Username '" << auth_user_request
->user()->username() << "'");
359 /* authentication failure (wrong password, etc.) */
360 auth_user_request
->denyMessageFromHelper("Negotiate", reply
);
361 auth_user_request
->user()->credentials(Auth::Failed
);
362 safe_free(lm_request
->server_blob
);
363 if (const char *tokenNote
= reply
.notes
.findFirst("token"))
364 lm_request
->server_blob
= xstrdup(tokenNote
);
365 lm_request
->releaseAuthServer();
366 debugs(29, 4, "Failed validating user via Negotiate. Result: " << reply
);
369 case Helper::Unknown
:
370 debugs(29, DBG_IMPORTANT
, "ERROR: Negotiate Authentication Helper crashed (" << reply
.reservationId
<< ")");
373 case Helper::TimedOut
:
374 case Helper::BrokenHelper
:
375 /* TODO kick off a refresh process. This can occur after a YR or after
376 * a KK. If after a YR release the helper and resubmit the request via
377 * Authenticate Negotiate start.
378 * If after a KK deny the user's request w/ 407 and mark the helper as
380 if (reply
.result
== Helper::Unknown
)
381 auth_user_request
->setDenyMessage("Internal Error");
383 auth_user_request
->denyMessageFromHelper("Negotiate", reply
);
384 auth_user_request
->user()->credentials(Auth::Failed
);
385 safe_free(lm_request
->server_blob
);
386 lm_request
->releaseAuthServer();
387 debugs(29, DBG_IMPORTANT
, "ERROR: Negotiate Authentication validating user. Result: " << reply
);
391 if (lm_request
->request
) {
392 HTTPMSGUNLOCK(lm_request
->request
);
393 lm_request
->request
= nullptr;