Go to the documentation of this file.
96 int fail_debug_enabled = 0;
104 WCHAR wszUserName[UNLEN+1];
105 WCHAR wszGroup[GNLEN+1];
107 LPLOCALGROUP_USERS_INFO_0 pBuf =
nullptr;
108 LPLOCALGROUP_USERS_INFO_0 pTmpBuf;
110 DWORD dwFlags = LG_INCLUDE_INDIRECT;
111 DWORD dwPrefMaxLen = -1;
112 DWORD dwEntriesRead = 0;
113 DWORD dwTotalEntries = 0;
114 NET_API_STATUS nStatus;
116 DWORD dwTotalCount = 0;
120 MultiByteToWideChar(CP_ACP, 0, UserName,
121 strlen(UserName) + 1, wszUserName,
122 sizeof(wszUserName) /
sizeof(wszUserName[0]));
123 MultiByteToWideChar(CP_ACP, 0, Group,
124 strlen(Group) + 1, wszGroup,
sizeof(wszGroup) /
sizeof(wszGroup[0]));
134 nStatus = NetUserGetLocalGroups(
nullptr,
138 (LPBYTE *) & pBuf, dwPrefMaxLen, &dwEntriesRead, &dwTotalEntries);
142 if (nStatus == NERR_Success) {
143 if ((pTmpBuf = pBuf) !=
NULL) {
144 for (i = 0; i < dwEntriesRead; ++i) {
145 if (pTmpBuf ==
NULL) {
149 if (wcscmp(pTmpBuf->lgrui0_name, wszGroup) == 0) {
163 NetApiBufferFree(pBuf);
178 if (!NTLM_LocalCall) {
187 if (domain[0] ==
'\0') {
188 debug(
"No domain supplied. Returning no-auth\n");
191 if (user[0] ==
'\0') {
192 debug(
"No username supplied. Returning no-auth\n");
195 debug(
"checking domain: '%s', user: '%s'\n", domain, user);
198 debug(
"checking local user\n");
201 snprintf(
credentials, DNLEN+UNLEN+2,
"%s\\%s", domain, user);
203 const auto rv = SSP_ValidateNTLMCredentials(auth, auth_length,
credentials);
205 debug(
"Login attempt had result %d\n", rv);
232 fail_debug_enabled =1;
250 "Usage: %s [-d] [-v] [-A|D LocalUserGroup] [-h]\n"
251 " -d enable debugging.\n"
252 " -v enable verbose NTLM packet debugging.\n"
253 " -A specify a Windows Local Group name allowed to authenticate\n"
254 " -D specify a Windows Local Group name not allowed to authenticate\n"
255 " -h this message\n\n",
262 int opt, had_error = 0;
265 while (-1 != (opt =
getopt(argc, argv,
"hdvA:D:"))) {
291 fprintf(stderr,
"unknown option: -%c. Exiting\n", opt);
307 SEND_BH(
"message=\"base64 decode failed\"");
308 fprintf(stderr,
"ERROR: base64 decoding failed for: '%s'\n", buf);
320 size_t decodedLen = 0;
321 char helper_command[3];
325 char domain[DNLEN+1];
330 if (memcpy(local_nego.
hdr.
signature,
"NTLMSSP", 8) != 0) {
341 if (fgets(buf,
sizeof(buf), stdin) ==
NULL)
344 char *c =
static_cast<char*
>(memchr(buf,
'\n',
sizeof(buf)));
347 helperfail(
"message=\"illegal request received\"");
348 fprintf(stderr,
"Illegal request received: '%s'\n", buf);
353 fprintf(stderr,
"No newline in '%s'\n", buf);
362 helper_command[0] = buf[0];
363 helper_command[1] = buf[1];
364 helper_command[2] =
'\0';
365 debug(
"Got '%s' from Squid with data:\n", helper_command);
366 hex_dump(
reinterpret_cast<unsigned char*
>(decoded), decodedLen);
368 debug(
"Got '%s' from Squid\n", buf);
369 if (memcmp(buf,
"YR", 2) == 0) {
371 if (strlen(buf) > 3) {
372 if (!decodedLen && !
token_decode(&decodedLen, decoded, buf+3))
375 debug(
"Negotiate packet not supplied - self generated\n");
376 memcpy(decoded, &local_nego,
sizeof(local_nego));
377 decodedLen =
sizeof(local_nego);
379 if ((
size_t)decodedLen <
sizeof(
ntlmhdr)) {
380 SEND_ERR(
"message=\"Packet format error\"");
384 fast_header = (
struct _ntlmhdr *) decoded;
388 SEND_ERR(
"message=\"Broken authentication packet\"");
391 switch (fast_header->
type) {
394 debug(
"attempting SSPI challenge retrieval\n");
395 char *c = (
char *) SSP_MakeChallenge((
ntlm_negotiate *) decoded, decodedLen);
401 debug(
"send 'TT' to squid with data:\n");
402 hex_dump(
reinterpret_cast<unsigned char*
>(decoded), decodedLen);
403 if (NTLM_LocalCall) {
404 debug(
"NTLM Local Call detected\n");
409 helperfail(
"message=\"can't obtain challenge\"");
415 SEND_ERR(
"message=\"Got a challenge. We refuse to have our authority disputed\"");
419 SEND_ERR(
"message=\"Got authentication request instead of negotiate request\"");
423 helperfail(
"message=\"unknown refresh-request packet type\"");
428 if (memcmp(buf,
"KK ", 3) == 0) {
434 if (!decodedLen && !
token_decode(&decodedLen, decoded, buf+3))
437 if ((
size_t)decodedLen <
sizeof(
ntlmhdr)) {
438 SEND_ERR(
"message=\"Packet format error\"");
442 fast_header = (
struct _ntlmhdr *) decoded;
446 SEND_ERR(
"message=\"Broken authentication packet\"");
449 switch (fast_header->
type) {
451 SEND_ERR(
"message=\"Invalid negotiation request received\"");
455 SEND_ERR(
"message=\"Got a challenge. We refuse to have our authority disputed\"");
464 fail_debug_enabled =1;
470 SEND_ERR(
"message=\"Incorrect Group Membership\"");
473 SEND_ERR(
"message=\"Incorrect Request Format\"");
477 FORMAT_MESSAGE_ALLOCATE_BUFFER |
478 FORMAT_MESSAGE_FROM_SYSTEM |
479 FORMAT_MESSAGE_IGNORE_INSERTS,
482 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
483 (LPTSTR) &ErrorMessage,
486 if (ErrorMessage[strlen(ErrorMessage) - 1] ==
'\n')
487 ErrorMessage[strlen(ErrorMessage) - 1] =
'\0';
488 if (ErrorMessage[strlen(ErrorMessage) - 1] ==
'\r')
489 ErrorMessage[strlen(ErrorMessage) - 1] =
'\0';
491 LocalFree(ErrorMessage);
494 SEND_ERR(
"message=\"Unknown Error\"");
501 fprintf(stdout,
"OK user=\"%s\\%s\"", domain, user);
505 helperfail(
"message=\"unknown authentication packet type\"");
510 helperfail(
"message=\"illegal request received\"");
511 fprintf(stderr,
"Illegal request received: '%s'\n", buf);
514 helperfail(
"message=\"detected protocol error\"");
529 fprintf(stderr,
"FATAL, can't initialize SSPI, exiting.\n");
532 debug(
"SSPI initialized OK\n");
537 setbuf(stdout,
nullptr);
538 setbuf(stderr,
nullptr);
static int manage_request()
static NtlmError ntlm_check_auth(ntlm_authenticate *auth, char *user, char *domain, int auth_length)
void debug(const char *format,...)
static int have_challenge
void base64_decode_init(struct base64_decode_ctx *ctx)
#define NTLM_NEGOTIATE_ALWAYS_SIGN
HMODULE LoadSecurityDll(int mode, const char *SSP_Package)
int NTLM_packet_debug_enabled
int main(int argc, char *argv[])
static int Valid_Group(char *UserName, char *Group)
int getopt(int nargc, char *const *nargv, const char *ostr)
#define NTLM_NEGOTIATE_ASCII
#define NTLM_NEGOTIATE_USE_NTLM
int base64_decode_final(struct base64_decode_ctx *ctx)
#define NTLM_AUTHENTICATE
static void helperfail(const char *reason)
void UnloadSecurityDll(void)
int base64_decode_update(struct base64_decode_ctx *ctx, size_t *dst_length, uint8_t *dst, size_t src_length, const char *src)
#define NTLM_NEGOTIATE_USE_LM
#define NTLM_PACKAGE_NAME
static void process_options(int argc, char *argv[])
#define HELPER_INPUT_BUFFER
static char credentials[MAX_USERNAME_LEN+MAX_DOMAIN_LEN+2]
NtlmError ntlm_validate_packet(const ntlmhdr *hdr, const int32_t type)
NtlmError ntlm_unpack_auth(const ntlm_authenticate *auth, char *user, char *domain, const int32_t size)
static bool token_decode(size_t *decodedLen, uint8_t decoded[], const char *buf)
void hex_dump(unsigned char *data, int size)