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>
13 #include "eap_peer/eap_methods.h"
14 #include "common/ieee802_11_defs.h"
15 #include "eapol_supp/eapol_supp_sm.h"
16 #include "rsn_supp/wpa.h"
17 #include "../config.h"
18 #include "../wpa_supplicant_i.h"
19 #include "../driver_i.h"
20 #include "../notify.h"
21 #include "../wpas_glue.h"
25 #include "dbus_old_handlers.h"
26 #include "dbus_dict_helpers.h"
29 * wpas_dbus_new_invalid_opts_error - Return a new invalid options error message
30 * @message: Pointer to incoming dbus message this error refers to
31 * Returns: a dbus error message
33 * Convenience function to create and return an invalid options error
35 DBusMessage * wpas_dbus_new_invalid_opts_error(DBusMessage *message,
40 reply = dbus_message_new_error(
41 message, WPAS_ERROR_INVALID_OPTS,
42 "Did not receive correct message arguments.");
44 dbus_message_append_args(reply, DBUS_TYPE_STRING, &arg,
52 * wpas_dbus_new_success_reply - Return a new success reply message
53 * @message: Pointer to incoming dbus message this reply refers to
54 * Returns: a dbus message containing a single UINT32 that indicates
55 * success (ie, a value of 1)
57 * Convenience function to create and return a success reply message
59 DBusMessage * wpas_dbus_new_success_reply(DBusMessage *message)
62 unsigned int success = 1;
64 reply = dbus_message_new_method_return(message);
65 dbus_message_append_args(reply, DBUS_TYPE_UINT32, &success,
72 * wpas_dbus_global_add_interface - Request registration of a network interface
73 * @message: Pointer to incoming dbus message
74 * @global: %wpa_supplicant global data structure
75 * Returns: The object path of the new interface object,
76 * or a dbus error message with more information
78 * Handler function for "addInterface" method call. Handles requests
79 * by dbus clients to register a network interface that wpa_supplicant
82 DBusMessage * wpas_dbus_global_add_interface(DBusMessage *message,
83 struct wpa_global *global)
87 char *driver_param = NULL;
88 char *confname = NULL;
89 char *bridge_ifname = NULL;
90 DBusMessage *reply = NULL;
93 dbus_message_iter_init(message, &iter);
95 /* First argument: interface name (DBUS_TYPE_STRING)
96 * Required; must be non-zero length
98 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
100 dbus_message_iter_get_basic(&iter, &ifname);
101 if (!os_strlen(ifname))
104 /* Second argument: dict of options */
105 if (dbus_message_iter_next(&iter)) {
106 DBusMessageIter iter_dict;
107 struct wpa_dbus_dict_entry entry;
109 if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
111 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
112 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
114 if (!strcmp(entry.key, "driver") &&
115 entry.type == DBUS_TYPE_STRING) {
117 driver = os_strdup(entry.str_value);
118 wpa_dbus_dict_entry_clear(&entry);
121 } else if (!strcmp(entry.key, "driver-params") &&
122 entry.type == DBUS_TYPE_STRING) {
123 os_free(driver_param);
124 driver_param = os_strdup(entry.str_value);
125 wpa_dbus_dict_entry_clear(&entry);
126 if (driver_param == NULL)
128 } else if (!strcmp(entry.key, "config-file") &&
129 entry.type == DBUS_TYPE_STRING) {
131 confname = os_strdup(entry.str_value);
132 wpa_dbus_dict_entry_clear(&entry);
133 if (confname == NULL)
135 } else if (!strcmp(entry.key, "bridge-ifname") &&
136 entry.type == DBUS_TYPE_STRING) {
137 os_free(bridge_ifname);
138 bridge_ifname = os_strdup(entry.str_value);
139 wpa_dbus_dict_entry_clear(&entry);
140 if (bridge_ifname == NULL)
143 wpa_dbus_dict_entry_clear(&entry);
150 * Try to get the wpa_supplicant record for this iface, return
151 * an error if we already control it.
153 if (wpa_supplicant_get_iface(global, ifname) != NULL) {
154 reply = dbus_message_new_error(
155 message, WPAS_ERROR_EXISTS_ERROR,
156 "wpa_supplicant already controls this interface.");
158 struct wpa_supplicant *wpa_s;
159 struct wpa_interface iface;
161 os_memset(&iface, 0, sizeof(iface));
162 iface.ifname = ifname;
163 iface.driver = driver;
164 iface.driver_param = driver_param;
165 iface.confname = confname;
166 iface.bridge_ifname = bridge_ifname;
167 /* Otherwise, have wpa_supplicant attach to it. */
168 wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
170 const char *path = wpa_s->dbus_path;
172 reply = dbus_message_new_method_return(message);
173 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
174 &path, DBUS_TYPE_INVALID);
176 reply = dbus_message_new_error(
177 message, WPAS_ERROR_ADD_ERROR,
178 "wpa_supplicant couldn't grab this interface.");
184 os_free(driver_param);
186 os_free(bridge_ifname);
190 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
196 * wpas_dbus_global_remove_interface - Request deregistration of an interface
197 * @message: Pointer to incoming dbus message
198 * @global: wpa_supplicant global data structure
199 * Returns: a dbus message containing a UINT32 indicating success (1) or
200 * failure (0), or returns a dbus error message with more information
202 * Handler function for "removeInterface" method call. Handles requests
203 * by dbus clients to deregister a network interface that wpa_supplicant
206 DBusMessage * wpas_dbus_global_remove_interface(DBusMessage *message,
207 struct wpa_global *global)
209 struct wpa_supplicant *wpa_s;
211 DBusMessage *reply = NULL;
213 if (!dbus_message_get_args(message, NULL,
214 DBUS_TYPE_OBJECT_PATH, &path,
215 DBUS_TYPE_INVALID)) {
216 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
220 wpa_s = wpa_supplicant_get_iface_by_dbus_path(global, path);
222 reply = wpas_dbus_new_invalid_iface_error(message);
226 if (!wpa_supplicant_remove_iface(global, wpa_s, 0)) {
227 reply = wpas_dbus_new_success_reply(message);
229 reply = dbus_message_new_error(
230 message, WPAS_ERROR_REMOVE_ERROR,
231 "wpa_supplicant couldn't remove this interface.");
240 * wpas_dbus_global_get_interface - Get the object path for an interface name
241 * @message: Pointer to incoming dbus message
242 * @global: %wpa_supplicant global data structure
243 * Returns: The object path of the interface object,
244 * or a dbus error message with more information
246 * Handler function for "getInterface" method call. Handles requests
247 * by dbus clients for the object path of an specific network interface.
249 DBusMessage * wpas_dbus_global_get_interface(DBusMessage *message,
250 struct wpa_global *global)
252 DBusMessage *reply = NULL;
255 struct wpa_supplicant *wpa_s;
257 if (!dbus_message_get_args(message, NULL,
258 DBUS_TYPE_STRING, &ifname,
259 DBUS_TYPE_INVALID)) {
260 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
264 wpa_s = wpa_supplicant_get_iface(global, ifname);
266 reply = wpas_dbus_new_invalid_iface_error(message);
270 path = wpa_s->dbus_path;
271 reply = dbus_message_new_method_return(message);
272 dbus_message_append_args(reply,
273 DBUS_TYPE_OBJECT_PATH, &path,
282 * wpas_dbus_global_set_debugparams- Set the debug params
283 * @message: Pointer to incoming dbus message
284 * @global: %wpa_supplicant global data structure
285 * Returns: a dbus message containing a UINT32 indicating success (1) or
286 * failure (0), or returns a dbus error message with more information
288 * Handler function for "setDebugParams" method call. Handles requests
289 * by dbus clients for the object path of an specific network interface.
291 DBusMessage * wpas_dbus_global_set_debugparams(DBusMessage *message,
292 struct wpa_global *global)
294 DBusMessage *reply = NULL;
296 dbus_bool_t debug_timestamp;
297 dbus_bool_t debug_show_keys;
299 if (!dbus_message_get_args(message, NULL,
300 DBUS_TYPE_INT32, &debug_level,
301 DBUS_TYPE_BOOLEAN, &debug_timestamp,
302 DBUS_TYPE_BOOLEAN, &debug_show_keys,
303 DBUS_TYPE_INVALID)) {
304 return wpas_dbus_new_invalid_opts_error(message, NULL);
307 if (wpa_supplicant_set_debug_params(global, debug_level,
308 debug_timestamp ? 1 : 0,
309 debug_show_keys ? 1 : 0)) {
310 return wpas_dbus_new_invalid_opts_error(message, NULL);
313 reply = wpas_dbus_new_success_reply(message);
320 * wpas_dbus_iface_scan - Request a wireless scan on an interface
321 * @message: Pointer to incoming dbus message
322 * @wpa_s: wpa_supplicant structure for a network interface
323 * Returns: a dbus message containing a UINT32 indicating success (1) or
326 * Handler function for "scan" method call of a network device. Requests
327 * that wpa_supplicant perform a wireless scan as soon as possible
328 * on a particular wireless interface.
330 DBusMessage * wpas_dbus_iface_scan(DBusMessage *message,
331 struct wpa_supplicant *wpa_s)
333 wpa_s->scan_req = MANUAL_SCAN_REQ;
334 wpa_supplicant_req_scan(wpa_s, 0, 0);
335 return wpas_dbus_new_success_reply(message);
340 * wpas_dbus_iface_scan_results - Get the results of a recent scan request
341 * @message: Pointer to incoming dbus message
342 * @wpa_s: wpa_supplicant structure for a network interface
343 * Returns: a dbus message containing a dbus array of objects paths, or returns
344 * a dbus error message if not scan results could be found
346 * Handler function for "scanResults" method call of a network device. Returns
347 * a dbus message containing the object paths of wireless networks found.
349 DBusMessage * wpas_dbus_iface_scan_results(DBusMessage *message,
350 struct wpa_supplicant *wpa_s)
353 DBusMessageIter iter;
354 DBusMessageIter sub_iter;
357 /* Create and initialize the return message */
358 reply = dbus_message_new_method_return(message);
359 dbus_message_iter_init_append(reply, &iter);
360 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
361 DBUS_TYPE_OBJECT_PATH_AS_STRING,
365 /* Loop through scan results and append each result's object path */
366 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
367 char path_buf[WPAS_DBUS_OBJECT_PATH_MAX];
368 char *path = path_buf;
370 /* Construct the object path for this network. Note that ':'
371 * is not a valid character in dbus object paths.
373 os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
374 "%s/" WPAS_DBUS_BSSIDS_PART "/"
375 WPAS_DBUS_BSSID_FORMAT,
376 wpa_s->dbus_path, MAC2STR(bss->bssid));
377 if (!dbus_message_iter_append_basic(&sub_iter,
378 DBUS_TYPE_OBJECT_PATH,
383 if (!dbus_message_iter_close_container(&iter, &sub_iter))
389 dbus_message_unref(reply);
390 return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
391 "an internal error occurred returning scan results");
396 * wpas_dbus_bssid_properties - Return the properties of a scanned network
397 * @message: Pointer to incoming dbus message
398 * @wpa_s: wpa_supplicant structure for a network interface
399 * @res: wpa_supplicant scan result for which to get properties
400 * Returns: a dbus message containing the properties for the requested network
402 * Handler function for "properties" method call of a scanned network.
403 * Returns a dbus message containing the the properties.
405 DBusMessage * wpas_dbus_bssid_properties(DBusMessage *message,
406 struct wpa_supplicant *wpa_s,
410 DBusMessageIter iter, iter_dict;
411 const u8 *wpa_ie, *rsn_ie, *wps_ie;
413 /* Dump the properties into a dbus message */
414 reply = dbus_message_new_method_return(message);
416 wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
417 rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
418 wps_ie = wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE);
420 dbus_message_iter_init_append(reply, &iter);
421 if (!wpa_dbus_dict_open_write(&iter, &iter_dict) ||
422 !wpa_dbus_dict_append_byte_array(&iter_dict, "bssid",
423 (const char *) bss->bssid,
425 !wpa_dbus_dict_append_byte_array(&iter_dict, "ssid",
426 (const char *) bss->ssid,
429 !wpa_dbus_dict_append_byte_array(&iter_dict, "wpaie",
430 (const char *) wpa_ie,
433 !wpa_dbus_dict_append_byte_array(&iter_dict, "rsnie",
434 (const char *) rsn_ie,
437 !wpa_dbus_dict_append_byte_array(&iter_dict, "wpsie",
438 (const char *) wps_ie,
441 !wpa_dbus_dict_append_int32(&iter_dict, "frequency", bss->freq)) ||
442 !wpa_dbus_dict_append_uint16(&iter_dict, "capabilities",
444 (!(bss->flags & WPA_BSS_QUAL_INVALID) &&
445 !wpa_dbus_dict_append_int32(&iter_dict, "quality", bss->qual)) ||
446 (!(bss->flags & WPA_BSS_NOISE_INVALID) &&
447 !wpa_dbus_dict_append_int32(&iter_dict, "noise", bss->noise)) ||
448 (!(bss->flags & WPA_BSS_LEVEL_INVALID) &&
449 !wpa_dbus_dict_append_int32(&iter_dict, "level", bss->level)) ||
450 !wpa_dbus_dict_append_int32(&iter_dict, "maxrate",
451 wpa_bss_get_max_rate(bss) * 500000) ||
452 !wpa_dbus_dict_close_write(&iter, &iter_dict)) {
454 dbus_message_unref(reply);
455 reply = dbus_message_new_error(
456 message, WPAS_ERROR_INTERNAL_ERROR,
457 "an internal error occurred returning BSSID properties.");
465 * wpas_dbus_iface_capabilities - Return interface capabilities
466 * @message: Pointer to incoming dbus message
467 * @wpa_s: wpa_supplicant structure for a network interface
468 * Returns: A dbus message containing a dict of strings
470 * Handler function for "capabilities" method call of an interface.
472 DBusMessage * wpas_dbus_iface_capabilities(DBusMessage *message,
473 struct wpa_supplicant *wpa_s)
475 DBusMessage *reply = NULL;
476 struct wpa_driver_capa capa;
478 DBusMessageIter iter, iter_dict;
481 dbus_bool_t strict = FALSE;
482 DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
484 if (!dbus_message_get_args(message, NULL,
485 DBUS_TYPE_BOOLEAN, &strict,
489 reply = dbus_message_new_method_return(message);
491 dbus_message_iter_init_append(reply, &iter);
492 if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
496 eap_methods = eap_get_names_as_string_array(&num_items);
498 dbus_bool_t success = FALSE;
501 success = wpa_dbus_dict_append_string_array(
502 &iter_dict, "eap", (const char **) eap_methods,
505 /* free returned method array */
506 while (eap_methods[i])
507 os_free(eap_methods[i++]);
508 os_free(eap_methods);
514 res = wpa_drv_get_capa(wpa_s, &capa);
516 /***** pairwise cipher */
519 const char *args[] = {"CCMP", "TKIP", "NONE"};
521 if (!wpa_dbus_dict_append_string_array(
522 &iter_dict, "pairwise", args,
527 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "pairwise",
531 ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) &&
532 !wpa_dbus_dict_string_array_add_element(
533 &iter_array, "CCMP")) ||
534 ((capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) &&
535 !wpa_dbus_dict_string_array_add_element(
536 &iter_array, "TKIP")) ||
537 ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) &&
538 !wpa_dbus_dict_string_array_add_element(
539 &iter_array, "NONE")) ||
540 !wpa_dbus_dict_end_string_array(&iter_dict,
547 /***** group cipher */
550 const char *args[] = {
551 "CCMP", "TKIP", "WEP104", "WEP40"
554 if (!wpa_dbus_dict_append_string_array(
555 &iter_dict, "group", args,
560 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "group",
566 if (((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) &&
567 !wpa_dbus_dict_string_array_add_element(
568 &iter_array, "CCMP")) ||
569 ((capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) &&
570 !wpa_dbus_dict_string_array_add_element(
571 &iter_array, "TKIP")) ||
572 ((capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) &&
573 !wpa_dbus_dict_string_array_add_element(
574 &iter_array, "WEP104")) ||
575 ((capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) &&
576 !wpa_dbus_dict_string_array_add_element(
577 &iter_array, "WEP40")) ||
578 !wpa_dbus_dict_end_string_array(&iter_dict,
585 /***** key management */
588 const char *args[] = {
589 "WPA-PSK", "WPA-EAP", "IEEE8021X", "WPA-NONE",
592 if (!wpa_dbus_dict_append_string_array(
593 &iter_dict, "key_mgmt", args,
598 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "key_mgmt",
602 !wpa_dbus_dict_string_array_add_element(&iter_array,
604 !wpa_dbus_dict_string_array_add_element(&iter_array,
606 ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
607 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) &&
608 !wpa_dbus_dict_string_array_add_element(
609 &iter_array, "WPA-EAP")) ||
610 ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
611 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) &&
612 !wpa_dbus_dict_string_array_add_element(
613 &iter_array, "WPA-PSK")) ||
614 ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) &&
615 !wpa_dbus_dict_string_array_add_element(
616 &iter_array, "WPA-NONE")) ||
617 !wpa_dbus_dict_end_string_array(&iter_dict,
624 /***** WPA protocol */
627 const char *args[] = { "RSN", "WPA" };
629 if (!wpa_dbus_dict_append_string_array(
630 &iter_dict, "proto", args,
635 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "proto",
639 ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
640 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) &&
641 !wpa_dbus_dict_string_array_add_element(
642 &iter_array, "RSN")) ||
643 ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
644 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) &&
645 !wpa_dbus_dict_string_array_add_element(
646 &iter_array, "WPA")) ||
647 !wpa_dbus_dict_end_string_array(&iter_dict,
657 const char *args[] = { "OPEN", "SHARED", "LEAP" };
659 if (!wpa_dbus_dict_append_string_array(
660 &iter_dict, "auth_alg", args,
665 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "auth_alg",
669 ((capa.auth & WPA_DRIVER_AUTH_OPEN) &&
670 !wpa_dbus_dict_string_array_add_element(
671 &iter_array, "OPEN")) ||
672 ((capa.auth & WPA_DRIVER_AUTH_SHARED) &&
673 !wpa_dbus_dict_string_array_add_element(
674 &iter_array, "SHARED")) ||
675 ((capa.auth & WPA_DRIVER_AUTH_LEAP) &&
676 !wpa_dbus_dict_string_array_add_element(
677 &iter_array, "LEAP")) ||
678 !wpa_dbus_dict_end_string_array(&iter_dict,
685 if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
692 dbus_message_unref(reply);
693 return dbus_message_new_error(
694 message, WPAS_ERROR_INTERNAL_ERROR,
695 "an internal error occurred returning interface capabilities.");
700 * wpas_dbus_iface_add_network - Add a new configured network
701 * @message: Pointer to incoming dbus message
702 * @wpa_s: wpa_supplicant structure for a network interface
703 * Returns: A dbus message containing the object path of the new network
705 * Handler function for "addNetwork" method call of a network interface.
707 DBusMessage * wpas_dbus_iface_add_network(DBusMessage *message,
708 struct wpa_supplicant *wpa_s)
710 DBusMessage *reply = NULL;
711 struct wpa_ssid *ssid;
712 char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *path = path_buf;
714 ssid = wpa_config_add_network(wpa_s->conf);
716 reply = dbus_message_new_error(
717 message, WPAS_ERROR_ADD_NETWORK_ERROR,
718 "wpa_supplicant could not add a network on this interface.");
721 wpas_notify_network_added(wpa_s, ssid);
723 wpa_config_set_network_defaults(ssid);
725 /* Construct the object path for this network. */
726 os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
727 "%s/" WPAS_DBUS_NETWORKS_PART "/%d",
728 wpa_s->dbus_path, ssid->id);
730 reply = dbus_message_new_method_return(message);
731 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
732 &path, DBUS_TYPE_INVALID);
740 * wpas_dbus_iface_remove_network - Remove a configured network
741 * @message: Pointer to incoming dbus message
742 * @wpa_s: wpa_supplicant structure for a network interface
743 * Returns: A dbus message containing a UINT32 indicating success (1) or
746 * Handler function for "removeNetwork" method call of a network interface.
748 DBusMessage * wpas_dbus_iface_remove_network(DBusMessage *message,
749 struct wpa_supplicant *wpa_s)
751 DBusMessage *reply = NULL;
753 char *iface = NULL, *net_id = NULL;
755 struct wpa_ssid *ssid;
757 if (!dbus_message_get_args(message, NULL,
758 DBUS_TYPE_OBJECT_PATH, &op,
759 DBUS_TYPE_INVALID)) {
760 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
764 /* Extract the network ID */
765 iface = wpas_dbus_decompose_object_path(op, &net_id, NULL);
766 if (iface == NULL || net_id == NULL) {
767 reply = wpas_dbus_new_invalid_network_error(message);
771 /* Ensure the network is actually a child of this interface */
772 if (os_strcmp(iface, wpa_s->dbus_path) != 0) {
773 reply = wpas_dbus_new_invalid_network_error(message);
777 id = strtoul(net_id, NULL, 10);
778 ssid = wpa_config_get_network(wpa_s->conf, id);
780 reply = wpas_dbus_new_invalid_network_error(message);
784 wpas_notify_network_removed(wpa_s, ssid);
786 if (ssid == wpa_s->current_ssid)
787 wpa_supplicant_deauthenticate(wpa_s,
788 WLAN_REASON_DEAUTH_LEAVING);
790 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
791 reply = dbus_message_new_error(
792 message, WPAS_ERROR_REMOVE_NETWORK_ERROR,
793 "error removing the specified on this interface.");
797 reply = wpas_dbus_new_success_reply(message);
806 static const char const *dont_quote[] = {
807 "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
808 "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
813 static dbus_bool_t should_quote_opt(const char *key)
817 while (dont_quote[i] != NULL) {
818 if (os_strcmp(key, dont_quote[i]) == 0)
827 * wpas_dbus_iface_set_network - Set options for a configured network
828 * @message: Pointer to incoming dbus message
829 * @wpa_s: wpa_supplicant structure for a network interface
830 * @ssid: wpa_ssid structure for a configured network
831 * Returns: a dbus message containing a UINT32 indicating success (1) or
834 * Handler function for "set" method call of a configured network.
836 DBusMessage * wpas_dbus_iface_set_network(DBusMessage *message,
837 struct wpa_supplicant *wpa_s,
838 struct wpa_ssid *ssid)
840 DBusMessage *reply = NULL;
841 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
842 DBusMessageIter iter, iter_dict;
844 dbus_message_iter_init(message, &iter);
846 if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL)) {
847 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
851 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
856 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
857 reply = wpas_dbus_new_invalid_opts_error(message,
862 /* Type conversions, since wpa_supplicant wants strings */
863 if (entry.type == DBUS_TYPE_ARRAY &&
864 entry.array_type == DBUS_TYPE_BYTE) {
865 if (entry.array_len <= 0)
868 size = entry.array_len * 2 + 1;
869 value = os_zalloc(size);
872 ret = wpa_snprintf_hex(value, size,
873 (u8 *) entry.bytearray_value,
877 } else if (entry.type == DBUS_TYPE_STRING) {
878 if (should_quote_opt(entry.key)) {
879 size = os_strlen(entry.str_value);
880 /* Zero-length option check */
883 size += 3; /* For quotes and terminator */
884 value = os_zalloc(size);
887 ret = os_snprintf(value, size, "\"%s\"",
889 if (os_snprintf_error(size, ret))
892 value = os_strdup(entry.str_value);
896 } else if (entry.type == DBUS_TYPE_UINT32) {
897 value = os_zalloc(size);
900 ret = os_snprintf(value, size, "%u",
902 if (os_snprintf_error(size, ret))
904 } else if (entry.type == DBUS_TYPE_INT32) {
905 value = os_zalloc(size);
908 ret = os_snprintf(value, size, "%d",
910 if (os_snprintf_error(size, ret))
915 if (wpa_config_set(ssid, entry.key, value, 0) < 0)
918 if ((os_strcmp(entry.key, "psk") == 0 &&
919 value[0] == '"' && ssid->ssid_len) ||
920 (os_strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
921 wpa_config_update_psk(ssid);
922 else if (os_strcmp(entry.key, "priority") == 0)
923 wpa_config_update_prio_list(wpa_s->conf);
926 wpa_dbus_dict_entry_clear(&entry);
931 reply = wpas_dbus_new_invalid_opts_error(message, entry.key);
932 wpa_dbus_dict_entry_clear(&entry);
937 reply = wpas_dbus_new_success_reply(message);
945 * wpas_dbus_iface_enable_network - Mark a configured network as enabled
946 * @message: Pointer to incoming dbus message
947 * @wpa_s: wpa_supplicant structure for a network interface
948 * @ssid: wpa_ssid structure for a configured network
949 * Returns: A dbus message containing a UINT32 indicating success (1) or
952 * Handler function for "enable" method call of a configured network.
954 DBusMessage * wpas_dbus_iface_enable_network(DBusMessage *message,
955 struct wpa_supplicant *wpa_s,
956 struct wpa_ssid *ssid)
958 wpa_supplicant_enable_network(wpa_s, ssid);
959 return wpas_dbus_new_success_reply(message);
964 * wpas_dbus_iface_disable_network - Mark a configured network as disabled
965 * @message: Pointer to incoming dbus message
966 * @wpa_s: wpa_supplicant structure for a network interface
967 * @ssid: wpa_ssid structure for a configured network
968 * Returns: A dbus message containing a UINT32 indicating success (1) or
971 * Handler function for "disable" method call of a configured network.
973 DBusMessage * wpas_dbus_iface_disable_network(DBusMessage *message,
974 struct wpa_supplicant *wpa_s,
975 struct wpa_ssid *ssid)
977 wpa_supplicant_disable_network(wpa_s, ssid);
978 return wpas_dbus_new_success_reply(message);
983 * wpas_dbus_iface_select_network - Attempt association with a configured network
984 * @message: Pointer to incoming dbus message
985 * @wpa_s: wpa_supplicant structure for a network interface
986 * Returns: A dbus message containing a UINT32 indicating success (1) or
989 * Handler function for "selectNetwork" method call of network interface.
991 DBusMessage * wpas_dbus_iface_select_network(DBusMessage *message,
992 struct wpa_supplicant *wpa_s)
994 DBusMessage *reply = NULL;
996 struct wpa_ssid *ssid;
997 char *iface_obj_path = NULL;
998 char *network = NULL;
1000 if (os_strlen(dbus_message_get_signature(message)) == 0) {
1006 if (!dbus_message_get_args(message, NULL,
1007 DBUS_TYPE_OBJECT_PATH, &op,
1008 DBUS_TYPE_INVALID)) {
1009 reply = wpas_dbus_new_invalid_opts_error(message,
1014 /* Extract the network number */
1015 iface_obj_path = wpas_dbus_decompose_object_path(op,
1018 if (iface_obj_path == NULL) {
1019 reply = wpas_dbus_new_invalid_iface_error(message);
1022 /* Ensure the object path really points to this interface */
1023 if (network == NULL ||
1024 os_strcmp(iface_obj_path, wpa_s->dbus_path) != 0) {
1025 reply = wpas_dbus_new_invalid_network_error(message);
1029 nid = strtoul(network, NULL, 10);
1030 if (errno == EINVAL) {
1031 reply = wpas_dbus_new_invalid_network_error(message);
1035 ssid = wpa_config_get_network(wpa_s->conf, nid);
1037 reply = wpas_dbus_new_invalid_network_error(message);
1042 /* Finally, associate with the network */
1043 wpa_supplicant_select_network(wpa_s, ssid);
1045 reply = wpas_dbus_new_success_reply(message);
1048 os_free(iface_obj_path);
1055 * wpas_dbus_iface_disconnect - Terminate the current connection
1056 * @message: Pointer to incoming dbus message
1057 * @wpa_s: wpa_supplicant structure for a network interface
1058 * Returns: A dbus message containing a UINT32 indicating success (1) or
1061 * Handler function for "disconnect" method call of network interface.
1063 DBusMessage * wpas_dbus_iface_disconnect(DBusMessage *message,
1064 struct wpa_supplicant *wpa_s)
1066 wpa_s->disconnected = 1;
1067 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1069 return wpas_dbus_new_success_reply(message);
1074 * wpas_dbus_iface_set_ap_scan - Control roaming mode
1075 * @message: Pointer to incoming dbus message
1076 * @wpa_s: wpa_supplicant structure for a network interface
1077 * Returns: A dbus message containing a UINT32 indicating success (1) or
1080 * Handler function for "setAPScan" method call.
1082 DBusMessage * wpas_dbus_iface_set_ap_scan(DBusMessage *message,
1083 struct wpa_supplicant *wpa_s)
1085 DBusMessage *reply = NULL;
1086 dbus_uint32_t ap_scan = 1;
1088 if (!dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &ap_scan,
1089 DBUS_TYPE_INVALID)) {
1090 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
1094 if (wpa_supplicant_set_ap_scan(wpa_s, ap_scan)) {
1095 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
1099 reply = wpas_dbus_new_success_reply(message);
1107 * wpas_dbus_iface_set_smartcard_modules - Set smartcard related module paths
1108 * @message: Pointer to incoming dbus message
1109 * @wpa_s: wpa_supplicant structure for a network interface
1110 * Returns: A dbus message containing a UINT32 indicating success (1) or
1113 * Handler function for "setSmartcardModules" method call.
1115 DBusMessage * wpas_dbus_iface_set_smartcard_modules(
1116 DBusMessage *message, struct wpa_supplicant *wpa_s)
1118 DBusMessageIter iter, iter_dict;
1119 char *opensc_engine_path = NULL;
1120 char *pkcs11_engine_path = NULL;
1121 char *pkcs11_module_path = NULL;
1122 struct wpa_dbus_dict_entry entry;
1124 if (!dbus_message_iter_init(message, &iter))
1127 if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
1130 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
1131 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
1133 if (!strcmp(entry.key, "opensc_engine_path") &&
1134 entry.type == DBUS_TYPE_STRING) {
1135 os_free(opensc_engine_path);
1136 opensc_engine_path = os_strdup(entry.str_value);
1137 wpa_dbus_dict_entry_clear(&entry);
1138 if (opensc_engine_path == NULL)
1140 } else if (!strcmp(entry.key, "pkcs11_engine_path") &&
1141 entry.type == DBUS_TYPE_STRING) {
1142 os_free(pkcs11_engine_path);
1143 pkcs11_engine_path = os_strdup(entry.str_value);
1144 wpa_dbus_dict_entry_clear(&entry);
1145 if (pkcs11_engine_path == NULL)
1147 } else if (!strcmp(entry.key, "pkcs11_module_path") &&
1148 entry.type == DBUS_TYPE_STRING) {
1149 os_free(pkcs11_module_path);
1150 pkcs11_module_path = os_strdup(entry.str_value);
1151 wpa_dbus_dict_entry_clear(&entry);
1152 if (pkcs11_module_path == NULL)
1155 wpa_dbus_dict_entry_clear(&entry);
1160 os_free(wpa_s->conf->opensc_engine_path);
1161 wpa_s->conf->opensc_engine_path = opensc_engine_path;
1162 os_free(wpa_s->conf->pkcs11_engine_path);
1163 wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path;
1164 os_free(wpa_s->conf->pkcs11_module_path);
1165 wpa_s->conf->pkcs11_module_path = pkcs11_module_path;
1167 wpa_sm_set_eapol(wpa_s->wpa, NULL);
1168 eapol_sm_deinit(wpa_s->eapol);
1169 wpa_s->eapol = NULL;
1170 wpa_supplicant_init_eapol(wpa_s);
1171 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
1173 return wpas_dbus_new_success_reply(message);
1176 os_free(opensc_engine_path);
1177 os_free(pkcs11_engine_path);
1178 os_free(pkcs11_module_path);
1179 return wpas_dbus_new_invalid_opts_error(message, NULL);
1184 * wpas_dbus_iface_get_state - Get interface state
1185 * @message: Pointer to incoming dbus message
1186 * @wpa_s: wpa_supplicant structure for a network interface
1187 * Returns: A dbus message containing a STRING representing the current
1190 * Handler function for "state" method call.
1192 DBusMessage * wpas_dbus_iface_get_state(DBusMessage *message,
1193 struct wpa_supplicant *wpa_s)
1195 DBusMessage *reply = NULL;
1196 const char *str_state;
1198 reply = dbus_message_new_method_return(message);
1199 if (reply != NULL) {
1200 str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
1201 dbus_message_append_args(reply, DBUS_TYPE_STRING, &str_state,
1210 * wpas_dbus_iface_get_scanning - Get interface scanning state
1211 * @message: Pointer to incoming dbus message
1212 * @wpa_s: wpa_supplicant structure for a network interface
1213 * Returns: A dbus message containing whether the interface is scanning
1215 * Handler function for "scanning" method call.
1217 DBusMessage * wpas_dbus_iface_get_scanning(DBusMessage *message,
1218 struct wpa_supplicant *wpa_s)
1220 DBusMessage *reply = NULL;
1221 dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
1223 reply = dbus_message_new_method_return(message);
1224 if (reply != NULL) {
1225 dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &scanning,
1228 wpa_printf(MSG_ERROR,
1229 "dbus: Not enough memory to return scanning state");
1236 #ifndef CONFIG_NO_CONFIG_BLOBS
1239 * wpas_dbus_iface_set_blobs - Store named binary blobs (ie, for certificates)
1240 * @message: Pointer to incoming dbus message
1241 * @wpa_s: %wpa_supplicant data structure
1242 * Returns: A dbus message containing a UINT32 indicating success (1) or
1245 * Asks wpa_supplicant to internally store a one or more binary blobs.
1247 DBusMessage * wpas_dbus_iface_set_blobs(DBusMessage *message,
1248 struct wpa_supplicant *wpa_s)
1250 DBusMessage *reply = NULL;
1251 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
1252 DBusMessageIter iter, iter_dict;
1254 dbus_message_iter_init(message, &iter);
1256 if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
1257 return wpas_dbus_new_invalid_opts_error(message, NULL);
1259 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
1260 struct wpa_config_blob *blob;
1262 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
1263 reply = wpas_dbus_new_invalid_opts_error(message,
1268 if (entry.type != DBUS_TYPE_ARRAY ||
1269 entry.array_type != DBUS_TYPE_BYTE) {
1270 reply = wpas_dbus_new_invalid_opts_error(
1271 message, "Byte array expected.");
1275 if ((entry.array_len <= 0) || (entry.array_len > 65536) ||
1276 !strlen(entry.key)) {
1277 reply = wpas_dbus_new_invalid_opts_error(
1278 message, "Invalid array size.");
1282 blob = os_zalloc(sizeof(*blob));
1284 reply = dbus_message_new_error(
1285 message, WPAS_ERROR_ADD_ERROR,
1286 "Not enough memory to add blob.");
1289 blob->data = os_zalloc(entry.array_len);
1290 if (blob->data == NULL) {
1291 reply = dbus_message_new_error(
1292 message, WPAS_ERROR_ADD_ERROR,
1293 "Not enough memory to add blob data.");
1298 blob->name = os_strdup(entry.key);
1299 blob->len = entry.array_len;
1300 os_memcpy(blob->data, (u8 *) entry.bytearray_value,
1302 if (blob->name == NULL) {
1303 wpa_config_free_blob(blob);
1304 reply = dbus_message_new_error(
1305 message, WPAS_ERROR_ADD_ERROR,
1306 "Error adding blob.");
1311 if (!wpa_config_remove_blob(wpa_s->conf, blob->name))
1312 wpas_notify_blob_removed(wpa_s, blob->name);
1313 wpa_config_set_blob(wpa_s->conf, blob);
1314 wpas_notify_blob_added(wpa_s, blob->name);
1316 wpa_dbus_dict_entry_clear(&entry);
1318 wpa_dbus_dict_entry_clear(&entry);
1320 return reply ? reply : wpas_dbus_new_success_reply(message);
1325 * wpas_dbus_iface_remove_blob - Remove named binary blobs
1326 * @message: Pointer to incoming dbus message
1327 * @wpa_s: %wpa_supplicant data structure
1328 * Returns: A dbus message containing a UINT32 indicating success (1) or
1331 * Asks wpa_supplicant to remove one or more previously stored binary blobs.
1333 DBusMessage * wpas_dbus_iface_remove_blobs(DBusMessage *message,
1334 struct wpa_supplicant *wpa_s)
1336 DBusMessageIter iter, array;
1337 char *err_msg = NULL;
1339 dbus_message_iter_init(message, &iter);
1341 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1342 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
1343 return wpas_dbus_new_invalid_opts_error(message, NULL);
1345 dbus_message_iter_recurse(&iter, &array);
1346 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
1349 dbus_message_iter_get_basic(&array, &name);
1350 if (!os_strlen(name))
1351 err_msg = "Invalid blob name.";
1352 else if (wpa_config_remove_blob(wpa_s->conf, name) != 0)
1353 err_msg = "Error removing blob.";
1355 wpas_notify_blob_removed(wpa_s, name);
1356 dbus_message_iter_next(&array);
1360 return dbus_message_new_error(message, WPAS_ERROR_REMOVE_ERROR,
1363 return wpas_dbus_new_success_reply(message);
1366 #endif /* CONFIG_NO_CONFIG_BLOBS */
1370 * wpas_dbus_iface_flush - Clear BSS of old or all inactive entries
1371 * @message: Pointer to incoming dbus message
1372 * @wpa_s: %wpa_supplicant data structure
1373 * Returns: a dbus message containing a UINT32 indicating success (1) or
1374 * failure (0), or returns a dbus error message with more information
1376 * Handler function for "flush" method call. Handles requests for an
1377 * interface with an optional "age" parameter that specifies the minimum
1378 * age of a BSS to be flushed.
1380 DBusMessage * wpas_dbus_iface_flush(DBusMessage *message,
1381 struct wpa_supplicant *wpa_s)
1385 if (os_strlen(dbus_message_get_signature(message)) != 0 &&
1386 !dbus_message_get_args(message, NULL,
1387 DBUS_TYPE_INT32, &flush_age,
1388 DBUS_TYPE_INVALID)) {
1389 return wpas_dbus_new_invalid_opts_error(message, NULL);
1393 wpa_bss_flush(wpa_s);
1395 wpa_bss_flush_by_age(wpa_s, flush_age);
1397 return wpas_dbus_new_success_reply(message);