dbus: Add support to get/set the country code
[mech_eap.git] / wpa_supplicant / dbus / dbus_new_handlers.c
1 /*
2  * WPA Supplicant / dbus-based control interface
3  * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
4  * Copyright (c) 2009-2010, Witold Sowa <witold.sowa@gmail.com>
5  * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * Alternatively, this software may be distributed under the terms of BSD
12  * license.
13  *
14  * See README and COPYING for more details.
15  */
16
17 #include "includes.h"
18
19 #include "common.h"
20 #include "common/ieee802_11_defs.h"
21 #include "eap_peer/eap_methods.h"
22 #include "eapol_supp/eapol_supp_sm.h"
23 #include "rsn_supp/wpa.h"
24 #include "../config.h"
25 #include "../wpa_supplicant_i.h"
26 #include "../driver_i.h"
27 #include "../notify.h"
28 #include "../wpas_glue.h"
29 #include "../bss.h"
30 #include "../scan.h"
31 #include "dbus_new_helpers.h"
32 #include "dbus_new.h"
33 #include "dbus_new_handlers.h"
34 #include "dbus_dict_helpers.h"
35
36 extern int wpa_debug_level;
37 extern int wpa_debug_show_keys;
38 extern int wpa_debug_timestamp;
39
40 static const char *debug_strings[] = {
41         "excessive", "msgdump", "debug", "info", "warning", "error", NULL
42 };
43
44
45 /**
46  * wpas_dbus_new_decompose_object_path - Decompose an interface object path into parts
47  * @path: The dbus object path
48  * @network: (out) the configured network this object path refers to, if any
49  * @bssid: (out) the scanned bssid this object path refers to, if any
50  * Returns: The object path of the network interface this path refers to
51  *
52  * For a given object path, decomposes the object path into object id, network,
53  * and BSSID parts, if those parts exist.
54  */
55 static char * wpas_dbus_new_decompose_object_path(const char *path,
56                                                   char **network,
57                                                   char **bssid)
58 {
59         const unsigned int dev_path_prefix_len =
60                 strlen(WPAS_DBUS_NEW_PATH_INTERFACES "/");
61         char *obj_path_only;
62         char *next_sep;
63
64         /* Be a bit paranoid about path */
65         if (!path || os_strncmp(path, WPAS_DBUS_NEW_PATH_INTERFACES "/",
66                                 dev_path_prefix_len))
67                 return NULL;
68
69         /* Ensure there's something at the end of the path */
70         if ((path + dev_path_prefix_len)[0] == '\0')
71                 return NULL;
72
73         obj_path_only = os_strdup(path);
74         if (obj_path_only == NULL)
75                 return NULL;
76
77         next_sep = os_strchr(obj_path_only + dev_path_prefix_len, '/');
78         if (next_sep != NULL) {
79                 const char *net_part = os_strstr(
80                         next_sep, WPAS_DBUS_NEW_NETWORKS_PART "/");
81                 const char *bssid_part = os_strstr(
82                         next_sep, WPAS_DBUS_NEW_BSSIDS_PART "/");
83
84                 if (network && net_part) {
85                         /* Deal with a request for a configured network */
86                         const char *net_name = net_part +
87                                 os_strlen(WPAS_DBUS_NEW_NETWORKS_PART "/");
88                         *network = NULL;
89                         if (os_strlen(net_name))
90                                 *network = os_strdup(net_name);
91                 } else if (bssid && bssid_part) {
92                         /* Deal with a request for a scanned BSSID */
93                         const char *bssid_name = bssid_part +
94                                 os_strlen(WPAS_DBUS_NEW_BSSIDS_PART "/");
95                         if (strlen(bssid_name))
96                                 *bssid = os_strdup(bssid_name);
97                         else
98                                 *bssid = NULL;
99                 }
100
101                 /* Cut off interface object path before "/" */
102                 *next_sep = '\0';
103         }
104
105         return obj_path_only;
106 }
107
108
109 /**
110  * wpas_dbus_error_unknown_error - Return a new InvalidArgs error message
111  * @message: Pointer to incoming dbus message this error refers to
112  * @arg: Optional string appended to error message
113  * Returns: a dbus error message
114  *
115  * Convenience function to create and return an UnknownError
116  */
117 DBusMessage * wpas_dbus_error_unknown_error(DBusMessage *message,
118                                             const char *arg)
119 {
120         /*
121          * This function can be called as a result of a failure
122          * within internal getter calls, which will call this function
123          * with a NULL message parameter.  However, dbus_message_new_error
124          * looks very unkindly (i.e, abort()) on a NULL message, so
125          * in this case, we should not call it.
126          */
127         if (message == NULL) {
128                 wpa_printf(MSG_INFO, "dbus: wpas_dbus_error_unknown_error "
129                            "called with NULL message (arg=%s)",
130                            arg ? arg : "N/A");
131                 return NULL;
132         }
133
134         return dbus_message_new_error(message, WPAS_DBUS_ERROR_UNKNOWN_ERROR,
135                                       arg);
136 }
137
138
139 /**
140  * wpas_dbus_error_iface_unknown - Return a new invalid interface error message
141  * @message: Pointer to incoming dbus message this error refers to
142  * Returns: A dbus error message
143  *
144  * Convenience function to create and return an invalid interface error
145  */
146 static DBusMessage * wpas_dbus_error_iface_unknown(DBusMessage *message)
147 {
148         return dbus_message_new_error(message, WPAS_DBUS_ERROR_IFACE_UNKNOWN,
149                                       "wpa_supplicant knows nothing about "
150                                       "this interface.");
151 }
152
153
154 /**
155  * wpas_dbus_error_network_unknown - Return a new NetworkUnknown error message
156  * @message: Pointer to incoming dbus message this error refers to
157  * Returns: a dbus error message
158  *
159  * Convenience function to create and return an invalid network error
160  */
161 static DBusMessage * wpas_dbus_error_network_unknown(DBusMessage *message)
162 {
163         return dbus_message_new_error(message, WPAS_DBUS_ERROR_NETWORK_UNKNOWN,
164                                       "There is no such a network in this "
165                                       "interface.");
166 }
167
168
169 /**
170  * wpas_dbus_error_invalid_args - Return a new InvalidArgs error message
171  * @message: Pointer to incoming dbus message this error refers to
172  * Returns: a dbus error message
173  *
174  * Convenience function to create and return an invalid options error
175  */
176 DBusMessage * wpas_dbus_error_invalid_args(DBusMessage *message,
177                                           const char *arg)
178 {
179         DBusMessage *reply;
180
181         reply = dbus_message_new_error(message, WPAS_DBUS_ERROR_INVALID_ARGS,
182                                        "Did not receive correct message "
183                                        "arguments.");
184         if (arg != NULL)
185                 dbus_message_append_args(reply, DBUS_TYPE_STRING, &arg,
186                                          DBUS_TYPE_INVALID);
187
188         return reply;
189 }
190
191
192 static const char *dont_quote[] = {
193         "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
194         "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
195         "bssid", NULL
196 };
197
198 static dbus_bool_t should_quote_opt(const char *key)
199 {
200         int i = 0;
201         while (dont_quote[i] != NULL) {
202                 if (os_strcmp(key, dont_quote[i]) == 0)
203                         return FALSE;
204                 i++;
205         }
206         return TRUE;
207 }
208
209 /**
210  * get_iface_by_dbus_path - Get a new network interface
211  * @global: Pointer to global data from wpa_supplicant_init()
212  * @path: Pointer to a dbus object path representing an interface
213  * Returns: Pointer to the interface or %NULL if not found
214  */
215 static struct wpa_supplicant * get_iface_by_dbus_path(
216         struct wpa_global *global, const char *path)
217 {
218         struct wpa_supplicant *wpa_s;
219
220         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
221                 if (os_strcmp(wpa_s->dbus_new_path, path) == 0)
222                         return wpa_s;
223         }
224         return NULL;
225 }
226
227
228 /**
229  * set_network_properties - Set properties of a configured network
230  * @message: Pointer to incoming dbus message
231  * @wpa_s: wpa_supplicant structure for a network interface
232  * @ssid: wpa_ssid structure for a configured network
233  * @iter: DBus message iterator containing dictionary of network
234  * properties to set.
235  * Returns: NULL when succeed or DBus error on failure
236  *
237  * Sets network configuration with parameters given id DBus dictionary
238  */
239 static DBusMessage * set_network_properties(DBusMessage *message,
240                                             struct wpa_supplicant *wpa_s,
241                                             struct wpa_ssid *ssid,
242                                             DBusMessageIter *iter)
243 {
244
245         struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
246         DBusMessage *reply = NULL;
247         DBusMessageIter iter_dict;
248
249         if (!wpa_dbus_dict_open_read(iter, &iter_dict))
250                 return wpas_dbus_error_invalid_args(message, NULL);
251
252         while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
253                 char *value = NULL;
254                 size_t size = 50;
255                 int ret;
256                 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
257                         reply = wpas_dbus_error_invalid_args(message, NULL);
258                         break;
259                 }
260                 if (entry.type == DBUS_TYPE_ARRAY &&
261                     entry.array_type == DBUS_TYPE_BYTE) {
262                         if (entry.array_len <= 0)
263                                 goto error;
264
265                         size = entry.array_len * 2 + 1;
266                         value = os_zalloc(size);
267                         if (value == NULL)
268                                 goto error;
269
270                         ret = wpa_snprintf_hex(value, size,
271                                                (u8 *) entry.bytearray_value,
272                                                entry.array_len);
273                         if (ret <= 0)
274                                 goto error;
275                 } else if (entry.type == DBUS_TYPE_STRING) {
276                         if (should_quote_opt(entry.key)) {
277                                 size = os_strlen(entry.str_value);
278                                 if (size <= 0)
279                                         goto error;
280
281                                 size += 3;
282                                 value = os_zalloc(size);
283                                 if (value == NULL)
284                                         goto error;
285
286                                 ret = os_snprintf(value, size, "\"%s\"",
287                                                   entry.str_value);
288                                 if (ret < 0 || (size_t) ret != (size - 1))
289                                         goto error;
290                         } else {
291                                 value = os_strdup(entry.str_value);
292                                 if (value == NULL)
293                                         goto error;
294                         }
295                 } else if (entry.type == DBUS_TYPE_UINT32) {
296                         value = os_zalloc(size);
297                         if (value == NULL)
298                                 goto error;
299
300                         ret = os_snprintf(value, size, "%u",
301                                           entry.uint32_value);
302                         if (ret <= 0)
303                                 goto error;
304                 } else if (entry.type == DBUS_TYPE_INT32) {
305                         value = os_zalloc(size);
306                         if (value == NULL)
307                                 goto error;
308
309                         ret = os_snprintf(value, size, "%d",
310                                           entry.int32_value);
311                         if (ret <= 0)
312                                 goto error;
313                 } else
314                         goto error;
315
316                 if (wpa_config_set(ssid, entry.key, value, 0) < 0)
317                         goto error;
318
319                 if ((os_strcmp(entry.key, "psk") == 0 &&
320                      value[0] == '"' && ssid->ssid_len) ||
321                     (strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
322                         wpa_config_update_psk(ssid);
323                 else if (os_strcmp(entry.key, "priority") == 0)
324                         wpa_config_update_prio_list(wpa_s->conf);
325
326                 os_free(value);
327                 wpa_dbus_dict_entry_clear(&entry);
328                 continue;
329
330         error:
331                 os_free(value);
332                 reply = wpas_dbus_error_invalid_args(message, entry.key);
333                 wpa_dbus_dict_entry_clear(&entry);
334                 break;
335         }
336
337         return reply;
338 }
339
340
341 /**
342  * wpas_dbus_simple_property_getter - Get basic type property
343  * @message: Pointer to incoming dbus message
344  * @type: DBus type of property (must be basic type)
345  * @val: pointer to place holding property value
346  * Returns: The DBus message containing response for Properties.Get call
347  * or DBus error message if error occurred.
348  *
349  * Generic getter for basic type properties. Type is required to be basic.
350  */
351 DBusMessage * wpas_dbus_simple_property_getter(DBusMessage *message,
352                                                const int type, const void *val)
353 {
354         DBusMessage *reply = NULL;
355         DBusMessageIter iter, variant_iter;
356
357         if (!dbus_type_is_basic(type)) {
358                 wpa_printf(MSG_ERROR, "dbus: wpas_dbus_simple_property_getter:"
359                            " given type is not basic");
360                 return wpas_dbus_error_unknown_error(message, NULL);
361         }
362
363         if (message == NULL)
364                 reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
365         else
366                 reply = dbus_message_new_method_return(message);
367
368         if (reply != NULL) {
369                 dbus_message_iter_init_append(reply, &iter);
370                 if (!dbus_message_iter_open_container(
371                             &iter, DBUS_TYPE_VARIANT,
372                             wpa_dbus_type_as_string(type), &variant_iter) ||
373                     !dbus_message_iter_append_basic(&variant_iter, type,
374                                                     val) ||
375                     !dbus_message_iter_close_container(&iter, &variant_iter)) {
376                         wpa_printf(MSG_ERROR, "dbus: "
377                                    "wpas_dbus_simple_property_getter: out of "
378                                    "memory to put property value into "
379                                    "message");
380                         dbus_message_unref(reply);
381                         reply = dbus_message_new_error(message,
382                                                        DBUS_ERROR_NO_MEMORY,
383                                                        NULL);
384                 }
385         } else {
386                 wpa_printf(MSG_ERROR, "dbus: wpas_dbus_simple_property_getter:"
387                            " out of memory to return property value");
388                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
389                                                NULL);
390         }
391
392         return reply;
393 }
394
395
396 /**
397  * wpas_dbus_simple_property_setter - Set basic type property
398  * @message: Pointer to incoming dbus message
399  * @type: DBus type of property (must be basic type)
400  * @val: pointer to place where value being set will be stored
401  * Returns: NULL or DBus error message if error occurred.
402  *
403  * Generic setter for basic type properties. Type is required to be basic.
404  */
405 DBusMessage * wpas_dbus_simple_property_setter(DBusMessage *message,
406                                                const int type, void *val)
407 {
408         DBusMessageIter iter, variant_iter;
409
410         if (!dbus_type_is_basic(type)) {
411                 wpa_printf(MSG_ERROR, "dbus: wpas_dbus_simple_property_setter:"
412                            " given type is not basic");
413                 return wpas_dbus_error_unknown_error(message, NULL);
414         }
415
416         if (!dbus_message_iter_init(message, &iter)) {
417                 wpa_printf(MSG_ERROR, "dbus: wpas_dbus_simple_property_setter:"
418                            " out of memory to return scanning state");
419                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
420                                               NULL);
421         }
422
423         /* omit first and second argument and get value from third */
424         dbus_message_iter_next(&iter);
425         dbus_message_iter_next(&iter);
426         dbus_message_iter_recurse(&iter, &variant_iter);
427
428         if (dbus_message_iter_get_arg_type(&variant_iter) != type) {
429                 wpa_printf(MSG_DEBUG, "dbus: wpas_dbus_simple_property_setter:"
430                            " wrong property type");
431                 return wpas_dbus_error_invalid_args(message,
432                                                     "wrong property type");
433         }
434         dbus_message_iter_get_basic(&variant_iter, val);
435
436         return NULL;
437 }
438
439
440 /**
441  * wpas_dbus_simple_array_property_getter - Get array type property
442  * @message: Pointer to incoming dbus message
443  * @type: DBus type of property array elements (must be basic type)
444  * @array: pointer to array of elements to put into response message
445  * @array_len: length of above array
446  * Returns: The DBus message containing response for Properties.Get call
447  * or DBus error message if error occurred.
448  *
449  * Generic getter for array type properties. Array elements type is
450  * required to be basic.
451  */
452 DBusMessage * wpas_dbus_simple_array_property_getter(DBusMessage *message,
453                                                      const int type,
454                                                      const void *array,
455                                                      size_t array_len)
456 {
457         DBusMessage *reply = NULL;
458         DBusMessageIter iter, variant_iter, array_iter;
459         char type_str[] = "a?"; /* ? will be replaced with subtype letter; */
460         const char *sub_type_str;
461         size_t element_size, i;
462
463         if (!dbus_type_is_basic(type)) {
464                 wpa_printf(MSG_ERROR, "dbus: "
465                            "wpas_dbus_simple_array_property_getter: given "
466                            "type is not basic");
467                 return wpas_dbus_error_unknown_error(message, NULL);
468         }
469
470         sub_type_str = wpa_dbus_type_as_string(type);
471         type_str[1] = sub_type_str[0];
472
473         if (message == NULL)
474                 reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
475         else
476                 reply = dbus_message_new_method_return(message);
477         if (reply == NULL) {
478                 wpa_printf(MSG_ERROR, "dbus: "
479                            "wpas_dbus_simple_array_property_getter: out of "
480                            "memory to create return message");
481                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
482                                               NULL);
483         }
484
485         dbus_message_iter_init_append(reply, &iter);
486
487         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
488                                               type_str, &variant_iter) ||
489             !dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
490                                               sub_type_str, &array_iter)) {
491                 wpa_printf(MSG_ERROR, "dbus: "
492                            "wpas_dbus_simple_array_property_getter: out of "
493                            "memory to open container");
494                 dbus_message_unref(reply);
495                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
496                                               NULL);
497         }
498
499         switch(type) {
500         case DBUS_TYPE_BYTE:
501         case DBUS_TYPE_BOOLEAN:
502                 element_size = 1;
503                 break;
504         case DBUS_TYPE_INT16:
505         case DBUS_TYPE_UINT16:
506                 element_size = sizeof(uint16_t);
507                 break;
508         case DBUS_TYPE_INT32:
509         case DBUS_TYPE_UINT32:
510                 element_size = sizeof(uint32_t);
511                 break;
512         case DBUS_TYPE_INT64:
513         case DBUS_TYPE_UINT64:
514                 element_size = sizeof(uint64_t);
515                 break;
516         case DBUS_TYPE_DOUBLE:
517                 element_size = sizeof(double);
518                 break;
519         case DBUS_TYPE_STRING:
520         case DBUS_TYPE_OBJECT_PATH:
521                 element_size = sizeof(char *);
522                 break;
523         default:
524                 wpa_printf(MSG_ERROR, "dbus: "
525                            "wpas_dbus_simple_array_property_getter: "
526                            "fatal: unknown element type");
527                 element_size = 1;
528                 break;
529         }
530
531         for (i = 0; i < array_len; i++) {
532                 dbus_message_iter_append_basic(&array_iter, type,
533                                                array + i * element_size);
534         }
535
536         if (!dbus_message_iter_close_container(&variant_iter, &array_iter) ||
537             !dbus_message_iter_close_container(&iter, &variant_iter)) {
538                 wpa_printf(MSG_ERROR, "dbus: "
539                            "wpas_dbus_simple_array_property_getter: out of "
540                            "memory to close container");
541                 dbus_message_unref(reply);
542                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
543                                               NULL);
544         }
545
546         return reply;
547 }
548
549
550 /**
551  * wpas_dbus_handler_create_interface - Request registration of a network iface
552  * @message: Pointer to incoming dbus message
553  * @global: %wpa_supplicant global data structure
554  * Returns: The object path of the new interface object,
555  *          or a dbus error message with more information
556  *
557  * Handler function for "CreateInterface" method call. Handles requests
558  * by dbus clients to register a network interface that wpa_supplicant
559  * will manage.
560  */
561 DBusMessage * wpas_dbus_handler_create_interface(DBusMessage *message,
562                                                  struct wpa_global *global)
563 {
564         DBusMessageIter iter_dict;
565         DBusMessage *reply = NULL;
566         DBusMessageIter iter;
567         struct wpa_dbus_dict_entry entry;
568         char *driver = NULL;
569         char *ifname = NULL;
570         char *confname = NULL;
571         char *bridge_ifname = NULL;
572
573         dbus_message_iter_init(message, &iter);
574
575         if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
576                 goto error;
577         while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
578                 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
579                         goto error;
580                 if (!strcmp(entry.key, "Driver") &&
581                     (entry.type == DBUS_TYPE_STRING)) {
582                         driver = os_strdup(entry.str_value);
583                         wpa_dbus_dict_entry_clear(&entry);
584                         if (driver == NULL)
585                                 goto error;
586                 } else if (!strcmp(entry.key, "Ifname") &&
587                            (entry.type == DBUS_TYPE_STRING)) {
588                         ifname = os_strdup(entry.str_value);
589                         wpa_dbus_dict_entry_clear(&entry);
590                         if (ifname == NULL)
591                                 goto error;
592                 } else if (!strcmp(entry.key, "ConfigFile") &&
593                            (entry.type == DBUS_TYPE_STRING)) {
594                         confname = os_strdup(entry.str_value);
595                         wpa_dbus_dict_entry_clear(&entry);
596                         if (confname == NULL)
597                                 goto error;
598                 } else if (!strcmp(entry.key, "BridgeIfname") &&
599                            (entry.type == DBUS_TYPE_STRING)) {
600                         bridge_ifname = os_strdup(entry.str_value);
601                         wpa_dbus_dict_entry_clear(&entry);
602                         if (bridge_ifname == NULL)
603                                 goto error;
604                 } else {
605                         wpa_dbus_dict_entry_clear(&entry);
606                         goto error;
607                 }
608         }
609
610         if (ifname == NULL)
611                 goto error; /* Required Ifname argument missing */
612
613         /*
614          * Try to get the wpa_supplicant record for this iface, return
615          * an error if we already control it.
616          */
617         if (wpa_supplicant_get_iface(global, ifname) != NULL) {
618                 reply = dbus_message_new_error(message,
619                                                WPAS_DBUS_ERROR_IFACE_EXISTS,
620                                                "wpa_supplicant already "
621                                                "controls this interface.");
622         } else {
623                 struct wpa_supplicant *wpa_s;
624                 struct wpa_interface iface;
625                 os_memset(&iface, 0, sizeof(iface));
626                 iface.driver = driver;
627                 iface.ifname = ifname;
628                 iface.confname = confname;
629                 iface.bridge_ifname = bridge_ifname;
630                 /* Otherwise, have wpa_supplicant attach to it. */
631                 if ((wpa_s = wpa_supplicant_add_iface(global, &iface))) {
632                         const char *path = wpa_s->dbus_new_path;
633                         reply = dbus_message_new_method_return(message);
634                         dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
635                                                  &path, DBUS_TYPE_INVALID);
636                 } else {
637                         reply = wpas_dbus_error_unknown_error(
638                                 message, "wpa_supplicant couldn't grab this "
639                                 "interface.");
640                 }
641         }
642
643 out:
644         os_free(driver);
645         os_free(ifname);
646         os_free(bridge_ifname);
647         return reply;
648
649 error:
650         reply = wpas_dbus_error_invalid_args(message, NULL);
651         goto out;
652 }
653
654
655 /**
656  * wpas_dbus_handler_remove_interface - Request deregistration of an interface
657  * @message: Pointer to incoming dbus message
658  * @global: wpa_supplicant global data structure
659  * Returns: a dbus message containing a UINT32 indicating success (1) or
660  *          failure (0), or returns a dbus error message with more information
661  *
662  * Handler function for "removeInterface" method call.  Handles requests
663  * by dbus clients to deregister a network interface that wpa_supplicant
664  * currently manages.
665  */
666 DBusMessage * wpas_dbus_handler_remove_interface(DBusMessage *message,
667                                                  struct wpa_global *global)
668 {
669         struct wpa_supplicant *wpa_s;
670         char *path;
671         DBusMessage *reply = NULL;
672
673         dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
674                               DBUS_TYPE_INVALID);
675
676         wpa_s = get_iface_by_dbus_path(global, path);
677         if (wpa_s == NULL)
678                 reply = wpas_dbus_error_iface_unknown(message);
679         else if (wpa_supplicant_remove_iface(global, wpa_s)) {
680                 reply = wpas_dbus_error_unknown_error(
681                         message, "wpa_supplicant couldn't remove this "
682                         "interface.");
683         }
684
685         return reply;
686 }
687
688
689 /**
690  * wpas_dbus_handler_get_interface - Get the object path for an interface name
691  * @message: Pointer to incoming dbus message
692  * @global: %wpa_supplicant global data structure
693  * Returns: The object path of the interface object,
694  *          or a dbus error message with more information
695  *
696  * Handler function for "getInterface" method call.
697  */
698 DBusMessage * wpas_dbus_handler_get_interface(DBusMessage *message,
699                                               struct wpa_global *global)
700 {
701         DBusMessage *reply = NULL;
702         const char *ifname;
703         const char *path;
704         struct wpa_supplicant *wpa_s;
705
706         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &ifname,
707                               DBUS_TYPE_INVALID);
708
709         wpa_s = wpa_supplicant_get_iface(global, ifname);
710         if (wpa_s == NULL)
711                 return wpas_dbus_error_iface_unknown(message);
712
713         path = wpa_s->dbus_new_path;
714         reply = dbus_message_new_method_return(message);
715         if (reply == NULL)
716                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
717                                               NULL);
718         if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
719                                       DBUS_TYPE_INVALID)) {
720                 dbus_message_unref(reply);
721                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
722                                               NULL);
723         }
724
725         return reply;
726 }
727
728
729 /**
730  * wpas_dbus_getter_debug_level - Get debug level
731  * @message: Pointer to incoming dbus message
732  * @global: %wpa_supplicant global data structure
733  * Returns: DBus message with value of debug level
734  *
735  * Getter for "DebugLevel" property.
736  */
737 DBusMessage * wpas_dbus_getter_debug_level(DBusMessage *message,
738                                            struct wpa_global *global)
739 {
740         const char *str;
741         int idx = wpa_debug_level;
742         if (idx < 0)
743                 idx = 0;
744         if (idx > 5)
745                 idx = 5;
746         str = debug_strings[idx];
747         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
748                                                 &str);
749 }
750
751
752 /**
753  * wpas_dbus_getter_debug_timestamp - Get debug timestamp
754  * @message: Pointer to incoming dbus message
755  * @global: %wpa_supplicant global data structure
756  * Returns: DBus message with value of debug timestamp
757  *
758  * Getter for "DebugTimestamp" property.
759  */
760 DBusMessage * wpas_dbus_getter_debug_timestamp(DBusMessage *message,
761                                                struct wpa_global *global)
762 {
763         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
764                                                 &wpa_debug_timestamp);
765
766 }
767
768
769 /**
770  * wpas_dbus_getter_debug_show_keys - Get debug show keys
771  * @message: Pointer to incoming dbus message
772  * @global: %wpa_supplicant global data structure
773  * Returns: DBus message with value of debug show_keys
774  *
775  * Getter for "DebugShowKeys" property.
776  */
777 DBusMessage * wpas_dbus_getter_debug_show_keys(DBusMessage *message,
778                                                struct wpa_global *global)
779 {
780         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
781                                                 &wpa_debug_show_keys);
782
783 }
784
785 /**
786  * wpas_dbus_setter_debug_level - Set debug level
787  * @message: Pointer to incoming dbus message
788  * @global: %wpa_supplicant global data structure
789  * Returns: %NULL or DBus error message
790  *
791  * Setter for "DebugLevel" property.
792  */
793 DBusMessage * wpas_dbus_setter_debug_level(DBusMessage *message,
794                                            struct wpa_global *global)
795 {
796         DBusMessage *reply;
797         const char *str = NULL;
798         int i, val = -1;
799
800         reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_STRING,
801                                                  &str);
802         if (reply)
803                 return reply;
804
805         for (i = 0; debug_strings[i]; i++)
806                 if (os_strcmp(debug_strings[i], str) == 0) {
807                         val = i;
808                         break;
809                 }
810
811         if (val < 0 ||
812             wpa_supplicant_set_debug_params(global, val, wpa_debug_timestamp,
813                                             wpa_debug_show_keys)) {
814                 return wpas_dbus_error_invalid_args(
815                         message, "Wrong debug level value");
816         }
817
818         return NULL;
819 }
820
821
822 /**
823  * wpas_dbus_setter_debug_timestamp - Set debug timestamp
824  * @message: Pointer to incoming dbus message
825  * @global: %wpa_supplicant global data structure
826  * Returns: %NULL or DBus error message
827  *
828  * Setter for "DebugTimestamp" property.
829  */
830 DBusMessage * wpas_dbus_setter_debug_timestamp(DBusMessage *message,
831                                                struct wpa_global *global)
832 {
833         DBusMessage *reply;
834         dbus_bool_t val;
835
836         reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_BOOLEAN,
837                                                  &val);
838         if (reply)
839                 return reply;
840
841         wpa_supplicant_set_debug_params(global, wpa_debug_level, val ? 1 : 0,
842                                         wpa_debug_show_keys);
843
844         return NULL;
845 }
846
847
848 /**
849  * wpas_dbus_setter_debug_show_keys - Set debug show keys
850  * @message: Pointer to incoming dbus message
851  * @global: %wpa_supplicant global data structure
852  * Returns: %NULL or DBus error message
853  *
854  * Setter for "DebugShowKeys" property.
855  */
856 DBusMessage * wpas_dbus_setter_debug_show_keys(DBusMessage *message,
857                                                struct wpa_global *global)
858 {
859         DBusMessage *reply;
860         dbus_bool_t val;
861
862         reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_BOOLEAN,
863                                                  &val);
864         if (reply)
865                 return reply;
866
867         wpa_supplicant_set_debug_params(global, wpa_debug_level,
868                                         wpa_debug_timestamp,
869                                         val ? 1 : 0);
870
871         return NULL;
872 }
873
874
875 /**
876  * wpas_dbus_getter_interfaces - Request registered interfaces list
877  * @message: Pointer to incoming dbus message
878  * @global: %wpa_supplicant global data structure
879  * Returns: The object paths array containing registered interfaces
880  * objects paths or DBus error on failure
881  *
882  * Getter for "Interfaces" property. Handles requests
883  * by dbus clients to return list of registered interfaces objects
884  * paths
885  */
886 DBusMessage * wpas_dbus_getter_interfaces(DBusMessage *message,
887                                           struct wpa_global *global)
888 {
889         DBusMessage *reply = NULL;
890         struct wpa_supplicant *wpa_s;
891         const char **paths;
892         unsigned int i = 0, num = 0;
893
894         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
895                 num++;
896
897         paths = os_zalloc(num * sizeof(char*));
898         if (!paths) {
899                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
900                                               NULL);
901         }
902
903         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
904                 paths[i++] = wpa_s->dbus_new_path;
905
906         reply = wpas_dbus_simple_array_property_getter(message,
907                                                        DBUS_TYPE_OBJECT_PATH,
908                                                        paths, num);
909
910         os_free(paths);
911         return reply;
912 }
913
914
915 /**
916  * wpas_dbus_getter_eap_methods - Request supported EAP methods list
917  * @message: Pointer to incoming dbus message
918  * @nothing: not used argument. may be NULL or anything else
919  * Returns: The object paths array containing supported EAP methods
920  * represented by strings or DBus error on failure
921  *
922  * Getter for "EapMethods" property. Handles requests
923  * by dbus clients to return list of strings with supported EAP methods
924  */
925 DBusMessage * wpas_dbus_getter_eap_methods(DBusMessage *message, void *nothing)
926 {
927         DBusMessage *reply = NULL;
928         char **eap_methods;
929         size_t num_items = 0;
930
931         eap_methods = eap_get_names_as_string_array(&num_items);
932         if (!eap_methods) {
933                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
934                                               NULL);
935         }
936
937         reply = wpas_dbus_simple_array_property_getter(message,
938                                                        DBUS_TYPE_STRING,
939                                                        eap_methods, num_items);
940
941         while (num_items)
942                 os_free(eap_methods[--num_items]);
943         os_free(eap_methods);
944         return reply;
945 }
946
947
948 static int wpas_dbus_get_scan_type(DBusMessage *message, DBusMessageIter *var,
949                                    char **type, DBusMessage **reply)
950 {
951         if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_STRING) {
952                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
953                            "Type must be a string");
954                 *reply = wpas_dbus_error_invalid_args(
955                         message, "Wrong Type value type. String required");
956                 return -1;
957         }
958         dbus_message_iter_get_basic(var, type);
959         return 0;
960 }
961
962
963 static int wpas_dbus_get_scan_ssids(DBusMessage *message, DBusMessageIter *var,
964                                     struct wpa_driver_scan_params *params,
965                                     DBusMessage **reply)
966 {
967         struct wpa_driver_scan_ssid *ssids = params->ssids;
968         size_t ssids_num = 0;
969         u8 *ssid;
970         DBusMessageIter array_iter, sub_array_iter;
971         char *val;
972         int len;
973
974         if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
975                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ssids "
976                            "must be an array of arrays of bytes");
977                 *reply = wpas_dbus_error_invalid_args(
978                         message, "Wrong SSIDs value type. Array of arrays of "
979                         "bytes required");
980                 return -1;
981         }
982
983         dbus_message_iter_recurse(var, &array_iter);
984
985         if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY ||
986             dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_BYTE)
987         {
988                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ssids "
989                            "must be an array of arrays of bytes");
990                 *reply = wpas_dbus_error_invalid_args(
991                         message, "Wrong SSIDs value type. Array of arrays of "
992                         "bytes required");
993                 return -1;
994         }
995
996         while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_ARRAY)
997         {
998                 if (ssids_num >= WPAS_MAX_SCAN_SSIDS) {
999                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1000                                    "Too many ssids specified on scan dbus "
1001                                    "call");
1002                         *reply = wpas_dbus_error_invalid_args(
1003                                 message, "Too many ssids specified. Specify "
1004                                 "at most four");
1005                         return -1;
1006                 }
1007
1008                 dbus_message_iter_recurse(&array_iter, &sub_array_iter);
1009
1010                 dbus_message_iter_get_fixed_array(&sub_array_iter, &val, &len);
1011
1012                 if (len != 0) {
1013                         ssid = os_malloc(len);
1014                         if (ssid == NULL) {
1015                                 wpa_printf(MSG_DEBUG,
1016                                            "wpas_dbus_handler_scan[dbus]: "
1017                                            "out of memory. Cannot allocate "
1018                                            "memory for SSID");
1019                                 *reply = dbus_message_new_error(
1020                                         message, DBUS_ERROR_NO_MEMORY, NULL);
1021                                 return -1;
1022                         }
1023                         os_memcpy(ssid, val, len);
1024                 } else {
1025                         /* Allow zero-length SSIDs */
1026                         ssid = NULL;
1027                 }
1028
1029                 ssids[ssids_num].ssid = ssid;
1030                 ssids[ssids_num].ssid_len = len;
1031
1032                 dbus_message_iter_next(&array_iter);
1033                 ssids_num++;
1034         }
1035
1036         params->num_ssids = ssids_num;
1037         return 0;
1038 }
1039
1040
1041 static int wpas_dbus_get_scan_ies(DBusMessage *message, DBusMessageIter *var,
1042                                   struct wpa_driver_scan_params *params,
1043                                   DBusMessage **reply)
1044 {
1045         u8 *ies = NULL, *nies;
1046         int ies_len = 0;
1047         DBusMessageIter array_iter, sub_array_iter;
1048         char *val;
1049         int len;
1050
1051         if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
1052                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ies must "
1053                            "be an array of arrays of bytes");
1054                 *reply = wpas_dbus_error_invalid_args(
1055                         message, "Wrong IEs value type. Array of arrays of "
1056                         "bytes required");
1057                 return -1;
1058         }
1059
1060         dbus_message_iter_recurse(var, &array_iter);
1061
1062         if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY ||
1063             dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_BYTE)
1064         {
1065                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ies must "
1066                            "be an array of arrays of bytes");
1067                 *reply = wpas_dbus_error_invalid_args(
1068                         message, "Wrong IEs value type. Array required");
1069                 return -1;
1070         }
1071
1072         while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_ARRAY)
1073         {
1074                 dbus_message_iter_recurse(&array_iter, &sub_array_iter);
1075
1076                 dbus_message_iter_get_fixed_array(&sub_array_iter, &val, &len);
1077                 if (len == 0) {
1078                         dbus_message_iter_next(&array_iter);
1079                         continue;
1080                 }
1081
1082                 nies = os_realloc(ies, ies_len + len);
1083                 if (nies == NULL) {
1084                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1085                                    "out of memory. Cannot allocate memory for "
1086                                    "IE");
1087                         os_free(ies);
1088                         *reply = dbus_message_new_error(
1089                                 message, DBUS_ERROR_NO_MEMORY, NULL);
1090                         return -1;
1091                 }
1092                 ies = nies;
1093                 os_memcpy(ies + ies_len, val, len);
1094                 ies_len += len;
1095
1096                 dbus_message_iter_next(&array_iter);
1097         }
1098
1099         params->extra_ies = ies;
1100         params->extra_ies_len = ies_len;
1101         return 0;
1102 }
1103
1104
1105 static int wpas_dbus_get_scan_channels(DBusMessage *message,
1106                                        DBusMessageIter *var,
1107                                        struct wpa_driver_scan_params *params,
1108                                        DBusMessage **reply)
1109 {
1110         DBusMessageIter array_iter, sub_array_iter;
1111         int *freqs = NULL, *nfreqs;
1112         int freqs_num = 0;
1113
1114         if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
1115                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1116                            "Channels must be an array of structs");
1117                 *reply = wpas_dbus_error_invalid_args(
1118                         message, "Wrong Channels value type. Array of structs "
1119                         "required");
1120                 return -1;
1121         }
1122
1123         dbus_message_iter_recurse(var, &array_iter);
1124
1125         if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_STRUCT) {
1126                 wpa_printf(MSG_DEBUG,
1127                            "wpas_dbus_handler_scan[dbus]: Channels must be an "
1128                            "array of structs");
1129                 *reply = wpas_dbus_error_invalid_args(
1130                         message, "Wrong Channels value type. Array of structs "
1131                         "required");
1132                 return -1;
1133         }
1134
1135         while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_STRUCT)
1136         {
1137                 int freq, width;
1138
1139                 dbus_message_iter_recurse(&array_iter, &sub_array_iter);
1140
1141                 if (dbus_message_iter_get_arg_type(&sub_array_iter) !=
1142                     DBUS_TYPE_UINT32) {
1143                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1144                                    "Channel must by specified by struct of "
1145                                    "two UINT32s %c",
1146                                    dbus_message_iter_get_arg_type(
1147                                            &sub_array_iter));
1148                         *reply = wpas_dbus_error_invalid_args(
1149                                 message, "Wrong Channel struct. Two UINT32s "
1150                                 "required");
1151                         os_free(freqs);
1152                         return -1;
1153                 }
1154                 dbus_message_iter_get_basic(&sub_array_iter, &freq);
1155
1156                 if (!dbus_message_iter_next(&sub_array_iter) ||
1157                     dbus_message_iter_get_arg_type(&sub_array_iter) !=
1158                     DBUS_TYPE_UINT32) {
1159                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1160                                    "Channel must by specified by struct of "
1161                                    "two UINT32s");
1162                         *reply = wpas_dbus_error_invalid_args(
1163                                 message,
1164                                 "Wrong Channel struct. Two UINT32s required");
1165                         os_free(freqs);
1166                         return -1;
1167                 }
1168
1169                 dbus_message_iter_get_basic(&sub_array_iter, &width);
1170
1171 #define FREQS_ALLOC_CHUNK 32
1172                 if (freqs_num % FREQS_ALLOC_CHUNK == 0) {
1173                         nfreqs = os_realloc(freqs, sizeof(int) *
1174                                             (freqs_num + FREQS_ALLOC_CHUNK));
1175                         if (nfreqs == NULL)
1176                                 os_free(freqs);
1177                         freqs = nfreqs;
1178                 }
1179                 if (freqs == NULL) {
1180                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1181                                    "out of memory. can't allocate memory for "
1182                                    "freqs");
1183                         *reply = dbus_message_new_error(
1184                                 message, DBUS_ERROR_NO_MEMORY, NULL);
1185                         return -1;
1186                 }
1187
1188                 freqs[freqs_num] = freq;
1189
1190                 freqs_num++;
1191                 dbus_message_iter_next(&array_iter);
1192         }
1193
1194         nfreqs = os_realloc(freqs,
1195                             sizeof(int) * (freqs_num + 1));
1196         if (nfreqs == NULL)
1197                 os_free(freqs);
1198         freqs = nfreqs;
1199         if (freqs == NULL) {
1200                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1201                            "out of memory. Can't allocate memory for freqs");
1202                 *reply = dbus_message_new_error(
1203                         message, DBUS_ERROR_NO_MEMORY, NULL);
1204                 return -1;
1205         }
1206         freqs[freqs_num] = 0;
1207
1208         params->freqs = freqs;
1209         return 0;
1210 }
1211
1212
1213 /**
1214  * wpas_dbus_handler_scan - Request a wireless scan on an interface
1215  * @message: Pointer to incoming dbus message
1216  * @wpa_s: wpa_supplicant structure for a network interface
1217  * Returns: NULL indicating success or DBus error message on failure
1218  *
1219  * Handler function for "Scan" method call of a network device. Requests
1220  * that wpa_supplicant perform a wireless scan as soon as possible
1221  * on a particular wireless interface.
1222  */
1223 DBusMessage * wpas_dbus_handler_scan(DBusMessage *message,
1224                                      struct wpa_supplicant *wpa_s)
1225 {
1226         DBusMessage *reply = NULL;
1227         DBusMessageIter iter, dict_iter, entry_iter, variant_iter;
1228         char *key = NULL, *type = NULL;
1229         struct wpa_driver_scan_params params;
1230         size_t i;
1231
1232         os_memset(&params, 0, sizeof(params));
1233
1234         dbus_message_iter_init(message, &iter);
1235
1236         dbus_message_iter_recurse(&iter, &dict_iter);
1237
1238         while (dbus_message_iter_get_arg_type(&dict_iter) ==
1239                         DBUS_TYPE_DICT_ENTRY) {
1240                 dbus_message_iter_recurse(&dict_iter, &entry_iter);
1241                 dbus_message_iter_get_basic(&entry_iter, &key);
1242                 dbus_message_iter_next(&entry_iter);
1243                 dbus_message_iter_recurse(&entry_iter, &variant_iter);
1244
1245                 if (os_strcmp(key, "Type") == 0) {
1246                         if (wpas_dbus_get_scan_type(message, &variant_iter,
1247                                                     &type, &reply) < 0)
1248                                 goto out;
1249                 } else if (os_strcmp(key, "SSIDs") == 0) {
1250                         if (wpas_dbus_get_scan_ssids(message, &variant_iter,
1251                                                      &params, &reply) < 0)
1252                                 goto out;
1253                 } else if (os_strcmp(key, "IEs") == 0) {
1254                         if (wpas_dbus_get_scan_ies(message, &variant_iter,
1255                                                    &params, &reply) < 0)
1256                                 goto out;
1257                 } else if (os_strcmp(key, "Channels") == 0) {
1258                         if (wpas_dbus_get_scan_channels(message, &variant_iter,
1259                                                         &params, &reply) < 0)
1260                                 goto out;
1261                 } else {
1262                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1263                                    "Unknown argument %s", key);
1264                         reply = wpas_dbus_error_invalid_args(message, key);
1265                         goto out;
1266                 }
1267
1268                 dbus_message_iter_next(&dict_iter);
1269         }
1270
1271         if (!type) {
1272                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1273                            "Scan type not specified");
1274                 reply = wpas_dbus_error_invalid_args(message, key);
1275                 goto out;
1276         }
1277
1278         if (!os_strcmp(type, "passive")) {
1279                 if (params.num_ssids || params.extra_ies_len) {
1280                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1281                                    "SSIDs or IEs specified for passive scan.");
1282                         reply = wpas_dbus_error_invalid_args(
1283                                 message, "You can specify only Channels in "
1284                                 "passive scan");
1285                         goto out;
1286                 } else if (params.freqs && params.freqs[0]) {
1287                         wpa_supplicant_trigger_scan(wpa_s, &params);
1288                 } else {
1289                         wpa_s->scan_req = 2;
1290                         wpa_supplicant_req_scan(wpa_s, 0, 0);
1291                 }
1292         } else if (!os_strcmp(type, "active")) {
1293                 if (!params.num_ssids) {
1294                         /* Add wildcard ssid */
1295                         params.num_ssids++;
1296                 }
1297                 wpa_supplicant_trigger_scan(wpa_s, &params);
1298         } else {
1299                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1300                            "Unknown scan type: %s", type);
1301                 reply = wpas_dbus_error_invalid_args(message,
1302                                                      "Wrong scan type");
1303                 goto out;
1304         }
1305
1306 out:
1307         for (i = 0; i < WPAS_MAX_SCAN_SSIDS; i++)
1308                 os_free((u8 *) params.ssids[i].ssid);
1309         os_free((u8 *) params.extra_ies);
1310         os_free(params.freqs);
1311         return reply;
1312 }
1313
1314
1315 /*
1316  * wpas_dbus_handler_disconnect - Terminate the current connection
1317  * @message: Pointer to incoming dbus message
1318  * @wpa_s: wpa_supplicant structure for a network interface
1319  * Returns: NotConnected DBus error message if already not connected
1320  * or NULL otherwise.
1321  *
1322  * Handler function for "Disconnect" method call of network interface.
1323  */
1324 DBusMessage * wpas_dbus_handler_disconnect(DBusMessage *message,
1325                                            struct wpa_supplicant *wpa_s)
1326 {
1327         if (wpa_s->current_ssid != NULL) {
1328                 wpa_s->disconnected = 1;
1329                 wpa_supplicant_deauthenticate(wpa_s,
1330                                               WLAN_REASON_DEAUTH_LEAVING);
1331
1332                 return NULL;
1333         }
1334
1335         return dbus_message_new_error(message, WPAS_DBUS_ERROR_NOT_CONNECTED,
1336                                       "This interface is not connected");
1337 }
1338
1339
1340 /**
1341  * wpas_dbus_new_iface_add_network - Add a new configured network
1342  * @message: Pointer to incoming dbus message
1343  * @wpa_s: wpa_supplicant structure for a network interface
1344  * Returns: A dbus message containing the object path of the new network
1345  *
1346  * Handler function for "AddNetwork" method call of a network interface.
1347  */
1348 DBusMessage * wpas_dbus_handler_add_network(DBusMessage *message,
1349                                             struct wpa_supplicant *wpa_s)
1350 {
1351         DBusMessage *reply = NULL;
1352         DBusMessageIter iter;
1353         struct wpa_ssid *ssid = NULL;
1354         char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *path = path_buf;
1355
1356         dbus_message_iter_init(message, &iter);
1357
1358         ssid = wpa_config_add_network(wpa_s->conf);
1359         if (ssid == NULL) {
1360                 wpa_printf(MSG_ERROR, "wpas_dbus_handler_add_network[dbus]: "
1361                            "can't add new interface.");
1362                 reply = wpas_dbus_error_unknown_error(
1363                         message,
1364                         "wpa_supplicant could not add "
1365                         "a network on this interface.");
1366                 goto err;
1367         }
1368         wpas_notify_network_added(wpa_s, ssid);
1369         ssid->disabled = 1;
1370         wpa_config_set_network_defaults(ssid);
1371
1372         reply = set_network_properties(message, wpa_s, ssid, &iter);
1373         if (reply) {
1374                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_add_network[dbus]:"
1375                            "control interface couldn't set network "
1376                            "properties");
1377                 goto err;
1378         }
1379
1380         /* Construct the object path for this network. */
1381         os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
1382                     "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
1383                     wpa_s->dbus_new_path, ssid->id);
1384
1385         reply = dbus_message_new_method_return(message);
1386         if (reply == NULL) {
1387                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1388                                                NULL);
1389                 goto err;
1390         }
1391         if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
1392                                       DBUS_TYPE_INVALID)) {
1393                 dbus_message_unref(reply);
1394                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1395                                                NULL);
1396                 goto err;
1397         }
1398
1399         return reply;
1400
1401 err:
1402         if (ssid) {
1403                 wpas_notify_network_removed(wpa_s, ssid);
1404                 wpa_config_remove_network(wpa_s->conf, ssid->id);
1405         }
1406         return reply;
1407 }
1408
1409
1410 /**
1411  * wpas_dbus_handler_remove_network - Remove a configured network
1412  * @message: Pointer to incoming dbus message
1413  * @wpa_s: wpa_supplicant structure for a network interface
1414  * Returns: NULL on success or dbus error on failure
1415  *
1416  * Handler function for "RemoveNetwork" method call of a network interface.
1417  */
1418 DBusMessage * wpas_dbus_handler_remove_network(DBusMessage *message,
1419                                                struct wpa_supplicant *wpa_s)
1420 {
1421         DBusMessage *reply = NULL;
1422         const char *op;
1423         char *iface = NULL, *net_id = NULL;
1424         int id;
1425         struct wpa_ssid *ssid;
1426
1427         dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
1428                               DBUS_TYPE_INVALID);
1429
1430         /* Extract the network ID and ensure the network */
1431         /* is actually a child of this interface */
1432         iface = wpas_dbus_new_decompose_object_path(op, &net_id, NULL);
1433         if (iface == NULL || os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
1434                 reply = wpas_dbus_error_invalid_args(message, op);
1435                 goto out;
1436         }
1437
1438         id = strtoul(net_id, NULL, 10);
1439         if (errno == EINVAL) {
1440                 reply = wpas_dbus_error_invalid_args(message, op);
1441                 goto out;
1442         }
1443
1444         ssid = wpa_config_get_network(wpa_s->conf, id);
1445         if (ssid == NULL) {
1446                 reply = wpas_dbus_error_network_unknown(message);
1447                 goto out;
1448         }
1449
1450         wpas_notify_network_removed(wpa_s, ssid);
1451
1452         if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
1453                 wpa_printf(MSG_ERROR,
1454                            "wpas_dbus_handler_remove_network[dbus]: "
1455                            "error occurred when removing network %d", id);
1456                 reply = wpas_dbus_error_unknown_error(
1457                         message, "error removing the specified network on "
1458                         "this interface.");
1459                 goto out;
1460         }
1461
1462         if (ssid == wpa_s->current_ssid)
1463                 wpa_supplicant_deauthenticate(wpa_s,
1464                                               WLAN_REASON_DEAUTH_LEAVING);
1465
1466 out:
1467         os_free(iface);
1468         os_free(net_id);
1469         return reply;
1470 }
1471
1472
1473 static void remove_network(void *arg, struct wpa_ssid *ssid)
1474 {
1475         struct wpa_supplicant *wpa_s = arg;
1476
1477         wpas_notify_network_removed(wpa_s, ssid);
1478
1479         if (wpa_config_remove_network(wpa_s->conf, ssid->id) < 0) {
1480                 wpa_printf(MSG_ERROR,
1481                            "wpas_dbus_handler_remove_all_networks[dbus]: "
1482                            "error occurred when removing network %d",
1483                            ssid->id);
1484                 return;
1485         }
1486
1487         if (ssid == wpa_s->current_ssid)
1488                 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1489 }
1490
1491
1492 /**
1493  * wpas_dbus_handler_remove_all_networks - Remove all configured networks
1494  * @message: Pointer to incoming dbus message
1495  * @wpa_s: wpa_supplicant structure for a network interface
1496  * Returns: NULL on success or dbus error on failure
1497  *
1498  * Handler function for "RemoveAllNetworks" method call of a network interface.
1499  */
1500 DBusMessage * wpas_dbus_handler_remove_all_networks(
1501         DBusMessage *message, struct wpa_supplicant *wpa_s)
1502 {
1503         /* NB: could check for failure and return an error */
1504         wpa_config_foreach_network(wpa_s->conf, remove_network, wpa_s);
1505         return NULL;
1506 }
1507
1508
1509 /**
1510  * wpas_dbus_handler_select_network - Attempt association with a network
1511  * @message: Pointer to incoming dbus message
1512  * @wpa_s: wpa_supplicant structure for a network interface
1513  * Returns: NULL on success or dbus error on failure
1514  *
1515  * Handler function for "SelectNetwork" method call of network interface.
1516  */
1517 DBusMessage * wpas_dbus_handler_select_network(DBusMessage *message,
1518                                                struct wpa_supplicant *wpa_s)
1519 {
1520         DBusMessage *reply = NULL;
1521         const char *op;
1522         char *iface = NULL, *net_id = NULL;
1523         int id;
1524         struct wpa_ssid *ssid;
1525
1526         dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
1527                               DBUS_TYPE_INVALID);
1528
1529         /* Extract the network ID and ensure the network */
1530         /* is actually a child of this interface */
1531         iface = wpas_dbus_new_decompose_object_path(op, &net_id, NULL);
1532         if (iface == NULL || os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
1533                 reply = wpas_dbus_error_invalid_args(message, op);
1534                 goto out;
1535         }
1536
1537         id = strtoul(net_id, NULL, 10);
1538         if (errno == EINVAL) {
1539                 reply = wpas_dbus_error_invalid_args(message, op);
1540                 goto out;
1541         }
1542
1543         ssid = wpa_config_get_network(wpa_s->conf, id);
1544         if (ssid == NULL) {
1545                 reply = wpas_dbus_error_network_unknown(message);
1546                 goto out;
1547         }
1548
1549         /* Finally, associate with the network */
1550         wpa_supplicant_select_network(wpa_s, ssid);
1551
1552 out:
1553         os_free(iface);
1554         os_free(net_id);
1555         return reply;
1556 }
1557
1558
1559 /**
1560  * wpas_dbus_handler_add_blob - Store named binary blob (ie, for certificates)
1561  * @message: Pointer to incoming dbus message
1562  * @wpa_s: %wpa_supplicant data structure
1563  * Returns: A dbus message containing an error on failure or NULL on success
1564  *
1565  * Asks wpa_supplicant to internally store a binary blobs.
1566  */
1567 DBusMessage * wpas_dbus_handler_add_blob(DBusMessage *message,
1568                                          struct wpa_supplicant *wpa_s)
1569 {
1570         DBusMessage *reply = NULL;
1571         DBusMessageIter iter, array_iter;
1572
1573         char *blob_name;
1574         u8 *blob_data;
1575         int blob_len;
1576         struct wpa_config_blob *blob = NULL;
1577
1578         dbus_message_iter_init(message, &iter);
1579         dbus_message_iter_get_basic(&iter, &blob_name);
1580
1581         if (wpa_config_get_blob(wpa_s->conf, blob_name)) {
1582                 return dbus_message_new_error(message,
1583                                               WPAS_DBUS_ERROR_BLOB_EXISTS,
1584                                               NULL);
1585         }
1586
1587         dbus_message_iter_next(&iter);
1588         dbus_message_iter_recurse(&iter, &array_iter);
1589
1590         dbus_message_iter_get_fixed_array(&array_iter, &blob_data, &blob_len);
1591
1592         blob = os_zalloc(sizeof(*blob));
1593         if (!blob) {
1594                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1595                                                NULL);
1596                 goto err;
1597         }
1598
1599         blob->data = os_malloc(blob_len);
1600         if (!blob->data) {
1601                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1602                                                NULL);
1603                 goto err;
1604         }
1605         os_memcpy(blob->data, blob_data, blob_len);
1606
1607         blob->len = blob_len;
1608         blob->name = os_strdup(blob_name);
1609         if (!blob->name) {
1610                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1611                                                NULL);
1612                 goto err;
1613         }
1614
1615         wpa_config_set_blob(wpa_s->conf, blob);
1616         wpas_notify_blob_added(wpa_s, blob->name);
1617
1618         return reply;
1619
1620 err:
1621         if (blob) {
1622                 os_free(blob->name);
1623                 os_free(blob->data);
1624                 os_free(blob);
1625         }
1626         return reply;
1627 }
1628
1629
1630 /**
1631  * wpas_dbus_handler_get_blob - Get named binary blob (ie, for certificates)
1632  * @message: Pointer to incoming dbus message
1633  * @wpa_s: %wpa_supplicant data structure
1634  * Returns: A dbus message containing array of bytes (blob)
1635  *
1636  * Gets one wpa_supplicant's binary blobs.
1637  */
1638 DBusMessage * wpas_dbus_handler_get_blob(DBusMessage *message,
1639                                          struct wpa_supplicant *wpa_s)
1640 {
1641         DBusMessage *reply = NULL;
1642         DBusMessageIter iter, array_iter;
1643
1644         char *blob_name;
1645         const struct wpa_config_blob *blob;
1646
1647         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &blob_name,
1648                               DBUS_TYPE_INVALID);
1649
1650         blob = wpa_config_get_blob(wpa_s->conf, blob_name);
1651         if (!blob) {
1652                 return dbus_message_new_error(message,
1653                                               WPAS_DBUS_ERROR_BLOB_UNKNOWN,
1654                                               "Blob id not set");
1655         }
1656
1657         reply = dbus_message_new_method_return(message);
1658         if (!reply) {
1659                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1660                                                NULL);
1661                 goto out;
1662         }
1663
1664         dbus_message_iter_init_append(reply, &iter);
1665
1666         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1667                                               DBUS_TYPE_BYTE_AS_STRING,
1668                                               &array_iter)) {
1669                 dbus_message_unref(reply);
1670                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1671                                                NULL);
1672                 goto out;
1673         }
1674
1675         if (!dbus_message_iter_append_fixed_array(&array_iter, DBUS_TYPE_BYTE,
1676                                                   &(blob->data), blob->len)) {
1677                 dbus_message_unref(reply);
1678                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1679                                                NULL);
1680                 goto out;
1681         }
1682
1683         if (!dbus_message_iter_close_container(&iter, &array_iter)) {
1684                 dbus_message_unref(reply);
1685                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1686                                                NULL);
1687                 goto out;
1688         }
1689
1690 out:
1691         return reply;
1692 }
1693
1694
1695 /**
1696  * wpas_remove_handler_remove_blob - Remove named binary blob
1697  * @message: Pointer to incoming dbus message
1698  * @wpa_s: %wpa_supplicant data structure
1699  * Returns: NULL on success or dbus error
1700  *
1701  * Asks wpa_supplicant to internally remove a binary blobs.
1702  */
1703 DBusMessage * wpas_dbus_handler_remove_blob(DBusMessage *message,
1704                                             struct wpa_supplicant *wpa_s)
1705 {
1706         DBusMessage *reply = NULL;
1707         char *blob_name;
1708
1709         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &blob_name,
1710                               DBUS_TYPE_INVALID);
1711
1712         if (wpa_config_remove_blob(wpa_s->conf, blob_name)) {
1713                 return dbus_message_new_error(message,
1714                                               WPAS_DBUS_ERROR_BLOB_UNKNOWN,
1715                                               "Blob id not set");
1716         }
1717         wpas_notify_blob_removed(wpa_s, blob_name);
1718
1719         return reply;
1720
1721 }
1722
1723 /*
1724  * wpas_dbus_handler_flush_bss - Flush the BSS cache
1725  * @message: Pointer to incoming dbus message
1726  * @wpa_s: wpa_supplicant structure for a network interface
1727  * Returns: NULL
1728  *
1729  * Handler function for "FlushBSS" method call of network interface.
1730  */
1731 DBusMessage * wpas_dbus_handler_flush_bss(DBusMessage *message,
1732                                           struct wpa_supplicant *wpa_s)
1733 {
1734         dbus_uint32_t age;
1735
1736         dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &age,
1737                               DBUS_TYPE_INVALID);
1738
1739         if (age == 0)
1740                 wpa_bss_flush(wpa_s);
1741         else
1742                 wpa_bss_flush_by_age(wpa_s, age);
1743
1744         return NULL;
1745 }
1746
1747
1748 /**
1749  * wpas_dbus_getter_capabilities - Return interface capabilities
1750  * @message: Pointer to incoming dbus message
1751  * @wpa_s: wpa_supplicant structure for a network interface
1752  * Returns: A dbus message containing a dict of strings
1753  *
1754  * Getter for "Capabilities" property of an interface.
1755  */
1756 DBusMessage * wpas_dbus_getter_capabilities(DBusMessage *message,
1757                                             struct wpa_supplicant *wpa_s)
1758 {
1759         DBusMessage *reply = NULL;
1760         struct wpa_driver_capa capa;
1761         int res;
1762         DBusMessageIter iter, iter_dict;
1763         DBusMessageIter iter_dict_entry, iter_dict_val, iter_array,
1764                 variant_iter;
1765         const char *scans[] = { "active", "passive", "ssid" };
1766         const char *modes[] = { "infrastructure", "ad-hoc", "ap" };
1767         int n = sizeof(modes) / sizeof(char *);
1768
1769         if (message == NULL)
1770                 reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
1771         else
1772                 reply = dbus_message_new_method_return(message);
1773         if (!reply)
1774                 goto nomem;
1775
1776         dbus_message_iter_init_append(reply, &iter);
1777         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
1778                                               "a{sv}", &variant_iter))
1779                 goto nomem;
1780
1781         if (!wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
1782                 goto nomem;
1783
1784         res = wpa_drv_get_capa(wpa_s, &capa);
1785
1786         /***** pairwise cipher */
1787         if (res < 0) {
1788                 const char *args[] = {"ccmp", "tkip", "none"};
1789                 if (!wpa_dbus_dict_append_string_array(
1790                             &iter_dict, "Pairwise", args,
1791                             sizeof(args) / sizeof(char*)))
1792                         goto nomem;
1793         } else {
1794                 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Pairwise",
1795                                                       &iter_dict_entry,
1796                                                       &iter_dict_val,
1797                                                       &iter_array))
1798                         goto nomem;
1799
1800                 if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
1801                         if (!wpa_dbus_dict_string_array_add_element(
1802                                     &iter_array, "ccmp"))
1803                                 goto nomem;
1804                 }
1805
1806                 if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
1807                         if (!wpa_dbus_dict_string_array_add_element(
1808                                     &iter_array, "tkip"))
1809                                 goto nomem;
1810                 }
1811
1812                 if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
1813                         if (!wpa_dbus_dict_string_array_add_element(
1814                                     &iter_array, "none"))
1815                                 goto nomem;
1816                 }
1817
1818                 if (!wpa_dbus_dict_end_string_array(&iter_dict,
1819                                                     &iter_dict_entry,
1820                                                     &iter_dict_val,
1821                                                     &iter_array))
1822                         goto nomem;
1823         }
1824
1825         /***** group cipher */
1826         if (res < 0) {
1827                 const char *args[] = {
1828                         "ccmp", "tkip", "wep104", "wep40"
1829                 };
1830                 if (!wpa_dbus_dict_append_string_array(
1831                             &iter_dict, "Group", args,
1832                             sizeof(args) / sizeof(char*)))
1833                         goto nomem;
1834         } else {
1835                 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Group",
1836                                                       &iter_dict_entry,
1837                                                       &iter_dict_val,
1838                                                       &iter_array))
1839                         goto nomem;
1840
1841                 if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
1842                         if (!wpa_dbus_dict_string_array_add_element(
1843                                     &iter_array, "ccmp"))
1844                                 goto nomem;
1845                 }
1846
1847                 if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
1848                         if (!wpa_dbus_dict_string_array_add_element(
1849                                     &iter_array, "tkip"))
1850                                 goto nomem;
1851                 }
1852
1853                 if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) {
1854                         if (!wpa_dbus_dict_string_array_add_element(
1855                                     &iter_array, "wep104"))
1856                                 goto nomem;
1857                 }
1858
1859                 if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) {
1860                         if (!wpa_dbus_dict_string_array_add_element(
1861                                     &iter_array, "wep40"))
1862                                 goto nomem;
1863                 }
1864
1865                 if (!wpa_dbus_dict_end_string_array(&iter_dict,
1866                                                     &iter_dict_entry,
1867                                                     &iter_dict_val,
1868                                                     &iter_array))
1869                         goto nomem;
1870         }
1871
1872         /***** key management */
1873         if (res < 0) {
1874                 const char *args[] = {
1875                         "wpa-psk", "wpa-eap", "ieee8021x", "wpa-none",
1876 #ifdef CONFIG_WPS
1877                         "wps",
1878 #endif /* CONFIG_WPS */
1879                         "none"
1880                 };
1881                 if (!wpa_dbus_dict_append_string_array(
1882                             &iter_dict, "KeyMgmt", args,
1883                             sizeof(args) / sizeof(char*)))
1884                         goto nomem;
1885         } else {
1886                 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "KeyMgmt",
1887                                                       &iter_dict_entry,
1888                                                       &iter_dict_val,
1889                                                       &iter_array))
1890                         goto nomem;
1891
1892                 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
1893                                                             "none"))
1894                         goto nomem;
1895
1896                 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
1897                                                             "ieee8021x"))
1898                         goto nomem;
1899
1900                 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1901                                      WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
1902                         if (!wpa_dbus_dict_string_array_add_element(
1903                                     &iter_array, "wpa-eap"))
1904                                 goto nomem;
1905
1906                         if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT)
1907                                 if (!wpa_dbus_dict_string_array_add_element(
1908                                             &iter_array, "wpa-ft-eap"))
1909                                         goto nomem;
1910
1911 /* TODO: Ensure that driver actually supports sha256 encryption. */
1912 #ifdef CONFIG_IEEE80211W
1913                         if (!wpa_dbus_dict_string_array_add_element(
1914                                     &iter_array, "wpa-eap-sha256"))
1915                                 goto nomem;
1916 #endif /* CONFIG_IEEE80211W */
1917                 }
1918
1919                 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
1920                                      WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
1921                         if (!wpa_dbus_dict_string_array_add_element(
1922                                     &iter_array, "wpa-psk"))
1923                                 goto nomem;
1924
1925                         if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK)
1926                                 if (!wpa_dbus_dict_string_array_add_element(
1927                                             &iter_array, "wpa-ft-psk"))
1928                                         goto nomem;
1929
1930 /* TODO: Ensure that driver actually supports sha256 encryption. */
1931 #ifdef CONFIG_IEEE80211W
1932                         if (!wpa_dbus_dict_string_array_add_element(
1933                                     &iter_array, "wpa-psk-sha256"))
1934                                 goto nomem;
1935 #endif /* CONFIG_IEEE80211W */
1936                 }
1937
1938                 if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
1939                         if (!wpa_dbus_dict_string_array_add_element(
1940                                     &iter_array, "wpa-none"))
1941                                 goto nomem;
1942                 }
1943
1944
1945 #ifdef CONFIG_WPS
1946                 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
1947                                                             "wps"))
1948                         goto nomem;
1949 #endif /* CONFIG_WPS */
1950
1951                 if (!wpa_dbus_dict_end_string_array(&iter_dict,
1952                                                     &iter_dict_entry,
1953                                                     &iter_dict_val,
1954                                                     &iter_array))
1955                         goto nomem;
1956         }
1957
1958         /***** WPA protocol */
1959         if (res < 0) {
1960                 const char *args[] = { "rsn", "wpa" };
1961                 if (!wpa_dbus_dict_append_string_array(
1962                             &iter_dict, "Protocol", args,
1963                             sizeof(args) / sizeof(char*)))
1964                         goto nomem;
1965         } else {
1966                 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Protocol",
1967                                                       &iter_dict_entry,
1968                                                       &iter_dict_val,
1969                                                       &iter_array))
1970                         goto nomem;
1971
1972                 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
1973                                      WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
1974                         if (!wpa_dbus_dict_string_array_add_element(
1975                                     &iter_array, "rsn"))
1976                                 goto nomem;
1977                 }
1978
1979                 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1980                                      WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
1981                         if (!wpa_dbus_dict_string_array_add_element(
1982                                     &iter_array, "wpa"))
1983                                 goto nomem;
1984                 }
1985
1986                 if (!wpa_dbus_dict_end_string_array(&iter_dict,
1987                                                     &iter_dict_entry,
1988                                                     &iter_dict_val,
1989                                                     &iter_array))
1990                         goto nomem;
1991         }
1992
1993         /***** auth alg */
1994         if (res < 0) {
1995                 const char *args[] = { "open", "shared", "leap" };
1996                 if (!wpa_dbus_dict_append_string_array(
1997                             &iter_dict, "AuthAlg", args,
1998                             sizeof(args) / sizeof(char*)))
1999                         goto nomem;
2000         } else {
2001                 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "AuthAlg",
2002                                                       &iter_dict_entry,
2003                                                       &iter_dict_val,
2004                                                       &iter_array))
2005                         goto nomem;
2006
2007                 if (capa.auth & (WPA_DRIVER_AUTH_OPEN)) {
2008                         if (!wpa_dbus_dict_string_array_add_element(
2009                                     &iter_array, "open"))
2010                                 goto nomem;
2011                 }
2012
2013                 if (capa.auth & (WPA_DRIVER_AUTH_SHARED)) {
2014                         if (!wpa_dbus_dict_string_array_add_element(
2015                                     &iter_array, "shared"))
2016                                 goto nomem;
2017                 }
2018
2019                 if (capa.auth & (WPA_DRIVER_AUTH_LEAP)) {
2020                         if (!wpa_dbus_dict_string_array_add_element(
2021                                     &iter_array, "leap"))
2022                                 goto nomem;
2023                 }
2024
2025                 if (!wpa_dbus_dict_end_string_array(&iter_dict,
2026                                                     &iter_dict_entry,
2027                                                     &iter_dict_val,
2028                                                     &iter_array))
2029                         goto nomem;
2030         }
2031
2032         /***** Scan */
2033         if (!wpa_dbus_dict_append_string_array(&iter_dict, "Scan", scans,
2034                                                sizeof(scans) / sizeof(char *)))
2035                 goto nomem;
2036
2037         /***** Modes */
2038         if (res < 0 || !(capa.flags & WPA_DRIVER_FLAGS_AP))
2039                 n--; /* exclude ap mode if it is not supported by the driver */
2040         if (!wpa_dbus_dict_append_string_array(&iter_dict, "Modes", modes, n))
2041                 goto nomem;
2042
2043         if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict))
2044                 goto nomem;
2045         if (!dbus_message_iter_close_container(&iter, &variant_iter))
2046                 goto nomem;
2047
2048         return reply;
2049
2050 nomem:
2051         if (reply)
2052                 dbus_message_unref(reply);
2053
2054         return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY, NULL);
2055 }
2056
2057
2058 /**
2059  * wpas_dbus_getter_state - Get interface state
2060  * @message: Pointer to incoming dbus message
2061  * @wpa_s: wpa_supplicant structure for a network interface
2062  * Returns: A dbus message containing a STRING representing the current
2063  *          interface state
2064  *
2065  * Getter for "State" property.
2066  */
2067 DBusMessage * wpas_dbus_getter_state(DBusMessage *message,
2068                                      struct wpa_supplicant *wpa_s)
2069 {
2070         DBusMessage *reply = NULL;
2071         const char *str_state;
2072         char *state_ls, *tmp;
2073
2074         str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
2075
2076         /* make state string lowercase to fit new DBus API convention
2077          */
2078         state_ls = tmp = os_strdup(str_state);
2079         if (!tmp) {
2080                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
2081                                               NULL);
2082         }
2083         while (*tmp) {
2084                 *tmp = tolower(*tmp);
2085                 tmp++;
2086         }
2087
2088         reply = wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
2089                                                  &state_ls);
2090
2091         os_free(state_ls);
2092
2093         return reply;
2094 }
2095
2096
2097 /**
2098  * wpas_dbus_new_iface_get_scanning - Get interface scanning state
2099  * @message: Pointer to incoming dbus message
2100  * @wpa_s: wpa_supplicant structure for a network interface
2101  * Returns: A dbus message containing whether the interface is scanning
2102  *
2103  * Getter for "scanning" property.
2104  */
2105 DBusMessage * wpas_dbus_getter_scanning(DBusMessage *message,
2106                                         struct wpa_supplicant *wpa_s)
2107 {
2108         dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
2109         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
2110                                                 &scanning);
2111 }
2112
2113
2114 /**
2115  * wpas_dbus_getter_ap_scan - Control roaming mode
2116  * @message: Pointer to incoming dbus message
2117  * @wpa_s: wpa_supplicant structure for a network interface
2118  * Returns: A message containong value of ap_scan variable
2119  *
2120  * Getter function for "ApScan" property.
2121  */
2122 DBusMessage * wpas_dbus_getter_ap_scan(DBusMessage *message,
2123                                        struct wpa_supplicant *wpa_s)
2124 {
2125         dbus_uint32_t ap_scan = wpa_s->conf->ap_scan;
2126         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_UINT32,
2127                                                 &ap_scan);
2128 }
2129
2130
2131 /**
2132  * wpas_dbus_setter_ap_scan - Control roaming mode
2133  * @message: Pointer to incoming dbus message
2134  * @wpa_s: wpa_supplicant structure for a network interface
2135  * Returns: NULL
2136  *
2137  * Setter function for "ApScan" property.
2138  */
2139 DBusMessage * wpas_dbus_setter_ap_scan(DBusMessage *message,
2140                                        struct wpa_supplicant *wpa_s)
2141 {
2142         DBusMessage *reply = NULL;
2143         dbus_uint32_t ap_scan;
2144
2145         reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_UINT32,
2146                                                  &ap_scan);
2147         if (reply)
2148                 return reply;
2149
2150         if (wpa_supplicant_set_ap_scan(wpa_s, ap_scan)) {
2151                 return wpas_dbus_error_invalid_args(
2152                         message, "ap_scan must equal 0, 1 or 2");
2153         }
2154         return NULL;
2155 }
2156
2157
2158 /**
2159  * wpas_dbus_getter_bss_expire_age - Get BSS entry expiration age
2160  * @message: Pointer to incoming dbus message
2161  * @wpa_s: wpa_supplicant structure for a network interface
2162  * Returns: A message containing value of bss_expiration_age variable
2163  *
2164  * Getter function for "BSSExpireAge" property.
2165  */
2166 DBusMessage * wpas_dbus_getter_bss_expire_age(DBusMessage *message,
2167                                               struct wpa_supplicant *wpa_s)
2168 {
2169         dbus_uint32_t expire_age = wpa_s->conf->bss_expiration_age;
2170         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_UINT32,
2171                                                 &expire_age);
2172 }
2173
2174
2175 /**
2176  * wpas_dbus_setter_bss_expire_age - Control BSS entry expiration age
2177  * @message: Pointer to incoming dbus message
2178  * @wpa_s: wpa_supplicant structure for a network interface
2179  * Returns: NULL
2180  *
2181  * Setter function for "BSSExpireAge" property.
2182  */
2183 DBusMessage * wpas_dbus_setter_bss_expire_age(DBusMessage *message,
2184                                               struct wpa_supplicant *wpa_s)
2185 {
2186         DBusMessage *reply = NULL;
2187         dbus_uint32_t expire_age;
2188
2189         reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_UINT32,
2190                                                  &expire_age);
2191         if (reply)
2192                 return reply;
2193
2194         if (wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age)) {
2195                 return wpas_dbus_error_invalid_args(
2196                         message, "BSSExpireAge must be >=10");
2197         }
2198         return NULL;
2199 }
2200
2201
2202 /**
2203  * wpas_dbus_getter_bss_expire_count - Get BSS entry expiration scan count
2204  * @message: Pointer to incoming dbus message
2205  * @wpa_s: wpa_supplicant structure for a network interface
2206  * Returns: A message containing value of bss_expire_count variable
2207  *
2208  * Getter function for "BSSExpireCount" property.
2209  */
2210 DBusMessage * wpas_dbus_getter_bss_expire_count(DBusMessage *message,
2211                                                 struct wpa_supplicant *wpa_s)
2212 {
2213         dbus_uint32_t expire_count = wpa_s->conf->bss_expiration_age;
2214         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_UINT32,
2215                                                 &expire_count);
2216 }
2217
2218
2219 /**
2220  * wpas_dbus_setter_bss_expire_count - Control BSS entry expiration scan count
2221  * @message: Pointer to incoming dbus message
2222  * @wpa_s: wpa_supplicant structure for a network interface
2223  * Returns: NULL
2224  *
2225  * Setter function for "BSSExpireCount" property.
2226  */
2227 DBusMessage * wpas_dbus_setter_bss_expire_count(DBusMessage *message,
2228                                                 struct wpa_supplicant *wpa_s)
2229 {
2230         DBusMessage *reply = NULL;
2231         dbus_uint32_t expire_count;
2232
2233         reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_UINT32,
2234                                                  &expire_count);
2235         if (reply)
2236                 return reply;
2237
2238         if (wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count)) {
2239                 return wpas_dbus_error_invalid_args(
2240                         message, "BSSExpireCount must be >0");
2241         }
2242         return NULL;
2243 }
2244
2245
2246 /**
2247  * wpas_dbus_getter_country - Control country code
2248  * @message: Pointer to incoming dbus message
2249  * @wpa_s: wpa_supplicant structure for a network interface
2250  * Returns: A message containong value of country variable
2251  *
2252  * Getter function for "Country" property.
2253  */
2254 DBusMessage * wpas_dbus_getter_country(DBusMessage *message,
2255                                        struct wpa_supplicant *wpa_s)
2256 {
2257         char country[3];
2258         char *str = country;
2259
2260         country[0] = wpa_s->conf->country[0];
2261         country[1] = wpa_s->conf->country[1];
2262         country[2] = '\0';
2263
2264         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
2265                                                 &str);
2266 }
2267
2268
2269 /**
2270  * wpas_dbus_setter_country - Control country code
2271  * @message: Pointer to incoming dbus message
2272  * @wpa_s: wpa_supplicant structure for a network interface
2273  * Returns: NULL
2274  *
2275  * Setter function for "Country" property.
2276  */
2277 DBusMessage * wpas_dbus_setter_country(DBusMessage *message,
2278                                        struct wpa_supplicant *wpa_s)
2279 {
2280         DBusMessage *reply = NULL;
2281         const char *country;
2282
2283         reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_STRING,
2284                                                  &country);
2285         if (reply)
2286                 return reply;
2287
2288         if (!country[0] || !country[1])
2289                 return wpas_dbus_error_invalid_args(message,
2290                                                     "invalid country code");
2291
2292         if (wpa_s->drv_priv != NULL && wpa_drv_set_country(wpa_s, country)) {
2293                 wpa_printf(MSG_DEBUG, "Failed to set country");
2294                 return wpas_dbus_error_invalid_args(
2295                         message, "failed to set country code");
2296         }
2297
2298         wpa_s->conf->country[0] = country[0];
2299         wpa_s->conf->country[1] = country[1];
2300         return NULL;
2301 }
2302
2303
2304 /**
2305  * wpas_dbus_getter_ifname - Get interface name
2306  * @message: Pointer to incoming dbus message
2307  * @wpa_s: wpa_supplicant structure for a network interface
2308  * Returns: A dbus message containing a name of network interface
2309  * associated with with wpa_s
2310  *
2311  * Getter for "Ifname" property.
2312  */
2313 DBusMessage * wpas_dbus_getter_ifname(DBusMessage *message,
2314                                       struct wpa_supplicant *wpa_s)
2315 {
2316         const char *ifname = wpa_s->ifname;
2317         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
2318                                                 &ifname);
2319 }
2320
2321
2322 /**
2323  * wpas_dbus_getter_driver - Get interface name
2324  * @message: Pointer to incoming dbus message
2325  * @wpa_s: wpa_supplicant structure for a network interface
2326  * Returns: A dbus message containing a name of network interface
2327  * driver associated with with wpa_s
2328  *
2329  * Getter for "Driver" property.
2330  */
2331 DBusMessage * wpas_dbus_getter_driver(DBusMessage *message,
2332                                       struct wpa_supplicant *wpa_s)
2333 {
2334         const char *driver;
2335
2336         if (wpa_s->driver == NULL || wpa_s->driver->name == NULL) {
2337                 wpa_printf(MSG_DEBUG, "wpas_dbus_getter_driver[dbus]: "
2338                            "wpa_s has no driver set");
2339                 return wpas_dbus_error_unknown_error(message, NULL);
2340         }
2341
2342         driver = wpa_s->driver->name;
2343         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
2344                                                 &driver);
2345 }
2346
2347
2348 /**
2349  * wpas_dbus_getter_current_bss - Get current bss object path
2350  * @message: Pointer to incoming dbus message
2351  * @wpa_s: wpa_supplicant structure for a network interface
2352  * Returns: A dbus message containing a DBus object path to
2353  * current BSS
2354  *
2355  * Getter for "CurrentBSS" property.
2356  */
2357 DBusMessage * wpas_dbus_getter_current_bss(DBusMessage *message,
2358                                            struct wpa_supplicant *wpa_s)
2359 {
2360         DBusMessage *reply;
2361         char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *bss_obj_path = path_buf;
2362
2363         if (wpa_s->current_bss)
2364                 os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
2365                             "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
2366                             wpa_s->dbus_new_path, wpa_s->current_bss->id);
2367         else
2368                 os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
2369
2370         reply = wpas_dbus_simple_property_getter(message,
2371                                                  DBUS_TYPE_OBJECT_PATH,
2372                                                  &bss_obj_path);
2373
2374         return reply;
2375 }
2376
2377
2378 /**
2379  * wpas_dbus_getter_current_network - Get current network object path
2380  * @message: Pointer to incoming dbus message
2381  * @wpa_s: wpa_supplicant structure for a network interface
2382  * Returns: A dbus message containing a DBus object path to
2383  * current network
2384  *
2385  * Getter for "CurrentNetwork" property.
2386  */
2387 DBusMessage * wpas_dbus_getter_current_network(DBusMessage *message,
2388                                                struct wpa_supplicant *wpa_s)
2389 {
2390         DBusMessage *reply;
2391         char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *net_obj_path = path_buf;
2392
2393         if (wpa_s->current_ssid)
2394                 os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
2395                             "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
2396                             wpa_s->dbus_new_path, wpa_s->current_ssid->id);
2397         else
2398                 os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
2399
2400         reply = wpas_dbus_simple_property_getter(message,
2401                                                  DBUS_TYPE_OBJECT_PATH,
2402                                                  &net_obj_path);
2403
2404         return reply;
2405 }
2406
2407
2408 /**
2409  * wpas_dbus_getter_current_auth_mode - Get current authentication type
2410  * @message: Pointer to incoming dbus message
2411  * @wpa_s: wpa_supplicant structure for a network interface
2412  * Returns: A dbus message containing a string indicating the current
2413  * authentication type.
2414  *
2415  * Getter for "CurrentAuthMode" property.
2416  */
2417 DBusMessage * wpas_dbus_getter_current_auth_mode(DBusMessage *message,
2418                                                  struct wpa_supplicant *wpa_s)
2419 {
2420         DBusMessage *reply;
2421         const char *eap_mode;
2422         const char *auth_mode;
2423         char eap_mode_buf[WPAS_DBUS_AUTH_MODE_MAX];
2424
2425         if (wpa_s->wpa_state != WPA_COMPLETED) {
2426                 auth_mode = "INACTIVE";
2427         } else if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
2428             wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
2429                 eap_mode = wpa_supplicant_get_eap_mode(wpa_s);
2430                 os_snprintf(eap_mode_buf, WPAS_DBUS_AUTH_MODE_MAX,
2431                             "EAP-%s", eap_mode);
2432                 auth_mode = eap_mode_buf;
2433
2434         } else {
2435                 auth_mode = wpa_key_mgmt_txt(wpa_s->key_mgmt,
2436                                              wpa_s->current_ssid->proto);
2437         }
2438
2439         reply = wpas_dbus_simple_property_getter(message,
2440                                                  DBUS_TYPE_STRING,
2441                                                  &auth_mode);
2442
2443         return reply;
2444 }
2445
2446
2447 /**
2448  * wpas_dbus_getter_bridge_ifname - Get interface name
2449  * @message: Pointer to incoming dbus message
2450  * @wpa_s: wpa_supplicant structure for a network interface
2451  * Returns: A dbus message containing a name of bridge network
2452  * interface associated with with wpa_s
2453  *
2454  * Getter for "BridgeIfname" property.
2455  */
2456 DBusMessage * wpas_dbus_getter_bridge_ifname(DBusMessage *message,
2457                                              struct wpa_supplicant *wpa_s)
2458 {
2459         const char *bridge_ifname = NULL;
2460
2461         bridge_ifname = wpa_s->bridge_ifname;
2462         if (bridge_ifname == NULL) {
2463                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bridge_ifname[dbus]: "
2464                            "wpa_s has no bridge interface name set");
2465                 return wpas_dbus_error_unknown_error(message, NULL);
2466         }
2467
2468         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
2469                                                 &bridge_ifname);
2470 }
2471
2472
2473 /**
2474  * wpas_dbus_getter_bsss - Get array of BSSs objects
2475  * @message: Pointer to incoming dbus message
2476  * @wpa_s: wpa_supplicant structure for a network interface
2477  * Returns: a dbus message containing an array of all known BSS objects
2478  * dbus paths
2479  *
2480  * Getter for "BSSs" property.
2481  */
2482 DBusMessage * wpas_dbus_getter_bsss(DBusMessage *message,
2483                                     struct wpa_supplicant *wpa_s)
2484 {
2485         DBusMessage *reply = NULL;
2486         struct wpa_bss *bss;
2487         char **paths;
2488         unsigned int i = 0;
2489
2490         paths = os_zalloc(wpa_s->num_bss * sizeof(char *));
2491         if (!paths) {
2492                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
2493                                               NULL);
2494         }
2495
2496         /* Loop through scan results and append each result's object path */
2497         dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
2498                 paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
2499                 if (paths[i] == NULL) {
2500                         reply = dbus_message_new_error(message,
2501                                                        DBUS_ERROR_NO_MEMORY,
2502                                                        NULL);
2503                         goto out;
2504                 }
2505                 /* Construct the object path for this BSS. */
2506                 os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
2507                             "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
2508                             wpa_s->dbus_new_path, bss->id);
2509         }
2510
2511         reply = wpas_dbus_simple_array_property_getter(message,
2512                                                        DBUS_TYPE_OBJECT_PATH,
2513                                                        paths, wpa_s->num_bss);
2514
2515 out:
2516         while (i)
2517                 os_free(paths[--i]);
2518         os_free(paths);
2519         return reply;
2520 }
2521
2522
2523 /**
2524  * wpas_dbus_getter_networks - Get array of networks objects
2525  * @message: Pointer to incoming dbus message
2526  * @wpa_s: wpa_supplicant structure for a network interface
2527  * Returns: a dbus message containing an array of all configured
2528  * networks dbus object paths.
2529  *
2530  * Getter for "Networks" property.
2531  */
2532 DBusMessage * wpas_dbus_getter_networks(DBusMessage *message,
2533                                         struct wpa_supplicant *wpa_s)
2534 {
2535         DBusMessage *reply = NULL;
2536         struct wpa_ssid *ssid;
2537         char **paths;
2538         unsigned int i = 0, num = 0;
2539
2540         if (wpa_s->conf == NULL) {
2541                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_networks[dbus]: "
2542                            "An error occurred getting networks list.");
2543                 return wpas_dbus_error_unknown_error(message, NULL);
2544         }
2545
2546         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2547                 num++;
2548
2549         paths = os_zalloc(num * sizeof(char *));
2550         if (!paths) {
2551                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
2552                                               NULL);
2553         }
2554
2555         /* Loop through configured networks and append object path of each */
2556         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
2557                 paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
2558                 if (paths[i] == NULL) {
2559                         reply = dbus_message_new_error(message,
2560                                                        DBUS_ERROR_NO_MEMORY,
2561                                                        NULL);
2562                         goto out;
2563                 }
2564
2565                 /* Construct the object path for this network. */
2566                 os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
2567                             "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
2568                             wpa_s->dbus_new_path, ssid->id);
2569         }
2570
2571         reply = wpas_dbus_simple_array_property_getter(message,
2572                                                        DBUS_TYPE_OBJECT_PATH,
2573                                                        paths, num);
2574
2575 out:
2576         while (i)
2577                 os_free(paths[--i]);
2578         os_free(paths);
2579         return reply;
2580 }
2581
2582
2583 /**
2584  * wpas_dbus_getter_blobs - Get all blobs defined for this interface
2585  * @message: Pointer to incoming dbus message
2586  * @wpa_s: wpa_supplicant structure for a network interface
2587  * Returns: a dbus message containing a dictionary of pairs (blob_name, blob)
2588  *
2589  * Getter for "Blobs" property.
2590  */
2591 DBusMessage * wpas_dbus_getter_blobs(DBusMessage *message,
2592                                      struct wpa_supplicant *wpa_s)
2593 {
2594         DBusMessage *reply = NULL;
2595         DBusMessageIter iter, variant_iter, dict_iter, entry_iter, array_iter;
2596         struct wpa_config_blob *blob;
2597
2598         if (message == NULL)
2599                 reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
2600         else
2601                 reply = dbus_message_new_method_return(message);
2602         if (!reply)
2603                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
2604                                               NULL);
2605
2606         dbus_message_iter_init_append(reply, &iter);
2607
2608         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
2609                                               "a{say}", &variant_iter) ||
2610             !dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
2611                                               "{say}", &dict_iter)) {
2612                 dbus_message_unref(reply);
2613                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
2614                                               NULL);
2615         }
2616
2617         blob = wpa_s->conf->blobs;
2618         while (blob) {
2619                 if (!dbus_message_iter_open_container(&dict_iter,
2620                                                       DBUS_TYPE_DICT_ENTRY,
2621                                                       NULL, &entry_iter) ||
2622                     !dbus_message_iter_append_basic(&entry_iter,
2623                                                     DBUS_TYPE_STRING,
2624                                                     &(blob->name)) ||
2625                     !dbus_message_iter_open_container(&entry_iter,
2626                                                       DBUS_TYPE_ARRAY,
2627                                                       DBUS_TYPE_BYTE_AS_STRING,
2628                                                       &array_iter) ||
2629                     !dbus_message_iter_append_fixed_array(&array_iter,
2630                                                           DBUS_TYPE_BYTE,
2631                                                           &(blob->data),
2632                                                           blob->len) ||
2633                     !dbus_message_iter_close_container(&entry_iter,
2634                                                        &array_iter) ||
2635                     !dbus_message_iter_close_container(&dict_iter,
2636                                                        &entry_iter)) {
2637                         dbus_message_unref(reply);
2638                         return dbus_message_new_error(message,
2639                                                       DBUS_ERROR_NO_MEMORY,
2640                                                       NULL);
2641                 }
2642
2643                 blob = blob->next;
2644         }
2645
2646         if (!dbus_message_iter_close_container(&variant_iter, &dict_iter) ||
2647             !dbus_message_iter_close_container(&iter, &variant_iter)) {
2648                 dbus_message_unref(reply);
2649                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
2650                                               NULL);
2651         }
2652
2653         return reply;
2654 }
2655
2656
2657 /**
2658  * wpas_dbus_getter_bss_bssid - Return the BSSID of a BSS
2659  * @message: Pointer to incoming dbus message
2660  * @bss: a pair of interface describing structure and bss's id
2661  * Returns: a dbus message containing the bssid for the requested bss
2662  *
2663  * Getter for "BSSID" property.
2664  */
2665 DBusMessage * wpas_dbus_getter_bss_bssid(DBusMessage *message,
2666                                          struct bss_handler_args *bss)
2667 {
2668         struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
2669
2670         if (!res) {
2671                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_bssid[dbus]: no "
2672                            "bss with id %d found", bss->id);
2673                 return NULL;
2674         }
2675
2676         return wpas_dbus_simple_array_property_getter(message, DBUS_TYPE_BYTE,
2677                                                       res->bssid, ETH_ALEN);
2678 }
2679
2680
2681 /**
2682  * wpas_dbus_getter_bss_ssid - Return the SSID of a BSS
2683  * @message: Pointer to incoming dbus message
2684  * @bss: a pair of interface describing structure and bss's id
2685  * Returns: a dbus message containing the ssid for the requested bss
2686  *
2687  * Getter for "SSID" property.
2688  */
2689 DBusMessage * wpas_dbus_getter_bss_ssid(DBusMessage *message,
2690                                               struct bss_handler_args *bss)
2691 {
2692         struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
2693
2694         if (!res) {
2695                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_ssid[dbus]: no "
2696                            "bss with id %d found", bss->id);
2697                 return NULL;
2698         }
2699
2700         return wpas_dbus_simple_array_property_getter(message, DBUS_TYPE_BYTE,
2701                                                       res->ssid,
2702                                                       res->ssid_len);
2703 }
2704
2705
2706 /**
2707  * wpas_dbus_getter_bss_privacy - Return the privacy flag of a BSS
2708  * @message: Pointer to incoming dbus message
2709  * @bss: a pair of interface describing structure and bss's id
2710  * Returns: a dbus message containing the privacy flag value of requested bss
2711  *
2712  * Getter for "Privacy" property.
2713  */
2714 DBusMessage * wpas_dbus_getter_bss_privacy(DBusMessage *message,
2715                                            struct bss_handler_args *bss)
2716 {
2717         struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
2718         dbus_bool_t privacy;
2719
2720         if (!res) {
2721                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_privacy[dbus]: no "
2722                            "bss with id %d found", bss->id);
2723                 return NULL;
2724         }
2725
2726         privacy = (res->caps & IEEE80211_CAP_PRIVACY) ? TRUE : FALSE;
2727         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
2728                                                 &privacy);
2729 }
2730
2731
2732 /**
2733  * wpas_dbus_getter_bss_mode - Return the mode of a BSS
2734  * @message: Pointer to incoming dbus message
2735  * @bss: a pair of interface describing structure and bss's id
2736  * Returns: a dbus message containing the mode of requested bss
2737  *
2738  * Getter for "Mode" property.
2739  */
2740 DBusMessage * wpas_dbus_getter_bss_mode(DBusMessage *message,
2741                                         struct bss_handler_args *bss)
2742 {
2743         struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
2744         const char *mode;
2745
2746         if (!res) {
2747                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_mode[dbus]: no "
2748                            "bss with id %d found", bss->id);
2749                 return NULL;
2750         }
2751
2752         if (res->caps & IEEE80211_CAP_IBSS)
2753                 mode = "ad-hoc";
2754         else
2755                 mode = "infrastructure";
2756
2757         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
2758                                                 &mode);
2759 }
2760
2761
2762 /**
2763  * wpas_dbus_getter_bss_level - Return the signal strength of a BSS
2764  * @message: Pointer to incoming dbus message
2765  * @bss: a pair of interface describing structure and bss's id
2766  * Returns: a dbus message containing the signal strength of requested bss
2767  *
2768  * Getter for "Level" property.
2769  */
2770 DBusMessage * wpas_dbus_getter_bss_signal(DBusMessage *message,
2771                                           struct bss_handler_args *bss)
2772 {
2773         struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
2774
2775         if (!res) {
2776                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_signal[dbus]: no "
2777                            "bss with id %d found", bss->id);
2778                 return NULL;
2779         }
2780
2781         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_INT16,
2782                                                 &res->level);
2783 }
2784
2785
2786 /**
2787  * wpas_dbus_getter_bss_frequency - Return the frequency of a BSS
2788  * @message: Pointer to incoming dbus message
2789  * @bss: a pair of interface describing structure and bss's id
2790  * Returns: a dbus message containing the frequency of requested bss
2791  *
2792  * Getter for "Frequency" property.
2793  */
2794 DBusMessage * wpas_dbus_getter_bss_frequency(DBusMessage *message,
2795                                              struct bss_handler_args *bss)
2796 {
2797         struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
2798
2799         if (!res) {
2800                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_frequency[dbus]: "
2801                            "no bss with id %d found", bss->id);
2802                 return NULL;
2803         }
2804
2805         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_UINT16,
2806                                                 &res->freq);
2807 }
2808
2809
2810 static int cmp_u8s_desc(const void *a, const void *b)
2811 {
2812         return (*(u8 *) b - *(u8 *) a);
2813 }
2814
2815
2816 /**
2817  * wpas_dbus_getter_bss_rates - Return available bit rates of a BSS
2818  * @message: Pointer to incoming dbus message
2819  * @bss: a pair of interface describing structure and bss's id
2820  * Returns: a dbus message containing sorted array of bit rates
2821  *
2822  * Getter for "Rates" property.
2823  */
2824 DBusMessage * wpas_dbus_getter_bss_rates(DBusMessage *message,
2825                                             struct bss_handler_args *bss)
2826 {
2827         DBusMessage *reply;
2828         struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
2829         u8 *ie_rates = NULL;
2830         u32 *real_rates;
2831         int rates_num, i;
2832
2833         if (!res) {
2834                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_rates[dbus]: "
2835                            "no bss with id %d found", bss->id);
2836                 return NULL;
2837         }
2838
2839         rates_num = wpa_bss_get_bit_rates(res, &ie_rates);
2840         if (rates_num < 0)
2841                 return NULL;
2842
2843         qsort(ie_rates, rates_num, 1, cmp_u8s_desc);
2844
2845         real_rates = os_malloc(sizeof(u32) * rates_num);
2846         if (!real_rates) {
2847                 os_free(ie_rates);
2848                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
2849                                               NULL);
2850         }
2851
2852         for (i = 0; i < rates_num; i++)
2853                 real_rates[i] = ie_rates[i] * 500000;
2854
2855         reply = wpas_dbus_simple_array_property_getter(message,
2856                                                        DBUS_TYPE_UINT32,
2857                                                        real_rates, rates_num);
2858
2859         os_free(ie_rates);
2860         os_free(real_rates);
2861         return reply;
2862 }
2863
2864
2865 static DBusMessage * wpas_dbus_get_bss_security_prop(
2866         DBusMessage *message, struct wpa_ie_data *ie_data)
2867 {
2868         DBusMessage *reply;
2869         DBusMessageIter iter, iter_dict, variant_iter;
2870         const char *group;
2871         const char *pairwise[2]; /* max 2 pairwise ciphers is supported */
2872         const char *key_mgmt[7]; /* max 7 key managements may be supported */
2873         int n;
2874
2875         if (message == NULL)
2876                 reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
2877         else
2878                 reply = dbus_message_new_method_return(message);
2879         if (!reply)
2880                 goto nomem;
2881
2882         dbus_message_iter_init_append(reply, &iter);
2883         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
2884                                               "a{sv}", &variant_iter))
2885                 goto nomem;
2886
2887         if (!wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
2888                 goto nomem;
2889
2890         /* KeyMgmt */
2891         n = 0;
2892         if (ie_data->key_mgmt & WPA_KEY_MGMT_PSK)
2893                 key_mgmt[n++] = "wpa-psk";
2894         if (ie_data->key_mgmt & WPA_KEY_MGMT_FT_PSK)
2895                 key_mgmt[n++] = "wpa-ft-psk";
2896         if (ie_data->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
2897                 key_mgmt[n++] = "wpa-psk-sha256";
2898         if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X)
2899                 key_mgmt[n++] = "wpa-eap";
2900         if (ie_data->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
2901                 key_mgmt[n++] = "wpa-ft-eap";
2902         if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
2903                 key_mgmt[n++] = "wpa-eap-sha256";
2904         if (ie_data->key_mgmt & WPA_KEY_MGMT_NONE)
2905                 key_mgmt[n++] = "wpa-none";
2906
2907         if (!wpa_dbus_dict_append_string_array(&iter_dict, "KeyMgmt",
2908                                                key_mgmt, n))
2909                 goto nomem;
2910
2911         /* Group */
2912         switch (ie_data->group_cipher) {
2913         case WPA_CIPHER_WEP40:
2914                 group = "wep40";
2915                 break;
2916         case WPA_CIPHER_TKIP:
2917                 group = "tkip";
2918                 break;
2919         case WPA_CIPHER_CCMP:
2920                 group = "ccmp";
2921                 break;
2922         case WPA_CIPHER_WEP104:
2923                 group = "wep104";
2924                 break;
2925         default:
2926                 group = "";
2927                 break;
2928         }
2929
2930         if (!wpa_dbus_dict_append_string(&iter_dict, "Group", group))
2931                 goto nomem;
2932
2933         /* Pairwise */
2934         n = 0;
2935         if (ie_data->pairwise_cipher & WPA_CIPHER_TKIP)
2936                 pairwise[n++] = "tkip";
2937         if (ie_data->pairwise_cipher & WPA_CIPHER_CCMP)
2938                 pairwise[n++] = "ccmp";
2939
2940         if (!wpa_dbus_dict_append_string_array(&iter_dict, "Pairwise",
2941                                                pairwise, n))
2942                 goto nomem;
2943
2944         /* Management group (RSN only) */
2945         if (ie_data->proto == WPA_PROTO_RSN) {
2946                 switch (ie_data->mgmt_group_cipher) {
2947 #ifdef CONFIG_IEEE80211W
2948                 case WPA_CIPHER_AES_128_CMAC:
2949                         group = "aes128cmac";
2950                         break;
2951 #endif /* CONFIG_IEEE80211W */
2952                 default:
2953                         group = "";
2954                         break;
2955                 }
2956
2957                 if (!wpa_dbus_dict_append_string(&iter_dict, "MgmtGroup",
2958                                                  group))
2959                         goto nomem;
2960         }
2961
2962         if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict))
2963                 goto nomem;
2964         if (!dbus_message_iter_close_container(&iter, &variant_iter))
2965                 goto nomem;
2966
2967         return reply;
2968
2969 nomem:
2970         if (reply)
2971                 dbus_message_unref(reply);
2972
2973         return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY, NULL);
2974 }
2975
2976
2977 /**
2978  * wpas_dbus_getter_bss_wpa - Return the WPA options of a BSS
2979  * @message: Pointer to incoming dbus message
2980  * @bss: a pair of interface describing structure and bss's id
2981  * Returns: a dbus message containing the WPA options of requested bss
2982  *
2983  * Getter for "WPA" property.
2984  */
2985 DBusMessage * wpas_dbus_getter_bss_wpa(DBusMessage *message,
2986                                        struct bss_handler_args *bss)
2987 {
2988         struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
2989         struct wpa_ie_data wpa_data;
2990         const u8 *ie;
2991
2992         if (!res) {
2993                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_wpa[dbus]: no "
2994                            "bss with id %d found", bss->id);
2995                 return NULL;
2996         }
2997
2998         os_memset(&wpa_data, 0, sizeof(wpa_data));
2999         ie = wpa_bss_get_vendor_ie(res, WPA_IE_VENDOR_TYPE);
3000         if (ie) {
3001                 if (wpa_parse_wpa_ie(ie, 2 + ie[1], &wpa_data) < 0)
3002                         return wpas_dbus_error_unknown_error(message,
3003                                                              "invalid WPA IE");
3004         }
3005
3006         return wpas_dbus_get_bss_security_prop(message, &wpa_data);
3007 }
3008
3009
3010 /**
3011  * wpas_dbus_getter_bss_rsn - Return the RSN options of a BSS
3012  * @message: Pointer to incoming dbus message
3013  * @bss: a pair of interface describing structure and bss's id
3014  * Returns: a dbus message containing the RSN options of requested bss
3015  *
3016  * Getter for "RSN" property.
3017  */
3018 DBusMessage * wpas_dbus_getter_bss_rsn(DBusMessage *message,
3019                                        struct bss_handler_args *bss)
3020 {
3021         struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
3022         struct wpa_ie_data wpa_data;
3023         const u8 *ie;
3024
3025         if (!res) {
3026                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_rsn[dbus]: no "
3027                            "bss with id %d found", bss->id);
3028                 return NULL;
3029         }
3030
3031         os_memset(&wpa_data, 0, sizeof(wpa_data));
3032         ie = wpa_bss_get_ie(res, WLAN_EID_RSN);
3033         if (ie) {
3034                 if (wpa_parse_wpa_ie(ie, 2 + ie[1], &wpa_data) < 0)
3035                         return wpas_dbus_error_unknown_error(message,
3036                                                              "invalid RSN IE");
3037         }
3038
3039         return wpas_dbus_get_bss_security_prop(message, &wpa_data);
3040 }
3041
3042
3043 /**
3044  * wpas_dbus_getter_bss_ies - Return all IEs of a BSS
3045  * @message: Pointer to incoming dbus message
3046  * @bss: a pair of interface describing structure and bss's id
3047  * Returns: a dbus message containing IEs byte array
3048  *
3049  * Getter for "IEs" property.
3050  */
3051 DBusMessage * wpas_dbus_getter_bss_ies(DBusMessage *message,
3052                                        struct bss_handler_args *bss)
3053 {
3054         struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
3055
3056         if (!res) {
3057                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_ies[dbus]: no "
3058                            "bss with id %d found", bss->id);
3059                 return NULL;
3060         }
3061
3062         return wpas_dbus_simple_array_property_getter(message, DBUS_TYPE_BYTE,
3063                                                       res + 1, res->ie_len);
3064 }
3065
3066
3067 /**
3068  * wpas_dbus_getter_enabled - Check whether network is enabled or disabled
3069  * @message: Pointer to incoming dbus message
3070  * @wpas_dbus_setter_enabled: wpa_supplicant structure for a network interface
3071  * and wpa_ssid structure for a configured network
3072  * Returns: DBus message with boolean indicating state of configured network
3073  * or DBus error on failure
3074  *
3075  * Getter for "enabled" property of a configured network.
3076  */
3077 DBusMessage * wpas_dbus_getter_enabled(DBusMessage *message,
3078                                        struct network_handler_args *net)
3079 {
3080         dbus_bool_t enabled = net->ssid->disabled ? FALSE : TRUE;
3081         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
3082                                                 &enabled);
3083 }
3084
3085
3086 /**
3087  * wpas_dbus_setter_enabled - Mark a configured network as enabled or disabled
3088  * @message: Pointer to incoming dbus message
3089  * @wpas_dbus_setter_enabled: wpa_supplicant structure for a network interface
3090  * and wpa_ssid structure for a configured network
3091  * Returns: NULL indicating success or DBus error on failure
3092  *
3093  * Setter for "Enabled" property of a configured network.
3094  */
3095 DBusMessage * wpas_dbus_setter_enabled(DBusMessage *message,
3096                                        struct network_handler_args *net)
3097 {
3098         DBusMessage *reply = NULL;
3099
3100         struct wpa_supplicant *wpa_s;
3101         struct wpa_ssid *ssid;
3102
3103         dbus_bool_t enable;
3104
3105         reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_BOOLEAN,
3106                                                  &enable);
3107
3108         if (reply)
3109                 return reply;
3110
3111         wpa_s = net->wpa_s;
3112         ssid = net->ssid;
3113
3114         if (enable)
3115                 wpa_supplicant_enable_network(wpa_s, ssid);
3116         else
3117                 wpa_supplicant_disable_network(wpa_s, ssid);
3118
3119         return NULL;
3120 }
3121
3122
3123 /**
3124  * wpas_dbus_getter_network_properties - Get options for a configured network
3125  * @message: Pointer to incoming dbus message
3126  * @net: wpa_supplicant structure for a network interface and
3127  * wpa_ssid structure for a configured network
3128  * Returns: DBus message with network properties or DBus error on failure
3129  *
3130  * Getter for "Properties" property of a configured network.
3131  */
3132 DBusMessage * wpas_dbus_getter_network_properties(
3133         DBusMessage *message, struct network_handler_args *net)
3134 {
3135         DBusMessage *reply = NULL;
3136         DBusMessageIter iter, variant_iter, dict_iter;
3137         char **iterator;
3138         char **props = wpa_config_get_all(net->ssid, 1);
3139         if (!props)
3140                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
3141                                               NULL);
3142
3143         if (message == NULL)
3144                 reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
3145         else
3146                 reply = dbus_message_new_method_return(message);
3147         if (!reply) {
3148                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
3149                                                NULL);
3150                 goto out;
3151         }
3152
3153         dbus_message_iter_init_append(reply, &iter);
3154
3155         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
3156                         "a{sv}", &variant_iter) ||
3157             !wpa_dbus_dict_open_write(&variant_iter, &dict_iter)) {
3158                 dbus_message_unref(reply);
3159                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
3160                                                NULL);
3161                 goto out;
3162         }
3163
3164         iterator = props;
3165         while (*iterator) {
3166                 if (!wpa_dbus_dict_append_string(&dict_iter, *iterator,
3167                                                  *(iterator + 1))) {
3168                         dbus_message_unref(reply);
3169                         reply = dbus_message_new_error(message,
3170                                                        DBUS_ERROR_NO_MEMORY,
3171                                                        NULL);
3172                         goto out;
3173                 }
3174                 iterator += 2;
3175         }
3176
3177
3178         if (!wpa_dbus_dict_close_write(&variant_iter, &dict_iter) ||
3179             !dbus_message_iter_close_container(&iter, &variant_iter)) {
3180                 dbus_message_unref(reply);
3181                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
3182                                                NULL);
3183                 goto out;
3184         }
3185
3186 out:
3187         iterator = props;
3188         while (*iterator) {
3189                 os_free(*iterator);
3190                 iterator++;
3191         }
3192         os_free(props);
3193         return reply;
3194 }
3195
3196
3197 /**
3198  * wpas_dbus_setter_network_properties - Set options for a configured network
3199  * @message: Pointer to incoming dbus message
3200  * @net: wpa_supplicant structure for a network interface and
3201  * wpa_ssid structure for a configured network
3202  * Returns: NULL indicating success or DBus error on failure
3203  *
3204  * Setter for "Properties" property of a configured network.
3205  */
3206 DBusMessage * wpas_dbus_setter_network_properties(
3207         DBusMessage *message, struct network_handler_args *net)
3208 {
3209         struct wpa_ssid *ssid = net->ssid;
3210
3211         DBusMessage *reply = NULL;
3212         DBusMessageIter iter, variant_iter;
3213
3214         dbus_message_iter_init(message, &iter);
3215
3216         dbus_message_iter_next(&iter);
3217         dbus_message_iter_next(&iter);
3218
3219         dbus_message_iter_recurse(&iter, &variant_iter);
3220
3221         reply = set_network_properties(message, net->wpa_s, ssid,
3222                                        &variant_iter);
3223         if (reply)
3224                 wpa_printf(MSG_DEBUG, "dbus control interface couldn't set "
3225                            "network properties");
3226
3227         return reply;
3228 }