blob: 0d1b3e1b66ac0fef6dc4e54503b77c5cae39f0ba [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 2010, Mandy Wu, <mandy.wu@intel.com>
9 * Copyright (C) 2011 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
10 *
11 * This software is licensed as described in the file COPYING, which
12 * you should have received as part of this distribution. The terms
13 * are also available at https://curl.haxx.se/docs/copyright.html.
14 *
15 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
16 * copies of the Software, and permit persons to whom the Software is
17 * furnished to do so, under the terms of the COPYING file.
18 *
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
21 *
22 ***************************************************************************/
23#include "server_setup.h"
24
25/*
26 * This is a fake ntlm_auth, which is used for testing NTLM single-sign-on.
27 * When DEBUGBUILD is defined, libcurl invoke this tool instead of real winbind
28 * daemon helper /usr/bin/ntlm_auth. This tool will accept commands and
29 * responses with a pre-written string saved in test case test2005.
30 */
31
32#define ENABLE_CURLX_PRINTF
33#include "curlx.h" /* from the private lib dir */
34#include "getpart.h"
35#include "util.h"
36
37/* include memdebug.h last */
38#include "memdebug.h"
39
40#define LOGFILE "log/fake_ntlm%d.log"
41
42const char *serverlogfile;
43
44/*
45 * Returns an allocated buffer with printable representation of input
46 * buffer contents or returns NULL on out of memory condition.
47 */
48static char *printable(char *inbuf, size_t inlength)
49{
50 char *outbuf;
51 char *newbuf;
52 size_t newsize;
53 size_t outsize;
54 size_t outincr = 0;
55 size_t i, o = 0;
56
57#define HEX_FMT_STR "[0x%02X]"
58#define HEX_STR_LEN 6
59#define NOTHING_STR "[NOTHING]"
60#define NOTHING_LEN 9
61
62 if(!inlength)
63 inlength = strlen(inbuf);
64
65 if(inlength) {
66 outincr = ((inlength/2) < (HEX_STR_LEN+1)) ? HEX_STR_LEN+1 : inlength/2;
67 outsize = inlength + outincr;
68 }
69 else
70 outsize = NOTHING_LEN + 1;
71
72 outbuf = malloc(outsize);
73 if(!outbuf)
74 return NULL;
75
76 if(!inlength) {
77 snprintf(&outbuf[0], outsize, "%s", NOTHING_STR);
78 return outbuf;
79 }
80
81 for(i=0; i<inlength; i++) {
82
83 if(o > outsize - (HEX_STR_LEN + 1)) {
84 newsize = outsize + outincr;
85 newbuf = realloc(outbuf, newsize);
86 if(!newbuf) {
87 free(outbuf);
88 return NULL;
89 }
90 outbuf = newbuf;
91 outsize = newsize;
92 }
93
94 if((inbuf[i] > 0x20) && (inbuf[i] < 0x7F)) {
95 outbuf[o] = inbuf[i];
96 o++;
97 }
98 else {
99 snprintf(&outbuf[o], outsize - o, HEX_FMT_STR, inbuf[i]);
100 o += HEX_STR_LEN;
101 }
102
103 }
104 outbuf[o] = '\0';
105
106 return outbuf;
107}
108
109int main(int argc, char *argv[])
110{
111 char buf[1024];
112 char logfilename[256];
113 FILE *stream;
114 char *filename;
115 int error;
116 char *type1_input = NULL, *type3_input = NULL;
117 char *type1_output = NULL, *type3_output = NULL;
118 size_t size = 0;
119 long testnum;
120 const char *env;
121 int arg = 1;
122 const char *helper_user = "unknown";
123 const char *helper_proto = "unknown";
124 const char *helper_domain = "unknown";
125 bool use_cached_creds = FALSE;
126 char *msgbuf;
127
128 buf[0] = '\0';
129
130 while(argc > arg) {
131 if(!strcmp("--use-cached-creds", argv[arg])) {
132 use_cached_creds = TRUE;
133 arg++;
134 }
135 else if(!strcmp("--helper-protocol", argv[arg])) {
136 arg++;
137 if(argc > arg)
138 helper_proto = argv[arg++];
139 }
140 else if(!strcmp("--username", argv[arg])) {
141 arg++;
142 if(argc > arg)
143 helper_user = argv[arg++];
144 }
145 else if(!strcmp("--domain", argv[arg])) {
146 arg++;
147 if(argc > arg)
148 helper_domain = argv[arg++];
149 }
150 else {
151 puts("Usage: fake_ntlm [option]\n"
152 " --use-cached-creds\n"
153 " --helper-protocol [protocol]\n"
154 " --username [username]\n"
155 " --domain [domain]");
156 exit(1);
157 }
158 }
159
160 env = getenv("CURL_NTLM_AUTH_TESTNUM");
161 if(env) {
162 char *endptr;
163 long lnum = strtol(env, &endptr, 10);
164 if((endptr != env + strlen(env)) || (lnum < 1L)) {
165 fprintf(stderr, "Test number not valid in CURL_NTLM_AUTH_TESTNUM");
166 exit(1);
167 }
168 testnum = lnum;
169 }
170 else {
171 fprintf(stderr, "Test number not specified in CURL_NTLM_AUTH_TESTNUM");
172 exit(1);
173 }
174
175 /* logmsg cannot be used until this file name is set */
176 snprintf(logfilename, sizeof(logfilename), LOGFILE, testnum);
177 serverlogfile = logfilename;
178
179 logmsg("fake_ntlm (user: %s) (proto: %s) (domain: %s) (cached creds: %s)",
180 helper_user, helper_proto, helper_domain,
181 (use_cached_creds) ? "yes" : "no");
182
183 env = getenv("CURL_NTLM_AUTH_SRCDIR");
184 if(env) {
185 path = env;
186 }
187
188 filename = test2file(testnum);
189 stream=fopen(filename, "rb");
190 if(!stream) {
191 error = errno;
192 logmsg("fopen() failed with error: %d %s", error, strerror(error));
193 logmsg("Error opening file: %s", filename);
194 logmsg("Couldn't open test file %ld", testnum);
195 exit(1);
196 }
197 else {
198 /* get the ntlm_auth input/output */
199 error = getpart(&type1_input, &size, "ntlm_auth_type1", "input", stream);
200 fclose(stream);
201 if(error || size == 0) {
202 logmsg("getpart() type 1 input failed with error: %d", error);
203 exit(1);
204 }
205 }
206
207 stream=fopen(filename, "rb");
208 if(!stream) {
209 error = errno;
210 logmsg("fopen() failed with error: %d %s", error, strerror(error));
211 logmsg("Error opening file: %s", filename);
212 logmsg("Couldn't open test file %ld", testnum);
213 exit(1);
214 }
215 else {
216 size = 0;
217 error = getpart(&type3_input, &size, "ntlm_auth_type3", "input", stream);
218 fclose(stream);
219 if(error || size == 0) {
220 logmsg("getpart() type 3 input failed with error: %d", error);
221 exit(1);
222 }
223 }
224
225 while(fgets(buf, sizeof(buf), stdin)) {
226 if(strcmp(buf, type1_input) == 0) {
227 stream=fopen(filename, "rb");
228 if(!stream) {
229 error = errno;
230 logmsg("fopen() failed with error: %d %s", error, strerror(error));
231 logmsg("Error opening file: %s", filename);
232 logmsg("Couldn't open test file %ld", testnum);
233 exit(1);
234 }
235 else {
236 size = 0;
237 error = getpart(&type1_output, &size, "ntlm_auth_type1", "output",
238 stream);
239 fclose(stream);
240 if(error || size == 0) {
241 logmsg("getpart() type 1 output failed with error: %d", error);
242 exit(1);
243 }
244 }
245 printf("%s", type1_output);
246 fflush(stdout);
247 }
248 else if(strncmp(buf, type3_input, strlen(type3_input)) == 0) {
249 stream=fopen(filename, "rb");
250 if(!stream) {
251 error = errno;
252 logmsg("fopen() failed with error: %d %s", error, strerror(error));
253 logmsg("Error opening file: %s", filename);
254 logmsg("Couldn't open test file %ld", testnum);
255 exit(1);
256 }
257 else {
258 size = 0;
259 error = getpart(&type3_output, &size, "ntlm_auth_type3", "output",
260 stream);
261 fclose(stream);
262 if(error || size == 0) {
263 logmsg("getpart() type 3 output failed with error: %d", error);
264 exit(1);
265 }
266 }
267 printf("%s", type3_output);
268 fflush(stdout);
269 }
270 else {
271 printf("Unknown request\n");
272 msgbuf = printable(buf, 0);
273 if(msgbuf) {
274 logmsg("invalid input: '%s'\n", msgbuf);
275 free(msgbuf);
276 }
277 else
278 logmsg("OOM formatting invalid input: '%s'\n", buf);
279 exit(1);
280 }
281 }
282 logmsg("Exit");
283 return 1;
284}