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