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"
21 #include "ctrl_iface_dbus.h"
22 #include "ctrl_iface_dbus_handlers.h"
23 #include "eap_peer/eap_methods.h"
24 #include "dbus_dict_helpers.h"
25 #include "ieee802_11_defs.h"
26 #include "wpas_glue.h"
27 #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 (!(res->flags & WPA_SCAN_QUAL_INVALID) &&
489 !wpa_dbus_dict_append_int32(&iter_dict, "quality", res->qual))
491 if (!(res->flags & WPA_SCAN_NOISE_INVALID) &&
492 !wpa_dbus_dict_append_int32(&iter_dict, "noise", res->noise))
494 if (!(res->flags & WPA_SCAN_LEVEL_INVALID) &&
495 !wpa_dbus_dict_append_int32(&iter_dict, "level", res->level))
497 if (!wpa_dbus_dict_append_int32(&iter_dict, "maxrate",
498 wpa_scan_get_max_rate(res) * 500000))
501 if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
508 dbus_message_unref(reply);
509 return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
510 "an internal error occurred returning "
511 "BSSID properties.");
516 * wpas_dbus_iface_capabilities - Return interface capabilities
517 * @message: Pointer to incoming dbus message
518 * @wpa_s: wpa_supplicant structure for a network interface
519 * Returns: A dbus message containing a dict of strings
521 * Handler function for "capabilities" method call of an interface.
523 DBusMessage * wpas_dbus_iface_capabilities(DBusMessage *message,
524 struct wpa_supplicant *wpa_s)
526 DBusMessage *reply = NULL;
527 struct wpa_driver_capa capa;
529 DBusMessageIter iter, iter_dict;
532 dbus_bool_t strict = FALSE;
533 DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
535 if (!dbus_message_get_args(message, NULL,
536 DBUS_TYPE_BOOLEAN, &strict,
540 reply = dbus_message_new_method_return(message);
542 dbus_message_iter_init_append(reply, &iter);
543 if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
547 eap_methods = eap_get_names_as_string_array(&num_items);
549 dbus_bool_t success = FALSE;
552 success = wpa_dbus_dict_append_string_array(
553 &iter_dict, "eap", (const char **) eap_methods,
556 /* free returned method array */
557 while (eap_methods[i])
558 free(eap_methods[i++]);
565 res = wpa_drv_get_capa(wpa_s, &capa);
567 /***** pairwise cipher */
570 const char *args[] = {"CCMP", "TKIP", "NONE"};
571 if (!wpa_dbus_dict_append_string_array(
572 &iter_dict, "pairwise", args,
573 sizeof(args) / sizeof(char*)))
577 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "pairwise",
583 if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
584 if (!wpa_dbus_dict_string_array_add_element(
585 &iter_array, "CCMP"))
589 if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
590 if (!wpa_dbus_dict_string_array_add_element(
591 &iter_array, "TKIP"))
595 if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
596 if (!wpa_dbus_dict_string_array_add_element(
597 &iter_array, "NONE"))
601 if (!wpa_dbus_dict_end_string_array(&iter_dict,
608 /***** group cipher */
611 const char *args[] = {
612 "CCMP", "TKIP", "WEP104", "WEP40"
614 if (!wpa_dbus_dict_append_string_array(
615 &iter_dict, "group", args,
616 sizeof(args) / sizeof(char*)))
620 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "group",
626 if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
627 if (!wpa_dbus_dict_string_array_add_element(
628 &iter_array, "CCMP"))
632 if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
633 if (!wpa_dbus_dict_string_array_add_element(
634 &iter_array, "TKIP"))
638 if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) {
639 if (!wpa_dbus_dict_string_array_add_element(
640 &iter_array, "WEP104"))
644 if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) {
645 if (!wpa_dbus_dict_string_array_add_element(
646 &iter_array, "WEP40"))
650 if (!wpa_dbus_dict_end_string_array(&iter_dict,
657 /***** key management */
660 const char *args[] = {
661 "WPA-PSK", "WPA-EAP", "IEEE8021X", "WPA-NONE",
664 if (!wpa_dbus_dict_append_string_array(
665 &iter_dict, "key_mgmt", args,
666 sizeof(args) / sizeof(char*)))
670 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "key_mgmt",
676 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
680 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
684 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
685 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
686 if (!wpa_dbus_dict_string_array_add_element(
687 &iter_array, "WPA-EAP"))
691 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
692 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
693 if (!wpa_dbus_dict_string_array_add_element(
694 &iter_array, "WPA-PSK"))
698 if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
699 if (!wpa_dbus_dict_string_array_add_element(
700 &iter_array, "WPA-NONE"))
704 if (!wpa_dbus_dict_end_string_array(&iter_dict,
711 /***** WPA protocol */
714 const char *args[] = { "RSN", "WPA" };
715 if (!wpa_dbus_dict_append_string_array(
716 &iter_dict, "proto", args,
717 sizeof(args) / sizeof(char*)))
721 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "proto",
727 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
728 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
729 if (!wpa_dbus_dict_string_array_add_element(
734 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
735 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
736 if (!wpa_dbus_dict_string_array_add_element(
741 if (!wpa_dbus_dict_end_string_array(&iter_dict,
751 const char *args[] = { "OPEN", "SHARED", "LEAP" };
752 if (!wpa_dbus_dict_append_string_array(
753 &iter_dict, "auth_alg", args,
754 sizeof(args) / sizeof(char*)))
758 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "auth_alg",
764 if (capa.auth & (WPA_DRIVER_AUTH_OPEN)) {
765 if (!wpa_dbus_dict_string_array_add_element(
766 &iter_array, "OPEN"))
770 if (capa.auth & (WPA_DRIVER_AUTH_SHARED)) {
771 if (!wpa_dbus_dict_string_array_add_element(
772 &iter_array, "SHARED"))
776 if (capa.auth & (WPA_DRIVER_AUTH_LEAP)) {
777 if (!wpa_dbus_dict_string_array_add_element(
778 &iter_array, "LEAP"))
782 if (!wpa_dbus_dict_end_string_array(&iter_dict,
789 if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
796 dbus_message_unref(reply);
797 return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
798 "an internal error occurred returning "
799 "interface capabilities.");
804 * wpas_dbus_iface_add_network - Add a new configured network
805 * @message: Pointer to incoming dbus message
806 * @wpa_s: wpa_supplicant structure for a network interface
807 * Returns: A dbus message containing the object path of the new network
809 * Handler function for "addNetwork" method call of a network interface.
811 DBusMessage * wpas_dbus_iface_add_network(DBusMessage *message,
812 struct wpa_supplicant *wpa_s)
814 DBusMessage *reply = NULL;
815 struct wpa_ssid *ssid;
818 path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
820 perror("wpas_dbus_iface_scan_results[dbus]: out of "
822 wpa_printf(MSG_ERROR, "dbus control interface: not "
823 "enough memory to send scan results "
828 ssid = wpa_config_add_network(wpa_s->conf);
830 reply = dbus_message_new_error(message,
831 WPAS_ERROR_ADD_NETWORK_ERROR,
832 "wpa_supplicant could not add "
833 "a network on this interface.");
837 wpa_config_set_network_defaults(ssid);
839 /* Construct the object path for this network. */
840 snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
841 "%s/" WPAS_DBUS_NETWORKS_PART "/%d",
842 wpa_supplicant_get_dbus_path(wpa_s),
845 reply = dbus_message_new_method_return(message);
846 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
847 &path, DBUS_TYPE_INVALID);
856 * wpas_dbus_iface_remove_network - Remove a configured network
857 * @message: Pointer to incoming dbus message
858 * @wpa_s: wpa_supplicant structure for a network interface
859 * Returns: A dbus message containing a UINT32 indicating success (1) or
862 * Handler function for "removeNetwork" method call of a network interface.
864 DBusMessage * wpas_dbus_iface_remove_network(DBusMessage *message,
865 struct wpa_supplicant *wpa_s)
867 DBusMessage *reply = NULL;
869 char *iface = NULL, *net_id = NULL;
871 struct wpa_ssid *ssid;
873 if (!dbus_message_get_args(message, NULL,
874 DBUS_TYPE_OBJECT_PATH, &op,
875 DBUS_TYPE_INVALID)) {
876 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
880 /* Extract the network ID */
881 iface = wpas_dbus_decompose_object_path(op, &net_id, NULL);
883 reply = wpas_dbus_new_invalid_network_error(message);
886 /* Ensure the network is actually a child of this interface */
887 if (strcmp(iface, wpa_supplicant_get_dbus_path(wpa_s)) != 0) {
888 reply = wpas_dbus_new_invalid_network_error(message);
892 id = strtoul(net_id, NULL, 10);
893 ssid = wpa_config_get_network(wpa_s->conf, id);
895 reply = wpas_dbus_new_invalid_network_error(message);
899 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
900 reply = dbus_message_new_error(message,
901 WPAS_ERROR_REMOVE_NETWORK_ERROR,
902 "error removing the specified "
903 "on this interface.");
907 if (ssid == wpa_s->current_ssid)
908 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
909 reply = wpas_dbus_new_success_reply(message);
918 static const char *dont_quote[] = {
919 "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
920 "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
924 static dbus_bool_t should_quote_opt(const char *key)
927 while (dont_quote[i] != NULL) {
928 if (strcmp(key, dont_quote[i]) == 0)
936 * wpas_dbus_iface_set_network - Set options for a configured network
937 * @message: Pointer to incoming dbus message
938 * @wpa_s: wpa_supplicant structure for a network interface
939 * @ssid: wpa_ssid structure for a configured network
940 * Returns: a dbus message containing a UINT32 indicating success (1) or
943 * Handler function for "set" method call of a configured network.
945 DBusMessage * wpas_dbus_iface_set_network(DBusMessage *message,
946 struct wpa_supplicant *wpa_s,
947 struct wpa_ssid *ssid)
949 DBusMessage *reply = NULL;
950 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
951 DBusMessageIter iter, iter_dict;
953 dbus_message_iter_init(message, &iter);
955 if (!wpa_dbus_dict_open_read(&iter, &iter_dict)) {
956 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
960 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
965 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
966 reply = wpas_dbus_new_invalid_opts_error(message,
971 /* Type conversions, since wpa_supplicant wants strings */
972 if (entry.type == DBUS_TYPE_ARRAY &&
973 entry.array_type == DBUS_TYPE_BYTE) {
974 if (entry.array_len <= 0)
977 size = entry.array_len * 2 + 1;
978 value = os_zalloc(size);
981 ret = wpa_snprintf_hex(value, size,
982 (u8 *) entry.bytearray_value,
986 } else if (entry.type == DBUS_TYPE_STRING) {
987 if (should_quote_opt(entry.key)) {
988 size = strlen(entry.str_value);
989 /* Zero-length option check */
992 size += 3; /* For quotes and terminator */
993 value = os_zalloc(size);
996 ret = snprintf(value, size, "\"%s\"",
998 if (ret < 0 || (size_t) ret != (size - 1))
1001 value = strdup(entry.str_value);
1005 } else if (entry.type == DBUS_TYPE_UINT32) {
1006 value = os_zalloc(size);
1009 ret = snprintf(value, size, "%u", entry.uint32_value);
1012 } else if (entry.type == DBUS_TYPE_INT32) {
1013 value = os_zalloc(size);
1016 ret = snprintf(value, size, "%d", entry.int32_value);
1022 if (wpa_config_set(ssid, entry.key, value, 0) < 0)
1025 if ((strcmp(entry.key, "psk") == 0 &&
1026 value[0] == '"' && ssid->ssid_len) ||
1027 (strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
1028 wpa_config_update_psk(ssid);
1031 wpa_dbus_dict_entry_clear(&entry);
1036 reply = wpas_dbus_new_invalid_opts_error(message, entry.key);
1037 wpa_dbus_dict_entry_clear(&entry);
1042 reply = wpas_dbus_new_success_reply(message);
1050 * wpas_dbus_iface_enable_network - Mark a configured network as enabled
1051 * @message: Pointer to incoming dbus message
1052 * @wpa_s: wpa_supplicant structure for a network interface
1053 * @ssid: wpa_ssid structure for a configured network
1054 * Returns: A dbus message containing a UINT32 indicating success (1) or
1057 * Handler function for "enable" method call of a configured network.
1059 DBusMessage * wpas_dbus_iface_enable_network(DBusMessage *message,
1060 struct wpa_supplicant *wpa_s,
1061 struct wpa_ssid *ssid)
1063 if (wpa_s->current_ssid == NULL && ssid->disabled) {
1065 * Try to reassociate since there is no current configuration
1066 * and a new network was made available.
1068 wpa_s->reassociate = 1;
1069 wpa_supplicant_req_scan(wpa_s, 0, 0);
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 if (ssid == wpa_s->current_ssid)
1092 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1095 return wpas_dbus_new_success_reply(message);
1100 * wpas_dbus_iface_select_network - Attempt association with a configured network
1101 * @message: Pointer to incoming dbus message
1102 * @wpa_s: wpa_supplicant structure for a network interface
1103 * Returns: A dbus message containing a UINT32 indicating success (1) or
1106 * Handler function for "selectNetwork" method call of network interface.
1108 DBusMessage * wpas_dbus_iface_select_network(DBusMessage *message,
1109 struct wpa_supplicant *wpa_s)
1111 DBusMessage *reply = NULL;
1113 struct wpa_ssid *ssid;
1114 char *iface_obj_path = NULL;
1115 char *network = NULL;
1117 if (strlen(dbus_message_get_signature(message)) == 0) {
1119 ssid = wpa_s->conf->ssid;
1124 wpa_s->reassociate = 1;
1125 wpa_supplicant_req_scan(wpa_s, 0, 0);
1127 const char *obj_path;
1130 if (!dbus_message_get_args(message, NULL,
1131 DBUS_TYPE_OBJECT_PATH, &op,
1132 DBUS_TYPE_INVALID)) {
1133 reply = wpas_dbus_new_invalid_opts_error(message,
1138 /* Extract the network number */
1139 iface_obj_path = wpas_dbus_decompose_object_path(op,
1142 if (iface_obj_path == NULL) {
1143 reply = wpas_dbus_new_invalid_iface_error(message);
1146 /* Ensure the object path really points to this interface */
1147 obj_path = wpa_supplicant_get_dbus_path(wpa_s);
1148 if (strcmp(iface_obj_path, obj_path) != 0) {
1149 reply = wpas_dbus_new_invalid_network_error(message);
1153 nid = strtoul(network, NULL, 10);
1154 if (errno == EINVAL) {
1155 reply = wpas_dbus_new_invalid_network_error(message);
1159 ssid = wpa_config_get_network(wpa_s->conf, nid);
1161 reply = wpas_dbus_new_invalid_network_error(message);
1165 /* Finally, associate with the network */
1166 if (ssid != wpa_s->current_ssid && wpa_s->current_ssid)
1167 wpa_supplicant_disassociate(
1168 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1170 /* Mark all other networks disabled and trigger reassociation
1172 ssid = wpa_s->conf->ssid;
1174 ssid->disabled = (nid != ssid->id);
1177 wpa_s->disconnected = 0;
1178 wpa_s->reassociate = 1;
1179 wpa_supplicant_req_scan(wpa_s, 0, 0);
1182 reply = wpas_dbus_new_success_reply(message);
1185 free(iface_obj_path);
1192 * wpas_dbus_iface_disconnect - Terminate the current connection
1193 * @message: Pointer to incoming dbus message
1194 * @wpa_s: wpa_supplicant structure for a network interface
1195 * Returns: A dbus message containing a UINT32 indicating success (1) or
1198 * Handler function for "disconnect" method call of network interface.
1200 DBusMessage * wpas_dbus_iface_disconnect(DBusMessage *message,
1201 struct wpa_supplicant *wpa_s)
1203 wpa_s->disconnected = 1;
1204 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1206 return wpas_dbus_new_success_reply(message);
1211 * wpas_dbus_iface_set_ap_scan - Control roaming mode
1212 * @message: Pointer to incoming dbus message
1213 * @wpa_s: wpa_supplicant structure for a network interface
1214 * Returns: A dbus message containing a UINT32 indicating success (1) or
1217 * Handler function for "setAPScan" method call.
1219 DBusMessage * wpas_dbus_iface_set_ap_scan(DBusMessage *message,
1220 struct wpa_supplicant *wpa_s)
1222 DBusMessage *reply = NULL;
1223 dbus_uint32_t ap_scan = 1;
1225 if (!dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &ap_scan,
1226 DBUS_TYPE_INVALID)) {
1227 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
1232 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
1235 wpa_s->conf->ap_scan = ap_scan;
1236 reply = wpas_dbus_new_success_reply(message);
1244 * wpas_dbus_iface_set_smartcard_modules - Set smartcard related module paths
1245 * @message: Pointer to incoming dbus message
1246 * @wpa_s: wpa_supplicant structure for a network interface
1247 * Returns: A dbus message containing a UINT32 indicating success (1) or
1250 * Handler function for "setSmartcardModules" method call.
1252 DBusMessage * wpas_dbus_iface_set_smartcard_modules(
1253 DBusMessage *message, struct wpa_supplicant *wpa_s)
1255 DBusMessageIter iter, iter_dict;
1256 char *opensc_engine_path = NULL;
1257 char *pkcs11_engine_path = NULL;
1258 char *pkcs11_module_path = NULL;
1259 struct wpa_dbus_dict_entry entry;
1261 if (!dbus_message_iter_init(message, &iter))
1264 if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
1267 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
1268 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
1270 if (!strcmp(entry.key, "opensc_engine_path") &&
1271 (entry.type == DBUS_TYPE_STRING)) {
1272 opensc_engine_path = os_strdup(entry.str_value);
1273 if (opensc_engine_path == NULL)
1275 } else if (!strcmp(entry.key, "pkcs11_engine_path") &&
1276 (entry.type == DBUS_TYPE_STRING)) {
1277 pkcs11_engine_path = os_strdup(entry.str_value);
1278 if (pkcs11_engine_path == NULL)
1280 } else if (!strcmp(entry.key, "pkcs11_module_path") &&
1281 (entry.type == DBUS_TYPE_STRING)) {
1282 pkcs11_module_path = os_strdup(entry.str_value);
1283 if (pkcs11_module_path == NULL)
1286 wpa_dbus_dict_entry_clear(&entry);
1289 wpa_dbus_dict_entry_clear(&entry);
1292 #ifdef EAP_TLS_OPENSSL
1293 os_free(wpa_s->conf->opensc_engine_path);
1294 wpa_s->conf->opensc_engine_path = opensc_engine_path;
1295 os_free(wpa_s->conf->pkcs11_engine_path);
1296 wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path;
1297 os_free(wpa_s->conf->pkcs11_module_path);
1298 wpa_s->conf->pkcs11_module_path = pkcs11_module_path;
1299 #endif /* EAP_TLS_OPENSSL */
1301 eapol_sm_deinit(wpa_s->eapol);
1302 wpa_supplicant_init_eapol(wpa_s);
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_set_blobs - Store named binary blobs (ie, for certificates)
1341 * @message: Pointer to incoming dbus message
1342 * @wpa_s: %wpa_supplicant data structure
1343 * Returns: A dbus message containing a UINT32 indicating success (1) or
1346 * Asks wpa_supplicant to internally store a one or more binary blobs.
1348 DBusMessage * wpas_dbus_iface_set_blobs(DBusMessage *message,
1349 struct wpa_supplicant *wpa_s)
1351 DBusMessage *reply = NULL;
1352 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
1353 DBusMessageIter iter, iter_dict;
1355 dbus_message_iter_init(message, &iter);
1357 if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
1358 return wpas_dbus_new_invalid_opts_error(message, NULL);
1360 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
1361 struct wpa_config_blob *blob;
1363 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
1364 reply = wpas_dbus_new_invalid_opts_error(message,
1369 if (entry.type != DBUS_TYPE_ARRAY ||
1370 entry.array_type != DBUS_TYPE_BYTE) {
1371 reply = wpas_dbus_new_invalid_opts_error(
1372 message, "Byte array expected.");
1376 if ((entry.array_len <= 0) || (entry.array_len > 65536) ||
1377 !strlen(entry.key)) {
1378 reply = wpas_dbus_new_invalid_opts_error(
1379 message, "Invalid array size.");
1383 blob = os_zalloc(sizeof(*blob));
1385 reply = dbus_message_new_error(
1386 message, WPAS_ERROR_ADD_ERROR,
1387 "Not enough memory to add blob.");
1390 blob->data = os_zalloc(entry.array_len);
1391 if (blob->data == NULL) {
1392 reply = dbus_message_new_error(
1393 message, WPAS_ERROR_ADD_ERROR,
1394 "Not enough memory to add blob data.");
1399 blob->name = os_strdup(entry.key);
1400 blob->len = entry.array_len;
1401 os_memcpy(blob->data, (u8 *) entry.bytearray_value,
1403 if (blob->name == NULL || blob->data == NULL) {
1404 wpa_config_free_blob(blob);
1405 reply = dbus_message_new_error(
1406 message, WPAS_ERROR_ADD_ERROR,
1407 "Error adding blob.");
1412 wpa_config_remove_blob(wpa_s->conf, blob->name);
1413 wpa_config_set_blob(wpa_s->conf, blob);
1414 wpa_dbus_dict_entry_clear(&entry);
1416 wpa_dbus_dict_entry_clear(&entry);
1418 return reply ? reply : wpas_dbus_new_success_reply(message);
1423 * wpas_dbus_iface_remove_blob - Remove named binary blobs
1424 * @message: Pointer to incoming dbus message
1425 * @wpa_s: %wpa_supplicant data structure
1426 * Returns: A dbus message containing a UINT32 indicating success (1) or
1429 * Asks wpa_supplicant to remove one or more previously stored binary blobs.
1431 DBusMessage * wpas_dbus_iface_remove_blobs(DBusMessage *message,
1432 struct wpa_supplicant *wpa_s)
1434 DBusMessageIter iter, array;
1435 char *err_msg = NULL;
1437 dbus_message_iter_init(message, &iter);
1439 if ((dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY) ||
1440 (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_STRING))
1441 return wpas_dbus_new_invalid_opts_error(message, NULL);
1443 dbus_message_iter_recurse(&iter, &array);
1444 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
1447 dbus_message_iter_get_basic(&array, &name);
1449 err_msg = "Invalid blob name.";
1451 if (wpa_config_remove_blob(wpa_s->conf, name) != 0)
1452 err_msg = "Error removing blob.";
1453 dbus_message_iter_next(&array);
1457 return dbus_message_new_error(message, WPAS_ERROR_REMOVE_ERROR,
1461 return wpas_dbus_new_success_reply(message);