]> git.ipfire.org Git - thirdparty/squid.git/blob - src/auth/ntlm/Config.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / auth / ntlm / Config.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 /* DEBUG: section 29 NTLM Authenticator */
10
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 */
14
15 #include "squid.h"
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"
22 #include "cache_cf.h"
23 #include "client_side.h"
24 #include "helper.h"
25 #include "HttpHeaderTools.h"
26 #include "HttpReply.h"
27 #include "HttpRequest.h"
28 #include "mgr/Registration.h"
29 #include "SquidTime.h"
30 #include "Store.h"
31 #include "wordlist.h"
32
33 /* NTLM Scheme */
34 static AUTHSSTATS authenticateNTLMStats;
35
36 statefulhelper *ntlmauthenticators = NULL;
37 static int authntlm_initialised = 0;
38
39 static hash_table *proxy_auth_cache = NULL;
40
41 void
42 Auth::Ntlm::Config::rotateHelpers()
43 {
44 /* schedule closure of existing helpers */
45 if (ntlmauthenticators) {
46 helperStatefulShutdown(ntlmauthenticators);
47 }
48
49 /* NP: dynamic helper restart will ensure they start up again as needed. */
50 }
51
52 /* free any allocated configuration details */
53 void
54 Auth::Ntlm::Config::done()
55 {
56 Auth::Config::done();
57
58 authntlm_initialised = 0;
59
60 if (ntlmauthenticators) {
61 helperStatefulShutdown(ntlmauthenticators);
62 }
63
64 if (!shutting_down)
65 return;
66
67 delete ntlmauthenticators;
68 ntlmauthenticators = NULL;
69
70 if (authenticateProgram)
71 wordlistDestroy(&authenticateProgram);
72
73 debugs(29, DBG_IMPORTANT, "Reconfigure: NTLM authentication configuration cleared.");
74 }
75
76 bool
77 Auth::Ntlm::Config::dump(StoreEntry * entry, const char *name, Auth::Config * scheme) const
78 {
79 if (!Auth::Config::dump(entry, name, scheme))
80 return false;
81
82 storeAppendPrintf(entry, "%s ntlm keep_alive %s\n", name, keep_alive ? "on" : "off");
83 return true;
84 }
85
86 Auth::Ntlm::Config::Config() : keep_alive(1)
87 { }
88
89 void
90 Auth::Ntlm::Config::parse(Auth::Config * scheme, int n_configured, char *param_str)
91 {
92 if (strcmp(param_str, "program") == 0) {
93 if (authenticateProgram)
94 wordlistDestroy(&authenticateProgram);
95
96 parse_wordlist(&authenticateProgram);
97
98 requirePathnameExists("auth_param ntlm program", authenticateProgram->key);
99 } else if (strcmp(param_str, "keep_alive") == 0) {
100 parse_onoff(&keep_alive);
101 } else
102 Auth::Config::parse(scheme, n_configured, param_str);
103 }
104
105 const char *
106 Auth::Ntlm::Config::type() const
107 {
108 return Auth::Ntlm::Scheme::GetInstance()->type();
109 }
110
111 /* Initialize helpers and the like for this auth scheme. Called AFTER parsing the
112 * config file */
113 void
114 Auth::Ntlm::Config::init(Auth::Config * scheme)
115 {
116 if (authenticateProgram) {
117
118 authntlm_initialised = 1;
119
120 if (ntlmauthenticators == NULL)
121 ntlmauthenticators = new statefulhelper("ntlmauthenticator");
122
123 if (!proxy_auth_cache)
124 proxy_auth_cache = hash_create((HASHCMP *) strcmp, 7921, hash_string);
125
126 assert(proxy_auth_cache);
127
128 ntlmauthenticators->cmdline = authenticateProgram;
129
130 ntlmauthenticators->childs.updateLimits(authenticateChildren);
131
132 ntlmauthenticators->ipc_type = IPC_STREAM;
133
134 helperStatefulOpenServers(ntlmauthenticators);
135 }
136 }
137
138 void
139 Auth::Ntlm::Config::registerWithCacheManager(void)
140 {
141 Mgr::RegisterAction("ntlmauthenticator",
142 "NTLM User Authenticator Stats",
143 authenticateNTLMStats, 0, 1);
144 }
145
146 bool
147 Auth::Ntlm::Config::active() const
148 {
149 return authntlm_initialised == 1;
150 }
151
152 bool
153 Auth::Ntlm::Config::configured() const
154 {
155 if ((authenticateProgram != NULL) && (authenticateChildren.n_max != 0)) {
156 debugs(29, 9, HERE << "returning configured");
157 return true;
158 }
159
160 debugs(29, 9, HERE << "returning unconfigured");
161 return false;
162 }
163
164 /* NTLM Scheme */
165
166 void
167 Auth::Ntlm::Config::fixHeader(Auth::UserRequest::Pointer auth_user_request, HttpReply *rep, http_hdr_type hdrType, HttpRequest * request)
168 {
169 if (!authenticateProgram)
170 return;
171
172 /* Need keep-alive */
173 if (!request->flags.proxyKeepalive && request->flags.mustKeepalive)
174 return;
175
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");
180
181 if (!keep_alive) {
182 /* drop the connection */
183 request->flags.proxyKeepalive = false;
184 }
185 } else {
186 Auth::Ntlm::UserRequest *ntlm_request = dynamic_cast<Auth::Ntlm::UserRequest *>(auth_user_request.getRaw());
187 assert(ntlm_request != NULL);
188
189 switch (ntlm_request->user()->credentials()) {
190
191 case Auth::Failed:
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;
195 /* fall through */
196
197 case Auth::Ok:
198 /* Special case: authentication finished OK but disallowed by ACL.
199 * Need to start over to give the client another chance.
200 */
201 /* fall through */
202
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");
208 break;
209
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);
215 break;
216
217 default:
218 debugs(29, DBG_CRITICAL, "NTLM Auth fixHeader: state " << ntlm_request->user()->credentials() << ".");
219 fatal("unexpected state in AuthenticateNTLMFixErrorHeader.\n");
220 }
221 }
222 }
223
224 static void
225 authenticateNTLMStats(StoreEntry * sentry)
226 {
227 helperStatefulStats(sentry, ntlmauthenticators, "NTLM Authenticator Statistics");
228 }
229
230 /*
231 * Decode a NTLM [Proxy-]Auth string, placing the results in the passed
232 * Auth_user structure.
233 */
234 Auth::UserRequest::Pointer
235 Auth::Ntlm::Config::decode(char const *proxy_auth, const char *aRequestRealm)
236 {
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);
240
241 auth_user_request->user(newUser);
242 auth_user_request->user()->auth_type = Auth::AUTH_NTLM;
243
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;
247 }
248