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