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.
19 #include "wpa_supplicant_i.h"
20 #include "ctrl_iface_dbus.h"
21 #include "ctrl_iface_dbus_handlers.h"
22 #include "eap_peer/eap_methods.h"
23 #include "dbus_dict_helpers.h"
24 #include "ieee802_11_defs.h"
25 #include "wpas_glue.h"
26 #include "eapol_supp/eapol_supp_sm.h"
29 extern int wpa_debug_level
;
30 extern int wpa_debug_show_keys
;
31 extern int wpa_debug_timestamp
;
34 * wpas_dbus_new_invalid_opts_error - Return a new invalid options error message
35 * @message: Pointer to incoming dbus message this error refers to
36 * Returns: a dbus error message
38 * Convenience function to create and return an invalid options error
40 static DBusMessage
* wpas_dbus_new_invalid_opts_error(DBusMessage
*message
,
45 reply
= dbus_message_new_error(message
, WPAS_ERROR_INVALID_OPTS
,
46 "Did not receive correct message "
49 dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &arg
,
57 * wpas_dbus_new_success_reply - Return a new success reply message
58 * @message: Pointer to incoming dbus message this reply refers to
59 * Returns: a dbus message containing a single UINT32 that indicates
60 * success (ie, a value of 1)
62 * Convenience function to create and return a success reply message
64 static DBusMessage
* wpas_dbus_new_success_reply(DBusMessage
*message
)
67 unsigned int success
= 1;
69 reply
= dbus_message_new_method_return(message
);
70 dbus_message_append_args(reply
, DBUS_TYPE_UINT32
, &success
,
76 static void wpas_dbus_free_wpa_interface(struct wpa_interface
*iface
)
78 free((char *) iface
->driver
);
79 free((char *) iface
->driver_param
);
80 free((char *) iface
->confname
);
81 free((char *) iface
->bridge_ifname
);
86 * wpas_dbus_global_add_interface - Request registration of a network interface
87 * @message: Pointer to incoming dbus message
88 * @global: %wpa_supplicant global data structure
89 * Returns: The object path of the new interface object,
90 * or a dbus error message with more information
92 * Handler function for "addInterface" method call. Handles requests
93 * by dbus clients to register a network interface that wpa_supplicant
96 DBusMessage
* wpas_dbus_global_add_interface(DBusMessage
*message
,
97 struct wpa_global
*global
)
99 struct wpa_interface iface
;
101 DBusMessage
*reply
= NULL
;
102 DBusMessageIter iter
;
104 memset(&iface
, 0, sizeof(iface
));
106 dbus_message_iter_init(message
, &iter
);
108 /* First argument: interface name (DBUS_TYPE_STRING)
109 * Required; must be non-zero length
111 if (dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_STRING
)
113 dbus_message_iter_get_basic(&iter
, &ifname
);
116 iface
.ifname
= ifname
;
118 /* Second argument: dict of options */
119 if (dbus_message_iter_next(&iter
)) {
120 DBusMessageIter iter_dict
;
121 struct wpa_dbus_dict_entry entry
;
123 if (!wpa_dbus_dict_open_read(&iter
, &iter_dict
))
125 while (wpa_dbus_dict_has_dict_entry(&iter_dict
)) {
126 if (!wpa_dbus_dict_get_entry(&iter_dict
, &entry
))
128 if (!strcmp(entry
.key
, "driver") &&
129 (entry
.type
== DBUS_TYPE_STRING
)) {
130 iface
.driver
= strdup(entry
.str_value
);
131 if (iface
.driver
== NULL
)
133 } else if (!strcmp(entry
.key
, "driver-params") &&
134 (entry
.type
== DBUS_TYPE_STRING
)) {
135 iface
.driver_param
= strdup(entry
.str_value
);
136 if (iface
.driver_param
== NULL
)
138 } else if (!strcmp(entry
.key
, "config-file") &&
139 (entry
.type
== DBUS_TYPE_STRING
)) {
140 iface
.confname
= strdup(entry
.str_value
);
141 if (iface
.confname
== NULL
)
143 } else if (!strcmp(entry
.key
, "bridge-ifname") &&
144 (entry
.type
== DBUS_TYPE_STRING
)) {
145 iface
.bridge_ifname
= strdup(entry
.str_value
);
146 if (iface
.bridge_ifname
== NULL
)
149 wpa_dbus_dict_entry_clear(&entry
);
152 wpa_dbus_dict_entry_clear(&entry
);
157 * Try to get the wpa_supplicant record for this iface, return
158 * an error if we already control it.
160 if (wpa_supplicant_get_iface(global
, iface
.ifname
) != NULL
) {
161 reply
= dbus_message_new_error(message
,
162 WPAS_ERROR_EXISTS_ERROR
,
163 "wpa_supplicant already "
164 "controls this interface.");
166 struct wpa_supplicant
*wpa_s
;
167 /* Otherwise, have wpa_supplicant attach to it. */
168 if ((wpa_s
= wpa_supplicant_add_iface(global
, &iface
))) {
169 const char *path
= wpa_supplicant_get_dbus_path(wpa_s
);
170 reply
= dbus_message_new_method_return(message
);
171 dbus_message_append_args(reply
, DBUS_TYPE_OBJECT_PATH
,
172 &path
, DBUS_TYPE_INVALID
);
174 reply
= dbus_message_new_error(message
,
175 WPAS_ERROR_ADD_ERROR
,
177 "couldn't grab this "
181 wpas_dbus_free_wpa_interface(&iface
);
185 wpas_dbus_free_wpa_interface(&iface
);
186 return wpas_dbus_new_invalid_opts_error(message
, NULL
);
191 * wpas_dbus_global_remove_interface - Request deregistration of an interface
192 * @message: Pointer to incoming dbus message
193 * @global: wpa_supplicant global data structure
194 * Returns: a dbus message containing a UINT32 indicating success (1) or
195 * failure (0), or returns a dbus error message with more information
197 * Handler function for "removeInterface" method call. Handles requests
198 * by dbus clients to deregister a network interface that wpa_supplicant
201 DBusMessage
* wpas_dbus_global_remove_interface(DBusMessage
*message
,
202 struct wpa_global
*global
)
204 struct wpa_supplicant
*wpa_s
;
206 DBusMessage
*reply
= NULL
;
208 if (!dbus_message_get_args(message
, NULL
,
209 DBUS_TYPE_OBJECT_PATH
, &path
,
210 DBUS_TYPE_INVALID
)) {
211 reply
= wpas_dbus_new_invalid_opts_error(message
, NULL
);
215 wpa_s
= wpa_supplicant_get_iface_by_dbus_path(global
, path
);
217 reply
= wpas_dbus_new_invalid_iface_error(message
);
221 if (!wpa_supplicant_remove_iface(global
, wpa_s
)) {
222 reply
= wpas_dbus_new_success_reply(message
);
224 reply
= dbus_message_new_error(message
,
225 WPAS_ERROR_REMOVE_ERROR
,
226 "wpa_supplicant couldn't "
227 "remove this interface.");
236 * wpas_dbus_global_get_interface - Get the object path for an interface name
237 * @message: Pointer to incoming dbus message
238 * @global: %wpa_supplicant global data structure
239 * Returns: The object path of the interface object,
240 * or a dbus error message with more information
242 * Handler function for "getInterface" method call. Handles requests
243 * by dbus clients for the object path of an specific network interface.
245 DBusMessage
* wpas_dbus_global_get_interface(DBusMessage
*message
,
246 struct wpa_global
*global
)
248 DBusMessage
*reply
= NULL
;
251 struct wpa_supplicant
*wpa_s
;
253 if (!dbus_message_get_args(message
, NULL
,
254 DBUS_TYPE_STRING
, &ifname
,
255 DBUS_TYPE_INVALID
)) {
256 reply
= wpas_dbus_new_invalid_opts_error(message
, NULL
);
260 wpa_s
= wpa_supplicant_get_iface(global
, ifname
);
262 reply
= wpas_dbus_new_invalid_iface_error(message
);
266 path
= wpa_supplicant_get_dbus_path(wpa_s
);
268 reply
= dbus_message_new_error(message
,
269 WPAS_ERROR_INTERNAL_ERROR
,
270 "an internal error occurred "
271 "getting the interface.");
275 reply
= dbus_message_new_method_return(message
);
276 dbus_message_append_args(reply
,
277 DBUS_TYPE_OBJECT_PATH
, &path
,
285 * wpas_dbus_global_set_debugparams- Set the debug params
286 * @message: Pointer to incoming dbus message
287 * @global: %wpa_supplicant global data structure
288 * Returns: a dbus message containing a UINT32 indicating success (1) or
289 * failure (0), or returns a dbus error message with more information
291 * Handler function for "setDebugParams" method call. Handles requests
292 * by dbus clients for the object path of an specific network interface.
294 DBusMessage
* wpas_dbus_global_set_debugparams(DBusMessage
*message
,
295 struct wpa_global
*global
)
297 DBusMessage
*reply
= NULL
;
299 dbus_bool_t debug_timestamp
;
300 dbus_bool_t debug_show_keys
;
302 if (!dbus_message_get_args(message
, NULL
,
303 DBUS_TYPE_INT32
, &debug_level
,
304 DBUS_TYPE_BOOLEAN
, &debug_timestamp
,
305 DBUS_TYPE_BOOLEAN
, &debug_show_keys
,
306 DBUS_TYPE_INVALID
)) {
307 reply
= wpas_dbus_new_invalid_opts_error(message
, NULL
);
311 /* check for allowed debuglevels */
312 if (debug_level
!= MSG_MSGDUMP
&&
313 debug_level
!= MSG_DEBUG
&&
314 debug_level
!= MSG_INFO
&&
315 debug_level
!= MSG_WARNING
&&
316 debug_level
!= MSG_ERROR
) {
317 reply
= wpas_dbus_new_invalid_opts_error(message
, NULL
);
321 wpa_debug_level
= debug_level
;
322 wpa_debug_timestamp
= debug_timestamp
? 1 : 0;
323 wpa_debug_show_keys
= debug_show_keys
? 1 : 0;
324 reply
= wpas_dbus_new_success_reply(message
);
331 * wpas_dbus_iface_scan - Request a wireless scan on an interface
332 * @message: Pointer to incoming dbus message
333 * @wpa_s: wpa_supplicant structure for a network interface
334 * Returns: a dbus message containing a UINT32 indicating success (1) or
337 * Handler function for "scan" method call of a network device. Requests
338 * that wpa_supplicant perform a wireless scan as soon as possible
339 * on a particular wireless interface.
341 DBusMessage
* wpas_dbus_iface_scan(DBusMessage
*message
,
342 struct wpa_supplicant
*wpa_s
)
345 wpa_supplicant_req_scan(wpa_s
, 0, 0);
346 return wpas_dbus_new_success_reply(message
);
351 * wpas_dbus_iface_scan_results - Get the results of a recent scan request
352 * @message: Pointer to incoming dbus message
353 * @wpa_s: wpa_supplicant structure for a network interface
354 * Returns: a dbus message containing a dbus array of objects paths, or returns
355 * a dbus error message if not scan results could be found
357 * Handler function for "scanResults" method call of a network device. Returns
358 * a dbus message containing the object paths of wireless networks found.
360 DBusMessage
* wpas_dbus_iface_scan_results(DBusMessage
*message
,
361 struct wpa_supplicant
*wpa_s
)
363 DBusMessage
*reply
= NULL
;
364 DBusMessageIter iter
;
365 DBusMessageIter sub_iter
;
368 /* Ensure we've actually got scan results to return */
369 if (wpa_s
->scan_res
== NULL
&&
370 wpa_supplicant_get_scan_results(wpa_s
) < 0) {
371 reply
= dbus_message_new_error(message
, WPAS_ERROR_SCAN_ERROR
,
372 "An error ocurred getting scan "
377 /* Create and initialize the return message */
378 reply
= dbus_message_new_method_return(message
);
379 dbus_message_iter_init_append(reply
, &iter
);
380 dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
,
381 DBUS_TYPE_OBJECT_PATH_AS_STRING
,
384 /* Loop through scan results and append each result's object path */
385 for (i
= 0; i
< wpa_s
->scan_res
->num
; i
++) {
386 struct wpa_scan_res
*res
= wpa_s
->scan_res
->res
[i
];
389 path
= os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX
);
391 perror("wpas_dbus_iface_scan_results[dbus]: out of "
393 wpa_printf(MSG_ERROR
, "dbus control interface: not "
394 "enough memory to send scan results "
398 /* Construct the object path for this network. Note that ':'
399 * is not a valid character in dbus object paths.
401 snprintf(path
, WPAS_DBUS_OBJECT_PATH_MAX
,
402 "%s/" WPAS_DBUS_BSSIDS_PART
"/"
403 WPAS_DBUS_BSSID_FORMAT
,
404 wpa_supplicant_get_dbus_path(wpa_s
),
405 MAC2STR(res
->bssid
));
406 dbus_message_iter_append_basic(&sub_iter
,
407 DBUS_TYPE_OBJECT_PATH
, &path
);
411 dbus_message_iter_close_container(&iter
, &sub_iter
);
419 * wpas_dbus_bssid_properties - Return the properties of a scanned network
420 * @message: Pointer to incoming dbus message
421 * @wpa_s: wpa_supplicant structure for a network interface
422 * @res: wpa_supplicant scan result for which to get properties
423 * Returns: a dbus message containing the properties for the requested network
425 * Handler function for "properties" method call of a scanned network.
426 * Returns a dbus message containing the the properties.
428 DBusMessage
* wpas_dbus_bssid_properties(DBusMessage
*message
,
429 struct wpa_supplicant
*wpa_s
,
430 struct wpa_scan_res
*res
)
432 DBusMessage
*reply
= NULL
;
433 DBusMessageIter iter
, iter_dict
;
436 /* Dump the properties into a dbus message */
437 reply
= dbus_message_new_method_return(message
);
439 dbus_message_iter_init_append(reply
, &iter
);
440 if (!wpa_dbus_dict_open_write(&iter
, &iter_dict
))
443 if (!wpa_dbus_dict_append_byte_array(&iter_dict
, "bssid",
444 (const char *) res
->bssid
,
448 ie
= wpa_scan_get_ie(res
, WLAN_EID_SSID
);
450 if (!wpa_dbus_dict_append_byte_array(&iter_dict
, "ssid",
451 (const char *) (ie
+ 2),
456 ie
= wpa_scan_get_vendor_ie(res
, WPA_IE_VENDOR_TYPE
);
458 if (!wpa_dbus_dict_append_byte_array(&iter_dict
, "wpaie",
464 ie
= wpa_scan_get_ie(res
, WLAN_EID_RSN
);
466 if (!wpa_dbus_dict_append_byte_array(&iter_dict
, "rsnie",
472 ie
= wpa_scan_get_vendor_ie(res
, WPS_IE_VENDOR_TYPE
);
474 if (!wpa_dbus_dict_append_byte_array(&iter_dict
, "wpsie",
481 if (!wpa_dbus_dict_append_int32(&iter_dict
, "frequency",
485 if (!wpa_dbus_dict_append_uint16(&iter_dict
, "capabilities",
488 if (!wpa_dbus_dict_append_int32(&iter_dict
, "quality", res
->qual
))
490 if (!wpa_dbus_dict_append_int32(&iter_dict
, "noise", res
->noise
))
492 if (!wpa_dbus_dict_append_int32(&iter_dict
, "level", res
->level
))
494 if (!wpa_dbus_dict_append_int32(&iter_dict
, "maxrate",
495 wpa_scan_get_max_rate(res
) * 500000))
498 if (!wpa_dbus_dict_close_write(&iter
, &iter_dict
))
505 dbus_message_unref(reply
);
506 return dbus_message_new_error(message
, WPAS_ERROR_INTERNAL_ERROR
,
507 "an internal error occurred returning "
508 "BSSID properties.");
513 * wpas_dbus_iface_capabilities - Return interface capabilities
514 * @message: Pointer to incoming dbus message
515 * @wpa_s: wpa_supplicant structure for a network interface
516 * Returns: A dbus message containing a dict of strings
518 * Handler function for "capabilities" method call of an interface.
520 DBusMessage
* wpas_dbus_iface_capabilities(DBusMessage
*message
,
521 struct wpa_supplicant
*wpa_s
)
523 DBusMessage
*reply
= NULL
;
524 struct wpa_driver_capa capa
;
526 DBusMessageIter iter
, iter_dict
;
529 dbus_bool_t strict
= FALSE
;
530 DBusMessageIter iter_dict_entry
, iter_dict_val
, iter_array
;
532 if (!dbus_message_get_args(message
, NULL
,
533 DBUS_TYPE_BOOLEAN
, &strict
,
537 reply
= dbus_message_new_method_return(message
);
539 dbus_message_iter_init_append(reply
, &iter
);
540 if (!wpa_dbus_dict_open_write(&iter
, &iter_dict
))
544 eap_methods
= eap_get_names_as_string_array(&num_items
);
546 dbus_bool_t success
= FALSE
;
549 success
= wpa_dbus_dict_append_string_array(
550 &iter_dict
, "eap", (const char **) eap_methods
,
553 /* free returned method array */
554 while (eap_methods
[i
])
555 free(eap_methods
[i
++]);
562 res
= wpa_drv_get_capa(wpa_s
, &capa
);
564 /***** pairwise cipher */
567 const char *args
[] = {"CCMP", "TKIP", "NONE"};
568 if (!wpa_dbus_dict_append_string_array(
569 &iter_dict
, "pairwise", args
,
570 sizeof(args
) / sizeof(char*)))
574 if (!wpa_dbus_dict_begin_string_array(&iter_dict
, "pairwise",
580 if (capa
.enc
& WPA_DRIVER_CAPA_ENC_CCMP
) {
581 if (!wpa_dbus_dict_string_array_add_element(
582 &iter_array
, "CCMP"))
586 if (capa
.enc
& WPA_DRIVER_CAPA_ENC_TKIP
) {
587 if (!wpa_dbus_dict_string_array_add_element(
588 &iter_array
, "TKIP"))
592 if (capa
.key_mgmt
& WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE
) {
593 if (!wpa_dbus_dict_string_array_add_element(
594 &iter_array
, "NONE"))
598 if (!wpa_dbus_dict_end_string_array(&iter_dict
,
605 /***** group cipher */
608 const char *args
[] = {
609 "CCMP", "TKIP", "WEP104", "WEP40"
611 if (!wpa_dbus_dict_append_string_array(
612 &iter_dict
, "group", args
,
613 sizeof(args
) / sizeof(char*)))
617 if (!wpa_dbus_dict_begin_string_array(&iter_dict
, "group",
623 if (capa
.enc
& WPA_DRIVER_CAPA_ENC_CCMP
) {
624 if (!wpa_dbus_dict_string_array_add_element(
625 &iter_array
, "CCMP"))
629 if (capa
.enc
& WPA_DRIVER_CAPA_ENC_TKIP
) {
630 if (!wpa_dbus_dict_string_array_add_element(
631 &iter_array
, "TKIP"))
635 if (capa
.enc
& WPA_DRIVER_CAPA_ENC_WEP104
) {
636 if (!wpa_dbus_dict_string_array_add_element(
637 &iter_array
, "WEP104"))
641 if (capa
.enc
& WPA_DRIVER_CAPA_ENC_WEP40
) {
642 if (!wpa_dbus_dict_string_array_add_element(
643 &iter_array
, "WEP40"))
647 if (!wpa_dbus_dict_end_string_array(&iter_dict
,
654 /***** key management */
657 const char *args
[] = {
658 "WPA-PSK", "WPA-EAP", "IEEE8021X", "WPA-NONE",
661 if (!wpa_dbus_dict_append_string_array(
662 &iter_dict
, "key_mgmt", args
,
663 sizeof(args
) / sizeof(char*)))
667 if (!wpa_dbus_dict_begin_string_array(&iter_dict
, "key_mgmt",
673 if (!wpa_dbus_dict_string_array_add_element(&iter_array
,
677 if (!wpa_dbus_dict_string_array_add_element(&iter_array
,
681 if (capa
.key_mgmt
& (WPA_DRIVER_CAPA_KEY_MGMT_WPA
|
682 WPA_DRIVER_CAPA_KEY_MGMT_WPA2
)) {
683 if (!wpa_dbus_dict_string_array_add_element(
684 &iter_array
, "WPA-EAP"))
688 if (capa
.key_mgmt
& (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK
|
689 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK
)) {
690 if (!wpa_dbus_dict_string_array_add_element(
691 &iter_array
, "WPA-PSK"))
695 if (capa
.key_mgmt
& WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE
) {
696 if (!wpa_dbus_dict_string_array_add_element(
697 &iter_array
, "WPA-NONE"))
701 if (!wpa_dbus_dict_end_string_array(&iter_dict
,
708 /***** WPA protocol */
711 const char *args
[] = { "RSN", "WPA" };
712 if (!wpa_dbus_dict_append_string_array(
713 &iter_dict
, "proto", args
,
714 sizeof(args
) / sizeof(char*)))
718 if (!wpa_dbus_dict_begin_string_array(&iter_dict
, "proto",
724 if (capa
.key_mgmt
& (WPA_DRIVER_CAPA_KEY_MGMT_WPA2
|
725 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK
)) {
726 if (!wpa_dbus_dict_string_array_add_element(
731 if (capa
.key_mgmt
& (WPA_DRIVER_CAPA_KEY_MGMT_WPA
|
732 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK
)) {
733 if (!wpa_dbus_dict_string_array_add_element(
738 if (!wpa_dbus_dict_end_string_array(&iter_dict
,
748 const char *args
[] = { "OPEN", "SHARED", "LEAP" };
749 if (!wpa_dbus_dict_append_string_array(
750 &iter_dict
, "auth_alg", args
,
751 sizeof(args
) / sizeof(char*)))
755 if (!wpa_dbus_dict_begin_string_array(&iter_dict
, "auth_alg",
761 if (capa
.auth
& (WPA_DRIVER_AUTH_OPEN
)) {
762 if (!wpa_dbus_dict_string_array_add_element(
763 &iter_array
, "OPEN"))
767 if (capa
.auth
& (WPA_DRIVER_AUTH_SHARED
)) {
768 if (!wpa_dbus_dict_string_array_add_element(
769 &iter_array
, "SHARED"))
773 if (capa
.auth
& (WPA_DRIVER_AUTH_LEAP
)) {
774 if (!wpa_dbus_dict_string_array_add_element(
775 &iter_array
, "LEAP"))
779 if (!wpa_dbus_dict_end_string_array(&iter_dict
,
786 if (!wpa_dbus_dict_close_write(&iter
, &iter_dict
))
793 dbus_message_unref(reply
);
794 return dbus_message_new_error(message
, WPAS_ERROR_INTERNAL_ERROR
,
795 "an internal error occurred returning "
796 "interface capabilities.");
801 * wpas_dbus_iface_add_network - Add a new configured network
802 * @message: Pointer to incoming dbus message
803 * @wpa_s: wpa_supplicant structure for a network interface
804 * Returns: A dbus message containing the object path of the new network
806 * Handler function for "addNetwork" method call of a network interface.
808 DBusMessage
* wpas_dbus_iface_add_network(DBusMessage
*message
,
809 struct wpa_supplicant
*wpa_s
)
811 DBusMessage
*reply
= NULL
;
812 struct wpa_ssid
*ssid
;
815 path
= os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX
);
817 perror("wpas_dbus_iface_scan_results[dbus]: out of "
819 wpa_printf(MSG_ERROR
, "dbus control interface: not "
820 "enough memory to send scan results "
825 ssid
= wpa_config_add_network(wpa_s
->conf
);
827 reply
= dbus_message_new_error(message
,
828 WPAS_ERROR_ADD_NETWORK_ERROR
,
829 "wpa_supplicant could not add "
830 "a network on this interface.");
834 wpa_config_set_network_defaults(ssid
);
836 /* Construct the object path for this network. */
837 snprintf(path
, WPAS_DBUS_OBJECT_PATH_MAX
,
838 "%s/" WPAS_DBUS_NETWORKS_PART
"/%d",
839 wpa_supplicant_get_dbus_path(wpa_s
),
842 reply
= dbus_message_new_method_return(message
);
843 dbus_message_append_args(reply
, DBUS_TYPE_OBJECT_PATH
,
844 &path
, DBUS_TYPE_INVALID
);
853 * wpas_dbus_iface_remove_network - Remove a configured network
854 * @message: Pointer to incoming dbus message
855 * @wpa_s: wpa_supplicant structure for a network interface
856 * Returns: A dbus message containing a UINT32 indicating success (1) or
859 * Handler function for "removeNetwork" method call of a network interface.
861 DBusMessage
* wpas_dbus_iface_remove_network(DBusMessage
*message
,
862 struct wpa_supplicant
*wpa_s
)
864 DBusMessage
*reply
= NULL
;
866 char *iface
= NULL
, *net_id
= NULL
;
868 struct wpa_ssid
*ssid
;
870 if (!dbus_message_get_args(message
, NULL
,
871 DBUS_TYPE_OBJECT_PATH
, &op
,
872 DBUS_TYPE_INVALID
)) {
873 reply
= wpas_dbus_new_invalid_opts_error(message
, NULL
);
877 /* Extract the network ID */
878 iface
= wpas_dbus_decompose_object_path(op
, &net_id
, NULL
);
880 reply
= wpas_dbus_new_invalid_network_error(message
);
883 /* Ensure the network is actually a child of this interface */
884 if (strcmp(iface
, wpa_supplicant_get_dbus_path(wpa_s
)) != 0) {
885 reply
= wpas_dbus_new_invalid_network_error(message
);
889 id
= strtoul(net_id
, NULL
, 10);
890 ssid
= wpa_config_get_network(wpa_s
->conf
, id
);
892 reply
= wpas_dbus_new_invalid_network_error(message
);
896 if (wpa_config_remove_network(wpa_s
->conf
, id
) < 0) {
897 reply
= dbus_message_new_error(message
,
898 WPAS_ERROR_REMOVE_NETWORK_ERROR
,
899 "error removing the specified "
900 "on this interface.");
904 if (ssid
== wpa_s
->current_ssid
)
905 wpa_supplicant_disassociate(wpa_s
, WLAN_REASON_DEAUTH_LEAVING
);
906 reply
= wpas_dbus_new_success_reply(message
);
915 static const char *dont_quote
[] = {
916 "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
917 "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
921 static dbus_bool_t
should_quote_opt(const char *key
)
924 while (dont_quote
[i
] != NULL
) {
925 if (strcmp(key
, dont_quote
[i
]) == 0)
933 * wpas_dbus_iface_set_network - Set options for a configured network
934 * @message: Pointer to incoming dbus message
935 * @wpa_s: wpa_supplicant structure for a network interface
936 * @ssid: wpa_ssid structure for a configured network
937 * Returns: a dbus message containing a UINT32 indicating success (1) or
940 * Handler function for "set" method call of a configured network.
942 DBusMessage
* wpas_dbus_iface_set_network(DBusMessage
*message
,
943 struct wpa_supplicant
*wpa_s
,
944 struct wpa_ssid
*ssid
)
946 DBusMessage
*reply
= NULL
;
947 struct wpa_dbus_dict_entry entry
= { .type
= DBUS_TYPE_STRING
};
948 DBusMessageIter iter
, iter_dict
;
950 dbus_message_iter_init(message
, &iter
);
952 if (!wpa_dbus_dict_open_read(&iter
, &iter_dict
)) {
953 reply
= wpas_dbus_new_invalid_opts_error(message
, NULL
);
957 while (wpa_dbus_dict_has_dict_entry(&iter_dict
)) {
962 if (!wpa_dbus_dict_get_entry(&iter_dict
, &entry
)) {
963 reply
= wpas_dbus_new_invalid_opts_error(message
,
968 /* Type conversions, since wpa_supplicant wants strings */
969 if (entry
.type
== DBUS_TYPE_ARRAY
&&
970 entry
.array_type
== DBUS_TYPE_BYTE
) {
971 if (entry
.array_len
<= 0)
974 size
= entry
.array_len
* 2 + 1;
975 value
= os_zalloc(size
);
978 ret
= wpa_snprintf_hex(value
, size
,
979 (u8
*) entry
.bytearray_value
,
983 } else if (entry
.type
== DBUS_TYPE_STRING
) {
984 if (should_quote_opt(entry
.key
)) {
985 size
= strlen(entry
.str_value
);
986 /* Zero-length option check */
989 size
+= 3; /* For quotes and terminator */
990 value
= os_zalloc(size
);
993 ret
= snprintf(value
, size
, "\"%s\"",
995 if (ret
< 0 || (size_t) ret
!= (size
- 1))
998 value
= strdup(entry
.str_value
);
1002 } else if (entry
.type
== DBUS_TYPE_UINT32
) {
1003 value
= os_zalloc(size
);
1006 ret
= snprintf(value
, size
, "%u", entry
.uint32_value
);
1009 } else if (entry
.type
== DBUS_TYPE_INT32
) {
1010 value
= os_zalloc(size
);
1013 ret
= snprintf(value
, size
, "%d", entry
.int32_value
);
1019 if (wpa_config_set(ssid
, entry
.key
, value
, 0) < 0)
1022 if ((strcmp(entry
.key
, "psk") == 0 &&
1023 value
[0] == '"' && ssid
->ssid_len
) ||
1024 (strcmp(entry
.key
, "ssid") == 0 && ssid
->passphrase
))
1025 wpa_config_update_psk(ssid
);
1028 wpa_dbus_dict_entry_clear(&entry
);
1033 reply
= wpas_dbus_new_invalid_opts_error(message
, entry
.key
);
1034 wpa_dbus_dict_entry_clear(&entry
);
1039 reply
= wpas_dbus_new_success_reply(message
);
1047 * wpas_dbus_iface_enable_network - Mark a configured network as enabled
1048 * @message: Pointer to incoming dbus message
1049 * @wpa_s: wpa_supplicant structure for a network interface
1050 * @ssid: wpa_ssid structure for a configured network
1051 * Returns: A dbus message containing a UINT32 indicating success (1) or
1054 * Handler function for "enable" method call of a configured network.
1056 DBusMessage
* wpas_dbus_iface_enable_network(DBusMessage
*message
,
1057 struct wpa_supplicant
*wpa_s
,
1058 struct wpa_ssid
*ssid
)
1060 if (wpa_s
->current_ssid
== NULL
&& ssid
->disabled
) {
1062 * Try to reassociate since there is no current configuration
1063 * and a new network was made available.
1065 wpa_s
->reassociate
= 1;
1066 wpa_supplicant_req_scan(wpa_s
, 0, 0);
1070 return wpas_dbus_new_success_reply(message
);
1075 * wpas_dbus_iface_disable_network - Mark a configured network as disabled
1076 * @message: Pointer to incoming dbus message
1077 * @wpa_s: wpa_supplicant structure for a network interface
1078 * @ssid: wpa_ssid structure for a configured network
1079 * Returns: A dbus message containing a UINT32 indicating success (1) or
1082 * Handler function for "disable" method call of a configured network.
1084 DBusMessage
* wpas_dbus_iface_disable_network(DBusMessage
*message
,
1085 struct wpa_supplicant
*wpa_s
,
1086 struct wpa_ssid
*ssid
)
1088 if (ssid
== wpa_s
->current_ssid
)
1089 wpa_supplicant_disassociate(wpa_s
, WLAN_REASON_DEAUTH_LEAVING
);
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 (strlen(dbus_message_get_signature(message
)) == 0) {
1116 ssid
= wpa_s
->conf
->ssid
;
1121 wpa_s
->reassociate
= 1;
1122 wpa_supplicant_req_scan(wpa_s
, 0, 0);
1124 const char *obj_path
;
1127 if (!dbus_message_get_args(message
, NULL
,
1128 DBUS_TYPE_OBJECT_PATH
, &op
,
1129 DBUS_TYPE_INVALID
)) {
1130 reply
= wpas_dbus_new_invalid_opts_error(message
,
1135 /* Extract the network number */
1136 iface_obj_path
= wpas_dbus_decompose_object_path(op
,
1139 if (iface_obj_path
== NULL
) {
1140 reply
= wpas_dbus_new_invalid_iface_error(message
);
1143 /* Ensure the object path really points to this interface */
1144 obj_path
= wpa_supplicant_get_dbus_path(wpa_s
);
1145 if (strcmp(iface_obj_path
, obj_path
) != 0) {
1146 reply
= wpas_dbus_new_invalid_network_error(message
);
1150 nid
= strtoul(network
, NULL
, 10);
1151 if (errno
== EINVAL
) {
1152 reply
= wpas_dbus_new_invalid_network_error(message
);
1156 ssid
= wpa_config_get_network(wpa_s
->conf
, nid
);
1158 reply
= wpas_dbus_new_invalid_network_error(message
);
1162 /* Finally, associate with the network */
1163 if (ssid
!= wpa_s
->current_ssid
&& wpa_s
->current_ssid
)
1164 wpa_supplicant_disassociate(
1165 wpa_s
, WLAN_REASON_DEAUTH_LEAVING
);
1167 /* Mark all other networks disabled and trigger reassociation
1169 ssid
= wpa_s
->conf
->ssid
;
1171 ssid
->disabled
= (nid
!= ssid
->id
);
1174 wpa_s
->disconnected
= 0;
1175 wpa_s
->reassociate
= 1;
1176 wpa_supplicant_req_scan(wpa_s
, 0, 0);
1179 reply
= wpas_dbus_new_success_reply(message
);
1182 free(iface_obj_path
);
1189 * wpas_dbus_iface_disconnect - Terminate the current connection
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 "disconnect" method call of network interface.
1197 DBusMessage
* wpas_dbus_iface_disconnect(DBusMessage
*message
,
1198 struct wpa_supplicant
*wpa_s
)
1200 wpa_s
->disconnected
= 1;
1201 wpa_supplicant_disassociate(wpa_s
, WLAN_REASON_DEAUTH_LEAVING
);
1203 return wpas_dbus_new_success_reply(message
);
1208 * wpas_dbus_iface_set_ap_scan - Control roaming mode
1209 * @message: Pointer to incoming dbus message
1210 * @wpa_s: wpa_supplicant structure for a network interface
1211 * Returns: A dbus message containing a UINT32 indicating success (1) or
1214 * Handler function for "setAPScan" method call.
1216 DBusMessage
* wpas_dbus_iface_set_ap_scan(DBusMessage
*message
,
1217 struct wpa_supplicant
*wpa_s
)
1219 DBusMessage
*reply
= NULL
;
1220 dbus_uint32_t ap_scan
= 1;
1222 if (!dbus_message_get_args(message
, NULL
, DBUS_TYPE_UINT32
, &ap_scan
,
1223 DBUS_TYPE_INVALID
)) {
1224 reply
= wpas_dbus_new_invalid_opts_error(message
, NULL
);
1229 reply
= wpas_dbus_new_invalid_opts_error(message
, NULL
);
1232 wpa_s
->conf
->ap_scan
= ap_scan
;
1233 reply
= wpas_dbus_new_success_reply(message
);
1241 * wpas_dbus_iface_set_smartcard_modules - Set smartcard related module paths
1242 * @message: Pointer to incoming dbus message
1243 * @wpa_s: wpa_supplicant structure for a network interface
1244 * Returns: A dbus message containing a UINT32 indicating success (1) or
1247 * Handler function for "setSmartcardModules" method call.
1249 DBusMessage
* wpas_dbus_iface_set_smartcard_modules(
1250 DBusMessage
*message
, struct wpa_supplicant
*wpa_s
)
1252 DBusMessageIter iter
, iter_dict
;
1253 char *opensc_engine_path
= NULL
;
1254 char *pkcs11_engine_path
= NULL
;
1255 char *pkcs11_module_path
= NULL
;
1256 struct wpa_dbus_dict_entry entry
;
1258 if (!dbus_message_iter_init(message
, &iter
))
1261 if (!wpa_dbus_dict_open_read(&iter
, &iter_dict
))
1264 while (wpa_dbus_dict_has_dict_entry(&iter_dict
)) {
1265 if (!wpa_dbus_dict_get_entry(&iter_dict
, &entry
))
1267 if (!strcmp(entry
.key
, "opensc_engine_path") &&
1268 (entry
.type
== DBUS_TYPE_STRING
)) {
1269 opensc_engine_path
= os_strdup(entry
.str_value
);
1270 if (opensc_engine_path
== NULL
)
1272 } else if (!strcmp(entry
.key
, "pkcs11_engine_path") &&
1273 (entry
.type
== DBUS_TYPE_STRING
)) {
1274 pkcs11_engine_path
= os_strdup(entry
.str_value
);
1275 if (pkcs11_engine_path
== NULL
)
1277 } else if (!strcmp(entry
.key
, "pkcs11_module_path") &&
1278 (entry
.type
== DBUS_TYPE_STRING
)) {
1279 pkcs11_module_path
= os_strdup(entry
.str_value
);
1280 if (pkcs11_module_path
== NULL
)
1283 wpa_dbus_dict_entry_clear(&entry
);
1286 wpa_dbus_dict_entry_clear(&entry
);
1289 #ifdef EAP_TLS_OPENSSL
1290 os_free(wpa_s
->conf
->opensc_engine_path
);
1291 wpa_s
->conf
->opensc_engine_path
= opensc_engine_path
;
1292 os_free(wpa_s
->conf
->pkcs11_engine_path
);
1293 wpa_s
->conf
->pkcs11_engine_path
= pkcs11_engine_path
;
1294 os_free(wpa_s
->conf
->pkcs11_module_path
);
1295 wpa_s
->conf
->pkcs11_module_path
= pkcs11_module_path
;
1296 #endif /* EAP_TLS_OPENSSL */
1298 wpa_sm_set_eapol(wpa_s
->wpa
, NULL
);
1299 eapol_sm_deinit(wpa_s
->eapol
);
1300 wpa_s
->eapol
= NULL
;
1301 wpa_supplicant_init_eapol(wpa_s
);
1302 wpa_sm_set_eapol(wpa_s
->wpa
, wpa_s
->eapol
);
1304 return wpas_dbus_new_success_reply(message
);
1307 os_free(opensc_engine_path
);
1308 os_free(pkcs11_engine_path
);
1309 os_free(pkcs11_module_path
);
1310 return wpas_dbus_new_invalid_opts_error(message
, NULL
);
1314 * wpas_dbus_iface_get_state - Get interface state
1315 * @message: Pointer to incoming dbus message
1316 * @wpa_s: wpa_supplicant structure for a network interface
1317 * Returns: A dbus message containing a STRING representing the current
1320 * Handler function for "state" method call.
1322 DBusMessage
* wpas_dbus_iface_get_state(DBusMessage
*message
,
1323 struct wpa_supplicant
*wpa_s
)
1325 DBusMessage
*reply
= NULL
;
1326 const char *str_state
;
1328 reply
= dbus_message_new_method_return(message
);
1329 if (reply
!= NULL
) {
1330 str_state
= wpa_supplicant_state_txt(wpa_s
->wpa_state
);
1331 dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &str_state
,
1340 * wpas_dbus_iface_get_scanning - Get interface scanning state
1341 * @message: Pointer to incoming dbus message
1342 * @wpa_s: wpa_supplicant structure for a network interface
1343 * Returns: A dbus message containing whether the interface is scanning
1345 * Handler function for "scanning" method call.
1347 DBusMessage
* wpas_dbus_iface_get_scanning(DBusMessage
*message
,
1348 struct wpa_supplicant
*wpa_s
)
1350 DBusMessage
*reply
= NULL
;
1351 dbus_bool_t scanning
= wpa_s
->scanning
? TRUE
: FALSE
;
1353 reply
= dbus_message_new_method_return(message
);
1354 if (reply
!= NULL
) {
1355 dbus_message_append_args(reply
, DBUS_TYPE_BOOLEAN
, &scanning
,
1358 perror("wpas_dbus_iface_get_scanning[dbus]: out of "
1360 wpa_printf(MSG_ERROR
, "dbus control interface: not enough "
1361 "memory to return scanning state.");
1369 * wpas_dbus_iface_set_blobs - Store named binary blobs (ie, for certificates)
1370 * @message: Pointer to incoming dbus message
1371 * @wpa_s: %wpa_supplicant data structure
1372 * Returns: A dbus message containing a UINT32 indicating success (1) or
1375 * Asks wpa_supplicant to internally store a one or more binary blobs.
1377 DBusMessage
* wpas_dbus_iface_set_blobs(DBusMessage
*message
,
1378 struct wpa_supplicant
*wpa_s
)
1380 DBusMessage
*reply
= NULL
;
1381 struct wpa_dbus_dict_entry entry
= { .type
= DBUS_TYPE_STRING
};
1382 DBusMessageIter iter
, iter_dict
;
1384 dbus_message_iter_init(message
, &iter
);
1386 if (!wpa_dbus_dict_open_read(&iter
, &iter_dict
))
1387 return wpas_dbus_new_invalid_opts_error(message
, NULL
);
1389 while (wpa_dbus_dict_has_dict_entry(&iter_dict
)) {
1390 struct wpa_config_blob
*blob
;
1392 if (!wpa_dbus_dict_get_entry(&iter_dict
, &entry
)) {
1393 reply
= wpas_dbus_new_invalid_opts_error(message
,
1398 if (entry
.type
!= DBUS_TYPE_ARRAY
||
1399 entry
.array_type
!= DBUS_TYPE_BYTE
) {
1400 reply
= wpas_dbus_new_invalid_opts_error(
1401 message
, "Byte array expected.");
1405 if ((entry
.array_len
<= 0) || (entry
.array_len
> 65536) ||
1406 !strlen(entry
.key
)) {
1407 reply
= wpas_dbus_new_invalid_opts_error(
1408 message
, "Invalid array size.");
1412 blob
= os_zalloc(sizeof(*blob
));
1414 reply
= dbus_message_new_error(
1415 message
, WPAS_ERROR_ADD_ERROR
,
1416 "Not enough memory to add blob.");
1419 blob
->data
= os_zalloc(entry
.array_len
);
1420 if (blob
->data
== NULL
) {
1421 reply
= dbus_message_new_error(
1422 message
, WPAS_ERROR_ADD_ERROR
,
1423 "Not enough memory to add blob data.");
1428 blob
->name
= os_strdup(entry
.key
);
1429 blob
->len
= entry
.array_len
;
1430 os_memcpy(blob
->data
, (u8
*) entry
.bytearray_value
,
1432 if (blob
->name
== NULL
|| blob
->data
== NULL
) {
1433 wpa_config_free_blob(blob
);
1434 reply
= dbus_message_new_error(
1435 message
, WPAS_ERROR_ADD_ERROR
,
1436 "Error adding blob.");
1441 wpa_config_remove_blob(wpa_s
->conf
, blob
->name
);
1442 wpa_config_set_blob(wpa_s
->conf
, blob
);
1443 wpa_dbus_dict_entry_clear(&entry
);
1445 wpa_dbus_dict_entry_clear(&entry
);
1447 return reply
? reply
: wpas_dbus_new_success_reply(message
);
1452 * wpas_dbus_iface_remove_blob - Remove named binary blobs
1453 * @message: Pointer to incoming dbus message
1454 * @wpa_s: %wpa_supplicant data structure
1455 * Returns: A dbus message containing a UINT32 indicating success (1) or
1458 * Asks wpa_supplicant to remove one or more previously stored binary blobs.
1460 DBusMessage
* wpas_dbus_iface_remove_blobs(DBusMessage
*message
,
1461 struct wpa_supplicant
*wpa_s
)
1463 DBusMessageIter iter
, array
;
1464 char *err_msg
= NULL
;
1466 dbus_message_iter_init(message
, &iter
);
1468 if ((dbus_message_iter_get_arg_type (&iter
) != DBUS_TYPE_ARRAY
) ||
1469 (dbus_message_iter_get_element_type (&iter
) != DBUS_TYPE_STRING
))
1470 return wpas_dbus_new_invalid_opts_error(message
, NULL
);
1472 dbus_message_iter_recurse(&iter
, &array
);
1473 while (dbus_message_iter_get_arg_type(&array
) == DBUS_TYPE_STRING
) {
1476 dbus_message_iter_get_basic(&array
, &name
);
1478 err_msg
= "Invalid blob name.";
1480 if (wpa_config_remove_blob(wpa_s
->conf
, name
) != 0)
1481 err_msg
= "Error removing blob.";
1482 dbus_message_iter_next(&array
);
1486 return dbus_message_new_error(message
, WPAS_ERROR_REMOVE_ERROR
,
1490 return wpas_dbus_new_success_reply(message
);