2
0

fake_ntlm.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 http://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. #ifndef DEFAULT_LOGFILE
  37. #define DEFAULT_LOGFILE "log/fake_ntlm.log"
  38. #endif
  39. const char *serverlogfile = DEFAULT_LOGFILE;
  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)) ? HEX_STR_LEN+1 : inlength/2;
  60. outsize = inlength + outincr;
  61. }
  62. else
  63. outsize = NOTHING_LEN + 1;
  64. outbuf = malloc(outsize);
  65. if(!outbuf)
  66. return NULL;
  67. if(!inlength) {
  68. sprintf(&outbuf[0], "%s", NOTHING_STR);
  69. return outbuf;
  70. }
  71. for(i=0; i<inlength; i++) {
  72. if(o > outsize - (HEX_STR_LEN + 1)) {
  73. newsize = outsize + outincr;
  74. newbuf = realloc(outbuf, newsize);
  75. if(!newbuf) {
  76. free(outbuf);
  77. return NULL;
  78. }
  79. outbuf = newbuf;
  80. outsize = newsize;
  81. }
  82. if((inbuf[i] > 0x20) && (inbuf[i] < 0x7F)) {
  83. outbuf[o] = inbuf[i];
  84. o++;
  85. }
  86. else {
  87. sprintf(&outbuf[o], HEX_FMT_STR, inbuf[i]);
  88. o += HEX_STR_LEN;
  89. }
  90. }
  91. outbuf[o] = '\0';
  92. return outbuf;
  93. }
  94. int main(int argc, char *argv[])
  95. {
  96. char buf[1024];
  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. char *helper_user = (char *)"unknown";
  107. char *helper_proto = (char *)"unknown";
  108. char *helper_domain = (char *)"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. logmsg("fake_ntlm (user: %s) (proto: %s) (domain: %s) (cached creds: %s)",
  142. helper_user, helper_proto, helper_domain,
  143. (use_cached_creds) ? "yes" : "no");
  144. env = getenv("CURL_NTLM_AUTH_TESTNUM");
  145. if (env) {
  146. char *endptr;
  147. long lnum = strtol(env, &endptr, 10);
  148. if((endptr != env + strlen(env)) || (lnum < 1L)) {
  149. logmsg("Test number not valid in CURL_NTLM_AUTH_TESTNUM");
  150. exit(1);
  151. }
  152. testnum = lnum;
  153. } else {
  154. logmsg("Test number not specified in CURL_NTLM_AUTH_TESTNUM");
  155. exit(1);
  156. }
  157. env = getenv("CURL_NTLM_AUTH_SRCDIR");
  158. if (env) {
  159. path = env;
  160. }
  161. filename = test2file(testnum);
  162. stream=fopen(filename, "rb");
  163. if(!stream) {
  164. error = errno;
  165. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  166. logmsg("Error opening file: %s", filename);
  167. logmsg("Couldn't open test file %ld", testnum);
  168. exit(1);
  169. }
  170. else {
  171. /* get the ntlm_auth input/output */
  172. error = getpart(&type1_input, &size, "ntlm_auth_type1", "input", stream);
  173. fclose(stream);
  174. if(error || size == 0) {
  175. logmsg("getpart() type 1 input failed with error: %d", error);
  176. exit(1);
  177. }
  178. }
  179. stream=fopen(filename, "rb");
  180. if(!stream) {
  181. error = errno;
  182. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  183. logmsg("Error opening file: %s", filename);
  184. logmsg("Couldn't open test file %ld", testnum);
  185. exit(1);
  186. }
  187. else {
  188. size = 0;
  189. error = getpart(&type3_input, &size, "ntlm_auth_type3", "input", stream);
  190. fclose(stream);
  191. if(error || size == 0) {
  192. logmsg("getpart() type 3 input failed with error: %d", error);
  193. exit(1);
  194. }
  195. }
  196. while(fgets(buf, sizeof(buf), stdin)) {
  197. if(strcmp(buf, type1_input) == 0) {
  198. stream=fopen(filename, "rb");
  199. if(!stream) {
  200. error = errno;
  201. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  202. logmsg("Error opening file: %s", filename);
  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", stream);
  209. fclose(stream);
  210. if(error || size == 0) {
  211. logmsg("getpart() type 1 output failed with error: %d", error);
  212. exit(1);
  213. }
  214. }
  215. printf("%s", type1_output);
  216. fflush(stdout);
  217. }
  218. else if(strncmp(buf, type3_input, strlen(type3_input)) == 0) {
  219. stream=fopen(filename, "rb");
  220. if(!stream) {
  221. error = errno;
  222. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  223. logmsg("Error opening file: %s", filename);
  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", stream);
  230. fclose(stream);
  231. if(error || size == 0) {
  232. logmsg("getpart() type 3 output failed with error: %d", error);
  233. exit(1);
  234. }
  235. }
  236. printf("%s", type3_output);
  237. fflush(stdout);
  238. }
  239. else {
  240. printf("Unknown request\n");
  241. msgbuf = printable(buf, 0);
  242. if(msgbuf) {
  243. logmsg("invalid input: '%s'\n", msgbuf);
  244. free(msgbuf);
  245. }
  246. else
  247. logmsg("OOM formatting invalid input: '%s'\n", buf);
  248. exit(1);
  249. }
  250. }
  251. return 1;
  252. }