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