1 /*
2 * WPA Supplicant / dbus-based control interface
3 * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
4 * Copyright (c) 2009, Witold Sowa <witold.sowa@gmail.com>
5 *
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
9
10 #include "utils/includes.h"
11
12 #include "utils/common.h"
13 #include "utils/eloop.h"
14 #include "drivers/driver.h"
15 #include "dbus_common.h"
16 #include "dbus_common_i.h"
17 #include "dbus_new.h"
18 #include "dbus_new_helpers.h"
19 #include "dbus_new_handlers.h"
20 #include "dbus_dict_helpers.h"
21
22
fill_dict_with_properties(DBusMessageIter * dict_iter,const struct wpa_dbus_property_desc * props,const char * interface,void * user_data,DBusError * error)23 static dbus_bool_t fill_dict_with_properties(
24 DBusMessageIter *dict_iter,
25 const struct wpa_dbus_property_desc *props,
26 const char *interface, void *user_data, DBusError *error)
27 {
28 DBusMessageIter entry_iter;
29 const struct wpa_dbus_property_desc *dsc;
30
31 for (dsc = props; dsc && dsc->dbus_property; dsc++) {
32 /* Only return properties for the requested D-Bus interface */
33 if (os_strncmp(dsc->dbus_interface, interface,
34 WPAS_DBUS_INTERFACE_MAX) != 0)
35 continue;
36
37 /* Skip write-only properties */
38 if (dsc->getter == NULL)
39 continue;
40
41 if (!dbus_message_iter_open_container(dict_iter,
42 DBUS_TYPE_DICT_ENTRY,
43 NULL, &entry_iter) ||
44 !dbus_message_iter_append_basic(&entry_iter,
45 DBUS_TYPE_STRING,
46 &dsc->dbus_property))
47 goto error;
48
49 /* An error getting a property fails the request entirely */
50 if (!dsc->getter(dsc, &entry_iter, error, user_data)) {
51 wpa_printf(MSG_INFO,
52 "dbus: %s dbus_interface=%s dbus_property=%s getter failed",
53 __func__, dsc->dbus_interface,
54 dsc->dbus_property);
55 return FALSE;
56 }
57
58 if (!dbus_message_iter_close_container(dict_iter, &entry_iter))
59 goto error;
60 }
61
62 return TRUE;
63
64 error:
65 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
66 return FALSE;
67 }
68
69
70 /**
71 * get_all_properties - Responds for GetAll properties calls on object
72 * @message: Message with GetAll call
73 * @interface: interface name which properties will be returned
74 * @property_dsc: list of object's properties
75 * Returns: Message with dict of variants as argument with properties values
76 *
77 * Iterates over all properties registered with object and execute getters
78 * of those, which are readable and which interface matches interface
79 * specified as argument. Returned message contains one dict argument
80 * with properties names as keys and theirs values as values.
81 */
get_all_properties(DBusMessage * message,char * interface,struct wpa_dbus_object_desc * obj_dsc)82 static DBusMessage * get_all_properties(DBusMessage *message, char *interface,
83 struct wpa_dbus_object_desc *obj_dsc)
84 {
85 DBusMessage *reply;
86 DBusMessageIter iter, dict_iter;
87 DBusError error;
88
89 reply = dbus_message_new_method_return(message);
90 if (reply == NULL)
91 return wpas_dbus_error_no_memory(message);
92
93 dbus_message_iter_init_append(reply, &iter);
94 if (!wpa_dbus_dict_open_write(&iter, &dict_iter)) {
95 dbus_message_unref(reply);
96 return wpas_dbus_error_no_memory(message);
97 }
98
99 dbus_error_init(&error);
100 if (!fill_dict_with_properties(&dict_iter, obj_dsc->properties,
101 interface, obj_dsc->user_data, &error)) {
102 wpa_dbus_dict_close_write(&iter, &dict_iter);
103 dbus_message_unref(reply);
104 reply = wpas_dbus_reply_new_from_error(
105 message, &error, DBUS_ERROR_INVALID_ARGS,
106 "No readable properties in this interface");
107 dbus_error_free(&error);
108 return reply;
109 }
110
111 if (!wpa_dbus_dict_close_write(&iter, &dict_iter)) {
112 dbus_message_unref(reply);
113 return wpas_dbus_error_no_memory(message);
114 }
115
116 return reply;
117 }
118
119
is_signature_correct(DBusMessage * message,const struct wpa_dbus_method_desc * method_dsc)120 static int is_signature_correct(DBusMessage *message,
121 const struct wpa_dbus_method_desc *method_dsc)
122 {
123 /* According to DBus documentation max length of signature is 255 */
124 #define MAX_SIG_LEN 256
125 char registered_sig[MAX_SIG_LEN], *pos;
126 const char *sig = dbus_message_get_signature(message);
127 int ret;
128 const struct wpa_dbus_argument *arg;
129
130 pos = registered_sig;
131 *pos = '\0';
132
133 for (arg = method_dsc->args; arg && arg->name; arg++) {
134 if (arg->dir == ARG_IN) {
135 size_t blen = registered_sig + MAX_SIG_LEN - pos;
136
137 ret = os_snprintf(pos, blen, "%s", arg->type);
138 if (os_snprintf_error(blen, ret))
139 return 0;
140 pos += ret;
141 }
142 }
143
144 return !os_strncmp(registered_sig, sig, MAX_SIG_LEN);
145 }
146
147
properties_get_all(DBusMessage * message,char * interface,struct wpa_dbus_object_desc * obj_dsc)148 static DBusMessage * properties_get_all(DBusMessage *message, char *interface,
149 struct wpa_dbus_object_desc *obj_dsc)
150 {
151 if (os_strcmp(dbus_message_get_signature(message), "s") != 0)
152 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
153 NULL);
154
155 return get_all_properties(message, interface, obj_dsc);
156 }
157
158
properties_get(DBusMessage * message,const struct wpa_dbus_property_desc * dsc,void * user_data)159 static DBusMessage * properties_get(DBusMessage *message,
160 const struct wpa_dbus_property_desc *dsc,
161 void *user_data)
162 {
163 DBusMessage *reply;
164 DBusMessageIter iter;
165 DBusError error;
166
167 if (os_strcmp(dbus_message_get_signature(message), "ss")) {
168 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
169 NULL);
170 }
171
172 if (dsc->getter == NULL) {
173 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
174 "Property is write-only");
175 }
176
177 reply = dbus_message_new_method_return(message);
178 dbus_message_iter_init_append(reply, &iter);
179
180 dbus_error_init(&error);
181 if (dsc->getter(dsc, &iter, &error, user_data) == FALSE) {
182 dbus_message_unref(reply);
183 reply = wpas_dbus_reply_new_from_error(
184 message, &error, DBUS_ERROR_FAILED,
185 "Failed to read property");
186 dbus_error_free(&error);
187 }
188
189 return reply;
190 }
191
192
properties_set(DBusMessage * message,const struct wpa_dbus_property_desc * dsc,void * user_data)193 static DBusMessage * properties_set(DBusMessage *message,
194 const struct wpa_dbus_property_desc *dsc,
195 void *user_data)
196 {
197 DBusMessage *reply;
198 DBusMessageIter iter;
199 DBusError error;
200
201 if (os_strcmp(dbus_message_get_signature(message), "ssv")) {
202 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
203 NULL);
204 }
205
206 if (dsc->setter == NULL) {
207 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
208 "Property is read-only");
209 }
210
211 dbus_message_iter_init(message, &iter);
212 /* Skip the interface name and the property name */
213 dbus_message_iter_next(&iter);
214 dbus_message_iter_next(&iter);
215
216 /* Iter will now point to the property's new value */
217 dbus_error_init(&error);
218 if (dsc->setter(dsc, &iter, &error, user_data) == TRUE) {
219 /* Success */
220 reply = dbus_message_new_method_return(message);
221 } else {
222 reply = wpas_dbus_reply_new_from_error(
223 message, &error, DBUS_ERROR_FAILED,
224 "Failed to set property");
225 dbus_error_free(&error);
226 }
227
228 return reply;
229 }
230
231
232 static DBusMessage *
properties_get_or_set(DBusMessage * message,DBusMessageIter * iter,char * interface,struct wpa_dbus_object_desc * obj_dsc)233 properties_get_or_set(DBusMessage *message, DBusMessageIter *iter,
234 char *interface,
235 struct wpa_dbus_object_desc *obj_dsc)
236 {
237 const struct wpa_dbus_property_desc *property_dsc;
238 char *property;
239 const char *method;
240
241 method = dbus_message_get_member(message);
242 property_dsc = obj_dsc->properties;
243
244 /* Second argument: property name (DBUS_TYPE_STRING) */
245 if (!dbus_message_iter_next(iter) ||
246 dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING) {
247 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
248 NULL);
249 }
250 dbus_message_iter_get_basic(iter, &property);
251
252 while (property_dsc && property_dsc->dbus_property) {
253 /* compare property names and
254 * interfaces */
255 if (!os_strncmp(property_dsc->dbus_property, property,
256 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
257 !os_strncmp(property_dsc->dbus_interface, interface,
258 WPAS_DBUS_INTERFACE_MAX))
259 break;
260
261 property_dsc++;
262 }
263 if (property_dsc == NULL || property_dsc->dbus_property == NULL) {
264 wpa_printf(MSG_DEBUG, "no property handler for %s.%s on %s",
265 interface, property,
266 dbus_message_get_path(message));
267 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
268 "No such property");
269 }
270
271 if (os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
272 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) == 0) {
273 wpa_printf(MSG_MSGDUMP, "%s: Get(%s)", __func__, property);
274 return properties_get(message, property_dsc,
275 obj_dsc->user_data);
276 }
277
278 wpa_printf(MSG_MSGDUMP, "%s: Set(%s)", __func__, property);
279 return properties_set(message, property_dsc, obj_dsc->user_data);
280 }
281
282
properties_handler(DBusMessage * message,struct wpa_dbus_object_desc * obj_dsc)283 static DBusMessage * properties_handler(DBusMessage *message,
284 struct wpa_dbus_object_desc *obj_dsc)
285 {
286 DBusMessageIter iter;
287 char *interface;
288 const char *method;
289
290 method = dbus_message_get_member(message);
291 dbus_message_iter_init(message, &iter);
292
293 if (!os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
294 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
295 !os_strncmp(WPA_DBUS_PROPERTIES_SET, method,
296 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
297 !os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
298 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
299 /* First argument: interface name (DBUS_TYPE_STRING) */
300 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) {
301 return dbus_message_new_error(message,
302 DBUS_ERROR_INVALID_ARGS,
303 NULL);
304 }
305
306 dbus_message_iter_get_basic(&iter, &interface);
307
308 if (!os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
309 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
310 /* GetAll */
311 return properties_get_all(message, interface, obj_dsc);
312 }
313 /* Get or Set */
314 return properties_get_or_set(message, &iter, interface,
315 obj_dsc);
316 }
317 return dbus_message_new_error(message, DBUS_ERROR_UNKNOWN_METHOD,
318 NULL);
319 }
320
321
msg_method_handler(DBusMessage * message,struct wpa_dbus_object_desc * obj_dsc)322 static DBusMessage * msg_method_handler(DBusMessage *message,
323 struct wpa_dbus_object_desc *obj_dsc)
324 {
325 const struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
326 const char *method;
327 const char *msg_interface;
328
329 method = dbus_message_get_member(message);
330 msg_interface = dbus_message_get_interface(message);
331
332 /* try match call to any registered method */
333 while (method_dsc && method_dsc->dbus_method) {
334 /* compare method names and interfaces */
335 if (!os_strncmp(method_dsc->dbus_method, method,
336 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
337 !os_strncmp(method_dsc->dbus_interface, msg_interface,
338 WPAS_DBUS_INTERFACE_MAX))
339 break;
340
341 method_dsc++;
342 }
343 if (method_dsc == NULL || method_dsc->dbus_method == NULL) {
344 wpa_printf(MSG_DEBUG, "no method handler for %s.%s on %s",
345 msg_interface, method,
346 dbus_message_get_path(message));
347 return dbus_message_new_error(message,
348 DBUS_ERROR_UNKNOWN_METHOD, NULL);
349 }
350
351 if (!is_signature_correct(message, method_dsc)) {
352 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
353 NULL);
354 }
355
356 return method_dsc->method_handler(message, obj_dsc->user_data);
357 }
358
359
360 /**
361 * message_handler - Handles incoming DBus messages
362 * @connection: DBus connection on which message was received
363 * @message: Received message
364 * @user_data: pointer to description of object to which message was sent
365 * Returns: Returns information whether message was handled or not
366 *
367 * Reads message interface and method name, then checks if they matches one
368 * of the special cases i.e. introspection call or properties get/getall/set
369 * methods and handles it. Else it iterates over registered methods list
370 * and tries to match method's name and interface to those read from message
371 * If appropriate method was found its handler function is called and
372 * response is sent. Otherwise, the DBUS_ERROR_UNKNOWN_METHOD error message
373 * will be sent.
374 */
message_handler(DBusConnection * connection,DBusMessage * message,void * user_data)375 static DBusHandlerResult message_handler(DBusConnection *connection,
376 DBusMessage *message, void *user_data)
377 {
378 struct wpa_dbus_object_desc *obj_dsc = user_data;
379 const char *method;
380 const char *path;
381 const char *msg_interface;
382 DBusMessage *reply;
383
384 /* get method, interface and path the message is addressed to */
385 method = dbus_message_get_member(message);
386 path = dbus_message_get_path(message);
387 msg_interface = dbus_message_get_interface(message);
388 if (!method || !path || !msg_interface)
389 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
390
391 wpa_printf(MSG_MSGDUMP, "dbus: %s.%s (%s) [%s]",
392 msg_interface, method, path,
393 dbus_message_get_signature(message));
394
395 /* if message is introspection method call */
396 if (!os_strncmp(WPA_DBUS_INTROSPECTION_METHOD, method,
397 WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
398 !os_strncmp(WPA_DBUS_INTROSPECTION_INTERFACE, msg_interface,
399 WPAS_DBUS_INTERFACE_MAX)) {
400 #ifdef CONFIG_CTRL_IFACE_DBUS_INTRO
401 reply = wpa_dbus_introspect(message, obj_dsc);
402 #else /* CONFIG_CTRL_IFACE_DBUS_INTRO */
403 reply = dbus_message_new_error(
404 message, DBUS_ERROR_UNKNOWN_METHOD,
405 "wpa_supplicant was compiled without introspection support.");
406 #endif /* CONFIG_CTRL_IFACE_DBUS_INTRO */
407 } else if (!os_strncmp(WPA_DBUS_PROPERTIES_INTERFACE, msg_interface,
408 WPAS_DBUS_INTERFACE_MAX)) {
409 /* if message is properties method call */
410 reply = properties_handler(message, obj_dsc);
411 } else {
412 reply = msg_method_handler(message, obj_dsc);
413 }
414
415 /* If handler succeed returning NULL, reply empty message */
416 if (!reply)
417 reply = dbus_message_new_method_return(message);
418 if (reply) {
419 if (!dbus_message_get_no_reply(message))
420 dbus_connection_send(connection, reply, NULL);
421 dbus_message_unref(reply);
422 }
423
424 wpa_dbus_flush_all_changed_properties(connection);
425
426 return DBUS_HANDLER_RESULT_HANDLED;
427 }
428
429
430 /**
431 * free_dbus_object_desc - Frees object description data structure
432 * @connection: DBus connection
433 * @obj_dsc: Object description to free
434 *
435 * Frees each of properties, methods and signals description lists and
436 * the object description structure itself.
437 */
free_dbus_object_desc(struct wpa_dbus_object_desc * obj_dsc)438 void free_dbus_object_desc(struct wpa_dbus_object_desc *obj_dsc)
439 {
440 if (!obj_dsc)
441 return;
442
443 /* free handler's argument */
444 if (obj_dsc->user_data_free_func)
445 obj_dsc->user_data_free_func(obj_dsc->user_data);
446
447 os_free(obj_dsc->path);
448 os_free(obj_dsc->prop_changed_flags);
449 os_free(obj_dsc);
450 }
451
452
free_dbus_object_desc_cb(DBusConnection * connection,void * obj_dsc)453 static void free_dbus_object_desc_cb(DBusConnection *connection, void *obj_dsc)
454 {
455 free_dbus_object_desc(obj_dsc);
456 }
457
458
459 /**
460 * wpa_dbus_ctrl_iface_init - Initialize dbus control interface
461 * @application_data: Pointer to application specific data structure
462 * @dbus_path: DBus path to interface object
463 * @dbus_service: DBus service name to register with
464 * @messageHandler: a pointer to function which will handle dbus messages
465 * coming on interface
466 * Returns: 0 on success, -1 on failure
467 *
468 * Initialize the dbus control interface and start receiving commands from
469 * external programs over the bus.
470 */
wpa_dbus_ctrl_iface_init(struct wpas_dbus_priv * iface,char * dbus_path,char * dbus_service,struct wpa_dbus_object_desc * obj_desc)471 int wpa_dbus_ctrl_iface_init(struct wpas_dbus_priv *iface,
472 char *dbus_path, char *dbus_service,
473 struct wpa_dbus_object_desc *obj_desc)
474 {
475 DBusError error;
476 int ret = -1;
477 DBusObjectPathVTable wpa_vtable = {
478 &free_dbus_object_desc_cb, &message_handler,
479 NULL, NULL, NULL, NULL
480 };
481
482 obj_desc->connection = iface->con;
483 obj_desc->path = os_strdup(dbus_path);
484
485 /* Register the message handler for the global dbus interface */
486 if (!dbus_connection_register_object_path(iface->con, dbus_path,
487 &wpa_vtable, obj_desc)) {
488 wpa_printf(MSG_ERROR, "dbus: Could not set up message handler");
489 return -1;
490 }
491
492 /* Register our service with the message bus */
493 dbus_error_init(&error);
494 switch (dbus_bus_request_name(iface->con, dbus_service, 0, &error)) {
495 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
496 ret = 0;
497 break;
498 case DBUS_REQUEST_NAME_REPLY_EXISTS:
499 case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
500 case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
501 wpa_printf(MSG_ERROR,
502 "dbus: Could not request service name: already registered");
503 break;
504 default:
505 wpa_printf(MSG_ERROR,
506 "dbus: Could not request service name: %s %s",
507 error.name, error.message);
508 break;
509 }
510 dbus_error_free(&error);
511
512 if (ret != 0)
513 return -1;
514
515 wpa_printf(MSG_DEBUG, "Providing DBus service '%s'.", dbus_service);
516
517 return 0;
518 }
519
520
521 /**
522 * wpa_dbus_register_object_per_iface - Register a new object with dbus
523 * @ctrl_iface: pointer to dbus private data
524 * @path: DBus path to object
525 * @ifname: interface name
526 * @obj_desc: description of object's methods, signals and properties
527 * Returns: 0 on success, -1 on error
528 *
529 * Registers a new interface with dbus and assigns it a dbus object path.
530 */
wpa_dbus_register_object_per_iface(struct wpas_dbus_priv * ctrl_iface,const char * path,const char * ifname,struct wpa_dbus_object_desc * obj_desc)531 int wpa_dbus_register_object_per_iface(struct wpas_dbus_priv *ctrl_iface,
532 const char *path, const char *ifname,
533 struct wpa_dbus_object_desc *obj_desc)
534 {
535 DBusConnection *con;
536 DBusError error;
537 DBusObjectPathVTable vtable = {
538 &free_dbus_object_desc_cb, &message_handler,
539 NULL, NULL, NULL, NULL
540 };
541
542 /* Do nothing if the control interface is not turned on */
543 if (ctrl_iface == NULL)
544 return 0;
545
546 con = ctrl_iface->con;
547 obj_desc->connection = con;
548 obj_desc->path = os_strdup(path);
549
550 dbus_error_init(&error);
551 /* Register the message handler for the interface functions */
552 if (!dbus_connection_try_register_object_path(con, path, &vtable,
553 obj_desc, &error)) {
554 if (os_strcmp(error.name, DBUS_ERROR_OBJECT_PATH_IN_USE) == 0) {
555 wpa_printf(MSG_DEBUG, "dbus: %s", error.message);
556 } else {
557 wpa_printf(MSG_ERROR,
558 "dbus: Could not set up message handler for interface %s object %s (error: %s message: %s)",
559 ifname, path, error.name, error.message);
560 }
561 dbus_error_free(&error);
562 return -1;
563 }
564
565 dbus_error_free(&error);
566 return 0;
567 }
568
569
570 static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx);
571
572
573 /**
574 * wpa_dbus_unregister_object_per_iface - Unregisters DBus object
575 * @ctrl_iface: Pointer to dbus private data
576 * @path: DBus path to object which will be unregistered
577 * Returns: Zero on success and -1 on failure
578 *
579 * Unregisters DBus object given by its path
580 */
wpa_dbus_unregister_object_per_iface(struct wpas_dbus_priv * ctrl_iface,const char * path)581 int wpa_dbus_unregister_object_per_iface(
582 struct wpas_dbus_priv *ctrl_iface, const char *path)
583 {
584 DBusConnection *con = ctrl_iface->con;
585 struct wpa_dbus_object_desc *obj_desc = NULL;
586
587 dbus_connection_get_object_path_data(con, path, (void **) &obj_desc);
588 if (!obj_desc) {
589 wpa_printf(MSG_ERROR,
590 "dbus: %s: Could not obtain object's private data: %s",
591 __func__, path);
592 return 0;
593 }
594
595 eloop_cancel_timeout(flush_object_timeout_handler, con, obj_desc);
596
597 if (!dbus_connection_unregister_object_path(con, path))
598 return -1;
599
600 return 0;
601 }
602
603
put_changed_properties(const struct wpa_dbus_object_desc * obj_dsc,const char * interface,DBusMessageIter * dict_iter,int clear_changed)604 static dbus_bool_t put_changed_properties(
605 const struct wpa_dbus_object_desc *obj_dsc, const char *interface,
606 DBusMessageIter *dict_iter, int clear_changed)
607 {
608 DBusMessageIter entry_iter;
609 const struct wpa_dbus_property_desc *dsc;
610 int i;
611 DBusError error;
612
613 for (dsc = obj_dsc->properties, i = 0; dsc && dsc->dbus_property;
614 dsc++, i++) {
615 if (obj_dsc->prop_changed_flags == NULL ||
616 !obj_dsc->prop_changed_flags[i])
617 continue;
618 if (os_strcmp(dsc->dbus_interface, interface) != 0)
619 continue;
620 if (clear_changed)
621 obj_dsc->prop_changed_flags[i] = 0;
622
623 if (!dbus_message_iter_open_container(dict_iter,
624 DBUS_TYPE_DICT_ENTRY,
625 NULL, &entry_iter) ||
626 !dbus_message_iter_append_basic(&entry_iter,
627 DBUS_TYPE_STRING,
628 &dsc->dbus_property))
629 return FALSE;
630
631 dbus_error_init(&error);
632 if (!dsc->getter(dsc, &entry_iter, &error, obj_dsc->user_data))
633 {
634 if (dbus_error_is_set(&error)) {
635 wpa_printf(MSG_ERROR,
636 "dbus: %s: Cannot get new value of property %s: (%s) %s",
637 __func__, dsc->dbus_property,
638 error.name, error.message);
639 } else {
640 wpa_printf(MSG_ERROR,
641 "dbus: %s: Cannot get new value of property %s",
642 __func__, dsc->dbus_property);
643 }
644 dbus_error_free(&error);
645 return FALSE;
646 }
647
648 if (!dbus_message_iter_close_container(dict_iter, &entry_iter))
649 return FALSE;
650 }
651
652 return TRUE;
653 }
654
655
do_send_prop_changed_signal(DBusConnection * con,const char * path,const char * interface,const struct wpa_dbus_object_desc * obj_dsc)656 static void do_send_prop_changed_signal(
657 DBusConnection *con, const char *path, const char *interface,
658 const struct wpa_dbus_object_desc *obj_dsc)
659 {
660 DBusMessage *msg;
661 DBusMessageIter signal_iter, dict_iter;
662
663 msg = dbus_message_new_signal(path, DBUS_INTERFACE_PROPERTIES,
664 "PropertiesChanged");
665 if (msg == NULL)
666 return;
667
668 dbus_message_iter_init_append(msg, &signal_iter);
669
670 if (!dbus_message_iter_append_basic(&signal_iter, DBUS_TYPE_STRING,
671 &interface) ||
672 /* Changed properties dict */
673 !dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
674 "{sv}", &dict_iter) ||
675 !put_changed_properties(obj_dsc, interface, &dict_iter, 0) ||
676 !dbus_message_iter_close_container(&signal_iter, &dict_iter) ||
677 /* Invalidated properties array (empty) */
678 !dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
679 "s", &dict_iter) ||
680 !dbus_message_iter_close_container(&signal_iter, &dict_iter)) {
681 wpa_printf(MSG_DEBUG, "dbus: %s: Failed to construct signal",
682 __func__);
683 } else {
684 dbus_connection_send(con, msg, NULL);
685 }
686
687 dbus_message_unref(msg);
688 }
689
690
do_send_deprecated_prop_changed_signal(DBusConnection * con,const char * path,const char * interface,const struct wpa_dbus_object_desc * obj_dsc)691 static void do_send_deprecated_prop_changed_signal(
692 DBusConnection *con, const char *path, const char *interface,
693 const struct wpa_dbus_object_desc *obj_dsc)
694 {
695 DBusMessage *msg;
696 DBusMessageIter signal_iter, dict_iter;
697
698 msg = dbus_message_new_signal(path, interface, "PropertiesChanged");
699 if (msg == NULL)
700 return;
701
702 dbus_message_iter_init_append(msg, &signal_iter);
703
704 if (!dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
705 "{sv}", &dict_iter) ||
706 !put_changed_properties(obj_dsc, interface, &dict_iter, 1) ||
707 !dbus_message_iter_close_container(&signal_iter, &dict_iter)) {
708 wpa_printf(MSG_DEBUG, "dbus: %s: Failed to construct signal",
709 __func__);
710 } else {
711 dbus_connection_send(con, msg, NULL);
712 }
713
714 dbus_message_unref(msg);
715 }
716
717
send_prop_changed_signal(DBusConnection * con,const char * path,const char * interface,const struct wpa_dbus_object_desc * obj_dsc)718 static void send_prop_changed_signal(
719 DBusConnection *con, const char *path, const char *interface,
720 const struct wpa_dbus_object_desc *obj_dsc)
721 {
722 /*
723 * First, send property change notification on the standardized
724 * org.freedesktop.DBus.Properties interface. This call will not
725 * clear the property change bits, so that they are preserved for
726 * the call that follows.
727 */
728 do_send_prop_changed_signal(con, path, interface, obj_dsc);
729
730 /*
731 * Now send PropertiesChanged on our own interface for backwards
732 * compatibility. This is deprecated and will be removed in a future
733 * release.
734 */
735 do_send_deprecated_prop_changed_signal(con, path, interface, obj_dsc);
736
737 /* Property change bits have now been cleared. */
738 }
739
740
flush_object_timeout_handler(void * eloop_ctx,void * timeout_ctx)741 static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx)
742 {
743 DBusConnection *con = eloop_ctx;
744 struct wpa_dbus_object_desc *obj_desc = timeout_ctx;
745
746 wpa_printf(MSG_MSGDUMP,
747 "dbus: %s: Timeout - sending changed properties of object %s",
748 __func__, obj_desc->path);
749 wpa_dbus_flush_object_changed_properties(con, obj_desc->path);
750 }
751
752
recursive_flush_changed_properties(DBusConnection * con,const char * path)753 static void recursive_flush_changed_properties(DBusConnection *con,
754 const char *path)
755 {
756 char **objects = NULL;
757 char subobj_path[WPAS_DBUS_OBJECT_PATH_MAX];
758 int i;
759
760 wpa_dbus_flush_object_changed_properties(con, path);
761
762 if (!dbus_connection_list_registered(con, path, &objects))
763 goto out;
764
765 for (i = 0; objects[i]; i++) {
766 os_snprintf(subobj_path, WPAS_DBUS_OBJECT_PATH_MAX,
767 "%s/%s", path, objects[i]);
768 recursive_flush_changed_properties(con, subobj_path);
769 }
770
771 out:
772 dbus_free_string_array(objects);
773 }
774
775
776 /**
777 * wpa_dbus_flush_all_changed_properties - Send all PropertiesChanged signals
778 * @con: DBus connection
779 *
780 * Traverses through all registered objects and sends PropertiesChanged for
781 * each properties.
782 */
wpa_dbus_flush_all_changed_properties(DBusConnection * con)783 void wpa_dbus_flush_all_changed_properties(DBusConnection *con)
784 {
785 recursive_flush_changed_properties(con, WPAS_DBUS_NEW_PATH);
786 }
787
788
789 /**
790 * wpa_dbus_flush_object_changed_properties - Send PropertiesChanged for object
791 * @con: DBus connection
792 * @path: path to a DBus object for which PropertiesChanged will be sent.
793 *
794 * Iterates over all properties registered with object and for each interface
795 * containing properties marked as changed, sends a PropertiesChanged signal
796 * containing names and new values of properties that have changed.
797 *
798 * You need to call this function after wpa_dbus_mark_property_changed()
799 * if you want to send PropertiesChanged signal immediately (i.e., without
800 * waiting timeout to expire). PropertiesChanged signal for an object is sent
801 * automatically short time after first marking property as changed. All
802 * PropertiesChanged signals are sent automatically after responding on DBus
803 * message, so if you marked a property changed as a result of DBus call
804 * (e.g., param setter), you usually do not need to call this function.
805 */
wpa_dbus_flush_object_changed_properties(DBusConnection * con,const char * path)806 void wpa_dbus_flush_object_changed_properties(DBusConnection *con,
807 const char *path)
808 {
809 struct wpa_dbus_object_desc *obj_desc = NULL;
810 const struct wpa_dbus_property_desc *dsc;
811 int i;
812
813 dbus_connection_get_object_path_data(con, path, (void **) &obj_desc);
814 if (!obj_desc)
815 return;
816 eloop_cancel_timeout(flush_object_timeout_handler, con, obj_desc);
817
818 for (dsc = obj_desc->properties, i = 0; dsc && dsc->dbus_property;
819 dsc++, i++) {
820 if (obj_desc->prop_changed_flags == NULL ||
821 !obj_desc->prop_changed_flags[i])
822 continue;
823 send_prop_changed_signal(con, path, dsc->dbus_interface,
824 obj_desc);
825 }
826 }
827
828
829 #define WPA_DBUS_SEND_PROP_CHANGED_TIMEOUT 5000
830
831
832 /**
833 * wpa_dbus_mark_property_changed - Mark a property as changed and
834 * @iface: dbus priv struct
835 * @path: path to DBus object which property has changed
836 * @interface: interface containing changed property
837 * @property: property name which has changed
838 *
839 * Iterates over all properties registered with an object and marks the one
840 * given in parameters as changed. All parameters registered for an object
841 * within a single interface will be aggregated together and sent in one
842 * PropertiesChanged signal when function
843 * wpa_dbus_flush_object_changed_properties() is called.
844 */
wpa_dbus_mark_property_changed(struct wpas_dbus_priv * iface,const char * path,const char * interface,const char * property)845 void wpa_dbus_mark_property_changed(struct wpas_dbus_priv *iface,
846 const char *path, const char *interface,
847 const char *property)
848 {
849 struct wpa_dbus_object_desc *obj_desc = NULL;
850 const struct wpa_dbus_property_desc *dsc;
851 int i = 0;
852
853 if (iface == NULL)
854 return;
855
856 dbus_connection_get_object_path_data(iface->con, path,
857 (void **) &obj_desc);
858 if (!obj_desc) {
859 wpa_printf(MSG_ERROR,
860 "dbus: wpa_dbus_property_changed: could not obtain object's private data: %s",
861 path);
862 return;
863 }
864
865 for (dsc = obj_desc->properties; dsc && dsc->dbus_property; dsc++, i++)
866 if (os_strcmp(property, dsc->dbus_property) == 0 &&
867 os_strcmp(interface, dsc->dbus_interface) == 0) {
868 if (obj_desc->prop_changed_flags)
869 obj_desc->prop_changed_flags[i] = 1;
870 break;
871 }
872
873 if (!dsc || !dsc->dbus_property) {
874 wpa_printf(MSG_ERROR,
875 "dbus: wpa_dbus_property_changed: no property %s in object %s",
876 property, path);
877 return;
878 }
879
880 if (!eloop_is_timeout_registered(flush_object_timeout_handler,
881 iface->con, obj_desc)) {
882 eloop_register_timeout(0, WPA_DBUS_SEND_PROP_CHANGED_TIMEOUT,
883 flush_object_timeout_handler,
884 iface->con, obj_desc);
885 }
886 }
887
888
889 /**
890 * wpa_dbus_get_object_properties - Put object's properties into dictionary
891 * @iface: dbus priv struct
892 * @path: path to DBus object which properties will be obtained
893 * @interface: interface name which properties will be obtained
894 * @iter: DBus message iter at which to append property dictionary.
895 *
896 * Iterates over all properties registered with object and execute getters
897 * of those, which are readable and which interface matches interface
898 * specified as argument. Obtained properties values are stored in
899 * dict_iter dictionary.
900 */
wpa_dbus_get_object_properties(struct wpas_dbus_priv * iface,const char * path,const char * interface,DBusMessageIter * iter)901 dbus_bool_t wpa_dbus_get_object_properties(struct wpas_dbus_priv *iface,
902 const char *path,
903 const char *interface,
904 DBusMessageIter *iter)
905 {
906 struct wpa_dbus_object_desc *obj_desc = NULL;
907 DBusMessageIter dict_iter;
908 DBusError error;
909
910 dbus_connection_get_object_path_data(iface->con, path,
911 (void **) &obj_desc);
912 if (!obj_desc) {
913 wpa_printf(MSG_ERROR,
914 "dbus: %s: could not obtain object's private data: %s",
915 __func__, path);
916 return FALSE;
917 }
918
919 if (!wpa_dbus_dict_open_write(iter, &dict_iter)) {
920 wpa_printf(MSG_ERROR, "dbus: %s: failed to open message dict",
921 __func__);
922 return FALSE;
923 }
924
925 dbus_error_init(&error);
926 if (!fill_dict_with_properties(&dict_iter, obj_desc->properties,
927 interface, obj_desc->user_data,
928 &error)) {
929 wpa_printf(MSG_ERROR,
930 "dbus: %s: failed to get object properties: (%s) %s",
931 __func__,
932 dbus_error_is_set(&error) ? error.name : "none",
933 dbus_error_is_set(&error) ? error.message : "none");
934 dbus_error_free(&error);
935 wpa_dbus_dict_close_write(iter, &dict_iter);
936 return FALSE;
937 }
938
939 return wpa_dbus_dict_close_write(iter, &dict_iter);
940 }
941
942 /**
943 * wpas_dbus_new_decompose_object_path - Decompose an interface object path into parts
944 * @path: The dbus object path
945 * @sep: Separating part (e.g., "Networks" or "PersistentGroups")
946 * @item: (out) The part following the specified separator, if any
947 * Returns: The object path of the interface this path refers to
948 *
949 * For a given object path, decomposes the object path into object id and
950 * requested part, if those parts exist. The caller is responsible for freeing
951 * the returned value. The *item pointer points to that allocated value and must
952 * not be freed separately.
953 *
954 * As an example, path = "/fi/w1/wpa_supplicant1/Interfaces/1/Networks/0" and
955 * sep = "Networks" would result in "/fi/w1/wpa_supplicant1/Interfaces/1"
956 * getting returned and *items set to point to "0".
957 */
wpas_dbus_new_decompose_object_path(const char * path,const char * sep,char ** item)958 char * wpas_dbus_new_decompose_object_path(const char *path, const char *sep,
959 char **item)
960 {
961 const unsigned int dev_path_prefix_len =
962 os_strlen(WPAS_DBUS_NEW_PATH_INTERFACES "/");
963 char *obj_path_only;
964 char *pos;
965 size_t sep_len;
966
967 *item = NULL;
968
969 /* Verify that this starts with our interface prefix */
970 if (os_strncmp(path, WPAS_DBUS_NEW_PATH_INTERFACES "/",
971 dev_path_prefix_len) != 0)
972 return NULL; /* not our path */
973
974 /* Ensure there's something at the end of the path */
975 if ((path + dev_path_prefix_len)[0] == '\0')
976 return NULL;
977
978 obj_path_only = os_strdup(path);
979 if (obj_path_only == NULL)
980 return NULL;
981
982 pos = obj_path_only + dev_path_prefix_len;
983 pos = os_strchr(pos, '/');
984 if (pos == NULL)
985 return obj_path_only; /* no next item on the path */
986
987 /* Separate network interface prefix from the path */
988 *pos++ = '\0';
989
990 sep_len = os_strlen(sep);
991 if (os_strncmp(pos, sep, sep_len) != 0 || pos[sep_len] != '/')
992 return obj_path_only; /* no match */
993
994 /* return a pointer to the requested item */
995 *item = pos + sep_len + 1;
996 return obj_path_only;
997 }
998
999
1000 /**
1001 * wpas_dbus_reply_new_from_error - Create a new D-Bus error message from a
1002 * dbus error structure
1003 * @message: The original request message for which the error is a reply
1004 * @error: The error containing a name and a descriptive error cause
1005 * @fallback_name: A generic error name if @error was not set
1006 * @fallback_string: A generic error string if @error was not set
1007 * Returns: A new D-Bus error message
1008 *
1009 * Given a DBusMessage structure, creates a new D-Bus error message using
1010 * the error name and string contained in that structure.
1011 */
wpas_dbus_reply_new_from_error(DBusMessage * message,DBusError * error,const char * fallback_name,const char * fallback_string)1012 DBusMessage * wpas_dbus_reply_new_from_error(DBusMessage *message,
1013 DBusError *error,
1014 const char *fallback_name,
1015 const char *fallback_string)
1016 {
1017 if (error && error->name && error->message) {
1018 return dbus_message_new_error(message, error->name,
1019 error->message);
1020 }
1021 if (fallback_name && fallback_string) {
1022 return dbus_message_new_error(message, fallback_name,
1023 fallback_string);
1024 }
1025 return NULL;
1026 }
1027
1028
1029 /**
1030 * wpas_dbus_new_from_signal_information - Adds a wpa_signal_info
1031 * to a DBusMessage.
1032 * @msg: Pointer to message to append fields to
1033 * @si: Pointer to wpa_signal_info to add to the message
1034 * Returns: 0 on success, otherwise, an errorcode
1035 *
1036 * Adds all the pertinent fields from a wpa_signal_info to a DBusMessage.
1037 * The same logic is useful in both responding to signal_poll calls, and
1038 * sending signal_change signals.
1039 */
wpas_dbus_new_from_signal_information(DBusMessageIter * iter,struct wpa_signal_info * si)1040 int wpas_dbus_new_from_signal_information(DBusMessageIter *iter,
1041 struct wpa_signal_info *si)
1042 {
1043 DBusMessageIter iter_dict, variant_iter;
1044
1045 if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
1046 "a{sv}", &variant_iter) ||
1047 !wpa_dbus_dict_open_write(&variant_iter, &iter_dict) ||
1048 !wpa_dbus_dict_append_int32(&iter_dict, "rssi",
1049 si->data.signal) ||
1050 !wpa_dbus_dict_append_uint32(&iter_dict, "linkspeed",
1051 si->data.current_tx_rate / 1000) ||
1052 !wpa_dbus_dict_append_int32(&iter_dict, "noise",
1053 si->current_noise) ||
1054 !wpa_dbus_dict_append_uint32(&iter_dict, "frequency",
1055 si->frequency) ||
1056 (si->chanwidth != CHAN_WIDTH_UNKNOWN &&
1057 !wpa_dbus_dict_append_string(
1058 &iter_dict, "width",
1059 channel_width_to_string(si->chanwidth))) ||
1060 (si->center_frq1 > 0 && si->center_frq2 > 0 &&
1061 (!wpa_dbus_dict_append_int32(&iter_dict, "center-frq1",
1062 si->center_frq1) ||
1063 !wpa_dbus_dict_append_int32(&iter_dict, "center-frq2",
1064 si->center_frq2))) ||
1065 (si->data.avg_signal &&
1066 !wpa_dbus_dict_append_int32(&iter_dict, "avg-rssi",
1067 si->data.avg_signal)) ||
1068 (si->data.rx_bytes &&
1069 !wpa_dbus_dict_append_uint64(&iter_dict, "rx-bytes",
1070 si->data.rx_bytes)) ||
1071 (si->data.tx_bytes &&
1072 !wpa_dbus_dict_append_uint64(&iter_dict, "tx-bytes",
1073 si->data.tx_bytes)) ||
1074 (si->data.rx_packets &&
1075 !wpa_dbus_dict_append_uint32(&iter_dict, "rx-packets",
1076 si->data.rx_packets)) ||
1077 (si->data.tx_packets &&
1078 !wpa_dbus_dict_append_uint32(&iter_dict, "tx-packets",
1079 si->data.tx_packets)) ||
1080 (si->data.beacons_count &&
1081 !wpa_dbus_dict_append_uint64(&iter_dict, "beacons",
1082 si->data.beacons_count)) ||
1083 (si->data.current_rx_rate &&
1084 !wpa_dbus_dict_append_uint32(&iter_dict, "linkrxspeed",
1085 si->data.current_rx_rate)) ||
1086 (si->data.current_rx_rate &&
1087 !wpa_dbus_dict_append_uint32(&iter_dict, "linktxspeed",
1088 si->data.current_tx_rate)) ||
1089 (si->data.inactive_msec &&
1090 !wpa_dbus_dict_append_uint32(&iter_dict, "inactive-time",
1091 si->data.inactive_msec)) ||
1092 (si->data.tx_retry_failed &&
1093 !wpa_dbus_dict_append_uint32(&iter_dict, "retries-failed",
1094 si->data.tx_retry_failed)) ||
1095 (si->data.tx_retry_count &&
1096 !wpa_dbus_dict_append_uint32(&iter_dict, "retries",
1097 si->data.tx_retry_count)) ||
1098 (si->data.last_ack_rssi &&
1099 !wpa_dbus_dict_append_int32(&iter_dict, "last-ack-rssi",
1100 si->data.last_ack_rssi)) ||
1101 (si->data.fcs_error_count &&
1102 !wpa_dbus_dict_append_uint32(&iter_dict, "fcs-errors",
1103 si->data.fcs_error_count)) ||
1104 (si->data.beacon_loss_count &&
1105 !wpa_dbus_dict_append_uint32(&iter_dict, "beacon-losses",
1106 si->data.beacon_loss_count)) ||
1107 (si->data.expected_throughput &&
1108 !wpa_dbus_dict_append_uint32(&iter_dict, "expected-throughput",
1109 si->data.expected_throughput)) ||
1110 (si->data.rx_drop_misc &&
1111 !wpa_dbus_dict_append_uint32(&iter_dict, "rx-drop-misc",
1112 si->data.rx_drop_misc)) ||
1113 (si->data.rx_mpdus &&
1114 !wpa_dbus_dict_append_uint32(&iter_dict, "rx-mpdus",
1115 si->data.rx_mpdus)) ||
1116 (si->data.rx_hemcs &&
1117 !wpa_dbus_dict_append_uint32(&iter_dict, "rx-he-mcs",
1118 si->data.rx_hemcs)) ||
1119 (si->data.tx_hemcs &&
1120 !wpa_dbus_dict_append_uint32(&iter_dict, "tx-he-mcs",
1121 si->data.tx_hemcs)) ||
1122 (si->data.rx_vhtmcs &&
1123 !wpa_dbus_dict_append_uint32(&iter_dict, "rx-vht-mcs",
1124 si->data.rx_vhtmcs)) ||
1125 (si->data.tx_vhtmcs &&
1126 !wpa_dbus_dict_append_uint32(&iter_dict, "tx-vht-mcs",
1127 si->data.tx_vhtmcs)) ||
1128 (si->data.rx_mcs &&
1129 !wpa_dbus_dict_append_uint32(&iter_dict, "rx-mcs",
1130 si->data.rx_mcs)) ||
1131 (si->data.tx_mcs &&
1132 !wpa_dbus_dict_append_uint32(&iter_dict, "tx-mcs",
1133 si->data.tx_mcs)) ||
1134 (si->data.rx_he_nss &&
1135 !wpa_dbus_dict_append_uint32(&iter_dict, "rx-he-nss",
1136 si->data.rx_he_nss)) ||
1137 (si->data.tx_he_nss &&
1138 !wpa_dbus_dict_append_uint32(&iter_dict, "tx-he-nss",
1139 si->data.tx_he_nss)) ||
1140 (si->data.rx_vht_nss &&
1141 !wpa_dbus_dict_append_uint32(&iter_dict, "rx-vht-nss",
1142 si->data.rx_vht_nss)) ||
1143 (si->data.tx_vht_nss &&
1144 !wpa_dbus_dict_append_uint32(&iter_dict, "tx-vht-nss",
1145 si->data.tx_vht_nss)) ||
1146 (si->data.avg_beacon_signal &&
1147 !wpa_dbus_dict_append_int32(&iter_dict, "avg-beacon-rssi",
1148 si->data.avg_beacon_signal)) ||
1149 (si->data.avg_ack_signal &&
1150 !wpa_dbus_dict_append_int32(&iter_dict, "avg-ack-rssi",
1151 si->data.avg_ack_signal)) ||
1152 !wpa_dbus_dict_close_write(&variant_iter, &iter_dict) ||
1153 !dbus_message_iter_close_container(iter, &variant_iter))
1154 return -ENOMEM;
1155
1156 return 0;
1157 }
1158