]> git.ipfire.org Git - thirdparty/squid.git/blame - helpers/ntlm_auth/smb_lm/ntlm_smb_lm_auth.cc
Bug 4309: Fix the presence of extensions detection in SSL Hello messages
[thirdparty/squid.git] / helpers / ntlm_auth / smb_lm / ntlm_smb_lm_auth.cc
CommitLineData
ca02e0ec 1/*
bde978a6 2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
ca02e0ec
AJ
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
94439e4e 9/*
10 * (C) 2000 Francesco Chemolli <kinkie@kame.usr.dsi.unimi.it>
11 * Distributed freely under the terms of the GNU General Public License,
12 * version 2. See the file COPYING for licensing details
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
26ac0430 18
94439e4e 19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
94439e4e 22 */
ca02e0ec 23
f7f3304a 24#include "squid.h"
25f98340 25#include "base64.h"
59a09b98 26#include "compat/debug.h"
aadbbd7d 27#include "helpers/defines.h"
7c16470c
AJ
28#include "ntlmauth/ntlmauth.h"
29#include "ntlmauth/support_bits.cci"
30#include "rfcnb/rfcnb.h"
31#include "smblib/smblib.h"
dc47f531 32
074d6a40
AJ
33#include <cassert>
34#include <cctype>
35#include <cerrno>
36#include <csignal>
37#include <cstdlib>
38#include <cstring>
39#include <ctime>
1dcf61eb
AJ
40#if HAVE_UNISTD_H
41#include <unistd.h>
42#endif
94439e4e 43#if HAVE_GETOPT_H
44#include <getopt.h>
45#endif
32d002cb 46#if HAVE_UNISTD_H
45b635fa 47#include <unistd.h>
48#endif
1dcf61eb 49
1dcf61eb
AJ
50/************* CONFIGURATION ***************/
51
52#define DEAD_DC_RETRY_INTERVAL 30
53
54/************* END CONFIGURATION ***************/
55
56/* A couple of harmless helper macros */
57#define SEND(X) debug("sending '%s' to squid\n",X); printf(X "\n");
58#ifdef __GNUC__
59#define SEND2(X,Y...) debug("sending '" X "' to squid\n",Y); printf(X "\n",Y);
60#define SEND3(X,Y...) debug("sending '" X "' to squid\n",Y); printf(X "\n",Y);
61#else
62/* no gcc, no debugging. varargs macros are a gcc extension */
63#define SEND2 printf
64#define SEND3 printf
65#endif
66
67const char *make_challenge(char *domain, char *controller);
68char *ntlm_check_auth(ntlm_authenticate * auth, int auth_length);
69void dc_disconnect(void);
70int connectedp(void);
71int is_dc_ok(char *domain, char *domain_controller);
72
73typedef struct _dc dc;
74struct _dc {
75 char *domain;
76 char *controller;
f53969cc 77 time_t dead; /* 0 if it's alive, otherwise time of death */
1dcf61eb
AJ
78 dc *next;
79};
94439e4e 80
a19a836d 81/* local functions */
a19a836d
AJ
82void usage(void);
83void process_options(int argc, char *argv[]);
84const char * obtain_challenge(void);
85void manage_request(void);
86
1dcf61eb
AJ
87#define ENCODED_PASS_LEN 24
88#define MAX_USERNAME_LEN 255
89#define MAX_DOMAIN_LEN 255
90#define MAX_PASSWD_LEN 31
91
92static unsigned char challenge[NTLM_NONCE_LEN];
93static unsigned char lmencoded_empty_pass[ENCODED_PASS_LEN],
f53969cc 94 ntencoded_empty_pass[ENCODED_PASS_LEN];
1dcf61eb
AJ
95SMB_Handle_Type handle = NULL;
96int ntlm_errno;
f53969cc 97static char credentials[MAX_USERNAME_LEN+MAX_DOMAIN_LEN+2]; /* we can afford to waste */
1dcf61eb
AJ
98static char my_domain[100], my_domain_controller[100];
99static char errstr[1001];
32d002cb 100#if DEBUG
1dcf61eb 101char error_messages_buffer[NTLM_BLOB_BUFFER_SIZE];
2d70df72 102#endif
5d146f7d 103char load_balance = 0, protocol_pedantic = 0;
94439e4e 104dc *controllers = NULL;
105int numcontrollers = 0;
106dc *current_dc;
2d70df72 107char smb_error_buffer[1000];
108
1dcf61eb
AJ
109/* Disconnects from the DC. A reconnection will be done upon the next request
110 */
111void
112dc_disconnect()
9bea1d5b 113{
1dcf61eb
AJ
114 if (handle != NULL)
115 SMB_Discon(handle, 0);
116 handle = NULL;
94439e4e 117}
118
1dcf61eb
AJ
119int
120connectedp()
94439e4e 121{
1dcf61eb
AJ
122 return (handle != NULL);
123}
124
125/* Tries to connect to a DC. Returns 0 on failure, 1 on OK */
126int
127is_dc_ok(char *domain, char *domain_controller)
128{
129 SMB_Handle_Type h = SMB_Connect_Server(NULL, domain_controller, domain);
130 if (h == NULL)
131 return 0;
132 SMB_Discon(h, 0);
133 return 1;
134}
135
136/* returns 0 on success, > 0 on failure */
137static int
138init_challenge(char *domain, char *domain_controller)
139{
140 int smberr;
141
142 if (handle != NULL) {
143 return 0;
94439e4e 144 }
1dcf61eb
AJ
145 debug("Connecting to server %s domain %s\n", domain_controller, domain);
146 handle = SMB_Connect_Server(NULL, domain_controller, domain);
147 smberr = SMB_Get_Last_Error();
148 SMB_Get_Error_Msg(smberr, errstr, 1000);
149
f53969cc 150 if (handle == NULL) { /* couldn't connect */
1dcf61eb
AJ
151 debug("Couldn't connect to SMB Server. Error:%s\n", errstr);
152 return 1;
153 }
f53969cc 154 if (SMB_Negotiate(handle, SMB_Prots) < 0) { /* An error */
1dcf61eb
AJ
155 debug("Error negotiating protocol with SMB Server\n");
156 SMB_Discon(handle, 0);
157 handle = NULL;
158 return 2;
159 }
f53969cc 160 if (handle->Security == 0) { /* share-level security, unuseable */
1dcf61eb
AJ
161 debug("SMB Server uses share-level security .. we need user security.\n");
162 SMB_Discon(handle, 0);
163 handle = NULL;
164 return 3;
165 }
166 memcpy(challenge, handle->Encrypt_Key, NTLM_NONCE_LEN);
167 SMBencrypt((unsigned char *)"",challenge,lmencoded_empty_pass);
168 SMBNTencrypt((unsigned char *)"",challenge,ntencoded_empty_pass);
169 return 0;
94439e4e 170}
171
1dcf61eb
AJ
172const char *
173make_challenge(char *domain, char *domain_controller)
94439e4e 174{
1dcf61eb
AJ
175 /* trying to circumvent some strange problem wih pointers in SMBLib */
176 /* Ugly as hell, but the lib is going to be dropped... */
34107f7c
AJ
177 strncpy(my_domain, domain, sizeof(my_domain)-1);
178 my_domain[sizeof(my_domain)-1] = '\0';
179 strncpy(my_domain_controller, domain_controller, sizeof(my_domain_controller)-1);
180 my_domain_controller[sizeof(my_domain_controller)-1] = '\0';
181
1dcf61eb
AJ
182 if (init_challenge(my_domain, my_domain_controller) > 0) {
183 return NULL;
184 }
aadbbd7d 185
1dcf61eb 186 ntlm_challenge chal;
09aabd84
FC
187 uint32_t flags = NTLM_REQUEST_NON_NT_SESSION_KEY |
188 NTLM_CHALLENGE_TARGET_IS_DOMAIN |
189 NTLM_NEGOTIATE_ALWAYS_SIGN |
190 NTLM_NEGOTIATE_USE_NTLM |
191 NTLM_NEGOTIATE_USE_LM |
192 NTLM_NEGOTIATE_ASCII;
1dcf61eb 193 ntlm_make_challenge(&chal, my_domain, my_domain_controller, (char *)challenge, NTLM_NONCE_LEN, flags);
aadbbd7d
AJ
194
195 size_t len = sizeof(chal) - sizeof(chal.payload) + le16toh(chal.target.maxlen);
196 // for lack of a good NTLM token size limit, allow up to what the helper input can be
197 // validations later will expect to be limited to that size.
198 static uint8_t b64buf[HELPER_INPUT_BUFFER-10]; /* 10 for other line fields, delimiters and terminator */
199 if (base64_encode_len(len) < sizeof(b64buf)-1) {
54835620 200 debug("base64 encoding of the token challenge will exceed %" PRIuSIZE " bytes", sizeof(b64buf));
aadbbd7d
AJ
201 return NULL;
202 }
203
204 struct base64_encode_ctx ctx;
205 base64_encode_init(&ctx);
206 size_t blen = base64_encode_update(&ctx, b64buf, len, reinterpret_cast<const uint8_t *>(&chal));
207 blen += base64_encode_final(&ctx, b64buf+blen);
208 b64buf[blen] = '\0';
209 return reinterpret_cast<const char*>(b64buf);
1dcf61eb
AJ
210}
211
212/* returns NULL on failure, or a pointer to
213 * the user's credentials (domain\\username)
214 * upon success. WARNING. It's pointing to static storage.
215 * In case of problem sets as side-effect ntlm_errno to one of the
216 * codes defined in ntlm.h
217 */
218char *
219ntlm_check_auth(ntlm_authenticate * auth, int auth_length)
220{
221 int rv;
222 char pass[MAX_PASSWD_LEN+1];
223 char *domain = credentials;
224 char *user;
225 lstring tmp;
226
f53969cc 227 if (handle == NULL) { /*if null we aren't connected, but it shouldn't happen */
1dcf61eb
AJ
228 debug("Weird, we've been disconnected\n");
229 ntlm_errno = NTLM_ERR_NOT_CONNECTED;
230 return NULL;
231 }
232
233 /* debug("fetching domain\n"); */
234 tmp = ntlm_fetch_string(&(auth->hdr), auth_length, &auth->domain, auth->flags);
235 if (tmp.str == NULL || tmp.l == 0) {
236 debug("No domain supplied. Returning no-auth\n");
237 ntlm_errno = NTLM_ERR_LOGON;
238 return NULL;
239 }
240 if (tmp.l > MAX_DOMAIN_LEN) {
241 debug("Domain string exceeds %d bytes, rejecting\n", MAX_DOMAIN_LEN);
242 ntlm_errno = NTLM_ERR_LOGON;
243 return NULL;
244 }
245 memcpy(domain, tmp.str, tmp.l);
246 user = domain + tmp.l;
f207fe64
FC
247 *user = '\0';
248 ++user;
1dcf61eb
AJ
249
250 /* debug("fetching user name\n"); */
251 tmp = ntlm_fetch_string(&(auth->hdr), auth_length, &auth->user, auth->flags);
252 if (tmp.str == NULL || tmp.l == 0) {
253 debug("No username supplied. Returning no-auth\n");
254 ntlm_errno = NTLM_ERR_LOGON;
255 return NULL;
256 }
257 if (tmp.l > MAX_USERNAME_LEN) {
258 debug("Username string exceeds %d bytes, rejecting\n", MAX_USERNAME_LEN);
259 ntlm_errno = NTLM_ERR_LOGON;
260 return NULL;
261 }
262 memcpy(user, tmp.str, tmp.l);
263 *(user + tmp.l) = '\0';
264
47567b73
AJ
265 // grab the *response blobs. these are fixed length 24 bytes of binary
266 const ntlmhdr *packet = &(auth->hdr);
267 {
268 const strhdr * str = &auth->lmresponse;
269
270 int16_t len = le16toh(str->len);
271 int32_t offset = le32toh(str->offset);
272
273 if (len != ENCODED_PASS_LEN || offset + len > auth_length || offset == 0) {
274 debug("LM response: insane data (pkt-sz: %d, fetch len: %d, offset: %d)\n", auth_length, len, offset);
275 ntlm_errno = NTLM_ERR_LOGON;
276 return NULL;
277 }
278 tmp.str = (char *)packet + offset;
279 tmp.l = len;
94439e4e 280 }
1dcf61eb
AJ
281 if (tmp.l > MAX_PASSWD_LEN) {
282 debug("Password string exceeds %d bytes, rejecting\n", MAX_PASSWD_LEN);
283 ntlm_errno = NTLM_ERR_LOGON;
284 return NULL;
285 }
286
47567b73 287 /* Authenticating against the NT response doesn't seem to work... in SMB LM helper. */
1dcf61eb
AJ
288 memcpy(pass, tmp.str, tmp.l);
289 pass[min(MAX_PASSWD_LEN,tmp.l)] = '\0';
290
291#if 1
292 debug("Empty LM pass detection: user: '%s', ours:'%s', his: '%s' (length: %d)\n",
293 user,lmencoded_empty_pass,tmp.str,tmp.l);
294 if (memcmp(tmp.str,lmencoded_empty_pass,ENCODED_PASS_LEN)==0) {
295 fprintf(stderr,"Empty LM password supplied for user %s\\%s. "
296 "No-auth\n",domain,user);
297 ntlm_errno=NTLM_ERR_LOGON;
298 return NULL;
299 }
300
47567b73
AJ
301 /* still fetch the NT response and check validity against empty password */
302 {
303 const strhdr * str = &auth->ntresponse;
304 int16_t len = le16toh(str->len);
305 int32_t offset = le32toh(str->offset);
306
307 if (len != ENCODED_PASS_LEN || offset + len > auth_length || offset == 0) {
308 debug("NT response: insane data (pkt-sz: %d, fetch len: %d, offset: %d)\n", auth_length, len, offset);
309 ntlm_errno = NTLM_ERR_LOGON;
310 return NULL;
311 }
312 tmp.str = (char *)packet + offset;
313 tmp.l = len;
314
1dcf61eb
AJ
315 debug("Empty NT pass detection: user: '%s', ours:'%s', his: '%s' (length: %d)\n",
316 user,ntencoded_empty_pass,tmp.str,tmp.l);
317 if (memcmp(tmp.str,lmencoded_empty_pass,ENCODED_PASS_LEN)==0) {
318 fprintf(stderr,"ERROR: Empty NT password supplied for user %s\\%s. No-auth\n", domain, user);
319 ntlm_errno = NTLM_ERR_LOGON;
320 return NULL;
321 }
322 }
323#endif
324
1dcf61eb
AJ
325 debug("checking domain: '%s', user: '%s', pass='%s'\n", domain, user, pass);
326
327 rv = SMB_Logon_Server(handle, user, pass, domain, 1);
328 debug("Login attempt had result %d\n", rv);
329
f53969cc 330 if (rv != NTLM_ERR_NONE) { /* failed */
1dcf61eb
AJ
331 ntlm_errno = rv;
332 return NULL;
333 }
f53969cc 334 *(user - 1) = '\\'; /* hack. Performing, but ugly. */
1dcf61eb
AJ
335
336 debug("credentials: %s\n", credentials);
337 return credentials;
94439e4e 338}
339
daacd51f
AJ
340extern "C" void timeout_during_auth(int signum);
341
1dcf61eb 342static char got_timeout = 0;
daacd51f
AJ
343/** signal handler to be invoked when the authentication operation
344 * times out */
345void
ced8def3 346timeout_during_auth(int)
1dcf61eb
AJ
347{
348 dc_disconnect();
349}
2d70df72 350
94439e4e 351/*
352 * options:
353 * -b try load-balancing the domain-controllers
354 * -f fail-over to another DC if DC connection fails.
5d146f7d 355 * DEPRECATED and VERBOSELY IGNORED. This is on by default now.
2d70df72 356 * -l last-ditch-mode
94439e4e 357 * domain\controller ...
358 */
a88de9ce 359char *my_program_name = NULL;
360
361void
362usage()
363{
364 fprintf(stderr,
26ac0430
AJ
365 "%s usage:\n%s [-b] [-f] [-d] [-l] domain\\controller [domain\\controller ...]\n"
366 "-b enables load-balancing among controllers\n"
367 "-f enables failover among controllers (DEPRECATED and always active)\n"
26ac0430
AJ
368 "-d enables debugging statements if DEBUG was defined at build-time.\n\n"
369 "You MUST specify at least one Domain Controller.\n"
370 "You can use either \\ or / as separator between the domain name \n"
371 "and the controller name\n",
372 my_program_name, my_program_name);
a88de9ce 373}
374
59a09b98 375/* int debug_enabled=0; defined in libcompat */
a88de9ce 376
94439e4e 377void
378process_options(int argc, char *argv[])
379{
380 int opt, j, had_error = 0;
381 dc *new_dc = NULL, *last_dc = NULL;
3c641669 382 while (-1 != (opt = getopt(argc, argv, "bfld"))) {
26ac0430
AJ
383 switch (opt) {
384 case 'b':
385 load_balance = 1;
386 break;
387 case 'f':
388 fprintf(stderr,
389 "WARNING. The -f flag is DEPRECATED and always active.\n");
390 break;
26ac0430
AJ
391 case 'd':
392 debug_enabled=1;
393 break;
394 default:
395 fprintf(stderr, "unknown option: -%c. Exiting\n", opt);
396 usage();
397 had_error = 1;
398 }
94439e4e 399 }
400 if (had_error)
26ac0430 401 exit(1);
94439e4e 402 /* Okay, now begin filling controllers up */
403 /* we can avoid memcpy-ing, and just reuse argv[] */
755494da 404 for (j = optind; j < argc; ++j) {
26ac0430
AJ
405 char *d, *c;
406 /* d will not be freed in case of non-error. Since we don't reconfigure,
407 * it's going to live as long as the process anyways */
c14fb378 408 d = static_cast<char*>(xmalloc(strlen(argv[j]) + 1));
26ac0430 409 strcpy(d, argv[j]);
75aa769b 410 debug("Adding domain-controller %s\n", d);
26ac0430
AJ
411 if (NULL == (c = strchr(d, '\\')) && NULL == (c = strchr(d, '/'))) {
412 fprintf(stderr, "Couldn't grok domain-controller %s\n", d);
413 free(d);
414 continue;
415 }
416 /* more than one delimiter is not allowed */
417 if (NULL != strchr(c + 1, '\\') || NULL != strchr(c + 1, '/')) {
418 fprintf(stderr, "Broken domain-controller %s\n", d);
419 free(d);
420 continue;
421 }
f207fe64
FC
422 *c= '\0';
423 ++c;
c14fb378 424 new_dc = static_cast<dc *>(xmalloc(sizeof(dc)));
26ac0430
AJ
425 if (!new_dc) {
426 fprintf(stderr, "Malloc error while parsing DC options\n");
427 free(d);
428 continue;
429 }
430 /* capitalize */
431 uc(c);
432 uc(d);
755494da 433 ++numcontrollers;
26ac0430
AJ
434 new_dc->domain = d;
435 new_dc->controller = c;
436 new_dc->dead = 0;
f53969cc 437 if (controllers == NULL) { /* first controller */
26ac0430
AJ
438 controllers = new_dc;
439 last_dc = new_dc;
440 } else {
f53969cc 441 last_dc->next = new_dc; /* can't be null */
26ac0430
AJ
442 last_dc = new_dc;
443 }
94439e4e 444 }
445 if (numcontrollers == 0) {
26ac0430
AJ
446 fprintf(stderr, "You must specify at least one domain-controller!\n");
447 usage();
448 exit(1);
94439e4e 449 }
f53969cc 450 last_dc->next = controllers; /* close the queue, now it's circular */
94439e4e 451}
452
a19a836d
AJ
453/**
454 * tries connecting to the domain controllers in the "controllers" ring,
94439e4e 455 * with failover if the adequate option is specified.
456 */
457const char *
458obtain_challenge()
459{
460 int j = 0;
60d096f4 461 const char *ch = NULL;
755494da 462 for (j = 0; j < numcontrollers; ++j) {
75aa769b 463 debug("obtain_challenge: selecting %s\\%s (attempt #%d)\n",
632d8053 464 current_dc->domain, current_dc->controller, j + 1);
26ac0430
AJ
465 if (current_dc->dead != 0) {
466 if (time(NULL) - current_dc->dead >= DEAD_DC_RETRY_INTERVAL) {
467 /* mark helper as retry-worthy if it's so. */
75aa769b 468 debug("Reviving DC\n");
26ac0430 469 current_dc->dead = 0;
f53969cc 470 } else { /* skip it */
75aa769b 471 debug("Skipping it\n");
26ac0430
AJ
472 continue;
473 }
474 }
475 /* else branch. Here we KNOW that the DC is fine */
75aa769b 476 debug("attempting challenge retrieval\n");
26ac0430 477 ch = make_challenge(current_dc->domain, current_dc->controller);
75aa769b 478 debug("make_challenge retuned %p\n", ch);
26ac0430 479 if (ch) {
75aa769b 480 debug("Got it\n");
f53969cc 481 return ch; /* All went OK, returning */
26ac0430
AJ
482 }
483 /* Huston, we've got a problem. Take this DC out of the loop */
75aa769b 484 debug("Marking DC as DEAD\n");
26ac0430
AJ
485 current_dc->dead = time(NULL);
486 /* Try with the next */
75aa769b 487 debug("moving on to next controller\n");
26ac0430 488 current_dc = current_dc->next;
94439e4e 489 }
5d146f7d 490 /* all DCs failed. */
94439e4e 491 return NULL;
492}
493
494void
495manage_request()
496{
497 ntlmhdr *fast_header;
1dcf61eb 498 char buf[NTLM_BLOB_BUFFER_SIZE];
8bdd0cec 499 char decoded[NTLM_BLOB_BUFFER_SIZE];
8bdd0cec 500 char *ch2, *cred = NULL;
94439e4e 501
1dcf61eb 502 if (fgets(buf, NTLM_BLOB_BUFFER_SIZE, stdin) == NULL) {
26ac0430
AJ
503 fprintf(stderr, "fgets() failed! dying..... errno=%d (%s)\n", errno,
504 strerror(errno));
f53969cc 505 exit(1); /* BIIG buffer */
6437ac71 506 }
75aa769b 507 debug("managing request\n");
f53969cc 508 ch2 = (char*)memchr(buf, '\n', NTLM_BLOB_BUFFER_SIZE); /* safer against overrun than strchr */
94439e4e 509 if (ch2) {
f53969cc 510 *ch2 = '\0'; /* terminate the string at newline. */
94439e4e 511 }
75aa769b 512 debug("ntlm authenticator. Got '%s' from Squid\n", buf);
94439e4e 513
f53969cc 514 if (memcmp(buf, "KK ", 3) == 0) { /* authenticate-request */
26ac0430 515 /* figure out what we got */
aadbbd7d
AJ
516 struct base64_decode_ctx ctx;
517 base64_decode_init(&ctx);
518 size_t dstLen = 0;
519 int decodedLen = 0;
520 if (!base64_decode_update(&ctx, &dstLen, reinterpret_cast<uint8_t*>(decoded), strlen(buf)-3, reinterpret_cast<const uint8_t*>(buf+3)) ||
521 !base64_decode_final(&ctx)) {
522 SEND("NA Packet format error, couldn't base64-decode");
523 return;
524 }
525 decodedLen = dstLen;
26ac0430 526
f53969cc 527 if ((size_t)decodedLen < sizeof(ntlmhdr)) { /* decoding failure, return error */
aadbbd7d 528 SEND("NA Packet format error, truncated packet header.");
26ac0430
AJ
529 return;
530 }
531 /* fast-track-decode request type. */
1dcf61eb 532 fast_header = (ntlmhdr *) decoded;
26ac0430
AJ
533
534 /* sanity-check: it IS a NTLMSSP packet, isn't it? */
1dcf61eb 535 if (ntlm_validate_packet(fast_header, NTLM_ANY) < 0) {
26ac0430
AJ
536 SEND("NA Broken authentication packet");
537 return;
538 }
539 switch (le32toh(fast_header->type)) {
540 case NTLM_NEGOTIATE:
541 SEND("NA Invalid negotiation request received");
542 return;
f53969cc 543 /* notreached */
26ac0430 544 case NTLM_CHALLENGE:
83ae34bc 545 SEND("NA Got a challenge. We refuse to have our authority disputed");
26ac0430 546 return;
f53969cc 547 /* notreached */
26ac0430
AJ
548 case NTLM_AUTHENTICATE:
549 /* check against the DC */
26ac0430
AJ
550 signal(SIGALRM, timeout_during_auth);
551 alarm(30);
8bdd0cec 552 cred = ntlm_check_auth((ntlm_authenticate *) decoded, decodedLen);
26ac0430
AJ
553 alarm(0);
554 signal(SIGALRM, SIG_DFL);
555 if (got_timeout != 0) {
556 fprintf(stderr, "ntlm-auth[%ld]: Timeout during authentication.\n", (long)getpid());
557 SEND("BH Timeout during authentication");
558 got_timeout = 0;
559 return;
560 }
561 if (cred == NULL) {
562 int smblib_err, smb_errorclass, smb_errorcode, nb_error;
f53969cc 563 if (ntlm_errno == NTLM_ERR_LOGON) { /* hackish */
26ac0430
AJ
564 SEND("NA Logon Failure");
565 return;
566 }
567 /* there was an error. We have two errno's to look at.
568 * libntlmssp's erno is insufficient, we'll have to look at
569 * the actual SMB library error codes, to acually figure
570 * out what's happening. The thing has braindamaged interfacess..*/
571 smblib_err = SMB_Get_Last_Error();
572 smb_errorclass = SMBlib_Error_Class(SMB_Get_Last_SMB_Err());
573 smb_errorcode = SMBlib_Error_Code(SMB_Get_Last_SMB_Err());
574 nb_error = RFCNB_Get_Last_Error();
75aa769b
AJ
575 debug("No creds. SMBlib error %d, SMB error class %d, SMB error code %d, NB error %d\n",
576 smblib_err, smb_errorclass, smb_errorcode, nb_error);
26ac0430
AJ
577 /* Should I use smblib_err? Actually it seems I can do as well
578 * without it.. */
f53969cc 579 if (nb_error != 0) { /* netbios-level error */
e490d2a3 580 SEND("BH NetBios error!");
26ac0430
AJ
581 fprintf(stderr, "NetBios error code %d (%s)\n", nb_error,
582 RFCNB_Error_Strings[abs(nb_error)]);
583 return;
584 }
585 switch (smb_errorclass) {
586 case SMBC_SUCCESS:
75aa769b 587 debug("Huh? Got a SMB success code but could check auth..");
26ac0430 588 SEND("NA Authentication failed");
26ac0430
AJ
589 return;
590 case SMBC_ERRDOS:
591 /*this is the most important one for errors */
75aa769b 592 debug("DOS error\n");
26ac0430 593 switch (smb_errorcode) {
f53969cc
SM
594 /* two categories matter to us: those which could be
595 * server errors, and those which are auth errors */
596 case SMBD_noaccess: /* 5 */
26ac0430
AJ
597 SEND("NA Access denied");
598 return;
599 case SMBD_badformat:
600 SEND("NA bad format in authentication packet");
601 return;
602 case SMBD_badaccess:
603 SEND("NA Bad access request");
604 return;
605 case SMBD_baddata:
606 SEND("NA Bad Data");
607 return;
608 default:
e490d2a3 609 SEND("BH DOS Error");
26ac0430
AJ
610 return;
611 }
f53969cc 612 case SMBC_ERRSRV: /* server errors */
75aa769b 613 debug("Server error");
26ac0430 614 switch (smb_errorcode) {
f53969cc 615 /* mostly same as above */
26ac0430
AJ
616 case SMBV_badpw:
617 SEND("NA Bad password");
618 return;
619 case SMBV_access:
620 SEND("NA Server access error");
621 return;
622 default:
e490d2a3 623 SEND("BH Server Error");
26ac0430
AJ
624 return;
625 }
f53969cc 626 case SMBC_ERRHRD: /* hardware errors don't really matter */
e490d2a3 627 SEND("BH Domain Controller Hardware error");
26ac0430
AJ
628 return;
629 case SMBC_ERRCMD:
e490d2a3 630 SEND("BH Domain Controller Command Error");
26ac0430
AJ
631 return;
632 }
83ae34bc
AJ
633 SEND("BH unknown internal error.");
634 return;
26ac0430 635 }
83ae34bc 636
f53969cc 637 lc(cred); /* let's lowercase them for our convenience */
26ac0430
AJ
638 SEND2("AF %s", cred);
639 return;
640 default:
641 SEND("BH unknown authentication packet type");
642 return;
643 }
7c572fcd 644 /* notreached */
26ac0430 645 return;
94439e4e 646 }
f53969cc 647 if (memcmp(buf, "YR", 2) == 0) { /* refresh-request */
26ac0430 648 dc_disconnect();
78a474a9 649 const char *ch = obtain_challenge();
26ac0430
AJ
650 /* Robert says we can afford to wait forever. I'll trust him on this
651 * one */
652 while (ch == NULL) {
653 sleep(30);
654 ch = obtain_challenge();
655 }
656 SEND2("TT %s", ch);
657 return;
94439e4e 658 }
659 SEND("BH Helper detected protocol error");
660 return;
26ac0430 661 /********* END ********/
94439e4e 662
94439e4e 663}
664
665int
666main(int argc, char *argv[])
667{
a20277e1 668 debug("%s " VERSION " " SQUID_BUILD_INFO " starting up...\n", argv[0]);
94439e4e 669
a88de9ce 670 my_program_name = argv[0];
94439e4e 671 process_options(argc, argv);
672
75aa769b 673 debug("options processed OK\n");
94439e4e 674
675 /* initialize FDescs */
676 setbuf(stdout, NULL);
677 setbuf(stderr, NULL);
678
679 /* select the first domain controller we're going to use */
680 current_dc = controllers;
681 if (load_balance != 0 && numcontrollers > 1) {
26ac0430
AJ
682 int n;
683 pid_t pid = getpid();
684 n = pid % numcontrollers;
75aa769b 685 debug("load balancing. Selected controller #%d\n", n);
26ac0430
AJ
686 while (n > 0) {
687 current_dc = current_dc->next;
755494da 688 --n;
26ac0430 689 }
94439e4e 690 }
691 while (1) {
26ac0430 692 manage_request();
94439e4e 693 }
7c572fcd 694 /* notreached */
94439e4e 695 return 0;
696}
f53969cc 697