]> git.ipfire.org Git - thirdparty/squid.git/blame - src/auth/ntlm/UserRequest.cc
Cleanup: fix most 'unused parameter' warnings
[thirdparty/squid.git] / src / auth / ntlm / UserRequest.cc
CommitLineData
bbc27441
AJ
1/*
2 * Copyright (C) 1996-2014 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
f7f3304a 9#include "squid.h"
d4806c91 10#include "AccessLogEntry.h"
12daeef6 11#include "auth/ntlm/Config.h"
616cfc4c 12#include "auth/ntlm/UserRequest.h"
d232141d 13#include "auth/State.h"
928f3421 14#include "cbdata.h"
582c2af2 15#include "client_side.h"
ed6e9fb9 16#include "fatal.h"
86c63190 17#include "format/Format.h"
582c2af2 18#include "globals.h"
24438ec5
AJ
19#include "helper.h"
20#include "helper/Reply.h"
d4806c91 21#include "HttpMsg.h"
928f3421 22#include "HttpRequest.h"
d4806c91 23#include "MemBuf.h"
928f3421
AJ
24#include "SquidTime.h"
25
c7baff40 26Auth::Ntlm::UserRequest::UserRequest()
928f3421
AJ
27{
28 waiting=0;
29 client_blob=0;
30 server_blob=0;
31 authserver=NULL;
7afc3bf2 32 request=NULL;
928f3421
AJ
33}
34
c7baff40 35Auth::Ntlm::UserRequest::~UserRequest()
928f3421 36{
8bf217bd 37 assert(LockCount()==0);
928f3421
AJ
38 safe_free(server_blob);
39 safe_free(client_blob);
40
41 releaseAuthServer();
42
43 if (request) {
44 HTTPMSGUNLOCK(request);
45 request = NULL;
46 }
47}
48
49const char *
c7baff40 50Auth::Ntlm::UserRequest::connLastHeader()
928f3421
AJ
51{
52 return NULL;
53}
54
7afc3bf2 55int
c7baff40 56Auth::Ntlm::UserRequest::authenticated() const
7afc3bf2
AJ
57{
58 if (user() != NULL && user()->credentials() == Auth::Ok) {
59 debugs(29, 9, HERE << "user authenticated.");
60 return 1;
61 }
62
63 debugs(29, 9, HERE << "user not fully authenticated.");
64 return 0;
65}
66
d4806c91
CT
67const char *
68Auth::Ntlm::UserRequest::credentialsStr()
69{
70 static char buf[MAX_AUTHTOKEN_LEN];
71 if (user()->credentials() == Auth::Pending) {
72 snprintf(buf, sizeof(buf), "YR %s\n", client_blob);
73 } else {
74 snprintf(buf, sizeof(buf), "KK %s\n", client_blob);
75 }
76 return buf;
77}
78
51a3dd58 79Auth::Direction
c7baff40 80Auth::Ntlm::UserRequest::module_direction()
928f3421 81{
c7baff40 82 /* null auth_user is checked for by Auth::UserRequest::direction() */
928f3421
AJ
83
84 if (waiting || client_blob)
51a3dd58 85 return Auth::CRED_LOOKUP; /* need helper response to continue */
928f3421 86
616cfc4c 87 if (user()->auth_type != Auth::AUTH_NTLM)
51a3dd58 88 return Auth::CRED_ERROR;
928f3421 89
d232141d 90 switch (user()->credentials()) {
928f3421 91
d87154ee 92 case Auth::Handshake:
928f3421 93 assert(server_blob);
7afc3bf2 94 return Auth::CRED_CHALLENGE;
928f3421 95
d87154ee 96 case Auth::Ok:
51a3dd58 97 return Auth::CRED_VALID;
928f3421 98
d87154ee 99 case Auth::Failed:
7afc3bf2 100 return Auth::CRED_ERROR; // XXX: really? not VALID or CHALLENGE?
928f3421 101
d232141d
AJ
102 default:
103 debugs(29, DBG_IMPORTANT, "WARNING: NTLM Authentication in unexpected state: " << user()->credentials());
51a3dd58 104 return Auth::CRED_ERROR;
d232141d 105 }
928f3421
AJ
106}
107
928f3421 108void
ced8def3 109Auth::Ntlm::UserRequest::startHelperLookup(HttpRequest *, AccessLogEntry::Pointer &al, AUTHCB * handler, void *data)
928f3421 110{
7afc3bf2 111 static char buf[MAX_AUTHTOKEN_LEN];
928f3421
AJ
112
113 assert(data);
114 assert(handler);
928f3421 115
372fccd6 116 if (static_cast<Auth::Ntlm::Config*>(Auth::Config::Find("ntlm"))->authenticateProgram == NULL) {
d232141d 117 debugs(29, DBG_CRITICAL, "ERROR: NTLM Start: no NTLM program configured.");
4c535e87 118 handler(data);
ec5858ff 119 return;
928f3421
AJ
120 }
121
7afc3bf2
AJ
122 debugs(29, 8, HERE << "credentials state is '" << user()->credentials() << "'");
123
d4806c91 124 const char *keyExtras = helperRequestKeyExtras(request, al);
d87154ee 125 if (user()->credentials() == Auth::Pending) {
d4806c91
CT
126 if (keyExtras)
127 snprintf(buf, sizeof(buf), "YR %s %s\n", client_blob, keyExtras);
128 else
129 snprintf(buf, sizeof(buf), "YR %s\n", client_blob); //CHECKME: can ever client_blob be 0 here?
928f3421 130 } else {
d4806c91
CT
131 if (keyExtras)
132 snprintf(buf, sizeof(buf), "KK %s %s\n", client_blob, keyExtras);
133 else
134 snprintf(buf, sizeof(buf), "KK %s\n", client_blob);
928f3421 135 }
928f3421
AJ
136 waiting = 1;
137
138 safe_free(client_blob);
c7baff40 139 helperStatefulSubmit(ntlmauthenticators, buf, Auth::Ntlm::UserRequest::HandleReply,
1c756645 140 new Auth::StateData(this, handler, data), authserver);
928f3421
AJ
141}
142
143/**
144 * Atomic action: properly release the NTLM auth helpers which may have been reserved
145 * for this request connections use.
146 */
147void
c7baff40 148Auth::Ntlm::UserRequest::releaseAuthServer()
928f3421
AJ
149{
150 if (authserver) {
151 debugs(29, 6, HERE << "releasing NTLM auth server '" << authserver << "'");
152 helperStatefulReleaseServer(authserver);
153 authserver = NULL;
154 } else
155 debugs(29, 6, HERE << "No NTLM auth server to release.");
156}
157
928f3421 158void
c7baff40 159Auth::Ntlm::UserRequest::authenticate(HttpRequest * aRequest, ConnStateData * conn, http_hdr_type type)
928f3421 160{
928f3421
AJ
161 /* Check that we are in the client side, where we can generate
162 * auth challenges */
163
164 if (conn == NULL || !cbdataReferenceValid(conn)) {
d87154ee 165 user()->credentials(Auth::Failed);
7afc3bf2 166 debugs(29, DBG_IMPORTANT, "WARNING: NTLM Authentication attempt to perform authentication without a connection!");
928f3421
AJ
167 return;
168 }
169
170 if (waiting) {
7afc3bf2 171 debugs(29, DBG_IMPORTANT, "WARNING: NTLM Authentication waiting for helper reply!");
928f3421
AJ
172 return;
173 }
174
175 if (server_blob) {
7afc3bf2 176 debugs(29, 2, HERE << "need to challenge client '" << server_blob << "'!");
928f3421
AJ
177 return;
178 }
179
180 /* get header */
7afc3bf2 181 const char *proxy_auth = aRequest->header.getStr(type);
928f3421
AJ
182
183 /* locate second word */
7afc3bf2 184 const char *blob = proxy_auth;
928f3421
AJ
185
186 /* if proxy_auth is actually NULL, we'd better not manipulate it. */
187 if (blob) {
188 while (xisspace(*blob) && *blob)
742a021b 189 ++blob;
928f3421
AJ
190
191 while (!xisspace(*blob) && *blob)
742a021b 192 ++blob;
928f3421
AJ
193
194 while (xisspace(*blob) && *blob)
742a021b 195 ++blob;
928f3421
AJ
196 }
197
d232141d 198 switch (user()->credentials()) {
928f3421 199
d87154ee 200 case Auth::Unchecked:
928f3421 201 /* we've received a ntlm request. pass to a helper */
7afc3bf2 202 debugs(29, 9, HERE << "auth state ntlm none. Received blob: '" << proxy_auth << "'");
d87154ee 203 user()->credentials(Auth::Pending);
928f3421
AJ
204 safe_free(client_blob);
205 client_blob=xstrdup(blob);
cc1e110a
AJ
206 assert(conn->getAuth() == NULL);
207 conn->setAuth(this, "new NTLM handshake request");
928f3421
AJ
208 request = aRequest;
209 HTTPMSGLOCK(request);
210 break;
211
d87154ee 212 case Auth::Pending:
e0236918 213 debugs(29, DBG_IMPORTANT, HERE << "need to ask helper");
928f3421
AJ
214 break;
215
d87154ee 216 case Auth::Handshake:
928f3421
AJ
217 /* we should have received a blob from the client. Hand it off to
218 * some helper */
219 safe_free(client_blob);
7afc3bf2 220 client_blob = xstrdup(blob);
928f3421
AJ
221 if (request)
222 HTTPMSGUNLOCK(request);
223 request = aRequest;
224 HTTPMSGLOCK(request);
225 break;
226
d87154ee 227 case Auth::Ok:
c7baff40 228 fatal("Auth::Ntlm::UserRequest::authenticate: unexpect auth state DONE! Report a bug to the squid developers.\n");
928f3421
AJ
229 break;
230
d87154ee 231 case Auth::Failed:
928f3421 232 /* we've failed somewhere in authentication */
7afc3bf2 233 debugs(29, 9, HERE << "auth state ntlm failed. " << proxy_auth);
928f3421
AJ
234 break;
235 }
236}
237
238void
24438ec5 239Auth::Ntlm::UserRequest::HandleReply(void *data, const Helper::Reply &reply)
928f3421 240{
1c756645 241 Auth::StateData *r = static_cast<Auth::StateData *>(data);
928f3421 242
0272dd08 243 debugs(29, 8, HERE << "helper: '" << reply.whichServer << "' sent us reply=" << reply);
928f3421 244
1c756645 245 if (!cbdataReferenceValid(r->data)) {
0272dd08 246 debugs(29, DBG_IMPORTANT, "ERROR: NTLM Authentication invalid callback data. helper '" << reply.whichServer << "'.");
1c756645 247 delete r;
928f3421
AJ
248 return;
249 }
250
c7baff40 251 Auth::UserRequest::Pointer auth_user_request = r->auth_user_request;
928f3421
AJ
252 assert(auth_user_request != NULL);
253
71e7400c
AJ
254 // add new helper kv-pair notes to the credentials object
255 // so that any transaction using those credentials can access them
256 auth_user_request->user()->notes.appendNewOnly(&reply.notes);
257
c7baff40 258 Auth::Ntlm::UserRequest *lm_request = dynamic_cast<Auth::Ntlm::UserRequest *>(auth_user_request.getRaw());
7afc3bf2
AJ
259 assert(lm_request != NULL);
260 assert(lm_request->waiting);
56a49fda 261
7afc3bf2
AJ
262 lm_request->waiting = 0;
263 safe_free(lm_request->client_blob);
928f3421 264
7afc3bf2
AJ
265 assert(auth_user_request->user() != NULL);
266 assert(auth_user_request->user()->auth_type == Auth::AUTH_NTLM);
267
268 if (lm_request->authserver == NULL)
0272dd08 269 lm_request->authserver = reply.whichServer.get(); // XXX: no locking?
928f3421 270 else
e166785a 271 assert(reply.whichServer == lm_request->authserver);
928f3421 272
dacb64b9 273 switch (reply.result) {
2428ce02 274 case Helper::TT:
928f3421 275 /* we have been given a blob to send to the client */
7afc3bf2 276 safe_free(lm_request->server_blob);
e857372a 277 lm_request->request->flags.mustKeepalive = true;
450fe1cb 278 if (lm_request->request->flags.proxyKeepalive) {
cf9f0261
CT
279 const char *serverBlob = reply.notes.findFirst("token");
280 lm_request->server_blob = xstrdup(serverBlob);
7afc3bf2 281 auth_user_request->user()->credentials(Auth::Handshake);
928f3421 282 auth_user_request->denyMessage("Authentication in progress");
cf9f0261 283 debugs(29, 4, HERE << "Need to challenge the client with a server token: '" << serverBlob << "'");
928f3421 284 } else {
7afc3bf2 285 auth_user_request->user()->credentials(Auth::Failed);
928f3421
AJ
286 auth_user_request->denyMessage("NTLM authentication requires a persistent connection");
287 }
0272dd08
AJ
288 break;
289
2428ce02 290 case Helper::Okay: {
928f3421 291 /* we're finished, release the helper */
cf9f0261 292 const char *userLabel = reply.notes.findFirst("user");
10f8979e
AJ
293 if (!userLabel) {
294 auth_user_request->user()->credentials(Auth::Failed);
295 safe_free(lm_request->server_blob);
296 lm_request->releaseAuthServer();
297 debugs(29, DBG_CRITICAL, "ERROR: NTLM Authentication helper returned no username. Result: " << reply);
298 break;
299 }
cf9f0261 300 auth_user_request->user()->username(userLabel);
928f3421 301 auth_user_request->denyMessage("Login successful");
7afc3bf2
AJ
302 safe_free(lm_request->server_blob);
303 lm_request->releaseAuthServer();
928f3421 304
cf9f0261 305 debugs(29, 4, HERE << "Successfully validated user via NTLM. Username '" << userLabel << "'");
928f3421 306 /* connection is authenticated */
7afc3bf2 307 debugs(29, 4, HERE << "authenticated user " << auth_user_request->user()->username());
928f3421
AJ
308 /* see if this is an existing user with a different proxy_auth
309 * string */
d4806c91 310 AuthUserHashPointer *usernamehash = static_cast<AuthUserHashPointer *>(hash_lookup(proxy_auth_username_cache, auth_user_request->user()->userKey()));
7afc3bf2 311 Auth::User::Pointer local_auth_user = lm_request->user();
616cfc4c 312 while (usernamehash && (usernamehash->user()->auth_type != Auth::AUTH_NTLM ||
d4806c91 313 strcmp(usernamehash->user()->userKey(), auth_user_request->user()->userKey()) != 0))
928f3421
AJ
314 usernamehash = static_cast<AuthUserHashPointer *>(usernamehash->next);
315 if (usernamehash) {
316 /* we can't seamlessly recheck the username due to the
317 * challenge-response nature of the protocol.
58e94342
AJ
318 * Just free the temporary auth_user after merging as
319 * much of it new state into the existing one as possible */
928f3421 320 usernamehash->user()->absorb(local_auth_user);
7afc3bf2 321 /* from here on we are working with the original cached credentials. */
928f3421 322 local_auth_user = usernamehash->user();
58e94342 323 auth_user_request->user(local_auth_user);
928f3421
AJ
324 } else {
325 /* store user in hash's */
326 local_auth_user->addToNameCache();
928f3421
AJ
327 }
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;
7afc3bf2 331 auth_user_request->user()->credentials(Auth::Ok);
7bbefa01 332 debugs(29, 4, HERE << "Successfully validated user via NTLM. Username '" << auth_user_request->user()->username() << "'");
0272dd08 333 }
dacb64b9 334 break;
7afc3bf2 335
2428ce02 336 case Helper::Error: {
928f3421 337 /* authentication failure (wrong password, etc.) */
cf9f0261 338 const char *errNote = reply.notes.find("message");
7bbefa01 339 if (errNote != NULL)
cf9f0261 340 auth_user_request->denyMessage(errNote);
7bbefa01
AJ
341 else
342 auth_user_request->denyMessage("NTLM Authentication denied with no reason given");
7afc3bf2
AJ
343 auth_user_request->user()->credentials(Auth::Failed);
344 safe_free(lm_request->server_blob);
345 lm_request->releaseAuthServer();
87a122b0 346 debugs(29, 4, "Failed validating user via NTLM. Result: " << reply);
7bbefa01
AJ
347 }
348 break;
0272dd08 349
2428ce02 350 case Helper::Unknown:
0272dd08 351 debugs(29, DBG_IMPORTANT, "ERROR: NTLM Authentication Helper '" << reply.whichServer << "' crashed!.");
f53969cc 352 /* continue to the next case */
0272dd08 353
32fd6d8a 354 case Helper::TimedOut:
2428ce02 355 case Helper::BrokenHelper: {
928f3421
AJ
356 /* TODO kick off a refresh process. This can occur after a YR or after
357 * a KK. If after a YR release the helper and resubmit the request via
358 * Authenticate NTLM start.
359 * If after a KK deny the user's request w/ 407 and mark the helper as
360 * Needing YR. */
cf9f0261 361 const char *errNote = reply.notes.find("message");
2428ce02 362 if (reply.result == Helper::Unknown)
7bbefa01
AJ
363 auth_user_request->denyMessage("Internal Error");
364 else if (errNote != NULL)
cf9f0261 365 auth_user_request->denyMessage(errNote);
7bbefa01
AJ
366 else
367 auth_user_request->denyMessage("NTLM Authentication failed with no reason given");
d87154ee 368 auth_user_request->user()->credentials(Auth::Failed);
7afc3bf2
AJ
369 safe_free(lm_request->server_blob);
370 lm_request->releaseAuthServer();
87a122b0 371 debugs(29, DBG_IMPORTANT, "ERROR: NTLM Authentication validating user. Result: " << reply);
7bbefa01
AJ
372 }
373 break;
928f3421
AJ
374 }
375
7afc3bf2
AJ
376 if (lm_request->request) {
377 HTTPMSGUNLOCK(lm_request->request);
378 lm_request->request = NULL;
928f3421 379 }
4c535e87 380 r->handler(r->data);
1c756645 381 delete r;
928f3421 382}
f53969cc 383