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