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"
31 * wpas_dbus_new_invalid_opts_error - Return a new invalid options error message
32 * @message: Pointer to incoming dbus message this error refers to
33 * Returns: a dbus error message
35 * Convenience function to create and return an invalid options error
37 static DBusMessage * wpas_dbus_new_invalid_opts_error(DBusMessage *message,
42 reply = dbus_message_new_error(message, WPAS_ERROR_INVALID_OPTS,
43 "Did not receive correct message "
46 dbus_message_append_args(reply, DBUS_TYPE_STRING, &arg,
54 * wpas_dbus_new_success_reply - Return a new success reply message
55 * @message: Pointer to incoming dbus message this reply refers to
56 * Returns: a dbus message containing a single UINT32 that indicates
57 * success (ie, a value of 1)
59 * Convenience function to create and return a success reply message
61 static DBusMessage * wpas_dbus_new_success_reply(DBusMessage *message)
64 unsigned int success = 1;
66 reply = dbus_message_new_method_return(message);
67 dbus_message_append_args(reply, DBUS_TYPE_UINT32, &success,
73 static void wpas_dbus_free_wpa_interface(struct wpa_interface *iface)
75 free((char *) iface->driver);
76 free((char *) iface->driver_param);
77 free((char *) iface->confname);
78 free((char *) iface->bridge_ifname);
83 * wpas_dbus_global_add_interface - Request registration of a network interface
84 * @message: Pointer to incoming dbus message
85 * @global: %wpa_supplicant global data structure
86 * Returns: The object path of the new interface object,
87 * or a dbus error message with more information
89 * Handler function for "addInterface" method call. Handles requests
90 * by dbus clients to register a network interface that wpa_supplicant
93 DBusMessage * wpas_dbus_global_add_interface(DBusMessage *message,
94 struct wpa_global *global)
96 struct wpa_interface iface;
98 DBusMessage *reply = NULL;
101 memset(&iface, 0, sizeof(iface));
103 dbus_message_iter_init(message, &iter);
105 /* First argument: interface name (DBUS_TYPE_STRING)
106 * Required; must be non-zero length
108 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
110 dbus_message_iter_get_basic(&iter, &ifname);
113 iface.ifname = ifname;
115 /* Second argument: dict of options */
116 if (dbus_message_iter_next(&iter)) {
117 DBusMessageIter iter_dict;
118 struct wpa_dbus_dict_entry entry;
120 if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
122 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
123 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
125 if (!strcmp(entry.key, "driver") &&
126 (entry.type == DBUS_TYPE_STRING)) {
127 iface.driver = strdup(entry.str_value);
128 if (iface.driver == NULL)
130 } else if (!strcmp(entry.key, "driver-params") &&
131 (entry.type == DBUS_TYPE_STRING)) {
132 iface.driver_param = strdup(entry.str_value);
133 if (iface.driver_param == NULL)
135 } else if (!strcmp(entry.key, "config-file") &&
136 (entry.type == DBUS_TYPE_STRING)) {
137 iface.confname = strdup(entry.str_value);
138 if (iface.confname == NULL)
140 } else if (!strcmp(entry.key, "bridge-ifname") &&
141 (entry.type == DBUS_TYPE_STRING)) {
142 iface.bridge_ifname = strdup(entry.str_value);
143 if (iface.bridge_ifname == NULL)
146 wpa_dbus_dict_entry_clear(&entry);
149 wpa_dbus_dict_entry_clear(&entry);
154 * Try to get the wpa_supplicant record for this iface, return
155 * an error if we already control it.
157 if (wpa_supplicant_get_iface(global, iface.ifname) != NULL) {
158 reply = dbus_message_new_error(message,
159 WPAS_ERROR_EXISTS_ERROR,
160 "wpa_supplicant already "
161 "controls this interface.");
163 struct wpa_supplicant *wpa_s;
164 /* Otherwise, have wpa_supplicant attach to it. */
165 if ((wpa_s = wpa_supplicant_add_iface(global, &iface))) {
166 const char *path = wpa_supplicant_get_dbus_path(wpa_s);
167 reply = dbus_message_new_method_return(message);
168 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
169 &path, DBUS_TYPE_INVALID);
171 reply = dbus_message_new_error(message,
172 WPAS_ERROR_ADD_ERROR,
174 "couldn't grab this "
178 wpas_dbus_free_wpa_interface(&iface);
182 wpas_dbus_free_wpa_interface(&iface);
183 return wpas_dbus_new_invalid_opts_error(message, NULL);
188 * wpas_dbus_global_remove_interface - Request deregistration of an interface
189 * @message: Pointer to incoming dbus message
190 * @global: wpa_supplicant global data structure
191 * Returns: a dbus message containing a UINT32 indicating success (1) or
192 * failure (0), or returns a dbus error message with more information
194 * Handler function for "removeInterface" method call. Handles requests
195 * by dbus clients to deregister a network interface that wpa_supplicant
198 DBusMessage * wpas_dbus_global_remove_interface(DBusMessage *message,
199 struct wpa_global *global)
201 struct wpa_supplicant *wpa_s;
203 DBusMessage *reply = NULL;
205 if (!dbus_message_get_args(message, NULL,
206 DBUS_TYPE_OBJECT_PATH, &path,
207 DBUS_TYPE_INVALID)) {
208 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
212 wpa_s = wpa_supplicant_get_iface_by_dbus_path(global, path);
214 reply = wpas_dbus_new_invalid_iface_error(message);
218 if (!wpa_supplicant_remove_iface(global, wpa_s)) {
219 reply = wpas_dbus_new_success_reply(message);
221 reply = dbus_message_new_error(message,
222 WPAS_ERROR_REMOVE_ERROR,
223 "wpa_supplicant couldn't "
224 "remove this interface.");
233 * wpas_dbus_global_get_interface - Get the object path for an interface name
234 * @message: Pointer to incoming dbus message
235 * @global: %wpa_supplicant global data structure
236 * Returns: The object path of the interface object,
237 * or a dbus error message with more information
239 * Handler function for "getInterface" method call. Handles requests
240 * by dbus clients for the object path of an specific network interface.
242 DBusMessage * wpas_dbus_global_get_interface(DBusMessage *message,
243 struct wpa_global *global)
245 DBusMessage *reply = NULL;
248 struct wpa_supplicant *wpa_s;
250 if (!dbus_message_get_args(message, NULL,
251 DBUS_TYPE_STRING, &ifname,
252 DBUS_TYPE_INVALID)) {
253 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
257 wpa_s = wpa_supplicant_get_iface(global, ifname);
259 reply = wpas_dbus_new_invalid_iface_error(message);
263 path = wpa_supplicant_get_dbus_path(wpa_s);
265 reply = dbus_message_new_error(message,
266 WPAS_ERROR_INTERNAL_ERROR,
267 "an internal error occurred "
268 "getting the interface.");
272 reply = dbus_message_new_method_return(message);
273 dbus_message_append_args(reply,
274 DBUS_TYPE_OBJECT_PATH, &path,
283 * wpas_dbus_iface_scan - Request a wireless scan on an interface
284 * @message: Pointer to incoming dbus message
285 * @wpa_s: wpa_supplicant structure for a network interface
286 * Returns: a dbus message containing a UINT32 indicating success (1) or
289 * Handler function for "scan" method call of a network device. Requests
290 * that wpa_supplicant perform a wireless scan as soon as possible
291 * on a particular wireless interface.
293 DBusMessage * wpas_dbus_iface_scan(DBusMessage *message,
294 struct wpa_supplicant *wpa_s)
297 wpa_supplicant_req_scan(wpa_s, 0, 0);
298 return wpas_dbus_new_success_reply(message);
303 * wpas_dbus_iface_scan_results - Get the results of a recent scan request
304 * @message: Pointer to incoming dbus message
305 * @wpa_s: wpa_supplicant structure for a network interface
306 * Returns: a dbus message containing a dbus array of objects paths, or returns
307 * a dbus error message if not scan results could be found
309 * Handler function for "scanResults" method call of a network device. Returns
310 * a dbus message containing the object paths of wireless networks found.
312 DBusMessage * wpas_dbus_iface_scan_results(DBusMessage *message,
313 struct wpa_supplicant *wpa_s)
315 DBusMessage *reply = NULL;
316 DBusMessageIter iter;
317 DBusMessageIter sub_iter;
320 /* Ensure we've actually got scan results to return */
321 if (wpa_s->scan_res == NULL &&
322 wpa_supplicant_get_scan_results(wpa_s) < 0) {
323 reply = dbus_message_new_error(message, WPAS_ERROR_SCAN_ERROR,
324 "An error ocurred getting scan "
329 /* Create and initialize the return message */
330 reply = dbus_message_new_method_return(message);
331 dbus_message_iter_init_append(reply, &iter);
332 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
333 DBUS_TYPE_OBJECT_PATH_AS_STRING,
336 /* Loop through scan results and append each result's object path */
337 for (i = 0; i < wpa_s->scan_res->num; i++) {
338 struct wpa_scan_res *res = wpa_s->scan_res->res[i];
341 path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
343 perror("wpas_dbus_iface_scan_results[dbus]: out of "
345 wpa_printf(MSG_ERROR, "dbus control interface: not "
346 "enough memory to send scan results "
350 /* Construct the object path for this network. Note that ':'
351 * is not a valid character in dbus object paths.
353 snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
354 "%s/" WPAS_DBUS_BSSIDS_PART "/"
355 WPAS_DBUS_BSSID_FORMAT,
356 wpa_supplicant_get_dbus_path(wpa_s),
357 MAC2STR(res->bssid));
358 dbus_message_iter_append_basic(&sub_iter,
359 DBUS_TYPE_OBJECT_PATH, &path);
363 dbus_message_iter_close_container(&iter, &sub_iter);
371 * wpas_dbus_bssid_properties - Return the properties of a scanned network
372 * @message: Pointer to incoming dbus message
373 * @wpa_s: wpa_supplicant structure for a network interface
374 * @res: wpa_supplicant scan result for which to get properties
375 * Returns: a dbus message containing the properties for the requested network
377 * Handler function for "properties" method call of a scanned network.
378 * Returns a dbus message containing the the properties.
380 DBusMessage * wpas_dbus_bssid_properties(DBusMessage *message,
381 struct wpa_supplicant *wpa_s,
382 struct wpa_scan_res *res)
384 DBusMessage *reply = NULL;
385 DBusMessageIter iter, iter_dict;
388 /* Dump the properties into a dbus message */
389 reply = dbus_message_new_method_return(message);
391 dbus_message_iter_init_append(reply, &iter);
392 if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
395 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "bssid",
396 (const char *) res->bssid,
400 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
402 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "ssid",
403 (const char *) (ie + 2),
408 ie = wpa_scan_get_vendor_ie(res, WPA_IE_VENDOR_TYPE);
410 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "wpaie",
416 ie = wpa_scan_get_ie(res, WLAN_EID_RSN);
418 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "rsnie",
424 ie = wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE);
426 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "wpsie",
433 if (!wpa_dbus_dict_append_int32(&iter_dict, "frequency",
437 if (!wpa_dbus_dict_append_uint16(&iter_dict, "capabilities",
440 if (!(res->flags & WPA_SCAN_QUAL_INVALID) &&
441 !wpa_dbus_dict_append_int32(&iter_dict, "quality", res->qual))
443 if (!(res->flags & WPA_SCAN_NOISE_INVALID) &&
444 !wpa_dbus_dict_append_int32(&iter_dict, "noise", res->noise))
446 if (!(res->flags & WPA_SCAN_LEVEL_INVALID) &&
447 !wpa_dbus_dict_append_int32(&iter_dict, "level", res->level))
449 if (!wpa_dbus_dict_append_int32(&iter_dict, "maxrate",
450 wpa_scan_get_max_rate(res) * 500000))
453 if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
460 dbus_message_unref(reply);
461 return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
462 "an internal error occurred returning "
463 "BSSID properties.");
468 * wpas_dbus_iface_capabilities - Return interface capabilities
469 * @message: Pointer to incoming dbus message
470 * @wpa_s: wpa_supplicant structure for a network interface
471 * Returns: A dbus message containing a dict of strings
473 * Handler function for "capabilities" method call of an interface.
475 DBusMessage * wpas_dbus_iface_capabilities(DBusMessage *message,
476 struct wpa_supplicant *wpa_s)
478 DBusMessage *reply = NULL;
479 struct wpa_driver_capa capa;
481 DBusMessageIter iter, iter_dict;
484 dbus_bool_t strict = FALSE;
485 DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
487 if (!dbus_message_get_args(message, NULL,
488 DBUS_TYPE_BOOLEAN, &strict,
492 reply = dbus_message_new_method_return(message);
494 dbus_message_iter_init_append(reply, &iter);
495 if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
499 eap_methods = eap_get_names_as_string_array(&num_items);
501 dbus_bool_t success = FALSE;
504 success = wpa_dbus_dict_append_string_array(
505 &iter_dict, "eap", (const char **) eap_methods,
508 /* free returned method array */
509 while (eap_methods[i])
510 free(eap_methods[i++]);
517 res = wpa_drv_get_capa(wpa_s, &capa);
519 /***** pairwise cipher */
522 const char *args[] = {"CCMP", "TKIP", "NONE"};
523 if (!wpa_dbus_dict_append_string_array(
524 &iter_dict, "pairwise", args,
525 sizeof(args) / sizeof(char*)))
529 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "pairwise",
535 if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
536 if (!wpa_dbus_dict_string_array_add_element(
537 &iter_array, "CCMP"))
541 if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
542 if (!wpa_dbus_dict_string_array_add_element(
543 &iter_array, "TKIP"))
547 if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
548 if (!wpa_dbus_dict_string_array_add_element(
549 &iter_array, "NONE"))
553 if (!wpa_dbus_dict_end_string_array(&iter_dict,
560 /***** group cipher */
563 const char *args[] = {
564 "CCMP", "TKIP", "WEP104", "WEP40"
566 if (!wpa_dbus_dict_append_string_array(
567 &iter_dict, "group", args,
568 sizeof(args) / sizeof(char*)))
572 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "group",
578 if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
579 if (!wpa_dbus_dict_string_array_add_element(
580 &iter_array, "CCMP"))
584 if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
585 if (!wpa_dbus_dict_string_array_add_element(
586 &iter_array, "TKIP"))
590 if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) {
591 if (!wpa_dbus_dict_string_array_add_element(
592 &iter_array, "WEP104"))
596 if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) {
597 if (!wpa_dbus_dict_string_array_add_element(
598 &iter_array, "WEP40"))
602 if (!wpa_dbus_dict_end_string_array(&iter_dict,
609 /***** key management */
612 const char *args[] = {
613 "WPA-PSK", "WPA-EAP", "IEEE8021X", "WPA-NONE",
616 if (!wpa_dbus_dict_append_string_array(
617 &iter_dict, "key_mgmt", args,
618 sizeof(args) / sizeof(char*)))
622 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "key_mgmt",
628 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
632 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
636 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
637 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
638 if (!wpa_dbus_dict_string_array_add_element(
639 &iter_array, "WPA-EAP"))
643 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
644 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
645 if (!wpa_dbus_dict_string_array_add_element(
646 &iter_array, "WPA-PSK"))
650 if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
651 if (!wpa_dbus_dict_string_array_add_element(
652 &iter_array, "WPA-NONE"))
656 if (!wpa_dbus_dict_end_string_array(&iter_dict,
663 /***** WPA protocol */
666 const char *args[] = { "RSN", "WPA" };
667 if (!wpa_dbus_dict_append_string_array(
668 &iter_dict, "proto", args,
669 sizeof(args) / sizeof(char*)))
673 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "proto",
679 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
680 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
681 if (!wpa_dbus_dict_string_array_add_element(
686 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
687 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
688 if (!wpa_dbus_dict_string_array_add_element(
693 if (!wpa_dbus_dict_end_string_array(&iter_dict,
703 const char *args[] = { "OPEN", "SHARED", "LEAP" };
704 if (!wpa_dbus_dict_append_string_array(
705 &iter_dict, "auth_alg", args,
706 sizeof(args) / sizeof(char*)))
710 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "auth_alg",
716 if (capa.auth & (WPA_DRIVER_AUTH_OPEN)) {
717 if (!wpa_dbus_dict_string_array_add_element(
718 &iter_array, "OPEN"))
722 if (capa.auth & (WPA_DRIVER_AUTH_SHARED)) {
723 if (!wpa_dbus_dict_string_array_add_element(
724 &iter_array, "SHARED"))
728 if (capa.auth & (WPA_DRIVER_AUTH_LEAP)) {
729 if (!wpa_dbus_dict_string_array_add_element(
730 &iter_array, "LEAP"))
734 if (!wpa_dbus_dict_end_string_array(&iter_dict,
741 if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
748 dbus_message_unref(reply);
749 return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
750 "an internal error occurred returning "
751 "interface capabilities.");
756 * wpas_dbus_iface_add_network - Add a new configured network
757 * @message: Pointer to incoming dbus message
758 * @wpa_s: wpa_supplicant structure for a network interface
759 * Returns: A dbus message containing the object path of the new network
761 * Handler function for "addNetwork" method call of a network interface.
763 DBusMessage * wpas_dbus_iface_add_network(DBusMessage *message,
764 struct wpa_supplicant *wpa_s)
766 DBusMessage *reply = NULL;
767 struct wpa_ssid *ssid;
770 path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
772 perror("wpas_dbus_iface_scan_results[dbus]: out of "
774 wpa_printf(MSG_ERROR, "dbus control interface: not "
775 "enough memory to send scan results "
780 ssid = wpa_config_add_network(wpa_s->conf);
782 reply = dbus_message_new_error(message,
783 WPAS_ERROR_ADD_NETWORK_ERROR,
784 "wpa_supplicant could not add "
785 "a network on this interface.");
789 wpa_config_set_network_defaults(ssid);
791 /* Construct the object path for this network. */
792 snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
793 "%s/" WPAS_DBUS_NETWORKS_PART "/%d",
794 wpa_supplicant_get_dbus_path(wpa_s),
797 reply = dbus_message_new_method_return(message);
798 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
799 &path, DBUS_TYPE_INVALID);
808 * wpas_dbus_iface_remove_network - Remove a configured network
809 * @message: Pointer to incoming dbus message
810 * @wpa_s: wpa_supplicant structure for a network interface
811 * Returns: A dbus message containing a UINT32 indicating success (1) or
814 * Handler function for "removeNetwork" method call of a network interface.
816 DBusMessage * wpas_dbus_iface_remove_network(DBusMessage *message,
817 struct wpa_supplicant *wpa_s)
819 DBusMessage *reply = NULL;
821 char *iface = NULL, *net_id = NULL;
823 struct wpa_ssid *ssid;
825 if (!dbus_message_get_args(message, NULL,
826 DBUS_TYPE_OBJECT_PATH, &op,
827 DBUS_TYPE_INVALID)) {
828 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
832 /* Extract the network ID */
833 iface = wpas_dbus_decompose_object_path(op, &net_id, NULL);
835 reply = wpas_dbus_new_invalid_network_error(message);
838 /* Ensure the network is actually a child of this interface */
839 if (strcmp(iface, wpa_supplicant_get_dbus_path(wpa_s)) != 0) {
840 reply = wpas_dbus_new_invalid_network_error(message);
844 id = strtoul(net_id, NULL, 10);
845 ssid = wpa_config_get_network(wpa_s->conf, id);
847 reply = wpas_dbus_new_invalid_network_error(message);
851 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
852 reply = dbus_message_new_error(message,
853 WPAS_ERROR_REMOVE_NETWORK_ERROR,
854 "error removing the specified "
855 "on this interface.");
859 if (ssid == wpa_s->current_ssid)
860 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
861 reply = wpas_dbus_new_success_reply(message);
870 static const char *dont_quote[] = {
871 "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
872 "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
876 static dbus_bool_t should_quote_opt(const char *key)
879 while (dont_quote[i] != NULL) {
880 if (strcmp(key, dont_quote[i]) == 0)
888 * wpas_dbus_iface_set_network - Set options for a configured network
889 * @message: Pointer to incoming dbus message
890 * @wpa_s: wpa_supplicant structure for a network interface
891 * @ssid: wpa_ssid structure for a configured network
892 * Returns: a dbus message containing a UINT32 indicating success (1) or
895 * Handler function for "set" method call of a configured network.
897 DBusMessage * wpas_dbus_iface_set_network(DBusMessage *message,
898 struct wpa_supplicant *wpa_s,
899 struct wpa_ssid *ssid)
901 DBusMessage *reply = NULL;
902 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
903 DBusMessageIter iter, iter_dict;
905 dbus_message_iter_init(message, &iter);
907 if (!wpa_dbus_dict_open_read(&iter, &iter_dict)) {
908 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
912 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
917 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
918 reply = wpas_dbus_new_invalid_opts_error(message,
923 /* Type conversions, since wpa_supplicant wants strings */
924 if (entry.type == DBUS_TYPE_ARRAY &&
925 entry.array_type == DBUS_TYPE_BYTE) {
926 if (entry.array_len <= 0)
929 size = entry.array_len * 2 + 1;
930 value = os_zalloc(size);
933 ret = wpa_snprintf_hex(value, size,
934 (u8 *) entry.bytearray_value,
938 } else if (entry.type == DBUS_TYPE_STRING) {
939 if (should_quote_opt(entry.key)) {
940 size = strlen(entry.str_value);
941 /* Zero-length option check */
944 size += 3; /* For quotes and terminator */
945 value = os_zalloc(size);
948 ret = snprintf(value, size, "\"%s\"",
950 if (ret < 0 || (size_t) ret != (size - 1))
953 value = strdup(entry.str_value);
957 } else if (entry.type == DBUS_TYPE_UINT32) {
958 value = os_zalloc(size);
961 ret = snprintf(value, size, "%u", entry.uint32_value);
964 } else if (entry.type == DBUS_TYPE_INT32) {
965 value = os_zalloc(size);
968 ret = snprintf(value, size, "%d", entry.int32_value);
974 if (wpa_config_set(ssid, entry.key, value, 0) < 0)
977 if ((strcmp(entry.key, "psk") == 0 &&
978 value[0] == '"' && ssid->ssid_len) ||
979 (strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
980 wpa_config_update_psk(ssid);
983 wpa_dbus_dict_entry_clear(&entry);
988 reply = wpas_dbus_new_invalid_opts_error(message, entry.key);
989 wpa_dbus_dict_entry_clear(&entry);
994 reply = wpas_dbus_new_success_reply(message);
1002 * wpas_dbus_iface_enable_network - Mark a configured network as enabled
1003 * @message: Pointer to incoming dbus message
1004 * @wpa_s: wpa_supplicant structure for a network interface
1005 * @ssid: wpa_ssid structure for a configured network
1006 * Returns: A dbus message containing a UINT32 indicating success (1) or
1009 * Handler function for "enable" method call of a configured network.
1011 DBusMessage * wpas_dbus_iface_enable_network(DBusMessage *message,
1012 struct wpa_supplicant *wpa_s,
1013 struct wpa_ssid *ssid)
1015 if (wpa_s->current_ssid == NULL && ssid->disabled) {
1017 * Try to reassociate since there is no current configuration
1018 * and a new network was made available.
1020 wpa_s->reassociate = 1;
1021 wpa_supplicant_req_scan(wpa_s, 0, 0);
1025 return wpas_dbus_new_success_reply(message);
1030 * wpas_dbus_iface_disable_network - Mark a configured network as disabled
1031 * @message: Pointer to incoming dbus message
1032 * @wpa_s: wpa_supplicant structure for a network interface
1033 * @ssid: wpa_ssid structure for a configured network
1034 * Returns: A dbus message containing a UINT32 indicating success (1) or
1037 * Handler function for "disable" method call of a configured network.
1039 DBusMessage * wpas_dbus_iface_disable_network(DBusMessage *message,
1040 struct wpa_supplicant *wpa_s,
1041 struct wpa_ssid *ssid)
1043 if (ssid == wpa_s->current_ssid)
1044 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1047 return wpas_dbus_new_success_reply(message);
1052 * wpas_dbus_iface_select_network - Attempt association with a configured network
1053 * @message: Pointer to incoming dbus message
1054 * @wpa_s: wpa_supplicant structure for a network interface
1055 * Returns: A dbus message containing a UINT32 indicating success (1) or
1058 * Handler function for "selectNetwork" method call of network interface.
1060 DBusMessage * wpas_dbus_iface_select_network(DBusMessage *message,
1061 struct wpa_supplicant *wpa_s)
1063 DBusMessage *reply = NULL;
1065 struct wpa_ssid *ssid;
1066 char *iface_obj_path = NULL;
1067 char *network = NULL;
1069 if (strlen(dbus_message_get_signature(message)) == 0) {
1071 ssid = wpa_s->conf->ssid;
1076 wpa_s->reassociate = 1;
1077 wpa_supplicant_req_scan(wpa_s, 0, 0);
1079 const char *obj_path;
1082 if (!dbus_message_get_args(message, NULL,
1083 DBUS_TYPE_OBJECT_PATH, &op,
1084 DBUS_TYPE_INVALID)) {
1085 reply = wpas_dbus_new_invalid_opts_error(message,
1090 /* Extract the network number */
1091 iface_obj_path = wpas_dbus_decompose_object_path(op,
1094 if (iface_obj_path == NULL) {
1095 reply = wpas_dbus_new_invalid_iface_error(message);
1098 /* Ensure the object path really points to this interface */
1099 obj_path = wpa_supplicant_get_dbus_path(wpa_s);
1100 if (strcmp(iface_obj_path, obj_path) != 0) {
1101 reply = wpas_dbus_new_invalid_network_error(message);
1105 nid = strtoul(network, NULL, 10);
1106 if (errno == EINVAL) {
1107 reply = wpas_dbus_new_invalid_network_error(message);
1111 ssid = wpa_config_get_network(wpa_s->conf, nid);
1113 reply = wpas_dbus_new_invalid_network_error(message);
1117 /* Finally, associate with the network */
1118 if (ssid != wpa_s->current_ssid && wpa_s->current_ssid)
1119 wpa_supplicant_disassociate(
1120 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1122 /* Mark all other networks disabled and trigger reassociation
1124 ssid = wpa_s->conf->ssid;
1126 ssid->disabled = (nid != ssid->id);
1129 wpa_s->disconnected = 0;
1130 wpa_s->reassociate = 1;
1131 wpa_supplicant_req_scan(wpa_s, 0, 0);
1134 reply = wpas_dbus_new_success_reply(message);
1137 free(iface_obj_path);
1144 * wpas_dbus_iface_disconnect - Terminate the current connection
1145 * @message: Pointer to incoming dbus message
1146 * @wpa_s: wpa_supplicant structure for a network interface
1147 * Returns: A dbus message containing a UINT32 indicating success (1) or
1150 * Handler function for "disconnect" method call of network interface.
1152 DBusMessage * wpas_dbus_iface_disconnect(DBusMessage *message,
1153 struct wpa_supplicant *wpa_s)
1155 wpa_s->disconnected = 1;
1156 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1158 return wpas_dbus_new_success_reply(message);
1163 * wpas_dbus_iface_set_ap_scan - Control roaming mode
1164 * @message: Pointer to incoming dbus message
1165 * @wpa_s: wpa_supplicant structure for a network interface
1166 * Returns: A dbus message containing a UINT32 indicating success (1) or
1169 * Handler function for "setAPScan" method call.
1171 DBusMessage * wpas_dbus_iface_set_ap_scan(DBusMessage *message,
1172 struct wpa_supplicant *wpa_s)
1174 DBusMessage *reply = NULL;
1175 dbus_uint32_t ap_scan = 1;
1177 if (!dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &ap_scan,
1178 DBUS_TYPE_INVALID)) {
1179 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
1184 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
1187 wpa_s->conf->ap_scan = ap_scan;
1188 reply = wpas_dbus_new_success_reply(message);
1196 * wpas_dbus_iface_set_smartcard_modules - Set smartcard related module paths
1197 * @message: Pointer to incoming dbus message
1198 * @wpa_s: wpa_supplicant structure for a network interface
1199 * Returns: A dbus message containing a UINT32 indicating success (1) or
1202 * Handler function for "setSmartcardModules" method call.
1204 DBusMessage * wpas_dbus_iface_set_smartcard_modules(
1205 DBusMessage *message, struct wpa_supplicant *wpa_s)
1207 DBusMessageIter iter, iter_dict;
1208 char *opensc_engine_path = NULL;
1209 char *pkcs11_engine_path = NULL;
1210 char *pkcs11_module_path = NULL;
1211 struct wpa_dbus_dict_entry entry;
1213 if (!dbus_message_iter_init(message, &iter))
1216 if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
1219 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
1220 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
1222 if (!strcmp(entry.key, "opensc_engine_path") &&
1223 (entry.type == DBUS_TYPE_STRING)) {
1224 opensc_engine_path = os_strdup(entry.str_value);
1225 if (opensc_engine_path == NULL)
1227 } else if (!strcmp(entry.key, "pkcs11_engine_path") &&
1228 (entry.type == DBUS_TYPE_STRING)) {
1229 pkcs11_engine_path = os_strdup(entry.str_value);
1230 if (pkcs11_engine_path == NULL)
1232 } else if (!strcmp(entry.key, "pkcs11_module_path") &&
1233 (entry.type == DBUS_TYPE_STRING)) {
1234 pkcs11_module_path = os_strdup(entry.str_value);
1235 if (pkcs11_module_path == NULL)
1238 wpa_dbus_dict_entry_clear(&entry);
1241 wpa_dbus_dict_entry_clear(&entry);
1244 #ifdef EAP_TLS_OPENSSL
1245 os_free(wpa_s->conf->opensc_engine_path);
1246 wpa_s->conf->opensc_engine_path = opensc_engine_path;
1247 os_free(wpa_s->conf->pkcs11_engine_path);
1248 wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path;
1249 os_free(wpa_s->conf->pkcs11_module_path);
1250 wpa_s->conf->pkcs11_module_path = pkcs11_module_path;
1251 #endif /* EAP_TLS_OPENSSL */
1253 eapol_sm_deinit(wpa_s->eapol);
1254 wpa_supplicant_init_eapol(wpa_s);
1256 return wpas_dbus_new_success_reply(message);
1259 os_free(opensc_engine_path);
1260 os_free(pkcs11_engine_path);
1261 os_free(pkcs11_module_path);
1262 return wpas_dbus_new_invalid_opts_error(message, NULL);
1266 * wpas_dbus_iface_get_state - Get interface state
1267 * @message: Pointer to incoming dbus message
1268 * @wpa_s: wpa_supplicant structure for a network interface
1269 * Returns: A dbus message containing a STRING representing the current
1272 * Handler function for "state" method call.
1274 DBusMessage * wpas_dbus_iface_get_state(DBusMessage *message,
1275 struct wpa_supplicant *wpa_s)
1277 DBusMessage *reply = NULL;
1278 const char *str_state;
1280 reply = dbus_message_new_method_return(message);
1281 if (reply != NULL) {
1282 str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
1283 dbus_message_append_args(reply, DBUS_TYPE_STRING, &str_state,
1292 * wpas_dbus_iface_set_blobs - Store named binary blobs (ie, for certificates)
1293 * @message: Pointer to incoming dbus message
1294 * @wpa_s: %wpa_supplicant data structure
1295 * Returns: A dbus message containing a UINT32 indicating success (1) or
1298 * Asks wpa_supplicant to internally store a one or more binary blobs.
1300 DBusMessage * wpas_dbus_iface_set_blobs(DBusMessage *message,
1301 struct wpa_supplicant *wpa_s)
1303 DBusMessage *reply = NULL;
1304 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
1305 DBusMessageIter iter, iter_dict;
1307 dbus_message_iter_init(message, &iter);
1309 if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
1310 return wpas_dbus_new_invalid_opts_error(message, NULL);
1312 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
1313 struct wpa_config_blob *blob;
1315 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
1316 reply = wpas_dbus_new_invalid_opts_error(message,
1321 if (entry.type != DBUS_TYPE_ARRAY ||
1322 entry.array_type != DBUS_TYPE_BYTE) {
1323 reply = wpas_dbus_new_invalid_opts_error(
1324 message, "Byte array expected.");
1328 if ((entry.array_len <= 0) || (entry.array_len > 65536) ||
1329 !strlen(entry.key)) {
1330 reply = wpas_dbus_new_invalid_opts_error(
1331 message, "Invalid array size.");
1335 blob = os_zalloc(sizeof(*blob));
1337 reply = dbus_message_new_error(
1338 message, WPAS_ERROR_ADD_ERROR,
1339 "Not enough memory to add blob.");
1342 blob->data = os_zalloc(entry.array_len);
1343 if (blob->data == NULL) {
1344 reply = dbus_message_new_error(
1345 message, WPAS_ERROR_ADD_ERROR,
1346 "Not enough memory to add blob data.");
1351 blob->name = os_strdup(entry.key);
1352 blob->len = entry.array_len;
1353 os_memcpy(blob->data, (u8 *) entry.bytearray_value,
1355 if (blob->name == NULL || blob->data == NULL) {
1356 wpa_config_free_blob(blob);
1357 reply = dbus_message_new_error(
1358 message, WPAS_ERROR_ADD_ERROR,
1359 "Error adding blob.");
1364 wpa_config_remove_blob(wpa_s->conf, blob->name);
1365 wpa_config_set_blob(wpa_s->conf, blob);
1366 wpa_dbus_dict_entry_clear(&entry);
1368 wpa_dbus_dict_entry_clear(&entry);
1370 return reply ? reply : wpas_dbus_new_success_reply(message);
1375 * wpas_dbus_iface_remove_blob - Remove named binary blobs
1376 * @message: Pointer to incoming dbus message
1377 * @wpa_s: %wpa_supplicant data structure
1378 * Returns: A dbus message containing a UINT32 indicating success (1) or
1381 * Asks wpa_supplicant to remove one or more previously stored binary blobs.
1383 DBusMessage * wpas_dbus_iface_remove_blobs(DBusMessage *message,
1384 struct wpa_supplicant *wpa_s)
1386 DBusMessageIter iter, array;
1387 char *err_msg = NULL;
1389 dbus_message_iter_init(message, &iter);
1391 if ((dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY) ||
1392 (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_STRING))
1393 return wpas_dbus_new_invalid_opts_error(message, NULL);
1395 dbus_message_iter_recurse(&iter, &array);
1396 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
1399 dbus_message_iter_get_basic(&array, &name);
1401 err_msg = "Invalid blob name.";
1403 if (wpa_config_remove_blob(wpa_s->conf, name) != 0)
1404 err_msg = "Error removing blob.";
1405 dbus_message_iter_next(&array);
1409 return dbus_message_new_error(message, WPAS_ERROR_REMOVE_ERROR,
1413 return wpas_dbus_new_success_reply(message);