1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 2010, Mandy Wu, <mandy.wu@intel.com>
9 * Copyright (C) 2011 - 2013, Daniel Stenberg, <daniel@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 http://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#ifndef DEFAULT_LOGFILE
41#define DEFAULT_LOGFILE "log/fake_ntlm.log"
42#endif
43
44const char *serverlogfile = DEFAULT_LOGFILE;
45
46/*
47 * Returns an allocated buffer with printable representation of input
48 * buffer contents or returns NULL on out of memory condition.
49 */
50static char *printable(char *inbuf, size_t inlength)
51{
52  char *outbuf;
53  char *newbuf;
54  size_t newsize;
55  size_t outsize;
56  size_t outincr = 0;
57  size_t i, o = 0;
58
59#define HEX_FMT_STR  "[0x%02X]"
60#define HEX_STR_LEN  6
61#define NOTHING_STR  "[NOTHING]"
62#define NOTHING_LEN  9
63
64  if(!inlength)
65    inlength = strlen(inbuf);
66
67  if(inlength) {
68    outincr = ((inlength/2) < (HEX_STR_LEN+1)) ? HEX_STR_LEN+1 : inlength/2;
69    outsize = inlength + outincr;
70  }
71  else
72    outsize = NOTHING_LEN + 1;
73
74  outbuf = malloc(outsize);
75  if(!outbuf)
76    return NULL;
77
78  if(!inlength) {
79    sprintf(&outbuf[0], "%s", NOTHING_STR);
80    return outbuf;
81  }
82
83  for(i=0; i<inlength; i++) {
84
85    if(o > outsize - (HEX_STR_LEN + 1)) {
86      newsize = outsize + outincr;
87      newbuf = realloc(outbuf, newsize);
88      if(!newbuf) {
89        free(outbuf);
90        return NULL;
91      }
92      outbuf = newbuf;
93      outsize = newsize;
94    }
95
96    if((inbuf[i] > 0x20) && (inbuf[i] < 0x7F)) {
97      outbuf[o] = inbuf[i];
98      o++;
99    }
100    else {
101      sprintf(&outbuf[o], HEX_FMT_STR, inbuf[i]);
102      o += HEX_STR_LEN;
103    }
104
105  }
106  outbuf[o] = '\0';
107
108  return outbuf;
109}
110
111int main(int argc, char *argv[])
112{
113  char buf[1024];
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  char *helper_user = (char *)"unknown";
124  char *helper_proto = (char *)"unknown";
125  char *helper_domain = (char *)"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  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");
164
165  env = getenv("CURL_NTLM_AUTH_TESTNUM");
166  if (env) {
167    char *endptr;
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");
171      exit(1);
172    }
173    testnum = lnum;
174  } else {
175    logmsg("Test number not specified in CURL_NTLM_AUTH_TESTNUM");
176    exit(1);
177  }
178
179  env = getenv("CURL_NTLM_AUTH_SRCDIR");
180  if (env) {
181    path = env;
182  }
183
184  filename = test2file(testnum);
185  stream=fopen(filename, "rb");
186  if(!stream) {
187    error = errno;
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);
191    exit(1);
192  }
193  else {
194    /* get the ntlm_auth input/output */
195    error = getpart(&type1_input, &size, "ntlm_auth_type1", "input", stream);
196    fclose(stream);
197    if(error || size == 0) {
198      logmsg("getpart() type 1 input failed with error: %d", error);
199      exit(1);
200    }
201  }
202
203  stream=fopen(filename, "rb");
204  if(!stream) {
205    error = errno;
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);
209    exit(1);
210  }
211  else {
212    size = 0;
213    error = getpart(&type3_input, &size, "ntlm_auth_type3", "input", stream);
214    fclose(stream);
215    if(error || size == 0) {
216      logmsg("getpart() type 3 input failed with error: %d", error);
217      exit(1);
218    }
219  }
220
221  while(fgets(buf, sizeof(buf), stdin)) {
222    if(strcmp(buf, type1_input) == 0) {
223      stream=fopen(filename, "rb");
224      if(!stream) {
225        error = errno;
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);
229        exit(1);
230      }
231      else {
232        size = 0;
233        error = getpart(&type1_output, &size, "ntlm_auth_type1", "output", stream);
234        fclose(stream);
235        if(error || size == 0) {
236          logmsg("getpart() type 1 output failed with error: %d", error);
237          exit(1);
238        }
239      }
240      printf("%s", type1_output);
241      fflush(stdout);
242    }
243    else if(strncmp(buf, type3_input, strlen(type3_input)) == 0) {
244      stream=fopen(filename, "rb");
245      if(!stream) {
246        error = errno;
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);
250        exit(1);
251      }
252      else {
253        size = 0;
254        error = getpart(&type3_output, &size, "ntlm_auth_type3", "output", stream);
255        fclose(stream);
256        if(error || size == 0) {
257          logmsg("getpart() type 3 output failed with error: %d", error);
258          exit(1);
259        }
260      }
261      printf("%s", type3_output);
262      fflush(stdout);
263    }
264    else {
265      printf("Unknown request\n");
266      msgbuf = printable(buf, 0);
267      if(msgbuf) {
268        logmsg("invalid input: '%s'\n", msgbuf);
269        free(msgbuf);
270      }
271      else
272        logmsg("OOM formatting invalid input: '%s'\n", buf);
273      exit(1);
274    }
275  }
276  return 1;
277}
278