fake_ntlm.c 7.6 KB

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