dbus_new_introspect.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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;
  40. struct wpa_dbus_signal_desc *signal_dsc;
  41. struct wpa_dbus_property_desc *property_dsc;
  42. struct interfaces *head = NULL;
  43. struct interfaces *iface, *last;
  44. /* extract interfaces from methods */
  45. for (method_dsc = obj_dsc->methods; method_dsc;
  46. method_dsc = method_dsc->next) {
  47. iface = head;
  48. last = NULL;
  49. /* go to next method if its interface is already extracted */
  50. while (iface) {
  51. if (!os_strcmp(iface->dbus_interface,
  52. method_dsc->dbus_interface))
  53. break;
  54. last = iface;
  55. iface = iface->next;
  56. }
  57. if (iface)
  58. continue;
  59. iface = os_zalloc(sizeof(struct interfaces));
  60. if (!iface)
  61. continue;
  62. if (last)
  63. last->next = iface;
  64. else
  65. head = iface;
  66. iface->dbus_interface = os_strdup(method_dsc->dbus_interface);
  67. if (!iface->dbus_interface)
  68. continue;
  69. iface->interface_node = xmlNewChild(root_node, NULL,
  70. BAD_CAST "interface",
  71. NULL);
  72. xmlNewProp(iface->interface_node, BAD_CAST "name",
  73. BAD_CAST method_dsc->dbus_interface);
  74. }
  75. /* extract interfaces from signals */
  76. for (signal_dsc = obj_dsc->signals; signal_dsc;
  77. signal_dsc = signal_dsc->next) {
  78. iface = head;
  79. last = NULL;
  80. /* go to next signal if its interface is already extracted */
  81. while (iface) {
  82. if (!os_strcmp(iface->dbus_interface,
  83. signal_dsc->dbus_interface))
  84. break;
  85. last = iface;
  86. iface = iface->next;
  87. }
  88. if (iface)
  89. continue;
  90. iface = os_zalloc(sizeof(struct interfaces));
  91. if (!iface)
  92. continue;
  93. if (last)
  94. last->next = iface;
  95. else
  96. head = iface;
  97. iface->dbus_interface = os_strdup(signal_dsc->dbus_interface);
  98. if (!iface->dbus_interface)
  99. continue;
  100. iface->interface_node = xmlNewChild(root_node, NULL,
  101. BAD_CAST "interface",
  102. NULL);
  103. xmlNewProp(iface->interface_node, BAD_CAST "name",
  104. BAD_CAST signal_dsc->dbus_interface);
  105. }
  106. /* extract interfaces from properties */
  107. for (property_dsc = obj_dsc->properties; property_dsc;
  108. property_dsc = property_dsc->next) {
  109. iface = head;
  110. last = NULL;
  111. /* go to next property if its interface is already extracted */
  112. while (iface) {
  113. if (!os_strcmp(iface->dbus_interface,
  114. property_dsc->dbus_interface))
  115. break;
  116. last = iface;
  117. iface = iface->next;
  118. }
  119. if (iface)
  120. continue;
  121. iface = os_zalloc(sizeof(struct interfaces));
  122. if (!iface)
  123. continue;
  124. if (last)
  125. last->next = iface;
  126. else
  127. head = iface;
  128. iface->dbus_interface =
  129. os_strdup(property_dsc->dbus_interface);
  130. if (!iface->dbus_interface)
  131. continue;
  132. iface->interface_node = xmlNewChild(root_node, NULL,
  133. BAD_CAST "interface",
  134. NULL);
  135. xmlNewProp(iface->interface_node, BAD_CAST "name",
  136. BAD_CAST property_dsc->dbus_interface);
  137. }
  138. return head;
  139. }
  140. /**
  141. * wpa_dbus_introspect - Responds for Introspect calls on object
  142. * @message: Message with Introspect call
  143. * @obj_dsc: Object description on which Introspect was called
  144. * Returns: Message with introspection result XML string as only argument
  145. *
  146. * Iterates over all methods, signals and properties registered with
  147. * object and generates introspection data for the object as XML string.
  148. */
  149. DBusMessage * wpa_dbus_introspect(DBusMessage *message,
  150. struct wpa_dbus_object_desc *obj_dsc)
  151. {
  152. DBusMessage *reply;
  153. struct interfaces *ifaces, *tmp;
  154. struct wpa_dbus_signal_desc *signal_dsc;
  155. struct wpa_dbus_method_desc *method_dsc;
  156. struct wpa_dbus_property_desc *property_dsc;
  157. xmlChar *intro_str;
  158. char **children;
  159. int i, s;
  160. xmlDocPtr doc = NULL;
  161. xmlNodePtr root_node = NULL, node = NULL, iface_node = NULL;
  162. xmlNodePtr method_node = NULL, signal_node = NULL;
  163. xmlNodePtr property_node = NULL, arg_node = NULL;
  164. /* root node and dtd */
  165. doc = xmlNewDoc(BAD_CAST "1.0");
  166. root_node = xmlNewNode(NULL, BAD_CAST "node");
  167. xmlDocSetRootElement(doc, root_node);
  168. xmlCreateIntSubset(doc, BAD_CAST "node",
  169. BAD_CAST DBUS_INTROSPECT_1_0_XML_PUBLIC_IDENTIFIER,
  170. BAD_CAST DBUS_INTROSPECT_1_0_XML_SYSTEM_IDENTIFIER);
  171. /* Add Introspectable interface */
  172. iface_node = xmlNewChild(root_node, NULL, BAD_CAST "interface", NULL);
  173. xmlNewProp(iface_node, BAD_CAST "name",
  174. BAD_CAST WPA_DBUS_INTROSPECTION_INTERFACE);
  175. /* Add Introspect method */
  176. method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
  177. xmlNewProp(method_node, BAD_CAST "name",
  178. BAD_CAST WPA_DBUS_INTROSPECTION_METHOD);
  179. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  180. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "data");
  181. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
  182. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "out");
  183. /* Add Properties interface */
  184. iface_node = xmlNewChild(root_node, NULL,
  185. BAD_CAST "interface", NULL);
  186. xmlNewProp(iface_node, BAD_CAST "name",
  187. BAD_CAST WPA_DBUS_PROPERTIES_INTERFACE);
  188. /* Add Get method */
  189. method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
  190. xmlNewProp(method_node, BAD_CAST "name",
  191. BAD_CAST WPA_DBUS_PROPERTIES_GET);
  192. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  193. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "interface");
  194. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
  195. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
  196. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  197. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "propname");
  198. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
  199. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
  200. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  201. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "value");
  202. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "v");
  203. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "out");
  204. method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
  205. /* Add GetAll method */
  206. xmlNewProp(method_node, BAD_CAST "name",
  207. BAD_CAST WPA_DBUS_PROPERTIES_GETALL);
  208. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  209. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "interface");
  210. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
  211. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
  212. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  213. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "props");
  214. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "a{sv}");
  215. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "out");
  216. method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
  217. /* Add Set method */
  218. xmlNewProp(method_node, BAD_CAST "name",
  219. BAD_CAST WPA_DBUS_PROPERTIES_SET);
  220. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  221. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "interface");
  222. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
  223. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
  224. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  225. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "propname");
  226. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
  227. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
  228. arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
  229. xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "value");
  230. xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "v");
  231. xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
  232. /* get all interfaces registered with object */
  233. ifaces = extract_interfaces(obj_dsc, root_node);
  234. /* create methods' nodes */
  235. for (method_dsc = obj_dsc->methods; method_dsc;
  236. method_dsc = method_dsc->next) {
  237. struct interfaces *iface = ifaces;
  238. while (iface) {
  239. if (!os_strcmp(iface->dbus_interface,
  240. method_dsc->dbus_interface))
  241. break;
  242. iface = iface->next;
  243. }
  244. if (!iface)
  245. continue;
  246. iface_node = iface->interface_node;
  247. method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method",
  248. NULL);
  249. xmlNewProp(method_node, BAD_CAST "name",
  250. BAD_CAST method_dsc->dbus_method);
  251. /* create args' nodes */
  252. for (i = 0; i < method_dsc->args_num; i++) {
  253. struct wpa_dbus_argument arg = method_dsc->args[i];
  254. arg_node = xmlNewChild(method_node, NULL,
  255. BAD_CAST "arg", NULL);
  256. if (arg.name && strlen(arg.name)) {
  257. xmlNewProp(arg_node, BAD_CAST "name",
  258. BAD_CAST arg.name);
  259. }
  260. xmlNewProp(arg_node, BAD_CAST "type",
  261. BAD_CAST arg.type);
  262. xmlNewProp(arg_node, BAD_CAST "direction",
  263. BAD_CAST (arg.dir == ARG_IN ?
  264. "in" : "out"));
  265. }
  266. }
  267. /* create signals' nodes */
  268. for (signal_dsc = obj_dsc->signals; signal_dsc;
  269. signal_dsc = signal_dsc->next) {
  270. struct interfaces *iface = ifaces;
  271. while (iface) {
  272. if (!os_strcmp(iface->dbus_interface,
  273. signal_dsc->dbus_interface))
  274. break;
  275. iface = iface->next;
  276. }
  277. if (!iface)
  278. continue;
  279. iface_node = iface->interface_node;
  280. signal_node = xmlNewChild(iface_node, NULL, BAD_CAST "signal",
  281. NULL);
  282. xmlNewProp(signal_node, BAD_CAST "name",
  283. BAD_CAST signal_dsc->dbus_signal);
  284. /* create args' nodes */
  285. for (i = 0; i < signal_dsc->args_num; i++) {
  286. struct wpa_dbus_argument arg = signal_dsc->args[i];
  287. arg_node = xmlNewChild(signal_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. }
  296. }
  297. /* create properties' nodes */
  298. for (property_dsc = obj_dsc->properties; property_dsc;
  299. property_dsc = property_dsc->next) {
  300. struct interfaces *iface = ifaces;
  301. while (iface) {
  302. if (!os_strcmp(iface->dbus_interface,
  303. property_dsc->dbus_interface))
  304. break;
  305. iface = iface->next;
  306. }
  307. if (!iface)
  308. continue;
  309. iface_node = iface->interface_node;
  310. property_node = xmlNewChild(iface_node, NULL,
  311. BAD_CAST "property", NULL);
  312. xmlNewProp(property_node, BAD_CAST "name",
  313. BAD_CAST property_dsc->dbus_property);
  314. xmlNewProp(property_node, BAD_CAST "type",
  315. BAD_CAST property_dsc->type);
  316. xmlNewProp(property_node, BAD_CAST "access", BAD_CAST
  317. (property_dsc->access == R ? "read" :
  318. (property_dsc->access == W ?
  319. "write" : "readwrite")));
  320. }
  321. /* add child nodes to introspection tree; */
  322. dbus_connection_list_registered(obj_dsc->connection,
  323. dbus_message_get_path(message),
  324. &children);
  325. for (i = 0; children[i]; i++) {
  326. node = xmlNewChild(root_node, NULL, BAD_CAST "node", NULL);
  327. xmlNewProp(node, BAD_CAST "name", BAD_CAST children[i]);
  328. }
  329. dbus_free_string_array(children);
  330. xmlDocDumpFormatMemory(doc, &intro_str, &s, 1);
  331. xmlFreeDoc(doc);
  332. while (ifaces) {
  333. tmp = ifaces;
  334. ifaces = ifaces->next;
  335. os_free(tmp->dbus_interface);
  336. os_free(tmp);
  337. }
  338. reply = dbus_message_new_method_return(message);
  339. if (reply == NULL) {
  340. xmlFree(intro_str);
  341. return NULL;
  342. }
  343. dbus_message_append_args(reply, DBUS_TYPE_STRING, &intro_str,
  344. DBUS_TYPE_INVALID);
  345. xmlFree(intro_str);
  346. return reply;
  347. }