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"
31 extern int wpa_debug_level;
32 extern int wpa_debug_show_keys;
33 extern int wpa_debug_timestamp;
36 * wpas_dbus_new_invalid_opts_error - Return a new invalid options error message
37 * @message: Pointer to incoming dbus message this error refers to
38 * Returns: a dbus error message
40 * Convenience function to create and return an invalid options error
42 static DBusMessage * wpas_dbus_new_invalid_opts_error(DBusMessage *message,
47 reply = dbus_message_new_error(message, WPAS_ERROR_INVALID_OPTS,
48 "Did not receive correct message "
51 dbus_message_append_args(reply, DBUS_TYPE_STRING, &arg,
59 * wpas_dbus_new_success_reply - Return a new success reply message
60 * @message: Pointer to incoming dbus message this reply refers to
61 * Returns: a dbus message containing a single UINT32 that indicates
62 * success (ie, a value of 1)
64 * Convenience function to create and return a success reply message
66 static DBusMessage * wpas_dbus_new_success_reply(DBusMessage *message)
69 unsigned int success = 1;
71 reply = dbus_message_new_method_return(message);
72 dbus_message_append_args(reply, DBUS_TYPE_UINT32, &success,
78 static void wpas_dbus_free_wpa_interface(struct wpa_interface *iface)
80 os_free((char *) iface->driver);
81 os_free((char *) iface->driver_param);
82 os_free((char *) iface->confname);
83 os_free((char *) iface->bridge_ifname);
88 * wpas_dbus_global_add_interface - Request registration of a network interface
89 * @message: Pointer to incoming dbus message
90 * @global: %wpa_supplicant global data structure
91 * Returns: The object path of the new interface object,
92 * or a dbus error message with more information
94 * Handler function for "addInterface" method call. Handles requests
95 * by dbus clients to register a network interface that wpa_supplicant
98 DBusMessage * wpas_dbus_global_add_interface(DBusMessage *message,
99 struct wpa_global *global)
101 struct wpa_interface iface;
103 DBusMessage *reply = NULL;
104 DBusMessageIter iter;
106 os_memset(&iface, 0, sizeof(iface));
108 dbus_message_iter_init(message, &iter);
110 /* First argument: interface name (DBUS_TYPE_STRING)
111 * Required; must be non-zero length
113 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
115 dbus_message_iter_get_basic(&iter, &ifname);
116 if (!os_strlen(ifname))
118 iface.ifname = ifname;
120 /* Second argument: dict of options */
121 if (dbus_message_iter_next(&iter)) {
122 DBusMessageIter iter_dict;
123 struct wpa_dbus_dict_entry entry;
125 if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
127 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
128 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
130 if (!strcmp(entry.key, "driver") &&
131 (entry.type == DBUS_TYPE_STRING)) {
132 iface.driver = strdup(entry.str_value);
133 if (iface.driver == NULL)
135 } else if (!strcmp(entry.key, "driver-params") &&
136 (entry.type == DBUS_TYPE_STRING)) {
137 iface.driver_param = strdup(entry.str_value);
138 if (iface.driver_param == NULL)
140 } else if (!strcmp(entry.key, "config-file") &&
141 (entry.type == DBUS_TYPE_STRING)) {
142 iface.confname = strdup(entry.str_value);
143 if (iface.confname == NULL)
145 } else if (!strcmp(entry.key, "bridge-ifname") &&
146 (entry.type == DBUS_TYPE_STRING)) {
147 iface.bridge_ifname = strdup(entry.str_value);
148 if (iface.bridge_ifname == NULL)
151 wpa_dbus_dict_entry_clear(&entry);
154 wpa_dbus_dict_entry_clear(&entry);
159 * Try to get the wpa_supplicant record for this iface, return
160 * an error if we already control it.
162 if (wpa_supplicant_get_iface(global, iface.ifname) != NULL) {
163 reply = dbus_message_new_error(message,
164 WPAS_ERROR_EXISTS_ERROR,
165 "wpa_supplicant already "
166 "controls this interface.");
168 struct wpa_supplicant *wpa_s;
169 /* Otherwise, have wpa_supplicant attach to it. */
170 if ((wpa_s = wpa_supplicant_add_iface(global, &iface))) {
171 const char *path = wpa_supplicant_get_dbus_path(wpa_s);
172 reply = dbus_message_new_method_return(message);
173 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
174 &path, DBUS_TYPE_INVALID);
176 reply = dbus_message_new_error(message,
177 WPAS_ERROR_ADD_ERROR,
179 "couldn't grab this "
183 wpas_dbus_free_wpa_interface(&iface);
187 wpas_dbus_free_wpa_interface(&iface);
188 return wpas_dbus_new_invalid_opts_error(message, NULL);
193 * wpas_dbus_global_remove_interface - Request deregistration of an interface
194 * @message: Pointer to incoming dbus message
195 * @global: wpa_supplicant global data structure
196 * Returns: a dbus message containing a UINT32 indicating success (1) or
197 * failure (0), or returns a dbus error message with more information
199 * Handler function for "removeInterface" method call. Handles requests
200 * by dbus clients to deregister a network interface that wpa_supplicant
203 DBusMessage * wpas_dbus_global_remove_interface(DBusMessage *message,
204 struct wpa_global *global)
206 struct wpa_supplicant *wpa_s;
208 DBusMessage *reply = NULL;
210 if (!dbus_message_get_args(message, NULL,
211 DBUS_TYPE_OBJECT_PATH, &path,
212 DBUS_TYPE_INVALID)) {
213 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
217 wpa_s = wpa_supplicant_get_iface_by_dbus_path(global, path);
219 reply = wpas_dbus_new_invalid_iface_error(message);
223 if (!wpa_supplicant_remove_iface(global, wpa_s)) {
224 reply = wpas_dbus_new_success_reply(message);
226 reply = dbus_message_new_error(message,
227 WPAS_ERROR_REMOVE_ERROR,
228 "wpa_supplicant couldn't "
229 "remove this interface.");
238 * wpas_dbus_global_get_interface - Get the object path for an interface name
239 * @message: Pointer to incoming dbus message
240 * @global: %wpa_supplicant global data structure
241 * Returns: The object path of the interface object,
242 * or a dbus error message with more information
244 * Handler function for "getInterface" method call. Handles requests
245 * by dbus clients for the object path of an specific network interface.
247 DBusMessage * wpas_dbus_global_get_interface(DBusMessage *message,
248 struct wpa_global *global)
250 DBusMessage *reply = NULL;
253 struct wpa_supplicant *wpa_s;
255 if (!dbus_message_get_args(message, NULL,
256 DBUS_TYPE_STRING, &ifname,
257 DBUS_TYPE_INVALID)) {
258 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
262 wpa_s = wpa_supplicant_get_iface(global, ifname);
264 reply = wpas_dbus_new_invalid_iface_error(message);
268 path = wpa_supplicant_get_dbus_path(wpa_s);
270 reply = dbus_message_new_error(message,
271 WPAS_ERROR_INTERNAL_ERROR,
272 "an internal error occurred "
273 "getting the interface.");
277 reply = dbus_message_new_method_return(message);
278 dbus_message_append_args(reply,
279 DBUS_TYPE_OBJECT_PATH, &path,
288 * wpas_dbus_global_set_debugparams- Set the debug params
289 * @message: Pointer to incoming dbus message
290 * @global: %wpa_supplicant global data structure
291 * Returns: a dbus message containing a UINT32 indicating success (1) or
292 * failure (0), or returns a dbus error message with more information
294 * Handler function for "setDebugParams" method call. Handles requests
295 * by dbus clients for the object path of an specific network interface.
297 DBusMessage * wpas_dbus_global_set_debugparams(DBusMessage *message,
298 struct wpa_global *global)
300 DBusMessage *reply = NULL;
302 dbus_bool_t debug_timestamp;
303 dbus_bool_t debug_show_keys;
305 if (!dbus_message_get_args(message, NULL,
306 DBUS_TYPE_INT32, &debug_level,
307 DBUS_TYPE_BOOLEAN, &debug_timestamp,
308 DBUS_TYPE_BOOLEAN, &debug_show_keys,
309 DBUS_TYPE_INVALID)) {
310 return wpas_dbus_new_invalid_opts_error(message, NULL);
313 if (wpa_supplicant_set_debug_params(global, debug_level,
314 debug_timestamp ? 1 : 0,
315 debug_show_keys ? 1 : 0)) {
316 return wpas_dbus_new_invalid_opts_error(message, NULL);
319 reply = wpas_dbus_new_success_reply(message);
326 * wpas_dbus_iface_scan - Request a wireless scan on an interface
327 * @message: Pointer to incoming dbus message
328 * @wpa_s: wpa_supplicant structure for a network interface
329 * Returns: a dbus message containing a UINT32 indicating success (1) or
332 * Handler function for "scan" method call of a network device. Requests
333 * that wpa_supplicant perform a wireless scan as soon as possible
334 * on a particular wireless interface.
336 DBusMessage * wpas_dbus_iface_scan(DBusMessage *message,
337 struct wpa_supplicant *wpa_s)
340 wpa_supplicant_req_scan(wpa_s, 0, 0);
341 return wpas_dbus_new_success_reply(message);
346 * wpas_dbus_iface_scan_results - Get the results of a recent scan request
347 * @message: Pointer to incoming dbus message
348 * @wpa_s: wpa_supplicant structure for a network interface
349 * Returns: a dbus message containing a dbus array of objects paths, or returns
350 * a dbus error message if not scan results could be found
352 * Handler function for "scanResults" method call of a network device. Returns
353 * a dbus message containing the object paths of wireless networks found.
355 DBusMessage * wpas_dbus_iface_scan_results(DBusMessage *message,
356 struct wpa_supplicant *wpa_s)
358 DBusMessage *reply = NULL;
359 DBusMessageIter iter;
360 DBusMessageIter sub_iter;
363 /* Ensure we've actually got scan results to return */
364 if (wpa_s->scan_res == NULL &&
365 wpa_supplicant_get_scan_results(wpa_s) < 0) {
366 reply = dbus_message_new_error(message, WPAS_ERROR_SCAN_ERROR,
367 "An error ocurred getting scan "
372 /* Create and initialize the return message */
373 reply = dbus_message_new_method_return(message);
374 dbus_message_iter_init_append(reply, &iter);
375 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
376 DBUS_TYPE_OBJECT_PATH_AS_STRING,
379 /* Loop through scan results and append each result's object path */
380 for (i = 0; i < wpa_s->scan_res->num; i++) {
381 struct wpa_scan_res *res = wpa_s->scan_res->res[i];
384 path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
386 perror("wpas_dbus_iface_scan_results[dbus]: out of "
388 wpa_printf(MSG_ERROR, "dbus control interface: not "
389 "enough memory to send scan results "
393 /* Construct the object path for this network. Note that ':'
394 * is not a valid character in dbus object paths.
396 os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
397 "%s/" WPAS_DBUS_BSSIDS_PART "/"
398 WPAS_DBUS_BSSID_FORMAT,
399 wpa_supplicant_get_dbus_path(wpa_s),
400 MAC2STR(res->bssid));
401 dbus_message_iter_append_basic(&sub_iter,
402 DBUS_TYPE_OBJECT_PATH, &path);
406 dbus_message_iter_close_container(&iter, &sub_iter);
414 * wpas_dbus_bssid_properties - Return the properties of a scanned network
415 * @message: Pointer to incoming dbus message
416 * @wpa_s: wpa_supplicant structure for a network interface
417 * @res: wpa_supplicant scan result for which to get properties
418 * Returns: a dbus message containing the properties for the requested network
420 * Handler function for "properties" method call of a scanned network.
421 * Returns a dbus message containing the the properties.
423 DBusMessage * wpas_dbus_bssid_properties(DBusMessage *message,
424 struct wpa_supplicant *wpa_s,
425 struct wpa_scan_res *res)
427 DBusMessage *reply = NULL;
428 DBusMessageIter iter, iter_dict;
431 /* Dump the properties into a dbus message */
432 reply = dbus_message_new_method_return(message);
434 dbus_message_iter_init_append(reply, &iter);
435 if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
438 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "bssid",
439 (const char *) res->bssid,
443 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
445 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "ssid",
446 (const char *) (ie + 2),
451 ie = wpa_scan_get_vendor_ie(res, WPA_IE_VENDOR_TYPE);
453 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "wpaie",
459 ie = wpa_scan_get_ie(res, WLAN_EID_RSN);
461 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "rsnie",
467 ie = wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE);
469 if (!wpa_dbus_dict_append_byte_array(&iter_dict, "wpsie",
476 if (!wpa_dbus_dict_append_int32(&iter_dict, "frequency",
480 if (!wpa_dbus_dict_append_uint16(&iter_dict, "capabilities",
483 if (!(res->flags & WPA_SCAN_QUAL_INVALID) &&
484 !wpa_dbus_dict_append_int32(&iter_dict, "quality", res->qual))
486 if (!(res->flags & WPA_SCAN_NOISE_INVALID) &&
487 !wpa_dbus_dict_append_int32(&iter_dict, "noise", res->noise))
489 if (!(res->flags & WPA_SCAN_LEVEL_INVALID) &&
490 !wpa_dbus_dict_append_int32(&iter_dict, "level", res->level))
492 if (!wpa_dbus_dict_append_int32(&iter_dict, "maxrate",
493 wpa_scan_get_max_rate(res) * 500000))
496 if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
503 dbus_message_unref(reply);
504 return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
505 "an internal error occurred returning "
506 "BSSID properties.");
511 * wpas_dbus_iface_capabilities - Return interface capabilities
512 * @message: Pointer to incoming dbus message
513 * @wpa_s: wpa_supplicant structure for a network interface
514 * Returns: A dbus message containing a dict of strings
516 * Handler function for "capabilities" method call of an interface.
518 DBusMessage * wpas_dbus_iface_capabilities(DBusMessage *message,
519 struct wpa_supplicant *wpa_s)
521 DBusMessage *reply = NULL;
522 struct wpa_driver_capa capa;
524 DBusMessageIter iter, iter_dict;
527 dbus_bool_t strict = FALSE;
528 DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
530 if (!dbus_message_get_args(message, NULL,
531 DBUS_TYPE_BOOLEAN, &strict,
535 reply = dbus_message_new_method_return(message);
537 dbus_message_iter_init_append(reply, &iter);
538 if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
542 eap_methods = eap_get_names_as_string_array(&num_items);
544 dbus_bool_t success = FALSE;
547 success = wpa_dbus_dict_append_string_array(
548 &iter_dict, "eap", (const char **) eap_methods,
551 /* free returned method array */
552 while (eap_methods[i])
553 os_free(eap_methods[i++]);
554 os_free(eap_methods);
560 res = wpa_drv_get_capa(wpa_s, &capa);
562 /***** pairwise cipher */
565 const char *args[] = {"CCMP", "TKIP", "NONE"};
566 if (!wpa_dbus_dict_append_string_array(
567 &iter_dict, "pairwise", args,
568 sizeof(args) / sizeof(char*)))
572 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "pairwise",
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.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
591 if (!wpa_dbus_dict_string_array_add_element(
592 &iter_array, "NONE"))
596 if (!wpa_dbus_dict_end_string_array(&iter_dict,
603 /***** group cipher */
606 const char *args[] = {
607 "CCMP", "TKIP", "WEP104", "WEP40"
609 if (!wpa_dbus_dict_append_string_array(
610 &iter_dict, "group", args,
611 sizeof(args) / sizeof(char*)))
615 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "group",
621 if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
622 if (!wpa_dbus_dict_string_array_add_element(
623 &iter_array, "CCMP"))
627 if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
628 if (!wpa_dbus_dict_string_array_add_element(
629 &iter_array, "TKIP"))
633 if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) {
634 if (!wpa_dbus_dict_string_array_add_element(
635 &iter_array, "WEP104"))
639 if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) {
640 if (!wpa_dbus_dict_string_array_add_element(
641 &iter_array, "WEP40"))
645 if (!wpa_dbus_dict_end_string_array(&iter_dict,
652 /***** key management */
655 const char *args[] = {
656 "WPA-PSK", "WPA-EAP", "IEEE8021X", "WPA-NONE",
659 if (!wpa_dbus_dict_append_string_array(
660 &iter_dict, "key_mgmt", args,
661 sizeof(args) / sizeof(char*)))
665 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "key_mgmt",
671 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
675 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
679 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
680 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
681 if (!wpa_dbus_dict_string_array_add_element(
682 &iter_array, "WPA-EAP"))
686 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
687 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
688 if (!wpa_dbus_dict_string_array_add_element(
689 &iter_array, "WPA-PSK"))
693 if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
694 if (!wpa_dbus_dict_string_array_add_element(
695 &iter_array, "WPA-NONE"))
699 if (!wpa_dbus_dict_end_string_array(&iter_dict,
706 /***** WPA protocol */
709 const char *args[] = { "RSN", "WPA" };
710 if (!wpa_dbus_dict_append_string_array(
711 &iter_dict, "proto", args,
712 sizeof(args) / sizeof(char*)))
716 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "proto",
722 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
723 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
724 if (!wpa_dbus_dict_string_array_add_element(
729 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
730 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
731 if (!wpa_dbus_dict_string_array_add_element(
736 if (!wpa_dbus_dict_end_string_array(&iter_dict,
746 const char *args[] = { "OPEN", "SHARED", "LEAP" };
747 if (!wpa_dbus_dict_append_string_array(
748 &iter_dict, "auth_alg", args,
749 sizeof(args) / sizeof(char*)))
753 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "auth_alg",
759 if (capa.auth & (WPA_DRIVER_AUTH_OPEN)) {
760 if (!wpa_dbus_dict_string_array_add_element(
761 &iter_array, "OPEN"))
765 if (capa.auth & (WPA_DRIVER_AUTH_SHARED)) {
766 if (!wpa_dbus_dict_string_array_add_element(
767 &iter_array, "SHARED"))
771 if (capa.auth & (WPA_DRIVER_AUTH_LEAP)) {
772 if (!wpa_dbus_dict_string_array_add_element(
773 &iter_array, "LEAP"))
777 if (!wpa_dbus_dict_end_string_array(&iter_dict,
784 if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
791 dbus_message_unref(reply);
792 return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
793 "an internal error occurred returning "
794 "interface capabilities.");
799 * wpas_dbus_iface_add_network - Add a new configured network
800 * @message: Pointer to incoming dbus message
801 * @wpa_s: wpa_supplicant structure for a network interface
802 * Returns: A dbus message containing the object path of the new network
804 * Handler function for "addNetwork" method call of a network interface.
806 DBusMessage * wpas_dbus_iface_add_network(DBusMessage *message,
807 struct wpa_supplicant *wpa_s)
809 DBusMessage *reply = NULL;
810 struct wpa_ssid *ssid;
813 path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
815 perror("wpas_dbus_iface_scan_results[dbus]: out of "
817 wpa_printf(MSG_ERROR, "dbus control interface: not "
818 "enough memory to send scan results "
823 ssid = wpa_config_add_network(wpa_s->conf);
825 reply = dbus_message_new_error(message,
826 WPAS_ERROR_ADD_NETWORK_ERROR,
827 "wpa_supplicant could not add "
828 "a network on this interface.");
831 wpas_notify_network_added(wpa_s, ssid);
833 wpa_config_set_network_defaults(ssid);
835 /* Construct the object path for this network. */
836 os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
837 "%s/" WPAS_DBUS_NETWORKS_PART "/%d",
838 wpa_supplicant_get_dbus_path(wpa_s),
841 reply = dbus_message_new_method_return(message);
842 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
843 &path, DBUS_TYPE_INVALID);
852 * wpas_dbus_iface_remove_network - Remove a configured network
853 * @message: Pointer to incoming dbus message
854 * @wpa_s: wpa_supplicant structure for a network interface
855 * Returns: A dbus message containing a UINT32 indicating success (1) or
858 * Handler function for "removeNetwork" method call of a network interface.
860 DBusMessage * wpas_dbus_iface_remove_network(DBusMessage *message,
861 struct wpa_supplicant *wpa_s)
863 DBusMessage *reply = NULL;
865 char *iface = NULL, *net_id = NULL;
867 struct wpa_ssid *ssid;
869 if (!dbus_message_get_args(message, NULL,
870 DBUS_TYPE_OBJECT_PATH, &op,
871 DBUS_TYPE_INVALID)) {
872 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
876 /* Extract the network ID */
877 iface = wpas_dbus_decompose_object_path(op, &net_id, NULL);
879 reply = wpas_dbus_new_invalid_network_error(message);
883 /* Ensure the network is actually a child of this interface */
884 if (strcmp(iface, wpa_supplicant_get_dbus_path(wpa_s)) != 0) {
885 reply = wpas_dbus_new_invalid_network_error(message);
889 id = strtoul(net_id, NULL, 10);
890 ssid = wpa_config_get_network(wpa_s->conf, id);
892 reply = wpas_dbus_new_invalid_network_error(message);
896 wpas_notify_network_removed(wpa_s, ssid);
898 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
899 reply = dbus_message_new_error(message,
900 WPAS_ERROR_REMOVE_NETWORK_ERROR,
901 "error removing the specified "
902 "on this interface.");
906 if (ssid == wpa_s->current_ssid)
907 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
908 reply = wpas_dbus_new_success_reply(message);
917 static const char *dont_quote[] = {
918 "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
919 "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
924 static dbus_bool_t should_quote_opt(const char *key)
927 while (dont_quote[i] != NULL) {
928 if (strcmp(key, dont_quote[i]) == 0)
937 * wpas_dbus_iface_set_network - Set options for a configured network
938 * @message: Pointer to incoming dbus message
939 * @wpa_s: wpa_supplicant structure for a network interface
940 * @ssid: wpa_ssid structure for a configured network
941 * Returns: a dbus message containing a UINT32 indicating success (1) or
944 * Handler function for "set" method call of a configured network.
946 DBusMessage * wpas_dbus_iface_set_network(DBusMessage *message,
947 struct wpa_supplicant *wpa_s,
948 struct wpa_ssid *ssid)
950 DBusMessage *reply = NULL;
951 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
952 DBusMessageIter iter, iter_dict;
954 dbus_message_iter_init(message, &iter);
956 if (!wpa_dbus_dict_open_read(&iter, &iter_dict)) {
957 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
961 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
966 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
967 reply = wpas_dbus_new_invalid_opts_error(message,
972 /* Type conversions, since wpa_supplicant wants strings */
973 if (entry.type == DBUS_TYPE_ARRAY &&
974 entry.array_type == DBUS_TYPE_BYTE) {
975 if (entry.array_len <= 0)
978 size = entry.array_len * 2 + 1;
979 value = os_zalloc(size);
982 ret = wpa_snprintf_hex(value, size,
983 (u8 *) entry.bytearray_value,
987 } else if (entry.type == DBUS_TYPE_STRING) {
988 if (should_quote_opt(entry.key)) {
989 size = os_strlen(entry.str_value);
990 /* Zero-length option check */
993 size += 3; /* For quotes and terminator */
994 value = os_zalloc(size);
997 ret = os_snprintf(value, size, "\"%s\"",
999 if (ret < 0 || (size_t) ret != (size - 1))
1002 value = os_strdup(entry.str_value);
1006 } else if (entry.type == DBUS_TYPE_UINT32) {
1007 value = os_zalloc(size);
1010 ret = os_snprintf(value, size, "%u",
1011 entry.uint32_value);
1014 } else if (entry.type == DBUS_TYPE_INT32) {
1015 value = os_zalloc(size);
1018 ret = os_snprintf(value, size, "%d",
1025 if (wpa_config_set(ssid, entry.key, value, 0) < 0)
1028 if ((os_strcmp(entry.key, "psk") == 0 &&
1029 value[0] == '"' && ssid->ssid_len) ||
1030 (os_strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
1031 wpa_config_update_psk(ssid);
1034 wpa_dbus_dict_entry_clear(&entry);
1039 reply = wpas_dbus_new_invalid_opts_error(message, entry.key);
1040 wpa_dbus_dict_entry_clear(&entry);
1045 reply = wpas_dbus_new_success_reply(message);
1053 * wpas_dbus_iface_enable_network - Mark a configured network as enabled
1054 * @message: Pointer to incoming dbus message
1055 * @wpa_s: wpa_supplicant structure for a network interface
1056 * @ssid: wpa_ssid structure for a configured network
1057 * Returns: A dbus message containing a UINT32 indicating success (1) or
1060 * Handler function for "enable" method call of a configured network.
1062 DBusMessage * wpas_dbus_iface_enable_network(DBusMessage *message,
1063 struct wpa_supplicant *wpa_s,
1064 struct wpa_ssid *ssid)
1066 wpa_supplicant_enable_network(wpa_s, ssid);
1067 return wpas_dbus_new_success_reply(message);
1072 * wpas_dbus_iface_disable_network - Mark a configured network as disabled
1073 * @message: Pointer to incoming dbus message
1074 * @wpa_s: wpa_supplicant structure for a network interface
1075 * @ssid: wpa_ssid structure for a configured network
1076 * Returns: A dbus message containing a UINT32 indicating success (1) or
1079 * Handler function for "disable" method call of a configured network.
1081 DBusMessage * wpas_dbus_iface_disable_network(DBusMessage *message,
1082 struct wpa_supplicant *wpa_s,
1083 struct wpa_ssid *ssid)
1085 wpa_supplicant_disable_network(wpa_s, ssid);
1086 return wpas_dbus_new_success_reply(message);
1091 * wpas_dbus_iface_select_network - Attempt association with a configured network
1092 * @message: Pointer to incoming dbus message
1093 * @wpa_s: wpa_supplicant structure for a network interface
1094 * Returns: A dbus message containing a UINT32 indicating success (1) or
1097 * Handler function for "selectNetwork" method call of network interface.
1099 DBusMessage * wpas_dbus_iface_select_network(DBusMessage *message,
1100 struct wpa_supplicant *wpa_s)
1102 DBusMessage *reply = NULL;
1104 struct wpa_ssid *ssid;
1105 char *iface_obj_path = NULL;
1106 char *network = NULL;
1108 if (os_strlen(dbus_message_get_signature(message)) == 0) {
1112 const char *obj_path;
1115 if (!dbus_message_get_args(message, NULL,
1116 DBUS_TYPE_OBJECT_PATH, &op,
1117 DBUS_TYPE_INVALID)) {
1118 reply = wpas_dbus_new_invalid_opts_error(message,
1123 /* Extract the network number */
1124 iface_obj_path = wpas_dbus_decompose_object_path(op,
1127 if (iface_obj_path == NULL) {
1128 reply = wpas_dbus_new_invalid_iface_error(message);
1131 /* Ensure the object path really points to this interface */
1132 obj_path = wpa_supplicant_get_dbus_path(wpa_s);
1133 if (os_strcmp(iface_obj_path, obj_path) != 0) {
1134 reply = wpas_dbus_new_invalid_network_error(message);
1138 nid = strtoul(network, NULL, 10);
1139 if (errno == EINVAL) {
1140 reply = wpas_dbus_new_invalid_network_error(message);
1144 ssid = wpa_config_get_network(wpa_s->conf, nid);
1146 reply = wpas_dbus_new_invalid_network_error(message);
1151 /* Finally, associate with the network */
1152 wpa_supplicant_select_network(wpa_s, ssid);
1154 reply = wpas_dbus_new_success_reply(message);
1157 os_free(iface_obj_path);
1164 * wpas_dbus_iface_disconnect - Terminate the current connection
1165 * @message: Pointer to incoming dbus message
1166 * @wpa_s: wpa_supplicant structure for a network interface
1167 * Returns: A dbus message containing a UINT32 indicating success (1) or
1170 * Handler function for "disconnect" method call of network interface.
1172 DBusMessage * wpas_dbus_iface_disconnect(DBusMessage *message,
1173 struct wpa_supplicant *wpa_s)
1175 wpa_s->disconnected = 1;
1176 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1178 return wpas_dbus_new_success_reply(message);
1183 * wpas_dbus_iface_set_ap_scan - Control roaming mode
1184 * @message: Pointer to incoming dbus message
1185 * @wpa_s: wpa_supplicant structure for a network interface
1186 * Returns: A dbus message containing a UINT32 indicating success (1) or
1189 * Handler function for "setAPScan" method call.
1191 DBusMessage * wpas_dbus_iface_set_ap_scan(DBusMessage *message,
1192 struct wpa_supplicant *wpa_s)
1194 DBusMessage *reply = NULL;
1195 dbus_uint32_t ap_scan = 1;
1197 if (!dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &ap_scan,
1198 DBUS_TYPE_INVALID)) {
1199 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
1203 if (wpa_supplicant_set_ap_scan(wpa_s, ap_scan)) {
1204 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
1208 reply = wpas_dbus_new_success_reply(message);
1216 * wpas_dbus_iface_set_smartcard_modules - Set smartcard related module paths
1217 * @message: Pointer to incoming dbus message
1218 * @wpa_s: wpa_supplicant structure for a network interface
1219 * Returns: A dbus message containing a UINT32 indicating success (1) or
1222 * Handler function for "setSmartcardModules" method call.
1224 DBusMessage * wpas_dbus_iface_set_smartcard_modules(
1225 DBusMessage *message, struct wpa_supplicant *wpa_s)
1227 DBusMessageIter iter, iter_dict;
1228 char *opensc_engine_path = NULL;
1229 char *pkcs11_engine_path = NULL;
1230 char *pkcs11_module_path = NULL;
1231 struct wpa_dbus_dict_entry entry;
1233 if (!dbus_message_iter_init(message, &iter))
1236 if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
1239 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
1240 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
1242 if (!strcmp(entry.key, "opensc_engine_path") &&
1243 (entry.type == DBUS_TYPE_STRING)) {
1244 opensc_engine_path = os_strdup(entry.str_value);
1245 if (opensc_engine_path == NULL)
1247 } else if (!strcmp(entry.key, "pkcs11_engine_path") &&
1248 (entry.type == DBUS_TYPE_STRING)) {
1249 pkcs11_engine_path = os_strdup(entry.str_value);
1250 if (pkcs11_engine_path == NULL)
1252 } else if (!strcmp(entry.key, "pkcs11_module_path") &&
1253 (entry.type == DBUS_TYPE_STRING)) {
1254 pkcs11_module_path = os_strdup(entry.str_value);
1255 if (pkcs11_module_path == NULL)
1258 wpa_dbus_dict_entry_clear(&entry);
1261 wpa_dbus_dict_entry_clear(&entry);
1264 #ifdef EAP_TLS_OPENSSL
1265 os_free(wpa_s->conf->opensc_engine_path);
1266 wpa_s->conf->opensc_engine_path = opensc_engine_path;
1267 os_free(wpa_s->conf->pkcs11_engine_path);
1268 wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path;
1269 os_free(wpa_s->conf->pkcs11_module_path);
1270 wpa_s->conf->pkcs11_module_path = pkcs11_module_path;
1271 #endif /* EAP_TLS_OPENSSL */
1273 eapol_sm_deinit(wpa_s->eapol);
1274 wpa_supplicant_init_eapol(wpa_s);
1276 return wpas_dbus_new_success_reply(message);
1279 os_free(opensc_engine_path);
1280 os_free(pkcs11_engine_path);
1281 os_free(pkcs11_module_path);
1282 return wpas_dbus_new_invalid_opts_error(message, NULL);
1287 * wpas_dbus_iface_get_state - Get interface state
1288 * @message: Pointer to incoming dbus message
1289 * @wpa_s: wpa_supplicant structure for a network interface
1290 * Returns: A dbus message containing a STRING representing the current
1293 * Handler function for "state" method call.
1295 DBusMessage * wpas_dbus_iface_get_state(DBusMessage *message,
1296 struct wpa_supplicant *wpa_s)
1298 DBusMessage *reply = NULL;
1299 const char *str_state;
1301 reply = dbus_message_new_method_return(message);
1302 if (reply != NULL) {
1303 str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
1304 dbus_message_append_args(reply, DBUS_TYPE_STRING, &str_state,
1313 * wpas_dbus_iface_get_scanning - Get interface scanning state
1314 * @message: Pointer to incoming dbus message
1315 * @wpa_s: wpa_supplicant structure for a network interface
1316 * Returns: A dbus message containing whether the interface is scanning
1318 * Handler function for "scanning" method call.
1320 DBusMessage * wpas_dbus_iface_get_scanning(DBusMessage *message,
1321 struct wpa_supplicant *wpa_s)
1323 DBusMessage *reply = NULL;
1324 dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
1326 reply = dbus_message_new_method_return(message);
1327 if (reply != NULL) {
1328 dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &scanning,
1331 perror("wpas_dbus_iface_get_scanning[dbus]: out of "
1333 wpa_printf(MSG_ERROR, "dbus control interface: not enough "
1334 "memory to return scanning state.");
1342 * wpas_dbus_iface_set_blobs - Store named binary blobs (ie, for certificates)
1343 * @message: Pointer to incoming dbus message
1344 * @wpa_s: %wpa_supplicant data structure
1345 * Returns: A dbus message containing a UINT32 indicating success (1) or
1348 * Asks wpa_supplicant to internally store a one or more binary blobs.
1350 DBusMessage * wpas_dbus_iface_set_blobs(DBusMessage *message,
1351 struct wpa_supplicant *wpa_s)
1353 DBusMessage *reply = NULL;
1354 struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
1355 DBusMessageIter iter, iter_dict;
1357 dbus_message_iter_init(message, &iter);
1359 if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
1360 return wpas_dbus_new_invalid_opts_error(message, NULL);
1362 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
1363 struct wpa_config_blob *blob;
1365 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
1366 reply = wpas_dbus_new_invalid_opts_error(message,
1371 if (entry.type != DBUS_TYPE_ARRAY ||
1372 entry.array_type != DBUS_TYPE_BYTE) {
1373 reply = wpas_dbus_new_invalid_opts_error(
1374 message, "Byte array expected.");
1378 if ((entry.array_len <= 0) || (entry.array_len > 65536) ||
1379 !strlen(entry.key)) {
1380 reply = wpas_dbus_new_invalid_opts_error(
1381 message, "Invalid array size.");
1385 blob = os_zalloc(sizeof(*blob));
1387 reply = dbus_message_new_error(
1388 message, WPAS_ERROR_ADD_ERROR,
1389 "Not enough memory to add blob.");
1392 blob->data = os_zalloc(entry.array_len);
1393 if (blob->data == NULL) {
1394 reply = dbus_message_new_error(
1395 message, WPAS_ERROR_ADD_ERROR,
1396 "Not enough memory to add blob data.");
1401 blob->name = os_strdup(entry.key);
1402 blob->len = entry.array_len;
1403 os_memcpy(blob->data, (u8 *) entry.bytearray_value,
1405 if (blob->name == NULL || blob->data == NULL) {
1406 wpa_config_free_blob(blob);
1407 reply = dbus_message_new_error(
1408 message, WPAS_ERROR_ADD_ERROR,
1409 "Error adding blob.");
1414 if (!wpa_config_remove_blob(wpa_s->conf, blob->name))
1415 wpas_notify_blob_removed(wpa_s, blob->name);
1416 wpa_config_set_blob(wpa_s->conf, blob);
1417 wpas_notify_blob_added(wpa_s, blob->name);
1419 wpa_dbus_dict_entry_clear(&entry);
1421 wpa_dbus_dict_entry_clear(&entry);
1423 return reply ? reply : wpas_dbus_new_success_reply(message);
1428 * wpas_dbus_iface_remove_blob - Remove named binary blobs
1429 * @message: Pointer to incoming dbus message
1430 * @wpa_s: %wpa_supplicant data structure
1431 * Returns: A dbus message containing a UINT32 indicating success (1) or
1434 * Asks wpa_supplicant to remove one or more previously stored binary blobs.
1436 DBusMessage * wpas_dbus_iface_remove_blobs(DBusMessage *message,
1437 struct wpa_supplicant *wpa_s)
1439 DBusMessageIter iter, array;
1440 char *err_msg = NULL;
1442 dbus_message_iter_init(message, &iter);
1444 if ((dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY) ||
1445 (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_STRING))
1446 return wpas_dbus_new_invalid_opts_error(message, NULL);
1448 dbus_message_iter_recurse(&iter, &array);
1449 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
1452 dbus_message_iter_get_basic(&array, &name);
1453 if (!os_strlen(name))
1454 err_msg = "Invalid blob name.";
1456 if (wpa_config_remove_blob(wpa_s->conf, name) != 0)
1457 err_msg = "Error removing blob.";
1459 wpas_notify_blob_removed(wpa_s, name);
1460 dbus_message_iter_next(&array);
1464 return dbus_message_new_error(message, WPAS_ERROR_REMOVE_ERROR,
1467 return wpas_dbus_new_success_reply(message);
1474 * wpas_dbus_iface_wps_pbc - Request credentials using WPS PBC method
1475 * @message: Pointer to incoming dbus message
1476 * @wpa_s: %wpa_supplicant data structure
1477 * Returns: A dbus message containing a UINT32 indicating success (1) or
1480 * Handler function for "wpsPbc" method call
1482 DBusMessage * wpas_dbus_iface_wps_pbc(DBusMessage *message,
1483 struct wpa_supplicant *wpa_s)
1485 char *arg_bssid = NULL;
1489 if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg_bssid,
1491 return wpas_dbus_new_invalid_opts_error(message, NULL);
1493 if (!os_strcmp(arg_bssid, "any"))
1494 ret = wpas_wps_start_pbc(wpa_s, NULL);
1495 else if (!hwaddr_aton(arg_bssid, bssid))
1496 ret = wpas_wps_start_pbc(wpa_s, bssid);
1498 return wpas_dbus_new_invalid_opts_error(message,
1503 return dbus_message_new_error(message,
1504 WPAS_ERROR_WPS_PBC_ERROR,
1505 "Could not start PBC "
1509 return wpas_dbus_new_success_reply(message);
1514 * wpas_dbus_iface_wps_pin - Establish the PIN number of the enrollee
1515 * @message: Pointer to incoming dbus message
1516 * @wpa_s: %wpa_supplicant data structure
1517 * Returns: A dbus message containing a UINT32 indicating success (1) or
1520 * Handler function for "wpsPin" method call
1522 DBusMessage * wpas_dbus_iface_wps_pin(DBusMessage *message,
1523 struct wpa_supplicant *wpa_s)
1525 DBusMessage *reply = NULL;
1528 u8 bssid[ETH_ALEN], *_bssid = NULL;
1531 if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg_bssid,
1532 DBUS_TYPE_STRING, &pin, DBUS_TYPE_INVALID))
1533 return wpas_dbus_new_invalid_opts_error(message, NULL);
1535 if (!os_strcmp(arg_bssid, "any"))
1537 else if (!hwaddr_aton(arg_bssid, bssid))
1540 return wpas_dbus_new_invalid_opts_error(message,
1544 if (os_strlen(pin) > 0)
1545 ret = wpas_wps_start_pin(wpa_s, _bssid, pin);
1547 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL);
1550 return dbus_message_new_error(message,
1551 WPAS_ERROR_WPS_PIN_ERROR,
1552 "Could not init PIN");
1555 reply = dbus_message_new_method_return(message);
1560 dbus_message_append_args(reply, DBUS_TYPE_STRING, &pin,
1564 os_snprintf(npin, sizeof(npin), "%08d", ret);
1565 dbus_message_append_args(reply, DBUS_TYPE_STRING, &npin,
1573 * wpas_dbus_iface_wps_reg - Request credentials using the PIN of the AP
1574 * @message: Pointer to incoming dbus message
1575 * @wpa_s: %wpa_supplicant data structure
1576 * Returns: A dbus message containing a UINT32 indicating success (1) or
1579 * Handler function for "wpsReg" method call
1581 DBusMessage * wpas_dbus_iface_wps_reg(DBusMessage *message,
1582 struct wpa_supplicant *wpa_s)
1589 if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg_bssid,
1590 DBUS_TYPE_STRING, &pin, DBUS_TYPE_INVALID))
1591 return wpas_dbus_new_invalid_opts_error(message, NULL);
1593 if (!os_strcmp(arg_bssid, "any"))
1594 ret = wpas_wps_start_reg(wpa_s, NULL, pin, NULL);
1595 else if (!hwaddr_aton(arg_bssid, bssid))
1596 ret = wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1598 return wpas_dbus_new_invalid_opts_error(message,
1603 return dbus_message_new_error(message,
1604 WPAS_ERROR_WPS_PBC_ERROR,
1605 "Could not request credentials");
1608 return wpas_dbus_new_success_reply(message);
1611 #endif /* CONFIG_WPS */