]> git.ipfire.org Git - thirdparty/squid.git/blob - src/auth/negotiate/UserRequest.cc
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / auth / negotiate / UserRequest.cc
1 /*
2 * Copyright (C) 1996-2020 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"
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"
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 "HttpHeaderTools.h"
25 #include "HttpReply.h"
26 #include "HttpRequest.h"
27 #include "MemBuf.h"
28 #include "SquidTime.h"
29
30 Auth::Negotiate::UserRequest::UserRequest() :
31 server_blob(nullptr),
32 client_blob(nullptr),
33 waiting(0),
34 request(nullptr)
35 {}
36
37 Auth::Negotiate::UserRequest::~UserRequest()
38 {
39 assert(LockCount()==0);
40 safe_free(server_blob);
41 safe_free(client_blob);
42
43 releaseAuthServer();
44
45 if (request) {
46 HTTPMSGUNLOCK(request);
47 request = NULL;
48 }
49 }
50
51 const char *
52 Auth::Negotiate::UserRequest::connLastHeader()
53 {
54 return NULL;
55 }
56
57 int
58 Auth::Negotiate::UserRequest::authenticated() const
59 {
60 if (user() != NULL && user()->credentials() == Auth::Ok) {
61 debugs(29, 9, HERE << "user authenticated.");
62 return 1;
63 }
64
65 debugs(29, 9, HERE << "user not fully authenticated.");
66 return 0;
67 }
68
69 const char *
70 Auth::Negotiate::UserRequest::credentialsStr()
71 {
72 static char buf[MAX_AUTHTOKEN_LEN];
73 int printResult = 0;
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?
76 } else {
77 printResult = snprintf(buf, sizeof(buf), "KK %s\n", client_blob);
78 }
79
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.");
83 buf[0] = '\0';
84 } else if (printResult >= (int)sizeof(buf))
85 debugs(29, 2, "Negotiate authentication credentials truncated.");
86
87 return buf;
88 }
89
90 Auth::Direction
91 Auth::Negotiate::UserRequest::module_direction()
92 {
93 /* null auth_user is checked for by Auth::UserRequest::direction() */
94
95 if (waiting || client_blob)
96 return Auth::CRED_LOOKUP; /* need helper response to continue */
97
98 if (user()->auth_type != Auth::AUTH_NEGOTIATE)
99 return Auth::CRED_ERROR;
100
101 switch (user()->credentials()) {
102
103 case Auth::Handshake:
104 assert(server_blob);
105 return Auth::CRED_CHALLENGE;
106
107 case Auth::Ok:
108 return Auth::CRED_VALID;
109
110 case Auth::Failed:
111 return Auth::CRED_ERROR; // XXX: really? not VALID or CHALLENGE?
112
113 default:
114 debugs(29, DBG_IMPORTANT, "WARNING: Negotiate Authentication in unexpected state: " << user()->credentials());
115 return Auth::CRED_ERROR;
116 }
117 }
118
119 void
120 Auth::Negotiate::UserRequest::startHelperLookup(HttpRequest *, AccessLogEntry::Pointer &al, AUTHCB * handler, void *data)
121 {
122 static char buf[MAX_AUTHTOKEN_LEN];
123
124 assert(data);
125 assert(handler);
126
127 assert(user() != NULL);
128 assert(user()->auth_type == Auth::AUTH_NEGOTIATE);
129
130 if (static_cast<Auth::Negotiate::Config*>(Auth::SchemeConfig::Find("negotiate"))->authenticateProgram == NULL) {
131 debugs(29, DBG_CRITICAL, "ERROR: No Negotiate authentication program configured.");
132 handler(data);
133 return;
134 }
135
136 debugs(29, 8, HERE << "credentials state is '" << user()->credentials() << "'");
137
138 const char *keyExtras = helperRequestKeyExtras(request, al);
139 int printResult = 0;
140 if (user()->credentials() == Auth::Pending) {
141 if (keyExtras)
142 printResult = snprintf(buf, sizeof(buf), "YR %s %s\n", client_blob, keyExtras);
143 else
144 printResult = snprintf(buf, sizeof(buf), "YR %s\n", client_blob); //CHECKME: can ever client_blob be 0 here?
145 } else {
146 if (keyExtras)
147 printResult = snprintf(buf, sizeof(buf), "KK %s %s\n", client_blob, keyExtras);
148 else
149 printResult = snprintf(buf, sizeof(buf), "KK %s\n", client_blob);
150 }
151
152 if (printResult < 0 || printResult >= (int)sizeof(buf)) {
153 if (printResult < 0)
154 debugs(29, DBG_CRITICAL, "ERROR: Can not build negotiate authentication helper request");
155 else
156 debugs(29, DBG_CRITICAL, "ERROR: Negotiate authentication helper request too big for the " << sizeof(buf) << "-byte buffer");
157 handler(data);
158 return;
159 }
160
161 waiting = 1;
162
163 safe_free(client_blob);
164
165 helperStatefulSubmit(negotiateauthenticators, buf, Auth::Negotiate::UserRequest::HandleReply,
166 new Auth::StateData(this, handler, data), reservationId);
167 }
168
169 /**
170 * Atomic action: properly release the Negotiate auth helpers which may have been reserved
171 * for this request connections use.
172 */
173 void
174 Auth::Negotiate::UserRequest::releaseAuthServer()
175 {
176 if (reservationId) {
177 debugs(29, 6, reservationId);
178 negotiateauthenticators->cancelReservation(reservationId);
179 reservationId.clear();
180 } else
181 debugs(29, 6, HERE << "No Negotiate auth server to release.");
182 }
183
184 void
185 Auth::Negotiate::UserRequest::authenticate(HttpRequest * aRequest, ConnStateData * conn, Http::HdrType type)
186 {
187 /* Check that we are in the client side, where we can generate
188 * auth challenges */
189
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!");
193 return;
194 }
195
196 if (waiting) {
197 debugs(29, DBG_IMPORTANT, "WARNING: Negotiate Authentication waiting for helper reply!");
198 return;
199 }
200
201 if (server_blob) {
202 debugs(29, 2, HERE << "need to challenge client '" << server_blob << "'!");
203 return;
204 }
205
206 /* get header */
207 const char *proxy_auth = aRequest->header.getStr(type);
208
209 /* locate second word */
210 const char *blob = proxy_auth;
211
212 if (blob) {
213 while (xisspace(*blob) && *blob)
214 ++blob;
215
216 while (!xisspace(*blob) && *blob)
217 ++blob;
218
219 while (xisspace(*blob) && *blob)
220 ++blob;
221 }
222
223 switch (user()->credentials()) {
224
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");
233 request = aRequest;
234 HTTPMSGLOCK(request);
235 break;
236
237 case Auth::Pending:
238 debugs(29, DBG_IMPORTANT, HERE << "need to ask helper");
239 break;
240
241 case Auth::Handshake:
242 /* we should have received a blob from the client. Hand it off to
243 * some helper */
244 safe_free(client_blob);
245 client_blob = xstrdup(blob);
246 if (request)
247 HTTPMSGUNLOCK(request);
248 request = aRequest;
249 HTTPMSGLOCK(request);
250 break;
251
252 case Auth::Ok:
253 fatal("Auth::Negotiate::UserRequest::authenticate: unexpected auth state DONE! Report a bug to the squid developers.\n");
254 break;
255
256 case Auth::Failed:
257 /* we've failed somewhere in authentication */
258 debugs(29, 9, HERE << "auth state negotiate failed. " << proxy_auth);
259 break;
260 }
261 }
262
263 void
264 Auth::Negotiate::UserRequest::HandleReply(void *data, const Helper::Reply &reply)
265 {
266 Auth::StateData *r = static_cast<Auth::StateData *>(data);
267
268 debugs(29, 8, reply.reservationId << " got reply=" << reply);
269
270 if (!cbdataReferenceValid(r->data)) {
271 debugs(29, DBG_IMPORTANT, "ERROR: Negotiate Authentication invalid callback data (" << reply.reservationId << ")");
272 delete r;
273 return;
274 }
275
276 Auth::UserRequest::Pointer auth_user_request = r->auth_user_request;
277 assert(auth_user_request != NULL);
278
279 // add new helper kv-pair notes to the credentials object
280 // so that any transaction using those credentials can access them
281 static const NotePairs::Names appendables = { SBuf("group"), SBuf("tag") };
282 auth_user_request->user()->notes.replaceOrAddOrAppend(&reply.notes, appendables);
283 // remove any private credentials detail which got added.
284 auth_user_request->user()->notes.remove("token");
285
286 Auth::Negotiate::UserRequest *lm_request = dynamic_cast<Auth::Negotiate::UserRequest *>(auth_user_request.getRaw());
287 assert(lm_request != NULL);
288 assert(lm_request->waiting);
289
290 lm_request->waiting = 0;
291 safe_free(lm_request->client_blob);
292
293 assert(auth_user_request->user() != NULL);
294 assert(auth_user_request->user()->auth_type == Auth::AUTH_NEGOTIATE);
295
296 if (!lm_request->reservationId)
297 lm_request->reservationId = reply.reservationId;
298 else
299 assert(reply.reservationId == lm_request->reservationId);
300
301 switch (reply.result) {
302 case Helper::TT:
303 /* we have been given a blob to send to the client */
304 safe_free(lm_request->server_blob);
305 lm_request->request->flags.mustKeepalive = true;
306 if (lm_request->request->flags.proxyKeepalive) {
307 const char *tokenNote = reply.notes.findFirst("token");
308 lm_request->server_blob = xstrdup(tokenNote);
309 auth_user_request->user()->credentials(Auth::Handshake);
310 auth_user_request->setDenyMessage("Authentication in progress");
311 debugs(29, 4, HERE << "Need to challenge the client with a server token: '" << tokenNote << "'");
312 } else {
313 auth_user_request->user()->credentials(Auth::Failed);
314 auth_user_request->setDenyMessage("Negotiate authentication requires a persistent connection");
315 }
316 break;
317
318 case Helper::Okay: {
319 const char *userNote = reply.notes.findFirst("user");
320 const char *tokenNote = reply.notes.findFirst("token");
321 if (userNote == NULL || tokenNote == NULL) {
322 // XXX: handle a success with no username better
323 /* protocol error */
324 fatalf("authenticateNegotiateHandleReply: *** Unsupported helper response ***, '%s'\n", reply.other().content());
325 break;
326 }
327
328 /* we're finished, release the helper */
329 auth_user_request->user()->username(userNote);
330 auth_user_request->setDenyMessage("Login successful");
331 safe_free(lm_request->server_blob);
332 lm_request->server_blob = xstrdup(tokenNote);
333 lm_request->releaseAuthServer();
334
335 /* connection is authenticated */
336 debugs(29, 4, HERE << "authenticated user " << auth_user_request->user()->username());
337 auto local_auth_user = lm_request->user();
338 auto cached_user = Auth::Negotiate::User::Cache()->lookup(auth_user_request->user()->userKey());
339 if (!cached_user) {
340 local_auth_user->addToNameCache();
341 } else {
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 cached_user->absorb(local_auth_user);
347 /* from here on we are working with the original cached credentials. */
348 local_auth_user = cached_user;
349 auth_user_request->user(local_auth_user);
350 }
351 /* set these to now because this is either a new login from an
352 * existing user or a new user */
353 local_auth_user->expiretime = current_time.tv_sec;
354 auth_user_request->user()->credentials(Auth::Ok);
355 debugs(29, 4, HERE << "Successfully validated user via Negotiate. Username '" << auth_user_request->user()->username() << "'");
356 }
357 break;
358
359 case Helper::Error:
360 /* authentication failure (wrong password, etc.) */
361 auth_user_request->denyMessageFromHelper("Negotiate", reply);
362 auth_user_request->user()->credentials(Auth::Failed);
363 safe_free(lm_request->server_blob);
364 if (const char *tokenNote = reply.notes.findFirst("token"))
365 lm_request->server_blob = xstrdup(tokenNote);
366 lm_request->releaseAuthServer();
367 debugs(29, 4, "Failed validating user via Negotiate. Result: " << reply);
368 break;
369
370 case Helper::Unknown:
371 debugs(29, DBG_IMPORTANT, "ERROR: Negotiate Authentication Helper crashed (" << reply.reservationId << ")");
372 /* continue to the next case */
373
374 case Helper::TimedOut:
375 case Helper::BrokenHelper:
376 /* TODO kick off a refresh process. This can occur after a YR or after
377 * a KK. If after a YR release the helper and resubmit the request via
378 * Authenticate Negotiate start.
379 * If after a KK deny the user's request w/ 407 and mark the helper as
380 * Needing YR. */
381 if (reply.result == Helper::Unknown)
382 auth_user_request->setDenyMessage("Internal Error");
383 else
384 auth_user_request->denyMessageFromHelper("Negotiate", reply);
385 auth_user_request->user()->credentials(Auth::Failed);
386 safe_free(lm_request->server_blob);
387 lm_request->releaseAuthServer();
388 debugs(29, DBG_IMPORTANT, "ERROR: Negotiate Authentication validating user. Result: " << reply);
389 break;
390 }
391
392 if (lm_request->request) {
393 HTTPMSGUNLOCK(lm_request->request);
394 lm_request->request = NULL;
395 }
396 r->handler(r->data);
397 delete r;
398 }
399