1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 2010, Mandy Wu, <mandy.wu@intel.com>
9 * Copyright (C) 2011 - 2020, 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 #define LOGFILE "log/fake_ntlm%ld.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 */
printable(char * inbuf,size_t inlength)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 msnprintf(&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 msnprintf(&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
main(int argc,char * argv[])110 int main(int argc, char *argv[])
111 {
112 char buf[1024];
113 char logfilename[256];
114 FILE *stream;
115 int error;
116 char *type1_input = NULL, *type3_input = NULL;
117 char *type1_output = NULL, *type3_output = NULL;
118 size_t size = 0;
119 long testnum;
120 const char *env;
121 int arg = 1;
122 const char *helper_user = "unknown";
123 const char *helper_proto = "unknown";
124 const char *helper_domain = "unknown";
125 bool use_cached_creds = FALSE;
126 char *msgbuf;
127
128 buf[0] = '\0';
129
130 while(argc > arg) {
131 if(!strcmp("--use-cached-creds", argv[arg])) {
132 use_cached_creds = TRUE;
133 arg++;
134 }
135 else if(!strcmp("--helper-protocol", argv[arg])) {
136 arg++;
137 if(argc > arg)
138 helper_proto = argv[arg++];
139 }
140 else if(!strcmp("--username", argv[arg])) {
141 arg++;
142 if(argc > arg)
143 helper_user = argv[arg++];
144 }
145 else if(!strcmp("--domain", argv[arg])) {
146 arg++;
147 if(argc > arg)
148 helper_domain = argv[arg++];
149 }
150 else {
151 puts("Usage: fake_ntlm [option]\n"
152 " --use-cached-creds\n"
153 " --helper-protocol [protocol]\n"
154 " --username [username]\n"
155 " --domain [domain]");
156 exit(1);
157 }
158 }
159
160 env = getenv("CURL_NTLM_AUTH_TESTNUM");
161 if(env) {
162 char *endptr;
163 long lnum = strtol(env, &endptr, 10);
164 if((endptr != env + strlen(env)) || (lnum < 1L)) {
165 fprintf(stderr, "Test number not valid in CURL_NTLM_AUTH_TESTNUM");
166 exit(1);
167 }
168 testnum = lnum;
169 }
170 else {
171 fprintf(stderr, "Test number not specified in CURL_NTLM_AUTH_TESTNUM");
172 exit(1);
173 }
174
175 /* logmsg cannot be used until this file name is set */
176 msnprintf(logfilename, sizeof(logfilename), LOGFILE, testnum);
177 serverlogfile = logfilename;
178
179 logmsg("fake_ntlm (user: %s) (proto: %s) (domain: %s) (cached creds: %s)",
180 helper_user, helper_proto, helper_domain,
181 (use_cached_creds) ? "yes" : "no");
182
183 env = getenv("CURL_NTLM_AUTH_SRCDIR");
184 if(env) {
185 path = env;
186 }
187
188 stream = test2fopen(testnum);
189 if(!stream) {
190 error = errno;
191 logmsg("fopen() failed with error: %d %s", error, strerror(error));
192 logmsg("Couldn't open test file %ld", testnum);
193 exit(1);
194 }
195 else {
196 /* get the ntlm_auth input/output */
197 error = getpart(&type1_input, &size, "ntlm_auth_type1", "input", stream);
198 fclose(stream);
199 if(error || size == 0) {
200 logmsg("getpart() type 1 input failed with error: %d", error);
201 exit(1);
202 }
203 }
204
205 stream = test2fopen(testnum);
206 if(!stream) {
207 error = errno;
208 logmsg("fopen() failed with error: %d %s", error, strerror(error));
209 logmsg("Couldn't open test file %ld", testnum);
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 = test2fopen(testnum);
224 if(!stream) {
225 error = errno;
226 logmsg("fopen() failed with error: %d %s", error, strerror(error));
227 logmsg("Couldn't open test file %ld", testnum);
228 exit(1);
229 }
230 else {
231 size = 0;
232 error = getpart(&type1_output, &size, "ntlm_auth_type1", "output",
233 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 = test2fopen(testnum);
245 if(!stream) {
246 error = errno;
247 logmsg("fopen() failed with error: %d %s", error, strerror(error));
248 logmsg("Couldn't open test file %ld", testnum);
249 exit(1);
250 }
251 else {
252 size = 0;
253 error = getpart(&type3_output, &size, "ntlm_auth_type3", "output",
254 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 logmsg("Exit");
277 return 1;
278 }
279