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.
17 #include <dbus/dbus.h>
21 #include "wpa_supplicant_i.h"
22 #include "ctrl_iface_dbus.h"
23 #include "ctrl_iface_dbus_handlers.h"
24 #include "l2_packet.h"
25 #include "eap_methods.h"
26 #include "dbus_dict_helpers.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) != 0) {
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_results == 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->num_scan_results; i++) {
338 struct wpa_scan_result *res = &wpa_s->scan_results[i];
341 path = wpa_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_result *res)
384 DBusMessage *reply = NULL;
385 char *bssid_data, *ssid_data, *wpa_ie_data, *rsn_ie_data;
386 DBusMessageIter iter, iter_dict;
388 /* dbus needs the address of a pointer to the actual value
389 * for array types, not the address of the value itself.
391 bssid_data = (char *) &res->bssid;
392 ssid_data = (char *) &res->ssid;
393 wpa_ie_data = (char *) &res->wpa_ie;
394 rsn_ie_data = (char *) &res->rsn_ie;
396 /* Dump the properties into a dbus message */
397 reply = dbus_message_new_method_return(message);
399 dbus_message_iter_init_append(reply, &iter);
400 if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
403 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "bssid",
404 bssid_data, ETH_ALEN))
406 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "ssid",
407 ssid_data, res->ssid_len))
409 if (res->wpa_ie_len) {
410 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "wpaie",
416 if (res->rsn_ie_len) {
417 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "rsnie",
424 if (!wpa_dbus_dict_append_int32(&iter_dict, "frequency",
428 if (!wpa_dbus_dict_append_uint16(&iter_dict, "capabilities",
431 if (!wpa_dbus_dict_append_int32(&iter_dict, "quality", res->qual))
433 if (!wpa_dbus_dict_append_int32(&iter_dict, "noise", res->noise))
435 if (!wpa_dbus_dict_append_int32(&iter_dict, "level", res->level))
437 if (!wpa_dbus_dict_append_int32(&iter_dict, "maxrate", res->maxrate))
440 if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
447 dbus_message_unref(reply);
448 return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
449 "an internal error occurred returning "
450 "BSSID properties.");
455 * wpas_dbus_iface_capabilities - Return interface capabilities
456 * @message: Pointer to incoming dbus message
457 * @wpa_s: wpa_supplicant structure for a network interface
458 * Returns: A dbus message containing a dict of strings
460 * Handler function for "capabilities" method call of an interface.
462 DBusMessage * wpas_dbus_iface_capabilities(DBusMessage *message,
463 struct wpa_supplicant *wpa_s)
465 DBusMessage *reply = NULL;
466 struct wpa_driver_capa capa;
468 DBusMessageIter iter, iter_dict;
471 dbus_bool_t strict = FALSE;
472 DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
474 if (!dbus_message_get_args(message, NULL,
475 DBUS_TYPE_BOOLEAN, &strict,
479 reply = dbus_message_new_method_return(message);
481 dbus_message_iter_init_append(reply, &iter);
482 if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
486 eap_methods = eap_get_names_as_string_array(&num_items);
488 dbus_bool_t success = FALSE;
491 success = wpa_dbus_dict_append_string_array(
492 &iter_dict, "eap", (const char **) eap_methods,
495 /* free returned method array */
496 while (eap_methods[i])
497 free(eap_methods[i++]);
504 res = wpa_drv_get_capa(wpa_s, &capa);
506 /***** pairwise cipher */
509 const char *args[] = {"CCMP", "TKIP", "NONE"};
510 if (!wpa_dbus_dict_append_string_array(
511 &iter_dict, "pairwise", args,
512 sizeof(args) / sizeof(char*)))
516 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "pairwise",
522 if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
523 if (!wpa_dbus_dict_string_array_add_element(
524 &iter_array, "CCMP"))
528 if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
529 if (!wpa_dbus_dict_string_array_add_element(
530 &iter_array, "TKIP"))
534 if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
535 if (!wpa_dbus_dict_string_array_add_element(
536 &iter_array, "NONE"))
540 if (!wpa_dbus_dict_end_string_array(&iter_dict,
547 /***** group cipher */
550 const char *args[] = {
551 "CCMP", "TKIP", "WEP104", "WEP40"
553 if (!wpa_dbus_dict_append_string_array(
554 &iter_dict, "group", args,
555 sizeof(args) / sizeof(char*)))
559 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "group",
565 if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
566 if (!wpa_dbus_dict_string_array_add_element(
567 &iter_array, "CCMP"))
571 if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
572 if (!wpa_dbus_dict_string_array_add_element(
573 &iter_array, "TKIP"))
577 if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) {
578 if (!wpa_dbus_dict_string_array_add_element(
579 &iter_array, "WEP104"))
583 if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) {
584 if (!wpa_dbus_dict_string_array_add_element(
585 &iter_array, "WEP40"))
589 if (!wpa_dbus_dict_end_string_array(&iter_dict,
596 /***** key management */
599 const char *args[] = {
600 "WPA-PSK", "WPA-EAP", "IEEE8021X", "WPA-NONE",
603 if (!wpa_dbus_dict_append_string_array(
604 &iter_dict, "key_mgmt", args,
605 sizeof(args) / sizeof(char*)))
609 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "key_mgmt",
615 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
619 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
623 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
624 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
625 if (!wpa_dbus_dict_string_array_add_element(
626 &iter_array, "WPA-EAP"))
630 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
631 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
632 if (!wpa_dbus_dict_string_array_add_element(
633 &iter_array, "WPA-PSK"))
637 if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
638 if (!wpa_dbus_dict_string_array_add_element(
639 &iter_array, "WPA-NONE"))
643 if (!wpa_dbus_dict_end_string_array(&iter_dict,
650 /***** WPA protocol */
653 const char *args[] = { "RSN", "WPA" };
654 if (!wpa_dbus_dict_append_string_array(
655 &iter_dict, "proto", args,
656 sizeof(args) / sizeof(char*)))
660 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "proto",
666 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
667 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
668 if (!wpa_dbus_dict_string_array_add_element(
673 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
674 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
675 if (!wpa_dbus_dict_string_array_add_element(
680 if (!wpa_dbus_dict_end_string_array(&iter_dict,
690 const char *args[] = { "OPEN", "SHARED", "LEAP" };
691 if (!wpa_dbus_dict_append_string_array(
692 &iter_dict, "auth_alg", args,
693 sizeof(args) / sizeof(char*)))
697 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "auth_alg",
703 if (capa.auth & (WPA_DRIVER_AUTH_OPEN)) {
704 if (!wpa_dbus_dict_string_array_add_element(
705 &iter_array, "OPEN"))
709 if (capa.auth & (WPA_DRIVER_AUTH_SHARED)) {
710 if (!wpa_dbus_dict_string_array_add_element(
711 &iter_array, "SHARED"))
715 if (capa.auth & (WPA_DRIVER_AUTH_LEAP)) {
716 if (!wpa_dbus_dict_string_array_add_element(
717 &iter_array, "LEAP"))
721 if (!wpa_dbus_dict_end_string_array(&iter_dict,
728 if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
735 dbus_message_unref(reply);
736 return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
737 "an internal error occurred returning "
738 "interface capabilities.");
743 * wpas_dbus_iface_add_network - Add a new configured network
744 * @message: Pointer to incoming dbus message
745 * @wpa_s: wpa_supplicant structure for a network interface
746 * Returns: A dbus message containing the object path of the new network
748 * Handler function for "addNetwork" method call of a network interface.
750 DBusMessage * wpas_dbus_iface_add_network(DBusMessage *message,
751 struct wpa_supplicant *wpa_s)
753 DBusMessage *reply = NULL;
754 struct wpa_ssid *ssid;
757 path = wpa_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
759 perror("wpas_dbus_iface_scan_results[dbus]: out of "
761 wpa_printf(MSG_ERROR, "dbus control interface: not "
762 "enough memory to send scan results "
767 ssid = wpa_config_add_network(wpa_s->conf);
769 reply = dbus_message_new_error(message,
770 WPAS_ERROR_ADD_NETWORK_ERROR,
771 "wpa_supplicant could not add "
772 "a network on this interface.");
776 wpa_config_set_network_defaults(ssid);
778 /* Construct the object path for this network. */
779 snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
780 "%s/" WPAS_DBUS_NETWORKS_PART "/%d",
781 wpa_supplicant_get_dbus_path(wpa_s),
784 reply = dbus_message_new_method_return(message);
785 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
786 &path, DBUS_TYPE_INVALID);
795 * wpas_dbus_iface_remove_network - Remove a configured network
796 * @message: Pointer to incoming dbus message
797 * @wpa_s: wpa_supplicant structure for a network interface
798 * Returns: A dbus message containing a UINT32 indicating success (1) or
801 * Handler function for "removeNetwork" method call of a network interface.
803 DBusMessage * wpas_dbus_iface_remove_network(DBusMessage *message,
804 struct wpa_supplicant *wpa_s)
806 DBusMessage *reply = NULL;
808 char *iface = NULL, *net_id = NULL;
810 struct wpa_ssid *ssid;
812 if (!dbus_message_get_args(message, NULL,
813 DBUS_TYPE_OBJECT_PATH, &op,
814 DBUS_TYPE_INVALID)) {
815 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
819 /* Extract the network ID */
820 iface = wpas_dbus_decompose_object_path(op, &net_id, NULL);
822 reply = wpas_dbus_new_invalid_network_error(message);
825 /* Ensure the network is actually a child of this interface */
826 if (strcmp(iface, wpa_supplicant_get_dbus_path(wpa_s)) != 0) {
827 reply = wpas_dbus_new_invalid_network_error(message);
831 id = strtoul(net_id, NULL, 10);
832 ssid = wpa_config_get_network(wpa_s->conf, id);
834 reply = wpas_dbus_new_invalid_network_error(message);
838 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
839 reply = dbus_message_new_error(message,
840 WPAS_ERROR_REMOVE_NETWORK_ERROR,
841 "error removing the specified "
842 "on this interface.");
846 if (ssid == wpa_s->current_ssid)
847 wpa_supplicant_disassociate(wpa_s, REASON_DEAUTH_LEAVING);
848 reply = wpas_dbus_new_success_reply(message);
857 static const char *dont_quote[] = {
858 "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
859 "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
863 static dbus_bool_t should_quote_opt(const char *key)
866 while (dont_quote[i] != NULL) {
867 if (strcmp(key, dont_quote[i]) == 0)
875 * wpas_dbus_iface_set_network - Set options for a configured network
876 * @message: Pointer to incoming dbus message
877 * @wpa_s: wpa_supplicant structure for a network interface
878 * @ssid: wpa_ssid structure for a configured network
879 * Returns: a dbus message containing a UINT32 indicating success (1) or
882 * Handler function for "set" method call of a configured network.
884 DBusMessage * wpas_dbus_iface_set_network(DBusMessage *message,
885 struct wpa_supplicant *wpa_s,
886 struct wpa_ssid *ssid)
888 DBusMessage *reply = NULL;
889 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
890 DBusMessageIter iter, iter_dict;
892 dbus_message_iter_init(message, &iter);
894 if (!wpa_dbus_dict_open_read(&iter, &iter_dict)) {
895 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
899 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
904 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
905 reply = wpas_dbus_new_invalid_opts_error(message,
910 /* Type conversions, since wpa_supplicant wants strings */
911 if (entry.type == DBUS_TYPE_ARRAY &&
912 entry.array_type == DBUS_TYPE_BYTE) {
913 if (entry.array_len <= 0)
916 size = entry.array_len * 2 + 1;
917 value = wpa_zalloc(size);
920 ret = wpa_snprintf_hex(value, size,
921 (u8 *) entry.bytearray_value,
925 } else if (entry.type == DBUS_TYPE_STRING) {
926 if (should_quote_opt(entry.key)) {
927 size = strlen(entry.str_value);
928 /* Zero-length option check */
931 size += 3; /* For quotes and terminator */
932 value = wpa_zalloc(size);
935 ret = snprintf(value, size, "\"%s\"",
937 if (ret < 0 || (size_t) ret != (size - 1))
940 value = strdup(entry.str_value);
944 } else if (entry.type == DBUS_TYPE_UINT32) {
945 value = wpa_zalloc(size);
948 ret = snprintf(value, size, "%u", entry.uint32_value);
951 } else if (entry.type == DBUS_TYPE_INT32) {
952 value = wpa_zalloc(size);
955 ret = snprintf(value, size, "%d", entry.int32_value);
961 if (wpa_config_set(ssid, entry.key, value, 0) < 0)
964 if ((strcmp(entry.key, "psk") == 0 &&
965 value[0] == '"' && ssid->ssid_len) ||
966 (strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
967 wpa_config_update_psk(ssid);
970 wpa_dbus_dict_entry_clear(&entry);
975 reply = wpas_dbus_new_invalid_opts_error(message, entry.key);
976 wpa_dbus_dict_entry_clear(&entry);
981 reply = wpas_dbus_new_success_reply(message);
989 * wpas_dbus_iface_enable_network - Mark a configured network as enabled
990 * @message: Pointer to incoming dbus message
991 * @wpa_s: wpa_supplicant structure for a network interface
992 * @ssid: wpa_ssid structure for a configured network
993 * Returns: A dbus message containing a UINT32 indicating success (1) or
996 * Handler function for "enable" method call of a configured network.
998 DBusMessage * wpas_dbus_iface_enable_network(DBusMessage *message,
999 struct wpa_supplicant *wpa_s,
1000 struct wpa_ssid *ssid)
1002 if (wpa_s->current_ssid == NULL && ssid->disabled) {
1004 * Try to reassociate since there is no current configuration
1005 * and a new network was made available.
1007 wpa_s->reassociate = 1;
1008 wpa_supplicant_req_scan(wpa_s, 0, 0);
1012 return wpas_dbus_new_success_reply(message);
1017 * wpas_dbus_iface_disable_network - Mark a configured network as disabled
1018 * @message: Pointer to incoming dbus message
1019 * @wpa_s: wpa_supplicant structure for a network interface
1020 * @ssid: wpa_ssid structure for a configured network
1021 * Returns: A dbus message containing a UINT32 indicating success (1) or
1024 * Handler function for "disable" method call of a configured network.
1026 DBusMessage * wpas_dbus_iface_disable_network(DBusMessage *message,
1027 struct wpa_supplicant *wpa_s,
1028 struct wpa_ssid *ssid)
1030 if (ssid == wpa_s->current_ssid)
1031 wpa_supplicant_disassociate(wpa_s, REASON_DEAUTH_LEAVING);
1034 return wpas_dbus_new_success_reply(message);
1039 * wpas_dbus_iface_select_network - Attempt association with a configured network
1040 * @message: Pointer to incoming dbus message
1041 * @wpa_s: wpa_supplicant structure for a network interface
1042 * Returns: A dbus message containing a UINT32 indicating success (1) or
1045 * Handler function for "selectNetwork" method call of network interface.
1047 DBusMessage * wpas_dbus_iface_select_network(DBusMessage *message,
1048 struct wpa_supplicant *wpa_s)
1050 DBusMessage *reply = NULL;
1052 struct wpa_ssid *ssid;
1053 char *iface_obj_path = NULL;
1054 char *network = NULL;
1056 if (strlen(dbus_message_get_signature(message)) == 0) {
1058 ssid = wpa_s->conf->ssid;
1063 wpa_s->reassociate = 1;
1064 wpa_supplicant_req_scan(wpa_s, 0, 0);
1066 const char *obj_path;
1069 if (!dbus_message_get_args(message, NULL,
1070 DBUS_TYPE_OBJECT_PATH, &op,
1071 DBUS_TYPE_INVALID)) {
1072 reply = wpas_dbus_new_invalid_opts_error(message,
1077 /* Extract the network number */
1078 iface_obj_path = wpas_dbus_decompose_object_path(op,
1081 if (iface_obj_path == NULL) {
1082 reply = wpas_dbus_new_invalid_iface_error(message);
1085 /* Ensure the object path really points to this interface */
1086 obj_path = wpa_supplicant_get_dbus_path(wpa_s);
1087 if (strcmp(iface_obj_path, obj_path) != 0) {
1088 reply = wpas_dbus_new_invalid_network_error(message);
1092 nid = strtoul(network, NULL, 10);
1093 if (errno == EINVAL) {
1094 reply = wpas_dbus_new_invalid_network_error(message);
1098 ssid = wpa_config_get_network(wpa_s->conf, nid);
1100 reply = wpas_dbus_new_invalid_network_error(message);
1104 /* Finally, associate with the network */
1105 if (ssid != wpa_s->current_ssid && wpa_s->current_ssid)
1106 wpa_supplicant_disassociate(wpa_s,
1107 REASON_DEAUTH_LEAVING);
1109 /* Mark all other networks disabled and trigger reassociation
1111 ssid = wpa_s->conf->ssid;
1113 ssid->disabled = (nid != ssid->id);
1116 wpa_s->disconnected = 0;
1117 wpa_s->reassociate = 1;
1118 wpa_supplicant_req_scan(wpa_s, 0, 0);
1121 reply = wpas_dbus_new_success_reply(message);
1124 free(iface_obj_path);
1131 * wpas_dbus_iface_disconnect - Terminate the current connection
1132 * @message: Pointer to incoming dbus message
1133 * @wpa_s: wpa_supplicant structure for a network interface
1134 * Returns: A dbus message containing a UINT32 indicating success (1) or
1137 * Handler function for "disconnect" method call of network interface.
1139 DBusMessage * wpas_dbus_iface_disconnect(DBusMessage *message,
1140 struct wpa_supplicant *wpa_s)
1142 wpa_s->disconnected = 1;
1143 wpa_supplicant_disassociate(wpa_s, REASON_DEAUTH_LEAVING);
1145 return wpas_dbus_new_success_reply(message);
1150 * wpas_dbus_iface_set_ap_scan - Control roaming mode
1151 * @message: Pointer to incoming dbus message
1152 * @wpa_s: wpa_supplicant structure for a network interface
1153 * Returns: A dbus message containing a UINT32 indicating success (1) or
1156 * Handler function for "setAPScan" method call.
1158 DBusMessage * wpas_dbus_iface_set_ap_scan(DBusMessage *message,
1159 struct wpa_supplicant *wpa_s)
1161 DBusMessage *reply = NULL;
1162 dbus_uint32_t ap_scan = 1;
1164 if (!dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &ap_scan,
1165 DBUS_TYPE_INVALID)) {
1166 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
1171 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
1174 wpa_s->conf->ap_scan = ap_scan;
1175 reply = wpas_dbus_new_success_reply(message);
1183 * wpas_dbus_iface_get_state - Get interface state
1184 * @message: Pointer to incoming dbus message
1185 * @wpa_s: wpa_supplicant structure for a network interface
1186 * Returns: A dbus message containing a STRING representing the current
1189 * Handler function for "state" method call.
1191 DBusMessage * wpas_dbus_iface_get_state(DBusMessage *message,
1192 struct wpa_supplicant *wpa_s)
1194 DBusMessage *reply = NULL;
1195 const char *str_state;
1197 reply = dbus_message_new_method_return(message);
1198 if (reply != NULL) {
1199 str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
1200 dbus_message_append_args(reply, DBUS_TYPE_STRING, &str_state,
1209 * wpas_dbus_iface_set_blobs - Store named binary blobs (ie, for certificates)
1210 * @message: Pointer to incoming dbus message
1211 * @global: %wpa_supplicant global data structure
1212 * Returns: A dbus message containing a UINT32 indicating success (1) or
1215 * Asks wpa_supplicant to internally store a one or more binary blobs.
1217 DBusMessage * wpas_dbus_iface_set_blobs(DBusMessage *message,
1218 struct wpa_supplicant *wpa_s)
1220 DBusMessage *reply = NULL;
1221 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
1222 DBusMessageIter iter, iter_dict;
1224 dbus_message_iter_init(message, &iter);
1226 if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
1227 return wpas_dbus_new_invalid_opts_error(message, NULL);
1229 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
1230 struct wpa_config_blob *blob;
1232 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
1233 reply = wpas_dbus_new_invalid_opts_error(message,
1238 if (entry.type != DBUS_TYPE_ARRAY ||
1239 entry.array_type != DBUS_TYPE_BYTE) {
1240 reply = wpas_dbus_new_invalid_opts_error(
1241 message, "Byte array expected.");
1245 if ((entry.array_len <= 0) || (entry.array_len > 65536) ||
1246 !strlen(entry.key)) {
1247 reply = wpas_dbus_new_invalid_opts_error(
1248 message, "Invalid array size.");
1252 blob = os_zalloc(sizeof(*blob));
1254 reply = dbus_message_new_error(
1255 message, WPAS_ERROR_ADD_ERROR,
1256 "Not enough memory to add blob.");
1259 blob->data = os_zalloc(entry.array_len);
1260 if (blob->data == NULL) {
1261 reply = dbus_message_new_error(
1262 message, WPAS_ERROR_ADD_ERROR,
1263 "Not enough memory to add blob data.");
1268 blob->name = os_strdup(entry.key);
1269 blob->len = entry.array_len;
1270 os_memcpy(blob->data, (u8 *) entry.bytearray_value,
1272 if (blob->name == NULL || blob->data == NULL) {
1273 wpa_config_free_blob(blob);
1274 reply = dbus_message_new_error(
1275 message, WPAS_ERROR_ADD_ERROR,
1276 "Error adding blob.");
1281 wpa_config_remove_blob(wpa_s->conf, blob->name);
1282 wpa_config_set_blob(wpa_s->conf, blob);
1283 wpa_dbus_dict_entry_clear(&entry);
1285 wpa_dbus_dict_entry_clear(&entry);
1287 return reply ? reply : wpas_dbus_new_success_reply(message);
1292 * wpas_dbus_iface_remove_blob - Remove named binary blobs
1293 * @message: Pointer to incoming dbus message
1294 * @global: %wpa_supplicant global data structure
1295 * Returns: A dbus message containing a UINT32 indicating success (1) or
1298 * Asks wpa_supplicant to remove one or more previously stored binary blobs.
1300 DBusMessage * wpas_dbus_iface_remove_blobs(DBusMessage *message,
1301 struct wpa_supplicant *wpa_s)
1303 DBusMessageIter iter, array;
1304 char *err_msg = NULL;
1306 dbus_message_iter_init(message, &iter);
1308 if ((dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY) ||
1309 (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_STRING))
1310 return wpas_dbus_new_invalid_opts_error(message, NULL);
1312 dbus_message_iter_recurse(&iter, &array);
1313 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
1316 dbus_message_iter_get_basic(&array, &name);
1318 err_msg = "Invalid blob name.";
1320 if (wpa_config_remove_blob(wpa_s->conf, name) != 0)
1321 err_msg = "Error removing blob.";
1322 dbus_message_iter_next(&array);
1326 return dbus_message_new_error(message, WPAS_ERROR_REMOVE_ERROR,
1330 return wpas_dbus_new_success_reply(message);