xmlrpc_cgi.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* Copyright (C) 2001 by Eric Kidd. All rights reserved.
  2. **
  3. ** Redistribution and use in source and binary forms, with or without
  4. ** modification, are permitted provided that the following conditions
  5. ** are met:
  6. ** 1. Redistributions of source code must retain the above copyright
  7. ** notice, this list of conditions and the following disclaimer.
  8. ** 2. Redistributions in binary form must reproduce the above copyright
  9. ** notice, this list of conditions and the following disclaimer in the
  10. ** documentation and/or other materials provided with the distribution.
  11. ** 3. The name of the author may not be used to endorse or promote products
  12. ** derived from this software without specific prior written permission.
  13. **
  14. ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. ** SUCH DAMAGE. */
  25. #include "xmlrpc_config.h"
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. /* Windows NT stdout binary mode fix. */
  30. #ifdef _WIN32
  31. #include <io.h>
  32. #include <fcntl.h>
  33. #endif
  34. #include "xmlrpc.h"
  35. #include "xmlrpc_server.h"
  36. #include "xmlrpc_cgi.h"
  37. /*=========================================================================
  38. ** Output Routines
  39. **=========================================================================
  40. ** These routines send various kinds of responses to the server.
  41. */
  42. static void send_xml (char *xml_data, size_t xml_len)
  43. {
  44. /* Send our CGI headers back to the server.
  45. ** XXX - Coercing 'size_t' to 'unsigned long' might be unsafe under
  46. ** really weird circumstances. */
  47. fprintf(stdout, "Status: 200 OK\n");
  48. /* Handle authentication cookie being sent back. */
  49. if (getenv("HTTP_COOKIE_AUTH") != NULL)
  50. fprintf(stdout, "Set-Cookie: auth=%s\n", getenv("HTTP_COOKIE_AUTH"));
  51. fprintf(stdout, "Content-type: text/xml; charset=\"utf-8\"\n");
  52. fprintf(stdout, "Content-length: %ld\n\n", (unsigned long) xml_len);
  53. /* Blast out our data. */
  54. fwrite(xml_data, sizeof(char), xml_len, stdout);
  55. }
  56. static void send_error (int code, char *message, xmlrpc_env *env)
  57. {
  58. /* Send an error header. */
  59. fprintf(stdout, "Status: %d %s\n", code, message);
  60. fprintf(stdout, "Content-type: text/html\n\n");
  61. /* Send an error message. */
  62. fprintf(stdout, "<title>%d %s</title>\n", code, message);
  63. fprintf(stdout, "<h1>%d %s</h1>\n", code, message);
  64. fprintf(stdout, "<p>An error occurred processing your request.</p>\n");
  65. /* Print out the XML-RPC fault, if present. */
  66. if (env && env->fault_occurred)
  67. fprintf(stdout, "<p>XML-RPC Fault #%d: %s</p>\n",
  68. env->fault_code, env->fault_string);
  69. }
  70. /*=========================================================================
  71. ** die_if_fault_occurred
  72. **=========================================================================
  73. ** Certain kinds of errors aren't worth the trouble of generating
  74. ** an XML-RPC fault. For these, we just send status 500 to our web server
  75. ** and log the fault to our server log.
  76. */
  77. static void die_if_fault_occurred (xmlrpc_env *env)
  78. {
  79. if (env->fault_occurred) {
  80. fprintf(stderr, "Unexpected XML-RPC fault: %s (%d)\n",
  81. env->fault_string, env->fault_code);
  82. send_error(500, "Internal Server Error", env);
  83. exit(1);
  84. }
  85. }
  86. /*=========================================================================
  87. ** Initialization, Cleanup & Method Registry
  88. **=========================================================================
  89. ** These are all related, so we group them together.
  90. */
  91. static xmlrpc_registry *registry;
  92. void xmlrpc_cgi_init (int flags ATTR_UNUSED)
  93. {
  94. xmlrpc_env env;
  95. xmlrpc_env_init(&env);
  96. registry = xmlrpc_registry_new(&env);
  97. die_if_fault_occurred(&env);
  98. xmlrpc_env_clean(&env);
  99. #ifdef _WIN32
  100. /* Fix from Jeff Stewart: NT opens stdin and stdout in text mode
  101. ** by default, badly confusing our length calculations. So we need
  102. ** to set these file handles to binary. */
  103. _setmode(_fileno(stdout), _O_BINARY);
  104. _setmode(_fileno(stdin), _O_BINARY);
  105. #endif
  106. }
  107. void xmlrpc_cgi_cleanup (void)
  108. {
  109. xmlrpc_registry_free(registry);
  110. }
  111. xmlrpc_registry *xmlrpc_cgi_registry (void)
  112. {
  113. return registry;
  114. }
  115. void xmlrpc_cgi_add_method (char *method_name,
  116. xmlrpc_method method,
  117. void *user_data)
  118. {
  119. xmlrpc_env env;
  120. xmlrpc_env_init(&env);
  121. xmlrpc_registry_add_method(&env, registry, NULL, method_name,
  122. method, user_data);
  123. die_if_fault_occurred(&env);
  124. xmlrpc_env_clean(&env);
  125. }
  126. extern void
  127. xmlrpc_cgi_add_method_w_doc (char *method_name,
  128. xmlrpc_method method,
  129. void *user_data,
  130. char *signature,
  131. char *help)
  132. {
  133. xmlrpc_env env;
  134. xmlrpc_env_init(&env);
  135. xmlrpc_registry_add_method_w_doc(&env, registry, NULL, method_name,
  136. method, user_data, signature, help);
  137. die_if_fault_occurred(&env);
  138. xmlrpc_env_clean(&env);
  139. }
  140. /*=========================================================================
  141. ** get_body
  142. **=========================================================================
  143. ** Slurp the body of the request into an xmlrpc_mem_block.
  144. */
  145. static xmlrpc_mem_block *get_body (xmlrpc_env *env, size_t length)
  146. {
  147. xmlrpc_mem_block *result;
  148. char *contents;
  149. size_t count;
  150. XMLRPC_ASSERT_ENV_OK(env);
  151. /* Error-handling preconditions. */
  152. result = NULL;
  153. /* XXX - Puke if length is too big. */
  154. /* Allocate our memory block. */
  155. result = xmlrpc_mem_block_new(env, length);
  156. XMLRPC_FAIL_IF_FAULT(env);
  157. contents = XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, result);
  158. /* Get our data off the network.
  159. ** XXX - Coercing 'size_t' to 'unsigned long' might be unsafe under
  160. ** really weird circumstances. */
  161. count = fread(contents, sizeof(char), length, stdin);
  162. if (count < length)
  163. XMLRPC_FAIL2(env, XMLRPC_INTERNAL_ERROR,
  164. "Expected %ld bytes, received %ld",
  165. (unsigned long) length, (unsigned long) count);
  166. cleanup:
  167. if (env->fault_occurred) {
  168. if (result)
  169. xmlrpc_mem_block_free(result);
  170. return NULL;
  171. }
  172. return result;
  173. }
  174. /*=========================================================================
  175. ** xmlrpc_cgi_process_call
  176. **=========================================================================
  177. ** Parse the incoming XML-RPC call, find the right method, call it, and
  178. ** serialize our response.
  179. */
  180. void xmlrpc_cgi_process_call (void)
  181. {
  182. xmlrpc_env env;
  183. char *method, *type, *length_str;
  184. int length;
  185. xmlrpc_mem_block *input, *output;
  186. char *input_data, *output_data;
  187. size_t input_size, output_size;
  188. int code;
  189. char *message;
  190. /* Error-handling preconditions. */
  191. xmlrpc_env_init(&env);
  192. input = output = NULL;
  193. /* Set up a default error message. */
  194. code = 500; message = "Internal Server Error";
  195. /* Get our HTTP information from the environment. */
  196. method = getenv("REQUEST_METHOD");
  197. type = getenv("CONTENT_TYPE");
  198. length_str = getenv("CONTENT_LENGTH");
  199. /* Perform some sanity checks. */
  200. if (!method || 0 != strcmp(method, "POST")) {
  201. code = 405; message = "Method Not Allowed";
  202. XMLRPC_FAIL(&env, XMLRPC_INTERNAL_ERROR, "Expected HTTP method POST");
  203. }
  204. if (!type || 0 != strcmp(type, "text/xml")) {
  205. code = 400; message = "Bad Request";
  206. XMLRPC_FAIL(&env, XMLRPC_INTERNAL_ERROR, "Expected text/xml content");
  207. }
  208. if (!length_str) {
  209. code = 411; message = "Length Required";
  210. XMLRPC_FAIL(&env, XMLRPC_INTERNAL_ERROR, "Content-length required");
  211. }
  212. /* Get our content length. */
  213. length = atoi(length_str);
  214. if (length <= 0) {
  215. code = 400; message = "Bad Request";
  216. XMLRPC_FAIL(&env, XMLRPC_INTERNAL_ERROR, "Content-length must be > 0");
  217. }
  218. /* SECURITY: Make sure our content length is legal.
  219. ** XXX - We can cast 'input_len' because we know it's >= 0, yes? */
  220. if ((size_t) length > xmlrpc_limit_get(XMLRPC_XML_SIZE_LIMIT_ID)) {
  221. code = 400; message = "Bad Request";
  222. XMLRPC_FAIL(&env, XMLRPC_LIMIT_EXCEEDED_ERROR,
  223. "XML-RPC request too large");
  224. }
  225. /* Get our body. */
  226. input = get_body(&env, length);
  227. XMLRPC_FAIL_IF_FAULT(&env);
  228. input_data = XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, input);
  229. input_size = XMLRPC_TYPED_MEM_BLOCK_SIZE(char, input);
  230. /* Process our call. */
  231. output = xmlrpc_registry_process_call(&env, registry, NULL,
  232. input_data, input_size);
  233. XMLRPC_FAIL_IF_FAULT(&env);
  234. output_data = XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, output);
  235. output_size = XMLRPC_TYPED_MEM_BLOCK_SIZE(char, output);
  236. /* Send our data. */
  237. send_xml(output_data, output_size);
  238. cleanup:
  239. if (input)
  240. xmlrpc_mem_block_free(input);
  241. if (output)
  242. xmlrpc_mem_block_free(output);
  243. if (env.fault_occurred)
  244. send_error(code, message, &env);
  245. xmlrpc_env_clean(&env);
  246. }