2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 29 NTLM Authenticator */
11 /* The functions in this file handle authentication.
12 * They DO NOT perform access control or auditing.
13 * See acl.c for access control and client_side.c for auditing */
16 #include "auth/Gadgets.h"
17 #include "auth/ntlm/Config.h"
18 #include "auth/ntlm/Scheme.h"
19 #include "auth/ntlm/User.h"
20 #include "auth/ntlm/UserRequest.h"
21 #include "auth/State.h"
23 #include "client_side.h"
25 #include "HttpHeaderTools.h"
26 #include "HttpReply.h"
27 #include "HttpRequest.h"
28 #include "mgr/Registration.h"
29 #include "SquidTime.h"
34 static AUTHSSTATS authenticateNTLMStats
;
36 statefulhelper
*ntlmauthenticators
= NULL
;
37 static int authntlm_initialised
= 0;
39 static hash_table
*proxy_auth_cache
= NULL
;
42 Auth::Ntlm::Config::rotateHelpers()
44 /* schedule closure of existing helpers */
45 if (ntlmauthenticators
) {
46 helperStatefulShutdown(ntlmauthenticators
);
49 /* NP: dynamic helper restart will ensure they start up again as needed. */
52 /* free any allocated configuration details */
54 Auth::Ntlm::Config::done()
58 authntlm_initialised
= 0;
60 if (ntlmauthenticators
) {
61 helperStatefulShutdown(ntlmauthenticators
);
67 delete ntlmauthenticators
;
68 ntlmauthenticators
= NULL
;
70 if (authenticateProgram
)
71 wordlistDestroy(&authenticateProgram
);
73 debugs(29, DBG_IMPORTANT
, "Reconfigure: NTLM authentication configuration cleared.");
77 Auth::Ntlm::Config::dump(StoreEntry
* entry
, const char *name
, Auth::Config
* scheme
) const
79 if (!Auth::Config::dump(entry
, name
, scheme
))
82 storeAppendPrintf(entry
, "%s ntlm keep_alive %s\n", name
, keep_alive
? "on" : "off");
86 Auth::Ntlm::Config::Config() : keep_alive(1)
90 Auth::Ntlm::Config::parse(Auth::Config
* scheme
, int n_configured
, char *param_str
)
92 if (strcmp(param_str
, "program") == 0) {
93 if (authenticateProgram
)
94 wordlistDestroy(&authenticateProgram
);
96 parse_wordlist(&authenticateProgram
);
98 requirePathnameExists("auth_param ntlm program", authenticateProgram
->key
);
99 } else if (strcmp(param_str
, "keep_alive") == 0) {
100 parse_onoff(&keep_alive
);
102 Auth::Config::parse(scheme
, n_configured
, param_str
);
106 Auth::Ntlm::Config::type() const
108 return Auth::Ntlm::Scheme::GetInstance()->type();
111 /* Initialize helpers and the like for this auth scheme. Called AFTER parsing the
114 Auth::Ntlm::Config::init(Auth::Config
* scheme
)
116 if (authenticateProgram
) {
118 authntlm_initialised
= 1;
120 if (ntlmauthenticators
== NULL
)
121 ntlmauthenticators
= new statefulhelper("ntlmauthenticator");
123 if (!proxy_auth_cache
)
124 proxy_auth_cache
= hash_create((HASHCMP
*) strcmp
, 7921, hash_string
);
126 assert(proxy_auth_cache
);
128 ntlmauthenticators
->cmdline
= authenticateProgram
;
130 ntlmauthenticators
->childs
.updateLimits(authenticateChildren
);
132 ntlmauthenticators
->ipc_type
= IPC_STREAM
;
134 helperStatefulOpenServers(ntlmauthenticators
);
139 Auth::Ntlm::Config::registerWithCacheManager(void)
141 Mgr::RegisterAction("ntlmauthenticator",
142 "NTLM User Authenticator Stats",
143 authenticateNTLMStats
, 0, 1);
147 Auth::Ntlm::Config::active() const
149 return authntlm_initialised
== 1;
153 Auth::Ntlm::Config::configured() const
155 if ((authenticateProgram
!= NULL
) && (authenticateChildren
.n_max
!= 0)) {
156 debugs(29, 9, HERE
<< "returning configured");
160 debugs(29, 9, HERE
<< "returning unconfigured");
167 Auth::Ntlm::Config::fixHeader(Auth::UserRequest::Pointer auth_user_request
, HttpReply
*rep
, http_hdr_type hdrType
, HttpRequest
* request
)
169 if (!authenticateProgram
)
172 /* Need keep-alive */
173 if (!request
->flags
.proxyKeepalive
&& request
->flags
.mustKeepalive
)
176 /* New request, no user details */
177 if (auth_user_request
== NULL
) {
178 debugs(29, 9, HERE
<< "Sending type:" << hdrType
<< " header: 'NTLM'");
179 httpHeaderPutStrf(&rep
->header
, hdrType
, "NTLM");
182 /* drop the connection */
183 request
->flags
.proxyKeepalive
= false;
186 Auth::Ntlm::UserRequest
*ntlm_request
= dynamic_cast<Auth::Ntlm::UserRequest
*>(auth_user_request
.getRaw());
187 assert(ntlm_request
!= NULL
);
189 switch (ntlm_request
->user()->credentials()) {
192 /* here it makes sense to drop the connection, as auth is
193 * tied to it, even if MAYBE the client could handle it - Kinkie */
194 request
->flags
.proxyKeepalive
= false;
198 /* Special case: authentication finished OK but disallowed by ACL.
199 * Need to start over to give the client another chance.
203 case Auth::Unchecked
:
204 /* semantic change: do not drop the connection.
205 * 2.5 implementation used to keep it open - Kinkie */
206 debugs(29, 9, HERE
<< "Sending type:" << hdrType
<< " header: 'NTLM'");
207 httpHeaderPutStrf(&rep
->header
, hdrType
, "NTLM");
210 case Auth::Handshake
:
211 /* we're waiting for a response from the client. Pass it the blob */
212 debugs(29, 9, HERE
<< "Sending type:" << hdrType
<< " header: 'NTLM " << ntlm_request
->server_blob
<< "'");
213 httpHeaderPutStrf(&rep
->header
, hdrType
, "NTLM %s", ntlm_request
->server_blob
);
214 safe_free(ntlm_request
->server_blob
);
218 debugs(29, DBG_CRITICAL
, "NTLM Auth fixHeader: state " << ntlm_request
->user()->credentials() << ".");
219 fatal("unexpected state in AuthenticateNTLMFixErrorHeader.\n");
225 authenticateNTLMStats(StoreEntry
* sentry
)
227 helperStatefulStats(sentry
, ntlmauthenticators
, "NTLM Authenticator Statistics");
231 * Decode a NTLM [Proxy-]Auth string, placing the results in the passed
232 * Auth_user structure.
234 Auth::UserRequest::Pointer
235 Auth::Ntlm::Config::decode(char const *proxy_auth
, const char *aRequestRealm
)
237 Auth::Ntlm::User
*newUser
= new Auth::Ntlm::User(Auth::Config::Find("ntlm"), aRequestRealm
);
238 Auth::UserRequest::Pointer auth_user_request
= new Auth::Ntlm::UserRequest();
239 assert(auth_user_request
->user() == NULL
);
241 auth_user_request
->user(newUser
);
242 auth_user_request
->user()->auth_type
= Auth::AUTH_NTLM
;
244 /* all we have to do is identify that it's NTLM - the helper does the rest */
245 debugs(29, 9, HERE
<< "decode: NTLM authentication");
246 return auth_user_request
;