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