Squid Web Cache master
Loading...
Searching...
No Matches
ntlm_fake_auth.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2026 The Squid Software Foundation and contributors
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
9/*
10 * AUTHOR: Andrew Doran <ad@interlude.eu.org>
11 * AUTHOR: Robert Collins <rbtcollins@hotmail.com>
12 * AUTHOR: Guido Serassio <guido.serassio@acmeconsulting.it>
13 */
14
15/*
16 * Example ntlm authentication program for Squid, based on the
17 * original proxy_auth code from client_side.c, written by
18 * Jon Thackray <jrmt@uk.gdscorp.com>. Initial ntlm code by
19 * Andrew Doran <ad@interlude.eu.org>.
20 *
21 * This code gets the username and returns it. No validation is done.
22 * and by the way: it is a complete patch-up. Use the "real thing" NTLMSSP
23 * if you can.
24 *
25 * Revised by Guido Serassio: <guido.serassio@acmeconsulting.it>
26 *
27 * - Added negotiation of UNICODE char support
28 * - More detailed debugging info
29 *
30 */
31
32/* undefine this to have strict protocol adherence. You don't really need
33 * that though */
34#define IGNORANCE_IS_BLISS
35
36#include "squid.h"
37#include "base64.h"
39#include "ntlmauth/ntlmauth.h"
41
42#include <cctype>
43#include <chrono>
44#include <cstring>
45#if HAVE_CRYPT_H
46#include <crypt.h>
47#endif
48#if HAVE_PWD_H
49#include <pwd.h>
50#endif
51#if HAVE_GETOPT_H
52#include <getopt.h>
53#endif
54#include <thread>
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#define SEND4(X,Y...) {debug("sending '" X "' to squid\n",Y); printf(X "\n",Y);}
62#else
63/* no gcc, no debugging. varargs macros are a gcc extension */
64#define SEND2(X,Y) {debug("sending '" X "' to squid\n",Y); printf(X "\n",Y);}
65#define SEND3(X,Y,Z) {debug("sending '" X "' to squid\n",Y,Z); printf(X "\n",Y,Z);}
66#define SEND4(X,Y,Z,W) {debug("sending '" X "' to squid\n",Y,Z,W); printf(X "\n",Y,Z,W);}
67#endif
68
69const char *authenticate_ntlm_domain = "WORKGROUP";
72unsigned int response_delay = 0;
73
74/*
75 * options:
76 * -d enable debugging.
77 * -v enable verbose NTLM packet debugging.
78 * -l if specified, changes behavior on failures to last-ditch.
79 */
80char *my_program_name = nullptr;
81
82static void
83usage(void)
84{
85 fprintf(stderr,
86 "Usage: %s [-d] [-t N] [-v] [-h]\n"
87 " -d enable debugging.\n"
88 " -S strip domain from username.\n"
89 " -t timeout to delay responses (milliseconds).\n"
90 " -v enable verbose NTLM packet debugging.\n"
91 " -h this message\n\n",
93}
94
95static void
96process_options(int argc, char *argv[])
97{
98 int opt, had_error = 0;
99
100 opterr = 0;
101 while (-1 != (opt = getopt(argc, argv, "hdvSt:"))) {
102 switch (opt) {
103 case 'd':
104 debug_enabled = 1;
105 break;
106 case 'v':
107 debug_enabled = 1;
109 break;
110 case 'S':
112 break;
113 case 't':
114 if (!xstrtoui(optarg, nullptr, &response_delay, 0, 86400)) {
115 fprintf(stderr, "ERROR: invalid parameter value for -t '%s'", optarg);
116 usage();
117 had_error = 1;
118 }
119 break;
120 case 'h':
121 usage();
122 exit(EXIT_SUCCESS);
123 case '?':
124 opt = optopt;
125 [[fallthrough]];
126 default:
127 fprintf(stderr, "unknown option: -%c. Exiting\n", opt);
128 usage();
129 had_error = 1;
130 }
131 }
132 if (had_error)
133 exit(EXIT_FAILURE);
134}
135
136int
137main(int argc, char *argv[])
138{
139 char buf[HELPER_INPUT_BUFFER];
140 int buflen = 0;
141 uint8_t decodedBuf[HELPER_INPUT_BUFFER];
142 int decodedLen;
144 char *p;
145 int len;
146
147 setbuf(stdout, nullptr);
148 setbuf(stderr, nullptr);
149
150 my_program_name = argv[0];
151
152 process_options(argc, argv);
153
154 debug("%s " VERSION " " SQUID_BUILD_INFO " starting up...\n", my_program_name);
155
156 while (fgets(buf, HELPER_INPUT_BUFFER, stdin) != nullptr) {
157 user[0] = '\0'; /*no user code */
158 domain[0] = '\0'; /*no domain code */
159
160 if ((p = strchr(buf, '\n')) != nullptr)
161 *p = '\0'; /* strip \n */
162 buflen = strlen(buf); /* keep this so we only scan the buffer for \0 once per loop */
163 ntlmhdr *packet;
164 struct base64_decode_ctx ctx;
165 base64_decode_init(&ctx);
166 size_t dstLen = 0;
167 if (buflen > 3 &&
168 base64_decode_update(&ctx, &dstLen, decodedBuf, buflen-3, buf+3) &&
169 base64_decode_final(&ctx)) {
170 decodedLen = dstLen;
171 packet = (ntlmhdr*)decodedBuf;
172 } else {
173 packet = nullptr;
174 decodedLen = 0;
175 }
176
177 if (buflen > 3 && NTLM_packet_debug_enabled) {
178 char helper_command[3];
179 xstrncpy(helper_command, buf, sizeof(helper_command));
180 debug("Got '%s' from Squid with data:\n", helper_command);
181 hex_dump((unsigned char *)decodedBuf, decodedLen);
182 } else
183 debug("Got '%s' from Squid\n", buf);
184
185 if (response_delay > 0) {
186 std::this_thread::sleep_for(std::chrono::milliseconds(response_delay));
187 }
188
189 if (strncmp(buf, "YR", 2) == 0) {
190 char nonce[NTLM_NONCE_LEN];
191 ntlm_challenge chal;
192 ntlm_make_nonce(nonce);
193 if (buflen > 3 && packet) {
194 ntlm_negotiate *nego = (ntlm_negotiate *)packet;
196 } else {
198 }
199 // TODO: find out what this context means, and why only the fake auth helper contains it.
200 chal.context_high = htole32(0x003a<<16);
201
202 len = sizeof(chal) - sizeof(chal.payload) + le16toh(chal.target.maxlen);
203
204 struct base64_encode_ctx eCtx;
205 base64_encode_init(&eCtx);
206 char *data = static_cast<char *>(xcalloc(base64_encode_len(len), 1));
207 size_t blen = base64_encode_update(&eCtx, data, len, reinterpret_cast<const uint8_t *>(&chal));
208 blen += base64_encode_final(&eCtx, data+blen);
210 printf("TT %.*s\n", (int)blen, data);
211 debug("sending 'TT' to squid with data:\n");
212 hex_dump((unsigned char *)&chal, len);
213 } else
214 SEND3("TT %.*s", (int)blen, data);
215 safe_free(data);
216
217 } else if (strncmp(buf, "KK ", 3) == 0) {
218 if (!packet) {
219 SEND("BH received KK with no data! user=");
221 if (ntlm_unpack_auth((ntlm_authenticate *)packet, user, domain, decodedLen) == NtlmError::None) {
222 lc(user);
224 SEND2("AF %s", user);
225 } else {
226 SEND4("AF %s%s%s", domain, (*domain?"\\":""), user);
227 }
228 } else {
229 lc(user);
230 SEND4("NA invalid credentials, user=%s%s%s", domain, (*domain?"\\":""), user);
231 }
232 } else {
233 SEND("BH wrong packet type! user=");
234 }
235 }
236 }
237 return EXIT_SUCCESS;
238}
239
#define VERSION
Definition autoconf.h:1670
#define SQUID_BUILD_INFO
Definition autoconf.h:1413
void base64_encode_init(struct base64_encode_ctx *ctx)
Definition base64.cc:232
size_t base64_encode_update(struct base64_encode_ctx *ctx, char *dst, size_t length, const uint8_t *src)
Definition base64.cc:265
void base64_decode_init(struct base64_decode_ctx *ctx)
Definition base64.cc:54
size_t base64_encode_final(struct base64_encode_ctx *ctx, char *dst)
Definition base64.cc:308
#define base64_encode_len(length)
Definition base64.h:161
int base64_decode_update(struct base64_decode_ctx *ctx, size_t *dst_length, uint8_t *dst, size_t src_length, const char *src)
Definition base64.cc:129
int base64_decode_final(struct base64_decode_ctx *ctx)
Definition base64.cc:159
#define HELPER_INPUT_BUFFER
int debug_enabled
Definition debug.cc:13
void debug(const char *format,...)
Definition debug.cc:19
int optopt
Definition getopt.c:49
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition getopt.c:62
char * optarg
Definition getopt.c:51
int opterr
Definition getopt.c:47
int main()
#define SEND3(X, Y, Z)
int NTLM_packet_debug_enabled
#define SEND(X)
#define SEND4(X, Y, Z, W)
const char * authenticate_ntlm_domain
int strip_domain_enabled
static void process_options(int argc, char *argv[])
char * my_program_name
static void usage(void)
#define SEND2(X, Y)
unsigned int response_delay
NtlmError ntlm_unpack_auth(const ntlm_authenticate *auth, char *user, char *domain, const int32_t size)
Definition ntlmauth.cc:247
NtlmError ntlm_validate_packet(const ntlmhdr *hdr, const int32_t type)
Definition ntlmauth.cc:67
void ntlm_make_challenge(ntlm_challenge *ch, const char *domain, const char *, const char *challenge_nonce, const int challenge_nonce_len, const uint32_t flags)
Definition ntlmauth.cc:210
void ntlm_make_nonce(char *nonce)
Definition ntlmauth.cc:196
#define NTLM_MAX_FIELD_LENGTH
Definition ntlmauth.h:18
#define NTLM_AUTHENTICATE
Definition ntlmauth.h:71
#define NTLM_NEGOTIATE_ASCII
Definition ntlmauth.h:104
#define NTLM_NONCE_LEN
Definition ntlmauth.h:130
uint32_t context_high
Definition ntlmauth.h:144
char payload[256]
Definition ntlmauth.h:145
uint32_t flags
Definition ntlmauth.h:120
int16_t maxlen
Definition ntlmauth.h:50
void lc(char *string)
void hex_dump(unsigned char *data, int size)
#define le16toh(x)
#define htole32(x)
void * xcalloc(size_t n, size_t sz)
Definition xalloc.cc:71
#define safe_free(x)
Definition xalloc.h:73
char * xstrncpy(char *dst, const char *src, size_t n)
Definition xstring.cc:37
bool xstrtoui(const char *s, char **end, unsigned int *value, unsigned int min, unsigned int max)
Definition xstrto.cc:86