]> git.ipfire.org Git - thirdparty/squid.git/blame - src/auth/ntlm/auth_ntlm.cc
Moved log/access_log.cc prototypes to log/access_log.h
[thirdparty/squid.git] / src / auth / ntlm / auth_ntlm.cc
CommitLineData
94439e4e 1/*
262a0e14 2 * $Id$
94439e4e 3 *
4 * DEBUG: section 29 NTLM Authenticator
6bf4f823 5 * AUTHOR: Robert Collins, Henrik Nordstrom, Francesco Chemolli
94439e4e 6 *
2b6662ba 7 * SQUID Web Proxy Cache http://www.squid-cache.org/
94439e4e 8 * ----------------------------------------------------------
9 *
2b6662ba 10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
94439e4e 18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
26ac0430 23 *
94439e4e 24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
26ac0430 28 *
94439e4e 29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 *
33 */
34
35/* The functions in this file handle authentication.
36 * They DO NOT perform access control or auditing.
37 * See acl.c for access control and client_side.c for auditing */
38
39
582c2af2 40#include "squid.h"
3ad63615 41#include "auth/Gadgets.h"
5817ee13 42#include "auth/ntlm/auth_ntlm.h"
616cfc4c 43#include "auth/ntlm/Scheme.h"
aa110616 44#include "auth/ntlm/User.h"
616cfc4c 45#include "auth/ntlm/UserRequest.h"
928f3421 46#include "auth/State.h"
8822ebee 47#include "mgr/Registration.h"
e6ccf245 48#include "Store.h"
a46d2c0e 49#include "client_side.h"
924f73bc 50#include "HttpReply.h"
a2ac85d9 51#include "HttpRequest.h"
582c2af2 52#include "protos.h"
d295d770 53#include "wordlist.h"
cc192b50 54#include "SquidTime.h"
c78aa667 55
94439e4e 56/* NTLM Scheme */
94439e4e 57static AUTHSSTATS authenticateNTLMStats;
94439e4e 58
928f3421 59statefulhelper *ntlmauthenticators = NULL;
94439e4e 60static int authntlm_initialised = 0;
61
94439e4e 62static hash_table *proxy_auth_cache = NULL;
63
64/*
65 *
66 * Private Functions
67 *
68 */
69
0bcb6908 70void
372fccd6 71Auth::Ntlm::Config::rotateHelpers()
0bcb6908
AJ
72{
73 /* schedule closure of existing helpers */
74 if (ntlmauthenticators) {
75 helperStatefulShutdown(ntlmauthenticators);
76 }
77
78 /* NP: dynamic helper restart will ensure they start up again as needed. */
79}
80
5817ee13 81/* free any allocated configuration details */
f5691f9c 82void
372fccd6 83Auth::Ntlm::Config::done()
94439e4e 84{
94439e4e 85 authntlm_initialised = 0;
62e76326 86
5817ee13
AJ
87 if (ntlmauthenticators) {
88 helperStatefulShutdown(ntlmauthenticators);
5817ee13 89 }
62e76326 90
94439e4e 91 if (!shutting_down)
62e76326 92 return;
93
48d54e4d 94 delete ntlmauthenticators;
94439e4e 95 ntlmauthenticators = NULL;
62e76326 96
58ee2093
AJ
97 if (authenticateProgram)
98 wordlistDestroy(&authenticateProgram);
cdabe87d 99
372fccd6 100 debugs(29, DBG_IMPORTANT, "Reconfigure: NTLM authentication configuration cleared.");
94439e4e 101}
102
f5691f9c 103void
372fccd6 104Auth::Ntlm::Config::dump(StoreEntry * entry, const char *name, Auth::Config * scheme)
94439e4e 105{
58ee2093 106 wordlist *list = authenticateProgram;
94439e4e 107 storeAppendPrintf(entry, "%s %s", name, "ntlm");
62e76326 108
94439e4e 109 while (list != NULL) {
62e76326 110 storeAppendPrintf(entry, " %s", list->key);
111 list = list->next;
94439e4e 112 }
62e76326 113
404cfda1
AJ
114 storeAppendPrintf(entry, "\n%s ntlm children %d startup=%d idle=%d concurrency=%d\n",
115 name, authenticateChildren.n_max, authenticateChildren.n_startup, authenticateChildren.n_idle, authenticateChildren.concurrency);
6bf4f823 116 storeAppendPrintf(entry, "%s %s keep_alive %s\n", name, "ntlm", keep_alive ? "on" : "off");
94439e4e 117
118}
119
372fccd6 120Auth::Ntlm::Config::Config() : keep_alive(1)
6bf4f823 121{ }
62e76326 122
f5691f9c 123void
372fccd6 124Auth::Ntlm::Config::parse(Auth::Config * scheme, int n_configured, char *param_str)
f5691f9c 125{
94439e4e 126 if (strcasecmp(param_str, "program") == 0) {
58ee2093
AJ
127 if (authenticateProgram)
128 wordlistDestroy(&authenticateProgram);
62e76326 129
58ee2093 130 parse_wordlist(&authenticateProgram);
62e76326 131
58ee2093 132 requirePathnameExists("auth_param ntlm program", authenticateProgram->key);
94439e4e 133 } else if (strcasecmp(param_str, "children") == 0) {
48d54e4d 134 authenticateChildren.parseConfig();
6bf4f823 135 } else if (strcasecmp(param_str, "keep_alive") == 0) {
136 parse_onoff(&keep_alive);
94439e4e 137 } else {
372fccd6 138 debugs(29, DBG_CRITICAL, "ERROR unrecognised NTLM auth scheme parameter '" << param_str << "'");
94439e4e 139 }
140}
141
f5691f9c 142const char *
372fccd6 143Auth::Ntlm::Config::type() const
94439e4e 144{
d6374be6 145 return Auth::Ntlm::Scheme::GetInstance()->type();
94439e4e 146}
147
148/* Initialize helpers and the like for this auth scheme. Called AFTER parsing the
149 * config file */
f5691f9c 150void
372fccd6 151Auth::Ntlm::Config::init(Auth::Config * scheme)
94439e4e 152{
58ee2093 153 if (authenticateProgram) {
6bf4f823 154
62e76326 155 authntlm_initialised = 1;
156
157 if (ntlmauthenticators == NULL)
48d54e4d 158 ntlmauthenticators = new statefulhelper("ntlmauthenticator");
62e76326 159
160 if (!proxy_auth_cache)
30abd221 161 proxy_auth_cache = hash_create((HASHCMP *) strcmp, 7921, hash_string);
62e76326 162
163 assert(proxy_auth_cache);
164
58ee2093 165 ntlmauthenticators->cmdline = authenticateProgram;
62e76326 166
1af735c7 167 ntlmauthenticators->childs.updateLimits(authenticateChildren);
62e76326 168
169 ntlmauthenticators->ipc_type = IPC_STREAM;
170
62e76326 171 helperStatefulOpenServers(ntlmauthenticators);
94439e4e 172 }
173}
174
62ee09ca 175void
372fccd6 176Auth::Ntlm::Config::registerWithCacheManager(void)
62ee09ca 177{
8822ebee 178 Mgr::RegisterAction("ntlmauthenticator",
d9fc6862
A
179 "NTLM User Authenticator Stats",
180 authenticateNTLMStats, 0, 1);
62ee09ca 181}
182
f5691f9c 183bool
372fccd6 184Auth::Ntlm::Config::active() const
2d70df72 185{
f5691f9c 186 return authntlm_initialised == 1;
2d70df72 187}
188
f5691f9c 189bool
372fccd6 190Auth::Ntlm::Config::configured() const
94439e4e 191{
58ee2093 192 if ((authenticateProgram != NULL) && (authenticateChildren.n_max != 0)) {
372fccd6 193 debugs(29, 9, HERE << "returning configured");
f5691f9c 194 return true;
2d70df72 195 }
62e76326 196
372fccd6 197 debugs(29, 9, HERE << "returning unconfigured");
f5691f9c 198 return false;
94439e4e 199}
200
201/* NTLM Scheme */
94439e4e 202
f5691f9c 203void
c7baff40 204Auth::Ntlm::Config::fixHeader(Auth::UserRequest::Pointer auth_user_request, HttpReply *rep, http_hdr_type hdrType, HttpRequest * request)
94439e4e 205{
58ee2093 206 if (!authenticateProgram)
6bf4f823 207 return;
62e76326 208
63a05fa3 209 /* Need keep-alive */
210 if (!request->flags.proxy_keepalive && request->flags.must_keepalive)
26ac0430 211 return;
63a05fa3 212
6bf4f823 213 /* New request, no user details */
214 if (auth_user_request == NULL) {
372fccd6 215 debugs(29, 9, HERE << "Sending type:" << hdrType << " header: 'NTLM'");
18ec8500 216 httpHeaderPutStrf(&rep->header, hdrType, "NTLM");
6bf4f823 217
218 if (!keep_alive) {
62e76326 219 /* drop the connection */
62e76326 220 request->flags.proxy_keepalive = 0;
62e76326 221 }
6bf4f823 222 } else {
c7baff40 223 Auth::Ntlm::UserRequest *ntlm_request = dynamic_cast<Auth::Ntlm::UserRequest *>(auth_user_request.getRaw());
3a11f20d 224 assert(ntlm_request != NULL);
225
d232141d 226 switch (ntlm_request->user()->credentials()) {
62e76326 227
d87154ee 228 case Auth::Failed:
6bf4f823 229 /* here it makes sense to drop the connection, as auth is
230 * tied to it, even if MAYBE the client could handle it - Kinkie */
6bf4f823 231 request->flags.proxy_keepalive = 0;
232 /* fall through */
94439e4e 233
d87154ee 234 case Auth::Ok:
6bf4f823 235 /* Special case: authentication finished OK but disallowed by ACL.
236 * Need to start over to give the client another chance.
237 */
238 /* fall through */
62e76326 239
d87154ee 240 case Auth::Unchecked:
6bf4f823 241 /* semantic change: do not drop the connection.
242 * 2.5 implementation used to keep it open - Kinkie */
372fccd6 243 debugs(29, 9, HERE << "Sending type:" << hdrType << " header: 'NTLM'");
18ec8500 244 httpHeaderPutStrf(&rep->header, hdrType, "NTLM");
6bf4f823 245 break;
62e76326 246
d87154ee 247 case Auth::Handshake:
6bf4f823 248 /* we're waiting for a response from the client. Pass it the blob */
372fccd6 249 debugs(29, 9, HERE << "Sending type:" << hdrType << " header: 'NTLM " << ntlm_request->server_blob << "'");
18ec8500 250 httpHeaderPutStrf(&rep->header, hdrType, "NTLM %s", ntlm_request->server_blob);
6bf4f823 251 safe_free(ntlm_request->server_blob);
252 break;
62e76326 253
6bf4f823 254 default:
372fccd6 255 debugs(29, DBG_CRITICAL, "NTLM Auth fixHeader: state " << ntlm_request->user()->credentials() << ".");
6bf4f823 256 fatal("unexpected state in AuthenticateNTLMFixErrorHeader.\n");
257 }
258 }
259}
62e76326 260
94439e4e 261static void
262authenticateNTLMStats(StoreEntry * sentry)
263{
9522b380 264 helperStatefulStats(sentry, ntlmauthenticators, "NTLM Authenticator Statistics");
94439e4e 265}
266
94439e4e 267/*
6bf4f823 268 * Decode a NTLM [Proxy-]Auth string, placing the results in the passed
94439e4e 269 * Auth_user structure.
270 */
c7baff40 271Auth::UserRequest::Pointer
372fccd6 272Auth::Ntlm::Config::decode(char const *proxy_auth)
94439e4e 273{
aa110616 274 Auth::Ntlm::User *newUser = new Auth::Ntlm::User(Auth::Config::Find("ntlm"));
c7baff40 275 Auth::UserRequest::Pointer auth_user_request = new Auth::Ntlm::UserRequest();
f5691f9c 276 assert(auth_user_request->user() == NULL);
a33a428a 277
f5691f9c 278 auth_user_request->user(newUser);
616cfc4c 279 auth_user_request->user()->auth_type = Auth::AUTH_NTLM;
94439e4e 280
281 /* all we have to do is identify that it's NTLM - the helper does the rest */
372fccd6 282 debugs(29, 9, HERE << "decode: NTLM authentication");
f5691f9c 283 return auth_user_request;
94439e4e 284}