Line data Source code
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 software may be distributed under the terms of the BSD license.
8 : * See README for more details.
9 : */
10 :
11 : #include "utils/includes.h"
12 :
13 : #include "utils/common.h"
14 : #include "utils/list.h"
15 : #include "utils/wpabuf.h"
16 : #include "dbus_common_i.h"
17 : #include "dbus_new_helpers.h"
18 :
19 :
20 : struct interfaces {
21 : struct dl_list list;
22 : char *dbus_interface;
23 : struct wpabuf *xml;
24 : };
25 :
26 :
27 23386 : static struct interfaces * add_interface(struct dl_list *list,
28 : const char *dbus_interface)
29 : {
30 : struct interfaces *iface;
31 :
32 34384 : dl_list_for_each(iface, list, struct interfaces, list) {
33 34000 : if (os_strcmp(iface->dbus_interface, dbus_interface) == 0)
34 23002 : return iface; /* already in the list */
35 : }
36 :
37 384 : iface = os_zalloc(sizeof(struct interfaces));
38 384 : if (!iface)
39 1 : return NULL;
40 383 : iface->dbus_interface = os_strdup(dbus_interface);
41 383 : iface->xml = wpabuf_alloc(15000);
42 383 : if (iface->dbus_interface == NULL || iface->xml == NULL) {
43 2 : os_free(iface->dbus_interface);
44 2 : wpabuf_free(iface->xml);
45 2 : os_free(iface);
46 2 : return NULL;
47 : }
48 381 : wpabuf_printf(iface->xml, "<interface name=\"%s\">", dbus_interface);
49 381 : dl_list_add_tail(list, &iface->list);
50 381 : return iface;
51 : }
52 :
53 :
54 12383 : static void add_arg(struct wpabuf *xml, const char *name, const char *type,
55 : const char *direction)
56 : {
57 12383 : wpabuf_printf(xml, "<arg name=\"%s\"", name);
58 12383 : if (type)
59 12383 : wpabuf_printf(xml, " type=\"%s\"", type);
60 12383 : if (direction)
61 6888 : wpabuf_printf(xml, " direction=\"%s\"", direction);
62 12383 : wpabuf_put_str(xml, "/>");
63 12383 : }
64 :
65 :
66 9753 : static void add_entry(struct wpabuf *xml, const char *type, const char *name,
67 : const struct wpa_dbus_argument *args, int include_dir)
68 : {
69 : const struct wpa_dbus_argument *arg;
70 :
71 9753 : if (args == NULL || args->name == NULL) {
72 1859 : wpabuf_printf(xml, "<%s name=\"%s\"/>", type, name);
73 11612 : return;
74 : }
75 7894 : wpabuf_printf(xml, "<%s name=\"%s\">", type, name);
76 18733 : for (arg = args; arg && arg->name; arg++) {
77 16183 : add_arg(xml, arg->name, arg->type,
78 5344 : include_dir ? (arg->dir == ARG_IN ? "in" : "out") :
79 : NULL);
80 : }
81 7894 : wpabuf_printf(xml, "</%s>", type);
82 : }
83 :
84 :
85 13630 : static void add_property(struct wpabuf *xml,
86 : const struct wpa_dbus_property_desc *dsc)
87 : {
88 27260 : wpabuf_printf(xml, "<property name=\"%s\" type=\"%s\" "
89 : "access=\"%s%s\"/>",
90 : dsc->dbus_property, dsc->type,
91 13630 : dsc->getter ? "read" : "",
92 13630 : dsc->setter ? "write" : "");
93 13630 : }
94 :
95 :
96 193 : static void extract_interfaces_methods(
97 : struct dl_list *list, const struct wpa_dbus_method_desc *methods)
98 : {
99 : const struct wpa_dbus_method_desc *dsc;
100 : struct interfaces *iface;
101 :
102 5749 : for (dsc = methods; dsc && dsc->dbus_method; dsc++) {
103 5556 : iface = add_interface(list, dsc->dbus_interface);
104 5556 : if (iface)
105 5553 : add_entry(iface->xml, "method", dsc->dbus_method,
106 5553 : dsc->args, 1);
107 : }
108 193 : }
109 :
110 :
111 193 : static void extract_interfaces_signals(
112 : struct dl_list *list, const struct wpa_dbus_signal_desc *signals)
113 : {
114 : const struct wpa_dbus_signal_desc *dsc;
115 : struct interfaces *iface;
116 :
117 4393 : for (dsc = signals; dsc && dsc->dbus_signal; dsc++) {
118 4200 : iface = add_interface(list, dsc->dbus_interface);
119 4200 : if (iface)
120 4200 : add_entry(iface->xml, "signal", dsc->dbus_signal,
121 4200 : dsc->args, 0);
122 : }
123 193 : }
124 :
125 :
126 193 : static void extract_interfaces_properties(
127 : struct dl_list *list, const struct wpa_dbus_property_desc *properties)
128 : {
129 : const struct wpa_dbus_property_desc *dsc;
130 : struct interfaces *iface;
131 :
132 13823 : for (dsc = properties; dsc && dsc->dbus_property; dsc++) {
133 13630 : iface = add_interface(list, dsc->dbus_interface);
134 13630 : if (iface)
135 13630 : add_property(iface->xml, dsc);
136 : }
137 193 : }
138 :
139 :
140 : /**
141 : * extract_interfaces - Extract interfaces from methods, signals and props
142 : * @list: Interface list to be filled
143 : * @obj_dsc: Description of object from which interfaces will be extracted
144 : *
145 : * Iterates over all methods, signals, and properties registered with an
146 : * object and collects all declared DBus interfaces and create interfaces'
147 : * node in XML root node for each. Returned list elements contain interface
148 : * name and XML node of corresponding interface.
149 : */
150 193 : static void extract_interfaces(struct dl_list *list,
151 : struct wpa_dbus_object_desc *obj_dsc)
152 : {
153 193 : extract_interfaces_methods(list, obj_dsc->methods);
154 193 : extract_interfaces_signals(list, obj_dsc->signals);
155 193 : extract_interfaces_properties(list, obj_dsc->properties);
156 193 : }
157 :
158 :
159 193 : static void add_interfaces(struct dl_list *list, struct wpabuf *xml)
160 : {
161 : struct interfaces *iface, *n;
162 :
163 574 : dl_list_for_each_safe(iface, n, list, struct interfaces, list) {
164 381 : if (wpabuf_len(iface->xml) + 20 < wpabuf_tailroom(xml)) {
165 381 : wpabuf_put_buf(xml, iface->xml);
166 381 : wpabuf_put_str(xml, "</interface>");
167 : } else {
168 0 : wpa_printf(MSG_DEBUG,
169 : "dbus: Not enough room for add_interfaces inspect data: tailroom %u, add %u",
170 0 : (unsigned int) wpabuf_tailroom(xml),
171 0 : (unsigned int) wpabuf_len(iface->xml));
172 : }
173 381 : dl_list_del(&iface->list);
174 381 : wpabuf_free(iface->xml);
175 381 : os_free(iface->dbus_interface);
176 381 : os_free(iface);
177 : }
178 193 : }
179 :
180 :
181 193 : static void add_child_nodes(struct wpabuf *xml, DBusConnection *con,
182 : const char *path)
183 : {
184 : char **children;
185 : int i;
186 :
187 : /* add child nodes to introspection tree */
188 193 : dbus_connection_list_registered(con, path, &children);
189 724 : for (i = 0; children[i]; i++)
190 531 : wpabuf_printf(xml, "<node name=\"%s\"/>", children[i]);
191 193 : dbus_free_string_array(children);
192 193 : }
193 :
194 :
195 193 : static void add_introspectable_interface(struct wpabuf *xml)
196 : {
197 193 : wpabuf_printf(xml, "<interface name=\"%s\">"
198 : "<method name=\"%s\">"
199 : "<arg name=\"data\" type=\"s\" direction=\"out\"/>"
200 : "</method>"
201 : "</interface>",
202 : WPA_DBUS_INTROSPECTION_INTERFACE,
203 : WPA_DBUS_INTROSPECTION_METHOD);
204 193 : }
205 :
206 :
207 193 : static void add_properties_interface(struct wpabuf *xml)
208 : {
209 193 : wpabuf_printf(xml, "<interface name=\"%s\">",
210 : WPA_DBUS_PROPERTIES_INTERFACE);
211 :
212 193 : wpabuf_printf(xml, "<method name=\"%s\">", WPA_DBUS_PROPERTIES_GET);
213 193 : add_arg(xml, "interface", "s", "in");
214 193 : add_arg(xml, "propname", "s", "in");
215 193 : add_arg(xml, "value", "v", "out");
216 193 : wpabuf_put_str(xml, "</method>");
217 :
218 193 : wpabuf_printf(xml, "<method name=\"%s\">", WPA_DBUS_PROPERTIES_GETALL);
219 193 : add_arg(xml, "interface", "s", "in");
220 193 : add_arg(xml, "props", "a{sv}", "out");
221 193 : wpabuf_put_str(xml, "</method>");
222 :
223 193 : wpabuf_printf(xml, "<method name=\"%s\">", WPA_DBUS_PROPERTIES_SET);
224 193 : add_arg(xml, "interface", "s", "in");
225 193 : add_arg(xml, "propname", "s", "in");
226 193 : add_arg(xml, "value", "v", "in");
227 193 : wpabuf_put_str(xml, "</method>");
228 :
229 193 : wpabuf_put_str(xml, "</interface>");
230 193 : }
231 :
232 :
233 193 : static void add_wpas_interfaces(struct wpabuf *xml,
234 : struct wpa_dbus_object_desc *obj_dsc)
235 : {
236 : struct dl_list ifaces;
237 :
238 193 : dl_list_init(&ifaces);
239 193 : extract_interfaces(&ifaces, obj_dsc);
240 193 : add_interfaces(&ifaces, xml);
241 193 : }
242 :
243 :
244 : /**
245 : * wpa_dbus_introspect - Responds for Introspect calls on object
246 : * @message: Message with Introspect call
247 : * @obj_dsc: Object description on which Introspect was called
248 : * Returns: Message with introspection result XML string as only argument
249 : *
250 : * Iterates over all methods, signals and properties registered with
251 : * object and generates introspection data for the object as XML string.
252 : */
253 194 : DBusMessage * wpa_dbus_introspect(DBusMessage *message,
254 : struct wpa_dbus_object_desc *obj_dsc)
255 : {
256 :
257 : DBusMessage *reply;
258 : struct wpabuf *xml;
259 :
260 194 : xml = wpabuf_alloc(20000);
261 194 : if (xml == NULL)
262 1 : return NULL;
263 :
264 193 : wpabuf_put_str(xml, "<?xml version=\"1.0\"?>\n");
265 193 : wpabuf_put_str(xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
266 193 : wpabuf_put_str(xml, "<node>");
267 :
268 193 : add_introspectable_interface(xml);
269 193 : add_properties_interface(xml);
270 193 : add_wpas_interfaces(xml, obj_dsc);
271 193 : add_child_nodes(xml, obj_dsc->connection,
272 : dbus_message_get_path(message));
273 :
274 193 : wpabuf_put_str(xml, "</node>\n");
275 :
276 193 : reply = dbus_message_new_method_return(message);
277 193 : if (reply) {
278 193 : const char *intro_str = wpabuf_head(xml);
279 :
280 193 : dbus_message_append_args(reply, DBUS_TYPE_STRING, &intro_str,
281 : DBUS_TYPE_INVALID);
282 : }
283 193 : wpabuf_free(xml);
284 :
285 193 : return reply;
286 : }
|