1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
/*
* mswin_ntlm_auth: helper for NTLM Authentication for Squid Cache
*
* (C)2002,2003 Guido Serassio - Acme Consulting S.r.l.
*
* Authors:
* Guido Serassio <guido.serassio@acmeconsulting.it>
* Acme Consulting S.r.l., Italy <http://www.acmeconsulting.it>
*
* With contributions from others mentioned in the change history section
* below.
*
* Based on previous work of Francesco Chemolli and Robert Collins.
*
* Dependencies: Windows NT4 SP4 and later.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
* History:
*
* Version 1.21
* 21-02-2004 Guido Serassio
* Removed control of use of NTLM NEGOTIATE packet from
* command line, now the support is automatic.
* Version 1.20
* 30-11-2003 Guido Serassio
* Added support for NTLM local calls.
* Added control of use of NTLM NEGOTIATE packet from
* command line.
* Updated documentation.
* Version 1.10
* 07-09-2003 Guido Serassio
* Now is true NTLM authenticator.
* More debug info.
* Updated documentation.
* Version 1.0
* 29-06-2002 Guido Serassio
* First release.
*
*
*/
#include "util.h"
#if HAVE_GETOPT_H
#include <getopt.h>
#endif
#include "ntlm.h"
#if HAVE_CTYPE_H
#include <ctype.h>
#endif
#define BUFFER_SIZE 10240
#ifdef NTLM_FAIL_OPEN
int last_ditch_enabled = 0;
#endif
int debug_enabled = 0;
int NTLM_packet_debug_enabled = 0;
static int have_challenge;
char *NTAllowedGroup;
char *NTDisAllowedGroup;
int UseDisallowedGroup = 0;
int UseAllowedGroup = 0;
#if FAIL_DEBUG
int fail_debug_enabled = 0;
#endif
/* makes a null-terminated string upper-case. Changes CONTENTS! */
void
uc(char *string)
{
char *p = string, c;
while ((c = *p)) {
*p = toupper(c);
p++;
}
}
/* makes a null-terminated string lower-case. Changes CONTENTS! */
static void
lc(char *string)
{
char *p = string, c;
while ((c = *p)) {
*p = tolower(c);
p++;
}
}
void
helperfail(const char *reason)
{
#if FAIL_DEBUG
fail_debug_enabled = 1;
#endif
SEND2("BH %s", reason);
}
/*
* options:
* -d enable debugging.
* -v enable verbose NTLM packet debugging.
* -l if specified, changes behavior on failures to last-ditch.
* -A can specify a Windows Local Group name allowed to authenticate.
* -D can specify a Windows Local Group name not allowed to authenticate.
*/
char *my_program_name = NULL;
void
usage()
{
fprintf(stderr,
#ifdef NTLM_FAIL_OPEN
"Usage: %s [-d] [-v] [-A|D LocalUserGroup] [-l] [-h]\n"
#else
"Usage: %s [-d] [-v] [-A|D LocalUserGroup] [-h]\n"
#endif
" -d enable debugging.\n"
" -v enable verbose NTLM packet debugging.\n"
#ifdef NTLM_FAIL_OPEN
" -l if specified, changes behavior on failures to last-ditch\n"
#endif
" -A specify a Windows Local Group name allowed to authenticate\n"
" -D specify a Windows Local Group name not allowed to authenticate\n"
" -h this message\n\n",
my_program_name);
}
void
process_options(int argc, char *argv[])
{
int opt, had_error = 0;
opterr = 0;
#ifdef NTLM_FAIL_OPEN
while (-1 != (opt = getopt(argc, argv, "hdvlA:D:"))) {
#else
while (-1 != (opt = getopt(argc, argv, "hdvA:D:"))) {
#endif
switch (opt) {
case 'A':
safe_free(NTAllowedGroup);
NTAllowedGroup = xstrdup(optarg);
UseAllowedGroup = 1;
break;
case 'D':
safe_free(NTDisAllowedGroup);
NTDisAllowedGroup = xstrdup(optarg);
UseDisallowedGroup = 1;
break;
#ifdef NTLM_FAIL_OPEN
case 'l':
last_ditch_enabled = 1;
break;
#endif
case 'd':
debug_enabled = 1;
break;
case 'v':
debug_enabled = 1;
NTLM_packet_debug_enabled = 1;
break;
case 'h':
usage();
exit(0);
case '?':
opt = optopt;
/* fall thru to default */
default:
fprintf(stderr, "unknown option: -%c. Exiting\n", opt);
usage();
had_error = 1;
}
}
if (had_error)
exit(1);
}
const char *
obtain_challenge(ntlm_negotiate * nego, int nego_length)
{
const char *ch = NULL;
debug("attempting SSPI challenge retrieval\n");
ch = SSP_MakeChallenge(nego, nego_length);
if (ch) {
debug("Got it\n");
return ch; /* All went OK, returning */
}
return NULL;
}
int
manage_request()
{
ntlmhdr *fast_header;
char buf[BUFFER_SIZE];
char helper_command[3];
char *c, *decoded, *cred;
int plen;
int oversized = 0;
char *ErrorMessage;
try_again:
if (fgets(buf, BUFFER_SIZE, stdin) == NULL)
return 0;
c = memchr(buf, '\n', BUFFER_SIZE); /* safer against overrun than strchr */
if (c) {
if (oversized) {
helperfail("illegal request received");
fprintf(stderr, "Illegal request received: '%s'\n", buf);
return 1;
}
*c = '\0';
} else {
fprintf(stderr, "No newline in '%s'\n", buf);
oversized = 1;
goto try_again;
}
if ((strlen(buf) > 3) && NTLM_packet_debug_enabled) {
decoded = base64_decode(buf + 3);
strncpy(helper_command, buf, 2);
debug("Got '%s' from Squid with data:\n", helper_command);
hex_dump(decoded, ((strlen(buf) - 3) * 3) / 4);
} else
debug("Got '%s' from Squid\n", buf);
if (memcmp(buf, "YR", 2) == 0) { /* refresh-request */
/* figure out what we got */
if (strlen(buf) > 3)
decoded = base64_decode(buf + 3);
else
decoded = base64_decode(ntlm_make_negotiate());
/* Note: we don't need to manage memory at this point, since
* base64_decode returns a pointer to static storage.
*/
if (!decoded) { /* decoding failure, return error */
SEND("NA Packet format error, couldn't base64-decode");
return 1;
}
/* fast-track-decode request type. */
fast_header = (struct _ntlmhdr *) decoded;
/* sanity-check: it IS a NTLMSSP packet, isn't it? */
if (memcmp(fast_header->signature, "NTLMSSP", 8) != 0) {
SEND("NA Broken authentication packet");
return 1;
}
switch (fast_header->type) {
case NTLM_NEGOTIATE:
/* Obtain challenge against SSPI */
if (strlen(buf) > 3)
plen = (strlen(buf) - 3) * 3 / 4; /* we only need it here. Optimization */
else
plen = NEGOTIATE_LENGTH;
if ((c = (char *) obtain_challenge((ntlm_negotiate *) decoded, plen)) != NULL) {
if (NTLM_packet_debug_enabled) {
printf("TT %s\n", c);
decoded = base64_decode(c);
debug("sending 'TT' to squid with data:\n");
hex_dump(decoded, (strlen(c) * 3) / 4);
if (NTLM_LocalCall)
debug("NTLM Local Call detected\n");
} else {
SEND2("TT %s", c);
}
have_challenge = 1;
} else
helperfail("can't obtain challenge");
return 1;
/* notreached */
case NTLM_CHALLENGE:
SEND
("NA Got a challenge. We refuse to have our authority disputed");
return 1;
/* notreached */
case NTLM_AUTHENTICATE:
SEND("NA Got authentication request instead of negotiate request");
return 1;
/* notreached */
default:
helperfail("unknown refresh-request packet type");
return 1;
}
return 1;
}
if (memcmp(buf, "KK ", 3) == 0) { /* authenticate-request */
if (!have_challenge) {
helperfail("invalid challenge");
return 1;
}
/* figure out what we got */
decoded = base64_decode(buf + 3);
/* Note: we don't need to manage memory at this point, since
* base64_decode returns a pointer to static storage.
*/
if (!decoded) { /* decoding failure, return error */
SEND("NA Packet format error, couldn't base64-decode");
return 1;
}
/* fast-track-decode request type. */
fast_header = (struct _ntlmhdr *) decoded;
/* sanity-check: it IS a NTLMSSP packet, isn't it? */
if (memcmp(fast_header->signature, "NTLMSSP", 8) != 0) {
SEND("NA Broken authentication packet");
return 1;
}
switch (fast_header->type) {
case NTLM_NEGOTIATE:
SEND("NA Invalid negotiation request received");
return 1;
/* notreached */
case NTLM_CHALLENGE:
SEND
("NA Got a challenge. We refuse to have our authority disputed");
return 1;
/* notreached */
case NTLM_AUTHENTICATE:
/* check against SSPI */
plen = (strlen(buf) - 3) * 3 / 4; /* we only need it here. Optimization */
cred = ntlm_check_auth((ntlm_authenticate *) decoded, plen);
have_challenge = 0;
if (cred == NULL) {
#if FAIL_DEBUG
fail_debug_enabled = 1;
#endif
switch (ntlm_errno) {
case NTLM_BAD_NTGROUP:
SEND("NA Incorrect Group Membership");
return 1;
case NTLM_BAD_REQUEST:
SEND("NA Incorrect Request Format");
return 1;
case NTLM_SSPI_ERROR:
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) & ErrorMessage,
0,
NULL);
if (ErrorMessage[strlen(ErrorMessage) - 1] == '\n')
ErrorMessage[strlen(ErrorMessage) - 1] = '\0';
if (ErrorMessage[strlen(ErrorMessage) - 1] == '\r')
ErrorMessage[strlen(ErrorMessage) - 1] = '\0';
SEND2("NA %s", ErrorMessage);
LocalFree(ErrorMessage);
return 1;
default:
SEND("NA Unknown Error");
return 1;
}
}
lc(cred); /* let's lowercase them for our convenience */
SEND2("AF %s", cred);
return 1;
default:
helperfail("unknown authentication packet type");
return 1;
}
return 1;
} else { /* not an auth-request */
helperfail("illegal request received");
fprintf(stderr, "Illegal request received: '%s'\n", buf);
return 1;
}
helperfail("detected protocol error");
return 1;
/********* END ********/
}
int
main(int argc, char *argv[])
{
my_program_name = argv[0];
process_options(argc, argv);
debug("%s build " __DATE__ ", " __TIME__ " starting up...\n", my_program_name);
if (LoadSecurityDll(SSP_NTLM, NTLM_PACKAGE_NAME) == NULL) {
fprintf(stderr, "FATAL, can't initialize SSPI, exiting.\n");
exit(1);
}
debug("SSPI initialized OK\n");
atexit(UnloadSecurityDll);
/* initialize FDescs */
setbuf(stdout, NULL);
setbuf(stderr, NULL);
while (manage_request()) {
/* everything is done within manage_request */
}
exit(0);
}
|