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"
28 #include "wps_supplicant.h"
30 extern int wpa_debug_level;
31 extern int wpa_debug_show_keys;
32 extern int wpa_debug_timestamp;
35 * wpas_dbus_new_invalid_opts_error - Return a new invalid options error message
36 * @message: Pointer to incoming dbus message this error refers to
37 * Returns: a dbus error message
39 * Convenience function to create and return an invalid options error
41 static DBusMessage * wpas_dbus_new_invalid_opts_error(DBusMessage *message,
46 reply = dbus_message_new_error(message, WPAS_ERROR_INVALID_OPTS,
47 "Did not receive correct message "
50 dbus_message_append_args(reply, DBUS_TYPE_STRING, &arg,
58 * wpas_dbus_new_success_reply - Return a new success reply message
59 * @message: Pointer to incoming dbus message this reply refers to
60 * Returns: a dbus message containing a single UINT32 that indicates
61 * success (ie, a value of 1)
63 * Convenience function to create and return a success reply message
65 static DBusMessage * wpas_dbus_new_success_reply(DBusMessage *message)
68 unsigned int success = 1;
70 reply = dbus_message_new_method_return(message);
71 dbus_message_append_args(reply, DBUS_TYPE_UINT32, &success,
77 static void wpas_dbus_free_wpa_interface(struct wpa_interface *iface)
79 free((char *) iface->driver);
80 free((char *) iface->driver_param);
81 free((char *) iface->confname);
82 free((char *) iface->bridge_ifname);
87 * wpas_dbus_global_add_interface - Request registration of a network interface
88 * @message: Pointer to incoming dbus message
89 * @global: %wpa_supplicant global data structure
90 * Returns: The object path of the new interface object,
91 * or a dbus error message with more information
93 * Handler function for "addInterface" method call. Handles requests
94 * by dbus clients to register a network interface that wpa_supplicant
97 DBusMessage * wpas_dbus_global_add_interface(DBusMessage *message,
98 struct wpa_global *global)
100 struct wpa_interface iface;
102 DBusMessage *reply = NULL;
103 DBusMessageIter iter;
105 memset(&iface, 0, sizeof(iface));
107 dbus_message_iter_init(message, &iter);
109 /* First argument: interface name (DBUS_TYPE_STRING)
110 * Required; must be non-zero length
112 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
114 dbus_message_iter_get_basic(&iter, &ifname);
117 iface.ifname = ifname;
119 /* Second argument: dict of options */
120 if (dbus_message_iter_next(&iter)) {
121 DBusMessageIter iter_dict;
122 struct wpa_dbus_dict_entry entry;
124 if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
126 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
127 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
129 if (!strcmp(entry.key, "driver") &&
130 (entry.type == DBUS_TYPE_STRING)) {
131 iface.driver = strdup(entry.str_value);
132 if (iface.driver == NULL)
134 } else if (!strcmp(entry.key, "driver-params") &&
135 (entry.type == DBUS_TYPE_STRING)) {
136 iface.driver_param = strdup(entry.str_value);
137 if (iface.driver_param == NULL)
139 } else if (!strcmp(entry.key, "config-file") &&
140 (entry.type == DBUS_TYPE_STRING)) {
141 iface.confname = strdup(entry.str_value);
142 if (iface.confname == NULL)
144 } else if (!strcmp(entry.key, "bridge-ifname") &&
145 (entry.type == DBUS_TYPE_STRING)) {
146 iface.bridge_ifname = strdup(entry.str_value);
147 if (iface.bridge_ifname == NULL)
150 wpa_dbus_dict_entry_clear(&entry);
153 wpa_dbus_dict_entry_clear(&entry);
158 * Try to get the wpa_supplicant record for this iface, return
159 * an error if we already control it.
161 if (wpa_supplicant_get_iface(global, iface.ifname) != NULL) {
162 reply = dbus_message_new_error(message,
163 WPAS_ERROR_EXISTS_ERROR,
164 "wpa_supplicant already "
165 "controls this interface.");
167 struct wpa_supplicant *wpa_s;
168 /* Otherwise, have wpa_supplicant attach to it. */
169 if ((wpa_s = wpa_supplicant_add_iface(global, &iface))) {
170 const char *path = wpa_supplicant_get_dbus_path(wpa_s);
171 reply = dbus_message_new_method_return(message);
172 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
173 &path, DBUS_TYPE_INVALID);
175 reply = dbus_message_new_error(message,
176 WPAS_ERROR_ADD_ERROR,
178 "couldn't grab this "
182 wpas_dbus_free_wpa_interface(&iface);
186 wpas_dbus_free_wpa_interface(&iface);
187 return wpas_dbus_new_invalid_opts_error(message, NULL);
192 * wpas_dbus_global_remove_interface - Request deregistration of an interface
193 * @message: Pointer to incoming dbus message
194 * @global: wpa_supplicant global data structure
195 * Returns: a dbus message containing a UINT32 indicating success (1) or
196 * failure (0), or returns a dbus error message with more information
198 * Handler function for "removeInterface" method call. Handles requests
199 * by dbus clients to deregister a network interface that wpa_supplicant
202 DBusMessage * wpas_dbus_global_remove_interface(DBusMessage *message,
203 struct wpa_global *global)
205 struct wpa_supplicant *wpa_s;
207 DBusMessage *reply = NULL;
209 if (!dbus_message_get_args(message, NULL,
210 DBUS_TYPE_OBJECT_PATH, &path,
211 DBUS_TYPE_INVALID)) {
212 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
216 wpa_s = wpa_supplicant_get_iface_by_dbus_path(global, path);
218 reply = wpas_dbus_new_invalid_iface_error(message);
222 if (!wpa_supplicant_remove_iface(global, wpa_s)) {
223 reply = wpas_dbus_new_success_reply(message);
225 reply = dbus_message_new_error(message,
226 WPAS_ERROR_REMOVE_ERROR,
227 "wpa_supplicant couldn't "
228 "remove this interface.");
237 * wpas_dbus_global_get_interface - Get the object path for an interface name
238 * @message: Pointer to incoming dbus message
239 * @global: %wpa_supplicant global data structure
240 * Returns: The object path of the interface object,
241 * or a dbus error message with more information
243 * Handler function for "getInterface" method call. Handles requests
244 * by dbus clients for the object path of an specific network interface.
246 DBusMessage * wpas_dbus_global_get_interface(DBusMessage *message,
247 struct wpa_global *global)
249 DBusMessage *reply = NULL;
252 struct wpa_supplicant *wpa_s;
254 if (!dbus_message_get_args(message, NULL,
255 DBUS_TYPE_STRING, &ifname,
256 DBUS_TYPE_INVALID)) {
257 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
261 wpa_s = wpa_supplicant_get_iface(global, ifname);
263 reply = wpas_dbus_new_invalid_iface_error(message);
267 path = wpa_supplicant_get_dbus_path(wpa_s);
269 reply = dbus_message_new_error(message,
270 WPAS_ERROR_INTERNAL_ERROR,
271 "an internal error occurred "
272 "getting the interface.");
276 reply = dbus_message_new_method_return(message);
277 dbus_message_append_args(reply,
278 DBUS_TYPE_OBJECT_PATH, &path,
286 * wpas_dbus_global_set_debugparams- Set the debug params
287 * @message: Pointer to incoming dbus message
288 * @global: %wpa_supplicant global data structure
289 * Returns: a dbus message containing a UINT32 indicating success (1) or
290 * failure (0), or returns a dbus error message with more information
292 * Handler function for "setDebugParams" method call. Handles requests
293 * by dbus clients for the object path of an specific network interface.
295 DBusMessage * wpas_dbus_global_set_debugparams(DBusMessage *message,
296 struct wpa_global *global)
298 DBusMessage *reply = NULL;
300 dbus_bool_t debug_timestamp;
301 dbus_bool_t debug_show_keys;
303 if (!dbus_message_get_args(message, NULL,
304 DBUS_TYPE_INT32, &debug_level,
305 DBUS_TYPE_BOOLEAN, &debug_timestamp,
306 DBUS_TYPE_BOOLEAN, &debug_show_keys,
307 DBUS_TYPE_INVALID)) {
308 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
312 /* check for allowed debuglevels */
313 if (debug_level != MSG_MSGDUMP &&
314 debug_level != MSG_DEBUG &&
315 debug_level != MSG_INFO &&
316 debug_level != MSG_WARNING &&
317 debug_level != MSG_ERROR) {
318 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
322 wpa_debug_level = debug_level;
323 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
324 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
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 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 free(eap_methods[i++]);
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.");
838 wpa_config_set_network_defaults(ssid);
840 /* Construct the object path for this network. */
841 snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
842 "%s/" WPAS_DBUS_NETWORKS_PART "/%d",
843 wpa_supplicant_get_dbus_path(wpa_s),
846 reply = dbus_message_new_method_return(message);
847 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
848 &path, DBUS_TYPE_INVALID);
857 * wpas_dbus_iface_remove_network - Remove a configured network
858 * @message: Pointer to incoming dbus message
859 * @wpa_s: wpa_supplicant structure for a network interface
860 * Returns: A dbus message containing a UINT32 indicating success (1) or
863 * Handler function for "removeNetwork" method call of a network interface.
865 DBusMessage * wpas_dbus_iface_remove_network(DBusMessage *message,
866 struct wpa_supplicant *wpa_s)
868 DBusMessage *reply = NULL;
870 char *iface = NULL, *net_id = NULL;
872 struct wpa_ssid *ssid;
874 if (!dbus_message_get_args(message, NULL,
875 DBUS_TYPE_OBJECT_PATH, &op,
876 DBUS_TYPE_INVALID)) {
877 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
881 /* Extract the network ID */
882 iface = wpas_dbus_decompose_object_path(op, &net_id, NULL);
884 reply = wpas_dbus_new_invalid_network_error(message);
887 /* Ensure the network is actually a child of this interface */
888 if (strcmp(iface, wpa_supplicant_get_dbus_path(wpa_s)) != 0) {
889 reply = wpas_dbus_new_invalid_network_error(message);
893 id = strtoul(net_id, NULL, 10);
894 ssid = wpa_config_get_network(wpa_s->conf, id);
896 reply = wpas_dbus_new_invalid_network_error(message);
900 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
901 reply = dbus_message_new_error(message,
902 WPAS_ERROR_REMOVE_NETWORK_ERROR,
903 "error removing the specified "
904 "on this interface.");
908 if (ssid == wpa_s->current_ssid)
909 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
910 reply = wpas_dbus_new_success_reply(message);
919 static const char *dont_quote[] = {
920 "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
921 "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
925 static dbus_bool_t should_quote_opt(const char *key)
928 while (dont_quote[i] != NULL) {
929 if (strcmp(key, dont_quote[i]) == 0)
937 * wpas_dbus_iface_set_network - Set options for a configured network
938 * @message: Pointer to incoming dbus message
939 * @wpa_s: wpa_supplicant structure for a network interface
940 * @ssid: wpa_ssid structure for a configured network
941 * Returns: a dbus message containing a UINT32 indicating success (1) or
944 * Handler function for "set" method call of a configured network.
946 DBusMessage * wpas_dbus_iface_set_network(DBusMessage *message,
947 struct wpa_supplicant *wpa_s,
948 struct wpa_ssid *ssid)
950 DBusMessage *reply = NULL;
951 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
952 DBusMessageIter iter, iter_dict;
954 dbus_message_iter_init(message, &iter);
956 if (!wpa_dbus_dict_open_read(&iter, &iter_dict)) {
957 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
961 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
966 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
967 reply = wpas_dbus_new_invalid_opts_error(message,
972 /* Type conversions, since wpa_supplicant wants strings */
973 if (entry.type == DBUS_TYPE_ARRAY &&
974 entry.array_type == DBUS_TYPE_BYTE) {
975 if (entry.array_len <= 0)
978 size = entry.array_len * 2 + 1;
979 value = os_zalloc(size);
982 ret = wpa_snprintf_hex(value, size,
983 (u8 *) entry.bytearray_value,
987 } else if (entry.type == DBUS_TYPE_STRING) {
988 if (should_quote_opt(entry.key)) {
989 size = strlen(entry.str_value);
990 /* Zero-length option check */
993 size += 3; /* For quotes and terminator */
994 value = os_zalloc(size);
997 ret = snprintf(value, size, "\"%s\"",
999 if (ret < 0 || (size_t) ret != (size - 1))
1002 value = strdup(entry.str_value);
1006 } else if (entry.type == DBUS_TYPE_UINT32) {
1007 value = os_zalloc(size);
1010 ret = snprintf(value, size, "%u", entry.uint32_value);
1013 } else if (entry.type == DBUS_TYPE_INT32) {
1014 value = os_zalloc(size);
1017 ret = snprintf(value, size, "%d", entry.int32_value);
1023 if (wpa_config_set(ssid, entry.key, value, 0) < 0)
1026 if ((strcmp(entry.key, "psk") == 0 &&
1027 value[0] == '"' && ssid->ssid_len) ||
1028 (strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
1029 wpa_config_update_psk(ssid);
1032 wpa_dbus_dict_entry_clear(&entry);
1037 reply = wpas_dbus_new_invalid_opts_error(message, entry.key);
1038 wpa_dbus_dict_entry_clear(&entry);
1043 reply = wpas_dbus_new_success_reply(message);
1051 * wpas_dbus_iface_enable_network - Mark a configured network as enabled
1052 * @message: Pointer to incoming dbus message
1053 * @wpa_s: wpa_supplicant structure for a network interface
1054 * @ssid: wpa_ssid structure for a configured network
1055 * Returns: A dbus message containing a UINT32 indicating success (1) or
1058 * Handler function for "enable" method call of a configured network.
1060 DBusMessage * wpas_dbus_iface_enable_network(DBusMessage *message,
1061 struct wpa_supplicant *wpa_s,
1062 struct wpa_ssid *ssid)
1064 if (wpa_s->current_ssid == NULL && ssid->disabled) {
1066 * Try to reassociate since there is no current configuration
1067 * and a new network was made available.
1069 wpa_s->reassociate = 1;
1070 wpa_supplicant_req_scan(wpa_s, 0, 0);
1074 return wpas_dbus_new_success_reply(message);
1079 * wpas_dbus_iface_disable_network - Mark a configured network as disabled
1080 * @message: Pointer to incoming dbus message
1081 * @wpa_s: wpa_supplicant structure for a network interface
1082 * @ssid: wpa_ssid structure for a configured network
1083 * Returns: A dbus message containing a UINT32 indicating success (1) or
1086 * Handler function for "disable" method call of a configured network.
1088 DBusMessage * wpas_dbus_iface_disable_network(DBusMessage *message,
1089 struct wpa_supplicant *wpa_s,
1090 struct wpa_ssid *ssid)
1092 if (ssid == wpa_s->current_ssid)
1093 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1096 return wpas_dbus_new_success_reply(message);
1101 * wpas_dbus_iface_select_network - Attempt association with a configured network
1102 * @message: Pointer to incoming dbus message
1103 * @wpa_s: wpa_supplicant structure for a network interface
1104 * Returns: A dbus message containing a UINT32 indicating success (1) or
1107 * Handler function for "selectNetwork" method call of network interface.
1109 DBusMessage * wpas_dbus_iface_select_network(DBusMessage *message,
1110 struct wpa_supplicant *wpa_s)
1112 DBusMessage *reply = NULL;
1114 struct wpa_ssid *ssid;
1115 char *iface_obj_path = NULL;
1116 char *network = NULL;
1118 if (strlen(dbus_message_get_signature(message)) == 0) {
1120 ssid = wpa_s->conf->ssid;
1125 wpa_s->reassociate = 1;
1126 wpa_supplicant_req_scan(wpa_s, 0, 0);
1128 const char *obj_path;
1131 if (!dbus_message_get_args(message, NULL,
1132 DBUS_TYPE_OBJECT_PATH, &op,
1133 DBUS_TYPE_INVALID)) {
1134 reply = wpas_dbus_new_invalid_opts_error(message,
1139 /* Extract the network number */
1140 iface_obj_path = wpas_dbus_decompose_object_path(op,
1143 if (iface_obj_path == NULL) {
1144 reply = wpas_dbus_new_invalid_iface_error(message);
1147 /* Ensure the object path really points to this interface */
1148 obj_path = wpa_supplicant_get_dbus_path(wpa_s);
1149 if (strcmp(iface_obj_path, obj_path) != 0) {
1150 reply = wpas_dbus_new_invalid_network_error(message);
1154 nid = strtoul(network, NULL, 10);
1155 if (errno == EINVAL) {
1156 reply = wpas_dbus_new_invalid_network_error(message);
1160 ssid = wpa_config_get_network(wpa_s->conf, nid);
1162 reply = wpas_dbus_new_invalid_network_error(message);
1166 /* Finally, associate with the network */
1167 if (ssid != wpa_s->current_ssid && wpa_s->current_ssid)
1168 wpa_supplicant_disassociate(
1169 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1171 /* Mark all other networks disabled and trigger reassociation
1173 ssid = wpa_s->conf->ssid;
1175 ssid->disabled = (nid != ssid->id);
1178 wpa_s->disconnected = 0;
1179 wpa_s->reassociate = 1;
1180 wpa_supplicant_req_scan(wpa_s, 0, 0);
1183 reply = wpas_dbus_new_success_reply(message);
1186 free(iface_obj_path);
1193 * wpas_dbus_iface_disconnect - Terminate the current connection
1194 * @message: Pointer to incoming dbus message
1195 * @wpa_s: wpa_supplicant structure for a network interface
1196 * Returns: A dbus message containing a UINT32 indicating success (1) or
1199 * Handler function for "disconnect" method call of network interface.
1201 DBusMessage * wpas_dbus_iface_disconnect(DBusMessage *message,
1202 struct wpa_supplicant *wpa_s)
1204 wpa_s->disconnected = 1;
1205 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1207 return wpas_dbus_new_success_reply(message);
1212 * wpas_dbus_iface_set_ap_scan - Control roaming mode
1213 * @message: Pointer to incoming dbus message
1214 * @wpa_s: wpa_supplicant structure for a network interface
1215 * Returns: A dbus message containing a UINT32 indicating success (1) or
1218 * Handler function for "setAPScan" method call.
1220 DBusMessage * wpas_dbus_iface_set_ap_scan(DBusMessage *message,
1221 struct wpa_supplicant *wpa_s)
1223 DBusMessage *reply = NULL;
1224 dbus_uint32_t ap_scan = 1;
1226 if (!dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &ap_scan,
1227 DBUS_TYPE_INVALID)) {
1228 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
1233 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
1236 wpa_s->conf->ap_scan = ap_scan;
1237 reply = wpas_dbus_new_success_reply(message);
1245 * wpas_dbus_iface_set_smartcard_modules - Set smartcard related module paths
1246 * @message: Pointer to incoming dbus message
1247 * @wpa_s: wpa_supplicant structure for a network interface
1248 * Returns: A dbus message containing a UINT32 indicating success (1) or
1251 * Handler function for "setSmartcardModules" method call.
1253 DBusMessage * wpas_dbus_iface_set_smartcard_modules(
1254 DBusMessage *message, struct wpa_supplicant *wpa_s)
1256 DBusMessageIter iter, iter_dict;
1257 char *opensc_engine_path = NULL;
1258 char *pkcs11_engine_path = NULL;
1259 char *pkcs11_module_path = NULL;
1260 struct wpa_dbus_dict_entry entry;
1262 if (!dbus_message_iter_init(message, &iter))
1265 if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
1268 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
1269 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
1271 if (!strcmp(entry.key, "opensc_engine_path") &&
1272 (entry.type == DBUS_TYPE_STRING)) {
1273 opensc_engine_path = os_strdup(entry.str_value);
1274 if (opensc_engine_path == NULL)
1276 } else if (!strcmp(entry.key, "pkcs11_engine_path") &&
1277 (entry.type == DBUS_TYPE_STRING)) {
1278 pkcs11_engine_path = os_strdup(entry.str_value);
1279 if (pkcs11_engine_path == NULL)
1281 } else if (!strcmp(entry.key, "pkcs11_module_path") &&
1282 (entry.type == DBUS_TYPE_STRING)) {
1283 pkcs11_module_path = os_strdup(entry.str_value);
1284 if (pkcs11_module_path == NULL)
1287 wpa_dbus_dict_entry_clear(&entry);
1290 wpa_dbus_dict_entry_clear(&entry);
1293 #ifdef EAP_TLS_OPENSSL
1294 os_free(wpa_s->conf->opensc_engine_path);
1295 wpa_s->conf->opensc_engine_path = opensc_engine_path;
1296 os_free(wpa_s->conf->pkcs11_engine_path);
1297 wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path;
1298 os_free(wpa_s->conf->pkcs11_module_path);
1299 wpa_s->conf->pkcs11_module_path = pkcs11_module_path;
1300 #endif /* EAP_TLS_OPENSSL */
1302 eapol_sm_deinit(wpa_s->eapol);
1303 wpa_supplicant_init_eapol(wpa_s);
1305 return wpas_dbus_new_success_reply(message);
1308 os_free(opensc_engine_path);
1309 os_free(pkcs11_engine_path);
1310 os_free(pkcs11_module_path);
1311 return wpas_dbus_new_invalid_opts_error(message, NULL);
1315 * wpas_dbus_iface_get_state - Get interface state
1316 * @message: Pointer to incoming dbus message
1317 * @wpa_s: wpa_supplicant structure for a network interface
1318 * Returns: A dbus message containing a STRING representing the current
1321 * Handler function for "state" method call.
1323 DBusMessage * wpas_dbus_iface_get_state(DBusMessage *message,
1324 struct wpa_supplicant *wpa_s)
1326 DBusMessage *reply = NULL;
1327 const char *str_state;
1329 reply = dbus_message_new_method_return(message);
1330 if (reply != NULL) {
1331 str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
1332 dbus_message_append_args(reply, DBUS_TYPE_STRING, &str_state,
1341 * wpas_dbus_iface_get_scanning - Get interface scanning state
1342 * @message: Pointer to incoming dbus message
1343 * @wpa_s: wpa_supplicant structure for a network interface
1344 * Returns: A dbus message containing whether the interface is scanning
1346 * Handler function for "scanning" method call.
1348 DBusMessage * wpas_dbus_iface_get_scanning(DBusMessage *message,
1349 struct wpa_supplicant *wpa_s)
1351 DBusMessage *reply = NULL;
1352 dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
1354 reply = dbus_message_new_method_return(message);
1355 if (reply != NULL) {
1356 dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &scanning,
1359 perror("wpas_dbus_iface_get_scanning[dbus]: out of "
1361 wpa_printf(MSG_ERROR, "dbus control interface: not enough "
1362 "memory to return scanning state.");
1370 * wpas_dbus_iface_set_blobs - Store named binary blobs (ie, for certificates)
1371 * @message: Pointer to incoming dbus message
1372 * @wpa_s: %wpa_supplicant data structure
1373 * Returns: A dbus message containing a UINT32 indicating success (1) or
1376 * Asks wpa_supplicant to internally store a one or more binary blobs.
1378 DBusMessage * wpas_dbus_iface_set_blobs(DBusMessage *message,
1379 struct wpa_supplicant *wpa_s)
1381 DBusMessage *reply = NULL;
1382 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
1383 DBusMessageIter iter, iter_dict;
1385 dbus_message_iter_init(message, &iter);
1387 if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
1388 return wpas_dbus_new_invalid_opts_error(message, NULL);
1390 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
1391 struct wpa_config_blob *blob;
1393 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
1394 reply = wpas_dbus_new_invalid_opts_error(message,
1399 if (entry.type != DBUS_TYPE_ARRAY ||
1400 entry.array_type != DBUS_TYPE_BYTE) {
1401 reply = wpas_dbus_new_invalid_opts_error(
1402 message, "Byte array expected.");
1406 if ((entry.array_len <= 0) || (entry.array_len > 65536) ||
1407 !strlen(entry.key)) {
1408 reply = wpas_dbus_new_invalid_opts_error(
1409 message, "Invalid array size.");
1413 blob = os_zalloc(sizeof(*blob));
1415 reply = dbus_message_new_error(
1416 message, WPAS_ERROR_ADD_ERROR,
1417 "Not enough memory to add blob.");
1420 blob->data = os_zalloc(entry.array_len);
1421 if (blob->data == NULL) {
1422 reply = dbus_message_new_error(
1423 message, WPAS_ERROR_ADD_ERROR,
1424 "Not enough memory to add blob data.");
1429 blob->name = os_strdup(entry.key);
1430 blob->len = entry.array_len;
1431 os_memcpy(blob->data, (u8 *) entry.bytearray_value,
1433 if (blob->name == NULL || blob->data == NULL) {
1434 wpa_config_free_blob(blob);
1435 reply = dbus_message_new_error(
1436 message, WPAS_ERROR_ADD_ERROR,
1437 "Error adding blob.");
1442 wpa_config_remove_blob(wpa_s->conf, blob->name);
1443 wpa_config_set_blob(wpa_s->conf, blob);
1444 wpa_dbus_dict_entry_clear(&entry);
1446 wpa_dbus_dict_entry_clear(&entry);
1448 return reply ? reply : wpas_dbus_new_success_reply(message);
1453 * wpas_dbus_iface_remove_blob - Remove named binary blobs
1454 * @message: Pointer to incoming dbus message
1455 * @wpa_s: %wpa_supplicant data structure
1456 * Returns: A dbus message containing a UINT32 indicating success (1) or
1459 * Asks wpa_supplicant to remove one or more previously stored binary blobs.
1461 DBusMessage * wpas_dbus_iface_remove_blobs(DBusMessage *message,
1462 struct wpa_supplicant *wpa_s)
1464 DBusMessageIter iter, array;
1465 char *err_msg = NULL;
1467 dbus_message_iter_init(message, &iter);
1469 if ((dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY) ||
1470 (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_STRING))
1471 return wpas_dbus_new_invalid_opts_error(message, NULL);
1473 dbus_message_iter_recurse(&iter, &array);
1474 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
1477 dbus_message_iter_get_basic(&array, &name);
1479 err_msg = "Invalid blob name.";
1481 if (wpa_config_remove_blob(wpa_s->conf, name) != 0)
1482 err_msg = "Error removing blob.";
1483 dbus_message_iter_next(&array);
1487 return dbus_message_new_error(message, WPAS_ERROR_REMOVE_ERROR,
1491 return wpas_dbus_new_success_reply(message);
1498 * wpas_dbus_iface_wps_pbc - Request credentials using WPS PBC method
1499 * @message: Pointer to incoming dbus message
1500 * @wpa_s: %wpa_supplicant data structure
1501 * Returns: A dbus message containing a UINT32 indicating success (1) or
1504 * Handler function for "wpsPbc" method call
1506 DBusMessage * wpas_dbus_iface_wps_pbc(DBusMessage *message,
1507 struct wpa_supplicant *wpa_s)
1509 char *arg_bssid = NULL;
1513 if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg_bssid,
1515 return wpas_dbus_new_invalid_opts_error(message, NULL);
1517 if (!os_strcmp(arg_bssid, "any"))
1518 ret = wpas_wps_start_pbc(wpa_s, NULL);
1519 else if (!hwaddr_aton(arg_bssid, bssid))
1520 ret = wpas_wps_start_pbc(wpa_s, bssid);
1522 return wpas_dbus_new_invalid_opts_error(message,
1527 return dbus_message_new_error(message,
1528 WPAS_ERROR_WPS_PBC_ERROR,
1529 "Could not start PBC "
1533 return wpas_dbus_new_success_reply(message);
1538 * wpas_dbus_iface_wps_pin - Establish the PIN number of the enrollee
1539 * @message: Pointer to incoming dbus message
1540 * @wpa_s: %wpa_supplicant data structure
1541 * Returns: A dbus message containing a UINT32 indicating success (1) or
1544 * Handler function for "wpsPin" method call
1546 DBusMessage * wpas_dbus_iface_wps_pin(DBusMessage *message,
1547 struct wpa_supplicant *wpa_s)
1549 DBusMessage *reply = NULL;
1552 u8 bssid[ETH_ALEN], *_bssid = NULL;
1555 if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg_bssid,
1556 DBUS_TYPE_STRING, &pin, DBUS_TYPE_INVALID))
1557 return wpas_dbus_new_invalid_opts_error(message, NULL);
1559 if (!os_strcmp(arg_bssid, "any"))
1561 else if (!hwaddr_aton(arg_bssid, bssid))
1564 return wpas_dbus_new_invalid_opts_error(message,
1568 if (os_strlen(pin) > 0)
1569 ret = wpas_wps_start_pin(wpa_s, _bssid, pin);
1571 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL);
1574 return dbus_message_new_error(message,
1575 WPAS_ERROR_WPS_PIN_ERROR,
1576 "Could not init PIN");
1579 reply = dbus_message_new_method_return(message);
1584 dbus_message_append_args(reply, DBUS_TYPE_STRING, &pin,
1588 sprintf(npin, "%08d", ret);
1589 dbus_message_append_args(reply, DBUS_TYPE_STRING, &npin,
1597 * wpas_dbus_iface_wps_reg - Request credentials using the PIN of the AP
1598 * @message: Pointer to incoming dbus message
1599 * @wpa_s: %wpa_supplicant data structure
1600 * Returns: A dbus message containing a UINT32 indicating success (1) or
1603 * Handler function for "wpsReg" method call
1605 DBusMessage * wpas_dbus_iface_wps_reg(DBusMessage *message,
1606 struct wpa_supplicant *wpa_s)
1613 if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg_bssid,
1614 DBUS_TYPE_STRING, &pin, DBUS_TYPE_INVALID))
1615 return wpas_dbus_new_invalid_opts_error(message, NULL);
1617 if (!os_strcmp(arg_bssid, "any"))
1618 ret = wpas_wps_start_reg(wpa_s, NULL, pin);
1619 else if (!hwaddr_aton(arg_bssid, bssid))
1620 ret = wpas_wps_start_reg(wpa_s, bssid, pin);
1622 return wpas_dbus_new_invalid_opts_error(message,
1627 return dbus_message_new_error(message,
1628 WPAS_ERROR_WPS_PBC_ERROR,
1629 "Could not request credentials");
1632 return wpas_dbus_new_success_reply(message);
1635 #endif /* CONFIG_WPS */