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.
18 #include "eap_peer/eap_methods.h"
19 #include "common/ieee802_11_defs.h"
20 #include "eapol_supp/eapol_supp_sm.h"
21 #include "rsn_supp/wpa.h"
22 #include "../config.h"
23 #include "../wpa_supplicant_i.h"
24 #include "../driver_i.h"
25 #include "../notify.h"
26 #include "../wpas_glue.h"
28 #include "dbus_handlers.h"
29 #include "dbus_dict_helpers.h"
31 extern int wpa_debug_level
;
32 extern int wpa_debug_show_keys
;
33 extern int wpa_debug_timestamp
;
36 * wpas_dbus_new_invalid_opts_error - Return a new invalid options error message
37 * @message: Pointer to incoming dbus message this error refers to
38 * Returns: a dbus error message
40 * Convenience function to create and return an invalid options error
42 DBusMessage
* wpas_dbus_new_invalid_opts_error(DBusMessage
*message
,
47 reply
= dbus_message_new_error(message
, WPAS_ERROR_INVALID_OPTS
,
48 "Did not receive correct message "
51 dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &arg
,
59 * wpas_dbus_new_success_reply - Return a new success reply message
60 * @message: Pointer to incoming dbus message this reply refers to
61 * Returns: a dbus message containing a single UINT32 that indicates
62 * success (ie, a value of 1)
64 * Convenience function to create and return a success reply message
66 DBusMessage
* wpas_dbus_new_success_reply(DBusMessage
*message
)
69 unsigned int success
= 1;
71 reply
= dbus_message_new_method_return(message
);
72 dbus_message_append_args(reply
, DBUS_TYPE_UINT32
, &success
,
79 * wpas_dbus_global_add_interface - Request registration of a network interface
80 * @message: Pointer to incoming dbus message
81 * @global: %wpa_supplicant global data structure
82 * Returns: The object path of the new interface object,
83 * or a dbus error message with more information
85 * Handler function for "addInterface" method call. Handles requests
86 * by dbus clients to register a network interface that wpa_supplicant
89 DBusMessage
* wpas_dbus_global_add_interface(DBusMessage
*message
,
90 struct wpa_global
*global
)
94 char *driver_param
= NULL
;
95 char *confname
= NULL
;
96 char *bridge_ifname
= NULL
;
97 DBusMessage
*reply
= NULL
;
100 dbus_message_iter_init(message
, &iter
);
102 /* First argument: interface name (DBUS_TYPE_STRING)
103 * Required; must be non-zero length
105 if (dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_STRING
)
107 dbus_message_iter_get_basic(&iter
, &ifname
);
108 if (!os_strlen(ifname
))
111 /* Second argument: dict of options */
112 if (dbus_message_iter_next(&iter
)) {
113 DBusMessageIter iter_dict
;
114 struct wpa_dbus_dict_entry entry
;
116 if (!wpa_dbus_dict_open_read(&iter
, &iter_dict
))
118 while (wpa_dbus_dict_has_dict_entry(&iter_dict
)) {
119 if (!wpa_dbus_dict_get_entry(&iter_dict
, &entry
))
121 if (!strcmp(entry
.key
, "driver") &&
122 (entry
.type
== DBUS_TYPE_STRING
)) {
123 driver
= os_strdup(entry
.str_value
);
124 wpa_dbus_dict_entry_clear(&entry
);
127 } else if (!strcmp(entry
.key
, "driver-params") &&
128 (entry
.type
== DBUS_TYPE_STRING
)) {
129 driver_param
= os_strdup(entry
.str_value
);
130 wpa_dbus_dict_entry_clear(&entry
);
131 if (driver_param
== NULL
)
133 } else if (!strcmp(entry
.key
, "config-file") &&
134 (entry
.type
== DBUS_TYPE_STRING
)) {
135 confname
= os_strdup(entry
.str_value
);
136 wpa_dbus_dict_entry_clear(&entry
);
137 if (confname
== NULL
)
139 } else if (!strcmp(entry
.key
, "bridge-ifname") &&
140 (entry
.type
== DBUS_TYPE_STRING
)) {
141 bridge_ifname
= os_strdup(entry
.str_value
);
142 wpa_dbus_dict_entry_clear(&entry
);
143 if (bridge_ifname
== NULL
)
146 wpa_dbus_dict_entry_clear(&entry
);
153 * Try to get the wpa_supplicant record for this iface, return
154 * an error if we already control it.
156 if (wpa_supplicant_get_iface(global
, ifname
) != NULL
) {
157 reply
= dbus_message_new_error(message
,
158 WPAS_ERROR_EXISTS_ERROR
,
159 "wpa_supplicant already "
160 "controls this interface.");
162 struct wpa_supplicant
*wpa_s
;
163 struct wpa_interface iface
;
164 os_memset(&iface
, 0, sizeof(iface
));
165 iface
.ifname
= ifname
;
166 iface
.driver
= driver
;
167 iface
.driver_param
= driver_param
;
168 iface
.confname
= confname
;
169 iface
.bridge_ifname
= bridge_ifname
;
170 /* Otherwise, have wpa_supplicant attach to it. */
171 if ((wpa_s
= wpa_supplicant_add_iface(global
, &iface
))) {
172 const char *path
= wpa_supplicant_get_dbus_path(wpa_s
);
173 reply
= dbus_message_new_method_return(message
);
174 dbus_message_append_args(reply
, DBUS_TYPE_OBJECT_PATH
,
175 &path
, DBUS_TYPE_INVALID
);
177 reply
= dbus_message_new_error(message
,
178 WPAS_ERROR_ADD_ERROR
,
180 "couldn't grab this "
187 os_free(driver_param
);
189 os_free(bridge_ifname
);
193 reply
= wpas_dbus_new_invalid_opts_error(message
, NULL
);
199 * wpas_dbus_global_remove_interface - Request deregistration of an interface
200 * @message: Pointer to incoming dbus message
201 * @global: wpa_supplicant global data structure
202 * Returns: a dbus message containing a UINT32 indicating success (1) or
203 * failure (0), or returns a dbus error message with more information
205 * Handler function for "removeInterface" method call. Handles requests
206 * by dbus clients to deregister a network interface that wpa_supplicant
209 DBusMessage
* wpas_dbus_global_remove_interface(DBusMessage
*message
,
210 struct wpa_global
*global
)
212 struct wpa_supplicant
*wpa_s
;
214 DBusMessage
*reply
= NULL
;
216 if (!dbus_message_get_args(message
, NULL
,
217 DBUS_TYPE_OBJECT_PATH
, &path
,
218 DBUS_TYPE_INVALID
)) {
219 reply
= wpas_dbus_new_invalid_opts_error(message
, NULL
);
223 wpa_s
= wpa_supplicant_get_iface_by_dbus_path(global
, path
);
225 reply
= wpas_dbus_new_invalid_iface_error(message
);
229 if (!wpa_supplicant_remove_iface(global
, wpa_s
)) {
230 reply
= wpas_dbus_new_success_reply(message
);
232 reply
= dbus_message_new_error(message
,
233 WPAS_ERROR_REMOVE_ERROR
,
234 "wpa_supplicant couldn't "
235 "remove this interface.");
244 * wpas_dbus_global_get_interface - Get the object path for an interface name
245 * @message: Pointer to incoming dbus message
246 * @global: %wpa_supplicant global data structure
247 * Returns: The object path of the interface object,
248 * or a dbus error message with more information
250 * Handler function for "getInterface" method call. Handles requests
251 * by dbus clients for the object path of an specific network interface.
253 DBusMessage
* wpas_dbus_global_get_interface(DBusMessage
*message
,
254 struct wpa_global
*global
)
256 DBusMessage
*reply
= NULL
;
259 struct wpa_supplicant
*wpa_s
;
261 if (!dbus_message_get_args(message
, NULL
,
262 DBUS_TYPE_STRING
, &ifname
,
263 DBUS_TYPE_INVALID
)) {
264 reply
= wpas_dbus_new_invalid_opts_error(message
, NULL
);
268 wpa_s
= wpa_supplicant_get_iface(global
, ifname
);
270 reply
= wpas_dbus_new_invalid_iface_error(message
);
274 path
= wpa_supplicant_get_dbus_path(wpa_s
);
276 reply
= dbus_message_new_error(message
,
277 WPAS_ERROR_INTERNAL_ERROR
,
278 "an internal error occurred "
279 "getting the interface.");
283 reply
= dbus_message_new_method_return(message
);
284 dbus_message_append_args(reply
,
285 DBUS_TYPE_OBJECT_PATH
, &path
,
294 * wpas_dbus_global_set_debugparams- Set the debug params
295 * @message: Pointer to incoming dbus message
296 * @global: %wpa_supplicant global data structure
297 * Returns: a dbus message containing a UINT32 indicating success (1) or
298 * failure (0), or returns a dbus error message with more information
300 * Handler function for "setDebugParams" method call. Handles requests
301 * by dbus clients for the object path of an specific network interface.
303 DBusMessage
* wpas_dbus_global_set_debugparams(DBusMessage
*message
,
304 struct wpa_global
*global
)
306 DBusMessage
*reply
= NULL
;
308 dbus_bool_t debug_timestamp
;
309 dbus_bool_t debug_show_keys
;
311 if (!dbus_message_get_args(message
, NULL
,
312 DBUS_TYPE_INT32
, &debug_level
,
313 DBUS_TYPE_BOOLEAN
, &debug_timestamp
,
314 DBUS_TYPE_BOOLEAN
, &debug_show_keys
,
315 DBUS_TYPE_INVALID
)) {
316 return wpas_dbus_new_invalid_opts_error(message
, NULL
);
319 if (wpa_supplicant_set_debug_params(global
, debug_level
,
320 debug_timestamp
? 1 : 0,
321 debug_show_keys
? 1 : 0)) {
322 return wpas_dbus_new_invalid_opts_error(message
, NULL
);
325 reply
= wpas_dbus_new_success_reply(message
);
332 * wpas_dbus_iface_scan - Request a wireless scan on an interface
333 * @message: Pointer to incoming dbus message
334 * @wpa_s: wpa_supplicant structure for a network interface
335 * Returns: a dbus message containing a UINT32 indicating success (1) or
338 * Handler function for "scan" method call of a network device. Requests
339 * that wpa_supplicant perform a wireless scan as soon as possible
340 * on a particular wireless interface.
342 DBusMessage
* wpas_dbus_iface_scan(DBusMessage
*message
,
343 struct wpa_supplicant
*wpa_s
)
346 wpa_supplicant_req_scan(wpa_s
, 0, 0);
347 return wpas_dbus_new_success_reply(message
);
352 * wpas_dbus_iface_scan_results - Get the results of a recent scan request
353 * @message: Pointer to incoming dbus message
354 * @wpa_s: wpa_supplicant structure for a network interface
355 * Returns: a dbus message containing a dbus array of objects paths, or returns
356 * a dbus error message if not scan results could be found
358 * Handler function for "scanResults" method call of a network device. Returns
359 * a dbus message containing the object paths of wireless networks found.
361 DBusMessage
* wpas_dbus_iface_scan_results(DBusMessage
*message
,
362 struct wpa_supplicant
*wpa_s
)
364 DBusMessage
*reply
= NULL
;
365 DBusMessageIter iter
;
366 DBusMessageIter sub_iter
;
369 /* Ensure we've actually got scan results to return */
370 if (wpa_s
->scan_res
== NULL
&&
371 wpa_supplicant_get_scan_results(wpa_s
) < 0) {
372 reply
= dbus_message_new_error(message
, WPAS_ERROR_SCAN_ERROR
,
373 "An error ocurred getting scan "
378 /* Create and initialize the return message */
379 reply
= dbus_message_new_method_return(message
);
380 dbus_message_iter_init_append(reply
, &iter
);
381 dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
,
382 DBUS_TYPE_OBJECT_PATH_AS_STRING
,
385 /* Loop through scan results and append each result's object path */
386 for (i
= 0; i
< wpa_s
->scan_res
->num
; i
++) {
387 struct wpa_scan_res
*res
= wpa_s
->scan_res
->res
[i
];
390 path
= os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX
);
392 perror("wpas_dbus_iface_scan_results[dbus]: out of "
394 wpa_printf(MSG_ERROR
, "dbus control interface: not "
395 "enough memory to send scan results "
399 /* Construct the object path for this network. Note that ':'
400 * is not a valid character in dbus object paths.
402 os_snprintf(path
, WPAS_DBUS_OBJECT_PATH_MAX
,
403 "%s/" WPAS_DBUS_BSSIDS_PART
"/"
404 WPAS_DBUS_BSSID_FORMAT
,
405 wpa_supplicant_get_dbus_path(wpa_s
),
406 MAC2STR(res
->bssid
));
407 dbus_message_iter_append_basic(&sub_iter
,
408 DBUS_TYPE_OBJECT_PATH
, &path
);
412 dbus_message_iter_close_container(&iter
, &sub_iter
);
420 * wpas_dbus_bssid_properties - Return the properties of a scanned network
421 * @message: Pointer to incoming dbus message
422 * @wpa_s: wpa_supplicant structure for a network interface
423 * @res: wpa_supplicant scan result for which to get properties
424 * Returns: a dbus message containing the properties for the requested network
426 * Handler function for "properties" method call of a scanned network.
427 * Returns a dbus message containing the the properties.
429 DBusMessage
* wpas_dbus_bssid_properties(DBusMessage
*message
,
430 struct wpa_supplicant
*wpa_s
,
431 struct wpa_scan_res
*res
)
433 DBusMessage
*reply
= NULL
;
434 DBusMessageIter iter
, iter_dict
;
437 /* Dump the properties into a dbus message */
438 reply
= dbus_message_new_method_return(message
);
440 dbus_message_iter_init_append(reply
, &iter
);
441 if (!wpa_dbus_dict_open_write(&iter
, &iter_dict
))
444 if (!wpa_dbus_dict_append_byte_array(&iter_dict
, "bssid",
445 (const char *) res
->bssid
,
449 ie
= wpa_scan_get_ie(res
, WLAN_EID_SSID
);
451 if (!wpa_dbus_dict_append_byte_array(&iter_dict
, "ssid",
452 (const char *) (ie
+ 2),
457 ie
= wpa_scan_get_vendor_ie(res
, WPA_IE_VENDOR_TYPE
);
459 if (!wpa_dbus_dict_append_byte_array(&iter_dict
, "wpaie",
465 ie
= wpa_scan_get_ie(res
, WLAN_EID_RSN
);
467 if (!wpa_dbus_dict_append_byte_array(&iter_dict
, "rsnie",
473 ie
= wpa_scan_get_vendor_ie(res
, WPS_IE_VENDOR_TYPE
);
475 if (!wpa_dbus_dict_append_byte_array(&iter_dict
, "wpsie",
482 if (!wpa_dbus_dict_append_int32(&iter_dict
, "frequency",
486 if (!wpa_dbus_dict_append_uint16(&iter_dict
, "capabilities",
489 if (!(res
->flags
& WPA_SCAN_QUAL_INVALID
) &&
490 !wpa_dbus_dict_append_int32(&iter_dict
, "quality", res
->qual
))
492 if (!(res
->flags
& WPA_SCAN_NOISE_INVALID
) &&
493 !wpa_dbus_dict_append_int32(&iter_dict
, "noise", res
->noise
))
495 if (!(res
->flags
& WPA_SCAN_LEVEL_INVALID
) &&
496 !wpa_dbus_dict_append_int32(&iter_dict
, "level", res
->level
))
498 if (!wpa_dbus_dict_append_int32(&iter_dict
, "maxrate",
499 wpa_scan_get_max_rate(res
) * 500000))
502 if (!wpa_dbus_dict_close_write(&iter
, &iter_dict
))
509 dbus_message_unref(reply
);
510 return dbus_message_new_error(message
, WPAS_ERROR_INTERNAL_ERROR
,
511 "an internal error occurred returning "
512 "BSSID properties.");
517 * wpas_dbus_iface_capabilities - Return interface capabilities
518 * @message: Pointer to incoming dbus message
519 * @wpa_s: wpa_supplicant structure for a network interface
520 * Returns: A dbus message containing a dict of strings
522 * Handler function for "capabilities" method call of an interface.
524 DBusMessage
* wpas_dbus_iface_capabilities(DBusMessage
*message
,
525 struct wpa_supplicant
*wpa_s
)
527 DBusMessage
*reply
= NULL
;
528 struct wpa_driver_capa capa
;
530 DBusMessageIter iter
, iter_dict
;
533 dbus_bool_t strict
= FALSE
;
534 DBusMessageIter iter_dict_entry
, iter_dict_val
, iter_array
;
536 if (!dbus_message_get_args(message
, NULL
,
537 DBUS_TYPE_BOOLEAN
, &strict
,
541 reply
= dbus_message_new_method_return(message
);
543 dbus_message_iter_init_append(reply
, &iter
);
544 if (!wpa_dbus_dict_open_write(&iter
, &iter_dict
))
548 eap_methods
= eap_get_names_as_string_array(&num_items
);
550 dbus_bool_t success
= FALSE
;
553 success
= wpa_dbus_dict_append_string_array(
554 &iter_dict
, "eap", (const char **) eap_methods
,
557 /* free returned method array */
558 while (eap_methods
[i
])
559 os_free(eap_methods
[i
++]);
560 os_free(eap_methods
);
566 res
= wpa_drv_get_capa(wpa_s
, &capa
);
568 /***** pairwise cipher */
571 const char *args
[] = {"CCMP", "TKIP", "NONE"};
572 if (!wpa_dbus_dict_append_string_array(
573 &iter_dict
, "pairwise", args
,
574 sizeof(args
) / sizeof(char*)))
578 if (!wpa_dbus_dict_begin_string_array(&iter_dict
, "pairwise",
584 if (capa
.enc
& WPA_DRIVER_CAPA_ENC_CCMP
) {
585 if (!wpa_dbus_dict_string_array_add_element(
586 &iter_array
, "CCMP"))
590 if (capa
.enc
& WPA_DRIVER_CAPA_ENC_TKIP
) {
591 if (!wpa_dbus_dict_string_array_add_element(
592 &iter_array
, "TKIP"))
596 if (capa
.key_mgmt
& WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE
) {
597 if (!wpa_dbus_dict_string_array_add_element(
598 &iter_array
, "NONE"))
602 if (!wpa_dbus_dict_end_string_array(&iter_dict
,
609 /***** group cipher */
612 const char *args
[] = {
613 "CCMP", "TKIP", "WEP104", "WEP40"
615 if (!wpa_dbus_dict_append_string_array(
616 &iter_dict
, "group", args
,
617 sizeof(args
) / sizeof(char*)))
621 if (!wpa_dbus_dict_begin_string_array(&iter_dict
, "group",
627 if (capa
.enc
& WPA_DRIVER_CAPA_ENC_CCMP
) {
628 if (!wpa_dbus_dict_string_array_add_element(
629 &iter_array
, "CCMP"))
633 if (capa
.enc
& WPA_DRIVER_CAPA_ENC_TKIP
) {
634 if (!wpa_dbus_dict_string_array_add_element(
635 &iter_array
, "TKIP"))
639 if (capa
.enc
& WPA_DRIVER_CAPA_ENC_WEP104
) {
640 if (!wpa_dbus_dict_string_array_add_element(
641 &iter_array
, "WEP104"))
645 if (capa
.enc
& WPA_DRIVER_CAPA_ENC_WEP40
) {
646 if (!wpa_dbus_dict_string_array_add_element(
647 &iter_array
, "WEP40"))
651 if (!wpa_dbus_dict_end_string_array(&iter_dict
,
658 /***** key management */
661 const char *args
[] = {
662 "WPA-PSK", "WPA-EAP", "IEEE8021X", "WPA-NONE",
665 if (!wpa_dbus_dict_append_string_array(
666 &iter_dict
, "key_mgmt", args
,
667 sizeof(args
) / sizeof(char*)))
671 if (!wpa_dbus_dict_begin_string_array(&iter_dict
, "key_mgmt",
677 if (!wpa_dbus_dict_string_array_add_element(&iter_array
,
681 if (!wpa_dbus_dict_string_array_add_element(&iter_array
,
685 if (capa
.key_mgmt
& (WPA_DRIVER_CAPA_KEY_MGMT_WPA
|
686 WPA_DRIVER_CAPA_KEY_MGMT_WPA2
)) {
687 if (!wpa_dbus_dict_string_array_add_element(
688 &iter_array
, "WPA-EAP"))
692 if (capa
.key_mgmt
& (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK
|
693 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK
)) {
694 if (!wpa_dbus_dict_string_array_add_element(
695 &iter_array
, "WPA-PSK"))
699 if (capa
.key_mgmt
& WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE
) {
700 if (!wpa_dbus_dict_string_array_add_element(
701 &iter_array
, "WPA-NONE"))
705 if (!wpa_dbus_dict_end_string_array(&iter_dict
,
712 /***** WPA protocol */
715 const char *args
[] = { "RSN", "WPA" };
716 if (!wpa_dbus_dict_append_string_array(
717 &iter_dict
, "proto", args
,
718 sizeof(args
) / sizeof(char*)))
722 if (!wpa_dbus_dict_begin_string_array(&iter_dict
, "proto",
728 if (capa
.key_mgmt
& (WPA_DRIVER_CAPA_KEY_MGMT_WPA2
|
729 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK
)) {
730 if (!wpa_dbus_dict_string_array_add_element(
735 if (capa
.key_mgmt
& (WPA_DRIVER_CAPA_KEY_MGMT_WPA
|
736 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK
)) {
737 if (!wpa_dbus_dict_string_array_add_element(
742 if (!wpa_dbus_dict_end_string_array(&iter_dict
,
752 const char *args
[] = { "OPEN", "SHARED", "LEAP" };
753 if (!wpa_dbus_dict_append_string_array(
754 &iter_dict
, "auth_alg", args
,
755 sizeof(args
) / sizeof(char*)))
759 if (!wpa_dbus_dict_begin_string_array(&iter_dict
, "auth_alg",
765 if (capa
.auth
& (WPA_DRIVER_AUTH_OPEN
)) {
766 if (!wpa_dbus_dict_string_array_add_element(
767 &iter_array
, "OPEN"))
771 if (capa
.auth
& (WPA_DRIVER_AUTH_SHARED
)) {
772 if (!wpa_dbus_dict_string_array_add_element(
773 &iter_array
, "SHARED"))
777 if (capa
.auth
& (WPA_DRIVER_AUTH_LEAP
)) {
778 if (!wpa_dbus_dict_string_array_add_element(
779 &iter_array
, "LEAP"))
783 if (!wpa_dbus_dict_end_string_array(&iter_dict
,
790 if (!wpa_dbus_dict_close_write(&iter
, &iter_dict
))
797 dbus_message_unref(reply
);
798 return dbus_message_new_error(message
, WPAS_ERROR_INTERNAL_ERROR
,
799 "an internal error occurred returning "
800 "interface capabilities.");
805 * wpas_dbus_iface_add_network - Add a new configured network
806 * @message: Pointer to incoming dbus message
807 * @wpa_s: wpa_supplicant structure for a network interface
808 * Returns: A dbus message containing the object path of the new network
810 * Handler function for "addNetwork" method call of a network interface.
812 DBusMessage
* wpas_dbus_iface_add_network(DBusMessage
*message
,
813 struct wpa_supplicant
*wpa_s
)
815 DBusMessage
*reply
= NULL
;
816 struct wpa_ssid
*ssid
;
819 path
= os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX
);
821 perror("wpas_dbus_iface_scan_results[dbus]: out of "
823 wpa_printf(MSG_ERROR
, "dbus control interface: not "
824 "enough memory to send scan results "
829 ssid
= wpa_config_add_network(wpa_s
->conf
);
831 reply
= dbus_message_new_error(message
,
832 WPAS_ERROR_ADD_NETWORK_ERROR
,
833 "wpa_supplicant could not add "
834 "a network on this interface.");
837 wpas_notify_network_added(wpa_s
, ssid
);
839 wpa_config_set_network_defaults(ssid
);
841 /* Construct the object path for this network. */
842 os_snprintf(path
, WPAS_DBUS_OBJECT_PATH_MAX
,
843 "%s/" WPAS_DBUS_NETWORKS_PART
"/%d",
844 wpa_supplicant_get_dbus_path(wpa_s
),
847 reply
= dbus_message_new_method_return(message
);
848 dbus_message_append_args(reply
, DBUS_TYPE_OBJECT_PATH
,
849 &path
, DBUS_TYPE_INVALID
);
858 * wpas_dbus_iface_remove_network - Remove a configured network
859 * @message: Pointer to incoming dbus message
860 * @wpa_s: wpa_supplicant structure for a network interface
861 * Returns: A dbus message containing a UINT32 indicating success (1) or
864 * Handler function for "removeNetwork" method call of a network interface.
866 DBusMessage
* wpas_dbus_iface_remove_network(DBusMessage
*message
,
867 struct wpa_supplicant
*wpa_s
)
869 DBusMessage
*reply
= NULL
;
871 char *iface
= NULL
, *net_id
= NULL
;
873 struct wpa_ssid
*ssid
;
875 if (!dbus_message_get_args(message
, NULL
,
876 DBUS_TYPE_OBJECT_PATH
, &op
,
877 DBUS_TYPE_INVALID
)) {
878 reply
= wpas_dbus_new_invalid_opts_error(message
, NULL
);
882 /* Extract the network ID */
883 iface
= wpas_dbus_decompose_object_path(op
, &net_id
, NULL
);
885 reply
= wpas_dbus_new_invalid_network_error(message
);
889 /* Ensure the network is actually a child of this interface */
890 if (strcmp(iface
, wpa_supplicant_get_dbus_path(wpa_s
)) != 0) {
891 reply
= wpas_dbus_new_invalid_network_error(message
);
895 id
= strtoul(net_id
, NULL
, 10);
896 ssid
= wpa_config_get_network(wpa_s
->conf
, id
);
898 reply
= wpas_dbus_new_invalid_network_error(message
);
902 wpas_notify_network_removed(wpa_s
, ssid
);
904 if (wpa_config_remove_network(wpa_s
->conf
, id
) < 0) {
905 reply
= dbus_message_new_error(message
,
906 WPAS_ERROR_REMOVE_NETWORK_ERROR
,
907 "error removing the specified "
908 "on this interface.");
912 if (ssid
== wpa_s
->current_ssid
)
913 wpa_supplicant_disassociate(wpa_s
, WLAN_REASON_DEAUTH_LEAVING
);
914 reply
= wpas_dbus_new_success_reply(message
);
923 static const char *dont_quote
[] = {
924 "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
925 "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
930 static dbus_bool_t
should_quote_opt(const char *key
)
933 while (dont_quote
[i
] != NULL
) {
934 if (strcmp(key
, dont_quote
[i
]) == 0)
943 * wpas_dbus_iface_set_network - Set options for a configured network
944 * @message: Pointer to incoming dbus message
945 * @wpa_s: wpa_supplicant structure for a network interface
946 * @ssid: wpa_ssid structure for a configured network
947 * Returns: a dbus message containing a UINT32 indicating success (1) or
950 * Handler function for "set" method call of a configured network.
952 DBusMessage
* wpas_dbus_iface_set_network(DBusMessage
*message
,
953 struct wpa_supplicant
*wpa_s
,
954 struct wpa_ssid
*ssid
)
956 DBusMessage
*reply
= NULL
;
957 struct wpa_dbus_dict_entry entry
= { .type
= DBUS_TYPE_STRING
};
958 DBusMessageIter iter
, iter_dict
;
960 dbus_message_iter_init(message
, &iter
);
962 if (!wpa_dbus_dict_open_read(&iter
, &iter_dict
)) {
963 reply
= wpas_dbus_new_invalid_opts_error(message
, NULL
);
967 while (wpa_dbus_dict_has_dict_entry(&iter_dict
)) {
972 if (!wpa_dbus_dict_get_entry(&iter_dict
, &entry
)) {
973 reply
= wpas_dbus_new_invalid_opts_error(message
,
978 /* Type conversions, since wpa_supplicant wants strings */
979 if (entry
.type
== DBUS_TYPE_ARRAY
&&
980 entry
.array_type
== DBUS_TYPE_BYTE
) {
981 if (entry
.array_len
<= 0)
984 size
= entry
.array_len
* 2 + 1;
985 value
= os_zalloc(size
);
988 ret
= wpa_snprintf_hex(value
, size
,
989 (u8
*) entry
.bytearray_value
,
993 } else if (entry
.type
== DBUS_TYPE_STRING
) {
994 if (should_quote_opt(entry
.key
)) {
995 size
= os_strlen(entry
.str_value
);
996 /* Zero-length option check */
999 size
+= 3; /* For quotes and terminator */
1000 value
= os_zalloc(size
);
1003 ret
= os_snprintf(value
, size
, "\"%s\"",
1005 if (ret
< 0 || (size_t) ret
!= (size
- 1))
1008 value
= os_strdup(entry
.str_value
);
1012 } else if (entry
.type
== DBUS_TYPE_UINT32
) {
1013 value
= os_zalloc(size
);
1016 ret
= os_snprintf(value
, size
, "%u",
1017 entry
.uint32_value
);
1020 } else if (entry
.type
== DBUS_TYPE_INT32
) {
1021 value
= os_zalloc(size
);
1024 ret
= os_snprintf(value
, size
, "%d",
1031 if (wpa_config_set(ssid
, entry
.key
, value
, 0) < 0)
1034 if ((os_strcmp(entry
.key
, "psk") == 0 &&
1035 value
[0] == '"' && ssid
->ssid_len
) ||
1036 (os_strcmp(entry
.key
, "ssid") == 0 && ssid
->passphrase
))
1037 wpa_config_update_psk(ssid
);
1040 wpa_dbus_dict_entry_clear(&entry
);
1045 reply
= wpas_dbus_new_invalid_opts_error(message
, entry
.key
);
1046 wpa_dbus_dict_entry_clear(&entry
);
1051 reply
= wpas_dbus_new_success_reply(message
);
1059 * wpas_dbus_iface_enable_network - Mark a configured network as enabled
1060 * @message: Pointer to incoming dbus message
1061 * @wpa_s: wpa_supplicant structure for a network interface
1062 * @ssid: wpa_ssid structure for a configured network
1063 * Returns: A dbus message containing a UINT32 indicating success (1) or
1066 * Handler function for "enable" method call of a configured network.
1068 DBusMessage
* wpas_dbus_iface_enable_network(DBusMessage
*message
,
1069 struct wpa_supplicant
*wpa_s
,
1070 struct wpa_ssid
*ssid
)
1072 wpa_supplicant_enable_network(wpa_s
, ssid
);
1073 return wpas_dbus_new_success_reply(message
);
1078 * wpas_dbus_iface_disable_network - Mark a configured network as disabled
1079 * @message: Pointer to incoming dbus message
1080 * @wpa_s: wpa_supplicant structure for a network interface
1081 * @ssid: wpa_ssid structure for a configured network
1082 * Returns: A dbus message containing a UINT32 indicating success (1) or
1085 * Handler function for "disable" method call of a configured network.
1087 DBusMessage
* wpas_dbus_iface_disable_network(DBusMessage
*message
,
1088 struct wpa_supplicant
*wpa_s
,
1089 struct wpa_ssid
*ssid
)
1091 wpa_supplicant_disable_network(wpa_s
, ssid
);
1092 return wpas_dbus_new_success_reply(message
);
1097 * wpas_dbus_iface_select_network - Attempt association with a configured network
1098 * @message: Pointer to incoming dbus message
1099 * @wpa_s: wpa_supplicant structure for a network interface
1100 * Returns: A dbus message containing a UINT32 indicating success (1) or
1103 * Handler function for "selectNetwork" method call of network interface.
1105 DBusMessage
* wpas_dbus_iface_select_network(DBusMessage
*message
,
1106 struct wpa_supplicant
*wpa_s
)
1108 DBusMessage
*reply
= NULL
;
1110 struct wpa_ssid
*ssid
;
1111 char *iface_obj_path
= NULL
;
1112 char *network
= NULL
;
1114 if (os_strlen(dbus_message_get_signature(message
)) == 0) {
1118 const char *obj_path
;
1121 if (!dbus_message_get_args(message
, NULL
,
1122 DBUS_TYPE_OBJECT_PATH
, &op
,
1123 DBUS_TYPE_INVALID
)) {
1124 reply
= wpas_dbus_new_invalid_opts_error(message
,
1129 /* Extract the network number */
1130 iface_obj_path
= wpas_dbus_decompose_object_path(op
,
1133 if (iface_obj_path
== NULL
) {
1134 reply
= wpas_dbus_new_invalid_iface_error(message
);
1137 /* Ensure the object path really points to this interface */
1138 obj_path
= wpa_supplicant_get_dbus_path(wpa_s
);
1139 if (os_strcmp(iface_obj_path
, obj_path
) != 0) {
1140 reply
= wpas_dbus_new_invalid_network_error(message
);
1144 nid
= strtoul(network
, NULL
, 10);
1145 if (errno
== EINVAL
) {
1146 reply
= wpas_dbus_new_invalid_network_error(message
);
1150 ssid
= wpa_config_get_network(wpa_s
->conf
, nid
);
1152 reply
= wpas_dbus_new_invalid_network_error(message
);
1157 /* Finally, associate with the network */
1158 wpa_supplicant_select_network(wpa_s
, ssid
);
1160 reply
= wpas_dbus_new_success_reply(message
);
1163 os_free(iface_obj_path
);
1170 * wpas_dbus_iface_disconnect - Terminate the current connection
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 "disconnect" method call of network interface.
1178 DBusMessage
* wpas_dbus_iface_disconnect(DBusMessage
*message
,
1179 struct wpa_supplicant
*wpa_s
)
1181 wpa_s
->disconnected
= 1;
1182 wpa_supplicant_disassociate(wpa_s
, WLAN_REASON_DEAUTH_LEAVING
);
1184 return wpas_dbus_new_success_reply(message
);
1189 * wpas_dbus_iface_set_ap_scan - Control roaming mode
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 "setAPScan" method call.
1197 DBusMessage
* wpas_dbus_iface_set_ap_scan(DBusMessage
*message
,
1198 struct wpa_supplicant
*wpa_s
)
1200 DBusMessage
*reply
= NULL
;
1201 dbus_uint32_t ap_scan
= 1;
1203 if (!dbus_message_get_args(message
, NULL
, DBUS_TYPE_UINT32
, &ap_scan
,
1204 DBUS_TYPE_INVALID
)) {
1205 reply
= wpas_dbus_new_invalid_opts_error(message
, NULL
);
1209 if (wpa_supplicant_set_ap_scan(wpa_s
, ap_scan
)) {
1210 reply
= wpas_dbus_new_invalid_opts_error(message
, NULL
);
1214 reply
= wpas_dbus_new_success_reply(message
);
1222 * wpas_dbus_iface_set_smartcard_modules - Set smartcard related module paths
1223 * @message: Pointer to incoming dbus message
1224 * @wpa_s: wpa_supplicant structure for a network interface
1225 * Returns: A dbus message containing a UINT32 indicating success (1) or
1228 * Handler function for "setSmartcardModules" method call.
1230 DBusMessage
* wpas_dbus_iface_set_smartcard_modules(
1231 DBusMessage
*message
, struct wpa_supplicant
*wpa_s
)
1233 DBusMessageIter iter
, iter_dict
;
1234 char *opensc_engine_path
= NULL
;
1235 char *pkcs11_engine_path
= NULL
;
1236 char *pkcs11_module_path
= NULL
;
1237 struct wpa_dbus_dict_entry entry
;
1239 if (!dbus_message_iter_init(message
, &iter
))
1242 if (!wpa_dbus_dict_open_read(&iter
, &iter_dict
))
1245 while (wpa_dbus_dict_has_dict_entry(&iter_dict
)) {
1246 if (!wpa_dbus_dict_get_entry(&iter_dict
, &entry
))
1248 if (!strcmp(entry
.key
, "opensc_engine_path") &&
1249 (entry
.type
== DBUS_TYPE_STRING
)) {
1250 opensc_engine_path
= os_strdup(entry
.str_value
);
1251 if (opensc_engine_path
== NULL
)
1253 } else if (!strcmp(entry
.key
, "pkcs11_engine_path") &&
1254 (entry
.type
== DBUS_TYPE_STRING
)) {
1255 pkcs11_engine_path
= os_strdup(entry
.str_value
);
1256 if (pkcs11_engine_path
== NULL
)
1258 } else if (!strcmp(entry
.key
, "pkcs11_module_path") &&
1259 (entry
.type
== DBUS_TYPE_STRING
)) {
1260 pkcs11_module_path
= os_strdup(entry
.str_value
);
1261 if (pkcs11_module_path
== NULL
)
1264 wpa_dbus_dict_entry_clear(&entry
);
1267 wpa_dbus_dict_entry_clear(&entry
);
1270 os_free(wpa_s
->conf
->opensc_engine_path
);
1271 wpa_s
->conf
->opensc_engine_path
= opensc_engine_path
;
1272 os_free(wpa_s
->conf
->pkcs11_engine_path
);
1273 wpa_s
->conf
->pkcs11_engine_path
= pkcs11_engine_path
;
1274 os_free(wpa_s
->conf
->pkcs11_module_path
);
1275 wpa_s
->conf
->pkcs11_module_path
= pkcs11_module_path
;
1277 wpa_sm_set_eapol(wpa_s
->wpa
, NULL
);
1278 eapol_sm_deinit(wpa_s
->eapol
);
1279 wpa_s
->eapol
= NULL
;
1280 wpa_supplicant_init_eapol(wpa_s
);
1281 wpa_sm_set_eapol(wpa_s
->wpa
, wpa_s
->eapol
);
1283 return wpas_dbus_new_success_reply(message
);
1286 os_free(opensc_engine_path
);
1287 os_free(pkcs11_engine_path
);
1288 os_free(pkcs11_module_path
);
1289 return wpas_dbus_new_invalid_opts_error(message
, NULL
);
1294 * wpas_dbus_iface_get_state - Get interface state
1295 * @message: Pointer to incoming dbus message
1296 * @wpa_s: wpa_supplicant structure for a network interface
1297 * Returns: A dbus message containing a STRING representing the current
1300 * Handler function for "state" method call.
1302 DBusMessage
* wpas_dbus_iface_get_state(DBusMessage
*message
,
1303 struct wpa_supplicant
*wpa_s
)
1305 DBusMessage
*reply
= NULL
;
1306 const char *str_state
;
1308 reply
= dbus_message_new_method_return(message
);
1309 if (reply
!= NULL
) {
1310 str_state
= wpa_supplicant_state_txt(wpa_s
->wpa_state
);
1311 dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &str_state
,
1320 * wpas_dbus_iface_get_scanning - Get interface scanning state
1321 * @message: Pointer to incoming dbus message
1322 * @wpa_s: wpa_supplicant structure for a network interface
1323 * Returns: A dbus message containing whether the interface is scanning
1325 * Handler function for "scanning" method call.
1327 DBusMessage
* wpas_dbus_iface_get_scanning(DBusMessage
*message
,
1328 struct wpa_supplicant
*wpa_s
)
1330 DBusMessage
*reply
= NULL
;
1331 dbus_bool_t scanning
= wpa_s
->scanning
? TRUE
: FALSE
;
1333 reply
= dbus_message_new_method_return(message
);
1334 if (reply
!= NULL
) {
1335 dbus_message_append_args(reply
, DBUS_TYPE_BOOLEAN
, &scanning
,
1338 perror("wpas_dbus_iface_get_scanning[dbus]: out of "
1340 wpa_printf(MSG_ERROR
, "dbus control interface: not enough "
1341 "memory to return scanning state.");
1349 * wpas_dbus_iface_set_blobs - Store named binary blobs (ie, for certificates)
1350 * @message: Pointer to incoming dbus message
1351 * @wpa_s: %wpa_supplicant data structure
1352 * Returns: A dbus message containing a UINT32 indicating success (1) or
1355 * Asks wpa_supplicant to internally store a one or more binary blobs.
1357 DBusMessage
* wpas_dbus_iface_set_blobs(DBusMessage
*message
,
1358 struct wpa_supplicant
*wpa_s
)
1360 DBusMessage
*reply
= NULL
;
1361 struct wpa_dbus_dict_entry entry
= { .type
= DBUS_TYPE_STRING
};
1362 DBusMessageIter iter
, iter_dict
;
1364 dbus_message_iter_init(message
, &iter
);
1366 if (!wpa_dbus_dict_open_read(&iter
, &iter_dict
))
1367 return wpas_dbus_new_invalid_opts_error(message
, NULL
);
1369 while (wpa_dbus_dict_has_dict_entry(&iter_dict
)) {
1370 struct wpa_config_blob
*blob
;
1372 if (!wpa_dbus_dict_get_entry(&iter_dict
, &entry
)) {
1373 reply
= wpas_dbus_new_invalid_opts_error(message
,
1378 if (entry
.type
!= DBUS_TYPE_ARRAY
||
1379 entry
.array_type
!= DBUS_TYPE_BYTE
) {
1380 reply
= wpas_dbus_new_invalid_opts_error(
1381 message
, "Byte array expected.");
1385 if ((entry
.array_len
<= 0) || (entry
.array_len
> 65536) ||
1386 !strlen(entry
.key
)) {
1387 reply
= wpas_dbus_new_invalid_opts_error(
1388 message
, "Invalid array size.");
1392 blob
= os_zalloc(sizeof(*blob
));
1394 reply
= dbus_message_new_error(
1395 message
, WPAS_ERROR_ADD_ERROR
,
1396 "Not enough memory to add blob.");
1399 blob
->data
= os_zalloc(entry
.array_len
);
1400 if (blob
->data
== NULL
) {
1401 reply
= dbus_message_new_error(
1402 message
, WPAS_ERROR_ADD_ERROR
,
1403 "Not enough memory to add blob data.");
1408 blob
->name
= os_strdup(entry
.key
);
1409 blob
->len
= entry
.array_len
;
1410 os_memcpy(blob
->data
, (u8
*) entry
.bytearray_value
,
1412 if (blob
->name
== NULL
|| blob
->data
== NULL
) {
1413 wpa_config_free_blob(blob
);
1414 reply
= dbus_message_new_error(
1415 message
, WPAS_ERROR_ADD_ERROR
,
1416 "Error adding blob.");
1421 if (!wpa_config_remove_blob(wpa_s
->conf
, blob
->name
))
1422 wpas_notify_blob_removed(wpa_s
, blob
->name
);
1423 wpa_config_set_blob(wpa_s
->conf
, blob
);
1424 wpas_notify_blob_added(wpa_s
, blob
->name
);
1426 wpa_dbus_dict_entry_clear(&entry
);
1428 wpa_dbus_dict_entry_clear(&entry
);
1430 return reply
? reply
: wpas_dbus_new_success_reply(message
);
1435 * wpas_dbus_iface_remove_blob - Remove named binary blobs
1436 * @message: Pointer to incoming dbus message
1437 * @wpa_s: %wpa_supplicant data structure
1438 * Returns: A dbus message containing a UINT32 indicating success (1) or
1441 * Asks wpa_supplicant to remove one or more previously stored binary blobs.
1443 DBusMessage
* wpas_dbus_iface_remove_blobs(DBusMessage
*message
,
1444 struct wpa_supplicant
*wpa_s
)
1446 DBusMessageIter iter
, array
;
1447 char *err_msg
= NULL
;
1449 dbus_message_iter_init(message
, &iter
);
1451 if ((dbus_message_iter_get_arg_type (&iter
) != DBUS_TYPE_ARRAY
) ||
1452 (dbus_message_iter_get_element_type (&iter
) != DBUS_TYPE_STRING
))
1453 return wpas_dbus_new_invalid_opts_error(message
, NULL
);
1455 dbus_message_iter_recurse(&iter
, &array
);
1456 while (dbus_message_iter_get_arg_type(&array
) == DBUS_TYPE_STRING
) {
1459 dbus_message_iter_get_basic(&array
, &name
);
1460 if (!os_strlen(name
))
1461 err_msg
= "Invalid blob name.";
1463 if (wpa_config_remove_blob(wpa_s
->conf
, name
) != 0)
1464 err_msg
= "Error removing blob.";
1466 wpas_notify_blob_removed(wpa_s
, name
);
1467 dbus_message_iter_next(&array
);
1471 return dbus_message_new_error(message
, WPAS_ERROR_REMOVE_ERROR
,
1474 return wpas_dbus_new_success_reply(message
);