Squid Web Cache master
Loading...
Searching...
No Matches
UserRequest.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9#include "squid.h"
10#include "AccessLogEntry.h"
12#include "auth/ntlm/Config.h"
13#include "auth/ntlm/User.h"
15#include "auth/State.h"
16#include "cbdata.h"
17#include "client_side.h"
18#include "fatal.h"
19#include "format/Format.h"
20#include "globals.h"
21#include "helper.h"
22#include "helper/Reply.h"
23#include "http/Stream.h"
24#include "HttpRequest.h"
25#include "MemBuf.h"
26
27Auth::Ntlm::UserRequest::UserRequest() :
28 server_blob(nullptr),
29 client_blob(nullptr),
30 waiting(0),
31 request(nullptr)
32{}
33
34Auth::Ntlm::UserRequest::~UserRequest()
35{
36 assert(LockCount()==0);
37 safe_free(server_blob);
38 safe_free(client_blob);
39
40 releaseAuthServer();
41
42 if (request) {
43 HTTPMSGUNLOCK(request);
44 request = nullptr;
45 }
46}
47
48const char *
49Auth::Ntlm::UserRequest::connLastHeader()
50{
51 return nullptr;
52}
53
54const char *
55Auth::Ntlm::UserRequest::credentialsStr()
56{
57 static char buf[MAX_AUTHTOKEN_LEN];
58 int printResult;
59 if (user()->credentials() == Auth::Pending) {
60 printResult = snprintf(buf, sizeof(buf), "YR %s\n", client_blob);
61 } else {
62 printResult = snprintf(buf, sizeof(buf), "KK %s\n", client_blob);
63 }
64
65 // truncation is OK because we are used only for logging
66 if (printResult < 0) {
67 debugs(29, 2, "Can not build ntlm authentication credentials.");
68 buf[0] = '\0';
69 } else if (printResult >= (int)sizeof(buf))
70 debugs(29, 2, "Ntlm authentication credentials truncated.");
71
72 return buf;
73}
74
76Auth::Ntlm::UserRequest::module_direction()
77{
78 /* null auth_user is checked for by Auth::UserRequest::direction() */
79
80 if (waiting || client_blob)
81 return Auth::CRED_LOOKUP; /* need helper response to continue */
82
83 if (user()->auth_type != Auth::AUTH_NTLM)
84 return Auth::CRED_ERROR;
85
86 switch (user()->credentials()) {
87
88 case Auth::Handshake:
89 assert(server_blob);
91
92 case Auth::Ok:
93 return Auth::CRED_VALID;
94
95 case Auth::Failed:
96 return Auth::CRED_ERROR; // XXX: really? not VALID or CHALLENGE?
97
98 default:
99 debugs(29, DBG_IMPORTANT, "WARNING: NTLM Authentication in unexpected state: " << user()->credentials());
100 return Auth::CRED_ERROR;
101 }
102}
103
104void
105Auth::Ntlm::UserRequest::startHelperLookup(HttpRequest *, AccessLogEntry::Pointer &al, AUTHCB * handler, void *data)
106{
107 static char buf[MAX_AUTHTOKEN_LEN];
108
109 assert(data);
110 assert(handler);
111
112 if (static_cast<Auth::Ntlm::Config*>(Auth::SchemeConfig::Find("ntlm"))->authenticateProgram == nullptr) {
113 debugs(29, DBG_CRITICAL, "ERROR: NTLM Start: no NTLM program configured.");
114 handler(data);
115 return;
116 }
117
118 debugs(29, 8, "credentials state is '" << user()->credentials() << "'");
119
120 const char *keyExtras = helperRequestKeyExtras(request, al);
121 int printResult = 0;
122 if (user()->credentials() == Auth::Pending) {
123 if (keyExtras)
124 printResult = snprintf(buf, sizeof(buf), "YR %s %s\n", client_blob, keyExtras);
125 else
126 printResult = snprintf(buf, sizeof(buf), "YR %s\n", client_blob); //CHECKME: can ever client_blob be 0 here?
127 } else {
128 if (keyExtras)
129 printResult = snprintf(buf, sizeof(buf), "KK %s %s\n", client_blob, keyExtras);
130 else
131 printResult = snprintf(buf, sizeof(buf), "KK %s\n", client_blob);
132 }
133 waiting = 1;
134
135 if (printResult < 0 || printResult >= (int)sizeof(buf)) {
136 if (printResult < 0)
137 debugs(29, DBG_CRITICAL, "ERROR: Can not build ntlm authentication helper request");
138 else
139 debugs(29, DBG_CRITICAL, "ERROR: Ntlm authentication helper request too big for the " << sizeof(buf) << "-byte buffer.");
140 handler(data);
141 return;
142 }
143
144 safe_free(client_blob);
145 helperStatefulSubmit(ntlmauthenticators, buf, Auth::Ntlm::UserRequest::HandleReply,
146 new Auth::StateData(this, handler, data), reservationId);
147}
148
153void
154Auth::Ntlm::UserRequest::releaseAuthServer()
155{
156 if (reservationId) {
157 debugs(29, 6, reservationId);
159 reservationId.clear();
160 } else
161 debugs(29, 6, "No NTLM auth server to release.");
162}
163
164void
165Auth::Ntlm::UserRequest::authenticate(HttpRequest * aRequest, ConnStateData * conn, Http::HdrType type)
166{
167 /* Check that we are in the client side, where we can generate
168 * auth challenges */
169
170 if (conn == nullptr || !cbdataReferenceValid(conn)) {
171 user()->credentials(Auth::Failed);
172 debugs(29, DBG_IMPORTANT, "WARNING: NTLM Authentication attempt to perform authentication without a connection!");
173 return;
174 }
175
176 if (waiting) {
177 debugs(29, DBG_IMPORTANT, "WARNING: NTLM Authentication waiting for helper reply!");
178 return;
179 }
180
181 if (server_blob) {
182 debugs(29, 2, "need to challenge client '" << server_blob << "'!");
183 return;
184 }
185
186 /* get header */
187 const char *proxy_auth = aRequest->header.getStr(type);
188
189 /* if proxy_auth is actually NULL, we'd better not manipulate it. */
190 if (!proxy_auth) {
191 debugs(29, 4, "WARNING: NTLM Authentication missing authorization header");
192 return;
193 }
194
195 /* locate second word */
196 const char *blob = proxy_auth;
197
198 while (xisspace(*blob) && *blob)
199 ++blob;
200
201 while (!xisspace(*blob) && *blob)
202 ++blob;
203
204 while (xisspace(*blob) && *blob)
205 ++blob;
206
207 switch (user()->credentials()) {
208
209 case Auth::Unchecked:
210 /* we've received a ntlm request. pass to a helper */
211 debugs(29, 9, "auth state ntlm none. Received blob: '" << proxy_auth << "'");
212 user()->credentials(Auth::Pending);
213 safe_free(client_blob);
214 client_blob=xstrdup(blob);
215 assert(conn->getAuth() == nullptr);
216 conn->setAuth(this, "new NTLM handshake request");
217 request = aRequest;
218 HTTPMSGLOCK(request);
219 break;
220
221 case Auth::Pending:
222 debugs(29, DBG_IMPORTANT, "need to ask helper");
223 break;
224
225 case Auth::Handshake:
226 /* we should have received a blob from the client. Hand it off to
227 * some helper */
228 safe_free(client_blob);
229 client_blob = xstrdup(blob);
230 if (request)
231 HTTPMSGUNLOCK(request);
232 request = aRequest;
233 HTTPMSGLOCK(request);
234 break;
235
236 case Auth::Ok:
237 fatal("Auth::Ntlm::UserRequest::authenticate: unexpected auth state DONE! Report a bug to the squid developers.\n");
238 break;
239
240 case Auth::Failed:
241 /* we've failed somewhere in authentication */
242 debugs(29, 9, "auth state ntlm failed. " << proxy_auth);
243 break;
244 }
245}
246
247void
248Auth::Ntlm::UserRequest::HandleReply(void *data, const Helper::Reply &reply)
249{
250 Auth::StateData *r = static_cast<Auth::StateData *>(data);
251
252 debugs(29, 8, reply.reservationId << " got reply=" << reply);
253
254 if (!cbdataReferenceValid(r->data)) {
255 debugs(29, DBG_IMPORTANT, "ERROR: NTLM Authentication invalid callback data(" << reply.reservationId <<")");
256 delete r;
257 return;
258 }
259
260 Auth::UserRequest::Pointer auth_user_request = r->auth_user_request;
261 assert(auth_user_request != nullptr);
262
263 // add new helper kv-pair notes to the credentials object
264 // so that any transaction using those credentials can access them
265 static const NotePairs::Names appendables = { SBuf("group"), SBuf("tag") };
266 auth_user_request->user()->notes.replaceOrAddOrAppend(&reply.notes, appendables);
267 // remove any private credentials detail which got added.
268 auth_user_request->user()->notes.remove("token");
269
270 Auth::Ntlm::UserRequest *lm_request = dynamic_cast<Auth::Ntlm::UserRequest *>(auth_user_request.getRaw());
271 assert(lm_request != nullptr);
272 assert(lm_request->waiting);
273
274 lm_request->waiting = 0;
275 safe_free(lm_request->client_blob);
276
277 assert(auth_user_request->user() != nullptr);
278 assert(auth_user_request->user()->auth_type == Auth::AUTH_NTLM);
279
280 if (!lm_request->reservationId)
281 lm_request->reservationId = reply.reservationId;
282 else
283 assert(lm_request->reservationId == reply.reservationId);
284
285 switch (reply.result) {
286 case Helper::TT:
287 /* we have been given a blob to send to the client */
288 safe_free(lm_request->server_blob);
289
290 if (lm_request->request)
291 lm_request->request->flags.mustKeepalive = true;
292
293 if (lm_request->request && lm_request->request->flags.proxyKeepalive) {
294 const char *serverBlob = reply.notes.findFirst("token");
295 lm_request->server_blob = xstrdup(serverBlob);
296 auth_user_request->user()->credentials(Auth::Handshake);
297 auth_user_request->setDenyMessage("Authentication in progress");
298 debugs(29, 4, "Need to challenge the client with a server token: '" << serverBlob << "'");
299 } else {
300 auth_user_request->user()->credentials(Auth::Failed);
301 auth_user_request->setDenyMessage("NTLM authentication requires a persistent connection");
302 }
303 break;
304
305 case Helper::Okay: {
306 /* we're finished, release the helper */
307 const char *userLabel = reply.notes.findFirst("user");
308 if (!userLabel) {
309 auth_user_request->user()->credentials(Auth::Failed);
310 safe_free(lm_request->server_blob);
311 lm_request->releaseAuthServer();
312 debugs(29, DBG_CRITICAL, "ERROR: NTLM Authentication helper returned no username. Result: " << reply);
313 break;
314 }
315 auth_user_request->user()->username(userLabel);
316 auth_user_request->setDenyMessage("Login successful");
317 safe_free(lm_request->server_blob);
318 lm_request->releaseAuthServer();
319
320 debugs(29, 4, "Successfully validated user via NTLM. Username '" << userLabel << "'");
321 /* connection is authenticated */
322 debugs(29, 4, "authenticated user " << auth_user_request->user()->username());
323 /* see if this is an existing user */
324 auto local_auth_user = lm_request->user();
325 auto cached_user = Auth::Ntlm::User::Cache()->lookup(auth_user_request->user()->userKey());
326 if (!cached_user) {
327 local_auth_user->addToNameCache();
328 } else {
329 /* we can't seamlessly recheck the username due to the
330 * challenge-response nature of the protocol.
331 * Just free the temporary auth_user after merging as
332 * much of it new state into the existing one as possible */
333 cached_user->absorb(local_auth_user);
334 /* from here on we are working with the original cached credentials. */
335 local_auth_user = cached_user;
336 auth_user_request->user(local_auth_user);
337 }
338 /* set these to now because this is either a new login from an
339 * existing user or a new user */
340 local_auth_user->expiretime = current_time.tv_sec;
341 auth_user_request->user()->credentials(Auth::Ok);
342 debugs(29, 4, "Successfully validated user via NTLM. Username '" << auth_user_request->user()->username() << "'");
343 }
344 break;
345
346 case Helper::Error:
347 /* authentication failure (wrong password, etc.) */
348 auth_user_request->denyMessageFromHelper("NTLM", reply);
349 auth_user_request->user()->credentials(Auth::Failed);
350 safe_free(lm_request->server_blob);
351 lm_request->releaseAuthServer();
352 debugs(29, 4, "Failed validating user via NTLM. Result: " << reply);
353 break;
354
355 case Helper::Unknown:
356 debugs(29, DBG_IMPORTANT, "ERROR: NTLM Authentication Helper crashed (" << reply.reservationId << ")");
357 [[fallthrough]];
358
359 case Helper::TimedOut:
361 /* TODO kick off a refresh process. This can occur after a YR or after
362 * a KK. If after a YR release the helper and resubmit the request via
363 * Authenticate NTLM start.
364 * If after a KK deny the user's request w/ 407 and mark the helper as
365 * Needing YR. */
366 if (reply.result == Helper::Unknown)
367 auth_user_request->setDenyMessage("Internal Error");
368 else
369 auth_user_request->denyMessageFromHelper("NTLM", reply);
370 auth_user_request->user()->credentials(Auth::Failed);
371 safe_free(lm_request->server_blob);
372 lm_request->releaseAuthServer();
373 debugs(29, DBG_IMPORTANT, "ERROR: NTLM Authentication validating user. Result: " << reply);
374 break;
375 }
376
377 if (lm_request->request) {
378 HTTPMSGUNLOCK(lm_request->request);
379 lm_request->request = nullptr;
380 }
381 r->handler(r->data);
382 delete r;
383}
384
void AUTHCB(void *)
Definition UserRequest.h:57
#define assert(EX)
Definition assert.h:17
Helper::StatefulClientPointer ntlmauthenticators
Definition Config.cc:36
int cbdataReferenceValid(const void *p)
Definition cbdata.cc:270
static SchemeConfig * Find(const char *proxy_auth)
UserRequest::Pointer auth_user_request
Definition State.h:39
AUTHCB * handler
Definition State.h:40
void * data
Definition State.h:38
void setDenyMessage(char const *)
void denyMessageFromHelper(char const *proto, const Helper::Reply &reply)
Sets the reason of 'authentication denied' helper response.
virtual User::Pointer user()
void setAuth(const Auth::UserRequest::Pointer &aur, const char *cause)
const Auth::UserRequest::Pointer & getAuth() const
NotePairs notes
Definition Reply.h:62
Helper::ResultCode result
The helper response 'result' field.
Definition Reply.h:59
Helper::ReservationId reservationId
The stateful replies should include the reservation ID.
Definition Reply.h:65
const char * getStr(Http::HdrType id) const
HttpHeader header
Definition Message.h:74
std::vector< SBuf > Names
Definition Notes.h:207
const char * findFirst(const char *noteKey) const
Definition Notes.cc:307
C * getRaw() const
Definition RefCount.h:89
Definition SBuf.h:94
void cancelReservation(const Helper::ReservationId reservation)
undo reserveServer(), clear the reservation and kick the queue
Definition helper.cc:618
#define DBG_IMPORTANT
Definition Stream.h:38
#define debugs(SECTION, LEVEL, CONTENT)
Definition Stream.h:192
#define DBG_CRITICAL
Definition Stream.h:37
void fatal(const char *message)
Definition fatal.cc:28
void helperStatefulSubmit(const statefulhelper::Pointer &hlp, const char *buf, HLPCB *callback, void *data, const Helper::ReservationId &reservation)
Definition helper.cc:587
void HTTPMSGUNLOCK(M *&a)
Definition Message.h:150
void HTTPMSGLOCK(Http::Message *a)
Definition Message.h:161
@ AUTH_NTLM
Definition Type.h:20
@ CRED_ERROR
ERROR in the auth module. Cannot determine the state of this request.
Definition UserRequest.h:68
@ CRED_CHALLENGE
Client needs to be challenged. secure token.
Definition UserRequest.h:65
@ CRED_LOOKUP
Credentials need to be validated with the backend helper.
Definition UserRequest.h:67
@ CRED_VALID
Credentials are valid and a up to date. The OK/Failed state is accurate.
Definition UserRequest.h:66
@ Unknown
Definition ResultCode.h:17
@ BrokenHelper
Definition ResultCode.h:20
@ TimedOut
Definition ResultCode.h:21
#define MAX_AUTHTOKEN_LEN
#define xstrdup
struct timeval current_time
the current UNIX time in timeval {seconds, microseconds} format
Definition gadgets.cc:18
#define safe_free(x)
Definition xalloc.h:73
#define xisspace(x)
Definition xis.h:15