2 * Copyright (C) 1996-2016 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 "http/Stream.h"
26 #include "HttpHeaderTools.h"
27 #include "HttpReply.h"
28 #include "HttpRequest.h"
29 #include "mgr/Registration.h"
30 #include "SquidTime.h"
35 static AUTHSSTATS authenticateNTLMStats
;
37 statefulhelper
*ntlmauthenticators
= NULL
;
38 static int authntlm_initialised
= 0;
40 static hash_table
*proxy_auth_cache
= NULL
;
43 Auth::Ntlm::Config::rotateHelpers()
45 /* schedule closure of existing helpers */
46 if (ntlmauthenticators
) {
47 helperStatefulShutdown(ntlmauthenticators
);
50 /* NP: dynamic helper restart will ensure they start up again as needed. */
53 /* free any allocated configuration details */
55 Auth::Ntlm::Config::done()
57 Auth::SchemeConfig::done();
59 authntlm_initialised
= 0;
61 if (ntlmauthenticators
) {
62 helperStatefulShutdown(ntlmauthenticators
);
68 delete ntlmauthenticators
;
69 ntlmauthenticators
= NULL
;
71 if (authenticateProgram
)
72 wordlistDestroy(&authenticateProgram
);
74 debugs(29, DBG_IMPORTANT
, "Reconfigure: NTLM authentication configuration cleared.");
78 Auth::Ntlm::Config::dump(StoreEntry
* entry
, const char *name
, Auth::SchemeConfig
* scheme
) const
80 if (!Auth::SchemeConfig::dump(entry
, name
, scheme
))
83 storeAppendPrintf(entry
, "%s ntlm keep_alive %s\n", name
, keep_alive
? "on" : "off");
87 Auth::Ntlm::Config::Config() : keep_alive(1)
91 Auth::Ntlm::Config::parse(Auth::SchemeConfig
* scheme
, int n_configured
, char *param_str
)
93 if (strcmp(param_str
, "keep_alive") == 0) {
94 parse_onoff(&keep_alive
);
96 Auth::SchemeConfig::parse(scheme
, n_configured
, param_str
);
100 Auth::Ntlm::Config::type() const
102 return Auth::Ntlm::Scheme::GetInstance()->type();
105 /* Initialize helpers and the like for this auth scheme. Called AFTER parsing the
108 Auth::Ntlm::Config::init(Auth::SchemeConfig
*)
110 if (authenticateProgram
) {
112 authntlm_initialised
= 1;
114 if (ntlmauthenticators
== NULL
)
115 ntlmauthenticators
= new statefulhelper("ntlmauthenticator");
117 if (!proxy_auth_cache
)
118 proxy_auth_cache
= hash_create((HASHCMP
*) strcmp
, 7921, hash_string
);
120 assert(proxy_auth_cache
);
122 ntlmauthenticators
->cmdline
= authenticateProgram
;
124 ntlmauthenticators
->childs
.updateLimits(authenticateChildren
);
126 ntlmauthenticators
->ipc_type
= IPC_STREAM
;
128 helperStatefulOpenServers(ntlmauthenticators
);
133 Auth::Ntlm::Config::registerWithCacheManager(void)
135 Mgr::RegisterAction("ntlmauthenticator",
136 "NTLM User Authenticator Stats",
137 authenticateNTLMStats
, 0, 1);
141 Auth::Ntlm::Config::active() const
143 return authntlm_initialised
== 1;
147 Auth::Ntlm::Config::configured() const
149 if ((authenticateProgram
!= NULL
) && (authenticateChildren
.n_max
!= 0)) {
150 debugs(29, 9, HERE
<< "returning configured");
154 debugs(29, 9, HERE
<< "returning unconfigured");
161 Auth::Ntlm::Config::fixHeader(Auth::UserRequest::Pointer auth_user_request
, HttpReply
*rep
, Http::HdrType hdrType
, HttpRequest
* request
)
163 if (!authenticateProgram
)
166 /* Need keep-alive */
167 if (!request
->flags
.proxyKeepalive
&& request
->flags
.mustKeepalive
)
170 /* New request, no user details */
171 if (auth_user_request
== NULL
) {
172 debugs(29, 9, HERE
<< "Sending type:" << hdrType
<< " header: 'NTLM'");
173 httpHeaderPutStrf(&rep
->header
, hdrType
, "NTLM");
176 /* drop the connection */
177 request
->flags
.proxyKeepalive
= false;
180 Auth::Ntlm::UserRequest
*ntlm_request
= dynamic_cast<Auth::Ntlm::UserRequest
*>(auth_user_request
.getRaw());
181 assert(ntlm_request
!= NULL
);
183 switch (ntlm_request
->user()->credentials()) {
186 /* here it makes sense to drop the connection, as auth is
187 * tied to it, even if MAYBE the client could handle it - Kinkie */
188 request
->flags
.proxyKeepalive
= false;
192 /* Special case: authentication finished OK but disallowed by ACL.
193 * Need to start over to give the client another chance.
197 case Auth::Unchecked
:
198 /* semantic change: do not drop the connection.
199 * 2.5 implementation used to keep it open - Kinkie */
200 debugs(29, 9, HERE
<< "Sending type:" << hdrType
<< " header: 'NTLM'");
201 httpHeaderPutStrf(&rep
->header
, hdrType
, "NTLM");
204 case Auth::Handshake
:
205 /* we're waiting for a response from the client. Pass it the blob */
206 debugs(29, 9, HERE
<< "Sending type:" << hdrType
<< " header: 'NTLM " << ntlm_request
->server_blob
<< "'");
207 httpHeaderPutStrf(&rep
->header
, hdrType
, "NTLM %s", ntlm_request
->server_blob
);
208 safe_free(ntlm_request
->server_blob
);
212 debugs(29, DBG_CRITICAL
, "NTLM Auth fixHeader: state " << ntlm_request
->user()->credentials() << ".");
213 fatal("unexpected state in AuthenticateNTLMFixErrorHeader.\n");
219 authenticateNTLMStats(StoreEntry
* sentry
)
221 if (ntlmauthenticators
)
222 ntlmauthenticators
->packStatsInto(sentry
, "NTLM Authenticator Statistics");
226 * Decode a NTLM [Proxy-]Auth string, placing the results in the passed
227 * Auth_user structure.
229 Auth::UserRequest::Pointer
230 Auth::Ntlm::Config::decode(char const *proxy_auth
, const char *aRequestRealm
)
232 Auth::Ntlm::User
*newUser
= new Auth::Ntlm::User(Auth::SchemeConfig::Find("ntlm"), aRequestRealm
);
233 Auth::UserRequest::Pointer auth_user_request
= new Auth::Ntlm::UserRequest();
234 assert(auth_user_request
->user() == NULL
);
236 auth_user_request
->user(newUser
);
237 auth_user_request
->user()->auth_type
= Auth::AUTH_NTLM
;
239 auth_user_request
->user()->BuildUserKey(proxy_auth
, aRequestRealm
);
241 /* all we have to do is identify that it's NTLM - the helper does the rest */
242 debugs(29, 9, HERE
<< "decode: NTLM authentication");
243 return auth_user_request
;