1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 2010, Mandy Wu, <mandy.wu (at) intel.com> 9 * Copyright (C) 2011 - 2013, Daniel Stenberg, <daniel (at) 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 42 const 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 */ 48 static 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)) ? 67 HEX_STR_LEN + 1 : inlength/2; 68 outsize = inlength + outincr; 69 } 70 else 71 outsize = NOTHING_LEN + 1; 72 73 outbuf = malloc(outsize); 74 if(!outbuf) 75 return NULL; 76 77 if(!inlength) { 78 snprintf(&outbuf[0], outsize, "%s", NOTHING_STR); 79 return outbuf; 80 } 81 82 for(i = 0; i<inlength; i++) { 83 84 if(o > outsize - (HEX_STR_LEN + 1)) { 85 newsize = outsize + outincr; 86 newbuf = realloc(outbuf, newsize); 87 if(!newbuf) { 88 free(outbuf); 89 return NULL; 90 } 91 outbuf = newbuf; 92 outsize = newsize; 93 } 94 95 if((inbuf[i] > 0x20) && (inbuf[i] < 0x7F)) { 96 outbuf[o] = inbuf[i]; 97 o++; 98 } 99 else { 100 snprintf(&outbuf[o], outsize - o, HEX_FMT_STR, inbuf[i]); 101 o += HEX_STR_LEN; 102 } 103 104 } 105 outbuf[o] = '\0'; 106 107 return outbuf; 108 } 109 110 int main(int argc, char *argv[]) 111 { 112 char buf[1024]; 113 char logfilename[256]; 114 FILE *stream; 115 char *filename; 116 int error; 117 char *type1_input = NULL, *type3_input = NULL; 118 char *type1_output = NULL, *type3_output = NULL; 119 size_t size = 0; 120 long testnum; 121 const char *env; 122 int arg = 1; 123 const char *helper_user = "unknown"; 124 const char *helper_proto = "unknown"; 125 const char *helper_domain = "unknown"; 126 bool use_cached_creds = FALSE; 127 char *msgbuf; 128 129 buf[0] = '\0'; 130 131 while(argc > arg) { 132 if(!strcmp("--use-cached-creds", argv[arg])) { 133 use_cached_creds = TRUE; 134 arg++; 135 } 136 else if(!strcmp("--helper-protocol", argv[arg])) { 137 arg++; 138 if(argc > arg) 139 helper_proto = argv[arg++]; 140 } 141 else if(!strcmp("--username", argv[arg])) { 142 arg++; 143 if(argc > arg) 144 helper_user = argv[arg++]; 145 } 146 else if(!strcmp("--domain", argv[arg])) { 147 arg++; 148 if(argc > arg) 149 helper_domain = argv[arg++]; 150 } 151 else { 152 puts("Usage: fake_ntlm [option]\n" 153 " --use-cached-creds\n" 154 " --helper-protocol [protocol]\n" 155 " --username [username]\n" 156 " --domain [domain]"); 157 exit(1); 158 } 159 } 160 161 env = getenv("CURL_NTLM_AUTH_TESTNUM"); 162 if(env) { 163 char *endptr; 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"); 167 exit(1); 168 } 169 testnum = lnum; 170 } 171 else { 172 fprintf(stderr, "Test number not specified in CURL_NTLM_AUTH_TESTNUM"); 173 exit(1); 174 } 175 176 /* logmsg cannot be used until this file name is set */ 177 snprintf(logfilename, sizeof(logfilename), LOGFILE, testnum); 178 serverlogfile = logfilename; 179 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"); 183 184 env = getenv("CURL_NTLM_AUTH_SRCDIR"); 185 if(env) { 186 path = env; 187 } 188 189 filename = test2file(testnum); 190 stream = fopen(filename, "rb"); 191 if(!stream) { 192 error = errno; 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); 196 exit(1); 197 } 198 else { 199 /* get the ntlm_auth input/output */ 200 error = getpart(&type1_input, &size, "ntlm_auth_type1", "input", stream); 201 fclose(stream); 202 if(error || size == 0) { 203 logmsg("getpart() type 1 input failed with error: %d", error); 204 exit(1); 205 } 206 } 207 208 stream = fopen(filename, "rb"); 209 if(!stream) { 210 error = errno; 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); 214 exit(1); 215 } 216 else { 217 size = 0; 218 error = getpart(&type3_input, &size, "ntlm_auth_type3", "input", stream); 219 fclose(stream); 220 if(error || size == 0) { 221 logmsg("getpart() type 3 input failed with error: %d", error); 222 exit(1); 223 } 224 } 225 226 while(fgets(buf, sizeof(buf), stdin)) { 227 if(strcmp(buf, type1_input) == 0) { 228 stream = fopen(filename, "rb"); 229 if(!stream) { 230 error = errno; 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); 234 exit(1); 235 } 236 else { 237 size = 0; 238 error = getpart(&type1_output, &size, "ntlm_auth_type1", "output", 239 stream); 240 fclose(stream); 241 if(error || size == 0) { 242 logmsg("getpart() type 1 output failed with error: %d", error); 243 exit(1); 244 } 245 } 246 printf("%s", type1_output); 247 fflush(stdout); 248 } 249 else if(strncmp(buf, type3_input, strlen(type3_input)) == 0) { 250 stream = fopen(filename, "rb"); 251 if(!stream) { 252 error = errno; 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); 256 exit(1); 257 } 258 else { 259 size = 0; 260 error = getpart(&type3_output, &size, "ntlm_auth_type3", "output", 261 stream); 262 fclose(stream); 263 if(error || size == 0) { 264 logmsg("getpart() type 3 output failed with error: %d", error); 265 exit(1); 266 } 267 } 268 printf("%s", type3_output); 269 fflush(stdout); 270 } 271 else { 272 printf("Unknown request\n"); 273 msgbuf = printable(buf, 0); 274 if(msgbuf) { 275 logmsg("invalid input: '%s'\n", msgbuf); 276 free(msgbuf); 277 } 278 else 279 logmsg("OOM formatting invalid input: '%s'\n", buf); 280 exit(1); 281 } 282 } 283 logmsg("Exit"); 284 return 1; 285 } 286