]> git.ipfire.org Git - thirdparty/squid.git/blob - helpers/negotiate_auth/SSPI/negotiate_sspi_auth.cc
SourceFormat Enforcement
[thirdparty/squid.git] / helpers / negotiate_auth / SSPI / negotiate_sspi_auth.cc
1 /*
2 * Copyright (C) 1996-2014 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 * negotiate_sspi_auth: helper for Negotiate Authentication for Squid Cache
11 *
12 * (C)2005 Guido Serassio - Acme Consulting S.r.l.
13 *
14 * Authors:
15 * Guido Serassio <guido.serassio@acmeconsulting.it>
16 * Acme Consulting S.r.l., Italy <http://www.acmeconsulting.it>
17 *
18 * With contributions from others mentioned in the change history section
19 * below.
20 *
21 * Based on previous work of Francesco Chemolli and Robert Collins.
22 *
23 * Dependencies: Windows 2000 and later.
24 *
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
29 *
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
38 *
39 * History:
40 *
41 * Version 1.0
42 * 29-10-2005 Guido Serassio
43 * First release.
44 */
45
46 #include "squid.h"
47 #include "base64.h"
48 #include "helpers/defines.h"
49 #include "ntlmauth/ntlmauth.h"
50 #include "ntlmauth/support_bits.cci"
51 #include "sspwin32.h"
52 #include "util.h"
53
54 #include <windows.h>
55 #include <sspi.h>
56 #include <security.h>
57 #if HAVE_GETOPT_H
58 #include <getopt.h>
59 #endif
60 #if HAVE_CTYPE_H
61 #include <ctype.h>
62 #endif
63
64 int Negotiate_packet_debug_enabled = 0;
65 static int have_serverblob;
66
67 /* A couple of harmless helper macros */
68 #define SEND(X) debug("sending '%s' to squid\n",X); printf(X "\n");
69 #ifdef __GNUC__
70 #define SEND2(X,Y...) debug("sending '" X "' to squid\n",Y); printf(X "\n",Y);
71 #define SEND3(X,Y...) debug("sending '" X "' to squid\n",Y); printf(X "\n",Y);
72 #else
73 /* no gcc, no debugging. varargs macros are a gcc extension */
74 #define SEND2(X,Y) debug("sending '" X "' to squid\n",Y); printf(X "\n",Y);
75 #define SEND3(X,Y,Z) debug("sending '" X "' to squid\n",Y,Z); printf(X "\n",Y,Z);
76 #endif
77
78 char *negotiate_check_auth(SSP_blobP auth, int auth_length);
79
80 /*
81 * options:
82 * -d enable debugging.
83 * -v enable verbose Negotiate packet debugging.
84 */
85 char *my_program_name = NULL;
86
87 void
88 usage()
89 {
90 fprintf(stderr,
91 "Usage: %s [-d] [-v] [-h]\n"
92 " -d enable debugging.\n"
93 " -v enable verbose Negotiate packet debugging.\n"
94 " -h this message\n\n",
95 my_program_name);
96 }
97
98 void
99 process_options(int argc, char *argv[])
100 {
101 int opt, had_error = 0;
102
103 opterr = 0;
104 while (-1 != (opt = getopt(argc, argv, "hdv"))) {
105 switch (opt) {
106 case 'd':
107 debug_enabled = 1;
108 break;
109 case 'v':
110 debug_enabled = 1;
111 Negotiate_packet_debug_enabled = 1;
112 break;
113 case 'h':
114 usage();
115 exit(0);
116 case '?':
117 opt = optopt;
118 /* fall thru to default */
119 default:
120 fprintf(stderr, "ERROR: unknown option: -%c. Exiting\n", opt);
121 usage();
122 had_error = 1;
123 }
124 }
125 if (had_error)
126 exit(1);
127 }
128
129 int
130 manage_request()
131 {
132 char buf[HELPER_INPUT_BUFFER];
133 char decoded[HELPER_INPUT_BUFFER];
134 int decodedLen;
135 char helper_command[3];
136 char *c;
137 int status;
138 int oversized = 0;
139 char *ErrorMessage;
140 static char cred[SSP_MAX_CRED_LEN + 1];
141 BOOL Done = FALSE;
142
143 try_again:
144 if (fgets(buf, HELPER_INPUT_BUFFER, stdin))
145 return 0;
146
147 c = static_cast<char*>(memchr(buf, '\n', HELPER_INPUT_BUFFER));
148 if (c) {
149 if (oversized) {
150 SEND("BH illegal request received");
151 fprintf(stderr, "ERROR: Illegal request received: '%s'\n", buf);
152 return 1;
153 }
154 *c = '\0';
155 } else {
156 fprintf(stderr, "No newline in '%s'\n", buf);
157 oversized = 1;
158 goto try_again;
159 }
160
161 if ((strlen(buf) > 3) && Negotiate_packet_debug_enabled) {
162 decodedLen = base64_decode(decoded, sizeof(decoded), buf+3);
163 strncpy(helper_command, buf, 2);
164 debug("Got '%s' from Squid with data:\n", helper_command);
165 hex_dump(reinterpret_cast<unsigned char*>(decoded), decodedLen);
166 } else
167 debug("Got '%s' from Squid\n", buf);
168
169 if (memcmp(buf, "YR ", 3) == 0) { /* refresh-request */
170 /* figure out what we got */
171 decodedLen = base64_decode(decoded, sizeof(decoded), buf + 3);
172 if ((size_t)decodedLen < sizeof(ntlmhdr)) { /* decoding failure, return error */
173 SEND("NA * Packet format error, couldn't base64-decode");
174 return 1;
175 }
176 /* Obtain server blob against SSPI */
177 c = (char *) SSP_MakeNegotiateBlob(decoded, decodedLen, &Done, &status, cred);
178
179 if (status == SSP_OK) {
180 if (Done) {
181 lc(cred); /* let's lowercase them for our convenience */
182 have_serverblob = 0;
183 Done = FALSE;
184 if (Negotiate_packet_debug_enabled) {
185 decodedLen = base64_decode(decoded, sizeof(decoded), c);
186 debug("sending 'AF' %s to squid with data:\n", cred);
187 if (c != NULL)
188 hex_dump(reinterpret_cast<unsigned char*>(decoded), decodedLen);
189 else
190 fprintf(stderr, "No data available.\n");
191 printf("AF %s %s\n", c, cred);
192 } else
193 SEND3("AF %s %s", c, cred);
194 } else {
195 if (Negotiate_packet_debug_enabled) {
196 decodedLen = base64_decode(decoded, sizeof(decoded), c);
197 debug("sending 'TT' to squid with data:\n");
198 hex_dump(reinterpret_cast<unsigned char*>(decoded), decodedLen);
199 printf("TT %s\n", c);
200 } else {
201 SEND2("TT %s", c);
202 }
203 have_serverblob = 1;
204 }
205 } else
206 SEND("BH can't obtain server blob");
207 return 1;
208 }
209 if (memcmp(buf, "KK ", 3) == 0) { /* authenticate-request */
210 if (!have_serverblob) {
211 SEND("BH invalid server blob");
212 return 1;
213 }
214 /* figure out what we got */
215 decodedLen = base64_decode(decoded, sizeof(decoded), buf+3);
216 if ((size_t)decodedLen < sizeof(ntlmhdr)) { /* decoding failure, return error */
217 SEND("NA * Packet format error, couldn't base64-decode");
218 return 1;
219 }
220 /* check against SSPI */
221 c = (char *) SSP_ValidateNegotiateCredentials(decoded, decodedLen, &Done, &status, cred);
222
223 if (status == SSP_ERROR) {
224 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
225 FORMAT_MESSAGE_IGNORE_INSERTS,
226 NULL,
227 GetLastError(),
228 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
229 (LPTSTR) & ErrorMessage,
230 0,
231 NULL);
232 if (ErrorMessage[strlen(ErrorMessage) - 1] == '\n')
233 ErrorMessage[strlen(ErrorMessage) - 1] = '\0';
234 if (ErrorMessage[strlen(ErrorMessage) - 1] == '\r')
235 ErrorMessage[strlen(ErrorMessage) - 1] = '\0';
236 SEND2("NA * %s", ErrorMessage);
237 LocalFree(ErrorMessage);
238 return 1;
239 }
240 if (Done) {
241 lc(cred); /* let's lowercase them for our convenience */
242 have_serverblob = 0;
243 Done = FALSE;
244 if (Negotiate_packet_debug_enabled) {
245 decodedLen = base64_decode(decoded, sizeof(decoded), c);
246 debug("sending 'AF' %s to squid with data:\n", cred);
247 if (c != NULL)
248 hex_dump(reinterpret_cast<unsigned char*>(decoded), decodedLen);
249 else
250 fprintf(stderr, "No data available.\n");
251 printf("AF %s %s\n", c, cred);
252 } else {
253 SEND3("AF %s %s", c, cred);
254 }
255 return 1;
256 } else {
257 if (Negotiate_packet_debug_enabled) {
258 decodedLen = base64_decode(decoded, sizeof(decoded), c);
259 debug("sending 'TT' to squid with data:\n");
260 hex_dump(reinterpret_cast<unsigned char*>(decoded), decodedLen);
261 printf("TT %s\n", c);
262 } else
263 SEND2("TT %s", c);
264 return 1;
265 }
266
267 } else { /* not an auth-request */
268 SEND("BH illegal request received");
269 fprintf(stderr, "Illegal request received: '%s'\n", buf);
270 return 1;
271 }
272 SEND("BH detected protocol error");
273 return 1;
274 /********* END ********/
275 }
276
277 int
278 main(int argc, char *argv[])
279 {
280 my_program_name = argv[0];
281
282 process_options(argc, argv);
283
284 debug("%s build " __DATE__ ", " __TIME__ " starting up...\n", my_program_name);
285
286 if (LoadSecurityDll(SSP_NTLM, NEGOTIATE_PACKAGE_NAME) == NULL) {
287 fprintf(stderr, "FATAL: %s: can't initialize SSPI, exiting.\n", argv[0]);
288 exit(1);
289 }
290 debug("SSPI initialized OK\n");
291
292 atexit(UnloadSecurityDll);
293
294 /* initialize FDescs */
295 setbuf(stdout, NULL);
296 setbuf(stderr, NULL);
297
298 while (manage_request()) {
299 /* everything is done within manage_request */
300 }
301 exit(0);
302 }
303