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);
169 if (wpa_s && wpa_s->dbus_path) {
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);
265 if (wpa_s == NULL || !wpa_s->dbus_path) {
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 if (!wpa_s->dbus_path)
358 return dbus_message_new_error(message,
359 WPAS_ERROR_INTERNAL_ERROR,
360 "no D-Bus interface available");
362 /* Create and initialize the return message */
363 reply = dbus_message_new_method_return(message);
364 dbus_message_iter_init_append(reply, &iter);
365 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
366 DBUS_TYPE_OBJECT_PATH_AS_STRING,
370 /* Loop through scan results and append each result's object path */
371 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
372 char path_buf[WPAS_DBUS_OBJECT_PATH_MAX];
373 char *path = path_buf;
375 /* Construct the object path for this network. Note that ':'
376 * is not a valid character in dbus object paths.
378 os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
379 "%s/" WPAS_DBUS_BSSIDS_PART "/"
380 WPAS_DBUS_BSSID_FORMAT,
381 wpa_s->dbus_path, MAC2STR(bss->bssid));
382 if (!dbus_message_iter_append_basic(&sub_iter,
383 DBUS_TYPE_OBJECT_PATH,
388 if (!dbus_message_iter_close_container(&iter, &sub_iter))
394 dbus_message_unref(reply);
395 return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
396 "an internal error occurred returning scan results");
401 * wpas_dbus_bssid_properties - Return the properties of a scanned network
402 * @message: Pointer to incoming dbus message
403 * @wpa_s: wpa_supplicant structure for a network interface
404 * @res: wpa_supplicant scan result for which to get properties
405 * Returns: a dbus message containing the properties for the requested network
407 * Handler function for "properties" method call of a scanned network.
408 * Returns a dbus message containing the the properties.
410 DBusMessage * wpas_dbus_bssid_properties(DBusMessage *message,
411 struct wpa_supplicant *wpa_s,
415 DBusMessageIter iter, iter_dict;
416 const u8 *wpa_ie, *rsn_ie, *wps_ie;
418 /* Dump the properties into a dbus message */
419 reply = dbus_message_new_method_return(message);
421 wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
422 rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
423 wps_ie = wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE);
425 dbus_message_iter_init_append(reply, &iter);
426 if (!wpa_dbus_dict_open_write(&iter, &iter_dict) ||
427 !wpa_dbus_dict_append_byte_array(&iter_dict, "bssid",
428 (const char *) bss->bssid,
430 !wpa_dbus_dict_append_byte_array(&iter_dict, "ssid",
431 (const char *) bss->ssid,
434 !wpa_dbus_dict_append_byte_array(&iter_dict, "wpaie",
435 (const char *) wpa_ie,
438 !wpa_dbus_dict_append_byte_array(&iter_dict, "rsnie",
439 (const char *) rsn_ie,
442 !wpa_dbus_dict_append_byte_array(&iter_dict, "wpsie",
443 (const char *) wps_ie,
446 !wpa_dbus_dict_append_int32(&iter_dict, "frequency", bss->freq)) ||
447 !wpa_dbus_dict_append_uint16(&iter_dict, "capabilities",
449 (!(bss->flags & WPA_BSS_QUAL_INVALID) &&
450 !wpa_dbus_dict_append_int32(&iter_dict, "quality", bss->qual)) ||
451 (!(bss->flags & WPA_BSS_NOISE_INVALID) &&
452 !wpa_dbus_dict_append_int32(&iter_dict, "noise", bss->noise)) ||
453 (!(bss->flags & WPA_BSS_LEVEL_INVALID) &&
454 !wpa_dbus_dict_append_int32(&iter_dict, "level", bss->level)) ||
455 !wpa_dbus_dict_append_int32(&iter_dict, "maxrate",
456 wpa_bss_get_max_rate(bss) * 500000) ||
457 !wpa_dbus_dict_close_write(&iter, &iter_dict)) {
459 dbus_message_unref(reply);
460 reply = dbus_message_new_error(
461 message, WPAS_ERROR_INTERNAL_ERROR,
462 "an internal error occurred returning BSSID properties.");
470 * wpas_dbus_iface_capabilities - Return interface capabilities
471 * @message: Pointer to incoming dbus message
472 * @wpa_s: wpa_supplicant structure for a network interface
473 * Returns: A dbus message containing a dict of strings
475 * Handler function for "capabilities" method call of an interface.
477 DBusMessage * wpas_dbus_iface_capabilities(DBusMessage *message,
478 struct wpa_supplicant *wpa_s)
480 DBusMessage *reply = NULL;
481 struct wpa_driver_capa capa;
483 DBusMessageIter iter, iter_dict;
486 dbus_bool_t strict = FALSE;
487 DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
489 if (!dbus_message_get_args(message, NULL,
490 DBUS_TYPE_BOOLEAN, &strict,
494 reply = dbus_message_new_method_return(message);
496 dbus_message_iter_init_append(reply, &iter);
497 if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
501 eap_methods = eap_get_names_as_string_array(&num_items);
506 success = wpa_dbus_dict_append_string_array(
507 &iter_dict, "eap", (const char **) eap_methods,
510 /* free returned method array */
511 while (eap_methods[i])
512 os_free(eap_methods[i++]);
513 os_free(eap_methods);
519 res = wpa_drv_get_capa(wpa_s, &capa);
521 /***** pairwise cipher */
524 const char *args[] = {"CCMP", "TKIP", "NONE"};
526 if (!wpa_dbus_dict_append_string_array(
527 &iter_dict, "pairwise", args,
532 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "pairwise",
536 ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) &&
537 !wpa_dbus_dict_string_array_add_element(
538 &iter_array, "CCMP")) ||
539 ((capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) &&
540 !wpa_dbus_dict_string_array_add_element(
541 &iter_array, "TKIP")) ||
542 ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) &&
543 !wpa_dbus_dict_string_array_add_element(
544 &iter_array, "NONE")) ||
545 !wpa_dbus_dict_end_string_array(&iter_dict,
552 /***** group cipher */
555 const char *args[] = {
556 "CCMP", "TKIP", "WEP104", "WEP40"
559 if (!wpa_dbus_dict_append_string_array(
560 &iter_dict, "group", args,
565 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "group",
571 if (((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) &&
572 !wpa_dbus_dict_string_array_add_element(
573 &iter_array, "CCMP")) ||
574 ((capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) &&
575 !wpa_dbus_dict_string_array_add_element(
576 &iter_array, "TKIP")) ||
577 ((capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) &&
578 !wpa_dbus_dict_string_array_add_element(
579 &iter_array, "WEP104")) ||
580 ((capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) &&
581 !wpa_dbus_dict_string_array_add_element(
582 &iter_array, "WEP40")) ||
583 !wpa_dbus_dict_end_string_array(&iter_dict,
590 /***** key management */
593 const char *args[] = {
594 "WPA-PSK", "WPA-EAP", "IEEE8021X", "WPA-NONE",
597 if (!wpa_dbus_dict_append_string_array(
598 &iter_dict, "key_mgmt", args,
603 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "key_mgmt",
607 !wpa_dbus_dict_string_array_add_element(&iter_array,
609 !wpa_dbus_dict_string_array_add_element(&iter_array,
611 ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
612 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) &&
613 !wpa_dbus_dict_string_array_add_element(
614 &iter_array, "WPA-EAP")) ||
615 ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
616 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) &&
617 !wpa_dbus_dict_string_array_add_element(
618 &iter_array, "WPA-PSK")) ||
619 ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) &&
620 !wpa_dbus_dict_string_array_add_element(
621 &iter_array, "WPA-NONE")) ||
622 !wpa_dbus_dict_end_string_array(&iter_dict,
629 /***** WPA protocol */
632 const char *args[] = { "RSN", "WPA" };
634 if (!wpa_dbus_dict_append_string_array(
635 &iter_dict, "proto", args,
640 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "proto",
644 ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
645 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) &&
646 !wpa_dbus_dict_string_array_add_element(
647 &iter_array, "RSN")) ||
648 ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
649 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) &&
650 !wpa_dbus_dict_string_array_add_element(
651 &iter_array, "WPA")) ||
652 !wpa_dbus_dict_end_string_array(&iter_dict,
662 const char *args[] = { "OPEN", "SHARED", "LEAP" };
664 if (!wpa_dbus_dict_append_string_array(
665 &iter_dict, "auth_alg", args,
670 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "auth_alg",
674 ((capa.auth & WPA_DRIVER_AUTH_OPEN) &&
675 !wpa_dbus_dict_string_array_add_element(
676 &iter_array, "OPEN")) ||
677 ((capa.auth & WPA_DRIVER_AUTH_SHARED) &&
678 !wpa_dbus_dict_string_array_add_element(
679 &iter_array, "SHARED")) ||
680 ((capa.auth & WPA_DRIVER_AUTH_LEAP) &&
681 !wpa_dbus_dict_string_array_add_element(
682 &iter_array, "LEAP")) ||
683 !wpa_dbus_dict_end_string_array(&iter_dict,
690 if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
697 dbus_message_unref(reply);
698 return dbus_message_new_error(
699 message, WPAS_ERROR_INTERNAL_ERROR,
700 "an internal error occurred returning interface capabilities.");
705 * wpas_dbus_iface_add_network - Add a new configured network
706 * @message: Pointer to incoming dbus message
707 * @wpa_s: wpa_supplicant structure for a network interface
708 * Returns: A dbus message containing the object path of the new network
710 * Handler function for "addNetwork" method call of a network interface.
712 DBusMessage * wpas_dbus_iface_add_network(DBusMessage *message,
713 struct wpa_supplicant *wpa_s)
715 DBusMessage *reply = NULL;
716 struct wpa_ssid *ssid = NULL;
717 char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *path = path_buf;
719 if (wpa_s->dbus_path)
720 ssid = wpa_config_add_network(wpa_s->conf);
722 reply = dbus_message_new_error(
723 message, WPAS_ERROR_ADD_NETWORK_ERROR,
724 "wpa_supplicant could not add a network on this interface.");
727 wpas_notify_network_added(wpa_s, ssid);
729 wpa_config_set_network_defaults(ssid);
731 /* Construct the object path for this network. */
732 os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
733 "%s/" WPAS_DBUS_NETWORKS_PART "/%d",
734 wpa_s->dbus_path, ssid->id);
736 reply = dbus_message_new_method_return(message);
737 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
738 &path, DBUS_TYPE_INVALID);
746 * wpas_dbus_iface_remove_network - Remove a configured network
747 * @message: Pointer to incoming dbus message
748 * @wpa_s: wpa_supplicant structure for a network interface
749 * Returns: A dbus message containing a UINT32 indicating success (1) or
752 * Handler function for "removeNetwork" method call of a network interface.
754 DBusMessage * wpas_dbus_iface_remove_network(DBusMessage *message,
755 struct wpa_supplicant *wpa_s)
757 DBusMessage *reply = NULL;
759 char *iface = NULL, *net_id = NULL;
761 struct wpa_ssid *ssid;
763 if (!dbus_message_get_args(message, NULL,
764 DBUS_TYPE_OBJECT_PATH, &op,
765 DBUS_TYPE_INVALID)) {
766 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
770 /* Extract the network ID */
771 iface = wpas_dbus_decompose_object_path(op, &net_id, NULL);
772 if (iface == NULL || net_id == NULL) {
773 reply = wpas_dbus_new_invalid_network_error(message);
777 /* Ensure the network is actually a child of this interface */
778 if (!wpa_s->dbus_path || os_strcmp(iface, wpa_s->dbus_path) != 0) {
779 reply = wpas_dbus_new_invalid_network_error(message);
783 id = strtoul(net_id, NULL, 10);
784 ssid = wpa_config_get_network(wpa_s->conf, id);
786 reply = wpas_dbus_new_invalid_network_error(message);
790 wpas_notify_network_removed(wpa_s, ssid);
792 if (ssid == wpa_s->current_ssid)
793 wpa_supplicant_deauthenticate(wpa_s,
794 WLAN_REASON_DEAUTH_LEAVING);
796 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
797 reply = dbus_message_new_error(
798 message, WPAS_ERROR_REMOVE_NETWORK_ERROR,
799 "error removing the specified on this interface.");
803 reply = wpas_dbus_new_success_reply(message);
812 static const char * const dont_quote[] = {
813 "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
814 "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
815 "bssid", "scan_freq", "freq_list", NULL
819 static dbus_bool_t should_quote_opt(const char *key)
823 while (dont_quote[i] != NULL) {
824 if (os_strcmp(key, dont_quote[i]) == 0)
833 * wpas_dbus_iface_set_network - Set options for a configured network
834 * @message: Pointer to incoming dbus message
835 * @wpa_s: wpa_supplicant structure for a network interface
836 * @ssid: wpa_ssid structure for a configured network
837 * Returns: a dbus message containing a UINT32 indicating success (1) or
840 * Handler function for "set" method call of a configured network.
842 DBusMessage * wpas_dbus_iface_set_network(DBusMessage *message,
843 struct wpa_supplicant *wpa_s,
844 struct wpa_ssid *ssid)
846 DBusMessage *reply = NULL;
847 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
848 DBusMessageIter iter, iter_dict;
850 dbus_message_iter_init(message, &iter);
852 if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL)) {
853 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
857 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
862 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
863 reply = wpas_dbus_new_invalid_opts_error(message,
868 /* Type conversions, since wpa_supplicant wants strings */
869 if (entry.type == DBUS_TYPE_ARRAY &&
870 entry.array_type == DBUS_TYPE_BYTE) {
871 if (entry.array_len <= 0)
874 size = entry.array_len * 2 + 1;
875 value = os_zalloc(size);
878 ret = wpa_snprintf_hex(value, size,
879 (u8 *) entry.bytearray_value,
883 } else if (entry.type == DBUS_TYPE_STRING) {
884 if (should_quote_opt(entry.key)) {
885 size = os_strlen(entry.str_value);
886 /* Zero-length option check */
889 size += 3; /* For quotes and terminator */
890 value = os_zalloc(size);
893 ret = os_snprintf(value, size, "\"%s\"",
895 if (os_snprintf_error(size, ret))
898 value = os_strdup(entry.str_value);
902 } else if (entry.type == DBUS_TYPE_UINT32) {
903 value = os_zalloc(size);
906 ret = os_snprintf(value, size, "%u",
908 if (os_snprintf_error(size, ret))
910 } else if (entry.type == DBUS_TYPE_INT32) {
911 value = os_zalloc(size);
914 ret = os_snprintf(value, size, "%d",
916 if (os_snprintf_error(size, ret))
921 if (wpa_config_set(ssid, entry.key, value, 0) < 0)
924 if ((os_strcmp(entry.key, "psk") == 0 &&
925 value[0] == '"' && ssid->ssid_len) ||
926 (os_strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
927 wpa_config_update_psk(ssid);
928 else if (os_strcmp(entry.key, "priority") == 0)
929 wpa_config_update_prio_list(wpa_s->conf);
932 wpa_dbus_dict_entry_clear(&entry);
937 reply = wpas_dbus_new_invalid_opts_error(message, entry.key);
938 wpa_dbus_dict_entry_clear(&entry);
943 reply = wpas_dbus_new_success_reply(message);
951 * wpas_dbus_iface_enable_network - Mark a configured network as enabled
952 * @message: Pointer to incoming dbus message
953 * @wpa_s: wpa_supplicant structure for a network interface
954 * @ssid: wpa_ssid structure for a configured network
955 * Returns: A dbus message containing a UINT32 indicating success (1) or
958 * Handler function for "enable" method call of a configured network.
960 DBusMessage * wpas_dbus_iface_enable_network(DBusMessage *message,
961 struct wpa_supplicant *wpa_s,
962 struct wpa_ssid *ssid)
964 wpa_supplicant_enable_network(wpa_s, ssid);
965 return wpas_dbus_new_success_reply(message);
970 * wpas_dbus_iface_disable_network - Mark a configured network as disabled
971 * @message: Pointer to incoming dbus message
972 * @wpa_s: wpa_supplicant structure for a network interface
973 * @ssid: wpa_ssid structure for a configured network
974 * Returns: A dbus message containing a UINT32 indicating success (1) or
977 * Handler function for "disable" method call of a configured network.
979 DBusMessage * wpas_dbus_iface_disable_network(DBusMessage *message,
980 struct wpa_supplicant *wpa_s,
981 struct wpa_ssid *ssid)
983 wpa_supplicant_disable_network(wpa_s, ssid);
984 return wpas_dbus_new_success_reply(message);
989 * wpas_dbus_iface_select_network - Attempt association with a configured network
990 * @message: Pointer to incoming dbus message
991 * @wpa_s: wpa_supplicant structure for a network interface
992 * Returns: A dbus message containing a UINT32 indicating success (1) or
995 * Handler function for "selectNetwork" method call of network interface.
997 DBusMessage * wpas_dbus_iface_select_network(DBusMessage *message,
998 struct wpa_supplicant *wpa_s)
1000 DBusMessage *reply = NULL;
1002 struct wpa_ssid *ssid;
1003 char *iface_obj_path = NULL;
1004 char *network = NULL;
1006 if (os_strlen(dbus_message_get_signature(message)) == 0) {
1012 if (!dbus_message_get_args(message, NULL,
1013 DBUS_TYPE_OBJECT_PATH, &op,
1014 DBUS_TYPE_INVALID)) {
1015 reply = wpas_dbus_new_invalid_opts_error(message,
1020 /* Extract the network number */
1021 iface_obj_path = wpas_dbus_decompose_object_path(op,
1024 if (iface_obj_path == NULL) {
1025 reply = wpas_dbus_new_invalid_iface_error(message);
1028 /* Ensure the object path really points to this interface */
1029 if (network == NULL || !wpa_s->dbus_path ||
1030 os_strcmp(iface_obj_path, wpa_s->dbus_path) != 0) {
1031 reply = wpas_dbus_new_invalid_network_error(message);
1035 nid = strtoul(network, NULL, 10);
1036 if (errno == EINVAL) {
1037 reply = wpas_dbus_new_invalid_network_error(message);
1041 ssid = wpa_config_get_network(wpa_s->conf, nid);
1043 reply = wpas_dbus_new_invalid_network_error(message);
1048 /* Finally, associate with the network */
1049 wpa_supplicant_select_network(wpa_s, ssid);
1051 reply = wpas_dbus_new_success_reply(message);
1054 os_free(iface_obj_path);
1061 * wpas_dbus_iface_disconnect - Terminate the current connection
1062 * @message: Pointer to incoming dbus message
1063 * @wpa_s: wpa_supplicant structure for a network interface
1064 * Returns: A dbus message containing a UINT32 indicating success (1) or
1067 * Handler function for "disconnect" method call of network interface.
1069 DBusMessage * wpas_dbus_iface_disconnect(DBusMessage *message,
1070 struct wpa_supplicant *wpa_s)
1072 wpa_s->disconnected = 1;
1073 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1075 return wpas_dbus_new_success_reply(message);
1080 * wpas_dbus_iface_set_ap_scan - Control roaming mode
1081 * @message: Pointer to incoming dbus message
1082 * @wpa_s: wpa_supplicant structure for a network interface
1083 * Returns: A dbus message containing a UINT32 indicating success (1) or
1086 * Handler function for "setAPScan" method call.
1088 DBusMessage * wpas_dbus_iface_set_ap_scan(DBusMessage *message,
1089 struct wpa_supplicant *wpa_s)
1091 DBusMessage *reply = NULL;
1092 dbus_uint32_t ap_scan = 1;
1094 if (!dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &ap_scan,
1095 DBUS_TYPE_INVALID)) {
1096 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
1100 if (wpa_supplicant_set_ap_scan(wpa_s, ap_scan)) {
1101 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
1105 reply = wpas_dbus_new_success_reply(message);
1113 * wpas_dbus_iface_set_smartcard_modules - Set smartcard related module paths
1114 * @message: Pointer to incoming dbus message
1115 * @wpa_s: wpa_supplicant structure for a network interface
1116 * Returns: A dbus message containing a UINT32 indicating success (1) or
1119 * Handler function for "setSmartcardModules" method call.
1121 DBusMessage * wpas_dbus_iface_set_smartcard_modules(
1122 DBusMessage *message, struct wpa_supplicant *wpa_s)
1124 DBusMessageIter iter, iter_dict;
1125 char *opensc_engine_path = NULL;
1126 char *pkcs11_engine_path = NULL;
1127 char *pkcs11_module_path = NULL;
1128 struct wpa_dbus_dict_entry entry;
1130 if (!dbus_message_iter_init(message, &iter))
1133 if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
1136 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
1137 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
1139 if (!strcmp(entry.key, "opensc_engine_path") &&
1140 entry.type == DBUS_TYPE_STRING) {
1141 os_free(opensc_engine_path);
1142 opensc_engine_path = os_strdup(entry.str_value);
1143 wpa_dbus_dict_entry_clear(&entry);
1144 if (opensc_engine_path == NULL)
1146 } else if (!strcmp(entry.key, "pkcs11_engine_path") &&
1147 entry.type == DBUS_TYPE_STRING) {
1148 os_free(pkcs11_engine_path);
1149 pkcs11_engine_path = os_strdup(entry.str_value);
1150 wpa_dbus_dict_entry_clear(&entry);
1151 if (pkcs11_engine_path == NULL)
1153 } else if (!strcmp(entry.key, "pkcs11_module_path") &&
1154 entry.type == DBUS_TYPE_STRING) {
1155 os_free(pkcs11_module_path);
1156 pkcs11_module_path = os_strdup(entry.str_value);
1157 wpa_dbus_dict_entry_clear(&entry);
1158 if (pkcs11_module_path == NULL)
1161 wpa_dbus_dict_entry_clear(&entry);
1166 os_free(wpa_s->conf->opensc_engine_path);
1167 wpa_s->conf->opensc_engine_path = opensc_engine_path;
1168 os_free(wpa_s->conf->pkcs11_engine_path);
1169 wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path;
1170 os_free(wpa_s->conf->pkcs11_module_path);
1171 wpa_s->conf->pkcs11_module_path = pkcs11_module_path;
1173 wpa_sm_set_eapol(wpa_s->wpa, NULL);
1174 eapol_sm_deinit(wpa_s->eapol);
1175 wpa_s->eapol = NULL;
1176 wpa_supplicant_init_eapol(wpa_s);
1177 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
1179 return wpas_dbus_new_success_reply(message);
1182 os_free(opensc_engine_path);
1183 os_free(pkcs11_engine_path);
1184 os_free(pkcs11_module_path);
1185 return wpas_dbus_new_invalid_opts_error(message, NULL);
1190 * wpas_dbus_iface_get_state - Get interface state
1191 * @message: Pointer to incoming dbus message
1192 * @wpa_s: wpa_supplicant structure for a network interface
1193 * Returns: A dbus message containing a STRING representing the current
1196 * Handler function for "state" method call.
1198 DBusMessage * wpas_dbus_iface_get_state(DBusMessage *message,
1199 struct wpa_supplicant *wpa_s)
1201 DBusMessage *reply = NULL;
1202 const char *str_state;
1204 reply = dbus_message_new_method_return(message);
1205 if (reply != NULL) {
1206 str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
1207 dbus_message_append_args(reply, DBUS_TYPE_STRING, &str_state,
1216 * wpas_dbus_iface_get_scanning - Get interface scanning state
1217 * @message: Pointer to incoming dbus message
1218 * @wpa_s: wpa_supplicant structure for a network interface
1219 * Returns: A dbus message containing whether the interface is scanning
1221 * Handler function for "scanning" method call.
1223 DBusMessage * wpas_dbus_iface_get_scanning(DBusMessage *message,
1224 struct wpa_supplicant *wpa_s)
1226 DBusMessage *reply = NULL;
1227 dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
1229 reply = dbus_message_new_method_return(message);
1230 if (reply != NULL) {
1231 dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &scanning,
1234 wpa_printf(MSG_ERROR,
1235 "dbus: Not enough memory to return scanning state");
1242 #ifndef CONFIG_NO_CONFIG_BLOBS
1245 * wpas_dbus_iface_set_blobs - Store named binary blobs (ie, for certificates)
1246 * @message: Pointer to incoming dbus message
1247 * @wpa_s: %wpa_supplicant data structure
1248 * Returns: A dbus message containing a UINT32 indicating success (1) or
1251 * Asks wpa_supplicant to internally store a one or more binary blobs.
1253 DBusMessage * wpas_dbus_iface_set_blobs(DBusMessage *message,
1254 struct wpa_supplicant *wpa_s)
1256 DBusMessage *reply = NULL;
1257 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
1258 DBusMessageIter iter, iter_dict;
1260 dbus_message_iter_init(message, &iter);
1262 if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
1263 return wpas_dbus_new_invalid_opts_error(message, NULL);
1265 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
1266 struct wpa_config_blob *blob;
1268 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
1269 reply = wpas_dbus_new_invalid_opts_error(message,
1274 if (entry.type != DBUS_TYPE_ARRAY ||
1275 entry.array_type != DBUS_TYPE_BYTE) {
1276 reply = wpas_dbus_new_invalid_opts_error(
1277 message, "Byte array expected.");
1281 if ((entry.array_len <= 0) || (entry.array_len > 65536) ||
1282 !strlen(entry.key)) {
1283 reply = wpas_dbus_new_invalid_opts_error(
1284 message, "Invalid array size.");
1288 blob = os_zalloc(sizeof(*blob));
1290 reply = dbus_message_new_error(
1291 message, WPAS_ERROR_ADD_ERROR,
1292 "Not enough memory to add blob.");
1295 blob->data = os_zalloc(entry.array_len);
1296 if (blob->data == NULL) {
1297 reply = dbus_message_new_error(
1298 message, WPAS_ERROR_ADD_ERROR,
1299 "Not enough memory to add blob data.");
1304 blob->name = os_strdup(entry.key);
1305 blob->len = entry.array_len;
1306 os_memcpy(blob->data, (u8 *) entry.bytearray_value,
1308 if (blob->name == NULL) {
1309 wpa_config_free_blob(blob);
1310 reply = dbus_message_new_error(
1311 message, WPAS_ERROR_ADD_ERROR,
1312 "Error adding blob.");
1317 if (!wpa_config_remove_blob(wpa_s->conf, blob->name))
1318 wpas_notify_blob_removed(wpa_s, blob->name);
1319 wpa_config_set_blob(wpa_s->conf, blob);
1320 wpas_notify_blob_added(wpa_s, blob->name);
1322 wpa_dbus_dict_entry_clear(&entry);
1324 wpa_dbus_dict_entry_clear(&entry);
1326 return reply ? reply : wpas_dbus_new_success_reply(message);
1331 * wpas_dbus_iface_remove_blob - Remove named binary blobs
1332 * @message: Pointer to incoming dbus message
1333 * @wpa_s: %wpa_supplicant data structure
1334 * Returns: A dbus message containing a UINT32 indicating success (1) or
1337 * Asks wpa_supplicant to remove one or more previously stored binary blobs.
1339 DBusMessage * wpas_dbus_iface_remove_blobs(DBusMessage *message,
1340 struct wpa_supplicant *wpa_s)
1342 DBusMessageIter iter, array;
1343 char *err_msg = NULL;
1345 dbus_message_iter_init(message, &iter);
1347 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1348 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
1349 return wpas_dbus_new_invalid_opts_error(message, NULL);
1351 dbus_message_iter_recurse(&iter, &array);
1352 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
1355 dbus_message_iter_get_basic(&array, &name);
1356 if (!os_strlen(name))
1357 err_msg = "Invalid blob name.";
1358 else if (wpa_config_remove_blob(wpa_s->conf, name) != 0)
1359 err_msg = "Error removing blob.";
1361 wpas_notify_blob_removed(wpa_s, name);
1362 dbus_message_iter_next(&array);
1366 return dbus_message_new_error(message, WPAS_ERROR_REMOVE_ERROR,
1369 return wpas_dbus_new_success_reply(message);
1372 #endif /* CONFIG_NO_CONFIG_BLOBS */
1376 * wpas_dbus_iface_flush - Clear BSS of old or all inactive entries
1377 * @message: Pointer to incoming dbus message
1378 * @wpa_s: %wpa_supplicant data structure
1379 * Returns: a dbus message containing a UINT32 indicating success (1) or
1380 * failure (0), or returns a dbus error message with more information
1382 * Handler function for "flush" method call. Handles requests for an
1383 * interface with an optional "age" parameter that specifies the minimum
1384 * age of a BSS to be flushed.
1386 DBusMessage * wpas_dbus_iface_flush(DBusMessage *message,
1387 struct wpa_supplicant *wpa_s)
1391 if (os_strlen(dbus_message_get_signature(message)) != 0 &&
1392 !dbus_message_get_args(message, NULL,
1393 DBUS_TYPE_INT32, &flush_age,
1394 DBUS_TYPE_INVALID)) {
1395 return wpas_dbus_new_invalid_opts_error(message, NULL);
1399 wpa_bss_flush(wpa_s);
1401 wpa_bss_flush_by_age(wpa_s, flush_age);
1403 return wpas_dbus_new_success_reply(message);