2 * Copyright (C) 1996-2020 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 "http/Stream.h"
26 #include "HttpHeaderTools.h"
27 #include "HttpReply.h"
28 #include "HttpRequest.h"
29 #include "mgr/Registration.h"
30 #include "SquidTime.h"
34 static AUTHSSTATS authenticateNegotiateStats
;
36 statefulhelper
*negotiateauthenticators
= NULL
;
38 static int authnegotiate_initialised
= 0;
40 static hash_table
*proxy_auth_cache
= NULL
;
43 Auth::Negotiate::Config::rotateHelpers()
45 /* schedule closure of existing helpers */
46 if (negotiateauthenticators
) {
47 helperStatefulShutdown(negotiateauthenticators
);
50 /* NP: dynamic helper restart will ensure they start up again as needed. */
54 Auth::Negotiate::Config::done()
56 Auth::SchemeConfig::done();
58 authnegotiate_initialised
= 0;
60 if (negotiateauthenticators
) {
61 helperStatefulShutdown(negotiateauthenticators
);
67 delete negotiateauthenticators
;
68 negotiateauthenticators
= NULL
;
70 if (authenticateProgram
)
71 wordlistDestroy(&authenticateProgram
);
73 debugs(29, DBG_IMPORTANT
, "Reconfigure: Negotiate authentication configuration cleared.");
77 Auth::Negotiate::Config::type() const
79 return Auth::Negotiate::Scheme::GetInstance()->type();
83 * Initialize helpers and the like for this auth scheme.
84 * Called AFTER parsing the config file
87 Auth::Negotiate::Config::init(Auth::SchemeConfig
*)
89 if (authenticateProgram
) {
91 authnegotiate_initialised
= 1;
93 if (negotiateauthenticators
== NULL
)
94 negotiateauthenticators
= new statefulhelper("negotiateauthenticator");
96 if (!proxy_auth_cache
)
97 proxy_auth_cache
= hash_create((HASHCMP
*) strcmp
, 7921, hash_string
);
99 assert(proxy_auth_cache
);
101 negotiateauthenticators
->cmdline
= authenticateProgram
;
103 negotiateauthenticators
->childs
.updateLimits(authenticateChildren
);
105 negotiateauthenticators
->ipc_type
= IPC_STREAM
;
107 helperStatefulOpenServers(negotiateauthenticators
);
112 Auth::Negotiate::Config::registerWithCacheManager(void)
114 Mgr::RegisterAction("negotiateauthenticator",
115 "Negotiate User Authenticator Stats",
116 authenticateNegotiateStats
, 0, 1);
120 Auth::Negotiate::Config::active() const
122 return authnegotiate_initialised
== 1;
126 Auth::Negotiate::Config::configured() const
128 if (authenticateProgram
&& (authenticateChildren
.n_max
!= 0)) {
129 debugs(29, 9, HERE
<< "returning configured");
133 debugs(29, 9, HERE
<< "returning unconfigured");
138 Auth::Negotiate::Config::fixHeader(Auth::UserRequest::Pointer auth_user_request
, HttpReply
*rep
, Http::HdrType reqType
, 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
== NULL
) {
149 debugs(29, 9, HERE
<< "Sending type:" << reqType
<< " header: 'Negotiate'");
150 httpHeaderPutStrf(&rep
->header
, reqType
, "Negotiate");
153 /* drop the connection */
154 rep
->header
.delByName("keep-alive");
155 request
->flags
.proxyKeepalive
= false;
158 Auth::Negotiate::UserRequest
*negotiate_request
= dynamic_cast<Auth::Negotiate::UserRequest
*>(auth_user_request
.getRaw());
159 assert(negotiate_request
!= NULL
);
161 switch (negotiate_request
->user()->credentials()) {
164 /* here it makes sense to drop the connection, as auth is
165 * tied to it, even if MAYBE the client could handle it - Kinkie */
166 rep
->header
.delByName("keep-alive");
167 request
->flags
.proxyKeepalive
= false;
171 /* Special case: authentication finished OK but disallowed by ACL.
172 * Need to start over to give the client another chance.
174 if (negotiate_request
->server_blob
) {
175 debugs(29, 9, HERE
<< "Sending type:" << reqType
<< " header: 'Negotiate " << negotiate_request
->server_blob
<< "'");
176 httpHeaderPutStrf(&rep
->header
, reqType
, "Negotiate %s", negotiate_request
->server_blob
);
177 safe_free(negotiate_request
->server_blob
);
179 debugs(29, 9, HERE
<< "Connection authenticated");
180 httpHeaderPutStrf(&rep
->header
, reqType
, "Negotiate");
184 case Auth::Unchecked
:
185 /* semantic change: do not drop the connection.
186 * 2.5 implementation used to keep it open - Kinkie */
187 debugs(29, 9, HERE
<< "Sending type:" << reqType
<< " header: 'Negotiate'");
188 httpHeaderPutStrf(&rep
->header
, reqType
, "Negotiate");
191 case Auth::Handshake
:
192 /* we're waiting for a response from the client. Pass it the blob */
193 debugs(29, 9, HERE
<< "Sending type:" << reqType
<< " header: 'Negotiate " << negotiate_request
->server_blob
<< "'");
194 httpHeaderPutStrf(&rep
->header
, reqType
, "Negotiate %s", negotiate_request
->server_blob
);
195 safe_free(negotiate_request
->server_blob
);
199 debugs(29, DBG_CRITICAL
, "ERROR: Negotiate auth fixHeader: state " << negotiate_request
->user()->credentials() << ".");
200 fatal("unexpected state in AuthenticateNegotiateFixErrorHeader.\n");
206 authenticateNegotiateStats(StoreEntry
* sentry
)
208 if (negotiateauthenticators
)
209 negotiateauthenticators
->packStatsInto(sentry
, "Negotiate Authenticator Statistics");
213 * Decode a Negotiate [Proxy-]Auth string, placing the results in the passed
214 * Auth_user structure.
216 Auth::UserRequest::Pointer
217 Auth::Negotiate::Config::decode(char const *proxy_auth
, const HttpRequest
*, const char *aRequestRealm
)
219 Auth::Negotiate::User
*newUser
= new Auth::Negotiate::User(Auth::SchemeConfig::Find("negotiate"), aRequestRealm
);
220 Auth::UserRequest
*auth_user_request
= new Auth::Negotiate::UserRequest();
221 assert(auth_user_request
->user() == NULL
);
223 auth_user_request
->user(newUser
);
224 auth_user_request
->user()->auth_type
= Auth::AUTH_NEGOTIATE
;
226 auth_user_request
->user()->BuildUserKey(proxy_auth
, aRequestRealm
);
228 /* all we have to do is identify that it's Negotiate - the helper does the rest */
229 debugs(29, 9, HERE
<< "decode Negotiate authentication");
230 return auth_user_request
;