aboutsummaryrefslogtreecommitdiffstats
path: root/wpa_supplicant/dbus/dbus_new_introspect.c
blob: 5c2265f0418d7153211632a7eef34ed66759c6e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * WPA Supplicant / dbus-based control interface
 * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
 * Copyright (c) 2009, Witold Sowa <witold.sowa@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Alternatively, this software may be distributed under the terms of BSD
 * license.
 *
 * See README and COPYING for more details.
 */

#include "utils/includes.h"
#include <libxml/tree.h>

#include "utils/common.h"
#include "dbus_common_i.h"
#include "dbus_new_helpers.h"


struct interfaces {
	struct interfaces *next;
	char *dbus_interface;
	xmlNodePtr interface_node;
};

/**
 * extract_interfaces - Extract interfaces from methods, signals and props
 * @obj_dsc: Description of object from which interfaces will be extracted
 * @root_node: root node of XML introspection document
 * Returns: List of interfaces found in object description
 *
 * Iterates over all methods, signals and properties registered with
 * object and collects all declared DBus interfaces and create interface's
 * node in XML root node for each. Returned list elements contains interface
 * name and XML node of corresponding interface.
 */
static struct interfaces * extract_interfaces(
	struct wpa_dbus_object_desc *obj_dsc, xmlNodePtr root_node)
{
	struct wpa_dbus_method_desc *method_dsc;
	struct wpa_dbus_signal_desc *signal_dsc;
	struct wpa_dbus_property_desc *property_dsc;
	struct interfaces *head = NULL;
	struct interfaces *iface, *last;

	/* extract interfaces from methods */
	for (method_dsc = obj_dsc->methods; method_dsc;
	     method_dsc = method_dsc->next) {
		iface = head;
		last = NULL;

		/* go to next method if its interface is already extracted */
		while (iface) {
			if (!os_strcmp(iface->dbus_interface,
				       method_dsc->dbus_interface))
				break;
			last = iface;
			iface = iface->next;
		}
		if (iface)
			continue;

		iface = os_zalloc(sizeof(struct interfaces));
		if (!iface)
			continue;

		if (last)
			last->next = iface;
		else
			head = iface;

		iface->dbus_interface = os_strdup(method_dsc->dbus_interface);
		if (!iface->dbus_interface)
			continue;

		iface->interface_node = xmlNewChild(root_node, NULL,
						    BAD_CAST "interface",
						    NULL);
		xmlNewProp(iface->interface_node, BAD_CAST "name",
			   BAD_CAST method_dsc->dbus_interface);
	}

	/* extract interfaces from signals */
	for (signal_dsc = obj_dsc->signals; signal_dsc;
	     signal_dsc = signal_dsc->next) {
		iface = head;
		last = NULL;

		/* go to next signal if its interface is already extracted */
		while (iface) {
			if (!os_strcmp(iface->dbus_interface,
				       signal_dsc->dbus_interface))
				break;
			last = iface;
			iface = iface->next;
		}
		if (iface)
			continue;

		iface = os_zalloc(sizeof(struct interfaces));
		if (!iface)
			continue;

		if (last)
			last->next = iface;
		else
			head = iface;

		iface->dbus_interface = os_strdup(signal_dsc->dbus_interface);
		if (!iface->dbus_interface)
			continue;

		iface->interface_node = xmlNewChild(root_node, NULL,
						    BAD_CAST "interface",
						    NULL);
		xmlNewProp(iface->interface_node, BAD_CAST "name",
			   BAD_CAST signal_dsc->dbus_interface);
	}

	/* extract interfaces from properties */
	for (property_dsc = obj_dsc->properties; property_dsc;
	     property_dsc = property_dsc->next) {
		iface = head;
		last = NULL;

		/* go to next property if its interface is already extracted */
		while (iface) {
			if (!os_strcmp(iface->dbus_interface,
				       property_dsc->dbus_interface))
				break;
			last = iface;
			iface = iface->next;
		}
		if (iface)
			continue;

		iface = os_zalloc(sizeof(struct interfaces));
		if (!iface)
			continue;

		if (last)
			last->next = iface;
		else
			head = iface;

		iface->dbus_interface =
			os_strdup(property_dsc->dbus_interface);
		if (!iface->dbus_interface)
			continue;

		iface->interface_node = xmlNewChild(root_node, NULL,
						    BAD_CAST "interface",
						    NULL);
		xmlNewProp(iface->interface_node, BAD_CAST "name",
			   BAD_CAST property_dsc->dbus_interface);
	}

	return head;
}


/**
 * wpa_dbus_introspect - Responds for Introspect calls on object
 * @message: Message with Introspect call
 * @obj_dsc: Object description on which Introspect was called
 * Returns: Message with introspection result XML string as only argument
 *
 * Iterates over all methods, signals and properties registered with
 * object and generates introspection data for the object as XML string.
 */
DBusMessage * wpa_dbus_introspect(DBusMessage *message,
				  struct wpa_dbus_object_desc *obj_dsc)
{

	DBusMessage *reply;
	struct interfaces *ifaces, *tmp;
	struct wpa_dbus_signal_desc *signal_dsc;
	struct wpa_dbus_method_desc *method_dsc;
	struct wpa_dbus_property_desc *property_dsc;
	xmlChar *intro_str;
	char **children;
	int i, s;

	xmlDocPtr doc = NULL;
	xmlNodePtr root_node = NULL, node = NULL, iface_node = NULL;
	xmlNodePtr method_node = NULL, signal_node = NULL;
	xmlNodePtr property_node = NULL, arg_node = NULL;

	/* root node and dtd */
	doc = xmlNewDoc(BAD_CAST "1.0");
	root_node = xmlNewNode(NULL, BAD_CAST "node");
	xmlDocSetRootElement(doc, root_node);
	xmlCreateIntSubset(doc, BAD_CAST "node",
			   BAD_CAST DBUS_INTROSPECT_1_0_XML_PUBLIC_IDENTIFIER,
			   BAD_CAST DBUS_INTROSPECT_1_0_XML_SYSTEM_IDENTIFIER);

	/* Add Introspectable interface */
	iface_node = xmlNewChild(root_node, NULL, BAD_CAST "interface", NULL);
	xmlNewProp(iface_node, BAD_CAST "name",
		   BAD_CAST WPA_DBUS_INTROSPECTION_INTERFACE);

	/* Add Introspect method */
	method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
	xmlNewProp(method_node, BAD_CAST "name",
		   BAD_CAST WPA_DBUS_INTROSPECTION_METHOD);
	arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
	xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "data");
	xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
	xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "out");


	/* Add Properties interface */
	iface_node = xmlNewChild(root_node, NULL,
				 BAD_CAST "interface", NULL);
	xmlNewProp(iface_node, BAD_CAST "name",
		   BAD_CAST WPA_DBUS_PROPERTIES_INTERFACE);

	/* Add Get method */
	method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);
	xmlNewProp(method_node, BAD_CAST "name",
		   BAD_CAST WPA_DBUS_PROPERTIES_GET);
	arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
	xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "interface");
	xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
	xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
	arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
	xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "propname");
	xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
	xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
	arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
	xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "value");
	xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "v");
	xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "out");
	method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);

	/* Add GetAll method */
	xmlNewProp(method_node, BAD_CAST "name",
		   BAD_CAST WPA_DBUS_PROPERTIES_GETALL);
	arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
	xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "interface");
	xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
	xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
	arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
	xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "props");
	xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "a{sv}");
	xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "out");
	method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method", NULL);

	/* Add Set method */
	xmlNewProp(method_node, BAD_CAST "name",
		   BAD_CAST WPA_DBUS_PROPERTIES_SET);
	arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
	xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "interface");
	xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
	xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
	arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
	xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "propname");
	xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "s");
	xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");
	arg_node = xmlNewChild(method_node, NULL, BAD_CAST "arg", NULL);
	xmlNewProp(arg_node, BAD_CAST "name", BAD_CAST "value");
	xmlNewProp(arg_node, BAD_CAST "type", BAD_CAST "v");
	xmlNewProp(arg_node, BAD_CAST "direction", BAD_CAST "in");

	/* get all interfaces registered with object */
	ifaces = extract_interfaces(obj_dsc, root_node);

	/* create methods' nodes */
	for (method_dsc = obj_dsc->methods; method_dsc;
	     method_dsc = method_dsc->next) {
		struct interfaces *iface = ifaces;
		while (iface) {
			if (!os_strcmp(iface->dbus_interface,
				       method_dsc->dbus_interface))
				break;
			iface = iface->next;
		}
		if (!iface)
			continue;

		iface_node = iface->interface_node;
		method_node = xmlNewChild(iface_node, NULL, BAD_CAST "method",
					  NULL);
		xmlNewProp(method_node, BAD_CAST "name",
			   BAD_CAST method_dsc->dbus_method);

		/* create args' nodes */
		for (i = 0; i < method_dsc->args_num; i++) {
			struct wpa_dbus_argument arg = method_dsc->args[i];
			arg_node = xmlNewChild(method_node, NULL,
					       BAD_CAST "arg", NULL);
			if (arg.name && strlen(arg.name)) {
				xmlNewProp(arg_node, BAD_CAST "name",
					   BAD_CAST arg.name);
			}
			xmlNewProp(arg_node, BAD_CAST "type",
				   BAD_CAST arg.type);
			xmlNewProp(arg_node, BAD_CAST "direction",
				   BAD_CAST (arg.dir == ARG_IN ?
					     "in" : "out"));
		}
	}

	/* create signals' nodes */
	for (signal_dsc = obj_dsc->signals; signal_dsc;
	     signal_dsc = signal_dsc->next) {
		struct interfaces *iface = ifaces;
		while (iface) {
			if (!os_strcmp(iface->dbus_interface,
				       signal_dsc->dbus_interface))
				break;
			iface = iface->next;
		}
		if (!iface)
			continue;

		iface_node = iface->interface_node;
		signal_node = xmlNewChild(iface_node, NULL, BAD_CAST "signal",
					  NULL);
		xmlNewProp(signal_node, BAD_CAST "name",
			   BAD_CAST signal_dsc->dbus_signal);

		/* create args' nodes */
		for (i = 0; i < signal_dsc->args_num; i++) {
			struct wpa_dbus_argument arg = signal_dsc->args[i];
			arg_node = xmlNewChild(signal_node, NULL,
					       BAD_CAST "arg", NULL);
			if (arg.name && strlen(arg.name)) {
				xmlNewProp(arg_node, BAD_CAST "name",
					   BAD_CAST arg.name);
			}
			xmlNewProp(arg_node, BAD_CAST "type",
				   BAD_CAST arg.type);
		}
	}

	/* create properties' nodes */
	for (property_dsc = obj_dsc->properties; property_dsc;
	     property_dsc = property_dsc->next) {
		struct interfaces *iface = ifaces;
		while (iface) {
			if (!os_strcmp(iface->dbus_interface,
				       property_dsc->dbus_interface))
				break;
			iface = iface->next;
		}
		if (!iface)
			continue;

		iface_node = iface->interface_node;
		property_node = xmlNewChild(iface_node, NULL,
					    BAD_CAST "property", NULL);
		xmlNewProp(property_node, BAD_CAST "name",
			   BAD_CAST property_dsc->dbus_property);
		xmlNewProp(property_node, BAD_CAST "type",
			   BAD_CAST property_dsc->type);
		xmlNewProp(property_node, BAD_CAST "access", BAD_CAST
			   (property_dsc->access == R ? "read" :
			    (property_dsc->access == W ?
			     "write" : "readwrite")));
	}

	/* add child nodes to introspection tree; */
	dbus_connection_list_registered(obj_dsc->connection,
					dbus_message_get_path(message),
					&children);
	for (i = 0; children[i]; i++) {
		node = xmlNewChild(root_node, NULL, BAD_CAST "node", NULL);
		xmlNewProp(node, BAD_CAST "name", BAD_CAST children[i]);
	}
	dbus_free_string_array(children);


	xmlDocDumpFormatMemory(doc, &intro_str, &s, 1);

	xmlFreeDoc(doc);

	while (ifaces) {
		tmp = ifaces;
		ifaces = ifaces->next;
		os_free(tmp->dbus_interface);
		os_free(tmp);
	}

	reply = dbus_message_new_method_return(message);
	if (reply == NULL) {
		xmlFree(intro_str);
		return NULL;
	}

	dbus_message_append_args(reply, DBUS_TYPE_STRING, &intro_str,
				 DBUS_TYPE_INVALID);

	xmlFree(intro_str);

	return reply;
}