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