2 * WPA Supplicant / dbus-based control interface
3 * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
10 #include <dbus/dbus.h>
15 #include "../config.h"
16 #include "../wpa_supplicant_i.h"
19 #include "dbus_old_handlers.h"
20 #include "dbus_common_i.h"
24 * wpas_dbus_decompose_object_path - Decompose an interface object path into parts
25 * @path: The dbus object path
26 * @network: (out) the configured network this object path refers to, if any
27 * @bssid: (out) the scanned bssid this object path refers to, if any
28 * Returns: The object path of the network interface this path refers to
30 * For a given object path, decomposes the object path into object id, network,
31 * and BSSID parts, if those parts exist.
33 char * wpas_dbus_decompose_object_path(const char *path, char **network,
36 const unsigned int dev_path_prefix_len =
37 strlen(WPAS_DBUS_PATH_INTERFACES "/");
41 /* Be a bit paranoid about path */
42 if (!path || strncmp(path, WPAS_DBUS_PATH_INTERFACES "/",
46 /* Ensure there's something at the end of the path */
47 if ((path + dev_path_prefix_len)[0] == '\0')
50 obj_path_only = os_strdup(path);
51 if (obj_path_only == NULL)
54 next_sep = strchr(obj_path_only + dev_path_prefix_len, '/');
55 if (next_sep != NULL) {
56 const char *net_part = strstr(next_sep,
57 WPAS_DBUS_NETWORKS_PART "/");
58 const char *bssid_part = strstr(next_sep,
59 WPAS_DBUS_BSSIDS_PART "/");
61 if (network && net_part) {
62 /* Deal with a request for a configured network */
63 const char *net_name = net_part +
64 strlen(WPAS_DBUS_NETWORKS_PART "/");
67 *network = os_strdup(net_name);
68 } else if (bssid && bssid_part) {
69 /* Deal with a request for a scanned BSSID */
70 const char *bssid_name = bssid_part +
71 strlen(WPAS_DBUS_BSSIDS_PART "/");
72 if (strlen(bssid_name))
73 *bssid = os_strdup(bssid_name);
78 /* Cut off interface object path before "/" */
87 * wpas_dbus_new_invalid_iface_error - Return a new invalid interface error message
88 * @message: Pointer to incoming dbus message this error refers to
89 * Returns: A dbus error message
91 * Convenience function to create and return an invalid interface error
93 DBusMessage * wpas_dbus_new_invalid_iface_error(DBusMessage *message)
95 return dbus_message_new_error(message, WPAS_ERROR_INVALID_IFACE,
96 "wpa_supplicant knows nothing about "
102 * wpas_dbus_new_invalid_network_error - Return a new invalid network error message
103 * @message: Pointer to incoming dbus message this error refers to
104 * Returns: a dbus error message
106 * Convenience function to create and return an invalid network error
108 DBusMessage * wpas_dbus_new_invalid_network_error(DBusMessage *message)
110 return dbus_message_new_error(message, WPAS_ERROR_INVALID_NETWORK,
111 "The requested network does not exist.");
116 * wpas_dbus_new_invalid_bssid_error - Return a new invalid bssid error message
117 * @message: Pointer to incoming dbus message this error refers to
118 * Returns: a dbus error message
120 * Convenience function to create and return an invalid bssid error
122 static DBusMessage * wpas_dbus_new_invalid_bssid_error(DBusMessage *message)
124 return dbus_message_new_error(message, WPAS_ERROR_INVALID_BSSID,
125 "The BSSID requested was invalid.");
130 * wpas_dispatch_network_method - dispatch messages for configured networks
131 * @message: the incoming dbus message
132 * @wpa_s: a network interface's data
133 * @network_id: id of the configured network we're interested in
134 * Returns: a reply dbus message, or a dbus error message
136 * This function dispatches all incoming dbus messages for configured networks.
138 static DBusMessage * wpas_dispatch_network_method(DBusMessage *message,
139 struct wpa_supplicant *wpa_s,
142 DBusMessage *reply = NULL;
143 const char *method = dbus_message_get_member(message);
144 struct wpa_ssid *ssid;
146 ssid = wpa_config_get_network(wpa_s->conf, network_id);
148 return wpas_dbus_new_invalid_network_error(message);
150 if (!strcmp(method, "set"))
151 reply = wpas_dbus_iface_set_network(message, wpa_s, ssid);
152 else if (!strcmp(method, "enable"))
153 reply = wpas_dbus_iface_enable_network(message, wpa_s, ssid);
154 else if (!strcmp(method, "disable"))
155 reply = wpas_dbus_iface_disable_network(message, wpa_s, ssid);
162 * wpas_dispatch_bssid_method - dispatch messages for scanned networks
163 * @message: the incoming dbus message
164 * @wpa_s: a network interface's data
165 * @bssid: bssid of the scanned network we're interested in
166 * Returns: a reply dbus message, or a dbus error message
168 * This function dispatches all incoming dbus messages for scanned networks.
170 static DBusMessage * wpas_dispatch_bssid_method(DBusMessage *message,
171 struct wpa_supplicant *wpa_s,
172 const char *bssid_txt)
177 if (hexstr2bin(bssid_txt, bssid, ETH_ALEN) < 0)
178 return wpas_dbus_new_invalid_bssid_error(message);
180 bss = wpa_bss_get_bssid(wpa_s, bssid);
182 return wpas_dbus_new_invalid_bssid_error(message);
184 /* Dispatch the method call against the scanned bssid */
185 if (os_strcmp(dbus_message_get_member(message), "properties") == 0)
186 return wpas_dbus_bssid_properties(message, wpa_s, bss);
193 * wpas_iface_message_handler - Dispatch messages for interfaces or networks
194 * @connection: Connection to the system message bus
195 * @message: An incoming dbus message
196 * @user_data: A pointer to a dbus control interface data structure
197 * Returns: Whether or not the message was handled
199 * This function dispatches all incoming dbus messages for network interfaces,
200 * or objects owned by them, such as scanned BSSIDs and configured networks.
202 static DBusHandlerResult wpas_iface_message_handler(DBusConnection *connection,
203 DBusMessage *message,
206 struct wpa_supplicant *wpa_s = user_data;
207 const char *method = dbus_message_get_member(message);
208 const char *path = dbus_message_get_path(message);
209 const char *msg_interface = dbus_message_get_interface(message);
210 char *iface_obj_path = NULL;
211 char *network = NULL;
213 DBusMessage *reply = NULL;
215 /* Caller must specify a message interface */
219 wpa_printf(MSG_MSGDUMP, "dbus[old/iface]: %s.%s (%s) [%s]",
220 msg_interface, method, path,
221 dbus_message_get_signature(message));
223 iface_obj_path = wpas_dbus_decompose_object_path(path, &network,
225 if (iface_obj_path == NULL) {
226 reply = wpas_dbus_new_invalid_iface_error(message);
230 /* Make sure the message's object path actually refers to the
231 * wpa_supplicant structure it's supposed to (which is wpa_s)
233 if (wpa_supplicant_get_iface_by_dbus_path(wpa_s->global,
234 iface_obj_path) != wpa_s) {
235 reply = wpas_dbus_new_invalid_iface_error(message);
239 if (network && !strcmp(msg_interface, WPAS_DBUS_IFACE_NETWORK)) {
240 /* A method for one of this interface's configured networks */
241 int nid = strtoul(network, NULL, 10);
243 reply = wpas_dispatch_network_method(message, wpa_s,
246 reply = wpas_dbus_new_invalid_network_error(message);
247 } else if (bssid && !strcmp(msg_interface, WPAS_DBUS_IFACE_BSSID)) {
248 /* A method for one of this interface's scanned BSSIDs */
249 reply = wpas_dispatch_bssid_method(message, wpa_s, bssid);
250 } else if (!strcmp(msg_interface, WPAS_DBUS_IFACE_INTERFACE)) {
251 /* A method for an interface only. */
252 if (!strcmp(method, "scan"))
253 reply = wpas_dbus_iface_scan(message, wpa_s);
254 else if (!strcmp(method, "scanResults"))
255 reply = wpas_dbus_iface_scan_results(message, wpa_s);
256 else if (!strcmp(method, "addNetwork"))
257 reply = wpas_dbus_iface_add_network(message, wpa_s);
258 else if (!strcmp(method, "removeNetwork"))
259 reply = wpas_dbus_iface_remove_network(message, wpa_s);
260 else if (!strcmp(method, "selectNetwork"))
261 reply = wpas_dbus_iface_select_network(message, wpa_s);
262 else if (!strcmp(method, "capabilities"))
263 reply = wpas_dbus_iface_capabilities(message, wpa_s);
264 else if (!strcmp(method, "disconnect"))
265 reply = wpas_dbus_iface_disconnect(message, wpa_s);
266 else if (!strcmp(method, "setAPScan"))
267 reply = wpas_dbus_iface_set_ap_scan(message, wpa_s);
268 else if (!strcmp(method, "setSmartcardModules"))
269 reply = wpas_dbus_iface_set_smartcard_modules(message,
271 else if (!strcmp(method, "state"))
272 reply = wpas_dbus_iface_get_state(message, wpa_s);
273 else if (!strcmp(method, "scanning"))
274 reply = wpas_dbus_iface_get_scanning(message, wpa_s);
275 #ifndef CONFIG_NO_CONFIG_BLOBS
276 else if (!strcmp(method, "setBlobs"))
277 reply = wpas_dbus_iface_set_blobs(message, wpa_s);
278 else if (!strcmp(method, "removeBlobs"))
279 reply = wpas_dbus_iface_remove_blobs(message, wpa_s);
280 #endif /* CONFIG_NO_CONFIG_BLOBS */
282 else if (!os_strcmp(method, "wpsPbc"))
283 reply = wpas_dbus_iface_wps_pbc(message, wpa_s);
284 else if (!os_strcmp(method, "wpsPin"))
285 reply = wpas_dbus_iface_wps_pin(message, wpa_s);
286 else if (!os_strcmp(method, "wpsReg"))
287 reply = wpas_dbus_iface_wps_reg(message, wpa_s);
288 #endif /* CONFIG_WPS */
289 else if (!os_strcmp(method, "flush"))
290 reply = wpas_dbus_iface_flush(message, wpa_s);
293 /* If the message was handled, send back the reply */
295 if (!dbus_message_get_no_reply(message))
296 dbus_connection_send(connection, reply, NULL);
297 dbus_message_unref(reply);
301 os_free(iface_obj_path);
304 return reply ? DBUS_HANDLER_RESULT_HANDLED :
305 DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
310 * wpas_message_handler - dispatch incoming dbus messages
311 * @connection: connection to the system message bus
312 * @message: an incoming dbus message
313 * @user_data: a pointer to a dbus control interface data structure
314 * Returns: whether or not the message was handled
316 * This function dispatches all incoming dbus messages to the correct
317 * handlers, depending on what the message's target object path is,
318 * and what the method call is.
320 static DBusHandlerResult wpas_message_handler(DBusConnection *connection,
321 DBusMessage *message, void *user_data)
323 struct wpas_dbus_priv *ctrl_iface = user_data;
326 const char *msg_interface;
327 DBusMessage *reply = NULL;
329 method = dbus_message_get_member(message);
330 path = dbus_message_get_path(message);
331 msg_interface = dbus_message_get_interface(message);
332 if (!method || !path || !ctrl_iface || !msg_interface)
333 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
335 wpa_printf(MSG_MSGDUMP, "dbus[old]: %s.%s (%s) [%s]",
336 msg_interface, method, path,
337 dbus_message_get_signature(message));
339 /* Validate the method interface */
340 if (strcmp(msg_interface, WPAS_DBUS_INTERFACE) != 0)
341 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
343 if (!strcmp(path, WPAS_DBUS_PATH)) {
344 /* dispatch methods against our global dbus interface here */
345 if (!strcmp(method, "addInterface")) {
346 reply = wpas_dbus_global_add_interface(
347 message, ctrl_iface->global);
348 } else if (!strcmp(method, "removeInterface")) {
349 reply = wpas_dbus_global_remove_interface(
350 message, ctrl_iface->global);
351 } else if (!strcmp(method, "getInterface")) {
352 reply = wpas_dbus_global_get_interface(
353 message, ctrl_iface->global);
354 } else if (!strcmp(method, "setDebugParams")) {
355 reply = wpas_dbus_global_set_debugparams(
356 message, ctrl_iface->global);
360 /* If the message was handled, send back the reply */
362 if (!dbus_message_get_no_reply(message))
363 dbus_connection_send(connection, reply, NULL);
364 dbus_message_unref(reply);
367 return reply ? DBUS_HANDLER_RESULT_HANDLED :
368 DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
373 * wpa_supplicant_dbus_notify_scan_results - Send a scan results signal
374 * @wpa_s: %wpa_supplicant network interface data
375 * Returns: 0 on success, -1 on failure
377 * Notify listeners that this interface has updated scan results.
379 void wpa_supplicant_dbus_notify_scan_results(struct wpa_supplicant *wpa_s)
381 struct wpas_dbus_priv *iface = wpa_s->global->dbus;
382 DBusMessage *_signal;
384 /* Do nothing if the control interface is not turned on */
388 _signal = dbus_message_new_signal(wpa_s->dbus_path,
389 WPAS_DBUS_IFACE_INTERFACE,
390 "ScanResultsAvailable");
391 if (_signal == NULL) {
392 wpa_printf(MSG_ERROR, "dbus: Not enough memory to send scan "
396 dbus_connection_send(iface->con, _signal, NULL);
397 dbus_message_unref(_signal);
402 * wpa_supplicant_dbus_notify_state_change - Send a state change signal
403 * @wpa_s: %wpa_supplicant network interface data
404 * @new_state: new state wpa_supplicant is entering
405 * @old_state: old state wpa_supplicant is leaving
406 * Returns: 0 on success, -1 on failure
408 * Notify listeners that wpa_supplicant has changed state
410 void wpa_supplicant_dbus_notify_state_change(struct wpa_supplicant *wpa_s,
411 enum wpa_states new_state,
412 enum wpa_states old_state)
414 struct wpas_dbus_priv *iface;
415 DBusMessage *_signal = NULL;
416 const char *new_state_str, *old_state_str;
418 if (wpa_s->dbus_path == NULL)
419 return; /* Skip signal since D-Bus setup is not yet ready */
421 /* Do nothing if the control interface is not turned on */
422 if (wpa_s->global == NULL)
424 iface = wpa_s->global->dbus;
428 /* Only send signal if state really changed */
429 if (new_state == old_state)
432 _signal = dbus_message_new_signal(wpa_s->dbus_path,
433 WPAS_DBUS_IFACE_INTERFACE,
435 if (_signal == NULL) {
436 wpa_printf(MSG_ERROR,
437 "dbus: wpa_supplicant_dbus_notify_state_change: "
438 "could not create dbus signal; likely out of "
443 new_state_str = wpa_supplicant_state_txt(new_state);
444 old_state_str = wpa_supplicant_state_txt(old_state);
446 if (!dbus_message_append_args(_signal,
447 DBUS_TYPE_STRING, &new_state_str,
448 DBUS_TYPE_STRING, &old_state_str,
449 DBUS_TYPE_INVALID)) {
450 wpa_printf(MSG_ERROR,
451 "dbus: wpa_supplicant_dbus_notify_state_change: "
452 "Not enough memory to construct state change "
457 dbus_connection_send(iface->con, _signal, NULL);
460 dbus_message_unref(_signal);
465 * wpa_supplicant_dbus_notify_scanning - send scanning status
466 * @wpa_s: %wpa_supplicant network interface data
467 * Returns: 0 on success, -1 on failure
469 * Notify listeners of interface scanning state changes
471 void wpa_supplicant_dbus_notify_scanning(struct wpa_supplicant *wpa_s)
473 struct wpas_dbus_priv *iface = wpa_s->global->dbus;
474 DBusMessage *_signal;
475 dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
477 /* Do nothing if the control interface is not turned on */
481 _signal = dbus_message_new_signal(wpa_s->dbus_path,
482 WPAS_DBUS_IFACE_INTERFACE,
484 if (_signal == NULL) {
485 wpa_printf(MSG_ERROR, "dbus: Not enough memory to send scan "
490 if (dbus_message_append_args(_signal,
491 DBUS_TYPE_BOOLEAN, &scanning,
492 DBUS_TYPE_INVALID)) {
493 dbus_connection_send(iface->con, _signal, NULL);
495 wpa_printf(MSG_ERROR, "dbus: Not enough memory to construct "
498 dbus_message_unref(_signal);
503 void wpa_supplicant_dbus_notify_wps_cred(struct wpa_supplicant *wpa_s,
504 const struct wps_credential *cred)
506 struct wpas_dbus_priv *iface;
507 DBusMessage *_signal = NULL;
509 /* Do nothing if the control interface is not turned on */
510 if (wpa_s->global == NULL)
512 iface = wpa_s->global->dbus;
516 _signal = dbus_message_new_signal(wpa_s->dbus_path,
517 WPAS_DBUS_IFACE_INTERFACE,
519 if (_signal == NULL) {
520 wpa_printf(MSG_ERROR,
521 "dbus: wpa_supplicant_dbus_notify_wps_cred: "
522 "Could not create dbus signal; likely out of "
527 if (!dbus_message_append_args(_signal,
528 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
529 &cred->cred_attr, cred->cred_attr_len,
530 DBUS_TYPE_INVALID)) {
531 wpa_printf(MSG_ERROR,
532 "dbus: wpa_supplicant_dbus_notify_wps_cred: "
533 "Not enough memory to construct signal");
537 dbus_connection_send(iface->con, _signal, NULL);
540 dbus_message_unref(_signal);
542 #else /* CONFIG_WPS */
543 void wpa_supplicant_dbus_notify_wps_cred(struct wpa_supplicant *wpa_s,
544 const struct wps_credential *cred)
547 #endif /* CONFIG_WPS */
549 void wpa_supplicant_dbus_notify_certification(struct wpa_supplicant *wpa_s,
550 int depth, const char *subject,
551 const char *cert_hash,
552 const struct wpabuf *cert)
554 struct wpas_dbus_priv *iface;
555 DBusMessage *_signal = NULL;
557 const char *cert_hex;
560 /* Do nothing if the control interface is not turned on */
561 if (wpa_s->global == NULL)
563 iface = wpa_s->global->dbus;
567 _signal = dbus_message_new_signal(wpa_s->dbus_path,
568 WPAS_DBUS_IFACE_INTERFACE,
570 if (_signal == NULL) {
571 wpa_printf(MSG_ERROR,
572 "dbus: wpa_supplicant_dbus_notify_certification: "
573 "Could not create dbus signal; likely out of "
578 hash = cert_hash ? cert_hash : "";
579 cert_hex = cert ? wpabuf_head(cert) : "";
580 cert_hex_len = cert ? wpabuf_len(cert) : 0;
582 if (!dbus_message_append_args(_signal,
583 DBUS_TYPE_INT32,&depth,
584 DBUS_TYPE_STRING, &subject,
585 DBUS_TYPE_STRING, &hash,
586 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
587 &cert_hex, cert_hex_len,
588 DBUS_TYPE_INVALID)) {
589 wpa_printf(MSG_ERROR,
590 "dbus: wpa_supplicant_dbus_notify_certification: "
591 "Not enough memory to construct signal");
595 dbus_connection_send(iface->con, _signal, NULL);
598 dbus_message_unref(_signal);
604 * wpa_supplicant_dbus_ctrl_iface_init - Initialize dbus control interface
605 * @global: Pointer to global data from wpa_supplicant_init()
606 * Returns: 0 on success, -1 on failure
608 * Initialize the dbus control interface and start receiving commands from
609 * external programs over the bus.
611 int wpa_supplicant_dbus_ctrl_iface_init(struct wpas_dbus_priv *iface)
615 DBusObjectPathVTable wpas_vtable = {
616 NULL, &wpas_message_handler, NULL, NULL, NULL, NULL
619 /* Register the message handler for the global dbus interface */
620 if (!dbus_connection_register_object_path(iface->con,
621 WPAS_DBUS_PATH, &wpas_vtable,
623 wpa_printf(MSG_ERROR, "dbus: Could not set up message "
628 /* Register our service with the message bus */
629 dbus_error_init(&error);
630 switch (dbus_bus_request_name(iface->con, WPAS_DBUS_SERVICE,
632 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
635 case DBUS_REQUEST_NAME_REPLY_EXISTS:
636 case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
637 case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
638 wpa_printf(MSG_ERROR, "dbus: Could not request service name: "
639 "already registered");
642 wpa_printf(MSG_ERROR, "dbus: Could not request service name: "
643 "%s %s", error.name, error.message);
646 dbus_error_free(&error);
651 wpa_printf(MSG_DEBUG, "Providing DBus service '" WPAS_DBUS_SERVICE
659 * wpas_dbus_register_new_iface - Register a new interface with dbus
660 * @wpa_s: %wpa_supplicant interface description structure to register
661 * Returns: 0 on success, -1 on error
663 * Registers a new interface with dbus and assigns it a dbus object path.
665 int wpas_dbus_register_iface(struct wpa_supplicant *wpa_s)
667 struct wpas_dbus_priv *ctrl_iface = wpa_s->global->dbus;
668 DBusConnection * con;
670 DBusObjectPathVTable vtable = {
671 NULL, &wpas_iface_message_handler, NULL, NULL, NULL, NULL
674 /* Do nothing if the control interface is not turned on */
675 if (ctrl_iface == NULL)
678 con = ctrl_iface->con;
679 next = ctrl_iface->next_objid++;
681 /* Create and set the interface's object path */
682 wpa_s->dbus_path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
683 if (wpa_s->dbus_path == NULL)
685 os_snprintf(wpa_s->dbus_path, WPAS_DBUS_OBJECT_PATH_MAX,
686 WPAS_DBUS_PATH_INTERFACES "/%u",
689 /* Register the message handler for the interface functions */
690 if (!dbus_connection_register_fallback(con, wpa_s->dbus_path, &vtable,
692 wpa_printf(MSG_ERROR, "dbus: Could not set up message "
693 "handler for interface %s", wpa_s->ifname);
702 * wpas_dbus_unregister_iface - Unregister an interface from dbus
703 * @wpa_s: wpa_supplicant interface structure
704 * Returns: 0 on success, -1 on failure
706 * Unregisters the interface with dbus
708 int wpas_dbus_unregister_iface(struct wpa_supplicant *wpa_s)
710 struct wpas_dbus_priv *ctrl_iface;
713 /* Do nothing if the control interface is not turned on */
714 if (wpa_s == NULL || wpa_s->global == NULL)
716 ctrl_iface = wpa_s->global->dbus;
717 if (ctrl_iface == NULL)
720 con = ctrl_iface->con;
721 if (!dbus_connection_unregister_object_path(con, wpa_s->dbus_path))
724 os_free(wpa_s->dbus_path);
725 wpa_s->dbus_path = NULL;
732 * wpa_supplicant_get_iface_by_dbus_path - Get a new network interface
733 * @global: Pointer to global data from wpa_supplicant_init()
734 * @path: Pointer to a dbus object path representing an interface
735 * Returns: Pointer to the interface or %NULL if not found
737 struct wpa_supplicant * wpa_supplicant_get_iface_by_dbus_path(
738 struct wpa_global *global, const char *path)
740 struct wpa_supplicant *wpa_s;
742 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
743 if (strcmp(wpa_s->dbus_path, path) == 0)