2
0

fake_ntlm.c 7.4 KB

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