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