2 * WPA Supplicant / dbus-based control interface
3 * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
16 #include <dbus/dbus.h>
19 #include "eap_peer/eap_methods.h"
20 #include "common/ieee802_11_defs.h"
21 #include "eapol_supp/eapol_supp_sm.h"
22 #include "rsn_supp/wpa.h"
23 #include "../config.h"
24 #include "../wpa_supplicant_i.h"
25 #include "../driver_i.h"
26 #include "../notify.h"
27 #include "../wpas_glue.h"
29 #include "dbus_old_handlers.h"
30 #include "dbus_dict_helpers.h"
32 extern int wpa_debug_level;
33 extern int wpa_debug_show_keys;
34 extern int wpa_debug_timestamp;
37 * wpas_dbus_new_invalid_opts_error - Return a new invalid options error message
38 * @message: Pointer to incoming dbus message this error refers to
39 * Returns: a dbus error message
41 * Convenience function to create and return an invalid options error
43 DBusMessage * wpas_dbus_new_invalid_opts_error(DBusMessage *message,
48 reply = dbus_message_new_error(message, WPAS_ERROR_INVALID_OPTS,
49 "Did not receive correct message "
52 dbus_message_append_args(reply, DBUS_TYPE_STRING, &arg,
60 * wpas_dbus_new_success_reply - Return a new success reply message
61 * @message: Pointer to incoming dbus message this reply refers to
62 * Returns: a dbus message containing a single UINT32 that indicates
63 * success (ie, a value of 1)
65 * Convenience function to create and return a success reply message
67 DBusMessage * wpas_dbus_new_success_reply(DBusMessage *message)
70 unsigned int success = 1;
72 reply = dbus_message_new_method_return(message);
73 dbus_message_append_args(reply, DBUS_TYPE_UINT32, &success,
80 * wpas_dbus_global_add_interface - Request registration of a network interface
81 * @message: Pointer to incoming dbus message
82 * @global: %wpa_supplicant global data structure
83 * Returns: The object path of the new interface object,
84 * or a dbus error message with more information
86 * Handler function for "addInterface" method call. Handles requests
87 * by dbus clients to register a network interface that wpa_supplicant
90 DBusMessage * wpas_dbus_global_add_interface(DBusMessage *message,
91 struct wpa_global *global)
95 char *driver_param = NULL;
96 char *confname = NULL;
97 char *bridge_ifname = NULL;
98 DBusMessage *reply = NULL;
101 dbus_message_iter_init(message, &iter);
103 /* First argument: interface name (DBUS_TYPE_STRING)
104 * Required; must be non-zero length
106 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
108 dbus_message_iter_get_basic(&iter, &ifname);
109 if (!os_strlen(ifname))
112 /* Second argument: dict of options */
113 if (dbus_message_iter_next(&iter)) {
114 DBusMessageIter iter_dict;
115 struct wpa_dbus_dict_entry entry;
117 if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
119 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
120 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
122 if (!strcmp(entry.key, "driver") &&
123 (entry.type == DBUS_TYPE_STRING)) {
124 driver = os_strdup(entry.str_value);
125 wpa_dbus_dict_entry_clear(&entry);
128 } else if (!strcmp(entry.key, "driver-params") &&
129 (entry.type == DBUS_TYPE_STRING)) {
130 driver_param = os_strdup(entry.str_value);
131 wpa_dbus_dict_entry_clear(&entry);
132 if (driver_param == NULL)
134 } else if (!strcmp(entry.key, "config-file") &&
135 (entry.type == DBUS_TYPE_STRING)) {
136 confname = os_strdup(entry.str_value);
137 wpa_dbus_dict_entry_clear(&entry);
138 if (confname == NULL)
140 } else if (!strcmp(entry.key, "bridge-ifname") &&
141 (entry.type == DBUS_TYPE_STRING)) {
142 bridge_ifname = os_strdup(entry.str_value);
143 wpa_dbus_dict_entry_clear(&entry);
144 if (bridge_ifname == NULL)
147 wpa_dbus_dict_entry_clear(&entry);
154 * Try to get the wpa_supplicant record for this iface, return
155 * an error if we already control it.
157 if (wpa_supplicant_get_iface(global, ifname) != NULL) {
158 reply = dbus_message_new_error(message,
159 WPAS_ERROR_EXISTS_ERROR,
160 "wpa_supplicant already "
161 "controls this interface.");
163 struct wpa_supplicant *wpa_s;
164 struct wpa_interface iface;
165 os_memset(&iface, 0, sizeof(iface));
166 iface.ifname = ifname;
167 iface.driver = driver;
168 iface.driver_param = driver_param;
169 iface.confname = confname;
170 iface.bridge_ifname = bridge_ifname;
171 /* Otherwise, have wpa_supplicant attach to it. */
172 if ((wpa_s = wpa_supplicant_add_iface(global, &iface))) {
173 const char *path = wpa_s->dbus_path;
174 reply = dbus_message_new_method_return(message);
175 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
176 &path, DBUS_TYPE_INVALID);
178 reply = dbus_message_new_error(message,
179 WPAS_ERROR_ADD_ERROR,
181 "couldn't grab this "
188 os_free(driver_param);
190 os_free(bridge_ifname);
194 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
200 * wpas_dbus_global_remove_interface - Request deregistration of an interface
201 * @message: Pointer to incoming dbus message
202 * @global: wpa_supplicant global data structure
203 * Returns: a dbus message containing a UINT32 indicating success (1) or
204 * failure (0), or returns a dbus error message with more information
206 * Handler function for "removeInterface" method call. Handles requests
207 * by dbus clients to deregister a network interface that wpa_supplicant
210 DBusMessage * wpas_dbus_global_remove_interface(DBusMessage *message,
211 struct wpa_global *global)
213 struct wpa_supplicant *wpa_s;
215 DBusMessage *reply = NULL;
217 if (!dbus_message_get_args(message, NULL,
218 DBUS_TYPE_OBJECT_PATH, &path,
219 DBUS_TYPE_INVALID)) {
220 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
224 wpa_s = wpa_supplicant_get_iface_by_dbus_path(global, path);
226 reply = wpas_dbus_new_invalid_iface_error(message);
230 if (!wpa_supplicant_remove_iface(global, wpa_s)) {
231 reply = wpas_dbus_new_success_reply(message);
233 reply = dbus_message_new_error(message,
234 WPAS_ERROR_REMOVE_ERROR,
235 "wpa_supplicant couldn't "
236 "remove this interface.");
245 * wpas_dbus_global_get_interface - Get the object path for an interface name
246 * @message: Pointer to incoming dbus message
247 * @global: %wpa_supplicant global data structure
248 * Returns: The object path of the interface object,
249 * or a dbus error message with more information
251 * Handler function for "getInterface" method call. Handles requests
252 * by dbus clients for the object path of an specific network interface.
254 DBusMessage * wpas_dbus_global_get_interface(DBusMessage *message,
255 struct wpa_global *global)
257 DBusMessage *reply = NULL;
260 struct wpa_supplicant *wpa_s;
262 if (!dbus_message_get_args(message, NULL,
263 DBUS_TYPE_STRING, &ifname,
264 DBUS_TYPE_INVALID)) {
265 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
269 wpa_s = wpa_supplicant_get_iface(global, ifname);
271 reply = wpas_dbus_new_invalid_iface_error(message);
275 path = wpa_s->dbus_path;
276 reply = dbus_message_new_method_return(message);
277 dbus_message_append_args(reply,
278 DBUS_TYPE_OBJECT_PATH, &path,
287 * wpas_dbus_global_set_debugparams- Set the debug params
288 * @message: Pointer to incoming dbus message
289 * @global: %wpa_supplicant global data structure
290 * Returns: a dbus message containing a UINT32 indicating success (1) or
291 * failure (0), or returns a dbus error message with more information
293 * Handler function for "setDebugParams" method call. Handles requests
294 * by dbus clients for the object path of an specific network interface.
296 DBusMessage * wpas_dbus_global_set_debugparams(DBusMessage *message,
297 struct wpa_global *global)
299 DBusMessage *reply = NULL;
301 dbus_bool_t debug_timestamp;
302 dbus_bool_t debug_show_keys;
304 if (!dbus_message_get_args(message, NULL,
305 DBUS_TYPE_INT32, &debug_level,
306 DBUS_TYPE_BOOLEAN, &debug_timestamp,
307 DBUS_TYPE_BOOLEAN, &debug_show_keys,
308 DBUS_TYPE_INVALID)) {
309 return wpas_dbus_new_invalid_opts_error(message, NULL);
312 if (wpa_supplicant_set_debug_params(global, debug_level,
313 debug_timestamp ? 1 : 0,
314 debug_show_keys ? 1 : 0)) {
315 return wpas_dbus_new_invalid_opts_error(message, NULL);
318 reply = wpas_dbus_new_success_reply(message);
325 * wpas_dbus_iface_scan - Request a wireless scan on an interface
326 * @message: Pointer to incoming dbus message
327 * @wpa_s: wpa_supplicant structure for a network interface
328 * Returns: a dbus message containing a UINT32 indicating success (1) or
331 * Handler function for "scan" method call of a network device. Requests
332 * that wpa_supplicant perform a wireless scan as soon as possible
333 * on a particular wireless interface.
335 DBusMessage * wpas_dbus_iface_scan(DBusMessage *message,
336 struct wpa_supplicant *wpa_s)
339 wpa_supplicant_req_scan(wpa_s, 0, 0);
340 return wpas_dbus_new_success_reply(message);
345 * wpas_dbus_iface_scan_results - Get the results of a recent scan request
346 * @message: Pointer to incoming dbus message
347 * @wpa_s: wpa_supplicant structure for a network interface
348 * Returns: a dbus message containing a dbus array of objects paths, or returns
349 * a dbus error message if not scan results could be found
351 * Handler function for "scanResults" method call of a network device. Returns
352 * a dbus message containing the object paths of wireless networks found.
354 DBusMessage * wpas_dbus_iface_scan_results(DBusMessage *message,
355 struct wpa_supplicant *wpa_s)
357 DBusMessage *reply = NULL;
358 DBusMessageIter iter;
359 DBusMessageIter sub_iter;
362 /* Ensure we've actually got scan results to return */
363 if (wpa_s->scan_res == NULL &&
364 wpa_supplicant_get_scan_results(wpa_s, NULL, 0) < 0) {
365 return dbus_message_new_error(message, WPAS_ERROR_SCAN_ERROR,
366 "An error ocurred getting scan "
370 /* Create and initialize the return message */
371 reply = dbus_message_new_method_return(message);
372 dbus_message_iter_init_append(reply, &iter);
373 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
374 DBUS_TYPE_OBJECT_PATH_AS_STRING,
377 /* Loop through scan results and append each result's object path */
378 for (i = 0; i < wpa_s->scan_res->num; i++) {
379 struct wpa_scan_res *res = wpa_s->scan_res->res[i];
382 path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
384 wpa_printf(MSG_ERROR, "dbus: Not enough memory to "
385 "send scan results signal");
388 /* Construct the object path for this network. Note that ':'
389 * is not a valid character in dbus object paths.
391 os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
392 "%s/" WPAS_DBUS_BSSIDS_PART "/"
393 WPAS_DBUS_BSSID_FORMAT,
394 wpa_s->dbus_path, MAC2STR(res->bssid));
395 dbus_message_iter_append_basic(&sub_iter,
396 DBUS_TYPE_OBJECT_PATH, &path);
400 dbus_message_iter_close_container(&iter, &sub_iter);
407 * wpas_dbus_bssid_properties - Return the properties of a scanned network
408 * @message: Pointer to incoming dbus message
409 * @wpa_s: wpa_supplicant structure for a network interface
410 * @res: wpa_supplicant scan result for which to get properties
411 * Returns: a dbus message containing the properties for the requested network
413 * Handler function for "properties" method call of a scanned network.
414 * Returns a dbus message containing the the properties.
416 DBusMessage * wpas_dbus_bssid_properties(DBusMessage *message,
417 struct wpa_supplicant *wpa_s,
418 struct wpa_scan_res *res)
420 DBusMessage *reply = NULL;
421 DBusMessageIter iter, iter_dict;
424 /* Dump the properties into a dbus message */
425 reply = dbus_message_new_method_return(message);
427 dbus_message_iter_init_append(reply, &iter);
428 if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
431 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "bssid",
432 (const char *) res->bssid,
436 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
438 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "ssid",
439 (const char *) (ie + 2),
444 ie = wpa_scan_get_vendor_ie(res, WPA_IE_VENDOR_TYPE);
446 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "wpaie",
452 ie = wpa_scan_get_ie(res, WLAN_EID_RSN);
454 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "rsnie",
460 ie = wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE);
462 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "wpsie",
469 if (!wpa_dbus_dict_append_int32(&iter_dict, "frequency",
473 if (!wpa_dbus_dict_append_uint16(&iter_dict, "capabilities",
476 if (!(res->flags & WPA_SCAN_QUAL_INVALID) &&
477 !wpa_dbus_dict_append_int32(&iter_dict, "quality", res->qual))
479 if (!(res->flags & WPA_SCAN_NOISE_INVALID) &&
480 !wpa_dbus_dict_append_int32(&iter_dict, "noise", res->noise))
482 if (!(res->flags & WPA_SCAN_LEVEL_INVALID) &&
483 !wpa_dbus_dict_append_int32(&iter_dict, "level", res->level))
485 if (!wpa_dbus_dict_append_int32(&iter_dict, "maxrate",
486 wpa_scan_get_max_rate(res) * 500000))
489 if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
496 dbus_message_unref(reply);
497 return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
498 "an internal error occurred returning "
499 "BSSID properties.");
504 * wpas_dbus_iface_capabilities - Return interface capabilities
505 * @message: Pointer to incoming dbus message
506 * @wpa_s: wpa_supplicant structure for a network interface
507 * Returns: A dbus message containing a dict of strings
509 * Handler function for "capabilities" method call of an interface.
511 DBusMessage * wpas_dbus_iface_capabilities(DBusMessage *message,
512 struct wpa_supplicant *wpa_s)
514 DBusMessage *reply = NULL;
515 struct wpa_driver_capa capa;
517 DBusMessageIter iter, iter_dict;
520 dbus_bool_t strict = FALSE;
521 DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
523 if (!dbus_message_get_args(message, NULL,
524 DBUS_TYPE_BOOLEAN, &strict,
528 reply = dbus_message_new_method_return(message);
530 dbus_message_iter_init_append(reply, &iter);
531 if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
535 eap_methods = eap_get_names_as_string_array(&num_items);
537 dbus_bool_t success = FALSE;
540 success = wpa_dbus_dict_append_string_array(
541 &iter_dict, "eap", (const char **) eap_methods,
544 /* free returned method array */
545 while (eap_methods[i])
546 os_free(eap_methods[i++]);
547 os_free(eap_methods);
553 res = wpa_drv_get_capa(wpa_s, &capa);
555 /***** pairwise cipher */
558 const char *args[] = {"CCMP", "TKIP", "NONE"};
559 if (!wpa_dbus_dict_append_string_array(
560 &iter_dict, "pairwise", args,
561 sizeof(args) / sizeof(char*)))
565 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "pairwise",
571 if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
572 if (!wpa_dbus_dict_string_array_add_element(
573 &iter_array, "CCMP"))
577 if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
578 if (!wpa_dbus_dict_string_array_add_element(
579 &iter_array, "TKIP"))
583 if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
584 if (!wpa_dbus_dict_string_array_add_element(
585 &iter_array, "NONE"))
589 if (!wpa_dbus_dict_end_string_array(&iter_dict,
596 /***** group cipher */
599 const char *args[] = {
600 "CCMP", "TKIP", "WEP104", "WEP40"
602 if (!wpa_dbus_dict_append_string_array(
603 &iter_dict, "group", args,
604 sizeof(args) / sizeof(char*)))
608 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "group",
614 if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
615 if (!wpa_dbus_dict_string_array_add_element(
616 &iter_array, "CCMP"))
620 if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
621 if (!wpa_dbus_dict_string_array_add_element(
622 &iter_array, "TKIP"))
626 if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) {
627 if (!wpa_dbus_dict_string_array_add_element(
628 &iter_array, "WEP104"))
632 if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) {
633 if (!wpa_dbus_dict_string_array_add_element(
634 &iter_array, "WEP40"))
638 if (!wpa_dbus_dict_end_string_array(&iter_dict,
645 /***** key management */
648 const char *args[] = {
649 "WPA-PSK", "WPA-EAP", "IEEE8021X", "WPA-NONE",
652 if (!wpa_dbus_dict_append_string_array(
653 &iter_dict, "key_mgmt", args,
654 sizeof(args) / sizeof(char*)))
658 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "key_mgmt",
664 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
668 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
672 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
673 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
674 if (!wpa_dbus_dict_string_array_add_element(
675 &iter_array, "WPA-EAP"))
679 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
680 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
681 if (!wpa_dbus_dict_string_array_add_element(
682 &iter_array, "WPA-PSK"))
686 if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
687 if (!wpa_dbus_dict_string_array_add_element(
688 &iter_array, "WPA-NONE"))
692 if (!wpa_dbus_dict_end_string_array(&iter_dict,
699 /***** WPA protocol */
702 const char *args[] = { "RSN", "WPA" };
703 if (!wpa_dbus_dict_append_string_array(
704 &iter_dict, "proto", args,
705 sizeof(args) / sizeof(char*)))
709 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "proto",
715 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
716 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
717 if (!wpa_dbus_dict_string_array_add_element(
722 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
723 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
724 if (!wpa_dbus_dict_string_array_add_element(
729 if (!wpa_dbus_dict_end_string_array(&iter_dict,
739 const char *args[] = { "OPEN", "SHARED", "LEAP" };
740 if (!wpa_dbus_dict_append_string_array(
741 &iter_dict, "auth_alg", args,
742 sizeof(args) / sizeof(char*)))
746 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "auth_alg",
752 if (capa.auth & (WPA_DRIVER_AUTH_OPEN)) {
753 if (!wpa_dbus_dict_string_array_add_element(
754 &iter_array, "OPEN"))
758 if (capa.auth & (WPA_DRIVER_AUTH_SHARED)) {
759 if (!wpa_dbus_dict_string_array_add_element(
760 &iter_array, "SHARED"))
764 if (capa.auth & (WPA_DRIVER_AUTH_LEAP)) {
765 if (!wpa_dbus_dict_string_array_add_element(
766 &iter_array, "LEAP"))
770 if (!wpa_dbus_dict_end_string_array(&iter_dict,
777 if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
784 dbus_message_unref(reply);
785 return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
786 "an internal error occurred returning "
787 "interface capabilities.");
792 * wpas_dbus_iface_add_network - Add a new configured network
793 * @message: Pointer to incoming dbus message
794 * @wpa_s: wpa_supplicant structure for a network interface
795 * Returns: A dbus message containing the object path of the new network
797 * Handler function for "addNetwork" method call of a network interface.
799 DBusMessage * wpas_dbus_iface_add_network(DBusMessage *message,
800 struct wpa_supplicant *wpa_s)
802 DBusMessage *reply = NULL;
803 struct wpa_ssid *ssid;
806 path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
808 wpa_printf(MSG_ERROR, "dbus: Not enough memory to send scan "
813 ssid = wpa_config_add_network(wpa_s->conf);
815 reply = dbus_message_new_error(message,
816 WPAS_ERROR_ADD_NETWORK_ERROR,
817 "wpa_supplicant could not add "
818 "a network on this interface.");
821 wpas_notify_network_added(wpa_s, ssid);
823 wpa_config_set_network_defaults(ssid);
825 /* Construct the object path for this network. */
826 os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
827 "%s/" WPAS_DBUS_NETWORKS_PART "/%d",
828 wpa_s->dbus_path, ssid->id);
830 reply = dbus_message_new_method_return(message);
831 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
832 &path, DBUS_TYPE_INVALID);
841 * wpas_dbus_iface_remove_network - Remove a configured network
842 * @message: Pointer to incoming dbus message
843 * @wpa_s: wpa_supplicant structure for a network interface
844 * Returns: A dbus message containing a UINT32 indicating success (1) or
847 * Handler function for "removeNetwork" method call of a network interface.
849 DBusMessage * wpas_dbus_iface_remove_network(DBusMessage *message,
850 struct wpa_supplicant *wpa_s)
852 DBusMessage *reply = NULL;
854 char *iface = NULL, *net_id = NULL;
856 struct wpa_ssid *ssid;
858 if (!dbus_message_get_args(message, NULL,
859 DBUS_TYPE_OBJECT_PATH, &op,
860 DBUS_TYPE_INVALID)) {
861 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
865 /* Extract the network ID */
866 iface = wpas_dbus_decompose_object_path(op, &net_id, NULL);
868 reply = wpas_dbus_new_invalid_network_error(message);
872 /* Ensure the network is actually a child of this interface */
873 if (os_strcmp(iface, wpa_s->dbus_path) != 0) {
874 reply = wpas_dbus_new_invalid_network_error(message);
878 id = strtoul(net_id, NULL, 10);
879 ssid = wpa_config_get_network(wpa_s->conf, id);
881 reply = wpas_dbus_new_invalid_network_error(message);
885 wpas_notify_network_removed(wpa_s, ssid);
887 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
888 reply = dbus_message_new_error(message,
889 WPAS_ERROR_REMOVE_NETWORK_ERROR,
890 "error removing the specified "
891 "on this interface.");
895 if (ssid == wpa_s->current_ssid)
896 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
897 reply = wpas_dbus_new_success_reply(message);
906 static const char *dont_quote[] = {
907 "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
908 "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
913 static dbus_bool_t should_quote_opt(const char *key)
916 while (dont_quote[i] != NULL) {
917 if (strcmp(key, dont_quote[i]) == 0)
926 * wpas_dbus_iface_set_network - Set options for a configured network
927 * @message: Pointer to incoming dbus message
928 * @wpa_s: wpa_supplicant structure for a network interface
929 * @ssid: wpa_ssid structure for a configured network
930 * Returns: a dbus message containing a UINT32 indicating success (1) or
933 * Handler function for "set" method call of a configured network.
935 DBusMessage * wpas_dbus_iface_set_network(DBusMessage *message,
936 struct wpa_supplicant *wpa_s,
937 struct wpa_ssid *ssid)
939 DBusMessage *reply = NULL;
940 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
941 DBusMessageIter iter, iter_dict;
943 dbus_message_iter_init(message, &iter);
945 if (!wpa_dbus_dict_open_read(&iter, &iter_dict)) {
946 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
950 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
955 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
956 reply = wpas_dbus_new_invalid_opts_error(message,
961 /* Type conversions, since wpa_supplicant wants strings */
962 if (entry.type == DBUS_TYPE_ARRAY &&
963 entry.array_type == DBUS_TYPE_BYTE) {
964 if (entry.array_len <= 0)
967 size = entry.array_len * 2 + 1;
968 value = os_zalloc(size);
971 ret = wpa_snprintf_hex(value, size,
972 (u8 *) entry.bytearray_value,
976 } else if (entry.type == DBUS_TYPE_STRING) {
977 if (should_quote_opt(entry.key)) {
978 size = os_strlen(entry.str_value);
979 /* Zero-length option check */
982 size += 3; /* For quotes and terminator */
983 value = os_zalloc(size);
986 ret = os_snprintf(value, size, "\"%s\"",
988 if (ret < 0 || (size_t) ret != (size - 1))
991 value = os_strdup(entry.str_value);
995 } else if (entry.type == DBUS_TYPE_UINT32) {
996 value = os_zalloc(size);
999 ret = os_snprintf(value, size, "%u",
1000 entry.uint32_value);
1003 } else if (entry.type == DBUS_TYPE_INT32) {
1004 value = os_zalloc(size);
1007 ret = os_snprintf(value, size, "%d",
1014 if (wpa_config_set(ssid, entry.key, value, 0) < 0)
1017 if ((os_strcmp(entry.key, "psk") == 0 &&
1018 value[0] == '"' && ssid->ssid_len) ||
1019 (os_strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
1020 wpa_config_update_psk(ssid);
1023 wpa_dbus_dict_entry_clear(&entry);
1028 reply = wpas_dbus_new_invalid_opts_error(message, entry.key);
1029 wpa_dbus_dict_entry_clear(&entry);
1034 reply = wpas_dbus_new_success_reply(message);
1042 * wpas_dbus_iface_enable_network - Mark a configured network as enabled
1043 * @message: Pointer to incoming dbus message
1044 * @wpa_s: wpa_supplicant structure for a network interface
1045 * @ssid: wpa_ssid structure for a configured network
1046 * Returns: A dbus message containing a UINT32 indicating success (1) or
1049 * Handler function for "enable" method call of a configured network.
1051 DBusMessage * wpas_dbus_iface_enable_network(DBusMessage *message,
1052 struct wpa_supplicant *wpa_s,
1053 struct wpa_ssid *ssid)
1055 wpa_supplicant_enable_network(wpa_s, ssid);
1056 return wpas_dbus_new_success_reply(message);
1061 * wpas_dbus_iface_disable_network - Mark a configured network as disabled
1062 * @message: Pointer to incoming dbus message
1063 * @wpa_s: wpa_supplicant structure for a network interface
1064 * @ssid: wpa_ssid structure for a configured network
1065 * Returns: A dbus message containing a UINT32 indicating success (1) or
1068 * Handler function for "disable" method call of a configured network.
1070 DBusMessage * wpas_dbus_iface_disable_network(DBusMessage *message,
1071 struct wpa_supplicant *wpa_s,
1072 struct wpa_ssid *ssid)
1074 wpa_supplicant_disable_network(wpa_s, ssid);
1075 return wpas_dbus_new_success_reply(message);
1080 * wpas_dbus_iface_select_network - Attempt association with a configured network
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 "selectNetwork" method call of network interface.
1088 DBusMessage * wpas_dbus_iface_select_network(DBusMessage *message,
1089 struct wpa_supplicant *wpa_s)
1091 DBusMessage *reply = NULL;
1093 struct wpa_ssid *ssid;
1094 char *iface_obj_path = NULL;
1095 char *network = NULL;
1097 if (os_strlen(dbus_message_get_signature(message)) == 0) {
1103 if (!dbus_message_get_args(message, NULL,
1104 DBUS_TYPE_OBJECT_PATH, &op,
1105 DBUS_TYPE_INVALID)) {
1106 reply = wpas_dbus_new_invalid_opts_error(message,
1111 /* Extract the network number */
1112 iface_obj_path = wpas_dbus_decompose_object_path(op,
1115 if (iface_obj_path == NULL) {
1116 reply = wpas_dbus_new_invalid_iface_error(message);
1119 /* Ensure the object path really points to this interface */
1120 if (os_strcmp(iface_obj_path, wpa_s->dbus_path) != 0) {
1121 reply = wpas_dbus_new_invalid_network_error(message);
1125 nid = strtoul(network, NULL, 10);
1126 if (errno == EINVAL) {
1127 reply = wpas_dbus_new_invalid_network_error(message);
1131 ssid = wpa_config_get_network(wpa_s->conf, nid);
1133 reply = wpas_dbus_new_invalid_network_error(message);
1138 /* Finally, associate with the network */
1139 wpa_supplicant_select_network(wpa_s, ssid);
1141 reply = wpas_dbus_new_success_reply(message);
1144 os_free(iface_obj_path);
1151 * wpas_dbus_iface_disconnect - Terminate the current connection
1152 * @message: Pointer to incoming dbus message
1153 * @wpa_s: wpa_supplicant structure for a network interface
1154 * Returns: A dbus message containing a UINT32 indicating success (1) or
1157 * Handler function for "disconnect" method call of network interface.
1159 DBusMessage * wpas_dbus_iface_disconnect(DBusMessage *message,
1160 struct wpa_supplicant *wpa_s)
1162 wpa_s->disconnected = 1;
1163 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1165 return wpas_dbus_new_success_reply(message);
1170 * wpas_dbus_iface_set_ap_scan - Control roaming mode
1171 * @message: Pointer to incoming dbus message
1172 * @wpa_s: wpa_supplicant structure for a network interface
1173 * Returns: A dbus message containing a UINT32 indicating success (1) or
1176 * Handler function for "setAPScan" method call.
1178 DBusMessage * wpas_dbus_iface_set_ap_scan(DBusMessage *message,
1179 struct wpa_supplicant *wpa_s)
1181 DBusMessage *reply = NULL;
1182 dbus_uint32_t ap_scan = 1;
1184 if (!dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &ap_scan,
1185 DBUS_TYPE_INVALID)) {
1186 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
1190 if (wpa_supplicant_set_ap_scan(wpa_s, ap_scan)) {
1191 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
1195 reply = wpas_dbus_new_success_reply(message);
1203 * wpas_dbus_iface_set_smartcard_modules - Set smartcard related module paths
1204 * @message: Pointer to incoming dbus message
1205 * @wpa_s: wpa_supplicant structure for a network interface
1206 * Returns: A dbus message containing a UINT32 indicating success (1) or
1209 * Handler function for "setSmartcardModules" method call.
1211 DBusMessage * wpas_dbus_iface_set_smartcard_modules(
1212 DBusMessage *message, struct wpa_supplicant *wpa_s)
1214 DBusMessageIter iter, iter_dict;
1215 char *opensc_engine_path = NULL;
1216 char *pkcs11_engine_path = NULL;
1217 char *pkcs11_module_path = NULL;
1218 struct wpa_dbus_dict_entry entry;
1220 if (!dbus_message_iter_init(message, &iter))
1223 if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
1226 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
1227 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
1229 if (!strcmp(entry.key, "opensc_engine_path") &&
1230 (entry.type == DBUS_TYPE_STRING)) {
1231 opensc_engine_path = os_strdup(entry.str_value);
1232 if (opensc_engine_path == NULL)
1234 } else if (!strcmp(entry.key, "pkcs11_engine_path") &&
1235 (entry.type == DBUS_TYPE_STRING)) {
1236 pkcs11_engine_path = os_strdup(entry.str_value);
1237 if (pkcs11_engine_path == NULL)
1239 } else if (!strcmp(entry.key, "pkcs11_module_path") &&
1240 (entry.type == DBUS_TYPE_STRING)) {
1241 pkcs11_module_path = os_strdup(entry.str_value);
1242 if (pkcs11_module_path == NULL)
1245 wpa_dbus_dict_entry_clear(&entry);
1248 wpa_dbus_dict_entry_clear(&entry);
1251 os_free(wpa_s->conf->opensc_engine_path);
1252 wpa_s->conf->opensc_engine_path = opensc_engine_path;
1253 os_free(wpa_s->conf->pkcs11_engine_path);
1254 wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path;
1255 os_free(wpa_s->conf->pkcs11_module_path);
1256 wpa_s->conf->pkcs11_module_path = pkcs11_module_path;
1258 wpa_sm_set_eapol(wpa_s->wpa, NULL);
1259 eapol_sm_deinit(wpa_s->eapol);
1260 wpa_s->eapol = NULL;
1261 wpa_supplicant_init_eapol(wpa_s);
1262 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
1264 return wpas_dbus_new_success_reply(message);
1267 os_free(opensc_engine_path);
1268 os_free(pkcs11_engine_path);
1269 os_free(pkcs11_module_path);
1270 return wpas_dbus_new_invalid_opts_error(message, NULL);
1275 * wpas_dbus_iface_get_state - Get interface state
1276 * @message: Pointer to incoming dbus message
1277 * @wpa_s: wpa_supplicant structure for a network interface
1278 * Returns: A dbus message containing a STRING representing the current
1281 * Handler function for "state" method call.
1283 DBusMessage * wpas_dbus_iface_get_state(DBusMessage *message,
1284 struct wpa_supplicant *wpa_s)
1286 DBusMessage *reply = NULL;
1287 const char *str_state;
1289 reply = dbus_message_new_method_return(message);
1290 if (reply != NULL) {
1291 str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
1292 dbus_message_append_args(reply, DBUS_TYPE_STRING, &str_state,
1301 * wpas_dbus_iface_get_scanning - Get interface scanning state
1302 * @message: Pointer to incoming dbus message
1303 * @wpa_s: wpa_supplicant structure for a network interface
1304 * Returns: A dbus message containing whether the interface is scanning
1306 * Handler function for "scanning" method call.
1308 DBusMessage * wpas_dbus_iface_get_scanning(DBusMessage *message,
1309 struct wpa_supplicant *wpa_s)
1311 DBusMessage *reply = NULL;
1312 dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
1314 reply = dbus_message_new_method_return(message);
1315 if (reply != NULL) {
1316 dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &scanning,
1319 wpa_printf(MSG_ERROR, "dbus: Not enough memory to return "
1328 * wpas_dbus_iface_set_blobs - Store named binary blobs (ie, for certificates)
1329 * @message: Pointer to incoming dbus message
1330 * @wpa_s: %wpa_supplicant data structure
1331 * Returns: A dbus message containing a UINT32 indicating success (1) or
1334 * Asks wpa_supplicant to internally store a one or more binary blobs.
1336 DBusMessage * wpas_dbus_iface_set_blobs(DBusMessage *message,
1337 struct wpa_supplicant *wpa_s)
1339 DBusMessage *reply = NULL;
1340 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
1341 DBusMessageIter iter, iter_dict;
1343 dbus_message_iter_init(message, &iter);
1345 if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
1346 return wpas_dbus_new_invalid_opts_error(message, NULL);
1348 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
1349 struct wpa_config_blob *blob;
1351 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
1352 reply = wpas_dbus_new_invalid_opts_error(message,
1357 if (entry.type != DBUS_TYPE_ARRAY ||
1358 entry.array_type != DBUS_TYPE_BYTE) {
1359 reply = wpas_dbus_new_invalid_opts_error(
1360 message, "Byte array expected.");
1364 if ((entry.array_len <= 0) || (entry.array_len > 65536) ||
1365 !strlen(entry.key)) {
1366 reply = wpas_dbus_new_invalid_opts_error(
1367 message, "Invalid array size.");
1371 blob = os_zalloc(sizeof(*blob));
1373 reply = dbus_message_new_error(
1374 message, WPAS_ERROR_ADD_ERROR,
1375 "Not enough memory to add blob.");
1378 blob->data = os_zalloc(entry.array_len);
1379 if (blob->data == NULL) {
1380 reply = dbus_message_new_error(
1381 message, WPAS_ERROR_ADD_ERROR,
1382 "Not enough memory to add blob data.");
1387 blob->name = os_strdup(entry.key);
1388 blob->len = entry.array_len;
1389 os_memcpy(blob->data, (u8 *) entry.bytearray_value,
1391 if (blob->name == NULL || blob->data == NULL) {
1392 wpa_config_free_blob(blob);
1393 reply = dbus_message_new_error(
1394 message, WPAS_ERROR_ADD_ERROR,
1395 "Error adding blob.");
1400 if (!wpa_config_remove_blob(wpa_s->conf, blob->name))
1401 wpas_notify_blob_removed(wpa_s, blob->name);
1402 wpa_config_set_blob(wpa_s->conf, blob);
1403 wpas_notify_blob_added(wpa_s, blob->name);
1405 wpa_dbus_dict_entry_clear(&entry);
1407 wpa_dbus_dict_entry_clear(&entry);
1409 return reply ? reply : wpas_dbus_new_success_reply(message);
1414 * wpas_dbus_iface_remove_blob - Remove named binary blobs
1415 * @message: Pointer to incoming dbus message
1416 * @wpa_s: %wpa_supplicant data structure
1417 * Returns: A dbus message containing a UINT32 indicating success (1) or
1420 * Asks wpa_supplicant to remove one or more previously stored binary blobs.
1422 DBusMessage * wpas_dbus_iface_remove_blobs(DBusMessage *message,
1423 struct wpa_supplicant *wpa_s)
1425 DBusMessageIter iter, array;
1426 char *err_msg = NULL;
1428 dbus_message_iter_init(message, &iter);
1430 if ((dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY) ||
1431 (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_STRING))
1432 return wpas_dbus_new_invalid_opts_error(message, NULL);
1434 dbus_message_iter_recurse(&iter, &array);
1435 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
1438 dbus_message_iter_get_basic(&array, &name);
1439 if (!os_strlen(name))
1440 err_msg = "Invalid blob name.";
1442 if (wpa_config_remove_blob(wpa_s->conf, name) != 0)
1443 err_msg = "Error removing blob.";
1445 wpas_notify_blob_removed(wpa_s, name);
1446 dbus_message_iter_next(&array);
1450 return dbus_message_new_error(message, WPAS_ERROR_REMOVE_ERROR,
1453 return wpas_dbus_new_success_reply(message);