You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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\" access=\"%s\"/>",
  78. dsc->dbus_property, dsc->type,
  79. (dsc->access == R ? "read" :
  80. (dsc->access == W ? "write" : "readwrite")));
  81. }
  82. static void extract_interfaces_methods(
  83. struct dl_list *list, const struct wpa_dbus_method_desc *methods)
  84. {
  85. const struct wpa_dbus_method_desc *dsc;
  86. struct interfaces *iface;
  87. for (dsc = methods; dsc && dsc->dbus_method; dsc++) {
  88. iface = add_interface(list, dsc->dbus_interface);
  89. if (iface)
  90. add_entry(iface->xml, "method", dsc->dbus_method,
  91. dsc->args, 1);
  92. }
  93. }
  94. static void extract_interfaces_signals(
  95. struct dl_list *list, const struct wpa_dbus_signal_desc *signals)
  96. {
  97. const struct wpa_dbus_signal_desc *dsc;
  98. struct interfaces *iface;
  99. for (dsc = signals; dsc && dsc->dbus_signal; dsc++) {
  100. iface = add_interface(list, dsc->dbus_interface);
  101. if (iface)
  102. add_entry(iface->xml, "signal", dsc->dbus_signal,
  103. dsc->args, 0);
  104. }
  105. }
  106. static void extract_interfaces_properties(
  107. struct dl_list *list, const struct wpa_dbus_property_desc *properties)
  108. {
  109. const struct wpa_dbus_property_desc *dsc;
  110. struct interfaces *iface;
  111. for (dsc = properties; dsc && dsc->dbus_property; dsc++) {
  112. iface = add_interface(list, dsc->dbus_interface);
  113. if (iface)
  114. add_property(iface->xml, dsc);
  115. }
  116. }
  117. /**
  118. * extract_interfaces - Extract interfaces from methods, signals and props
  119. * @list: Interface list to be filled
  120. * @obj_dsc: Description of object from which interfaces will be extracted
  121. *
  122. * Iterates over all methods, signals, and properties registered with an
  123. * object and collects all declared DBus interfaces and create interfaces'
  124. * node in XML root node for each. Returned list elements contain interface
  125. * name and XML node of corresponding interface.
  126. */
  127. static void extract_interfaces(struct dl_list *list,
  128. struct wpa_dbus_object_desc *obj_dsc)
  129. {
  130. extract_interfaces_methods(list, obj_dsc->methods);
  131. extract_interfaces_signals(list, obj_dsc->signals);
  132. extract_interfaces_properties(list, obj_dsc->properties);
  133. }
  134. static void add_interfaces(struct dl_list *list, struct wpabuf *xml)
  135. {
  136. struct interfaces *iface, *n;
  137. dl_list_for_each_safe(iface, n, list, struct interfaces, list) {
  138. if (wpabuf_len(iface->xml) + 20 < wpabuf_tailroom(xml)) {
  139. wpabuf_put_buf(xml, iface->xml);
  140. wpabuf_put_str(xml, "</interface>");
  141. }
  142. dl_list_del(&iface->list);
  143. wpabuf_free(iface->xml);
  144. os_free(iface->dbus_interface);
  145. os_free(iface);
  146. }
  147. }
  148. static void add_child_nodes(struct wpabuf *xml, DBusConnection *con,
  149. const char *path)
  150. {
  151. char **children;
  152. int i;
  153. /* add child nodes to introspection tree */
  154. dbus_connection_list_registered(con, path, &children);
  155. for (i = 0; children[i]; i++)
  156. wpabuf_printf(xml, "<node name=\"%s\"/>", children[i]);
  157. dbus_free_string_array(children);
  158. }
  159. static void add_introspectable_interface(struct wpabuf *xml)
  160. {
  161. wpabuf_printf(xml, "<interface name=\"%s\">"
  162. "<method name=\"%s\">"
  163. "<arg name=\"data\" type=\"s\" direction=\"out\"/>"
  164. "</method>"
  165. "</interface>",
  166. WPA_DBUS_INTROSPECTION_INTERFACE,
  167. WPA_DBUS_INTROSPECTION_METHOD);
  168. }
  169. static void add_properties_interface(struct wpabuf *xml)
  170. {
  171. wpabuf_printf(xml, "<interface name=\"%s\">",
  172. WPA_DBUS_PROPERTIES_INTERFACE);
  173. wpabuf_printf(xml, "<method name=\"%s\">", WPA_DBUS_PROPERTIES_GET);
  174. add_arg(xml, "interface", "s", "in");
  175. add_arg(xml, "propname", "s", "in");
  176. add_arg(xml, "value", "v", "out");
  177. wpabuf_put_str(xml, "</method>");
  178. wpabuf_printf(xml, "<method name=\"%s\">", WPA_DBUS_PROPERTIES_GETALL);
  179. add_arg(xml, "interface", "s", "in");
  180. add_arg(xml, "props", "a{sv}", "out");
  181. wpabuf_put_str(xml, "</method>");
  182. wpabuf_printf(xml, "<method name=\"%s\">", WPA_DBUS_PROPERTIES_SET);
  183. add_arg(xml, "interface", "s", "in");
  184. add_arg(xml, "propname", "s", "in");
  185. add_arg(xml, "value", "v", "in");
  186. wpabuf_put_str(xml, "</method>");
  187. wpabuf_put_str(xml, "</interface>");
  188. }
  189. static void add_wpas_interfaces(struct wpabuf *xml,
  190. struct wpa_dbus_object_desc *obj_dsc)
  191. {
  192. struct dl_list ifaces;
  193. dl_list_init(&ifaces);
  194. extract_interfaces(&ifaces, obj_dsc);
  195. add_interfaces(&ifaces, xml);
  196. }
  197. /**
  198. * wpa_dbus_introspect - Responds for Introspect calls on object
  199. * @message: Message with Introspect call
  200. * @obj_dsc: Object description on which Introspect was called
  201. * Returns: Message with introspection result XML string as only argument
  202. *
  203. * Iterates over all methods, signals and properties registered with
  204. * object and generates introspection data for the object as XML string.
  205. */
  206. DBusMessage * wpa_dbus_introspect(DBusMessage *message,
  207. struct wpa_dbus_object_desc *obj_dsc)
  208. {
  209. DBusMessage *reply;
  210. struct wpabuf *xml;
  211. xml = wpabuf_alloc(8000);
  212. if (xml == NULL)
  213. return NULL;
  214. wpabuf_put_str(xml, "<?xml version=\"1.0\"?>\n");
  215. wpabuf_put_str(xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
  216. wpabuf_put_str(xml, "<node>");
  217. add_introspectable_interface(xml);
  218. add_properties_interface(xml);
  219. add_wpas_interfaces(xml, obj_dsc);
  220. add_child_nodes(xml, obj_dsc->connection,
  221. dbus_message_get_path(message));
  222. wpabuf_put_str(xml, "</node>\n");
  223. reply = dbus_message_new_method_return(message);
  224. if (reply) {
  225. const char *intro_str = wpabuf_head(xml);
  226. dbus_message_append_args(reply, DBUS_TYPE_STRING, &intro_str,
  227. DBUS_TYPE_INVALID);
  228. }
  229. wpabuf_free(xml);
  230. return reply;
  231. }