Imported Upstream version 7.50.2
[platform/upstream/curl.git] / tests / server / fake_ntlm.c
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 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 #ifndef DEFAULT_LOGFILE
41 #define DEFAULT_LOGFILE "log/fake_ntlm.log"
42 #endif
43
44 const 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  */
50 static 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     snprintf(&outbuf[0], outsize, "%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       snprintf(&outbuf[o], outsize - o, HEX_FMT_STR, inbuf[i]);
102       o += HEX_STR_LEN;
103     }
104
105   }
106   outbuf[o] = '\0';
107
108   return outbuf;
109 }
110
111 int 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   }
175   else {
176     logmsg("Test number not specified in CURL_NTLM_AUTH_TESTNUM");
177     exit(1);
178   }
179
180   env = getenv("CURL_NTLM_AUTH_SRCDIR");
181   if(env) {
182     path = env;
183   }
184
185   filename = test2file(testnum);
186   stream=fopen(filename, "rb");
187   if(!stream) {
188     error = errno;
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);
192     exit(1);
193   }
194   else {
195     /* get the ntlm_auth input/output */
196     error = getpart(&type1_input, &size, "ntlm_auth_type1", "input", stream);
197     fclose(stream);
198     if(error || size == 0) {
199       logmsg("getpart() type 1 input failed with error: %d", error);
200       exit(1);
201     }
202   }
203
204   stream=fopen(filename, "rb");
205   if(!stream) {
206     error = errno;
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);
210     exit(1);
211   }
212   else {
213     size = 0;
214     error = getpart(&type3_input, &size, "ntlm_auth_type3", "input", stream);
215     fclose(stream);
216     if(error || size == 0) {
217       logmsg("getpart() type 3 input failed with error: %d", error);
218       exit(1);
219     }
220   }
221
222   while(fgets(buf, sizeof(buf), stdin)) {
223     if(strcmp(buf, type1_input) == 0) {
224       stream=fopen(filename, "rb");
225       if(!stream) {
226         error = errno;
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);
230         exit(1);
231       }
232       else {
233         size = 0;
234         error = getpart(&type1_output, &size, "ntlm_auth_type1", "output",
235                         stream);
236         fclose(stream);
237         if(error || size == 0) {
238           logmsg("getpart() type 1 output failed with error: %d", error);
239           exit(1);
240         }
241       }
242       printf("%s", type1_output);
243       fflush(stdout);
244     }
245     else if(strncmp(buf, type3_input, strlen(type3_input)) == 0) {
246       stream=fopen(filename, "rb");
247       if(!stream) {
248         error = errno;
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);
252         exit(1);
253       }
254       else {
255         size = 0;
256         error = getpart(&type3_output, &size, "ntlm_auth_type3", "output",
257                         stream);
258         fclose(stream);
259         if(error || size == 0) {
260           logmsg("getpart() type 3 output failed with error: %d", error);
261           exit(1);
262         }
263       }
264       printf("%s", type3_output);
265       fflush(stdout);
266     }
267     else {
268       printf("Unknown request\n");
269       msgbuf = printable(buf, 0);
270       if(msgbuf) {
271         logmsg("invalid input: '%s'\n", msgbuf);
272         free(msgbuf);
273       }
274       else
275         logmsg("OOM formatting invalid input: '%s'\n", buf);
276       exit(1);
277     }
278   }
279   return 1;
280 }