2
0

fake_ntlm.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Mandy Wu, <mandy.wu@intel.com>
  9. * Copyright (C) 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.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. * SPDX-License-Identifier: curl
  23. *
  24. ***************************************************************************/
  25. #include "server_setup.h"
  26. /*
  27. * This is a fake ntlm_auth, which is used for testing NTLM single-sign-on.
  28. * When DEBUGBUILD is defined, libcurl invoke this tool instead of real winbind
  29. * daemon helper /usr/bin/ntlm_auth. This tool will accept commands and
  30. * responses with a pre-written string saved in test case test2005.
  31. */
  32. #define ENABLE_CURLX_PRINTF
  33. #include "curlx.h" /* from the private lib dir */
  34. #include "getpart.h"
  35. #include "util.h"
  36. /* include memdebug.h last */
  37. #include "memdebug.h"
  38. #define LOGFILE "%s/fake_ntlm%ld.log"
  39. static const char *logdir = "log";
  40. const char *serverlogfile;
  41. /*
  42. * Returns an allocated buffer with printable representation of input
  43. * buffer contents or returns NULL on out of memory condition.
  44. */
  45. static char *printable(char *inbuf, size_t inlength)
  46. {
  47. char *outbuf;
  48. char *newbuf;
  49. size_t newsize;
  50. size_t outsize;
  51. size_t outincr = 0;
  52. size_t i, o = 0;
  53. #define HEX_FMT_STR "[0x%02X]"
  54. #define HEX_STR_LEN 6
  55. #define NOTHING_STR "[NOTHING]"
  56. #define NOTHING_LEN 9
  57. if(!inlength)
  58. inlength = strlen(inbuf);
  59. if(inlength) {
  60. outincr = ((inlength/2) < (HEX_STR_LEN + 1)) ?
  61. HEX_STR_LEN + 1 : inlength/2;
  62. outsize = inlength + outincr;
  63. }
  64. else
  65. outsize = NOTHING_LEN + 1;
  66. outbuf = malloc(outsize);
  67. if(!outbuf)
  68. return NULL;
  69. if(!inlength) {
  70. msnprintf(&outbuf[0], outsize, "%s", NOTHING_STR);
  71. return outbuf;
  72. }
  73. for(i = 0; i<inlength; i++) {
  74. if(o > outsize - (HEX_STR_LEN + 1)) {
  75. newsize = outsize + outincr;
  76. newbuf = realloc(outbuf, newsize);
  77. if(!newbuf) {
  78. free(outbuf);
  79. return NULL;
  80. }
  81. outbuf = newbuf;
  82. outsize = newsize;
  83. }
  84. if((inbuf[i] > 0x20) && (inbuf[i] < 0x7F)) {
  85. outbuf[o] = inbuf[i];
  86. o++;
  87. }
  88. else {
  89. msnprintf(&outbuf[o], outsize - o, HEX_FMT_STR, inbuf[i]);
  90. o += HEX_STR_LEN;
  91. }
  92. }
  93. outbuf[o] = '\0';
  94. return outbuf;
  95. }
  96. int main(int argc, char *argv[])
  97. {
  98. char buf[1024];
  99. char logfilename[256];
  100. FILE *stream;
  101. int error;
  102. char *type1_input = NULL, *type3_input = NULL;
  103. char *type1_output = NULL, *type3_output = NULL;
  104. size_t size = 0;
  105. long testnum;
  106. const char *env;
  107. int arg = 1;
  108. const char *helper_user = "unknown";
  109. const char *helper_proto = "unknown";
  110. const char *helper_domain = "unknown";
  111. bool use_cached_creds = FALSE;
  112. char *msgbuf;
  113. buf[0] = '\0';
  114. while(argc > arg) {
  115. if(!strcmp("--use-cached-creds", argv[arg])) {
  116. use_cached_creds = TRUE;
  117. arg++;
  118. }
  119. else if(!strcmp("--helper-protocol", argv[arg])) {
  120. arg++;
  121. if(argc > arg)
  122. helper_proto = argv[arg++];
  123. }
  124. else if(!strcmp("--username", argv[arg])) {
  125. arg++;
  126. if(argc > arg)
  127. helper_user = argv[arg++];
  128. }
  129. else if(!strcmp("--domain", argv[arg])) {
  130. arg++;
  131. if(argc > arg)
  132. helper_domain = argv[arg++];
  133. }
  134. else {
  135. puts("Usage: fake_ntlm [option]\n"
  136. " --use-cached-creds\n"
  137. " --helper-protocol [protocol]\n"
  138. " --username [username]\n"
  139. " --domain [domain]");
  140. exit(1);
  141. }
  142. }
  143. env = getenv("CURL_NTLM_LOGDIR");
  144. if(env) {
  145. logdir = env;
  146. }
  147. env = getenv("CURL_NTLM_AUTH_TESTNUM");
  148. if(env) {
  149. char *endptr;
  150. long lnum = strtol(env, &endptr, 10);
  151. if((endptr != env + strlen(env)) || (lnum < 1L)) {
  152. fprintf(stderr, "Test number not valid in CURL_NTLM_AUTH_TESTNUM");
  153. exit(1);
  154. }
  155. testnum = lnum;
  156. }
  157. else {
  158. fprintf(stderr, "Test number not specified in CURL_NTLM_AUTH_TESTNUM");
  159. exit(1);
  160. }
  161. /* logmsg cannot be used until this file name is set */
  162. msnprintf(logfilename, sizeof(logfilename), LOGFILE, logdir, testnum);
  163. serverlogfile = logfilename;
  164. logmsg("fake_ntlm (user: %s) (proto: %s) (domain: %s) (cached creds: %s)",
  165. helper_user, helper_proto, helper_domain,
  166. (use_cached_creds) ? "yes" : "no");
  167. env = getenv("CURL_NTLM_AUTH_SRCDIR");
  168. if(env) {
  169. path = env;
  170. }
  171. stream = test2fopen(testnum, logdir);
  172. if(!stream) {
  173. error = errno;
  174. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  175. logmsg("Couldn't open test file %ld", testnum);
  176. exit(1);
  177. }
  178. else {
  179. /* get the ntlm_auth input/output */
  180. error = getpart(&type1_input, &size, "ntlm_auth_type1", "input", stream);
  181. fclose(stream);
  182. if(error || size == 0) {
  183. logmsg("getpart() type 1 input failed with error: %d", error);
  184. exit(1);
  185. }
  186. }
  187. stream = test2fopen(testnum, logdir);
  188. if(!stream) {
  189. error = errno;
  190. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  191. logmsg("Couldn't open test file %ld", testnum);
  192. }
  193. else {
  194. size = 0;
  195. error = getpart(&type3_input, &size, "ntlm_auth_type3", "input", stream);
  196. fclose(stream);
  197. if(error || size == 0) {
  198. logmsg("getpart() type 3 input failed with error: %d", error);
  199. exit(1);
  200. }
  201. }
  202. while(fgets(buf, sizeof(buf), stdin)) {
  203. if(strcmp(buf, type1_input) == 0) {
  204. stream = test2fopen(testnum, logdir);
  205. if(!stream) {
  206. error = errno;
  207. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  208. logmsg("Couldn't open test file %ld", testnum);
  209. exit(1);
  210. }
  211. else {
  212. size = 0;
  213. error = getpart(&type1_output, &size, "ntlm_auth_type1", "output",
  214. stream);
  215. fclose(stream);
  216. if(error || size == 0) {
  217. logmsg("getpart() type 1 output failed with error: %d", error);
  218. exit(1);
  219. }
  220. }
  221. printf("%s", type1_output);
  222. fflush(stdout);
  223. }
  224. else if(strncmp(buf, type3_input, strlen(type3_input)) == 0) {
  225. stream = test2fopen(testnum, logdir);
  226. if(!stream) {
  227. error = errno;
  228. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  229. logmsg("Couldn't open test file %ld", testnum);
  230. exit(1);
  231. }
  232. else {
  233. size = 0;
  234. error = getpart(&type3_output, &size, "ntlm_auth_type3", "output",
  235. stream);
  236. fclose(stream);
  237. if(error || size == 0) {
  238. logmsg("getpart() type 3 output failed with error: %d", error);
  239. exit(1);
  240. }
  241. }
  242. printf("%s", type3_output);
  243. fflush(stdout);
  244. }
  245. else {
  246. printf("Unknown request\n");
  247. msgbuf = printable(buf, 0);
  248. if(msgbuf) {
  249. logmsg("invalid input: '%s'\n", msgbuf);
  250. free(msgbuf);
  251. }
  252. else
  253. logmsg("OOM formatting invalid input: '%s'\n", buf);
  254. exit(1);
  255. }
  256. }
  257. logmsg("Exit");
  258. return 1;
  259. }