2 * Copyright (C) 1996-2023 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"
34 static AUTHSSTATS authenticateNTLMStats
;
36 statefulhelper
*ntlmauthenticators
= nullptr;
37 static int authntlm_initialised
= 0;
39 static hash_table
*proxy_auth_cache
= nullptr;
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()
56 Auth::SchemeConfig::done();
58 authntlm_initialised
= 0;
60 if (ntlmauthenticators
) {
61 helperStatefulShutdown(ntlmauthenticators
);
67 delete ntlmauthenticators
;
68 ntlmauthenticators
= nullptr;
70 if (authenticateProgram
)
71 wordlistDestroy(&authenticateProgram
);
73 debugs(29, DBG_IMPORTANT
, "Reconfigure: NTLM authentication configuration cleared.");
77 Auth::Ntlm::Config::type() const
79 return Auth::Ntlm::Scheme::GetInstance()->type();
82 /* Initialize helpers and the like for this auth scheme. Called AFTER parsing the
85 Auth::Ntlm::Config::init(Auth::SchemeConfig
*)
87 if (authenticateProgram
) {
89 authntlm_initialised
= 1;
91 if (ntlmauthenticators
== nullptr)
92 ntlmauthenticators
= new statefulhelper("ntlmauthenticator");
94 if (!proxy_auth_cache
)
95 proxy_auth_cache
= hash_create((HASHCMP
*) strcmp
, 7921, hash_string
);
97 assert(proxy_auth_cache
);
99 ntlmauthenticators
->cmdline
= authenticateProgram
;
101 ntlmauthenticators
->childs
.updateLimits(authenticateChildren
);
103 ntlmauthenticators
->ipc_type
= IPC_STREAM
;
105 helperStatefulOpenServers(ntlmauthenticators
);
110 Auth::Ntlm::Config::registerWithCacheManager(void)
112 Mgr::RegisterAction("ntlmauthenticator",
113 "NTLM User Authenticator Stats",
114 authenticateNTLMStats
, 0, 1);
118 Auth::Ntlm::Config::active() const
120 return authntlm_initialised
== 1;
124 Auth::Ntlm::Config::configured() const
126 if ((authenticateProgram
!= nullptr) && (authenticateChildren
.n_max
!= 0)) {
127 debugs(29, 9, "returning configured");
131 debugs(29, 9, "returning unconfigured");
138 Auth::Ntlm::Config::fixHeader(Auth::UserRequest::Pointer auth_user_request
, HttpReply
*rep
, Http::HdrType hdrType
, HttpRequest
* request
)
140 if (!authenticateProgram
)
143 /* Need keep-alive */
144 if (!request
->flags
.proxyKeepalive
&& request
->flags
.mustKeepalive
)
147 /* New request, no user details */
148 if (auth_user_request
== nullptr) {
149 debugs(29, 9, "Sending type:" << hdrType
<< " header: 'NTLM'");
150 httpHeaderPutStrf(&rep
->header
, hdrType
, "NTLM");
153 /* drop the connection */
154 request
->flags
.proxyKeepalive
= false;
157 Auth::Ntlm::UserRequest
*ntlm_request
= dynamic_cast<Auth::Ntlm::UserRequest
*>(auth_user_request
.getRaw());
158 assert(ntlm_request
!= nullptr);
160 switch (ntlm_request
->user()->credentials()) {
163 /* here it makes sense to drop the connection, as auth is
164 * tied to it, even if MAYBE the client could handle it - Kinkie */
165 request
->flags
.proxyKeepalive
= false;
169 /* Special case: authentication finished OK but disallowed by ACL.
170 * Need to start over to give the client another chance.
174 case Auth::Unchecked
:
175 /* semantic change: do not drop the connection.
176 * 2.5 implementation used to keep it open - Kinkie */
177 debugs(29, 9, "Sending type:" << hdrType
<< " header: 'NTLM'");
178 httpHeaderPutStrf(&rep
->header
, hdrType
, "NTLM");
181 case Auth::Handshake
:
182 /* we're waiting for a response from the client. Pass it the blob */
183 debugs(29, 9, "Sending type:" << hdrType
<< " header: 'NTLM " << ntlm_request
->server_blob
<< "'");
184 httpHeaderPutStrf(&rep
->header
, hdrType
, "NTLM %s", ntlm_request
->server_blob
);
185 safe_free(ntlm_request
->server_blob
);
189 debugs(29, DBG_CRITICAL
, "NTLM Auth fixHeader: state " << ntlm_request
->user()->credentials() << ".");
190 fatal("unexpected state in AuthenticateNTLMFixErrorHeader.\n");
196 authenticateNTLMStats(StoreEntry
* sentry
)
198 if (ntlmauthenticators
)
199 ntlmauthenticators
->packStatsInto(sentry
, "NTLM Authenticator Statistics");
203 * Decode a NTLM [Proxy-]Auth string, placing the results in the passed
204 * Auth_user structure.
206 Auth::UserRequest::Pointer
207 Auth::Ntlm::Config::decode(char const *proxy_auth
, const HttpRequest
*, const char *aRequestRealm
)
209 Auth::Ntlm::User
*newUser
= new Auth::Ntlm::User(Auth::SchemeConfig::Find("ntlm"), aRequestRealm
);
210 Auth::UserRequest::Pointer auth_user_request
= new Auth::Ntlm::UserRequest();
211 assert(auth_user_request
->user() == nullptr);
213 auth_user_request
->user(newUser
);
214 auth_user_request
->user()->auth_type
= Auth::AUTH_NTLM
;
216 auth_user_request
->user()->BuildUserKey(proxy_auth
, aRequestRealm
);
218 /* all we have to do is identify that it's NTLM - the helper does the rest */
219 debugs(29, 9, "decode: NTLM authentication");
220 return auth_user_request
;