]> git.ipfire.org Git - thirdparty/squid.git/blob - src/auth/ntlm/UserRequest.cc
Merged from trunk
[thirdparty/squid.git] / src / auth / ntlm / UserRequest.cc
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
9 #include "squid.h"
10 #include "AccessLogEntry.h"
11 #include "auth/ntlm/Config.h"
12 #include "auth/ntlm/UserRequest.h"
13 #include "auth/State.h"
14 #include "cbdata.h"
15 #include "client_side.h"
16 #include "fatal.h"
17 #include "format/Format.h"
18 #include "globals.h"
19 #include "helper.h"
20 #include "helper/Reply.h"
21 #include "HttpMsg.h"
22 #include "HttpRequest.h"
23 #include "MemBuf.h"
24 #include "SquidTime.h"
25
26 Auth::Ntlm::UserRequest::UserRequest()
27 {
28 waiting=0;
29 client_blob=0;
30 server_blob=0;
31 authserver=NULL;
32 request=NULL;
33 }
34
35 Auth::Ntlm::UserRequest::~UserRequest()
36 {
37 assert(LockCount()==0);
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
49 const char *
50 Auth::Ntlm::UserRequest::connLastHeader()
51 {
52 return NULL;
53 }
54
55 int
56 Auth::Ntlm::UserRequest::authenticated() const
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
67 const char *
68 Auth::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
79 Auth::Direction
80 Auth::Ntlm::UserRequest::module_direction()
81 {
82 /* null auth_user is checked for by Auth::UserRequest::direction() */
83
84 if (waiting || client_blob)
85 return Auth::CRED_LOOKUP; /* need helper response to continue */
86
87 if (user()->auth_type != Auth::AUTH_NTLM)
88 return Auth::CRED_ERROR;
89
90 switch (user()->credentials()) {
91
92 case Auth::Handshake:
93 assert(server_blob);
94 return Auth::CRED_CHALLENGE;
95
96 case Auth::Ok:
97 return Auth::CRED_VALID;
98
99 case Auth::Failed:
100 return Auth::CRED_ERROR; // XXX: really? not VALID or CHALLENGE?
101
102 default:
103 debugs(29, DBG_IMPORTANT, "WARNING: NTLM Authentication in unexpected state: " << user()->credentials());
104 return Auth::CRED_ERROR;
105 }
106 }
107
108 void
109 Auth::Ntlm::UserRequest::startHelperLookup(HttpRequest *, AccessLogEntry::Pointer &al, AUTHCB * handler, void *data)
110 {
111 static char buf[MAX_AUTHTOKEN_LEN];
112
113 assert(data);
114 assert(handler);
115
116 if (static_cast<Auth::Ntlm::Config*>(Auth::Config::Find("ntlm"))->authenticateProgram == NULL) {
117 debugs(29, DBG_CRITICAL, "ERROR: NTLM Start: no NTLM program configured.");
118 handler(data);
119 return;
120 }
121
122 debugs(29, 8, HERE << "credentials state is '" << user()->credentials() << "'");
123
124 const char *keyExtras = helperRequestKeyExtras(request, al);
125 if (user()->credentials() == Auth::Pending) {
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?
130 } else {
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);
135 }
136 waiting = 1;
137
138 safe_free(client_blob);
139 helperStatefulSubmit(ntlmauthenticators, buf, Auth::Ntlm::UserRequest::HandleReply,
140 new Auth::StateData(this, handler, data), authserver);
141 }
142
143 /**
144 * Atomic action: properly release the NTLM auth helpers which may have been reserved
145 * for this request connections use.
146 */
147 void
148 Auth::Ntlm::UserRequest::releaseAuthServer()
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
158 void
159 Auth::Ntlm::UserRequest::authenticate(HttpRequest * aRequest, ConnStateData * conn, http_hdr_type type)
160 {
161 /* Check that we are in the client side, where we can generate
162 * auth challenges */
163
164 if (conn == NULL || !cbdataReferenceValid(conn)) {
165 user()->credentials(Auth::Failed);
166 debugs(29, DBG_IMPORTANT, "WARNING: NTLM Authentication attempt to perform authentication without a connection!");
167 return;
168 }
169
170 if (waiting) {
171 debugs(29, DBG_IMPORTANT, "WARNING: NTLM Authentication waiting for helper reply!");
172 return;
173 }
174
175 if (server_blob) {
176 debugs(29, 2, HERE << "need to challenge client '" << server_blob << "'!");
177 return;
178 }
179
180 /* get header */
181 const char *proxy_auth = aRequest->header.getStr(type);
182
183 /* locate second word */
184 const char *blob = proxy_auth;
185
186 /* if proxy_auth is actually NULL, we'd better not manipulate it. */
187 if (blob) {
188 while (xisspace(*blob) && *blob)
189 ++blob;
190
191 while (!xisspace(*blob) && *blob)
192 ++blob;
193
194 while (xisspace(*blob) && *blob)
195 ++blob;
196 }
197
198 switch (user()->credentials()) {
199
200 case Auth::Unchecked:
201 /* we've received a ntlm request. pass to a helper */
202 debugs(29, 9, HERE << "auth state ntlm none. Received blob: '" << proxy_auth << "'");
203 user()->credentials(Auth::Pending);
204 safe_free(client_blob);
205 client_blob=xstrdup(blob);
206 assert(conn->getAuth() == NULL);
207 conn->setAuth(this, "new NTLM handshake request");
208 request = aRequest;
209 HTTPMSGLOCK(request);
210 break;
211
212 case Auth::Pending:
213 debugs(29, DBG_IMPORTANT, HERE << "need to ask helper");
214 break;
215
216 case Auth::Handshake:
217 /* we should have received a blob from the client. Hand it off to
218 * some helper */
219 safe_free(client_blob);
220 client_blob = xstrdup(blob);
221 if (request)
222 HTTPMSGUNLOCK(request);
223 request = aRequest;
224 HTTPMSGLOCK(request);
225 break;
226
227 case Auth::Ok:
228 fatal("Auth::Ntlm::UserRequest::authenticate: unexpect auth state DONE! Report a bug to the squid developers.\n");
229 break;
230
231 case Auth::Failed:
232 /* we've failed somewhere in authentication */
233 debugs(29, 9, HERE << "auth state ntlm failed. " << proxy_auth);
234 break;
235 }
236 }
237
238 void
239 Auth::Ntlm::UserRequest::HandleReply(void *data, const Helper::Reply &reply)
240 {
241 Auth::StateData *r = static_cast<Auth::StateData *>(data);
242
243 debugs(29, 8, HERE << "helper: '" << reply.whichServer << "' sent us reply=" << reply);
244
245 if (!cbdataReferenceValid(r->data)) {
246 debugs(29, DBG_IMPORTANT, "ERROR: NTLM Authentication invalid callback data. helper '" << reply.whichServer << "'.");
247 delete r;
248 return;
249 }
250
251 Auth::UserRequest::Pointer auth_user_request = r->auth_user_request;
252 assert(auth_user_request != NULL);
253
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
258 Auth::Ntlm::UserRequest *lm_request = dynamic_cast<Auth::Ntlm::UserRequest *>(auth_user_request.getRaw());
259 assert(lm_request != NULL);
260 assert(lm_request->waiting);
261
262 lm_request->waiting = 0;
263 safe_free(lm_request->client_blob);
264
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)
269 lm_request->authserver = reply.whichServer.get(); // XXX: no locking?
270 else
271 assert(reply.whichServer == lm_request->authserver);
272
273 switch (reply.result) {
274 case Helper::TT:
275 /* we have been given a blob to send to the client */
276 safe_free(lm_request->server_blob);
277 lm_request->request->flags.mustKeepalive = true;
278 if (lm_request->request->flags.proxyKeepalive) {
279 const char *serverBlob = reply.notes.findFirst("token");
280 lm_request->server_blob = xstrdup(serverBlob);
281 auth_user_request->user()->credentials(Auth::Handshake);
282 auth_user_request->denyMessage("Authentication in progress");
283 debugs(29, 4, HERE << "Need to challenge the client with a server token: '" << serverBlob << "'");
284 } else {
285 auth_user_request->user()->credentials(Auth::Failed);
286 auth_user_request->denyMessage("NTLM authentication requires a persistent connection");
287 }
288 break;
289
290 case Helper::Okay: {
291 /* we're finished, release the helper */
292 const char *userLabel = reply.notes.findFirst("user");
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 }
300 auth_user_request->user()->username(userLabel);
301 auth_user_request->denyMessage("Login successful");
302 safe_free(lm_request->server_blob);
303 lm_request->releaseAuthServer();
304
305 debugs(29, 4, HERE << "Successfully validated user via NTLM. Username '" << userLabel << "'");
306 /* connection is authenticated */
307 debugs(29, 4, HERE << "authenticated user " << auth_user_request->user()->username());
308 /* see if this is an existing user with a different proxy_auth
309 * string */
310 AuthUserHashPointer *usernamehash = static_cast<AuthUserHashPointer *>(hash_lookup(proxy_auth_username_cache, auth_user_request->user()->userKey()));
311 Auth::User::Pointer local_auth_user = lm_request->user();
312 while (usernamehash && (usernamehash->user()->auth_type != Auth::AUTH_NTLM ||
313 strcmp(usernamehash->user()->userKey(), auth_user_request->user()->userKey()) != 0))
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.
318 * Just free the temporary auth_user after merging as
319 * much of it new state into the existing one as possible */
320 usernamehash->user()->absorb(local_auth_user);
321 /* from here on we are working with the original cached credentials. */
322 local_auth_user = usernamehash->user();
323 auth_user_request->user(local_auth_user);
324 } else {
325 /* store user in hash's */
326 local_auth_user->addToNameCache();
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;
331 auth_user_request->user()->credentials(Auth::Ok);
332 debugs(29, 4, HERE << "Successfully validated user via NTLM. Username '" << auth_user_request->user()->username() << "'");
333 }
334 break;
335
336 case Helper::Error: {
337 /* authentication failure (wrong password, etc.) */
338 const char *errNote = reply.notes.find("message");
339 if (errNote != NULL)
340 auth_user_request->denyMessage(errNote);
341 else
342 auth_user_request->denyMessage("NTLM Authentication denied with no reason given");
343 auth_user_request->user()->credentials(Auth::Failed);
344 safe_free(lm_request->server_blob);
345 lm_request->releaseAuthServer();
346 debugs(29, 4, "Failed validating user via NTLM. Result: " << reply);
347 }
348 break;
349
350 case Helper::Unknown:
351 debugs(29, DBG_IMPORTANT, "ERROR: NTLM Authentication Helper '" << reply.whichServer << "' crashed!.");
352 /* continue to the next case */
353
354 case Helper::TimedOut:
355 case Helper::BrokenHelper: {
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. */
361 const char *errNote = reply.notes.find("message");
362 if (reply.result == Helper::Unknown)
363 auth_user_request->denyMessage("Internal Error");
364 else if (errNote != NULL)
365 auth_user_request->denyMessage(errNote);
366 else
367 auth_user_request->denyMessage("NTLM Authentication failed with no reason given");
368 auth_user_request->user()->credentials(Auth::Failed);
369 safe_free(lm_request->server_blob);
370 lm_request->releaseAuthServer();
371 debugs(29, DBG_IMPORTANT, "ERROR: NTLM Authentication validating user. Result: " << reply);
372 }
373 break;
374 }
375
376 if (lm_request->request) {
377 HTTPMSGUNLOCK(lm_request->request);
378 lm_request->request = NULL;
379 }
380 r->handler(r->data);
381 delete r;
382 }
383