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"
24 #include "eap_peer/eap_methods.h"
25 #include "dbus_dict_helpers.h"
26 #include "ieee802_11_defs.h"
27 #include "wpas_glue.h"
28 #include "eapol_supp/eapol_supp_sm.h"
29 #include "wps_supplicant.h"
32 extern int wpa_debug_level;
33 extern int wpa_debug_show_keys;
34 extern int wpa_debug_timestamp;
37 * wpas_dbus_new_invalid_opts_error - Return a new invalid options error message
38 * @message: Pointer to incoming dbus message this error refers to
39 * Returns: a dbus error message
41 * Convenience function to create and return an invalid options error
43 static DBusMessage * wpas_dbus_new_invalid_opts_error(DBusMessage *message,
48 reply = dbus_message_new_error(message, WPAS_ERROR_INVALID_OPTS,
49 "Did not receive correct message "
52 dbus_message_append_args(reply, DBUS_TYPE_STRING, &arg,
60 * wpas_dbus_new_success_reply - Return a new success reply message
61 * @message: Pointer to incoming dbus message this reply refers to
62 * Returns: a dbus message containing a single UINT32 that indicates
63 * success (ie, a value of 1)
65 * Convenience function to create and return a success reply message
67 static DBusMessage * wpas_dbus_new_success_reply(DBusMessage *message)
70 unsigned int success = 1;
72 reply = dbus_message_new_method_return(message);
73 dbus_message_append_args(reply, DBUS_TYPE_UINT32, &success,
79 static void wpas_dbus_free_wpa_interface(struct wpa_interface *iface)
81 os_free((char *) iface->driver);
82 os_free((char *) iface->driver_param);
83 os_free((char *) iface->confname);
84 os_free((char *) iface->bridge_ifname);
89 * wpas_dbus_global_add_interface - Request registration of a network interface
90 * @message: Pointer to incoming dbus message
91 * @global: %wpa_supplicant global data structure
92 * Returns: The object path of the new interface object,
93 * or a dbus error message with more information
95 * Handler function for "addInterface" method call. Handles requests
96 * by dbus clients to register a network interface that wpa_supplicant
99 DBusMessage * wpas_dbus_global_add_interface(DBusMessage *message,
100 struct wpa_global *global)
102 struct wpa_interface iface;
104 DBusMessage *reply = NULL;
105 DBusMessageIter iter;
107 os_memset(&iface, 0, sizeof(iface));
109 dbus_message_iter_init(message, &iter);
111 /* First argument: interface name (DBUS_TYPE_STRING)
112 * Required; must be non-zero length
114 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
116 dbus_message_iter_get_basic(&iter, &ifname);
117 if (!os_strlen(ifname))
119 iface.ifname = ifname;
121 /* Second argument: dict of options */
122 if (dbus_message_iter_next(&iter)) {
123 DBusMessageIter iter_dict;
124 struct wpa_dbus_dict_entry entry;
126 if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
128 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
129 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
131 if (!strcmp(entry.key, "driver") &&
132 (entry.type == DBUS_TYPE_STRING)) {
133 iface.driver = strdup(entry.str_value);
134 if (iface.driver == NULL)
136 } else if (!strcmp(entry.key, "driver-params") &&
137 (entry.type == DBUS_TYPE_STRING)) {
138 iface.driver_param = strdup(entry.str_value);
139 if (iface.driver_param == NULL)
141 } else if (!strcmp(entry.key, "config-file") &&
142 (entry.type == DBUS_TYPE_STRING)) {
143 iface.confname = strdup(entry.str_value);
144 if (iface.confname == NULL)
146 } else if (!strcmp(entry.key, "bridge-ifname") &&
147 (entry.type == DBUS_TYPE_STRING)) {
148 iface.bridge_ifname = strdup(entry.str_value);
149 if (iface.bridge_ifname == NULL)
152 wpa_dbus_dict_entry_clear(&entry);
155 wpa_dbus_dict_entry_clear(&entry);
160 * Try to get the wpa_supplicant record for this iface, return
161 * an error if we already control it.
163 if (wpa_supplicant_get_iface(global, iface.ifname) != NULL) {
164 reply = dbus_message_new_error(message,
165 WPAS_ERROR_EXISTS_ERROR,
166 "wpa_supplicant already "
167 "controls this interface.");
169 struct wpa_supplicant *wpa_s;
170 /* Otherwise, have wpa_supplicant attach to it. */
171 if ((wpa_s = wpa_supplicant_add_iface(global, &iface))) {
172 const char *path = wpa_supplicant_get_dbus_path(wpa_s);
173 reply = dbus_message_new_method_return(message);
174 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
175 &path, DBUS_TYPE_INVALID);
177 reply = dbus_message_new_error(message,
178 WPAS_ERROR_ADD_ERROR,
180 "couldn't grab this "
184 wpas_dbus_free_wpa_interface(&iface);
188 wpas_dbus_free_wpa_interface(&iface);
189 return wpas_dbus_new_invalid_opts_error(message, NULL);
194 * wpas_dbus_global_remove_interface - Request deregistration of an interface
195 * @message: Pointer to incoming dbus message
196 * @global: wpa_supplicant global data structure
197 * Returns: a dbus message containing a UINT32 indicating success (1) or
198 * failure (0), or returns a dbus error message with more information
200 * Handler function for "removeInterface" method call. Handles requests
201 * by dbus clients to deregister a network interface that wpa_supplicant
204 DBusMessage * wpas_dbus_global_remove_interface(DBusMessage *message,
205 struct wpa_global *global)
207 struct wpa_supplicant *wpa_s;
209 DBusMessage *reply = NULL;
211 if (!dbus_message_get_args(message, NULL,
212 DBUS_TYPE_OBJECT_PATH, &path,
213 DBUS_TYPE_INVALID)) {
214 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
218 wpa_s = wpa_supplicant_get_iface_by_dbus_path(global, path);
220 reply = wpas_dbus_new_invalid_iface_error(message);
224 if (!wpa_supplicant_remove_iface(global, wpa_s)) {
225 reply = wpas_dbus_new_success_reply(message);
227 reply = dbus_message_new_error(message,
228 WPAS_ERROR_REMOVE_ERROR,
229 "wpa_supplicant couldn't "
230 "remove this interface.");
239 * wpas_dbus_global_get_interface - Get the object path for an interface name
240 * @message: Pointer to incoming dbus message
241 * @global: %wpa_supplicant global data structure
242 * Returns: The object path of the interface object,
243 * or a dbus error message with more information
245 * Handler function for "getInterface" method call. Handles requests
246 * by dbus clients for the object path of an specific network interface.
248 DBusMessage * wpas_dbus_global_get_interface(DBusMessage *message,
249 struct wpa_global *global)
251 DBusMessage *reply = NULL;
254 struct wpa_supplicant *wpa_s;
256 if (!dbus_message_get_args(message, NULL,
257 DBUS_TYPE_STRING, &ifname,
258 DBUS_TYPE_INVALID)) {
259 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
263 wpa_s = wpa_supplicant_get_iface(global, ifname);
265 reply = wpas_dbus_new_invalid_iface_error(message);
269 path = wpa_supplicant_get_dbus_path(wpa_s);
271 reply = dbus_message_new_error(message,
272 WPAS_ERROR_INTERNAL_ERROR,
273 "an internal error occurred "
274 "getting the interface.");
278 reply = dbus_message_new_method_return(message);
279 dbus_message_append_args(reply,
280 DBUS_TYPE_OBJECT_PATH, &path,
289 * wpas_dbus_global_set_debugparams- Set the debug params
290 * @message: Pointer to incoming dbus message
291 * @global: %wpa_supplicant global data structure
292 * Returns: a dbus message containing a UINT32 indicating success (1) or
293 * failure (0), or returns a dbus error message with more information
295 * Handler function for "setDebugParams" method call. Handles requests
296 * by dbus clients for the object path of an specific network interface.
298 DBusMessage * wpas_dbus_global_set_debugparams(DBusMessage *message,
299 struct wpa_global *global)
301 DBusMessage *reply = NULL;
303 dbus_bool_t debug_timestamp;
304 dbus_bool_t debug_show_keys;
306 if (!dbus_message_get_args(message, NULL,
307 DBUS_TYPE_INT32, &debug_level,
308 DBUS_TYPE_BOOLEAN, &debug_timestamp,
309 DBUS_TYPE_BOOLEAN, &debug_show_keys,
310 DBUS_TYPE_INVALID)) {
311 return wpas_dbus_new_invalid_opts_error(message, NULL);
314 if (wpa_supplicant_set_debug_params(global, debug_level,
315 debug_timestamp ? 1 : 0,
316 debug_show_keys ? 1 : 0)) {
317 return wpas_dbus_new_invalid_opts_error(message, NULL);
320 reply = wpas_dbus_new_success_reply(message);
327 * wpas_dbus_iface_scan - Request a wireless scan on an interface
328 * @message: Pointer to incoming dbus message
329 * @wpa_s: wpa_supplicant structure for a network interface
330 * Returns: a dbus message containing a UINT32 indicating success (1) or
333 * Handler function for "scan" method call of a network device. Requests
334 * that wpa_supplicant perform a wireless scan as soon as possible
335 * on a particular wireless interface.
337 DBusMessage * wpas_dbus_iface_scan(DBusMessage *message,
338 struct wpa_supplicant *wpa_s)
341 wpa_supplicant_req_scan(wpa_s, 0, 0);
342 return wpas_dbus_new_success_reply(message);
347 * wpas_dbus_iface_scan_results - Get the results of a recent scan request
348 * @message: Pointer to incoming dbus message
349 * @wpa_s: wpa_supplicant structure for a network interface
350 * Returns: a dbus message containing a dbus array of objects paths, or returns
351 * a dbus error message if not scan results could be found
353 * Handler function for "scanResults" method call of a network device. Returns
354 * a dbus message containing the object paths of wireless networks found.
356 DBusMessage * wpas_dbus_iface_scan_results(DBusMessage *message,
357 struct wpa_supplicant *wpa_s)
359 DBusMessage *reply = NULL;
360 DBusMessageIter iter;
361 DBusMessageIter sub_iter;
364 /* Ensure we've actually got scan results to return */
365 if (wpa_s->scan_res == NULL &&
366 wpa_supplicant_get_scan_results(wpa_s) < 0) {
367 reply = dbus_message_new_error(message, WPAS_ERROR_SCAN_ERROR,
368 "An error ocurred getting scan "
373 /* Create and initialize the return message */
374 reply = dbus_message_new_method_return(message);
375 dbus_message_iter_init_append(reply, &iter);
376 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
377 DBUS_TYPE_OBJECT_PATH_AS_STRING,
380 /* Loop through scan results and append each result's object path */
381 for (i = 0; i < wpa_s->scan_res->num; i++) {
382 struct wpa_scan_res *res = wpa_s->scan_res->res[i];
385 path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
387 perror("wpas_dbus_iface_scan_results[dbus]: out of "
389 wpa_printf(MSG_ERROR, "dbus control interface: not "
390 "enough memory to send scan results "
394 /* Construct the object path for this network. Note that ':'
395 * is not a valid character in dbus object paths.
397 os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
398 "%s/" WPAS_DBUS_BSSIDS_PART "/"
399 WPAS_DBUS_BSSID_FORMAT,
400 wpa_supplicant_get_dbus_path(wpa_s),
401 MAC2STR(res->bssid));
402 dbus_message_iter_append_basic(&sub_iter,
403 DBUS_TYPE_OBJECT_PATH, &path);
407 dbus_message_iter_close_container(&iter, &sub_iter);
415 * wpas_dbus_bssid_properties - Return the properties of a scanned network
416 * @message: Pointer to incoming dbus message
417 * @wpa_s: wpa_supplicant structure for a network interface
418 * @res: wpa_supplicant scan result for which to get properties
419 * Returns: a dbus message containing the properties for the requested network
421 * Handler function for "properties" method call of a scanned network.
422 * Returns a dbus message containing the the properties.
424 DBusMessage * wpas_dbus_bssid_properties(DBusMessage *message,
425 struct wpa_supplicant *wpa_s,
426 struct wpa_scan_res *res)
428 DBusMessage *reply = NULL;
429 DBusMessageIter iter, iter_dict;
432 /* Dump the properties into a dbus message */
433 reply = dbus_message_new_method_return(message);
435 dbus_message_iter_init_append(reply, &iter);
436 if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
439 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "bssid",
440 (const char *) res->bssid,
444 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
446 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "ssid",
447 (const char *) (ie + 2),
452 ie = wpa_scan_get_vendor_ie(res, WPA_IE_VENDOR_TYPE);
454 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "wpaie",
460 ie = wpa_scan_get_ie(res, WLAN_EID_RSN);
462 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "rsnie",
468 ie = wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE);
470 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "wpsie",
477 if (!wpa_dbus_dict_append_int32(&iter_dict, "frequency",
481 if (!wpa_dbus_dict_append_uint16(&iter_dict, "capabilities",
484 if (!(res->flags & WPA_SCAN_QUAL_INVALID) &&
485 !wpa_dbus_dict_append_int32(&iter_dict, "quality", res->qual))
487 if (!(res->flags & WPA_SCAN_NOISE_INVALID) &&
488 !wpa_dbus_dict_append_int32(&iter_dict, "noise", res->noise))
490 if (!(res->flags & WPA_SCAN_LEVEL_INVALID) &&
491 !wpa_dbus_dict_append_int32(&iter_dict, "level", res->level))
493 if (!wpa_dbus_dict_append_int32(&iter_dict, "maxrate",
494 wpa_scan_get_max_rate(res) * 500000))
497 if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
504 dbus_message_unref(reply);
505 return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
506 "an internal error occurred returning "
507 "BSSID properties.");
512 * wpas_dbus_iface_capabilities - Return interface capabilities
513 * @message: Pointer to incoming dbus message
514 * @wpa_s: wpa_supplicant structure for a network interface
515 * Returns: A dbus message containing a dict of strings
517 * Handler function for "capabilities" method call of an interface.
519 DBusMessage * wpas_dbus_iface_capabilities(DBusMessage *message,
520 struct wpa_supplicant *wpa_s)
522 DBusMessage *reply = NULL;
523 struct wpa_driver_capa capa;
525 DBusMessageIter iter, iter_dict;
528 dbus_bool_t strict = FALSE;
529 DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
531 if (!dbus_message_get_args(message, NULL,
532 DBUS_TYPE_BOOLEAN, &strict,
536 reply = dbus_message_new_method_return(message);
538 dbus_message_iter_init_append(reply, &iter);
539 if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
543 eap_methods = eap_get_names_as_string_array(&num_items);
545 dbus_bool_t success = FALSE;
548 success = wpa_dbus_dict_append_string_array(
549 &iter_dict, "eap", (const char **) eap_methods,
552 /* free returned method array */
553 while (eap_methods[i])
554 os_free(eap_methods[i++]);
555 os_free(eap_methods);
561 res = wpa_drv_get_capa(wpa_s, &capa);
563 /***** pairwise cipher */
566 const char *args[] = {"CCMP", "TKIP", "NONE"};
567 if (!wpa_dbus_dict_append_string_array(
568 &iter_dict, "pairwise", args,
569 sizeof(args) / sizeof(char*)))
573 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "pairwise",
579 if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
580 if (!wpa_dbus_dict_string_array_add_element(
581 &iter_array, "CCMP"))
585 if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
586 if (!wpa_dbus_dict_string_array_add_element(
587 &iter_array, "TKIP"))
591 if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
592 if (!wpa_dbus_dict_string_array_add_element(
593 &iter_array, "NONE"))
597 if (!wpa_dbus_dict_end_string_array(&iter_dict,
604 /***** group cipher */
607 const char *args[] = {
608 "CCMP", "TKIP", "WEP104", "WEP40"
610 if (!wpa_dbus_dict_append_string_array(
611 &iter_dict, "group", args,
612 sizeof(args) / sizeof(char*)))
616 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "group",
622 if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
623 if (!wpa_dbus_dict_string_array_add_element(
624 &iter_array, "CCMP"))
628 if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
629 if (!wpa_dbus_dict_string_array_add_element(
630 &iter_array, "TKIP"))
634 if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) {
635 if (!wpa_dbus_dict_string_array_add_element(
636 &iter_array, "WEP104"))
640 if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) {
641 if (!wpa_dbus_dict_string_array_add_element(
642 &iter_array, "WEP40"))
646 if (!wpa_dbus_dict_end_string_array(&iter_dict,
653 /***** key management */
656 const char *args[] = {
657 "WPA-PSK", "WPA-EAP", "IEEE8021X", "WPA-NONE",
660 if (!wpa_dbus_dict_append_string_array(
661 &iter_dict, "key_mgmt", args,
662 sizeof(args) / sizeof(char*)))
666 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "key_mgmt",
672 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
676 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
680 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
681 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
682 if (!wpa_dbus_dict_string_array_add_element(
683 &iter_array, "WPA-EAP"))
687 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
688 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
689 if (!wpa_dbus_dict_string_array_add_element(
690 &iter_array, "WPA-PSK"))
694 if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
695 if (!wpa_dbus_dict_string_array_add_element(
696 &iter_array, "WPA-NONE"))
700 if (!wpa_dbus_dict_end_string_array(&iter_dict,
707 /***** WPA protocol */
710 const char *args[] = { "RSN", "WPA" };
711 if (!wpa_dbus_dict_append_string_array(
712 &iter_dict, "proto", args,
713 sizeof(args) / sizeof(char*)))
717 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "proto",
723 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
724 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
725 if (!wpa_dbus_dict_string_array_add_element(
730 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
731 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
732 if (!wpa_dbus_dict_string_array_add_element(
737 if (!wpa_dbus_dict_end_string_array(&iter_dict,
747 const char *args[] = { "OPEN", "SHARED", "LEAP" };
748 if (!wpa_dbus_dict_append_string_array(
749 &iter_dict, "auth_alg", args,
750 sizeof(args) / sizeof(char*)))
754 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "auth_alg",
760 if (capa.auth & (WPA_DRIVER_AUTH_OPEN)) {
761 if (!wpa_dbus_dict_string_array_add_element(
762 &iter_array, "OPEN"))
766 if (capa.auth & (WPA_DRIVER_AUTH_SHARED)) {
767 if (!wpa_dbus_dict_string_array_add_element(
768 &iter_array, "SHARED"))
772 if (capa.auth & (WPA_DRIVER_AUTH_LEAP)) {
773 if (!wpa_dbus_dict_string_array_add_element(
774 &iter_array, "LEAP"))
778 if (!wpa_dbus_dict_end_string_array(&iter_dict,
785 if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
792 dbus_message_unref(reply);
793 return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
794 "an internal error occurred returning "
795 "interface capabilities.");
800 * wpas_dbus_iface_add_network - Add a new configured network
801 * @message: Pointer to incoming dbus message
802 * @wpa_s: wpa_supplicant structure for a network interface
803 * Returns: A dbus message containing the object path of the new network
805 * Handler function for "addNetwork" method call of a network interface.
807 DBusMessage * wpas_dbus_iface_add_network(DBusMessage *message,
808 struct wpa_supplicant *wpa_s)
810 DBusMessage *reply = NULL;
811 struct wpa_ssid *ssid;
814 path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
816 perror("wpas_dbus_iface_scan_results[dbus]: out of "
818 wpa_printf(MSG_ERROR, "dbus control interface: not "
819 "enough memory to send scan results "
824 ssid = wpa_config_add_network(wpa_s->conf);
826 reply = dbus_message_new_error(message,
827 WPAS_ERROR_ADD_NETWORK_ERROR,
828 "wpa_supplicant could not add "
829 "a network on this interface.");
832 wpas_notify_network_added(wpa_s, ssid);
834 wpa_config_set_network_defaults(ssid);
836 /* Construct the object path for this network. */
837 os_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);
884 /* Ensure the network is actually a child of this interface */
885 if (strcmp(iface, wpa_supplicant_get_dbus_path(wpa_s)) != 0) {
886 reply = wpas_dbus_new_invalid_network_error(message);
890 id = strtoul(net_id, NULL, 10);
891 ssid = wpa_config_get_network(wpa_s->conf, id);
893 reply = wpas_dbus_new_invalid_network_error(message);
897 wpas_notify_network_removed(wpa_s, ssid);
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",
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)
938 * wpas_dbus_iface_set_network - Set options for a configured network
939 * @message: Pointer to incoming dbus message
940 * @wpa_s: wpa_supplicant structure for a network interface
941 * @ssid: wpa_ssid structure for a configured network
942 * Returns: a dbus message containing a UINT32 indicating success (1) or
945 * Handler function for "set" method call of a configured network.
947 DBusMessage * wpas_dbus_iface_set_network(DBusMessage *message,
948 struct wpa_supplicant *wpa_s,
949 struct wpa_ssid *ssid)
951 DBusMessage *reply = NULL;
952 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
953 DBusMessageIter iter, iter_dict;
955 dbus_message_iter_init(message, &iter);
957 if (!wpa_dbus_dict_open_read(&iter, &iter_dict)) {
958 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
962 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
967 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
968 reply = wpas_dbus_new_invalid_opts_error(message,
973 /* Type conversions, since wpa_supplicant wants strings */
974 if (entry.type == DBUS_TYPE_ARRAY &&
975 entry.array_type == DBUS_TYPE_BYTE) {
976 if (entry.array_len <= 0)
979 size = entry.array_len * 2 + 1;
980 value = os_zalloc(size);
983 ret = wpa_snprintf_hex(value, size,
984 (u8 *) entry.bytearray_value,
988 } else if (entry.type == DBUS_TYPE_STRING) {
989 if (should_quote_opt(entry.key)) {
990 size = os_strlen(entry.str_value);
991 /* Zero-length option check */
994 size += 3; /* For quotes and terminator */
995 value = os_zalloc(size);
998 ret = os_snprintf(value, size, "\"%s\"",
1000 if (ret < 0 || (size_t) ret != (size - 1))
1003 value = os_strdup(entry.str_value);
1007 } else if (entry.type == DBUS_TYPE_UINT32) {
1008 value = os_zalloc(size);
1011 ret = os_snprintf(value, size, "%u",
1012 entry.uint32_value);
1015 } else if (entry.type == DBUS_TYPE_INT32) {
1016 value = os_zalloc(size);
1019 ret = os_snprintf(value, size, "%d",
1026 if (wpa_config_set(ssid, entry.key, value, 0) < 0)
1029 if ((os_strcmp(entry.key, "psk") == 0 &&
1030 value[0] == '"' && ssid->ssid_len) ||
1031 (os_strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
1032 wpa_config_update_psk(ssid);
1035 wpa_dbus_dict_entry_clear(&entry);
1040 reply = wpas_dbus_new_invalid_opts_error(message, entry.key);
1041 wpa_dbus_dict_entry_clear(&entry);
1046 reply = wpas_dbus_new_success_reply(message);
1054 * wpas_dbus_iface_enable_network - Mark a configured network as enabled
1055 * @message: Pointer to incoming dbus message
1056 * @wpa_s: wpa_supplicant structure for a network interface
1057 * @ssid: wpa_ssid structure for a configured network
1058 * Returns: A dbus message containing a UINT32 indicating success (1) or
1061 * Handler function for "enable" method call of a configured network.
1063 DBusMessage * wpas_dbus_iface_enable_network(DBusMessage *message,
1064 struct wpa_supplicant *wpa_s,
1065 struct wpa_ssid *ssid)
1067 wpa_supplicant_enable_network(wpa_s, ssid);
1068 return wpas_dbus_new_success_reply(message);
1073 * wpas_dbus_iface_disable_network - Mark a configured network as disabled
1074 * @message: Pointer to incoming dbus message
1075 * @wpa_s: wpa_supplicant structure for a network interface
1076 * @ssid: wpa_ssid structure for a configured network
1077 * Returns: A dbus message containing a UINT32 indicating success (1) or
1080 * Handler function for "disable" method call of a configured network.
1082 DBusMessage * wpas_dbus_iface_disable_network(DBusMessage *message,
1083 struct wpa_supplicant *wpa_s,
1084 struct wpa_ssid *ssid)
1086 wpa_supplicant_disable_network(wpa_s, ssid);
1087 return wpas_dbus_new_success_reply(message);
1092 * wpas_dbus_iface_select_network - Attempt association with a configured network
1093 * @message: Pointer to incoming dbus message
1094 * @wpa_s: wpa_supplicant structure for a network interface
1095 * Returns: A dbus message containing a UINT32 indicating success (1) or
1098 * Handler function for "selectNetwork" method call of network interface.
1100 DBusMessage * wpas_dbus_iface_select_network(DBusMessage *message,
1101 struct wpa_supplicant *wpa_s)
1103 DBusMessage *reply = NULL;
1105 struct wpa_ssid *ssid;
1106 char *iface_obj_path = NULL;
1107 char *network = NULL;
1109 if (os_strlen(dbus_message_get_signature(message)) == 0) {
1113 const char *obj_path;
1116 if (!dbus_message_get_args(message, NULL,
1117 DBUS_TYPE_OBJECT_PATH, &op,
1118 DBUS_TYPE_INVALID)) {
1119 reply = wpas_dbus_new_invalid_opts_error(message,
1124 /* Extract the network number */
1125 iface_obj_path = wpas_dbus_decompose_object_path(op,
1128 if (iface_obj_path == NULL) {
1129 reply = wpas_dbus_new_invalid_iface_error(message);
1132 /* Ensure the object path really points to this interface */
1133 obj_path = wpa_supplicant_get_dbus_path(wpa_s);
1134 if (os_strcmp(iface_obj_path, obj_path) != 0) {
1135 reply = wpas_dbus_new_invalid_network_error(message);
1139 nid = strtoul(network, NULL, 10);
1140 if (errno == EINVAL) {
1141 reply = wpas_dbus_new_invalid_network_error(message);
1145 ssid = wpa_config_get_network(wpa_s->conf, nid);
1147 reply = wpas_dbus_new_invalid_network_error(message);
1152 /* Finally, associate with the network */
1153 wpa_supplicant_select_network(wpa_s, ssid);
1155 reply = wpas_dbus_new_success_reply(message);
1158 os_free(iface_obj_path);
1165 * wpas_dbus_iface_disconnect - Terminate the current connection
1166 * @message: Pointer to incoming dbus message
1167 * @wpa_s: wpa_supplicant structure for a network interface
1168 * Returns: A dbus message containing a UINT32 indicating success (1) or
1171 * Handler function for "disconnect" method call of network interface.
1173 DBusMessage * wpas_dbus_iface_disconnect(DBusMessage *message,
1174 struct wpa_supplicant *wpa_s)
1176 wpa_s->disconnected = 1;
1177 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1179 return wpas_dbus_new_success_reply(message);
1184 * wpas_dbus_iface_set_ap_scan - Control roaming mode
1185 * @message: Pointer to incoming dbus message
1186 * @wpa_s: wpa_supplicant structure for a network interface
1187 * Returns: A dbus message containing a UINT32 indicating success (1) or
1190 * Handler function for "setAPScan" method call.
1192 DBusMessage * wpas_dbus_iface_set_ap_scan(DBusMessage *message,
1193 struct wpa_supplicant *wpa_s)
1195 DBusMessage *reply = NULL;
1196 dbus_uint32_t ap_scan = 1;
1198 if (!dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &ap_scan,
1199 DBUS_TYPE_INVALID)) {
1200 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
1204 if (wpa_supplicant_set_ap_scan(wpa_s, ap_scan)) {
1205 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
1209 reply = wpas_dbus_new_success_reply(message);
1217 * wpas_dbus_iface_set_smartcard_modules - Set smartcard related module paths
1218 * @message: Pointer to incoming dbus message
1219 * @wpa_s: wpa_supplicant structure for a network interface
1220 * Returns: A dbus message containing a UINT32 indicating success (1) or
1223 * Handler function for "setSmartcardModules" method call.
1225 DBusMessage * wpas_dbus_iface_set_smartcard_modules(
1226 DBusMessage *message, struct wpa_supplicant *wpa_s)
1228 DBusMessageIter iter, iter_dict;
1229 char *opensc_engine_path = NULL;
1230 char *pkcs11_engine_path = NULL;
1231 char *pkcs11_module_path = NULL;
1232 struct wpa_dbus_dict_entry entry;
1234 if (!dbus_message_iter_init(message, &iter))
1237 if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
1240 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
1241 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
1243 if (!strcmp(entry.key, "opensc_engine_path") &&
1244 (entry.type == DBUS_TYPE_STRING)) {
1245 opensc_engine_path = os_strdup(entry.str_value);
1246 if (opensc_engine_path == NULL)
1248 } else if (!strcmp(entry.key, "pkcs11_engine_path") &&
1249 (entry.type == DBUS_TYPE_STRING)) {
1250 pkcs11_engine_path = os_strdup(entry.str_value);
1251 if (pkcs11_engine_path == NULL)
1253 } else if (!strcmp(entry.key, "pkcs11_module_path") &&
1254 (entry.type == DBUS_TYPE_STRING)) {
1255 pkcs11_module_path = os_strdup(entry.str_value);
1256 if (pkcs11_module_path == NULL)
1259 wpa_dbus_dict_entry_clear(&entry);
1262 wpa_dbus_dict_entry_clear(&entry);
1265 #ifdef EAP_TLS_OPENSSL
1266 os_free(wpa_s->conf->opensc_engine_path);
1267 wpa_s->conf->opensc_engine_path = opensc_engine_path;
1268 os_free(wpa_s->conf->pkcs11_engine_path);
1269 wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path;
1270 os_free(wpa_s->conf->pkcs11_module_path);
1271 wpa_s->conf->pkcs11_module_path = pkcs11_module_path;
1272 #endif /* EAP_TLS_OPENSSL */
1274 wpa_sm_set_eapol(wpa_s->wpa, NULL);
1275 eapol_sm_deinit(wpa_s->eapol);
1276 wpa_s->eapol = NULL;
1277 wpa_supplicant_init_eapol(wpa_s);
1278 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
1280 return wpas_dbus_new_success_reply(message);
1283 os_free(opensc_engine_path);
1284 os_free(pkcs11_engine_path);
1285 os_free(pkcs11_module_path);
1286 return wpas_dbus_new_invalid_opts_error(message, NULL);
1291 * wpas_dbus_iface_get_state - Get interface state
1292 * @message: Pointer to incoming dbus message
1293 * @wpa_s: wpa_supplicant structure for a network interface
1294 * Returns: A dbus message containing a STRING representing the current
1297 * Handler function for "state" method call.
1299 DBusMessage * wpas_dbus_iface_get_state(DBusMessage *message,
1300 struct wpa_supplicant *wpa_s)
1302 DBusMessage *reply = NULL;
1303 const char *str_state;
1305 reply = dbus_message_new_method_return(message);
1306 if (reply != NULL) {
1307 str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
1308 dbus_message_append_args(reply, DBUS_TYPE_STRING, &str_state,
1317 * wpas_dbus_iface_get_scanning - Get interface scanning state
1318 * @message: Pointer to incoming dbus message
1319 * @wpa_s: wpa_supplicant structure for a network interface
1320 * Returns: A dbus message containing whether the interface is scanning
1322 * Handler function for "scanning" method call.
1324 DBusMessage * wpas_dbus_iface_get_scanning(DBusMessage *message,
1325 struct wpa_supplicant *wpa_s)
1327 DBusMessage *reply = NULL;
1328 dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
1330 reply = dbus_message_new_method_return(message);
1331 if (reply != NULL) {
1332 dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &scanning,
1335 perror("wpas_dbus_iface_get_scanning[dbus]: out of "
1337 wpa_printf(MSG_ERROR, "dbus control interface: not enough "
1338 "memory to return scanning state.");
1346 * wpas_dbus_iface_set_blobs - Store named binary blobs (ie, for certificates)
1347 * @message: Pointer to incoming dbus message
1348 * @wpa_s: %wpa_supplicant data structure
1349 * Returns: A dbus message containing a UINT32 indicating success (1) or
1352 * Asks wpa_supplicant to internally store a one or more binary blobs.
1354 DBusMessage * wpas_dbus_iface_set_blobs(DBusMessage *message,
1355 struct wpa_supplicant *wpa_s)
1357 DBusMessage *reply = NULL;
1358 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
1359 DBusMessageIter iter, iter_dict;
1361 dbus_message_iter_init(message, &iter);
1363 if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
1364 return wpas_dbus_new_invalid_opts_error(message, NULL);
1366 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
1367 struct wpa_config_blob *blob;
1369 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
1370 reply = wpas_dbus_new_invalid_opts_error(message,
1375 if (entry.type != DBUS_TYPE_ARRAY ||
1376 entry.array_type != DBUS_TYPE_BYTE) {
1377 reply = wpas_dbus_new_invalid_opts_error(
1378 message, "Byte array expected.");
1382 if ((entry.array_len <= 0) || (entry.array_len > 65536) ||
1383 !strlen(entry.key)) {
1384 reply = wpas_dbus_new_invalid_opts_error(
1385 message, "Invalid array size.");
1389 blob = os_zalloc(sizeof(*blob));
1391 reply = dbus_message_new_error(
1392 message, WPAS_ERROR_ADD_ERROR,
1393 "Not enough memory to add blob.");
1396 blob->data = os_zalloc(entry.array_len);
1397 if (blob->data == NULL) {
1398 reply = dbus_message_new_error(
1399 message, WPAS_ERROR_ADD_ERROR,
1400 "Not enough memory to add blob data.");
1405 blob->name = os_strdup(entry.key);
1406 blob->len = entry.array_len;
1407 os_memcpy(blob->data, (u8 *) entry.bytearray_value,
1409 if (blob->name == NULL || blob->data == NULL) {
1410 wpa_config_free_blob(blob);
1411 reply = dbus_message_new_error(
1412 message, WPAS_ERROR_ADD_ERROR,
1413 "Error adding blob.");
1418 if (!wpa_config_remove_blob(wpa_s->conf, blob->name))
1419 wpas_notify_blob_removed(wpa_s, blob->name);
1420 wpa_config_set_blob(wpa_s->conf, blob);
1421 wpas_notify_blob_added(wpa_s, blob->name);
1423 wpa_dbus_dict_entry_clear(&entry);
1425 wpa_dbus_dict_entry_clear(&entry);
1427 return reply ? reply : wpas_dbus_new_success_reply(message);
1432 * wpas_dbus_iface_remove_blob - Remove named binary blobs
1433 * @message: Pointer to incoming dbus message
1434 * @wpa_s: %wpa_supplicant data structure
1435 * Returns: A dbus message containing a UINT32 indicating success (1) or
1438 * Asks wpa_supplicant to remove one or more previously stored binary blobs.
1440 DBusMessage * wpas_dbus_iface_remove_blobs(DBusMessage *message,
1441 struct wpa_supplicant *wpa_s)
1443 DBusMessageIter iter, array;
1444 char *err_msg = NULL;
1446 dbus_message_iter_init(message, &iter);
1448 if ((dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY) ||
1449 (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_STRING))
1450 return wpas_dbus_new_invalid_opts_error(message, NULL);
1452 dbus_message_iter_recurse(&iter, &array);
1453 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
1456 dbus_message_iter_get_basic(&array, &name);
1457 if (!os_strlen(name))
1458 err_msg = "Invalid blob name.";
1460 if (wpa_config_remove_blob(wpa_s->conf, name) != 0)
1461 err_msg = "Error removing blob.";
1463 wpas_notify_blob_removed(wpa_s, name);
1464 dbus_message_iter_next(&array);
1468 return dbus_message_new_error(message, WPAS_ERROR_REMOVE_ERROR,
1471 return wpas_dbus_new_success_reply(message);
1478 * wpas_dbus_iface_wps_pbc - Request credentials using WPS PBC method
1479 * @message: Pointer to incoming dbus message
1480 * @wpa_s: %wpa_supplicant data structure
1481 * Returns: A dbus message containing a UINT32 indicating success (1) or
1484 * Handler function for "wpsPbc" method call
1486 DBusMessage * wpas_dbus_iface_wps_pbc(DBusMessage *message,
1487 struct wpa_supplicant *wpa_s)
1489 char *arg_bssid = NULL;
1493 if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg_bssid,
1495 return wpas_dbus_new_invalid_opts_error(message, NULL);
1497 if (!os_strcmp(arg_bssid, "any"))
1498 ret = wpas_wps_start_pbc(wpa_s, NULL);
1499 else if (!hwaddr_aton(arg_bssid, bssid))
1500 ret = wpas_wps_start_pbc(wpa_s, bssid);
1502 return wpas_dbus_new_invalid_opts_error(message,
1507 return dbus_message_new_error(message,
1508 WPAS_ERROR_WPS_PBC_ERROR,
1509 "Could not start PBC "
1513 return wpas_dbus_new_success_reply(message);
1518 * wpas_dbus_iface_wps_pin - Establish the PIN number of the enrollee
1519 * @message: Pointer to incoming dbus message
1520 * @wpa_s: %wpa_supplicant data structure
1521 * Returns: A dbus message containing a UINT32 indicating success (1) or
1524 * Handler function for "wpsPin" method call
1526 DBusMessage * wpas_dbus_iface_wps_pin(DBusMessage *message,
1527 struct wpa_supplicant *wpa_s)
1529 DBusMessage *reply = NULL;
1532 u8 bssid[ETH_ALEN], *_bssid = NULL;
1535 if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg_bssid,
1536 DBUS_TYPE_STRING, &pin, DBUS_TYPE_INVALID))
1537 return wpas_dbus_new_invalid_opts_error(message, NULL);
1539 if (!os_strcmp(arg_bssid, "any"))
1541 else if (!hwaddr_aton(arg_bssid, bssid))
1544 return wpas_dbus_new_invalid_opts_error(message,
1548 if (os_strlen(pin) > 0)
1549 ret = wpas_wps_start_pin(wpa_s, _bssid, pin);
1551 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL);
1554 return dbus_message_new_error(message,
1555 WPAS_ERROR_WPS_PIN_ERROR,
1556 "Could not init PIN");
1559 reply = dbus_message_new_method_return(message);
1564 dbus_message_append_args(reply, DBUS_TYPE_STRING, &pin,
1568 os_snprintf(npin, sizeof(npin), "%08d", ret);
1569 dbus_message_append_args(reply, DBUS_TYPE_STRING, &npin,
1577 * wpas_dbus_iface_wps_reg - Request credentials using the PIN of the AP
1578 * @message: Pointer to incoming dbus message
1579 * @wpa_s: %wpa_supplicant data structure
1580 * Returns: A dbus message containing a UINT32 indicating success (1) or
1583 * Handler function for "wpsReg" method call
1585 DBusMessage * wpas_dbus_iface_wps_reg(DBusMessage *message,
1586 struct wpa_supplicant *wpa_s)
1593 if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg_bssid,
1594 DBUS_TYPE_STRING, &pin, DBUS_TYPE_INVALID))
1595 return wpas_dbus_new_invalid_opts_error(message, NULL);
1597 if (!os_strcmp(arg_bssid, "any"))
1598 ret = wpas_wps_start_reg(wpa_s, NULL, pin, NULL);
1599 else if (!hwaddr_aton(arg_bssid, bssid))
1600 ret = wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1602 return wpas_dbus_new_invalid_opts_error(message,
1607 return dbus_message_new_error(message,
1608 WPAS_ERROR_WPS_PBC_ERROR,
1609 "Could not request credentials");
1612 return wpas_dbus_new_success_reply(message);
1615 #endif /* CONFIG_WPS */