dbus_new_introspect.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * WPA Supplicant / dbus-based control interface
  3. * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
  4. * Copyright (c) 2009, Witold Sowa <witold.sowa@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Alternatively, this software may be distributed under the terms of BSD
  11. * license.
  12. *
  13. * See README and COPYING for more details.
  14. */
  15. #include "utils/includes.h"
  16. #include <libxml/tree.h>
  17. #include "utils/common.h"
  18. #include "dbus_common_i.h"
  19. #include "dbus_new_helpers.h"
  20. struct interfaces {
  21. struct interfaces *next;
  22. char *dbus_interface;
  23. xmlNodePtr interface_node;
  24. };
  25. /**
  26. * extract_interfaces - Extract interfaces from methods, signals and props
  27. * @obj_dsc: Description of object from which interfaces will be extracted
  28. * @root_node: root node of XML introspection document
  29. * Returns: List of interfaces found in object description
  30. *
  31. * Iterates over all methods, signals and properties registered with
  32. * object and collects all declared DBus interfaces and create interface's
  33. * node in XML root node for each. Returned list elements contains interface
  34. * name and XML node of corresponding interface.
  35. */
  36. static struct interfaces * extract_interfaces(
  37. struct wpa_dbus_object_desc *obj_dsc, xmlNodePtr root_node)
  38. {
  39. struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
  40. struct wpa_dbus_signal_desc *signal_dsc = obj_dsc->signals;
  41. struct wpa_dbus_property_desc *property_dsc = obj_dsc->properties;
  42. struct interfaces *head = NULL;
  43. struct interfaces *iface, *last;
  44. /* extract interfaces from methods */
  45. while (method_dsc) {
  46. iface = head;
  47. last = NULL;
  48. /* go to next method if its interface is already extracted */
  49. while (iface) {
  50. if (!os_strcmp(iface->dbus_interface,
  51. method_dsc->dbus_interface))
  52. break;
  53. last = iface;
  54. iface = iface->next;
  55. }
  56. if (iface) {
  57. method_dsc = method_dsc->next;
  58. continue;
  59. }
  60. iface = os_zalloc(sizeof(struct interfaces));
  61. if (!iface) {
  62. wpa_printf(MSG_ERROR, "Not enough memory to create "
  63. "interface introspection data");
  64. method_dsc = method_dsc->next;
  65. continue;
  66. }
  67. if (last)
  68. last->next = iface;
  69. else
  70. head = iface;
  71. iface->dbus_interface = os_strdup(method_dsc->dbus_interface);
  72. if (!iface->dbus_interface) {
  73. wpa_printf(MSG_ERROR, "Not enough memory to create "
  74. "interface introspection data (interface "
  75. "name)");
  76. method_dsc = method_dsc->next;
  77. continue;
  78. }
  79. iface->interface_node = xmlNewChild(root_node, NULL,
  80. BAD_CAST "interface",
  81. NULL);
  82. xmlNewProp(iface->interface_node, BAD_CAST "name",
  83. BAD_CAST method_dsc->dbus_interface);
  84. method_dsc = method_dsc->next;
  85. }
  86. /* extract interfaces from signals */
  87. while (signal_dsc) {
  88. iface = head;
  89. last = NULL;
  90. /* go to next signal if its interface is already extracted */
  91. while (iface) {
  92. if (!os_strcmp(iface->dbus_interface,
  93. signal_dsc->dbus_interface))
  94. break;
  95. last = iface;
  96. iface = iface->next;
  97. }
  98. if (iface) {
  99. signal_dsc = signal_dsc->next;
  100. continue;
  101. }
  102. iface = os_zalloc(sizeof(struct interfaces));
  103. if (!iface) {
  104. wpa_printf(MSG_ERROR, "Not enough memory to create "
  105. "interface introspection data");
  106. signal_dsc = signal_dsc->next;
  107. continue;
  108. }
  109. if (last)
  110. last->next = iface;
  111. else
  112. head = iface;
  113. iface->dbus_interface = os_strdup(signal_dsc->dbus_interface);
  114. if (!iface->dbus_interface) {
  115. wpa_printf(MSG_ERROR, "Not enough memory to create "
  116. "interface introspection data (interface "
  117. "name)");
  118. signal_dsc = signal_dsc->next;
  119. continue;
  120. }
  121. iface->interface_node = xmlNewChild(root_node, NULL,
  122. BAD_CAST "interface",
  123. NULL);
  124. xmlNewProp(iface->interface_node, BAD_CAST "name",
  125. BAD_CAST signal_dsc->dbus_interface);
  126. signal_dsc = signal_dsc->next;
  127. }
  128. /* extract interfaces from properties */
  129. while (property_dsc) {
  130. iface = head;
  131. last = NULL;
  132. /* go to next property if its interface is already extracted */
  133. while (iface) {
  134. if (!os_strcmp(iface->dbus_interface,
  135. property_dsc->dbus_interface))
  136. break;
  137. last = iface;
  138. iface = iface->next;
  139. }
  140. if (iface) {
  141. property_dsc = property_dsc->next;
  142. continue;
  143. }
  144. iface = os_zalloc(sizeof(struct interfaces));
  145. if (!iface) {
  146. wpa_printf(MSG_ERROR, "Not enough memory to create "
  147. "interface introspection data");
  148. property_dsc = property_dsc->next;
  149. continue;
  150. }
  151. if (last)
  152. last->next = iface;
  153. else
  154. head = iface;
  155. iface->dbus_interface =
  156. os_strdup(property_dsc->dbus_interface);
  157. if (!iface->dbus_interface) {
  158. wpa_printf(MSG_ERROR, "Not enough memory to create "
  159. "interface introspection data (interface "
  160. "name)");
  161. property_dsc = property_dsc->next;
  162. continue;
  163. }
  164. iface->interface_node = xmlNewChild(root_node, NULL,
  165. BAD_CAST "interface",
  166. NULL);
  167. xmlNewProp(iface->interface_node, BAD_CAST "name",
  168. BAD_CAST property_dsc->dbus_interface);
  169. property_dsc = property_dsc->next;
  170. }
  171. return head;
  172. }
  173. /**
  174. * wpa_dbus_introspect - Responds for Introspect calls on object
  175. * @message: Message with Introspect call
  176. * @obj_dsc: Object description on which Introspect was called
  177. * Returns: Message with introspection result XML string as only argument
  178. *
  179. * Iterates over all methods, signals and properties registered with
  180. * object and generates introspection data for the object as XML string.
  181. */
  182. DBusMessage * wpa_dbus_introspect(DBusMessage *message,
  183. struct wpa_dbus_object_desc *obj_dsc)
  184. {
  185. DBusMessage *reply;
  186. struct interfaces *ifaces, *tmp;
  187. struct wpa_dbus_signal_desc *signal_dsc;
  188. struct wpa_dbus_method_desc *method_dsc;
  189. struct wpa_dbus_property_desc *property_dsc;
  190. xmlChar *intro_str;
  191. char **children;
  192. int i, s;
  193. xmlDocPtr doc = NULL;
  194. xmlNodePtr root_node = NULL, node = NULL, iface_node = NULL;
  195. xmlNodePtr method_node = NULL, signal_node = NULL;
  196. xmlNodePtr property_node = NULL, arg_node = NULL;
  197. /* root node and dtd */
  198. doc = xmlNewDoc(BAD_CAST "1.0");
  199. root_node = xmlNewNode(NULL, BAD_CAST "node");
  200. xmlDocSetRootElement(doc, root_node);
  201. xmlCreateIntSubset(doc, BAD_CAST "node",
  202. BAD_CAST DBUS_INTROSPECT_1_0_XML_PUBLIC_IDENTIFIER,
  203. BAD_CAST DBUS_INTROSPECT_1_0_XML_SYSTEM_IDENTIFIER);
  204. /* Add Introspectable interface */
  205. iface_node = xmlNewChild(root_node, NULL, BAD_CAST "interface", NULL);
  206. xmlNewProp(iface_node, BAD_CAST "name",
  207. BAD_CAST WPA_DBUS_INTROSPECTION_INTERFACE);
  208. /* Add Introspect method */
  209. method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
  210. xmlNewProp(method_node, BAD_CAST "name",
  211. BAD_CAST WPA_DBUS_INTROSPECTION_METHOD);
  212. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  213. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "data");
  214. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
  215. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "out");
  216. /* Add Properties interface */
  217. iface_node = xmlNewChild(root_node, NULL,
  218. BAD_CAST "interface", NULL);
  219. xmlNewProp(iface_node, BAD_CAST "name",
  220. BAD_CAST WPA_DBUS_PROPERTIES_INTERFACE);
  221. /* Add Get method */
  222. method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
  223. xmlNewProp(method_node, BAD_CAST "name",
  224. BAD_CAST WPA_DBUS_PROPERTIES_GET);
  225. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  226. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "interface");
  227. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
  228. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
  229. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  230. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "propname");
  231. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
  232. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
  233. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  234. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "value");
  235. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "v");
  236. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "out");
  237. method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
  238. /* Add GetAll method */
  239. xmlNewProp(method_node, BAD_CAST "name",
  240. BAD_CAST WPA_DBUS_PROPERTIES_GETALL);
  241. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  242. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "interface");
  243. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
  244. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
  245. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  246. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "props");
  247. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "a{sv}");
  248. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "out");
  249. method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
  250. /* Add Set method */
  251. xmlNewProp(method_node, BAD_CAST "name",
  252. BAD_CAST WPA_DBUS_PROPERTIES_SET);
  253. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  254. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "interface");
  255. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
  256. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
  257. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  258. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "propname");
  259. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
  260. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
  261. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  262. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "value");
  263. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "v");
  264. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
  265. /* get all interfaces registered with object */
  266. ifaces = extract_interfaces(obj_dsc, root_node);
  267. /* create methods' nodes */
  268. method_dsc = obj_dsc->methods;
  269. while (method_dsc) {
  270. struct interfaces *iface = ifaces;
  271. while (iface) {
  272. if (!os_strcmp(iface->dbus_interface,
  273. method_dsc->dbus_interface))
  274. break;
  275. iface = iface->next;
  276. }
  277. if (!iface)
  278. continue;
  279. iface_node = iface->interface_node;
  280. method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method",
  281. NULL);
  282. xmlNewProp(method_node, BAD_CAST "name",
  283. BAD_CAST method_dsc->dbus_method);
  284. /* create args' nodes */
  285. for (i = 0; i < method_dsc->args_num; i++) {
  286. struct wpa_dbus_argument arg = method_dsc->args[i];
  287. arg_node = xmlNewChild(method_node, NULL,
  288. BAD_CAST "arg", NULL);
  289. if (arg.name && strlen(arg.name)) {
  290. xmlNewProp(arg_node, BAD_CAST "name",
  291. BAD_CAST arg.name);
  292. }
  293. xmlNewProp(arg_node, BAD_CAST "type",
  294. BAD_CAST arg.type);
  295. xmlNewProp(arg_node, BAD_CAST "direction",
  296. BAD_CAST (arg.dir == ARG_IN ?
  297. "in" : "out"));
  298. }
  299. method_dsc = method_dsc->next;
  300. }
  301. /* create signals' nodes */
  302. signal_dsc = obj_dsc->signals;
  303. while (signal_dsc) {
  304. struct interfaces *iface = ifaces;
  305. while (iface) {
  306. if (!os_strcmp(iface->dbus_interface,
  307. signal_dsc->dbus_interface))
  308. break;
  309. iface = iface->next;
  310. }
  311. if (!iface)
  312. continue;
  313. iface_node = iface->interface_node;
  314. signal_node = xmlNewChild(iface_node, NULL, BAD_CAST "signal",
  315. NULL);
  316. xmlNewProp(signal_node, BAD_CAST "name",
  317. BAD_CAST signal_dsc->dbus_signal);
  318. /* create args' nodes */
  319. for (i = 0; i < signal_dsc->args_num; i++) {
  320. struct wpa_dbus_argument arg = signal_dsc->args[i];
  321. arg_node = xmlNewChild(signal_node, NULL,
  322. BAD_CAST "arg", NULL);
  323. if (arg.name && strlen(arg.name)) {
  324. xmlNewProp(arg_node, BAD_CAST "name",
  325. BAD_CAST arg.name);
  326. }
  327. xmlNewProp(arg_node, BAD_CAST "type",
  328. BAD_CAST arg.type);
  329. }
  330. signal_dsc = signal_dsc->next;
  331. }
  332. /* create properties' nodes */
  333. property_dsc = obj_dsc->properties;
  334. while (property_dsc) {
  335. struct interfaces *iface = ifaces;
  336. while (iface) {
  337. if (!os_strcmp(iface->dbus_interface,
  338. property_dsc->dbus_interface))
  339. break;
  340. iface = iface->next;
  341. }
  342. if (!iface)
  343. continue;
  344. iface_node = iface->interface_node;
  345. property_node = xmlNewChild(iface_node, NULL,
  346. BAD_CAST "property", NULL);
  347. xmlNewProp(property_node, BAD_CAST "name",
  348. BAD_CAST property_dsc->dbus_property);
  349. xmlNewProp(property_node, BAD_CAST "type",
  350. BAD_CAST property_dsc->type);
  351. xmlNewProp(property_node, BAD_CAST "access", BAD_CAST
  352. (property_dsc->access == R ? "read" :
  353. (property_dsc->access == W ?
  354. "write" : "readwrite")));
  355. property_dsc = property_dsc->next;
  356. }
  357. /* add child nodes to introspection tree; */
  358. dbus_connection_list_registered(obj_dsc->connection,
  359. dbus_message_get_path(message),
  360. &children);
  361. for (i = 0; children[i]; i++) {
  362. node = xmlNewChild(root_node, NULL, BAD_CAST "node", NULL);
  363. xmlNewProp(node, BAD_CAST "name", BAD_CAST children[i]);
  364. }
  365. dbus_free_string_array(children);
  366. xmlDocDumpFormatMemory(doc, &intro_str, &s, 1);
  367. xmlFreeDoc(doc);
  368. while (ifaces) {
  369. tmp = ifaces;
  370. ifaces = ifaces->next;
  371. os_free(tmp->dbus_interface);
  372. os_free(tmp);
  373. }
  374. reply = dbus_message_new_method_return(message);
  375. if (reply == NULL) {
  376. xmlFree(intro_str);
  377. return NULL;
  378. }
  379. dbus_message_append_args(reply, DBUS_TYPE_STRING, &intro_str,
  380. DBUS_TYPE_INVALID);
  381. xmlFree(intro_str);
  382. return reply;
  383. }