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(message, WPAS_ERROR_INVALID_OPTS,
41 "Did not receive correct message "
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(message,
155 WPAS_ERROR_EXISTS_ERROR,
156 "wpa_supplicant already "
157 "controls this interface.");
159 struct wpa_supplicant *wpa_s;
160 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 if ((wpa_s = wpa_supplicant_add_iface(global, &iface))) {
169 const char *path = wpa_s->dbus_path;
170 reply = dbus_message_new_method_return(message);
171 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
172 &path, DBUS_TYPE_INVALID);
174 reply = dbus_message_new_error(message,
175 WPAS_ERROR_ADD_ERROR,
177 "couldn't grab this "
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(message,
230 WPAS_ERROR_REMOVE_ERROR,
231 "wpa_supplicant couldn't "
232 "remove this interface.");
241 * wpas_dbus_global_get_interface - Get the object path for an interface name
242 * @message: Pointer to incoming dbus message
243 * @global: %wpa_supplicant global data structure
244 * Returns: The object path of the interface object,
245 * or a dbus error message with more information
247 * Handler function for "getInterface" method call. Handles requests
248 * by dbus clients for the object path of an specific network interface.
250 DBusMessage * wpas_dbus_global_get_interface(DBusMessage *message,
251 struct wpa_global *global)
253 DBusMessage *reply = NULL;
256 struct wpa_supplicant *wpa_s;
258 if (!dbus_message_get_args(message, NULL,
259 DBUS_TYPE_STRING, &ifname,
260 DBUS_TYPE_INVALID)) {
261 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
265 wpa_s = wpa_supplicant_get_iface(global, ifname);
267 reply = wpas_dbus_new_invalid_iface_error(message);
271 path = wpa_s->dbus_path;
272 reply = dbus_message_new_method_return(message);
273 dbus_message_append_args(reply,
274 DBUS_TYPE_OBJECT_PATH, &path,
283 * wpas_dbus_global_set_debugparams- Set the debug params
284 * @message: Pointer to incoming dbus message
285 * @global: %wpa_supplicant global data structure
286 * Returns: a dbus message containing a UINT32 indicating success (1) or
287 * failure (0), or returns a dbus error message with more information
289 * Handler function for "setDebugParams" method call. Handles requests
290 * by dbus clients for the object path of an specific network interface.
292 DBusMessage * wpas_dbus_global_set_debugparams(DBusMessage *message,
293 struct wpa_global *global)
295 DBusMessage *reply = NULL;
297 dbus_bool_t debug_timestamp;
298 dbus_bool_t debug_show_keys;
300 if (!dbus_message_get_args(message, NULL,
301 DBUS_TYPE_INT32, &debug_level,
302 DBUS_TYPE_BOOLEAN, &debug_timestamp,
303 DBUS_TYPE_BOOLEAN, &debug_show_keys,
304 DBUS_TYPE_INVALID)) {
305 return wpas_dbus_new_invalid_opts_error(message, NULL);
308 if (wpa_supplicant_set_debug_params(global, debug_level,
309 debug_timestamp ? 1 : 0,
310 debug_show_keys ? 1 : 0)) {
311 return wpas_dbus_new_invalid_opts_error(message, NULL);
314 reply = wpas_dbus_new_success_reply(message);
321 * wpas_dbus_iface_scan - Request a wireless scan on an interface
322 * @message: Pointer to incoming dbus message
323 * @wpa_s: wpa_supplicant structure for a network interface
324 * Returns: a dbus message containing a UINT32 indicating success (1) or
327 * Handler function for "scan" method call of a network device. Requests
328 * that wpa_supplicant perform a wireless scan as soon as possible
329 * on a particular wireless interface.
331 DBusMessage * wpas_dbus_iface_scan(DBusMessage *message,
332 struct wpa_supplicant *wpa_s)
334 wpa_s->scan_req = MANUAL_SCAN_REQ;
335 wpa_supplicant_req_scan(wpa_s, 0, 0);
336 return wpas_dbus_new_success_reply(message);
341 * wpas_dbus_iface_scan_results - Get the results of a recent scan request
342 * @message: Pointer to incoming dbus message
343 * @wpa_s: wpa_supplicant structure for a network interface
344 * Returns: a dbus message containing a dbus array of objects paths, or returns
345 * a dbus error message if not scan results could be found
347 * Handler function for "scanResults" method call of a network device. Returns
348 * a dbus message containing the object paths of wireless networks found.
350 DBusMessage * wpas_dbus_iface_scan_results(DBusMessage *message,
351 struct wpa_supplicant *wpa_s)
354 DBusMessageIter iter;
355 DBusMessageIter sub_iter;
358 /* Create and initialize the return message */
359 reply = dbus_message_new_method_return(message);
360 dbus_message_iter_init_append(reply, &iter);
361 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
362 DBUS_TYPE_OBJECT_PATH_AS_STRING,
366 /* Loop through scan results and append each result's object path */
367 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
368 char path_buf[WPAS_DBUS_OBJECT_PATH_MAX];
369 char *path = path_buf;
371 /* Construct the object path for this network. Note that ':'
372 * is not a valid character in dbus object paths.
374 os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
375 "%s/" WPAS_DBUS_BSSIDS_PART "/"
376 WPAS_DBUS_BSSID_FORMAT,
377 wpa_s->dbus_path, MAC2STR(bss->bssid));
378 if (!dbus_message_iter_append_basic(&sub_iter,
379 DBUS_TYPE_OBJECT_PATH,
384 if (!dbus_message_iter_close_container(&iter, &sub_iter))
390 dbus_message_unref(reply);
391 return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
392 "an internal error occurred returning scan results");
397 * wpas_dbus_bssid_properties - Return the properties of a scanned network
398 * @message: Pointer to incoming dbus message
399 * @wpa_s: wpa_supplicant structure for a network interface
400 * @res: wpa_supplicant scan result for which to get properties
401 * Returns: a dbus message containing the properties for the requested network
403 * Handler function for "properties" method call of a scanned network.
404 * Returns a dbus message containing the the properties.
406 DBusMessage * wpas_dbus_bssid_properties(DBusMessage *message,
407 struct wpa_supplicant *wpa_s,
411 DBusMessageIter iter, iter_dict;
414 /* Dump the properties into a dbus message */
415 reply = dbus_message_new_method_return(message);
417 dbus_message_iter_init_append(reply, &iter);
418 if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
421 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "bssid",
422 (const char *) bss->bssid,
426 ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
428 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "ssid",
429 (const char *) (ie + 2),
434 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
436 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "wpaie",
442 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
444 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "rsnie",
450 ie = wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE);
452 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "wpsie",
459 if (!wpa_dbus_dict_append_int32(&iter_dict, "frequency",
463 if (!wpa_dbus_dict_append_uint16(&iter_dict, "capabilities",
466 if (!(bss->flags & WPA_BSS_QUAL_INVALID) &&
467 !wpa_dbus_dict_append_int32(&iter_dict, "quality", bss->qual))
469 if (!(bss->flags & WPA_BSS_NOISE_INVALID) &&
470 !wpa_dbus_dict_append_int32(&iter_dict, "noise", bss->noise))
472 if (!(bss->flags & WPA_BSS_LEVEL_INVALID) &&
473 !wpa_dbus_dict_append_int32(&iter_dict, "level", bss->level))
475 if (!wpa_dbus_dict_append_int32(&iter_dict, "maxrate",
476 wpa_bss_get_max_rate(bss) * 500000))
479 if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
486 dbus_message_unref(reply);
487 return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
488 "an internal error occurred returning "
489 "BSSID properties.");
494 * wpas_dbus_iface_capabilities - Return interface capabilities
495 * @message: Pointer to incoming dbus message
496 * @wpa_s: wpa_supplicant structure for a network interface
497 * Returns: A dbus message containing a dict of strings
499 * Handler function for "capabilities" method call of an interface.
501 DBusMessage * wpas_dbus_iface_capabilities(DBusMessage *message,
502 struct wpa_supplicant *wpa_s)
504 DBusMessage *reply = NULL;
505 struct wpa_driver_capa capa;
507 DBusMessageIter iter, iter_dict;
510 dbus_bool_t strict = FALSE;
511 DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
513 if (!dbus_message_get_args(message, NULL,
514 DBUS_TYPE_BOOLEAN, &strict,
518 reply = dbus_message_new_method_return(message);
520 dbus_message_iter_init_append(reply, &iter);
521 if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
525 eap_methods = eap_get_names_as_string_array(&num_items);
527 dbus_bool_t success = FALSE;
530 success = wpa_dbus_dict_append_string_array(
531 &iter_dict, "eap", (const char **) eap_methods,
534 /* free returned method array */
535 while (eap_methods[i])
536 os_free(eap_methods[i++]);
537 os_free(eap_methods);
543 res = wpa_drv_get_capa(wpa_s, &capa);
545 /***** pairwise cipher */
548 const char *args[] = {"CCMP", "TKIP", "NONE"};
549 if (!wpa_dbus_dict_append_string_array(
550 &iter_dict, "pairwise", args,
555 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "pairwise",
561 if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
562 if (!wpa_dbus_dict_string_array_add_element(
563 &iter_array, "CCMP"))
567 if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
568 if (!wpa_dbus_dict_string_array_add_element(
569 &iter_array, "TKIP"))
573 if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
574 if (!wpa_dbus_dict_string_array_add_element(
575 &iter_array, "NONE"))
579 if (!wpa_dbus_dict_end_string_array(&iter_dict,
586 /***** group cipher */
589 const char *args[] = {
590 "CCMP", "TKIP", "WEP104", "WEP40"
592 if (!wpa_dbus_dict_append_string_array(
593 &iter_dict, "group", args,
598 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "group",
604 if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
605 if (!wpa_dbus_dict_string_array_add_element(
606 &iter_array, "CCMP"))
610 if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
611 if (!wpa_dbus_dict_string_array_add_element(
612 &iter_array, "TKIP"))
616 if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) {
617 if (!wpa_dbus_dict_string_array_add_element(
618 &iter_array, "WEP104"))
622 if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) {
623 if (!wpa_dbus_dict_string_array_add_element(
624 &iter_array, "WEP40"))
628 if (!wpa_dbus_dict_end_string_array(&iter_dict,
635 /***** key management */
638 const char *args[] = {
639 "WPA-PSK", "WPA-EAP", "IEEE8021X", "WPA-NONE",
642 if (!wpa_dbus_dict_append_string_array(
643 &iter_dict, "key_mgmt", args,
648 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "key_mgmt",
654 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
658 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
662 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
663 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
664 if (!wpa_dbus_dict_string_array_add_element(
665 &iter_array, "WPA-EAP"))
669 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
670 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
671 if (!wpa_dbus_dict_string_array_add_element(
672 &iter_array, "WPA-PSK"))
676 if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
677 if (!wpa_dbus_dict_string_array_add_element(
678 &iter_array, "WPA-NONE"))
682 if (!wpa_dbus_dict_end_string_array(&iter_dict,
689 /***** WPA protocol */
692 const char *args[] = { "RSN", "WPA" };
693 if (!wpa_dbus_dict_append_string_array(
694 &iter_dict, "proto", args,
699 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "proto",
705 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
706 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
707 if (!wpa_dbus_dict_string_array_add_element(
712 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
713 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
714 if (!wpa_dbus_dict_string_array_add_element(
719 if (!wpa_dbus_dict_end_string_array(&iter_dict,
729 const char *args[] = { "OPEN", "SHARED", "LEAP" };
730 if (!wpa_dbus_dict_append_string_array(
731 &iter_dict, "auth_alg", args,
736 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "auth_alg",
742 if (capa.auth & (WPA_DRIVER_AUTH_OPEN)) {
743 if (!wpa_dbus_dict_string_array_add_element(
744 &iter_array, "OPEN"))
748 if (capa.auth & (WPA_DRIVER_AUTH_SHARED)) {
749 if (!wpa_dbus_dict_string_array_add_element(
750 &iter_array, "SHARED"))
754 if (capa.auth & (WPA_DRIVER_AUTH_LEAP)) {
755 if (!wpa_dbus_dict_string_array_add_element(
756 &iter_array, "LEAP"))
760 if (!wpa_dbus_dict_end_string_array(&iter_dict,
767 if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
774 dbus_message_unref(reply);
775 return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
776 "an internal error occurred returning "
777 "interface capabilities.");
782 * wpas_dbus_iface_add_network - Add a new configured network
783 * @message: Pointer to incoming dbus message
784 * @wpa_s: wpa_supplicant structure for a network interface
785 * Returns: A dbus message containing the object path of the new network
787 * Handler function for "addNetwork" method call of a network interface.
789 DBusMessage * wpas_dbus_iface_add_network(DBusMessage *message,
790 struct wpa_supplicant *wpa_s)
792 DBusMessage *reply = NULL;
793 struct wpa_ssid *ssid;
794 char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *path = path_buf;
796 ssid = wpa_config_add_network(wpa_s->conf);
798 reply = dbus_message_new_error(message,
799 WPAS_ERROR_ADD_NETWORK_ERROR,
800 "wpa_supplicant could not add "
801 "a network on this interface.");
804 wpas_notify_network_added(wpa_s, ssid);
806 wpa_config_set_network_defaults(ssid);
808 /* Construct the object path for this network. */
809 os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
810 "%s/" WPAS_DBUS_NETWORKS_PART "/%d",
811 wpa_s->dbus_path, ssid->id);
813 reply = dbus_message_new_method_return(message);
814 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
815 &path, DBUS_TYPE_INVALID);
823 * wpas_dbus_iface_remove_network - Remove a configured network
824 * @message: Pointer to incoming dbus message
825 * @wpa_s: wpa_supplicant structure for a network interface
826 * Returns: A dbus message containing a UINT32 indicating success (1) or
829 * Handler function for "removeNetwork" method call of a network interface.
831 DBusMessage * wpas_dbus_iface_remove_network(DBusMessage *message,
832 struct wpa_supplicant *wpa_s)
834 DBusMessage *reply = NULL;
836 char *iface = NULL, *net_id = NULL;
838 struct wpa_ssid *ssid;
840 if (!dbus_message_get_args(message, NULL,
841 DBUS_TYPE_OBJECT_PATH, &op,
842 DBUS_TYPE_INVALID)) {
843 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
847 /* Extract the network ID */
848 iface = wpas_dbus_decompose_object_path(op, &net_id, NULL);
849 if (iface == NULL || net_id == NULL) {
850 reply = wpas_dbus_new_invalid_network_error(message);
854 /* Ensure the network is actually a child of this interface */
855 if (os_strcmp(iface, wpa_s->dbus_path) != 0) {
856 reply = wpas_dbus_new_invalid_network_error(message);
860 id = strtoul(net_id, NULL, 10);
861 ssid = wpa_config_get_network(wpa_s->conf, id);
863 reply = wpas_dbus_new_invalid_network_error(message);
867 wpas_notify_network_removed(wpa_s, ssid);
869 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
870 reply = dbus_message_new_error(message,
871 WPAS_ERROR_REMOVE_NETWORK_ERROR,
872 "error removing the specified "
873 "on this interface.");
877 if (ssid == wpa_s->current_ssid)
878 wpa_supplicant_deauthenticate(wpa_s,
879 WLAN_REASON_DEAUTH_LEAVING);
880 reply = wpas_dbus_new_success_reply(message);
889 static const char *dont_quote[] = {
890 "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
891 "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
896 static dbus_bool_t should_quote_opt(const char *key)
899 while (dont_quote[i] != NULL) {
900 if (strcmp(key, dont_quote[i]) == 0)
909 * wpas_dbus_iface_set_network - Set options for a configured network
910 * @message: Pointer to incoming dbus message
911 * @wpa_s: wpa_supplicant structure for a network interface
912 * @ssid: wpa_ssid structure for a configured network
913 * Returns: a dbus message containing a UINT32 indicating success (1) or
916 * Handler function for "set" method call of a configured network.
918 DBusMessage * wpas_dbus_iface_set_network(DBusMessage *message,
919 struct wpa_supplicant *wpa_s,
920 struct wpa_ssid *ssid)
922 DBusMessage *reply = NULL;
923 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
924 DBusMessageIter iter, iter_dict;
926 dbus_message_iter_init(message, &iter);
928 if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL)) {
929 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
933 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
938 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
939 reply = wpas_dbus_new_invalid_opts_error(message,
944 /* Type conversions, since wpa_supplicant wants strings */
945 if (entry.type == DBUS_TYPE_ARRAY &&
946 entry.array_type == DBUS_TYPE_BYTE) {
947 if (entry.array_len <= 0)
950 size = entry.array_len * 2 + 1;
951 value = os_zalloc(size);
954 ret = wpa_snprintf_hex(value, size,
955 (u8 *) entry.bytearray_value,
959 } else if (entry.type == DBUS_TYPE_STRING) {
960 if (should_quote_opt(entry.key)) {
961 size = os_strlen(entry.str_value);
962 /* Zero-length option check */
965 size += 3; /* For quotes and terminator */
966 value = os_zalloc(size);
969 ret = os_snprintf(value, size, "\"%s\"",
971 if (os_snprintf_error(size, ret))
974 value = os_strdup(entry.str_value);
978 } else if (entry.type == DBUS_TYPE_UINT32) {
979 value = os_zalloc(size);
982 ret = os_snprintf(value, size, "%u",
984 if (os_snprintf_error(size, ret))
986 } else if (entry.type == DBUS_TYPE_INT32) {
987 value = os_zalloc(size);
990 ret = os_snprintf(value, size, "%d",
992 if (os_snprintf_error(size, ret))
997 if (wpa_config_set(ssid, entry.key, value, 0) < 0)
1000 if ((os_strcmp(entry.key, "psk") == 0 &&
1001 value[0] == '"' && ssid->ssid_len) ||
1002 (os_strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
1003 wpa_config_update_psk(ssid);
1004 else if (os_strcmp(entry.key, "priority") == 0)
1005 wpa_config_update_prio_list(wpa_s->conf);
1008 wpa_dbus_dict_entry_clear(&entry);
1013 reply = wpas_dbus_new_invalid_opts_error(message, entry.key);
1014 wpa_dbus_dict_entry_clear(&entry);
1019 reply = wpas_dbus_new_success_reply(message);
1027 * wpas_dbus_iface_enable_network - Mark a configured network as enabled
1028 * @message: Pointer to incoming dbus message
1029 * @wpa_s: wpa_supplicant structure for a network interface
1030 * @ssid: wpa_ssid structure for a configured network
1031 * Returns: A dbus message containing a UINT32 indicating success (1) or
1034 * Handler function for "enable" method call of a configured network.
1036 DBusMessage * wpas_dbus_iface_enable_network(DBusMessage *message,
1037 struct wpa_supplicant *wpa_s,
1038 struct wpa_ssid *ssid)
1040 wpa_supplicant_enable_network(wpa_s, ssid);
1041 return wpas_dbus_new_success_reply(message);
1046 * wpas_dbus_iface_disable_network - Mark a configured network as disabled
1047 * @message: Pointer to incoming dbus message
1048 * @wpa_s: wpa_supplicant structure for a network interface
1049 * @ssid: wpa_ssid structure for a configured network
1050 * Returns: A dbus message containing a UINT32 indicating success (1) or
1053 * Handler function for "disable" method call of a configured network.
1055 DBusMessage * wpas_dbus_iface_disable_network(DBusMessage *message,
1056 struct wpa_supplicant *wpa_s,
1057 struct wpa_ssid *ssid)
1059 wpa_supplicant_disable_network(wpa_s, ssid);
1060 return wpas_dbus_new_success_reply(message);
1065 * wpas_dbus_iface_select_network - Attempt association with a configured network
1066 * @message: Pointer to incoming dbus message
1067 * @wpa_s: wpa_supplicant structure for a network interface
1068 * Returns: A dbus message containing a UINT32 indicating success (1) or
1071 * Handler function for "selectNetwork" method call of network interface.
1073 DBusMessage * wpas_dbus_iface_select_network(DBusMessage *message,
1074 struct wpa_supplicant *wpa_s)
1076 DBusMessage *reply = NULL;
1078 struct wpa_ssid *ssid;
1079 char *iface_obj_path = NULL;
1080 char *network = NULL;
1082 if (os_strlen(dbus_message_get_signature(message)) == 0) {
1088 if (!dbus_message_get_args(message, NULL,
1089 DBUS_TYPE_OBJECT_PATH, &op,
1090 DBUS_TYPE_INVALID)) {
1091 reply = wpas_dbus_new_invalid_opts_error(message,
1096 /* Extract the network number */
1097 iface_obj_path = wpas_dbus_decompose_object_path(op,
1100 if (iface_obj_path == NULL) {
1101 reply = wpas_dbus_new_invalid_iface_error(message);
1104 /* Ensure the object path really points to this interface */
1105 if (network == NULL ||
1106 os_strcmp(iface_obj_path, wpa_s->dbus_path) != 0) {
1107 reply = wpas_dbus_new_invalid_network_error(message);
1111 nid = strtoul(network, NULL, 10);
1112 if (errno == EINVAL) {
1113 reply = wpas_dbus_new_invalid_network_error(message);
1117 ssid = wpa_config_get_network(wpa_s->conf, nid);
1119 reply = wpas_dbus_new_invalid_network_error(message);
1124 /* Finally, associate with the network */
1125 wpa_supplicant_select_network(wpa_s, ssid);
1127 reply = wpas_dbus_new_success_reply(message);
1130 os_free(iface_obj_path);
1137 * wpas_dbus_iface_disconnect - Terminate the current connection
1138 * @message: Pointer to incoming dbus message
1139 * @wpa_s: wpa_supplicant structure for a network interface
1140 * Returns: A dbus message containing a UINT32 indicating success (1) or
1143 * Handler function for "disconnect" method call of network interface.
1145 DBusMessage * wpas_dbus_iface_disconnect(DBusMessage *message,
1146 struct wpa_supplicant *wpa_s)
1148 wpa_s->disconnected = 1;
1149 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1151 return wpas_dbus_new_success_reply(message);
1156 * wpas_dbus_iface_set_ap_scan - Control roaming mode
1157 * @message: Pointer to incoming dbus message
1158 * @wpa_s: wpa_supplicant structure for a network interface
1159 * Returns: A dbus message containing a UINT32 indicating success (1) or
1162 * Handler function for "setAPScan" method call.
1164 DBusMessage * wpas_dbus_iface_set_ap_scan(DBusMessage *message,
1165 struct wpa_supplicant *wpa_s)
1167 DBusMessage *reply = NULL;
1168 dbus_uint32_t ap_scan = 1;
1170 if (!dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &ap_scan,
1171 DBUS_TYPE_INVALID)) {
1172 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
1176 if (wpa_supplicant_set_ap_scan(wpa_s, ap_scan)) {
1177 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
1181 reply = wpas_dbus_new_success_reply(message);
1189 * wpas_dbus_iface_set_smartcard_modules - Set smartcard related module paths
1190 * @message: Pointer to incoming dbus message
1191 * @wpa_s: wpa_supplicant structure for a network interface
1192 * Returns: A dbus message containing a UINT32 indicating success (1) or
1195 * Handler function for "setSmartcardModules" method call.
1197 DBusMessage * wpas_dbus_iface_set_smartcard_modules(
1198 DBusMessage *message, struct wpa_supplicant *wpa_s)
1200 DBusMessageIter iter, iter_dict;
1201 char *opensc_engine_path = NULL;
1202 char *pkcs11_engine_path = NULL;
1203 char *pkcs11_module_path = NULL;
1204 struct wpa_dbus_dict_entry entry;
1206 if (!dbus_message_iter_init(message, &iter))
1209 if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
1212 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
1213 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
1215 if (!strcmp(entry.key, "opensc_engine_path") &&
1216 (entry.type == DBUS_TYPE_STRING)) {
1217 os_free(opensc_engine_path);
1218 opensc_engine_path = os_strdup(entry.str_value);
1219 if (opensc_engine_path == NULL)
1221 } else if (!strcmp(entry.key, "pkcs11_engine_path") &&
1222 (entry.type == DBUS_TYPE_STRING)) {
1223 os_free(pkcs11_engine_path);
1224 pkcs11_engine_path = os_strdup(entry.str_value);
1225 if (pkcs11_engine_path == NULL)
1227 } else if (!strcmp(entry.key, "pkcs11_module_path") &&
1228 (entry.type == DBUS_TYPE_STRING)) {
1229 os_free(pkcs11_module_path);
1230 pkcs11_module_path = os_strdup(entry.str_value);
1231 if (pkcs11_module_path == NULL)
1234 wpa_dbus_dict_entry_clear(&entry);
1237 wpa_dbus_dict_entry_clear(&entry);
1240 os_free(wpa_s->conf->opensc_engine_path);
1241 wpa_s->conf->opensc_engine_path = opensc_engine_path;
1242 os_free(wpa_s->conf->pkcs11_engine_path);
1243 wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path;
1244 os_free(wpa_s->conf->pkcs11_module_path);
1245 wpa_s->conf->pkcs11_module_path = pkcs11_module_path;
1247 wpa_sm_set_eapol(wpa_s->wpa, NULL);
1248 eapol_sm_deinit(wpa_s->eapol);
1249 wpa_s->eapol = NULL;
1250 wpa_supplicant_init_eapol(wpa_s);
1251 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
1253 return wpas_dbus_new_success_reply(message);
1256 os_free(opensc_engine_path);
1257 os_free(pkcs11_engine_path);
1258 os_free(pkcs11_module_path);
1259 return wpas_dbus_new_invalid_opts_error(message, NULL);
1264 * wpas_dbus_iface_get_state - Get interface state
1265 * @message: Pointer to incoming dbus message
1266 * @wpa_s: wpa_supplicant structure for a network interface
1267 * Returns: A dbus message containing a STRING representing the current
1270 * Handler function for "state" method call.
1272 DBusMessage * wpas_dbus_iface_get_state(DBusMessage *message,
1273 struct wpa_supplicant *wpa_s)
1275 DBusMessage *reply = NULL;
1276 const char *str_state;
1278 reply = dbus_message_new_method_return(message);
1279 if (reply != NULL) {
1280 str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
1281 dbus_message_append_args(reply, DBUS_TYPE_STRING, &str_state,
1290 * wpas_dbus_iface_get_scanning - Get interface scanning state
1291 * @message: Pointer to incoming dbus message
1292 * @wpa_s: wpa_supplicant structure for a network interface
1293 * Returns: A dbus message containing whether the interface is scanning
1295 * Handler function for "scanning" method call.
1297 DBusMessage * wpas_dbus_iface_get_scanning(DBusMessage *message,
1298 struct wpa_supplicant *wpa_s)
1300 DBusMessage *reply = NULL;
1301 dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
1303 reply = dbus_message_new_method_return(message);
1304 if (reply != NULL) {
1305 dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &scanning,
1308 wpa_printf(MSG_ERROR, "dbus: Not enough memory to return "
1316 #ifndef CONFIG_NO_CONFIG_BLOBS
1319 * wpas_dbus_iface_set_blobs - Store named binary blobs (ie, for certificates)
1320 * @message: Pointer to incoming dbus message
1321 * @wpa_s: %wpa_supplicant data structure
1322 * Returns: A dbus message containing a UINT32 indicating success (1) or
1325 * Asks wpa_supplicant to internally store a one or more binary blobs.
1327 DBusMessage * wpas_dbus_iface_set_blobs(DBusMessage *message,
1328 struct wpa_supplicant *wpa_s)
1330 DBusMessage *reply = NULL;
1331 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
1332 DBusMessageIter iter, iter_dict;
1334 dbus_message_iter_init(message, &iter);
1336 if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
1337 return wpas_dbus_new_invalid_opts_error(message, NULL);
1339 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
1340 struct wpa_config_blob *blob;
1342 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
1343 reply = wpas_dbus_new_invalid_opts_error(message,
1348 if (entry.type != DBUS_TYPE_ARRAY ||
1349 entry.array_type != DBUS_TYPE_BYTE) {
1350 reply = wpas_dbus_new_invalid_opts_error(
1351 message, "Byte array expected.");
1355 if ((entry.array_len <= 0) || (entry.array_len > 65536) ||
1356 !strlen(entry.key)) {
1357 reply = wpas_dbus_new_invalid_opts_error(
1358 message, "Invalid array size.");
1362 blob = os_zalloc(sizeof(*blob));
1364 reply = dbus_message_new_error(
1365 message, WPAS_ERROR_ADD_ERROR,
1366 "Not enough memory to add blob.");
1369 blob->data = os_zalloc(entry.array_len);
1370 if (blob->data == NULL) {
1371 reply = dbus_message_new_error(
1372 message, WPAS_ERROR_ADD_ERROR,
1373 "Not enough memory to add blob data.");
1378 blob->name = os_strdup(entry.key);
1379 blob->len = entry.array_len;
1380 os_memcpy(blob->data, (u8 *) entry.bytearray_value,
1382 if (blob->name == NULL) {
1383 wpa_config_free_blob(blob);
1384 reply = dbus_message_new_error(
1385 message, WPAS_ERROR_ADD_ERROR,
1386 "Error adding blob.");
1391 if (!wpa_config_remove_blob(wpa_s->conf, blob->name))
1392 wpas_notify_blob_removed(wpa_s, blob->name);
1393 wpa_config_set_blob(wpa_s->conf, blob);
1394 wpas_notify_blob_added(wpa_s, blob->name);
1396 wpa_dbus_dict_entry_clear(&entry);
1398 wpa_dbus_dict_entry_clear(&entry);
1400 return reply ? reply : wpas_dbus_new_success_reply(message);
1405 * wpas_dbus_iface_remove_blob - Remove named binary blobs
1406 * @message: Pointer to incoming dbus message
1407 * @wpa_s: %wpa_supplicant data structure
1408 * Returns: A dbus message containing a UINT32 indicating success (1) or
1411 * Asks wpa_supplicant to remove one or more previously stored binary blobs.
1413 DBusMessage * wpas_dbus_iface_remove_blobs(DBusMessage *message,
1414 struct wpa_supplicant *wpa_s)
1416 DBusMessageIter iter, array;
1417 char *err_msg = NULL;
1419 dbus_message_iter_init(message, &iter);
1421 if ((dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY) ||
1422 (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_STRING))
1423 return wpas_dbus_new_invalid_opts_error(message, NULL);
1425 dbus_message_iter_recurse(&iter, &array);
1426 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
1429 dbus_message_iter_get_basic(&array, &name);
1430 if (!os_strlen(name))
1431 err_msg = "Invalid blob name.";
1432 else if (wpa_config_remove_blob(wpa_s->conf, name) != 0)
1433 err_msg = "Error removing blob.";
1435 wpas_notify_blob_removed(wpa_s, name);
1436 dbus_message_iter_next(&array);
1440 return dbus_message_new_error(message, WPAS_ERROR_REMOVE_ERROR,
1443 return wpas_dbus_new_success_reply(message);
1446 #endif /* CONFIG_NO_CONFIG_BLOBS */
1450 * wpas_dbus_iface_flush - Clear BSS of old or all inactive entries
1451 * @message: Pointer to incoming dbus message
1452 * @wpa_s: %wpa_supplicant data structure
1453 * Returns: a dbus message containing a UINT32 indicating success (1) or
1454 * failure (0), or returns a dbus error message with more information
1456 * Handler function for "flush" method call. Handles requests for an
1457 * interface with an optional "age" parameter that specifies the minimum
1458 * age of a BSS to be flushed.
1460 DBusMessage * wpas_dbus_iface_flush(DBusMessage *message,
1461 struct wpa_supplicant *wpa_s)
1465 if (os_strlen(dbus_message_get_signature(message)) != 0 &&
1466 !dbus_message_get_args(message, NULL,
1467 DBUS_TYPE_INT32, &flush_age,
1468 DBUS_TYPE_INVALID)) {
1469 return wpas_dbus_new_invalid_opts_error(message, NULL);
1473 wpa_bss_flush(wpa_s);
1475 wpa_bss_flush_by_age(wpa_s, flush_age);
1477 return wpas_dbus_new_success_reply(message);