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 #ifndef DEFAULT_LOGFILE
41 #define DEFAULT_LOGFILE "log/fake_ntlm.log"
44 const char *serverlogfile = DEFAULT_LOGFILE;
47 * Returns an allocated buffer with printable representation of input
48 * buffer contents or returns NULL on out of memory condition.
50 static char *printable(char *inbuf, size_t inlength)
59 #define HEX_FMT_STR "[0x%02X]"
61 #define NOTHING_STR "[NOTHING]"
65 inlength = strlen(inbuf);
68 outincr = ((inlength/2) < (HEX_STR_LEN+1)) ? HEX_STR_LEN+1 : inlength/2;
69 outsize = inlength + outincr;
72 outsize = NOTHING_LEN + 1;
74 outbuf = malloc(outsize);
79 snprintf(&outbuf[0], outsize, "%s", NOTHING_STR);
83 for(i=0; i<inlength; i++) {
85 if(o > outsize - (HEX_STR_LEN + 1)) {
86 newsize = outsize + outincr;
87 newbuf = realloc(outbuf, newsize);
96 if((inbuf[i] > 0x20) && (inbuf[i] < 0x7F)) {
101 snprintf(&outbuf[o], outsize - o, HEX_FMT_STR, inbuf[i]);
111 int main(int argc, char *argv[])
117 char *type1_input = NULL, *type3_input = NULL;
118 char *type1_output = NULL, *type3_output = NULL;
123 char *helper_user = (char *)"unknown";
124 char *helper_proto = (char *)"unknown";
125 char *helper_domain = (char *)"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 logmsg("fake_ntlm (user: %s) (proto: %s) (domain: %s) (cached creds: %s)",
162 helper_user, helper_proto, helper_domain,
163 (use_cached_creds) ? "yes" : "no");
165 env = getenv("CURL_NTLM_AUTH_TESTNUM");
168 long lnum = strtol(env, &endptr, 10);
169 if((endptr != env + strlen(env)) || (lnum < 1L)) {
170 logmsg("Test number not valid in CURL_NTLM_AUTH_TESTNUM");
176 logmsg("Test number not specified in CURL_NTLM_AUTH_TESTNUM");
180 env = getenv("CURL_NTLM_AUTH_SRCDIR");
185 filename = test2file(testnum);
186 stream=fopen(filename, "rb");
189 logmsg("fopen() failed with error: %d %s", error, strerror(error));
190 logmsg("Error opening file: %s", filename);
191 logmsg("Couldn't open test file %ld", testnum);
195 /* get the ntlm_auth input/output */
196 error = getpart(&type1_input, &size, "ntlm_auth_type1", "input", stream);
198 if(error || size == 0) {
199 logmsg("getpart() type 1 input failed with error: %d", error);
204 stream=fopen(filename, "rb");
207 logmsg("fopen() failed with error: %d %s", error, strerror(error));
208 logmsg("Error opening file: %s", filename);
209 logmsg("Couldn't open test file %ld", testnum);
214 error = getpart(&type3_input, &size, "ntlm_auth_type3", "input", stream);
216 if(error || size == 0) {
217 logmsg("getpart() type 3 input failed with error: %d", error);
222 while(fgets(buf, sizeof(buf), stdin)) {
223 if(strcmp(buf, type1_input) == 0) {
224 stream=fopen(filename, "rb");
227 logmsg("fopen() failed with error: %d %s", error, strerror(error));
228 logmsg("Error opening file: %s", filename);
229 logmsg("Couldn't open test file %ld", testnum);
234 error = getpart(&type1_output, &size, "ntlm_auth_type1", "output",
237 if(error || size == 0) {
238 logmsg("getpart() type 1 output failed with error: %d", error);
242 printf("%s", type1_output);
245 else if(strncmp(buf, type3_input, strlen(type3_input)) == 0) {
246 stream=fopen(filename, "rb");
249 logmsg("fopen() failed with error: %d %s", error, strerror(error));
250 logmsg("Error opening file: %s", filename);
251 logmsg("Couldn't open test file %ld", testnum);
256 error = getpart(&type3_output, &size, "ntlm_auth_type3", "output",
259 if(error || size == 0) {
260 logmsg("getpart() type 3 output failed with error: %d", error);
264 printf("%s", type3_output);
268 printf("Unknown request\n");
269 msgbuf = printable(buf, 0);
271 logmsg("invalid input: '%s'\n", msgbuf);
275 logmsg("OOM formatting invalid input: '%s'\n", buf);