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 sprintf(&outbuf[0], "%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 sprintf(&outbuf[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");
175 logmsg("Test number not specified in CURL_NTLM_AUTH_TESTNUM");
179 env = getenv("CURL_NTLM_AUTH_SRCDIR");
184 filename = test2file(testnum);
185 stream=fopen(filename, "rb");
188 logmsg("fopen() failed with error: %d %s", error, strerror(error));
189 logmsg("Error opening file: %s", filename);
190 logmsg("Couldn't open test file %ld", testnum);
194 /* get the ntlm_auth input/output */
195 error = getpart(&type1_input, &size, "ntlm_auth_type1", "input", stream);
197 if(error || size == 0) {
198 logmsg("getpart() type 1 input failed with error: %d", error);
203 stream=fopen(filename, "rb");
206 logmsg("fopen() failed with error: %d %s", error, strerror(error));
207 logmsg("Error opening file: %s", filename);
208 logmsg("Couldn't open test file %ld", testnum);
213 error = getpart(&type3_input, &size, "ntlm_auth_type3", "input", stream);
215 if(error || size == 0) {
216 logmsg("getpart() type 3 input failed with error: %d", error);
221 while(fgets(buf, sizeof(buf), stdin)) {
222 if(strcmp(buf, type1_input) == 0) {
223 stream=fopen(filename, "rb");
226 logmsg("fopen() failed with error: %d %s", error, strerror(error));
227 logmsg("Error opening file: %s", filename);
228 logmsg("Couldn't open test file %ld", testnum);
233 error = getpart(&type1_output, &size, "ntlm_auth_type1", "output", stream);
235 if(error || size == 0) {
236 logmsg("getpart() type 1 output failed with error: %d", error);
240 printf("%s", type1_output);
243 else if(strncmp(buf, type3_input, strlen(type3_input)) == 0) {
244 stream=fopen(filename, "rb");
247 logmsg("fopen() failed with error: %d %s", error, strerror(error));
248 logmsg("Error opening file: %s", filename);
249 logmsg("Couldn't open test file %ld", testnum);
254 error = getpart(&type3_output, &size, "ntlm_auth_type3", "output", stream);
256 if(error || size == 0) {
257 logmsg("getpart() type 3 output failed with error: %d", error);
261 printf("%s", type3_output);
265 printf("Unknown request\n");
266 msgbuf = printable(buf, 0);
268 logmsg("invalid input: '%s'\n", msgbuf);
272 logmsg("OOM formatting invalid input: '%s'\n", buf);