1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, Mandy Wu, <mandy.wu@intel.com>
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
24 * This is a fake ntlm_auth, which is used for testing NTLM single-sign-on.
25 * When DEBUGBUILD is defined, libcurl invoke this tool instead of real winbind
26 * daemon helper /usr/bin/ntlm_auth. This tool will accept commands and
27 * responses with a pre-written string saved in test case test2005.
30 #define CURL_NO_OLDIES
38 #define ENABLE_CURLX_PRINTF
39 #include "curlx.h" /* from the private lib dir */
43 /* include memdebug.h last */
46 #ifndef DEFAULT_LOGFILE
47 #define DEFAULT_LOGFILE "log/fake_ntlm.log"
50 const char *serverlogfile = DEFAULT_LOGFILE;
53 * Returns an allocated buffer with printable representation of input
54 * buffer contents or returns NULL on out of memory condition.
56 static char *printable(char *inbuf, size_t inlength)
65 #define HEX_FMT_STR "[0x%02X]"
67 #define NOTHING_STR "[NOTHING]"
71 inlength = strlen(inbuf);
74 outincr = ((inlength/2) < (HEX_STR_LEN+1)) ? HEX_STR_LEN+1 : inlength/2;
75 outsize = inlength + outincr;
78 outsize = NOTHING_LEN + 1;
80 outbuf = malloc(outsize);
85 sprintf(&outbuf[0], "%s", NOTHING_STR);
89 for(i=0; i<inlength; i++) {
91 if(o > outsize - (HEX_STR_LEN + 1)) {
92 newsize = outsize + outincr;
93 newbuf = realloc(outbuf, newsize);
102 if((inbuf[i] > 0x20) && (inbuf[i] < 0x7F)) {
103 outbuf[o] = inbuf[i];
107 sprintf(&outbuf[o], HEX_FMT_STR, inbuf[i]);
117 int main(int argc, char *argv[])
123 char *type1_input = NULL, *type3_input = NULL;
124 char *type1_output = NULL, *type3_output = NULL;
129 char *helper_user = (char *)"unknown";
130 char *helper_proto = (char *)"unknown";
131 char *helper_domain = (char *)"unknown";
132 bool use_cached_creds = FALSE;
138 if(!strcmp("--use-cached-creds", argv[arg])) {
139 use_cached_creds = TRUE;
142 else if(!strcmp("--helper-protocol", argv[arg])) {
145 helper_proto = argv[arg++];
147 else if(!strcmp("--username", argv[arg])) {
150 helper_user = argv[arg++];
152 else if(!strcmp("--domain", argv[arg])) {
155 helper_domain = argv[arg++];
158 puts("Usage: fake_ntlm [option]\n"
159 " --use-cached-creds\n"
160 " --helper-protocol [protocol]\n"
161 " --username [username]\n"
162 " --domain [domain]");
167 logmsg("fake_ntlm (user: %s) (proto: %s) (domain: %s) (cached creds: %s)",
168 helper_user, helper_proto, helper_domain,
169 (use_cached_creds == TRUE) ? "yes" : "no");
171 env = getenv("CURL_NTLM_AUTH_TESTNUM");
174 long lnum = strtol(env, &endptr, 10);
175 if((endptr != env + strlen(env)) || (lnum < 1L)) {
176 logmsg("Test number not valid in CURL_NTLM_AUTH_TESTNUM");
181 logmsg("Test number not specified in CURL_NTLM_AUTH_TESTNUM");
185 env = getenv("CURL_NTLM_AUTH_SRCDIR");
190 filename = test2file(testnum);
191 stream=fopen(filename, "rb");
194 logmsg("fopen() failed with error: %d %s", error, strerror(error));
195 logmsg("Error opening file: %s", filename);
196 logmsg("Couldn't open test file %ld", testnum);
200 /* get the ntlm_auth input/output */
201 error = getpart(&type1_input, &size, "ntlm_auth_type1", "input", stream);
203 if(error || size == 0) {
204 logmsg("getpart() type 1 input failed with error: %d", error);
209 stream=fopen(filename, "rb");
212 logmsg("fopen() failed with error: %d %s", error, strerror(error));
213 logmsg("Error opening file: %s", filename);
214 logmsg("Couldn't open test file %ld", testnum);
219 error = getpart(&type3_input, &size, "ntlm_auth_type3", "input", stream);
221 if(error || size == 0) {
222 logmsg("getpart() type 3 input failed with error: %d", error);
227 while(fgets(buf, sizeof(buf), stdin)) {
228 if(strcmp(buf, type1_input) == 0) {
229 stream=fopen(filename, "rb");
232 logmsg("fopen() failed with error: %d %s", error, strerror(error));
233 logmsg("Error opening file: %s", filename);
234 logmsg("Couldn't open test file %ld", testnum);
239 error = getpart(&type1_output, &size, "ntlm_auth_type1", "output", stream);
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", stream);
262 if(error || size == 0) {
263 logmsg("getpart() type 3 output failed with error: %d", error);
267 printf("%s", type3_output);
271 printf("Unknown request\n");
272 msgbuf = printable(buf, 0);
274 logmsg("invalid input: '%s'\n", msgbuf);
278 logmsg("OOM formatting invalid input: '%s'\n", buf);