xmlrpc_parse.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /* Copyright (C) 2001 by First Peer, Inc. 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 <stddef.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <errno.h>
  30. #include <ctype.h>
  31. #include "xmlrpc.h"
  32. #include "xmlrpc_int.h"
  33. #include "xmlrpc_xmlparser.h"
  34. /*=========================================================================
  35. ** Data Format
  36. **=========================================================================
  37. ** All XML-RPC documents contain a single methodCall or methodResponse
  38. ** element.
  39. **
  40. ** methodCall methodName, params
  41. ** methodResponse (params|fault)
  42. ** params param*
  43. ** param value
  44. ** fault value
  45. ** value (i4|int|boolean|string|double|dateTime.iso8601|base64|
  46. ** struct|array)
  47. ** array data
  48. ** data value*
  49. ** struct member*
  50. ** member name, value
  51. **
  52. ** Contain CDATA: methodName, i4, int, boolean, string, double,
  53. ** dateTime.iso8601, base64, name
  54. **
  55. ** We attempt to validate the structure of the XML document carefully.
  56. ** We also try *very* hard to handle malicious data gracefully, and without
  57. ** leaking memory.
  58. **
  59. ** The CHECK_NAME and CHECK_CHILD_COUNT macros examine an XML element, and
  60. ** invoke XMLRPC_FAIL if something looks wrong.
  61. */
  62. #define CHECK_NAME(env,elem,name) \
  63. do \
  64. if (strcmp((name), xml_element_name(elem)) != 0) \
  65. XMLRPC_FAIL2(env, XMLRPC_PARSE_ERROR, \
  66. "Expected element of type <%s>, found <%s>", \
  67. (name), xml_element_name(elem)); \
  68. while (0)
  69. #define CHECK_CHILD_COUNT(env,elem,count) \
  70. do \
  71. if (xml_element_children_size(elem) != (count)) \
  72. XMLRPC_FAIL3(env, XMLRPC_PARSE_ERROR, \
  73. "Expected <%s> to have %d children, found %d", \
  74. xml_element_name(elem), (count), \
  75. xml_element_children_size(elem)); \
  76. while (0)
  77. static xml_element *
  78. get_child_by_name (xmlrpc_env *env, xml_element *parent, char *name)
  79. {
  80. size_t child_count, i;
  81. xml_element **children;
  82. children = xml_element_children(parent);
  83. child_count = xml_element_children_size(parent);
  84. for (i = 0; i < child_count; i++) {
  85. if (0 == strcmp(xml_element_name(children[i]), name))
  86. return children[i];
  87. }
  88. xmlrpc_env_set_fault_formatted(env, XMLRPC_PARSE_ERROR,
  89. "Expected <%s> to have child <%s>",
  90. xml_element_name(parent), name);
  91. return NULL;
  92. }
  93. /*=========================================================================
  94. ** Number-Parsing Functions
  95. **=========================================================================
  96. ** These functions mirror atoi, atof, etc., but provide better
  97. ** error-handling. These routines may reset errno to zero.
  98. */
  99. static xmlrpc_int32
  100. xmlrpc_atoi(xmlrpc_env *env, char *str, size_t stringLength,
  101. xmlrpc_int32 min, xmlrpc_int32 max)
  102. {
  103. long i;
  104. char *end;
  105. XMLRPC_ASSERT_ENV_OK(env);
  106. XMLRPC_ASSERT_PTR_OK(str);
  107. /* Suppress compiler warnings. */
  108. i = 0;
  109. /* Check for leading white space. */
  110. if (isspace((int)(str[0])))
  111. XMLRPC_FAIL1(env, XMLRPC_PARSE_ERROR,
  112. "\"%s\" must not contain whitespace", str);
  113. /* Convert the value. */
  114. end = str + stringLength;
  115. errno = 0;
  116. i = strtol(str, &end, 10);
  117. /* Look for ERANGE. */
  118. if (errno != 0)
  119. /* XXX - Do all operating systems have thread-safe strerror? */
  120. XMLRPC_FAIL3(env, XMLRPC_PARSE_ERROR,
  121. "error parsing \"%s\": %s (%d)",
  122. str, strerror(errno), errno);
  123. /* Look for out-of-range errors which didn't produce ERANGE. */
  124. if (i < min || i > max)
  125. XMLRPC_FAIL3(env, XMLRPC_PARSE_ERROR,
  126. "\"%s\" must be in range %d to %d", str, min, max);
  127. /* Check for unused characters. */
  128. if (end != str + stringLength)
  129. XMLRPC_FAIL1(env, XMLRPC_PARSE_ERROR,
  130. "\"%s\" contained trailing data", str);
  131. cleanup:
  132. errno = 0;
  133. if (env->fault_occurred)
  134. return 0;
  135. return (xmlrpc_int32) i;
  136. }
  137. static double
  138. xmlrpc_atod(xmlrpc_env *env, char *str, size_t stringLength)
  139. {
  140. double d;
  141. char *end;
  142. XMLRPC_ASSERT_ENV_OK(env);
  143. XMLRPC_ASSERT_PTR_OK(str);
  144. /* Suppress compiler warnings. */
  145. d = 0.0;
  146. /* Check for leading white space. */
  147. if (isspace((int)(str[0])))
  148. XMLRPC_FAIL1(env, XMLRPC_PARSE_ERROR,
  149. "\"%s\" must not contain whitespace", str);
  150. /* Convert the value. */
  151. end = str + stringLength;
  152. errno = 0;
  153. d = strtod(str, &end);
  154. /* Look for ERANGE. */
  155. if (errno != 0)
  156. /* XXX - Do all operating systems have thread-safe strerror? */
  157. XMLRPC_FAIL3(env, XMLRPC_PARSE_ERROR,
  158. "error parsing \"%s\": %s (%d)",
  159. str, strerror(errno), errno);
  160. /* Check for unused characters. */
  161. if (end != str + stringLength)
  162. XMLRPC_FAIL1(env, XMLRPC_PARSE_ERROR,
  163. "\"%s\" contained trailing data", str);
  164. cleanup:
  165. errno = 0;
  166. if (env->fault_occurred)
  167. return 0.0;
  168. return d;
  169. }
  170. /*=========================================================================
  171. ** make_string
  172. **=========================================================================
  173. ** Make an XML-RPC string.
  174. **
  175. ** SECURITY: We validate our UTF-8 first. This incurs a performance
  176. ** penalty, but ensures that we will never pass maliciously malformed
  177. ** UTF-8 data back up to the user layer, where it could wreak untold
  178. ** damange. Don't comment out this check unless you know *exactly* what
  179. ** you're doing. (Win32 developers who remove this check are *begging*
  180. ** to wind up on BugTraq, because many of the Win32 filesystem routines
  181. ** rely on an insecure UTF-8 decoder.)
  182. **
  183. ** XXX - This validation is redundant if the user chooses to convert
  184. ** UTF-8 data into a wchar_t string.
  185. */
  186. static xmlrpc_value *
  187. make_string(xmlrpc_env *env, char *cdata, size_t cdata_size)
  188. {
  189. #ifdef HAVE_UNICODE_WCHAR
  190. xmlrpc_validate_utf8(env, cdata, cdata_size);
  191. #endif
  192. if (env->fault_occurred)
  193. return NULL;
  194. return xmlrpc_build_value(env, "s#", cdata, cdata_size);
  195. }
  196. /*=========================================================================
  197. ** convert_value
  198. **=========================================================================
  199. ** Convert an XML element representing a value into an xmlrpc_value.
  200. */
  201. static xmlrpc_value *
  202. convert_array (xmlrpc_env *env, unsigned *depth, xml_element *elem);
  203. static xmlrpc_value *
  204. convert_struct(xmlrpc_env *env, unsigned *depth, xml_element *elem);
  205. static xmlrpc_value *
  206. convert_value(xmlrpc_env *env, unsigned *depth, xml_element *elem)
  207. {
  208. xml_element *child;
  209. int child_count;
  210. char *cdata, *child_name;
  211. size_t cdata_size, ascii_len;
  212. xmlrpc_mem_block *decoded;
  213. unsigned char *ascii_data;
  214. xmlrpc_value *retval;
  215. xmlrpc_int32 i;
  216. double d;
  217. XMLRPC_ASSERT_ENV_OK(env);
  218. XMLRPC_ASSERT(elem != NULL);
  219. /* Error-handling precoditions.
  220. ** If we haven't changed any of these from their default state, we're
  221. ** allowed to tail-call xmlrpc_build_value. */
  222. retval = NULL;
  223. decoded = NULL;
  224. /* Make sure we haven't recursed too deeply. */
  225. if (*depth > xmlrpc_limit_get(XMLRPC_NESTING_LIMIT_ID))
  226. XMLRPC_FAIL(env, XMLRPC_PARSE_ERROR,
  227. "Nested data structure too deep.");
  228. /* Validate our structure, and see whether we have a child element. */
  229. CHECK_NAME(env, elem, "value");
  230. child_count = xml_element_children_size(elem);
  231. if (child_count == 0) {
  232. /* We have no type element, so treat the value as a string. */
  233. cdata = xml_element_cdata(elem);
  234. cdata_size = xml_element_cdata_size(elem);
  235. return make_string(env, cdata, cdata_size);
  236. } else {
  237. /* We should have a type tag inside our value tag. */
  238. CHECK_CHILD_COUNT(env, elem, 1);
  239. child = xml_element_children(elem)[0];
  240. /* Parse our value-containing element. */
  241. child_name = xml_element_name(child);
  242. if (strcmp(child_name, "struct") == 0) {
  243. return convert_struct(env, depth, child);
  244. } else if (strcmp(child_name, "array") == 0) {
  245. CHECK_CHILD_COUNT(env, child, 1);
  246. return convert_array(env, depth, child);
  247. } else {
  248. CHECK_CHILD_COUNT(env, child, 0);
  249. cdata = xml_element_cdata(child);
  250. cdata_size = xml_element_cdata_size(child);
  251. if (strcmp(child_name, "i4") == 0 ||
  252. strcmp(child_name, "int") == 0)
  253. {
  254. i = xmlrpc_atoi(env, cdata, strlen(cdata),
  255. XMLRPC_INT32_MIN, XMLRPC_INT32_MAX);
  256. XMLRPC_FAIL_IF_FAULT(env);
  257. return xmlrpc_build_value(env, "i", i);
  258. } else if (strcmp(child_name, "string") == 0) {
  259. return make_string(env, cdata, cdata_size);
  260. } else if (strcmp(child_name, "boolean") == 0) {
  261. i = xmlrpc_atoi(env, cdata, strlen(cdata), 0, 1);
  262. XMLRPC_FAIL_IF_FAULT(env);
  263. return xmlrpc_build_value(env, "b", (xmlrpc_bool) i);
  264. } else if (strcmp(child_name, "double") == 0) {
  265. d = xmlrpc_atod(env, cdata, strlen(cdata));
  266. XMLRPC_FAIL_IF_FAULT(env);
  267. return xmlrpc_build_value(env, "d", d);
  268. } else if (strcmp(child_name, "dateTime.iso8601") == 0) {
  269. return xmlrpc_build_value(env, "8", cdata);
  270. } else if (strcmp(child_name, "base64") == 0) {
  271. /* No more tail calls once we do this! */
  272. decoded = xmlrpc_base64_decode(env, cdata, cdata_size);
  273. XMLRPC_FAIL_IF_FAULT(env);
  274. ascii_data = XMLRPC_TYPED_MEM_BLOCK_CONTENTS(unsigned char,
  275. decoded);
  276. ascii_len = XMLRPC_TYPED_MEM_BLOCK_SIZE(unsigned char,
  277. decoded);
  278. retval = xmlrpc_build_value(env, "6", ascii_data, ascii_len);
  279. XMLRPC_FAIL_IF_FAULT(env);
  280. } else {
  281. XMLRPC_FAIL1(env, XMLRPC_PARSE_ERROR,
  282. "Unknown value type <%s>", child_name);
  283. }
  284. }
  285. }
  286. cleanup:
  287. if (decoded)
  288. xmlrpc_mem_block_free(decoded);
  289. if (env->fault_occurred) {
  290. if (retval)
  291. xmlrpc_DECREF(retval);
  292. return NULL;
  293. }
  294. return retval;
  295. }
  296. /*=========================================================================
  297. ** convert_array
  298. **=========================================================================
  299. ** Convert an XML element representing an array into an xmlrpc_value.
  300. */
  301. static xmlrpc_value *
  302. convert_array(xmlrpc_env *env, unsigned *depth, xml_element *elem)
  303. {
  304. xml_element *data, **values, *value;
  305. xmlrpc_value *array, *item;
  306. int size, i;
  307. XMLRPC_ASSERT_ENV_OK(env);
  308. XMLRPC_ASSERT(elem != NULL);
  309. /* Set up our error-handling preconditions. */
  310. item = NULL;
  311. (*depth)++;
  312. /* Allocate an array to hold our values. */
  313. array = xmlrpc_build_value(env, "()");
  314. XMLRPC_FAIL_IF_FAULT(env);
  315. /* We don't need to check our element name--our callers do that. */
  316. CHECK_CHILD_COUNT(env, elem, 1);
  317. data = xml_element_children(elem)[0];
  318. CHECK_NAME(env, data, "data");
  319. /* Iterate over our children. */
  320. values = xml_element_children(data);
  321. size = xml_element_children_size(data);
  322. for (i = 0; i < size; i++) {
  323. value = values[i];
  324. item = convert_value(env, depth, value);
  325. XMLRPC_FAIL_IF_FAULT(env);
  326. xmlrpc_array_append_item(env, array, item);
  327. xmlrpc_DECREF(item);
  328. item = NULL;
  329. XMLRPC_FAIL_IF_FAULT(env);
  330. }
  331. cleanup:
  332. (*depth)--;
  333. if (item)
  334. xmlrpc_DECREF(item);
  335. if (env->fault_occurred) {
  336. if (array)
  337. xmlrpc_DECREF(array);
  338. return NULL;
  339. }
  340. return array;
  341. }
  342. /*=========================================================================
  343. ** convert_struct
  344. **=========================================================================
  345. ** Convert an XML element representing a struct into an xmlrpc_value.
  346. */
  347. static xmlrpc_value *
  348. convert_struct(xmlrpc_env *env, unsigned *depth, xml_element *elem)
  349. {
  350. xmlrpc_value *strct, *key, *value;
  351. xml_element **members, *member, *name_elem, *value_elem;
  352. int size, i;
  353. char *cdata;
  354. size_t cdata_size;
  355. XMLRPC_ASSERT_ENV_OK(env);
  356. XMLRPC_ASSERT(elem != NULL);
  357. /* Set up our error-handling preconditions. */
  358. key = value = NULL;
  359. (*depth)++;
  360. /* Allocate an array to hold our members. */
  361. strct = xmlrpc_struct_new(env);
  362. XMLRPC_FAIL_IF_FAULT(env);
  363. /* Iterate over our children, extracting key/value pairs. */
  364. /* We don't need to check our element name--our callers do that. */
  365. members = xml_element_children(elem);
  366. size = xml_element_children_size(elem);
  367. for (i = 0; i < size; i++) {
  368. member = members[i];
  369. CHECK_NAME(env, member, "member");
  370. CHECK_CHILD_COUNT(env, member, 2);
  371. /* Get our key. */
  372. name_elem = get_child_by_name(env, member, "name");
  373. XMLRPC_FAIL_IF_FAULT(env);
  374. CHECK_CHILD_COUNT(env, name_elem, 0);
  375. cdata = xml_element_cdata(name_elem);
  376. cdata_size = xml_element_cdata_size(name_elem);
  377. key = make_string(env, cdata, cdata_size);
  378. XMLRPC_FAIL_IF_FAULT(env);
  379. /* Get our value. */
  380. value_elem = get_child_by_name(env, member, "value");
  381. XMLRPC_FAIL_IF_FAULT(env);
  382. value = convert_value(env, depth, value_elem);
  383. XMLRPC_FAIL_IF_FAULT(env);
  384. /* Add the key/value pair to our struct. */
  385. xmlrpc_struct_set_value_v(env, strct, key, value);
  386. XMLRPC_FAIL_IF_FAULT(env);
  387. /* Release our references & memory, and restore our invariants. */
  388. xmlrpc_DECREF(key);
  389. key = NULL;
  390. xmlrpc_DECREF(value);
  391. value = NULL;
  392. }
  393. cleanup:
  394. (*depth)--;
  395. if (key)
  396. xmlrpc_DECREF(key);
  397. if (value)
  398. xmlrpc_DECREF(value);
  399. if (env->fault_occurred) {
  400. if (strct)
  401. xmlrpc_DECREF(strct);
  402. return NULL;
  403. }
  404. return strct;
  405. }
  406. /*=========================================================================
  407. ** convert_params
  408. **=========================================================================
  409. ** Convert an XML element representing a list of params into an
  410. ** xmlrpc_value (of type array).
  411. */
  412. static xmlrpc_value *
  413. convert_params(xmlrpc_env *env, unsigned *depth, xml_element *elem)
  414. {
  415. xmlrpc_value *array, *item;
  416. int size, i;
  417. xml_element **params, *param, *value;
  418. XMLRPC_ASSERT_ENV_OK(env);
  419. XMLRPC_ASSERT(elem != NULL);
  420. /* Set up our error-handling preconditions. */
  421. item = NULL;
  422. /* Allocate an array to hold our parameters. */
  423. array = xmlrpc_build_value(env, "()");
  424. XMLRPC_FAIL_IF_FAULT(env);
  425. /* We're responsible for checking our own element name. */
  426. CHECK_NAME(env, elem, "params");
  427. /* Iterate over our children. */
  428. size = xml_element_children_size(elem);
  429. params = xml_element_children(elem);
  430. for (i = 0; i < size; i++) {
  431. param = params[i];
  432. CHECK_NAME(env, param, "param");
  433. CHECK_CHILD_COUNT(env, param, 1);
  434. value = xml_element_children(param)[0];
  435. item = convert_value(env, depth, value);
  436. XMLRPC_FAIL_IF_FAULT(env);
  437. xmlrpc_array_append_item(env, array, item);
  438. xmlrpc_DECREF(item);
  439. item = NULL;
  440. XMLRPC_FAIL_IF_FAULT(env);
  441. }
  442. cleanup:
  443. if (env->fault_occurred) {
  444. if (array)
  445. xmlrpc_DECREF(array);
  446. if (item)
  447. xmlrpc_DECREF(item);
  448. return NULL;
  449. }
  450. return array;
  451. }
  452. static void
  453. parseCallXml(xmlrpc_env * const envP,
  454. const char * const xmlData,
  455. size_t const xmlLen,
  456. xml_element ** const callElemP) {
  457. xmlrpc_env env;
  458. xmlrpc_env_init(&env);
  459. *callElemP = xml_parse(&env, xmlData, xmlLen);
  460. if (env.fault_occurred)
  461. xmlrpc_env_set_fault_formatted(
  462. envP, env.fault_code, "Call is not valid XML. %s",
  463. env.fault_string);
  464. xmlrpc_env_clean(&env);
  465. }
  466. /*=========================================================================
  467. ** xmlrpc_parse_call
  468. **=========================================================================
  469. ** Given some XML text, attempt to parse it as an XML-RPC call. Return
  470. ** a newly allocated xmlrpc_call structure (or NULL, if an error occurs).
  471. ** The two output variables will contain either valid values (which
  472. ** must free() and xmlrpc_DECREF(), respectively) or NULLs (if an error
  473. ** occurs).
  474. */
  475. void
  476. xmlrpc_parse_call(xmlrpc_env * const envP,
  477. const char * const xml_data,
  478. size_t const xml_len,
  479. const char ** const out_method_nameP,
  480. xmlrpc_value ** const out_param_arrayPP) {
  481. xml_element *call_elem, *name_elem, *params_elem;
  482. char *cdata;
  483. unsigned depth;
  484. size_t call_child_count;
  485. char * outMethodName;
  486. xmlrpc_value * outParamArrayP;
  487. XMLRPC_ASSERT_ENV_OK(envP);
  488. XMLRPC_ASSERT(xml_data != NULL);
  489. XMLRPC_ASSERT(out_method_nameP != NULL && out_param_arrayPP != NULL);
  490. /* Set up our error-handling preconditions. */
  491. outMethodName = NULL;
  492. outParamArrayP = NULL;
  493. call_elem = NULL;
  494. /* SECURITY: Last-ditch attempt to make sure our content length is legal.
  495. ** XXX - This check occurs too late to prevent an attacker from creating
  496. ** an enormous memory block in RAM, so you should try to enforce it
  497. ** *before* reading any data off the network. */
  498. if (xml_len > xmlrpc_limit_get(XMLRPC_XML_SIZE_LIMIT_ID))
  499. XMLRPC_FAIL(envP, XMLRPC_LIMIT_EXCEEDED_ERROR,
  500. "XML-RPC request too large");
  501. parseCallXml(envP, xml_data, xml_len, &call_elem);
  502. XMLRPC_FAIL_IF_FAULT(envP);
  503. /* Pick apart and verify our structure. */
  504. CHECK_NAME(envP, call_elem, "methodCall");
  505. call_child_count = xml_element_children_size(call_elem);
  506. if (call_child_count != 2 && call_child_count != 1)
  507. XMLRPC_FAIL1(envP, XMLRPC_PARSE_ERROR,
  508. "Expected <methodCall> to have 1 or 2 children, found %d",
  509. call_child_count);
  510. /* Extract the method name.
  511. ** SECURITY: We make sure the method name is valid UTF-8. */
  512. name_elem = get_child_by_name(envP, call_elem, "methodName");
  513. XMLRPC_FAIL_IF_FAULT(envP);
  514. CHECK_CHILD_COUNT(envP, name_elem, 0);
  515. cdata = xml_element_cdata(name_elem);
  516. #ifdef HAVE_UNICODE_WCHAR
  517. xmlrpc_validate_utf8(envP, cdata, strlen(cdata));
  518. XMLRPC_FAIL_IF_FAULT(envP);
  519. #endif /* HAVE_UNICODE_WCHAR */
  520. outMethodName = malloc(strlen(cdata) + 1);
  521. XMLRPC_FAIL_IF_NULL(outMethodName, envP, XMLRPC_INTERNAL_ERROR,
  522. "Could not allocate memory for method name");
  523. strcpy(outMethodName, cdata);
  524. /* Convert our parameters. */
  525. if (call_child_count == 1) {
  526. /* Workaround for Ruby XML-RPC and old versions of xmlrpc-epi. */
  527. outParamArrayP = xmlrpc_build_value(envP, "()");
  528. XMLRPC_FAIL_IF_FAULT(envP);
  529. } else {
  530. params_elem = get_child_by_name(envP, call_elem, "params");
  531. XMLRPC_FAIL_IF_FAULT(envP);
  532. depth = 0;
  533. outParamArrayP = convert_params(envP, &depth, params_elem);
  534. XMLRPC_ASSERT(depth == 0);
  535. XMLRPC_FAIL_IF_FAULT(envP);
  536. }
  537. cleanup:
  538. if (call_elem)
  539. xml_element_free(call_elem);
  540. if (envP->fault_occurred) {
  541. if (outMethodName)
  542. free(outMethodName);
  543. if (outParamArrayP)
  544. xmlrpc_DECREF(outParamArrayP);
  545. outMethodName = NULL;
  546. outParamArrayP = NULL;
  547. }
  548. *out_method_nameP = outMethodName;
  549. *out_param_arrayPP = outParamArrayP;
  550. }
  551. /*=========================================================================
  552. ** xmlrpc_parse_response
  553. **=========================================================================
  554. ** Given some XML text, attempt to parse it as an XML-RPC response.
  555. ** If the response is a regular, valid response, return a new reference
  556. ** to the appropriate value. If the response is a fault, or an error
  557. ** occurs during processing, return NULL and set up env appropriately.
  558. */
  559. xmlrpc_value *
  560. xmlrpc_parse_response(xmlrpc_env *env,
  561. const char *xml_data,
  562. size_t xml_len) {
  563. xml_element *response, *child, *value;
  564. unsigned depth;
  565. xmlrpc_value *params, *retval, *fault;
  566. int retval_incremented;
  567. xmlrpc_value *fault_code_value, *fault_str_value;
  568. xmlrpc_int32 fault_code;
  569. char *fault_str;
  570. XMLRPC_ASSERT_ENV_OK(env);
  571. XMLRPC_ASSERT(xml_data != NULL);
  572. /* Set up our error-handling preconditions. */
  573. response = NULL;
  574. params = fault = NULL;
  575. retval_incremented = 0;
  576. /* SECURITY: Last-ditch attempt to make sure our content length is legal.
  577. ** XXX - This check occurs too late to prevent an attacker from creating
  578. ** an enormous memory block in RAM, so you should try to enforce it
  579. ** *before* reading any data off the network. */
  580. if (xml_len > xmlrpc_limit_get(XMLRPC_XML_SIZE_LIMIT_ID))
  581. XMLRPC_FAIL(env, XMLRPC_LIMIT_EXCEEDED_ERROR,
  582. "XML-RPC response too large");
  583. /* SECURITY: Set up our recursion depth counter. */
  584. depth = 0;
  585. /* Parse our XML data. */
  586. response = xml_parse(env, xml_data, xml_len);
  587. XMLRPC_FAIL_IF_FAULT(env);
  588. /* Pick apart and verify our structure. */
  589. CHECK_NAME(env, response, "methodResponse");
  590. CHECK_CHILD_COUNT(env, response, 1);
  591. child = xml_element_children(response)[0];
  592. /* Parse the response itself. */
  593. if (strcmp("params", xml_element_name(child)) == 0) {
  594. /* Convert our parameter list. */
  595. params = convert_params(env, &depth, child);
  596. XMLRPC_FAIL_IF_FAULT(env);
  597. /* Extract the return value, and jiggle our reference counts. */
  598. xmlrpc_parse_value(env, params, "(V)", &retval);
  599. XMLRPC_FAIL_IF_FAULT(env);
  600. xmlrpc_INCREF(retval);
  601. retval_incremented = 1;
  602. } else if (strcmp("fault", xml_element_name(child)) == 0) {
  603. /* Convert our fault structure. */
  604. CHECK_CHILD_COUNT(env, child, 1);
  605. value = xml_element_children(child)[0];
  606. fault = convert_value(env, &depth, value);
  607. XMLRPC_FAIL_IF_FAULT(env);
  608. XMLRPC_TYPE_CHECK(env, fault, XMLRPC_TYPE_STRUCT);
  609. /* Get our fault code. */
  610. fault_code_value = xmlrpc_struct_get_value(env, fault, "faultCode");
  611. XMLRPC_FAIL_IF_FAULT(env);
  612. xmlrpc_parse_value(env, fault_code_value, "i", &fault_code);
  613. XMLRPC_FAIL_IF_FAULT(env);
  614. /* Get our fault string. */
  615. fault_str_value = xmlrpc_struct_get_value(env, fault, "faultString");
  616. XMLRPC_FAIL_IF_FAULT(env);
  617. xmlrpc_parse_value(env, fault_str_value, "s", &fault_str);
  618. XMLRPC_FAIL_IF_FAULT(env);
  619. /* Return our fault. */
  620. XMLRPC_FAIL(env, fault_code, fault_str);
  621. } else {
  622. XMLRPC_FAIL(env, XMLRPC_PARSE_ERROR,
  623. "Expected <params> or <fault> in <methodResponse>");
  624. }
  625. /* Sanity-check our depth-counting code. */
  626. XMLRPC_ASSERT(depth == 0);
  627. cleanup:
  628. if (response)
  629. xml_element_free(response);
  630. if (params)
  631. xmlrpc_DECREF(params);
  632. if (fault)
  633. xmlrpc_DECREF(fault);
  634. if (env->fault_occurred) {
  635. if (retval_incremented)
  636. xmlrpc_DECREF(retval);
  637. return NULL;
  638. }
  639. return retval;
  640. }