LCOV - code coverage report
Current view: top level - wpa_supplicant/dbus - dbus_old_handlers.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1443382998 Lines: 550 602 91.4 %
Date: 2015-09-27 Functions: 25 25 100.0 %

          Line data    Source code
       1             : /*
       2             :  * WPA Supplicant / dbus-based control interface
       3             :  * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
       4             :  *
       5             :  * This software may be distributed under the terms of the BSD license.
       6             :  * See README for more details.
       7             :  */
       8             : 
       9             : #include "includes.h"
      10             : #include <dbus/dbus.h>
      11             : 
      12             : #include "common.h"
      13             : #include "eap_peer/eap_methods.h"
      14             : #include "common/ieee802_11_defs.h"
      15             : #include "eapol_supp/eapol_supp_sm.h"
      16             : #include "rsn_supp/wpa.h"
      17             : #include "../config.h"
      18             : #include "../wpa_supplicant_i.h"
      19             : #include "../driver_i.h"
      20             : #include "../notify.h"
      21             : #include "../wpas_glue.h"
      22             : #include "../bss.h"
      23             : #include "../scan.h"
      24             : #include "dbus_old.h"
      25             : #include "dbus_old_handlers.h"
      26             : #include "dbus_dict_helpers.h"
      27             : 
      28             : /**
      29             :  * wpas_dbus_new_invalid_opts_error - Return a new invalid options error message
      30             :  * @message: Pointer to incoming dbus message this error refers to
      31             :  * Returns: a dbus error message
      32             :  *
      33             :  * Convenience function to create and return an invalid options error
      34             :  */
      35          48 : DBusMessage * wpas_dbus_new_invalid_opts_error(DBusMessage *message,
      36             :                                                const char *arg)
      37             : {
      38             :         DBusMessage *reply;
      39             : 
      40          48 :         reply = dbus_message_new_error(
      41             :                 message, WPAS_ERROR_INVALID_OPTS,
      42             :                 "Did not receive correct message arguments.");
      43          48 :         if (arg != NULL)
      44          12 :                 dbus_message_append_args(reply, DBUS_TYPE_STRING, &arg,
      45             :                                          DBUS_TYPE_INVALID);
      46             : 
      47          48 :         return reply;
      48             : }
      49             : 
      50             : 
      51             : /**
      52             :  * wpas_dbus_new_success_reply - Return a new success reply message
      53             :  * @message: Pointer to incoming dbus message this reply refers to
      54             :  * Returns: a dbus message containing a single UINT32 that indicates
      55             :  *          success (ie, a value of 1)
      56             :  *
      57             :  * Convenience function to create and return a success reply message
      58             :  */
      59          29 : DBusMessage * wpas_dbus_new_success_reply(DBusMessage *message)
      60             : {
      61             :         DBusMessage *reply;
      62          29 :         unsigned int success = 1;
      63             : 
      64          29 :         reply = dbus_message_new_method_return(message);
      65          29 :         dbus_message_append_args(reply, DBUS_TYPE_UINT32, &success,
      66             :                                  DBUS_TYPE_INVALID);
      67          29 :         return reply;
      68             : }
      69             : 
      70             : 
      71             : /**
      72             :  * wpas_dbus_global_add_interface - Request registration of a network interface
      73             :  * @message: Pointer to incoming dbus message
      74             :  * @global: %wpa_supplicant global data structure
      75             :  * Returns: The object path of the new interface object,
      76             :  *          or a dbus error message with more information
      77             :  *
      78             :  * Handler function for "addInterface" method call. Handles requests
      79             :  * by dbus clients to register a network interface that wpa_supplicant
      80             :  * will manage.
      81             :  */
      82          13 : DBusMessage * wpas_dbus_global_add_interface(DBusMessage *message,
      83             :                                              struct wpa_global *global)
      84             : {
      85          13 :         char *ifname = NULL;
      86          13 :         char *driver = NULL;
      87          13 :         char *driver_param = NULL;
      88          13 :         char *confname = NULL;
      89          13 :         char *bridge_ifname = NULL;
      90          13 :         DBusMessage *reply = NULL;
      91             :         DBusMessageIter iter;
      92             : 
      93          13 :         dbus_message_iter_init(message, &iter);
      94             : 
      95             :         /* First argument: interface name (DBUS_TYPE_STRING)
      96             :          *    Required; must be non-zero length
      97             :          */
      98          13 :         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
      99           1 :                 goto error;
     100          12 :         dbus_message_iter_get_basic(&iter, &ifname);
     101          12 :         if (!os_strlen(ifname))
     102           1 :                 goto error;
     103             : 
     104             :         /* Second argument: dict of options */
     105          11 :         if (dbus_message_iter_next(&iter)) {
     106             :                 DBusMessageIter iter_dict;
     107             :                 struct wpa_dbus_dict_entry entry;
     108             : 
     109           9 :                 if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
     110           8 :                         goto error;
     111          21 :                 while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
     112          11 :                         if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
     113           1 :                                 goto error;
     114          13 :                         if (!strcmp(entry.key, "driver") &&
     115           3 :                             entry.type == DBUS_TYPE_STRING) {
     116           3 :                                 os_free(driver);
     117           3 :                                 driver = os_strdup(entry.str_value);
     118           3 :                                 wpa_dbus_dict_entry_clear(&entry);
     119           5 :                                 if (driver == NULL)
     120           1 :                                         goto error;
     121           9 :                         } else if (!strcmp(entry.key, "driver-params") &&
     122           2 :                                    entry.type == DBUS_TYPE_STRING) {
     123           2 :                                 os_free(driver_param);
     124           2 :                                 driver_param = os_strdup(entry.str_value);
     125           2 :                                 wpa_dbus_dict_entry_clear(&entry);
     126           3 :                                 if (driver_param == NULL)
     127           1 :                                         goto error;
     128           7 :                         } else if (!strcmp(entry.key, "config-file") &&
     129           2 :                                    entry.type == DBUS_TYPE_STRING) {
     130           2 :                                 os_free(confname);
     131           2 :                                 confname = os_strdup(entry.str_value);
     132           2 :                                 wpa_dbus_dict_entry_clear(&entry);
     133           3 :                                 if (confname == NULL)
     134           1 :                                         goto error;
     135           5 :                         } else if (!strcmp(entry.key, "bridge-ifname") &&
     136           2 :                                    entry.type == DBUS_TYPE_STRING) {
     137           2 :                                 os_free(bridge_ifname);
     138           2 :                                 bridge_ifname = os_strdup(entry.str_value);
     139           2 :                                 wpa_dbus_dict_entry_clear(&entry);
     140           3 :                                 if (bridge_ifname == NULL)
     141           1 :                                         goto error;
     142             :                         } else {
     143           1 :                                 wpa_dbus_dict_entry_clear(&entry);
     144           1 :                                 goto error;
     145             :                         }
     146             :                 }
     147             :         }
     148             : 
     149             :         /*
     150             :          * Try to get the wpa_supplicant record for this iface, return
     151             :          * an error if we already control it.
     152             :          */
     153           4 :         if (wpa_supplicant_get_iface(global, ifname) != NULL) {
     154           1 :                 reply = dbus_message_new_error(
     155             :                         message, WPAS_ERROR_EXISTS_ERROR,
     156             :                         "wpa_supplicant already controls this interface.");
     157             :         } else {
     158             :                 struct wpa_supplicant *wpa_s;
     159             :                 struct wpa_interface iface;
     160             : 
     161           3 :                 os_memset(&iface, 0, sizeof(iface));
     162           3 :                 iface.ifname = ifname;
     163           3 :                 iface.driver = driver;
     164           3 :                 iface.driver_param = driver_param;
     165           3 :                 iface.confname = confname;
     166           3 :                 iface.bridge_ifname = bridge_ifname;
     167             :                 /* Otherwise, have wpa_supplicant attach to it. */
     168           3 :                 wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
     169           4 :                 if (wpa_s && wpa_s->dbus_path) {
     170           1 :                         const char *path = wpa_s->dbus_path;
     171             : 
     172           1 :                         reply = dbus_message_new_method_return(message);
     173           1 :                         dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
     174             :                                                  &path, DBUS_TYPE_INVALID);
     175             :                 } else {
     176           2 :                         reply = dbus_message_new_error(
     177             :                                 message, WPAS_ERROR_ADD_ERROR,
     178             :                                 "wpa_supplicant couldn't grab this interface.");
     179             :                 }
     180             :         }
     181             : 
     182             : out:
     183          13 :         os_free(driver);
     184          13 :         os_free(driver_param);
     185          13 :         os_free(confname);
     186          13 :         os_free(bridge_ifname);
     187          26 :         return reply;
     188             : 
     189             : error:
     190           9 :         reply = wpas_dbus_new_invalid_opts_error(message, NULL);
     191           9 :         goto out;
     192             : }
     193             : 
     194             : 
     195             : /**
     196             :  * wpas_dbus_global_remove_interface - Request deregistration of an interface
     197             :  * @message: Pointer to incoming dbus message
     198             :  * @global: wpa_supplicant global data structure
     199             :  * Returns: a dbus message containing a UINT32 indicating success (1) or
     200             :  *          failure (0), or returns a dbus error message with more information
     201             :  *
     202             :  * Handler function for "removeInterface" method call.  Handles requests
     203             :  * by dbus clients to deregister a network interface that wpa_supplicant
     204             :  * currently manages.
     205             :  */
     206           3 : DBusMessage * wpas_dbus_global_remove_interface(DBusMessage *message,
     207             :                                                 struct wpa_global *global)
     208             : {
     209             :         struct wpa_supplicant *wpa_s;
     210             :         char *path;
     211           3 :         DBusMessage *reply = NULL;
     212             : 
     213           3 :         if (!dbus_message_get_args(message, NULL,
     214             :                                    DBUS_TYPE_OBJECT_PATH, &path,
     215             :                                    DBUS_TYPE_INVALID)) {
     216           1 :                 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
     217           1 :                 goto out;
     218             :         }
     219             : 
     220           2 :         wpa_s = wpa_supplicant_get_iface_by_dbus_path(global, path);
     221           2 :         if (wpa_s == NULL) {
     222           1 :                 reply = wpas_dbus_new_invalid_iface_error(message);
     223           1 :                 goto out;
     224             :         }
     225             : 
     226           1 :         if (!wpa_supplicant_remove_iface(global, wpa_s, 0)) {
     227           1 :                 reply = wpas_dbus_new_success_reply(message);
     228             :         } else {
     229           0 :                 reply = dbus_message_new_error(
     230             :                         message, WPAS_ERROR_REMOVE_ERROR,
     231             :                         "wpa_supplicant couldn't remove this interface.");
     232             :         }
     233             : 
     234             : out:
     235           3 :         return reply;
     236             : }
     237             : 
     238             : 
     239             : /**
     240             :  * wpas_dbus_global_get_interface - Get the object path for an interface name
     241             :  * @message: Pointer to incoming dbus message
     242             :  * @global: %wpa_supplicant global data structure
     243             :  * Returns: The object path of the interface object,
     244             :  *          or a dbus error message with more information
     245             :  *
     246             :  * Handler function for "getInterface" method call. Handles requests
     247             :  * by dbus clients for the object path of an specific network interface.
     248             :  */
     249          20 : DBusMessage * wpas_dbus_global_get_interface(DBusMessage *message,
     250             :                                              struct wpa_global *global)
     251             : {
     252          20 :         DBusMessage *reply = NULL;
     253             :         const char *ifname;
     254             :         const char *path;
     255             :         struct wpa_supplicant *wpa_s;
     256             : 
     257          20 :         if (!dbus_message_get_args(message, NULL,
     258             :                                    DBUS_TYPE_STRING, &ifname,
     259             :                                    DBUS_TYPE_INVALID)) {
     260           1 :                 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
     261           1 :                 goto out;
     262             :         }
     263             : 
     264          19 :         wpa_s = wpa_supplicant_get_iface(global, ifname);
     265          19 :         if (wpa_s == NULL || !wpa_s->dbus_path) {
     266           1 :                 reply = wpas_dbus_new_invalid_iface_error(message);
     267           1 :                 goto out;
     268             :         }
     269             : 
     270          18 :         path = wpa_s->dbus_path;
     271          18 :         reply = dbus_message_new_method_return(message);
     272          18 :         dbus_message_append_args(reply,
     273             :                                  DBUS_TYPE_OBJECT_PATH, &path,
     274             :                                  DBUS_TYPE_INVALID);
     275             : 
     276             : out:
     277          20 :         return reply;
     278             : }
     279             : 
     280             : 
     281             : /**
     282             :  * wpas_dbus_global_set_debugparams- Set the debug params
     283             :  * @message: Pointer to incoming dbus message
     284             :  * @global: %wpa_supplicant global data structure
     285             :  * Returns: a dbus message containing a UINT32 indicating success (1) or
     286             :  *          failure (0), or returns a dbus error message with more information
     287             :  *
     288             :  * Handler function for "setDebugParams" method call. Handles requests
     289             :  * by dbus clients for the object path of an specific network interface.
     290             :  */
     291           3 : DBusMessage * wpas_dbus_global_set_debugparams(DBusMessage *message,
     292             :                                                struct wpa_global *global)
     293             : {
     294           3 :         DBusMessage *reply = NULL;
     295             :         int debug_level;
     296             :         dbus_bool_t debug_timestamp;
     297             :         dbus_bool_t debug_show_keys;
     298             : 
     299           3 :         if (!dbus_message_get_args(message, NULL,
     300             :                                    DBUS_TYPE_INT32, &debug_level,
     301             :                                    DBUS_TYPE_BOOLEAN, &debug_timestamp,
     302             :                                    DBUS_TYPE_BOOLEAN, &debug_show_keys,
     303             :                                    DBUS_TYPE_INVALID)) {
     304           1 :                 return wpas_dbus_new_invalid_opts_error(message, NULL);
     305             :         }
     306             : 
     307           2 :         if (wpa_supplicant_set_debug_params(global, debug_level,
     308             :                                             debug_timestamp ? 1 : 0,
     309             :                                             debug_show_keys ? 1 : 0)) {
     310           1 :                 return wpas_dbus_new_invalid_opts_error(message, NULL);
     311             :         }
     312             : 
     313           1 :         reply = wpas_dbus_new_success_reply(message);
     314             : 
     315           1 :         return reply;
     316             : }
     317             : 
     318             : 
     319             : /**
     320             :  * wpas_dbus_iface_scan - Request a wireless scan on an interface
     321             :  * @message: Pointer to incoming dbus message
     322             :  * @wpa_s: wpa_supplicant structure for a network interface
     323             :  * Returns: a dbus message containing a UINT32 indicating success (1) or
     324             :  *          failure (0)
     325             :  *
     326             :  * Handler function for "scan" method call of a network device. Requests
     327             :  * that wpa_supplicant perform a wireless scan as soon as possible
     328             :  * on a particular wireless interface.
     329             :  */
     330           1 : DBusMessage * wpas_dbus_iface_scan(DBusMessage *message,
     331             :                                    struct wpa_supplicant *wpa_s)
     332             : {
     333           1 :         wpa_s->scan_req = MANUAL_SCAN_REQ;
     334           1 :         wpa_supplicant_req_scan(wpa_s, 0, 0);
     335           1 :         return wpas_dbus_new_success_reply(message);
     336             : }
     337             : 
     338             : 
     339             : /**
     340             :  * wpas_dbus_iface_scan_results - Get the results of a recent scan request
     341             :  * @message: Pointer to incoming dbus message
     342             :  * @wpa_s: wpa_supplicant structure for a network interface
     343             :  * Returns: a dbus message containing a dbus array of objects paths, or returns
     344             :  *          a dbus error message if not scan results could be found
     345             :  *
     346             :  * Handler function for "scanResults" method call of a network device. Returns
     347             :  * a dbus message containing the object paths of wireless networks found.
     348             :  */
     349           3 : DBusMessage * wpas_dbus_iface_scan_results(DBusMessage *message,
     350             :                                            struct wpa_supplicant *wpa_s)
     351             : {
     352             :         DBusMessage *reply;
     353             :         DBusMessageIter iter;
     354             :         DBusMessageIter sub_iter;
     355             :         struct wpa_bss *bss;
     356             : 
     357           3 :         if (!wpa_s->dbus_path)
     358           0 :                 return dbus_message_new_error(message,
     359             :                                               WPAS_ERROR_INTERNAL_ERROR,
     360             :                                               "no D-Bus interface available");
     361             : 
     362             :         /* Create and initialize the return message */
     363           3 :         reply = dbus_message_new_method_return(message);
     364           3 :         dbus_message_iter_init_append(reply, &iter);
     365           3 :         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
     366             :                                               DBUS_TYPE_OBJECT_PATH_AS_STRING,
     367             :                                               &sub_iter))
     368           0 :                 goto error;
     369             : 
     370             :         /* Loop through scan results and append each result's object path */
     371           6 :         dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
     372             :                 char path_buf[WPAS_DBUS_OBJECT_PATH_MAX];
     373           3 :                 char *path = path_buf;
     374             : 
     375             :                 /* Construct the object path for this network.  Note that ':'
     376             :                  * is not a valid character in dbus object paths.
     377             :                  */
     378          18 :                 os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
     379             :                             "%s/" WPAS_DBUS_BSSIDS_PART "/"
     380             :                             WPAS_DBUS_BSSID_FORMAT,
     381          18 :                             wpa_s->dbus_path, MAC2STR(bss->bssid));
     382           3 :                 if (!dbus_message_iter_append_basic(&sub_iter,
     383             :                                                     DBUS_TYPE_OBJECT_PATH,
     384             :                                                     &path))
     385           0 :                         goto error;
     386             :         }
     387             : 
     388           3 :         if (!dbus_message_iter_close_container(&iter, &sub_iter))
     389           0 :                 goto error;
     390             : 
     391           3 :         return reply;
     392             : 
     393             : error:
     394           0 :         dbus_message_unref(reply);
     395           0 :         return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
     396             :                                       "an internal error occurred returning scan results");
     397             : }
     398             : 
     399             : 
     400             : /**
     401             :  * wpas_dbus_bssid_properties - Return the properties of a scanned network
     402             :  * @message: Pointer to incoming dbus message
     403             :  * @wpa_s: wpa_supplicant structure for a network interface
     404             :  * @res: wpa_supplicant scan result for which to get properties
     405             :  * Returns: a dbus message containing the properties for the requested network
     406             :  *
     407             :  * Handler function for "properties" method call of a scanned network.
     408             :  * Returns a dbus message containing the the properties.
     409             :  */
     410           3 : DBusMessage * wpas_dbus_bssid_properties(DBusMessage *message,
     411             :                                          struct wpa_supplicant *wpa_s,
     412             :                                          struct wpa_bss *bss)
     413             : {
     414             :         DBusMessage *reply;
     415             :         DBusMessageIter iter, iter_dict;
     416             :         const u8 *wpa_ie, *rsn_ie, *wps_ie;
     417             : 
     418             :         /* Dump the properties into a dbus message */
     419           3 :         reply = dbus_message_new_method_return(message);
     420             : 
     421           3 :         wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
     422           3 :         rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
     423           3 :         wps_ie = wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE);
     424             : 
     425           3 :         dbus_message_iter_init_append(reply, &iter);
     426           6 :         if (!wpa_dbus_dict_open_write(&iter, &iter_dict) ||
     427           3 :             !wpa_dbus_dict_append_byte_array(&iter_dict, "bssid",
     428           3 :                                              (const char *) bss->bssid,
     429           3 :                                              ETH_ALEN) ||
     430           6 :             !wpa_dbus_dict_append_byte_array(&iter_dict, "ssid",
     431           3 :                                              (const char *) bss->ssid,
     432           6 :                                              bss->ssid_len) ||
     433           1 :             (wpa_ie &&
     434           1 :              !wpa_dbus_dict_append_byte_array(&iter_dict, "wpaie",
     435             :                                               (const char *) wpa_ie,
     436           4 :                                               wpa_ie[1] + 2)) ||
     437           2 :             (rsn_ie &&
     438           2 :              !wpa_dbus_dict_append_byte_array(&iter_dict, "rsnie",
     439             :                                               (const char *) rsn_ie,
     440           5 :                                               rsn_ie[1] + 2)) ||
     441           1 :             (wps_ie &&
     442           1 :              !wpa_dbus_dict_append_byte_array(&iter_dict, "wpsie",
     443             :                                              (const char *) wps_ie,
     444           4 :                                               wps_ie[1] + 2)) ||
     445           6 :             (bss->freq &&
     446           6 :              !wpa_dbus_dict_append_int32(&iter_dict, "frequency", bss->freq)) ||
     447           3 :             !wpa_dbus_dict_append_uint16(&iter_dict, "capabilities",
     448           6 :                                          bss->caps) ||
     449           3 :             (!(bss->flags & WPA_BSS_QUAL_INVALID) &&
     450           3 :              !wpa_dbus_dict_append_int32(&iter_dict, "quality", bss->qual)) ||
     451           6 :             (!(bss->flags & WPA_BSS_NOISE_INVALID) &&
     452           6 :              !wpa_dbus_dict_append_int32(&iter_dict, "noise", bss->noise)) ||
     453           6 :             (!(bss->flags & WPA_BSS_LEVEL_INVALID) &&
     454           6 :              !wpa_dbus_dict_append_int32(&iter_dict, "level", bss->level)) ||
     455           3 :             !wpa_dbus_dict_append_int32(&iter_dict, "maxrate",
     456           6 :                                         wpa_bss_get_max_rate(bss) * 500000) ||
     457           3 :             !wpa_dbus_dict_close_write(&iter, &iter_dict)) {
     458           0 :                 if (reply)
     459           0 :                         dbus_message_unref(reply);
     460           0 :                 reply = dbus_message_new_error(
     461             :                         message, WPAS_ERROR_INTERNAL_ERROR,
     462             :                         "an internal error occurred returning BSSID properties.");
     463             :         }
     464             : 
     465           3 :         return reply;
     466             : }
     467             : 
     468             : 
     469             : /**
     470             :  * wpas_dbus_iface_capabilities - Return interface capabilities
     471             :  * @message: Pointer to incoming dbus message
     472             :  * @wpa_s: wpa_supplicant structure for a network interface
     473             :  * Returns: A dbus message containing a dict of strings
     474             :  *
     475             :  * Handler function for "capabilities" method call of an interface.
     476             :  */
     477           2 : DBusMessage * wpas_dbus_iface_capabilities(DBusMessage *message,
     478             :                                            struct wpa_supplicant *wpa_s)
     479             : {
     480           2 :         DBusMessage *reply = NULL;
     481             :         struct wpa_driver_capa capa;
     482             :         int res;
     483             :         DBusMessageIter iter, iter_dict;
     484             :         char **eap_methods;
     485             :         size_t num_items;
     486           2 :         dbus_bool_t strict = FALSE;
     487             :         DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
     488             : 
     489           2 :         if (!dbus_message_get_args(message, NULL,
     490             :                                    DBUS_TYPE_BOOLEAN, &strict,
     491             :                                    DBUS_TYPE_INVALID))
     492           1 :                 strict = FALSE;
     493             : 
     494           2 :         reply = dbus_message_new_method_return(message);
     495             : 
     496           2 :         dbus_message_iter_init_append(reply, &iter);
     497           2 :         if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
     498           0 :                 goto error;
     499             : 
     500             :         /* EAP methods */
     501           2 :         eap_methods = eap_get_names_as_string_array(&num_items);
     502           2 :         if (eap_methods) {
     503             :                 dbus_bool_t success;
     504           2 :                 size_t i = 0;
     505             : 
     506           2 :                 success = wpa_dbus_dict_append_string_array(
     507             :                         &iter_dict, "eap", (const char **) eap_methods,
     508             :                         num_items);
     509             : 
     510             :                 /* free returned method array */
     511          52 :                 while (eap_methods[i])
     512          48 :                         os_free(eap_methods[i++]);
     513           2 :                 os_free(eap_methods);
     514             : 
     515           2 :                 if (!success)
     516           0 :                         goto error;
     517             :         }
     518             : 
     519           2 :         res = wpa_drv_get_capa(wpa_s, &capa);
     520             : 
     521             :         /***** pairwise cipher */
     522           2 :         if (res < 0) {
     523           0 :                 if (!strict) {
     524           0 :                         const char *args[] = {"CCMP", "TKIP", "NONE"};
     525             : 
     526           0 :                         if (!wpa_dbus_dict_append_string_array(
     527             :                                     &iter_dict, "pairwise", args,
     528             :                                     ARRAY_SIZE(args)))
     529           0 :                                 goto error;
     530             :                 }
     531             :         } else {
     532           2 :                 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "pairwise",
     533             :                                                       &iter_dict_entry,
     534             :                                                       &iter_dict_val,
     535           2 :                                                       &iter_array) ||
     536           4 :                     ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) &&
     537           2 :                      !wpa_dbus_dict_string_array_add_element(
     538           2 :                              &iter_array, "CCMP")) ||
     539           4 :                     ((capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) &&
     540           2 :                      !wpa_dbus_dict_string_array_add_element(
     541           2 :                              &iter_array, "TKIP")) ||
     542           2 :                     ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) &&
     543           0 :                      !wpa_dbus_dict_string_array_add_element(
     544           2 :                              &iter_array, "NONE")) ||
     545           2 :                     !wpa_dbus_dict_end_string_array(&iter_dict,
     546             :                                                     &iter_dict_entry,
     547             :                                                     &iter_dict_val,
     548             :                                                     &iter_array))
     549             :                         goto error;
     550             :         }
     551             : 
     552             :         /***** group cipher */
     553           2 :         if (res < 0) {
     554           0 :                 if (!strict) {
     555           0 :                         const char *args[] = {
     556             :                                 "CCMP", "TKIP", "WEP104", "WEP40"
     557             :                         };
     558             : 
     559           0 :                         if (!wpa_dbus_dict_append_string_array(
     560             :                                     &iter_dict, "group", args,
     561             :                                     ARRAY_SIZE(args)))
     562           0 :                                 goto error;
     563             :                 }
     564             :         } else {
     565           2 :                 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "group",
     566             :                                                       &iter_dict_entry,
     567             :                                                       &iter_dict_val,
     568             :                                                       &iter_array))
     569           0 :                         goto error;
     570             : 
     571           4 :                 if (((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) &&
     572           2 :                      !wpa_dbus_dict_string_array_add_element(
     573           2 :                              &iter_array, "CCMP")) ||
     574           4 :                     ((capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) &&
     575           2 :                      !wpa_dbus_dict_string_array_add_element(
     576           2 :                              &iter_array, "TKIP")) ||
     577           4 :                     ((capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) &&
     578           2 :                      !wpa_dbus_dict_string_array_add_element(
     579           2 :                              &iter_array, "WEP104")) ||
     580           4 :                     ((capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) &&
     581           2 :                      !wpa_dbus_dict_string_array_add_element(
     582           2 :                              &iter_array, "WEP40")) ||
     583           2 :                     !wpa_dbus_dict_end_string_array(&iter_dict,
     584             :                                                     &iter_dict_entry,
     585             :                                                     &iter_dict_val,
     586             :                                                     &iter_array))
     587             :                         goto error;
     588             :         }
     589             : 
     590             :         /***** key management */
     591           2 :         if (res < 0) {
     592           0 :                 if (!strict) {
     593           0 :                         const char *args[] = {
     594             :                                 "WPA-PSK", "WPA-EAP", "IEEE8021X", "WPA-NONE",
     595             :                                 "NONE"
     596             :                         };
     597           0 :                         if (!wpa_dbus_dict_append_string_array(
     598             :                                     &iter_dict, "key_mgmt", args,
     599             :                                     ARRAY_SIZE(args)))
     600           0 :                                 goto error;
     601             :                 }
     602             :         } else {
     603           2 :                 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "key_mgmt",
     604             :                                                       &iter_dict_entry,
     605             :                                                       &iter_dict_val,
     606           2 :                                                       &iter_array) ||
     607           2 :                     !wpa_dbus_dict_string_array_add_element(&iter_array,
     608           2 :                                                             "NONE") ||
     609           2 :                     !wpa_dbus_dict_string_array_add_element(&iter_array,
     610           2 :                                                             "IEEE8021X") ||
     611           2 :                     ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
     612           2 :                                        WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) &&
     613           2 :                      !wpa_dbus_dict_string_array_add_element(
     614           2 :                              &iter_array, "WPA-EAP")) ||
     615           2 :                     ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
     616           2 :                                        WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) &&
     617           2 :                      !wpa_dbus_dict_string_array_add_element(
     618           2 :                              &iter_array, "WPA-PSK")) ||
     619           2 :                     ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) &&
     620           0 :                      !wpa_dbus_dict_string_array_add_element(
     621           2 :                              &iter_array, "WPA-NONE")) ||
     622           2 :                     !wpa_dbus_dict_end_string_array(&iter_dict,
     623             :                                                     &iter_dict_entry,
     624             :                                                     &iter_dict_val,
     625             :                                                     &iter_array))
     626             :                         goto error;
     627             :         }
     628             : 
     629             :         /***** WPA protocol */
     630           2 :         if (res < 0) {
     631           0 :                 if (!strict) {
     632           0 :                         const char *args[] = { "RSN", "WPA" };
     633             : 
     634           0 :                         if (!wpa_dbus_dict_append_string_array(
     635             :                                     &iter_dict, "proto", args,
     636             :                                     ARRAY_SIZE(args)))
     637           0 :                                 goto error;
     638             :                 }
     639             :         } else {
     640           2 :                 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "proto",
     641             :                                                       &iter_dict_entry,
     642             :                                                       &iter_dict_val,
     643           2 :                                                       &iter_array) ||
     644           2 :                     ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
     645           2 :                                        WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) &&
     646           2 :                      !wpa_dbus_dict_string_array_add_element(
     647           2 :                              &iter_array, "RSN")) ||
     648           2 :                     ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
     649           2 :                                        WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) &&
     650           2 :                      !wpa_dbus_dict_string_array_add_element(
     651           2 :                              &iter_array, "WPA")) ||
     652           2 :                     !wpa_dbus_dict_end_string_array(&iter_dict,
     653             :                                                     &iter_dict_entry,
     654             :                                                     &iter_dict_val,
     655             :                                                     &iter_array))
     656             :                         goto error;
     657             :         }
     658             : 
     659             :         /***** auth alg */
     660           2 :         if (res < 0) {
     661           0 :                 if (!strict) {
     662           0 :                         const char *args[] = { "OPEN", "SHARED", "LEAP" };
     663             : 
     664           0 :                         if (!wpa_dbus_dict_append_string_array(
     665             :                                     &iter_dict, "auth_alg", args,
     666             :                                     ARRAY_SIZE(args)))
     667           0 :                                 goto error;
     668             :                 }
     669             :         } else {
     670           2 :                 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "auth_alg",
     671             :                                                       &iter_dict_entry,
     672             :                                                       &iter_dict_val,
     673           2 :                                                       &iter_array) ||
     674           4 :                     ((capa.auth & WPA_DRIVER_AUTH_OPEN) &&
     675           2 :                      !wpa_dbus_dict_string_array_add_element(
     676           2 :                              &iter_array, "OPEN")) ||
     677           4 :                     ((capa.auth & WPA_DRIVER_AUTH_SHARED) &&
     678           2 :                      !wpa_dbus_dict_string_array_add_element(
     679           2 :                              &iter_array, "SHARED")) ||
     680           4 :                     ((capa.auth & WPA_DRIVER_AUTH_LEAP) &&
     681           2 :                      !wpa_dbus_dict_string_array_add_element(
     682           2 :                              &iter_array, "LEAP")) ||
     683           2 :                     !wpa_dbus_dict_end_string_array(&iter_dict,
     684             :                                                     &iter_dict_entry,
     685             :                                                     &iter_dict_val,
     686             :                                                     &iter_array))
     687             :                         goto error;
     688             :         }
     689             : 
     690           2 :         if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
     691           0 :                 goto error;
     692             : 
     693           2 :         return reply;
     694             : 
     695             : error:
     696           0 :         if (reply)
     697           0 :                 dbus_message_unref(reply);
     698           0 :         return dbus_message_new_error(
     699             :                 message, WPAS_ERROR_INTERNAL_ERROR,
     700             :                 "an internal error occurred returning interface capabilities.");
     701             : }
     702             : 
     703             : 
     704             : /**
     705             :  * wpas_dbus_iface_add_network - Add a new configured network
     706             :  * @message: Pointer to incoming dbus message
     707             :  * @wpa_s: wpa_supplicant structure for a network interface
     708             :  * Returns: A dbus message containing the object path of the new network
     709             :  *
     710             :  * Handler function for "addNetwork" method call of a network interface.
     711             :  */
     712           6 : DBusMessage * wpas_dbus_iface_add_network(DBusMessage *message,
     713             :                                           struct wpa_supplicant *wpa_s)
     714             : {
     715           6 :         DBusMessage *reply = NULL;
     716           6 :         struct wpa_ssid *ssid = NULL;
     717           6 :         char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *path = path_buf;
     718             : 
     719           6 :         if (wpa_s->dbus_path)
     720           6 :                 ssid = wpa_config_add_network(wpa_s->conf);
     721           6 :         if (ssid == NULL) {
     722           1 :                 reply = dbus_message_new_error(
     723             :                         message, WPAS_ERROR_ADD_NETWORK_ERROR,
     724             :                         "wpa_supplicant could not add a network on this interface.");
     725           1 :                 goto out;
     726             :         }
     727           5 :         wpas_notify_network_added(wpa_s, ssid);
     728           5 :         ssid->disabled = 1;
     729           5 :         wpa_config_set_network_defaults(ssid);
     730             : 
     731             :         /* Construct the object path for this network. */
     732           5 :         os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
     733             :                     "%s/" WPAS_DBUS_NETWORKS_PART "/%d",
     734             :                     wpa_s->dbus_path, ssid->id);
     735             : 
     736           5 :         reply = dbus_message_new_method_return(message);
     737           5 :         dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
     738             :                                  &path, DBUS_TYPE_INVALID);
     739             : 
     740             : out:
     741           6 :         return reply;
     742             : }
     743             : 
     744             : 
     745             : /**
     746             :  * wpas_dbus_iface_remove_network - Remove a configured network
     747             :  * @message: Pointer to incoming dbus message
     748             :  * @wpa_s: wpa_supplicant structure for a network interface
     749             :  * Returns: A dbus message containing a UINT32 indicating success (1) or
     750             :  *          failure (0)
     751             :  *
     752             :  * Handler function for "removeNetwork" method call of a network interface.
     753             :  */
     754          10 : DBusMessage * wpas_dbus_iface_remove_network(DBusMessage *message,
     755             :                                              struct wpa_supplicant *wpa_s)
     756             : {
     757          10 :         DBusMessage *reply = NULL;
     758             :         const char *op;
     759          10 :         char *iface = NULL, *net_id = NULL;
     760             :         int id;
     761             :         struct wpa_ssid *ssid;
     762             : 
     763          10 :         if (!dbus_message_get_args(message, NULL,
     764             :                                    DBUS_TYPE_OBJECT_PATH, &op,
     765             :                                    DBUS_TYPE_INVALID)) {
     766           1 :                 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
     767           1 :                 goto out;
     768             :         }
     769             : 
     770             :         /* Extract the network ID */
     771           9 :         iface = wpas_dbus_decompose_object_path(op, &net_id, NULL);
     772           9 :         if (iface == NULL || net_id == NULL) {
     773           3 :                 reply = wpas_dbus_new_invalid_network_error(message);
     774           3 :                 goto out;
     775             :         }
     776             : 
     777             :         /* Ensure the network is actually a child of this interface */
     778           6 :         if (!wpa_s->dbus_path || os_strcmp(iface, wpa_s->dbus_path) != 0) {
     779           1 :                 reply = wpas_dbus_new_invalid_network_error(message);
     780           1 :                 goto out;
     781             :         }
     782             : 
     783           5 :         id = strtoul(net_id, NULL, 10);
     784           5 :         ssid = wpa_config_get_network(wpa_s->conf, id);
     785           5 :         if (ssid == NULL) {
     786           3 :                 reply = wpas_dbus_new_invalid_network_error(message);
     787           3 :                 goto out;
     788             :         }
     789             : 
     790           2 :         wpas_notify_network_removed(wpa_s, ssid);
     791             : 
     792           2 :         if (ssid == wpa_s->current_ssid)
     793           1 :                 wpa_supplicant_deauthenticate(wpa_s,
     794             :                                               WLAN_REASON_DEAUTH_LEAVING);
     795             : 
     796           2 :         if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
     797           0 :                 reply = dbus_message_new_error(
     798             :                         message, WPAS_ERROR_REMOVE_NETWORK_ERROR,
     799             :                         "error removing the specified on this interface.");
     800           0 :                 goto out;
     801             :         }
     802             : 
     803           2 :         reply = wpas_dbus_new_success_reply(message);
     804             : 
     805             : out:
     806          10 :         os_free(iface);
     807          10 :         os_free(net_id);
     808          10 :         return reply;
     809             : }
     810             : 
     811             : 
     812             : static const char * const dont_quote[] = {
     813             :         "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
     814             :         "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
     815             :         "bssid", "scan_freq", "freq_list", NULL
     816             : };
     817             : 
     818             : 
     819          19 : static dbus_bool_t should_quote_opt(const char *key)
     820             : {
     821          19 :         int i = 0;
     822             : 
     823         213 :         while (dont_quote[i] != NULL) {
     824         182 :                 if (os_strcmp(key, dont_quote[i]) == 0)
     825           7 :                         return FALSE;
     826         175 :                 i++;
     827             :         }
     828          12 :         return TRUE;
     829             : }
     830             : 
     831             : 
     832             : /**
     833             :  * wpas_dbus_iface_set_network - Set options for a configured network
     834             :  * @message: Pointer to incoming dbus message
     835             :  * @wpa_s: wpa_supplicant structure for a network interface
     836             :  * @ssid: wpa_ssid structure for a configured network
     837             :  * Returns: a dbus message containing a UINT32 indicating success (1) or
     838             :  *          failure (0)
     839             :  *
     840             :  * Handler function for "set" method call of a configured network.
     841             :  */
     842          13 : DBusMessage * wpas_dbus_iface_set_network(DBusMessage *message,
     843             :                                           struct wpa_supplicant *wpa_s,
     844             :                                           struct wpa_ssid *ssid)
     845             : {
     846          13 :         DBusMessage *reply = NULL;
     847          13 :         struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
     848             :         DBusMessageIter iter, iter_dict;
     849             : 
     850          13 :         dbus_message_iter_init(message, &iter);
     851             : 
     852          13 :         if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL)) {
     853           1 :                 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
     854           1 :                 goto out;
     855             :         }
     856             : 
     857          45 :         while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
     858          29 :                 char *value = NULL;
     859          29 :                 size_t size = 50;
     860             :                 int ret;
     861             : 
     862          29 :                 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
     863           1 :                         reply = wpas_dbus_new_invalid_opts_error(message,
     864             :                                                                  NULL);
     865           1 :                         goto out;
     866             :                 }
     867             : 
     868             :                 /* Type conversions, since wpa_supplicant wants strings */
     869          30 :                 if (entry.type == DBUS_TYPE_ARRAY &&
     870           2 :                     entry.array_type == DBUS_TYPE_BYTE) {
     871           2 :                         if (entry.array_len <= 0)
     872           0 :                                 goto error;
     873             : 
     874           2 :                         size = entry.array_len * 2 + 1;
     875           2 :                         value = os_zalloc(size);
     876           2 :                         if (value == NULL)
     877           1 :                                 goto error;
     878           2 :                         ret = wpa_snprintf_hex(value, size,
     879           1 :                                                (u8 *) entry.bytearray_value,
     880           1 :                                                entry.array_len);
     881           2 :                         if (ret <= 0)
     882           0 :                                 goto error;
     883          26 :                 } else if (entry.type == DBUS_TYPE_STRING) {
     884          19 :                         if (should_quote_opt(entry.key)) {
     885          12 :                                 size = os_strlen(entry.str_value);
     886             :                                 /* Zero-length option check */
     887          12 :                                 if (size == 0)
     888           0 :                                         goto error;
     889          12 :                                 size += 3;  /* For quotes and terminator */
     890          12 :                                 value = os_zalloc(size);
     891          12 :                                 if (value == NULL)
     892           1 :                                         goto error;
     893          11 :                                 ret = os_snprintf(value, size, "\"%s\"",
     894             :                                                   entry.str_value);
     895          11 :                                 if (os_snprintf_error(size, ret))
     896           0 :                                         goto error;
     897             :                         } else {
     898           7 :                                 value = os_strdup(entry.str_value);
     899           7 :                                 if (value == NULL)
     900           1 :                                         goto error;
     901             :                         }
     902           7 :                 } else if (entry.type == DBUS_TYPE_UINT32) {
     903           2 :                         value = os_zalloc(size);
     904           2 :                         if (value == NULL)
     905           1 :                                 goto error;
     906           1 :                         ret = os_snprintf(value, size, "%u",
     907             :                                           entry.uint32_value);
     908           1 :                         if (os_snprintf_error(size, ret))
     909           0 :                                 goto error;
     910           5 :                 } else if (entry.type == DBUS_TYPE_INT32) {
     911           4 :                         value = os_zalloc(size);
     912           4 :                         if (value == NULL)
     913           1 :                                 goto error;
     914           3 :                         ret = os_snprintf(value, size, "%d",
     915             :                                           entry.int32_value);
     916           3 :                         if (os_snprintf_error(size, ret))
     917           0 :                                 goto error;
     918             :                 } else
     919           1 :                         goto error;
     920             : 
     921          22 :                 if (wpa_config_set(ssid, entry.key, value, 0) < 0)
     922           1 :                         goto error;
     923             : 
     924          23 :                 if ((os_strcmp(entry.key, "psk") == 0 &&
     925          25 :                      value[0] == '"' && ssid->ssid_len) ||
     926          24 :                     (os_strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
     927           2 :                         wpa_config_update_psk(ssid);
     928          19 :                 else if (os_strcmp(entry.key, "priority") == 0)
     929           1 :                         wpa_config_update_prio_list(wpa_s->conf);
     930             : 
     931          21 :                 os_free(value);
     932          21 :                 wpa_dbus_dict_entry_clear(&entry);
     933          21 :                 continue;
     934             : 
     935             :         error:
     936           7 :                 os_free(value);
     937           7 :                 reply = wpas_dbus_new_invalid_opts_error(message, entry.key);
     938           7 :                 wpa_dbus_dict_entry_clear(&entry);
     939           7 :                 break;
     940             :         }
     941             : 
     942          11 :         if (!reply)
     943           4 :                 reply = wpas_dbus_new_success_reply(message);
     944             : 
     945             : out:
     946          13 :         return reply;
     947             : }
     948             : 
     949             : 
     950             : /**
     951             :  * wpas_dbus_iface_enable_network - Mark a configured network as enabled
     952             :  * @message: Pointer to incoming dbus message
     953             :  * @wpa_s: wpa_supplicant structure for a network interface
     954             :  * @ssid: wpa_ssid structure for a configured network
     955             :  * Returns: A dbus message containing a UINT32 indicating success (1) or
     956             :  *          failure (0)
     957             :  *
     958             :  * Handler function for "enable" method call of a configured network.
     959             :  */
     960           3 : DBusMessage * wpas_dbus_iface_enable_network(DBusMessage *message,
     961             :                                              struct wpa_supplicant *wpa_s,
     962             :                                              struct wpa_ssid *ssid)
     963             : {
     964           3 :         wpa_supplicant_enable_network(wpa_s, ssid);
     965           3 :         return wpas_dbus_new_success_reply(message);
     966             : }
     967             : 
     968             : 
     969             : /**
     970             :  * wpas_dbus_iface_disable_network - Mark a configured network as disabled
     971             :  * @message: Pointer to incoming dbus message
     972             :  * @wpa_s: wpa_supplicant structure for a network interface
     973             :  * @ssid: wpa_ssid structure for a configured network
     974             :  * Returns: A dbus message containing a UINT32 indicating success (1) or
     975             :  *          failure (0)
     976             :  *
     977             :  * Handler function for "disable" method call of a configured network.
     978             :  */
     979           4 : DBusMessage * wpas_dbus_iface_disable_network(DBusMessage *message,
     980             :                                               struct wpa_supplicant *wpa_s,
     981             :                                               struct wpa_ssid *ssid)
     982             : {
     983           4 :         wpa_supplicant_disable_network(wpa_s, ssid);
     984           4 :         return wpas_dbus_new_success_reply(message);
     985             : }
     986             : 
     987             : 
     988             : /**
     989             :  * wpas_dbus_iface_select_network - Attempt association with a configured network
     990             :  * @message: Pointer to incoming dbus message
     991             :  * @wpa_s: wpa_supplicant structure for a network interface
     992             :  * Returns: A dbus message containing a UINT32 indicating success (1) or
     993             :  *          failure (0)
     994             :  *
     995             :  * Handler function for "selectNetwork" method call of network interface.
     996             :  */
     997           7 : DBusMessage * wpas_dbus_iface_select_network(DBusMessage *message,
     998             :                                              struct wpa_supplicant *wpa_s)
     999             : {
    1000           7 :         DBusMessage *reply = NULL;
    1001             :         const char *op;
    1002             :         struct wpa_ssid *ssid;
    1003           7 :         char *iface_obj_path = NULL;
    1004           7 :         char *network = NULL;
    1005             : 
    1006           7 :         if (os_strlen(dbus_message_get_signature(message)) == 0) {
    1007             :                 /* Any network */
    1008           1 :                 ssid = NULL;
    1009             :         } else {
    1010             :                 int nid;
    1011             : 
    1012           6 :                 if (!dbus_message_get_args(message, NULL,
    1013             :                                            DBUS_TYPE_OBJECT_PATH, &op,
    1014             :                                            DBUS_TYPE_INVALID)) {
    1015           1 :                         reply = wpas_dbus_new_invalid_opts_error(message,
    1016             :                                                                  NULL);
    1017           1 :                         goto out;
    1018             :                 }
    1019             : 
    1020             :                 /* Extract the network number */
    1021           5 :                 iface_obj_path = wpas_dbus_decompose_object_path(op,
    1022             :                                                                  &network,
    1023             :                                                                  NULL);
    1024           5 :                 if (iface_obj_path == NULL) {
    1025           1 :                         reply = wpas_dbus_new_invalid_iface_error(message);
    1026           1 :                         goto out;
    1027             :                 }
    1028             :                 /* Ensure the object path really points to this interface */
    1029           7 :                 if (network == NULL || !wpa_s->dbus_path ||
    1030           3 :                     os_strcmp(iface_obj_path, wpa_s->dbus_path) != 0) {
    1031           1 :                         reply = wpas_dbus_new_invalid_network_error(message);
    1032           1 :                         goto out;
    1033             :                 }
    1034             : 
    1035           3 :                 nid = strtoul(network, NULL, 10);
    1036           3 :                 if (errno == EINVAL) {
    1037           0 :                         reply = wpas_dbus_new_invalid_network_error(message);
    1038           0 :                         goto out;
    1039             :                 }
    1040             : 
    1041           3 :                 ssid = wpa_config_get_network(wpa_s->conf, nid);
    1042           3 :                 if (ssid == NULL) {
    1043           2 :                         reply = wpas_dbus_new_invalid_network_error(message);
    1044           2 :                         goto out;
    1045             :                 }
    1046             :         }
    1047             : 
    1048             :         /* Finally, associate with the network */
    1049           2 :         wpa_supplicant_select_network(wpa_s, ssid);
    1050             : 
    1051           2 :         reply = wpas_dbus_new_success_reply(message);
    1052             : 
    1053             : out:
    1054           7 :         os_free(iface_obj_path);
    1055           7 :         os_free(network);
    1056           7 :         return reply;
    1057             : }
    1058             : 
    1059             : 
    1060             : /**
    1061             :  * wpas_dbus_iface_disconnect - Terminate the current connection
    1062             :  * @message: Pointer to incoming dbus message
    1063             :  * @wpa_s: wpa_supplicant structure for a network interface
    1064             :  * Returns: A dbus message containing a UINT32 indicating success (1) or
    1065             :  *          failure (0)
    1066             :  *
    1067             :  * Handler function for "disconnect" method call of network interface.
    1068             :  */
    1069           2 : DBusMessage * wpas_dbus_iface_disconnect(DBusMessage *message,
    1070             :                                          struct wpa_supplicant *wpa_s)
    1071             : {
    1072           2 :         wpa_s->disconnected = 1;
    1073           2 :         wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
    1074             : 
    1075           2 :         return wpas_dbus_new_success_reply(message);
    1076             : }
    1077             : 
    1078             : 
    1079             : /**
    1080             :  * wpas_dbus_iface_set_ap_scan - Control roaming mode
    1081             :  * @message: Pointer to incoming dbus message
    1082             :  * @wpa_s: wpa_supplicant structure for a network interface
    1083             :  * Returns: A dbus message containing a UINT32 indicating success (1) or
    1084             :  *          failure (0)
    1085             :  *
    1086             :  * Handler function for "setAPScan" method call.
    1087             :  */
    1088           3 : DBusMessage * wpas_dbus_iface_set_ap_scan(DBusMessage *message,
    1089             :                                           struct wpa_supplicant *wpa_s)
    1090             : {
    1091           3 :         DBusMessage *reply = NULL;
    1092           3 :         dbus_uint32_t ap_scan = 1;
    1093             : 
    1094           3 :         if (!dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &ap_scan,
    1095             :                                    DBUS_TYPE_INVALID)) {
    1096           1 :                 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
    1097           1 :                 goto out;
    1098             :         }
    1099             : 
    1100           2 :         if (wpa_supplicant_set_ap_scan(wpa_s, ap_scan)) {
    1101           1 :                 reply = wpas_dbus_new_invalid_opts_error(message, NULL);
    1102           1 :                 goto out;
    1103             :         }
    1104             : 
    1105           1 :         reply = wpas_dbus_new_success_reply(message);
    1106             : 
    1107             : out:
    1108           3 :         return reply;
    1109             : }
    1110             : 
    1111             : 
    1112             : /**
    1113             :  * wpas_dbus_iface_set_smartcard_modules - Set smartcard related module paths
    1114             :  * @message: Pointer to incoming dbus message
    1115             :  * @wpa_s: wpa_supplicant structure for a network interface
    1116             :  * Returns: A dbus message containing a UINT32 indicating success (1) or
    1117             :  *          failure (0)
    1118             :  *
    1119             :  * Handler function for "setSmartcardModules" method call.
    1120             :  */
    1121          10 : DBusMessage * wpas_dbus_iface_set_smartcard_modules(
    1122             :         DBusMessage *message, struct wpa_supplicant *wpa_s)
    1123             : {
    1124             :         DBusMessageIter iter, iter_dict;
    1125          10 :         char *opensc_engine_path = NULL;
    1126          10 :         char *pkcs11_engine_path = NULL;
    1127          10 :         char *pkcs11_module_path = NULL;
    1128             :         struct wpa_dbus_dict_entry entry;
    1129             : 
    1130          10 :         if (!dbus_message_iter_init(message, &iter))
    1131           0 :                 goto error;
    1132             : 
    1133          10 :         if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
    1134           1 :                 goto error;
    1135             : 
    1136          22 :         while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
    1137          12 :                 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
    1138           1 :                         goto error;
    1139          13 :                 if (!strcmp(entry.key, "opensc_engine_path") &&
    1140           2 :                     entry.type == DBUS_TYPE_STRING) {
    1141           2 :                         os_free(opensc_engine_path);
    1142           2 :                         opensc_engine_path = os_strdup(entry.str_value);
    1143           2 :                         wpa_dbus_dict_entry_clear(&entry);
    1144           3 :                         if (opensc_engine_path == NULL)
    1145           1 :                                 goto error;
    1146          12 :                 } else if (!strcmp(entry.key, "pkcs11_engine_path") &&
    1147           3 :                            entry.type == DBUS_TYPE_STRING) {
    1148           3 :                         os_free(pkcs11_engine_path);
    1149           3 :                         pkcs11_engine_path = os_strdup(entry.str_value);
    1150           3 :                         wpa_dbus_dict_entry_clear(&entry);
    1151           5 :                         if (pkcs11_engine_path == NULL)
    1152           1 :                                 goto error;
    1153           8 :                 } else if (!strcmp(entry.key, "pkcs11_module_path") &&
    1154           2 :                                  entry.type == DBUS_TYPE_STRING) {
    1155           2 :                         os_free(pkcs11_module_path);
    1156           2 :                         pkcs11_module_path = os_strdup(entry.str_value);
    1157           2 :                         wpa_dbus_dict_entry_clear(&entry);
    1158           3 :                         if (pkcs11_module_path == NULL)
    1159           1 :                                 goto error;
    1160             :                 } else {
    1161           4 :                         wpa_dbus_dict_entry_clear(&entry);
    1162           4 :                         goto error;
    1163             :                 }
    1164             :         }
    1165             : 
    1166           1 :         os_free(wpa_s->conf->opensc_engine_path);
    1167           1 :         wpa_s->conf->opensc_engine_path = opensc_engine_path;
    1168           1 :         os_free(wpa_s->conf->pkcs11_engine_path);
    1169           1 :         wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path;
    1170           1 :         os_free(wpa_s->conf->pkcs11_module_path);
    1171           1 :         wpa_s->conf->pkcs11_module_path = pkcs11_module_path;
    1172             : 
    1173           1 :         wpa_sm_set_eapol(wpa_s->wpa, NULL);
    1174           1 :         eapol_sm_deinit(wpa_s->eapol);
    1175           1 :         wpa_s->eapol = NULL;
    1176           1 :         wpa_supplicant_init_eapol(wpa_s);
    1177           1 :         wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
    1178             : 
    1179           1 :         return wpas_dbus_new_success_reply(message);
    1180             : 
    1181             : error:
    1182           9 :         os_free(opensc_engine_path);
    1183           9 :         os_free(pkcs11_engine_path);
    1184           9 :         os_free(pkcs11_module_path);
    1185           9 :         return wpas_dbus_new_invalid_opts_error(message, NULL);
    1186             : }
    1187             : 
    1188             : 
    1189             : /**
    1190             :  * wpas_dbus_iface_get_state - Get interface state
    1191             :  * @message: Pointer to incoming dbus message
    1192             :  * @wpa_s: wpa_supplicant structure for a network interface
    1193             :  * Returns: A dbus message containing a STRING representing the current
    1194             :  *          interface state
    1195             :  *
    1196             :  * Handler function for "state" method call.
    1197             :  */
    1198           1 : DBusMessage * wpas_dbus_iface_get_state(DBusMessage *message,
    1199             :                                         struct wpa_supplicant *wpa_s)
    1200             : {
    1201           1 :         DBusMessage *reply = NULL;
    1202             :         const char *str_state;
    1203             : 
    1204           1 :         reply = dbus_message_new_method_return(message);
    1205           1 :         if (reply != NULL) {
    1206           1 :                 str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
    1207           1 :                 dbus_message_append_args(reply, DBUS_TYPE_STRING, &str_state,
    1208             :                                          DBUS_TYPE_INVALID);
    1209             :         }
    1210             : 
    1211           1 :         return reply;
    1212             : }
    1213             : 
    1214             : 
    1215             : /**
    1216             :  * wpas_dbus_iface_get_scanning - Get interface scanning state
    1217             :  * @message: Pointer to incoming dbus message
    1218             :  * @wpa_s: wpa_supplicant structure for a network interface
    1219             :  * Returns: A dbus message containing whether the interface is scanning
    1220             :  *
    1221             :  * Handler function for "scanning" method call.
    1222             :  */
    1223           1 : DBusMessage * wpas_dbus_iface_get_scanning(DBusMessage *message,
    1224             :                                            struct wpa_supplicant *wpa_s)
    1225             : {
    1226           1 :         DBusMessage *reply = NULL;
    1227           1 :         dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
    1228             : 
    1229           1 :         reply = dbus_message_new_method_return(message);
    1230           1 :         if (reply != NULL) {
    1231           1 :                 dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &scanning,
    1232             :                                          DBUS_TYPE_INVALID);
    1233             :         } else {
    1234           0 :                 wpa_printf(MSG_ERROR,
    1235             :                            "dbus: Not enough memory to return scanning state");
    1236             :         }
    1237             : 
    1238           1 :         return reply;
    1239             : }
    1240             : 
    1241             : 
    1242             : #ifndef CONFIG_NO_CONFIG_BLOBS
    1243             : 
    1244             : /**
    1245             :  * wpas_dbus_iface_set_blobs - Store named binary blobs (ie, for certificates)
    1246             :  * @message: Pointer to incoming dbus message
    1247             :  * @wpa_s: %wpa_supplicant data structure
    1248             :  * Returns: A dbus message containing a UINT32 indicating success (1) or
    1249             :  *          failure (0)
    1250             :  *
    1251             :  * Asks wpa_supplicant to internally store a one or more binary blobs.
    1252             :  */
    1253           8 : DBusMessage * wpas_dbus_iface_set_blobs(DBusMessage *message,
    1254             :                                         struct wpa_supplicant *wpa_s)
    1255             : {
    1256           8 :         DBusMessage *reply = NULL;
    1257           8 :         struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
    1258             :         DBusMessageIter iter, iter_dict;
    1259             : 
    1260           8 :         dbus_message_iter_init(message, &iter);
    1261             : 
    1262           8 :         if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
    1263           1 :                 return wpas_dbus_new_invalid_opts_error(message, NULL);
    1264             : 
    1265          16 :         while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
    1266             :                 struct wpa_config_blob *blob;
    1267             : 
    1268           8 :                 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
    1269           1 :                         reply = wpas_dbus_new_invalid_opts_error(message,
    1270             :                                                                  NULL);
    1271           1 :                         break;
    1272             :                 }
    1273             : 
    1274          13 :                 if (entry.type != DBUS_TYPE_ARRAY ||
    1275           6 :                     entry.array_type != DBUS_TYPE_BYTE) {
    1276           1 :                         reply = wpas_dbus_new_invalid_opts_error(
    1277             :                                 message, "Byte array expected.");
    1278           1 :                         break;
    1279             :                 }
    1280             : 
    1281          12 :                 if ((entry.array_len <= 0) || (entry.array_len > 65536) ||
    1282           6 :                     !strlen(entry.key)) {
    1283           1 :                         reply = wpas_dbus_new_invalid_opts_error(
    1284             :                                 message, "Invalid array size.");
    1285           1 :                         break;
    1286             :                 }
    1287             : 
    1288           5 :                 blob = os_zalloc(sizeof(*blob));
    1289           5 :                 if (blob == NULL) {
    1290           1 :                         reply = dbus_message_new_error(
    1291             :                                 message, WPAS_ERROR_ADD_ERROR,
    1292             :                                 "Not enough memory to add blob.");
    1293           1 :                         break;
    1294             :                 }
    1295           4 :                 blob->data = os_zalloc(entry.array_len);
    1296           4 :                 if (blob->data == NULL) {
    1297           1 :                         reply = dbus_message_new_error(
    1298             :                                 message, WPAS_ERROR_ADD_ERROR,
    1299             :                                 "Not enough memory to add blob data.");
    1300           1 :                         os_free(blob);
    1301           1 :                         break;
    1302             :                 }
    1303             : 
    1304           3 :                 blob->name = os_strdup(entry.key);
    1305           3 :                 blob->len = entry.array_len;
    1306           3 :                 os_memcpy(blob->data, (u8 *) entry.bytearray_value,
    1307             :                                 entry.array_len);
    1308           3 :                 if (blob->name == NULL) {
    1309           1 :                         wpa_config_free_blob(blob);
    1310           1 :                         reply = dbus_message_new_error(
    1311             :                                 message, WPAS_ERROR_ADD_ERROR,
    1312             :                                 "Error adding blob.");
    1313           1 :                         break;
    1314             :                 }
    1315             : 
    1316             :                 /* Success */
    1317           2 :                 if (!wpa_config_remove_blob(wpa_s->conf, blob->name))
    1318           0 :                         wpas_notify_blob_removed(wpa_s, blob->name);
    1319           2 :                 wpa_config_set_blob(wpa_s->conf, blob);
    1320           2 :                 wpas_notify_blob_added(wpa_s, blob->name);
    1321             : 
    1322           2 :                 wpa_dbus_dict_entry_clear(&entry);
    1323             :         }
    1324           7 :         wpa_dbus_dict_entry_clear(&entry);
    1325             : 
    1326           7 :         return reply ? reply : wpas_dbus_new_success_reply(message);
    1327             : }
    1328             : 
    1329             : 
    1330             : /**
    1331             :  * wpas_dbus_iface_remove_blob - Remove named binary blobs
    1332             :  * @message: Pointer to incoming dbus message
    1333             :  * @wpa_s: %wpa_supplicant data structure
    1334             :  * Returns: A dbus message containing a UINT32 indicating success (1) or
    1335             :  *          failure (0)
    1336             :  *
    1337             :  * Asks wpa_supplicant to remove one or more previously stored binary blobs.
    1338             :  */
    1339           5 : DBusMessage * wpas_dbus_iface_remove_blobs(DBusMessage *message,
    1340             :                                            struct wpa_supplicant *wpa_s)
    1341             : {
    1342             :         DBusMessageIter iter, array;
    1343           5 :         char *err_msg = NULL;
    1344             : 
    1345           5 :         dbus_message_iter_init(message, &iter);
    1346             : 
    1347           9 :         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
    1348           4 :             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
    1349           2 :                 return wpas_dbus_new_invalid_opts_error(message, NULL);
    1350             : 
    1351           3 :         dbus_message_iter_recurse(&iter, &array);
    1352          10 :         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
    1353             :                 const char *name;
    1354             : 
    1355           4 :                 dbus_message_iter_get_basic(&array, &name);
    1356           4 :                 if (!os_strlen(name))
    1357           1 :                         err_msg = "Invalid blob name.";
    1358           3 :                 else if (wpa_config_remove_blob(wpa_s->conf, name) != 0)
    1359           1 :                         err_msg = "Error removing blob.";
    1360             :                 else
    1361           2 :                         wpas_notify_blob_removed(wpa_s, name);
    1362           4 :                 dbus_message_iter_next(&array);
    1363             :         }
    1364             : 
    1365           3 :         if (err_msg)
    1366           2 :                 return dbus_message_new_error(message, WPAS_ERROR_REMOVE_ERROR,
    1367             :                                               err_msg);
    1368             : 
    1369           1 :         return wpas_dbus_new_success_reply(message);
    1370             : }
    1371             : 
    1372             : #endif /* CONFIG_NO_CONFIG_BLOBS */
    1373             : 
    1374             : 
    1375             : /**
    1376             :  * wpas_dbus_iface_flush - Clear BSS of old or all inactive entries
    1377             :  * @message: Pointer to incoming dbus message
    1378             :  * @wpa_s: %wpa_supplicant data structure
    1379             :  * Returns: a dbus message containing a UINT32 indicating success (1) or
    1380             :  *          failure (0), or returns a dbus error message with more information
    1381             :  *
    1382             :  * Handler function for "flush" method call. Handles requests for an
    1383             :  * interface with an optional "age" parameter that specifies the minimum
    1384             :  * age of a BSS to be flushed.
    1385             :  */
    1386           3 : DBusMessage * wpas_dbus_iface_flush(DBusMessage *message,
    1387             :                                     struct wpa_supplicant *wpa_s)
    1388             : {
    1389           3 :         int flush_age = 0;
    1390             : 
    1391           6 :         if (os_strlen(dbus_message_get_signature(message)) != 0 &&
    1392           3 :             !dbus_message_get_args(message, NULL,
    1393             :                                    DBUS_TYPE_INT32, &flush_age,
    1394             :                                    DBUS_TYPE_INVALID)) {
    1395           1 :                 return wpas_dbus_new_invalid_opts_error(message, NULL);
    1396             :         }
    1397             : 
    1398           2 :         if (flush_age == 0)
    1399           1 :                 wpa_bss_flush(wpa_s);
    1400             :         else
    1401           1 :                 wpa_bss_flush_by_age(wpa_s, flush_age);
    1402             : 
    1403           2 :         return wpas_dbus_new_success_reply(message);
    1404             : }

Generated by: LCOV version 1.10