fake_ntlm.c 7.4 KB

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