2 * Copyright (C) 1996-2015 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 Negotiate 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/negotiate/Config.h"
18 #include "auth/negotiate/Scheme.h"
19 #include "auth/negotiate/User.h"
20 #include "auth/negotiate/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 \defgroup AuthNegotiateInternal Negotiate Authenticator Internals
35 \ingroup AuthNegotiateAPI
38 /* Negotiate Scheme */
39 static AUTHSSTATS authenticateNegotiateStats
;
41 /// \ingroup AuthNegotiateInternal
42 statefulhelper
*negotiateauthenticators
= NULL
;
44 /// \ingroup AuthNegotiateInternal
45 static int authnegotiate_initialised
= 0;
47 /// \ingroup AuthNegotiateInternal
48 static hash_table
*proxy_auth_cache
= NULL
;
51 Auth::Negotiate::Config::rotateHelpers()
53 /* schedule closure of existing helpers */
54 if (negotiateauthenticators
) {
55 helperStatefulShutdown(negotiateauthenticators
);
58 /* NP: dynamic helper restart will ensure they start up again as needed. */
62 Auth::Negotiate::Config::done()
66 authnegotiate_initialised
= 0;
68 if (negotiateauthenticators
) {
69 helperStatefulShutdown(negotiateauthenticators
);
75 delete negotiateauthenticators
;
76 negotiateauthenticators
= NULL
;
78 if (authenticateProgram
)
79 wordlistDestroy(&authenticateProgram
);
81 debugs(29, DBG_IMPORTANT
, "Reconfigure: Negotiate authentication configuration cleared.");
85 Auth::Negotiate::Config::dump(StoreEntry
* entry
, const char *name
, Auth::Config
* scheme
) const
87 if (!Auth::Config::dump(entry
, name
, scheme
))
90 storeAppendPrintf(entry
, "%s negotiate keep_alive %s\n", name
, keep_alive
? "on" : "off");
94 Auth::Negotiate::Config::Config() : keep_alive(1)
98 Auth::Negotiate::Config::parse(Auth::Config
* scheme
, int n_configured
, char *param_str
)
100 if (strcmp(param_str
, "program") == 0) {
101 if (authenticateProgram
)
102 wordlistDestroy(&authenticateProgram
);
104 parse_wordlist(&authenticateProgram
);
106 requirePathnameExists("auth_param negotiate program", authenticateProgram
->key
);
107 } else if (strcmp(param_str
, "keep_alive") == 0) {
108 parse_onoff(&keep_alive
);
110 Auth::Config::parse(scheme
, n_configured
, param_str
);
114 Auth::Negotiate::Config::type() const
116 return Auth::Negotiate::Scheme::GetInstance()->type();
120 * Initialize helpers and the like for this auth scheme.
121 * Called AFTER parsing the config file
124 Auth::Negotiate::Config::init(Auth::Config
*)
126 if (authenticateProgram
) {
128 authnegotiate_initialised
= 1;
130 if (negotiateauthenticators
== NULL
)
131 negotiateauthenticators
= new statefulhelper("negotiateauthenticator");
133 if (!proxy_auth_cache
)
134 proxy_auth_cache
= hash_create((HASHCMP
*) strcmp
, 7921, hash_string
);
136 assert(proxy_auth_cache
);
138 negotiateauthenticators
->cmdline
= authenticateProgram
;
140 negotiateauthenticators
->childs
.updateLimits(authenticateChildren
);
142 negotiateauthenticators
->ipc_type
= IPC_STREAM
;
144 helperStatefulOpenServers(negotiateauthenticators
);
149 Auth::Negotiate::Config::registerWithCacheManager(void)
151 Mgr::RegisterAction("negotiateauthenticator",
152 "Negotiate User Authenticator Stats",
153 authenticateNegotiateStats
, 0, 1);
157 Auth::Negotiate::Config::active() const
159 return authnegotiate_initialised
== 1;
163 Auth::Negotiate::Config::configured() const
165 if (authenticateProgram
&& (authenticateChildren
.n_max
!= 0)) {
166 debugs(29, 9, HERE
<< "returning configured");
170 debugs(29, 9, HERE
<< "returning unconfigured");
174 /* Negotiate Scheme */
177 Auth::Negotiate::Config::fixHeader(Auth::UserRequest::Pointer auth_user_request
, HttpReply
*rep
, http_hdr_type reqType
, HttpRequest
* request
)
179 if (!authenticateProgram
)
182 /* Need keep-alive */
183 if (!request
->flags
.proxyKeepalive
&& request
->flags
.mustKeepalive
)
186 /* New request, no user details */
187 if (auth_user_request
== NULL
) {
188 debugs(29, 9, HERE
<< "Sending type:" << reqType
<< " header: 'Negotiate'");
189 httpHeaderPutStrf(&rep
->header
, reqType
, "Negotiate");
192 /* drop the connection */
193 rep
->header
.delByName("keep-alive");
194 request
->flags
.proxyKeepalive
= false;
197 Auth::Negotiate::UserRequest
*negotiate_request
= dynamic_cast<Auth::Negotiate::UserRequest
*>(auth_user_request
.getRaw());
198 assert(negotiate_request
!= NULL
);
200 switch (negotiate_request
->user()->credentials()) {
203 /* here it makes sense to drop the connection, as auth is
204 * tied to it, even if MAYBE the client could handle it - Kinkie */
205 rep
->header
.delByName("keep-alive");
206 request
->flags
.proxyKeepalive
= false;
210 /* Special case: authentication finished OK but disallowed by ACL.
211 * Need to start over to give the client another chance.
213 if (negotiate_request
->server_blob
) {
214 debugs(29, 9, HERE
<< "Sending type:" << reqType
<< " header: 'Negotiate " << negotiate_request
->server_blob
<< "'");
215 httpHeaderPutStrf(&rep
->header
, reqType
, "Negotiate %s", negotiate_request
->server_blob
);
216 safe_free(negotiate_request
->server_blob
);
218 debugs(29, 9, HERE
<< "Connection authenticated");
219 httpHeaderPutStrf(&rep
->header
, reqType
, "Negotiate");
223 case Auth::Unchecked
:
224 /* semantic change: do not drop the connection.
225 * 2.5 implementation used to keep it open - Kinkie */
226 debugs(29, 9, HERE
<< "Sending type:" << reqType
<< " header: 'Negotiate'");
227 httpHeaderPutStrf(&rep
->header
, reqType
, "Negotiate");
230 case Auth::Handshake
:
231 /* we're waiting for a response from the client. Pass it the blob */
232 debugs(29, 9, HERE
<< "Sending type:" << reqType
<< " header: 'Negotiate " << negotiate_request
->server_blob
<< "'");
233 httpHeaderPutStrf(&rep
->header
, reqType
, "Negotiate %s", negotiate_request
->server_blob
);
234 safe_free(negotiate_request
->server_blob
);
238 debugs(29, DBG_CRITICAL
, "ERROR: Negotiate auth fixHeader: state " << negotiate_request
->user()->credentials() << ".");
239 fatal("unexpected state in AuthenticateNegotiateFixErrorHeader.\n");
245 authenticateNegotiateStats(StoreEntry
* sentry
)
247 helperStatefulStats(sentry
, negotiateauthenticators
, "Negotiate Authenticator Statistics");
251 * Decode a Negotiate [Proxy-]Auth string, placing the results in the passed
252 * Auth_user structure.
254 Auth::UserRequest::Pointer
255 Auth::Negotiate::Config::decode(char const *proxy_auth
, const char *aRequestRealm
)
257 Auth::Negotiate::User
*newUser
= new Auth::Negotiate::User(Auth::Config::Find("negotiate"), aRequestRealm
);
258 Auth::UserRequest
*auth_user_request
= new Auth::Negotiate::UserRequest();
259 assert(auth_user_request
->user() == NULL
);
261 auth_user_request
->user(newUser
);
262 auth_user_request
->user()->auth_type
= Auth::AUTH_NEGOTIATE
;
264 auth_user_request
->user()->BuildUserKey(proxy_auth
, aRequestRealm
);
266 /* all we have to do is identify that it's Negotiate - the helper does the rest */
267 debugs(29, 9, HERE
<< "decode Negotiate authentication");
268 return auth_user_request
;