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