1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2010, Mandy Wu, <mandy.wu@intel.com>
9 * Copyright (C) 2011 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ***************************************************************************/
23 #include "server_setup.h"
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.
32 #define ENABLE_CURLX_PRINTF
33 #include "curlx.h" /* from the private lib dir */
37 /* include memdebug.h last */
40 #define LOGFILE "log/fake_ntlm%d.log"
42 const char *serverlogfile;
45 * Returns an allocated buffer with printable representation of input
46 * buffer contents or returns NULL on out of memory condition.
48 static char *printable(char *inbuf, size_t inlength)
57 #define HEX_FMT_STR "[0x%02X]"
59 #define NOTHING_STR "[NOTHING]"
63 inlength = strlen(inbuf);
66 outincr = ((inlength/2) < (HEX_STR_LEN + 1)) ?
67 HEX_STR_LEN + 1 : inlength/2;
68 outsize = inlength + outincr;
71 outsize = NOTHING_LEN + 1;
73 outbuf = malloc(outsize);
78 snprintf(&outbuf[0], outsize, "%s", NOTHING_STR);
82 for(i = 0; i<inlength; i++) {
84 if(o > outsize - (HEX_STR_LEN + 1)) {
85 newsize = outsize + outincr;
86 newbuf = realloc(outbuf, newsize);
95 if((inbuf[i] > 0x20) && (inbuf[i] < 0x7F)) {
100 snprintf(&outbuf[o], outsize - o, HEX_FMT_STR, inbuf[i]);
110 int main(int argc, char *argv[])
113 char logfilename[256];
117 char *type1_input = NULL, *type3_input = NULL;
118 char *type1_output = NULL, *type3_output = NULL;
123 const char *helper_user = "unknown";
124 const char *helper_proto = "unknown";
125 const char *helper_domain = "unknown";
126 bool use_cached_creds = FALSE;
132 if(!strcmp("--use-cached-creds", argv[arg])) {
133 use_cached_creds = TRUE;
136 else if(!strcmp("--helper-protocol", argv[arg])) {
139 helper_proto = argv[arg++];
141 else if(!strcmp("--username", argv[arg])) {
144 helper_user = argv[arg++];
146 else if(!strcmp("--domain", argv[arg])) {
149 helper_domain = argv[arg++];
152 puts("Usage: fake_ntlm [option]\n"
153 " --use-cached-creds\n"
154 " --helper-protocol [protocol]\n"
155 " --username [username]\n"
156 " --domain [domain]");
161 env = getenv("CURL_NTLM_AUTH_TESTNUM");
164 long lnum = strtol(env, &endptr, 10);
165 if((endptr != env + strlen(env)) || (lnum < 1L)) {
166 fprintf(stderr, "Test number not valid in CURL_NTLM_AUTH_TESTNUM");
172 fprintf(stderr, "Test number not specified in CURL_NTLM_AUTH_TESTNUM");
176 /* logmsg cannot be used until this file name is set */
177 snprintf(logfilename, sizeof(logfilename), LOGFILE, testnum);
178 serverlogfile = logfilename;
180 logmsg("fake_ntlm (user: %s) (proto: %s) (domain: %s) (cached creds: %s)",
181 helper_user, helper_proto, helper_domain,
182 (use_cached_creds) ? "yes" : "no");
184 env = getenv("CURL_NTLM_AUTH_SRCDIR");
189 filename = test2file(testnum);
190 stream = fopen(filename, "rb");
193 logmsg("fopen() failed with error: %d %s", error, strerror(error));
194 logmsg("Error opening file: %s", filename);
195 logmsg("Couldn't open test file %ld", testnum);
199 /* get the ntlm_auth input/output */
200 error = getpart(&type1_input, &size, "ntlm_auth_type1", "input", stream);
202 if(error || size == 0) {
203 logmsg("getpart() type 1 input failed with error: %d", error);
208 stream = fopen(filename, "rb");
211 logmsg("fopen() failed with error: %d %s", error, strerror(error));
212 logmsg("Error opening file: %s", filename);
213 logmsg("Couldn't open test file %ld", testnum);
218 error = getpart(&type3_input, &size, "ntlm_auth_type3", "input", stream);
220 if(error || size == 0) {
221 logmsg("getpart() type 3 input failed with error: %d", error);
226 while(fgets(buf, sizeof(buf), stdin)) {
227 if(strcmp(buf, type1_input) == 0) {
228 stream = fopen(filename, "rb");
231 logmsg("fopen() failed with error: %d %s", error, strerror(error));
232 logmsg("Error opening file: %s", filename);
233 logmsg("Couldn't open test file %ld", testnum);
238 error = getpart(&type1_output, &size, "ntlm_auth_type1", "output",
241 if(error || size == 0) {
242 logmsg("getpart() type 1 output failed with error: %d", error);
246 printf("%s", type1_output);
249 else if(strncmp(buf, type3_input, strlen(type3_input)) == 0) {
250 stream = fopen(filename, "rb");
253 logmsg("fopen() failed with error: %d %s", error, strerror(error));
254 logmsg("Error opening file: %s", filename);
255 logmsg("Couldn't open test file %ld", testnum);
260 error = getpart(&type3_output, &size, "ntlm_auth_type3", "output",
263 if(error || size == 0) {
264 logmsg("getpart() type 3 output failed with error: %d", error);
268 printf("%s", type3_output);
272 printf("Unknown request\n");
273 msgbuf = printable(buf, 0);
275 logmsg("invalid input: '%s'\n", msgbuf);
279 logmsg("OOM formatting invalid input: '%s'\n", buf);