dbus_new_introspect.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * wpa_supplicant - D-Bus introspection
  3. * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
  4. * Copyright (c) 2009, Witold Sowa <witold.sowa@gmail.com>
  5. * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Alternatively, this software may be distributed under the terms of BSD
  12. * license.
  13. *
  14. * See README and COPYING for more details.
  15. */
  16. #include "utils/includes.h"
  17. #include "utils/common.h"
  18. #include "utils/list.h"
  19. #include "utils/wpabuf.h"
  20. #include "dbus_common_i.h"
  21. #include "dbus_new_helpers.h"
  22. struct interfaces {
  23. struct dl_list list;
  24. char *dbus_interface;
  25. struct wpabuf *xml;
  26. };
  27. static struct interfaces * add_interface(struct dl_list *list,
  28. const char *dbus_interface)
  29. {
  30. struct interfaces *iface;
  31. dl_list_for_each(iface, list, struct interfaces, list) {
  32. if (os_strcmp(iface->dbus_interface, dbus_interface) == 0)
  33. return iface; /* already in the list */
  34. }
  35. iface = os_zalloc(sizeof(struct interfaces));
  36. if (!iface)
  37. return NULL;
  38. iface->xml = wpabuf_alloc(6000);
  39. if (iface->xml == NULL) {
  40. os_free(iface);
  41. return NULL;
  42. }
  43. wpabuf_printf(iface->xml, "<interface name=\"%s\">", dbus_interface);
  44. dl_list_add_tail(list, &iface->list);
  45. iface->dbus_interface = os_strdup(dbus_interface);
  46. return iface;
  47. }
  48. static void add_arg(struct wpabuf *xml, const char *name, const char *type,
  49. const char *direction)
  50. {
  51. wpabuf_printf(xml, "<arg name=\"%s\"", name);
  52. if (type)
  53. wpabuf_printf(xml, " type=\"%s\"", type);
  54. if (direction)
  55. wpabuf_printf(xml, " direction=\"%s\"", direction);
  56. wpabuf_put_str(xml, "/>");
  57. }
  58. static void add_entry(struct wpabuf *xml, const char *type, const char *name,
  59. const struct wpa_dbus_argument *args, int include_dir)
  60. {
  61. const struct wpa_dbus_argument *arg;
  62. if (args == NULL || args->name == NULL) {
  63. wpabuf_printf(xml, "<%s name=\"%s\"/>", type, name);
  64. return;
  65. }
  66. wpabuf_printf(xml, "<%s name=\"%s\">", type, name);
  67. for (arg = args; arg && arg->name; arg++) {
  68. add_arg(xml, arg->name, arg->type,
  69. include_dir ? (arg->dir == ARG_IN ? "in" : "out") :
  70. NULL);
  71. }
  72. wpabuf_printf(xml, "</%s>", type);
  73. }
  74. static void add_property(struct wpabuf *xml,
  75. const struct wpa_dbus_property_desc *dsc)
  76. {
  77. wpabuf_printf(xml, "<property name=\"%s\" type=\"%s\" "
  78. "access=\"%s%s\"/>",
  79. dsc->dbus_property, dsc->type,
  80. dsc->getter ? "read" : "",
  81. dsc->setter ? "write" : "");
  82. }
  83. static void extract_interfaces_methods(
  84. struct dl_list *list, const struct wpa_dbus_method_desc *methods)
  85. {
  86. const struct wpa_dbus_method_desc *dsc;
  87. struct interfaces *iface;
  88. for (dsc = methods; dsc && dsc->dbus_method; dsc++) {
  89. iface = add_interface(list, dsc->dbus_interface);
  90. if (iface)
  91. add_entry(iface->xml, "method", dsc->dbus_method,
  92. dsc->args, 1);
  93. }
  94. }
  95. static void extract_interfaces_signals(
  96. struct dl_list *list, const struct wpa_dbus_signal_desc *signals)
  97. {
  98. const struct wpa_dbus_signal_desc *dsc;
  99. struct interfaces *iface;
  100. for (dsc = signals; dsc && dsc->dbus_signal; dsc++) {
  101. iface = add_interface(list, dsc->dbus_interface);
  102. if (iface)
  103. add_entry(iface->xml, "signal", dsc->dbus_signal,
  104. dsc->args, 0);
  105. }
  106. }
  107. static void extract_interfaces_properties(
  108. struct dl_list *list, const struct wpa_dbus_property_desc *properties)
  109. {
  110. const struct wpa_dbus_property_desc *dsc;
  111. struct interfaces *iface;
  112. for (dsc = properties; dsc && dsc->dbus_property; dsc++) {
  113. iface = add_interface(list, dsc->dbus_interface);
  114. if (iface)
  115. add_property(iface->xml, dsc);
  116. }
  117. }
  118. /**
  119. * extract_interfaces - Extract interfaces from methods, signals and props
  120. * @list: Interface list to be filled
  121. * @obj_dsc: Description of object from which interfaces will be extracted
  122. *
  123. * Iterates over all methods, signals, and properties registered with an
  124. * object and collects all declared DBus interfaces and create interfaces'
  125. * node in XML root node for each. Returned list elements contain interface
  126. * name and XML node of corresponding interface.
  127. */
  128. static void extract_interfaces(struct dl_list *list,
  129. struct wpa_dbus_object_desc *obj_dsc)
  130. {
  131. extract_interfaces_methods(list, obj_dsc->methods);
  132. extract_interfaces_signals(list, obj_dsc->signals);
  133. extract_interfaces_properties(list, obj_dsc->properties);
  134. }
  135. static void add_interfaces(struct dl_list *list, struct wpabuf *xml)
  136. {
  137. struct interfaces *iface, *n;
  138. dl_list_for_each_safe(iface, n, list, struct interfaces, list) {
  139. if (wpabuf_len(iface->xml) + 20 < wpabuf_tailroom(xml)) {
  140. wpabuf_put_buf(xml, iface->xml);
  141. wpabuf_put_str(xml, "</interface>");
  142. } else {
  143. wpa_printf(MSG_DEBUG, "dbus: Not enough room for "
  144. "add_interfaces inspect data: tailroom %u, "
  145. "add %u",
  146. (unsigned int) wpabuf_tailroom(xml),
  147. (unsigned int) wpabuf_len(iface->xml));
  148. }
  149. dl_list_del(&iface->list);
  150. wpabuf_free(iface->xml);
  151. os_free(iface->dbus_interface);
  152. os_free(iface);
  153. }
  154. }
  155. static void add_child_nodes(struct wpabuf *xml, DBusConnection *con,
  156. const char *path)
  157. {
  158. char **children;
  159. int i;
  160. /* add child nodes to introspection tree */
  161. dbus_connection_list_registered(con, path, &children);
  162. for (i = 0; children[i]; i++)
  163. wpabuf_printf(xml, "<node name=\"%s\"/>", children[i]);
  164. dbus_free_string_array(children);
  165. }
  166. static void add_introspectable_interface(struct wpabuf *xml)
  167. {
  168. wpabuf_printf(xml, "<interface name=\"%s\">"
  169. "<method name=\"%s\">"
  170. "<arg name=\"data\" type=\"s\" direction=\"out\"/>"
  171. "</method>"
  172. "</interface>",
  173. WPA_DBUS_INTROSPECTION_INTERFACE,
  174. WPA_DBUS_INTROSPECTION_METHOD);
  175. }
  176. static void add_properties_interface(struct wpabuf *xml)
  177. {
  178. wpabuf_printf(xml, "<interface name=\"%s\">",
  179. WPA_DBUS_PROPERTIES_INTERFACE);
  180. wpabuf_printf(xml, "<method name=\"%s\">", WPA_DBUS_PROPERTIES_GET);
  181. add_arg(xml, "interface", "s", "in");
  182. add_arg(xml, "propname", "s", "in");
  183. add_arg(xml, "value", "v", "out");
  184. wpabuf_put_str(xml, "</method>");
  185. wpabuf_printf(xml, "<method name=\"%s\">", WPA_DBUS_PROPERTIES_GETALL);
  186. add_arg(xml, "interface", "s", "in");
  187. add_arg(xml, "props", "a{sv}", "out");
  188. wpabuf_put_str(xml, "</method>");
  189. wpabuf_printf(xml, "<method name=\"%s\">", WPA_DBUS_PROPERTIES_SET);
  190. add_arg(xml, "interface", "s", "in");
  191. add_arg(xml, "propname", "s", "in");
  192. add_arg(xml, "value", "v", "in");
  193. wpabuf_put_str(xml, "</method>");
  194. wpabuf_put_str(xml, "</interface>");
  195. }
  196. static void add_wpas_interfaces(struct wpabuf *xml,
  197. struct wpa_dbus_object_desc *obj_dsc)
  198. {
  199. struct dl_list ifaces;
  200. dl_list_init(&ifaces);
  201. extract_interfaces(&ifaces, obj_dsc);
  202. add_interfaces(&ifaces, xml);
  203. }
  204. /**
  205. * wpa_dbus_introspect - Responds for Introspect calls on object
  206. * @message: Message with Introspect call
  207. * @obj_dsc: Object description on which Introspect was called
  208. * Returns: Message with introspection result XML string as only argument
  209. *
  210. * Iterates over all methods, signals and properties registered with
  211. * object and generates introspection data for the object as XML string.
  212. */
  213. DBusMessage * wpa_dbus_introspect(DBusMessage *message,
  214. struct wpa_dbus_object_desc *obj_dsc)
  215. {
  216. DBusMessage *reply;
  217. struct wpabuf *xml;
  218. xml = wpabuf_alloc(10000);
  219. if (xml == NULL)
  220. return NULL;
  221. wpabuf_put_str(xml, "<?xml version=\"1.0\"?>\n");
  222. wpabuf_put_str(xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
  223. wpabuf_put_str(xml, "<node>");
  224. add_introspectable_interface(xml);
  225. add_properties_interface(xml);
  226. add_wpas_interfaces(xml, obj_dsc);
  227. add_child_nodes(xml, obj_dsc->connection,
  228. dbus_message_get_path(message));
  229. wpabuf_put_str(xml, "</node>\n");
  230. reply = dbus_message_new_method_return(message);
  231. if (reply) {
  232. const char *intro_str = wpabuf_head(xml);
  233. dbus_message_append_args(reply, DBUS_TYPE_STRING, &intro_str,
  234. DBUS_TYPE_INVALID);
  235. }
  236. wpabuf_free(xml);
  237. return reply;
  238. }