PageRenderTime 65ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/external/bsd/wpa/dist/wpa_supplicant/dbus/dbus_new_handlers.c

https://github.com/rumpkernel/netbsd-userspace-src
C | 3759 lines | 3663 code | 34 blank | 62 comment | 55 complexity | 8aaa17d4596611659b77799c5b275908 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * WPA Supplicant / dbus-based control interface
  3. * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
  4. * Copyright (c) 2009-2010, Witold Sowa <witold.sowa@gmail.com>
  5. * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
  6. *
  7. * This software may be distributed under the terms of the BSD license.
  8. * See README for more details.
  9. */
  10. #include "includes.h"
  11. #include "common.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "eap_peer/eap_methods.h"
  14. #include "eapol_supp/eapol_supp_sm.h"
  15. #include "rsn_supp/wpa.h"
  16. #include "../config.h"
  17. #include "../wpa_supplicant_i.h"
  18. #include "../driver_i.h"
  19. #include "../notify.h"
  20. #include "../bss.h"
  21. #include "../scan.h"
  22. #include "../autoscan.h"
  23. #include "dbus_new_helpers.h"
  24. #include "dbus_new.h"
  25. #include "dbus_new_handlers.h"
  26. #include "dbus_dict_helpers.h"
  27. #include "dbus_common_i.h"
  28. extern int wpa_debug_level;
  29. extern int wpa_debug_show_keys;
  30. extern int wpa_debug_timestamp;
  31. static const char *debug_strings[] = {
  32. "excessive", "msgdump", "debug", "info", "warning", "error", NULL
  33. };
  34. /**
  35. * wpas_dbus_error_unknown_error - Return a new InvalidArgs error message
  36. * @message: Pointer to incoming dbus message this error refers to
  37. * @arg: Optional string appended to error message
  38. * Returns: a dbus error message
  39. *
  40. * Convenience function to create and return an UnknownError
  41. */
  42. DBusMessage * wpas_dbus_error_unknown_error(DBusMessage *message,
  43. const char *arg)
  44. {
  45. /*
  46. * This function can be called as a result of a failure
  47. * within internal getter calls, which will call this function
  48. * with a NULL message parameter. However, dbus_message_new_error
  49. * looks very unkindly (i.e, abort()) on a NULL message, so
  50. * in this case, we should not call it.
  51. */
  52. if (message == NULL) {
  53. wpa_printf(MSG_INFO, "dbus: wpas_dbus_error_unknown_error "
  54. "called with NULL message (arg=%s)",
  55. arg ? arg : "N/A");
  56. return NULL;
  57. }
  58. return dbus_message_new_error(message, WPAS_DBUS_ERROR_UNKNOWN_ERROR,
  59. arg);
  60. }
  61. /**
  62. * wpas_dbus_error_iface_unknown - Return a new invalid interface error message
  63. * @message: Pointer to incoming dbus message this error refers to
  64. * Returns: A dbus error message
  65. *
  66. * Convenience function to create and return an invalid interface error
  67. */
  68. static DBusMessage * wpas_dbus_error_iface_unknown(DBusMessage *message)
  69. {
  70. return dbus_message_new_error(message, WPAS_DBUS_ERROR_IFACE_UNKNOWN,
  71. "wpa_supplicant knows nothing about "
  72. "this interface.");
  73. }
  74. /**
  75. * wpas_dbus_error_network_unknown - Return a new NetworkUnknown error message
  76. * @message: Pointer to incoming dbus message this error refers to
  77. * Returns: a dbus error message
  78. *
  79. * Convenience function to create and return an invalid network error
  80. */
  81. static DBusMessage * wpas_dbus_error_network_unknown(DBusMessage *message)
  82. {
  83. return dbus_message_new_error(message, WPAS_DBUS_ERROR_NETWORK_UNKNOWN,
  84. "There is no such a network in this "
  85. "interface.");
  86. }
  87. /**
  88. * wpas_dbus_error_invalid_args - Return a new InvalidArgs error message
  89. * @message: Pointer to incoming dbus message this error refers to
  90. * Returns: a dbus error message
  91. *
  92. * Convenience function to create and return an invalid options error
  93. */
  94. DBusMessage * wpas_dbus_error_invalid_args(DBusMessage *message,
  95. const char *arg)
  96. {
  97. DBusMessage *reply;
  98. reply = dbus_message_new_error(message, WPAS_DBUS_ERROR_INVALID_ARGS,
  99. "Did not receive correct message "
  100. "arguments.");
  101. if (arg != NULL)
  102. dbus_message_append_args(reply, DBUS_TYPE_STRING, &arg,
  103. DBUS_TYPE_INVALID);
  104. return reply;
  105. }
  106. static const char *dont_quote[] = {
  107. "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
  108. "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
  109. "bssid", "scan_freq", "freq_list", NULL
  110. };
  111. static dbus_bool_t should_quote_opt(const char *key)
  112. {
  113. int i = 0;
  114. while (dont_quote[i] != NULL) {
  115. if (os_strcmp(key, dont_quote[i]) == 0)
  116. return FALSE;
  117. i++;
  118. }
  119. return TRUE;
  120. }
  121. /**
  122. * get_iface_by_dbus_path - Get a new network interface
  123. * @global: Pointer to global data from wpa_supplicant_init()
  124. * @path: Pointer to a dbus object path representing an interface
  125. * Returns: Pointer to the interface or %NULL if not found
  126. */
  127. static struct wpa_supplicant * get_iface_by_dbus_path(
  128. struct wpa_global *global, const char *path)
  129. {
  130. struct wpa_supplicant *wpa_s;
  131. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  132. if (os_strcmp(wpa_s->dbus_new_path, path) == 0)
  133. return wpa_s;
  134. }
  135. return NULL;
  136. }
  137. /**
  138. * set_network_properties - Set properties of a configured network
  139. * @wpa_s: wpa_supplicant structure for a network interface
  140. * @ssid: wpa_ssid structure for a configured network
  141. * @iter: DBus message iterator containing dictionary of network
  142. * properties to set.
  143. * @error: On failure, an error describing the failure
  144. * Returns: TRUE if the request succeeds, FALSE if it failed
  145. *
  146. * Sets network configuration with parameters given id DBus dictionary
  147. */
  148. dbus_bool_t set_network_properties(struct wpa_supplicant *wpa_s,
  149. struct wpa_ssid *ssid,
  150. DBusMessageIter *iter,
  151. DBusError *error)
  152. {
  153. struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
  154. DBusMessageIter iter_dict;
  155. char *value = NULL;
  156. if (!wpa_dbus_dict_open_read(iter, &iter_dict, error))
  157. return FALSE;
  158. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  159. size_t size = 50;
  160. int ret;
  161. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  162. goto error;
  163. value = NULL;
  164. if (entry.type == DBUS_TYPE_ARRAY &&
  165. entry.array_type == DBUS_TYPE_BYTE) {
  166. if (entry.array_len <= 0)
  167. goto error;
  168. size = entry.array_len * 2 + 1;
  169. value = os_zalloc(size);
  170. if (value == NULL)
  171. goto error;
  172. ret = wpa_snprintf_hex(value, size,
  173. (u8 *) entry.bytearray_value,
  174. entry.array_len);
  175. if (ret <= 0)
  176. goto error;
  177. } else if (entry.type == DBUS_TYPE_STRING) {
  178. if (should_quote_opt(entry.key)) {
  179. size = os_strlen(entry.str_value);
  180. if (size <= 0)
  181. goto error;
  182. size += 3;
  183. value = os_zalloc(size);
  184. if (value == NULL)
  185. goto error;
  186. ret = os_snprintf(value, size, "\"%s\"",
  187. entry.str_value);
  188. if (ret < 0 || (size_t) ret != (size - 1))
  189. goto error;
  190. } else {
  191. value = os_strdup(entry.str_value);
  192. if (value == NULL)
  193. goto error;
  194. }
  195. } else if (entry.type == DBUS_TYPE_UINT32) {
  196. value = os_zalloc(size);
  197. if (value == NULL)
  198. goto error;
  199. ret = os_snprintf(value, size, "%u",
  200. entry.uint32_value);
  201. if (ret <= 0)
  202. goto error;
  203. } else if (entry.type == DBUS_TYPE_INT32) {
  204. value = os_zalloc(size);
  205. if (value == NULL)
  206. goto error;
  207. ret = os_snprintf(value, size, "%d",
  208. entry.int32_value);
  209. if (ret <= 0)
  210. goto error;
  211. } else
  212. goto error;
  213. if (wpa_config_set(ssid, entry.key, value, 0) < 0)
  214. goto error;
  215. if ((os_strcmp(entry.key, "psk") == 0 &&
  216. value[0] == '"' && ssid->ssid_len) ||
  217. (os_strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
  218. wpa_config_update_psk(ssid);
  219. else if (os_strcmp(entry.key, "priority") == 0)
  220. wpa_config_update_prio_list(wpa_s->conf);
  221. os_free(value);
  222. wpa_dbus_dict_entry_clear(&entry);
  223. }
  224. return TRUE;
  225. error:
  226. os_free(value);
  227. wpa_dbus_dict_entry_clear(&entry);
  228. dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS,
  229. "invalid message format");
  230. return FALSE;
  231. }
  232. /**
  233. * wpas_dbus_simple_property_getter - Get basic type property
  234. * @iter: Message iter to use when appending arguments
  235. * @type: DBus type of property (must be basic type)
  236. * @val: pointer to place holding property value
  237. * @error: On failure an error describing the failure
  238. * Returns: TRUE if the request was successful, FALSE if it failed
  239. *
  240. * Generic getter for basic type properties. Type is required to be basic.
  241. */
  242. dbus_bool_t wpas_dbus_simple_property_getter(DBusMessageIter *iter,
  243. const int type,
  244. const void *val,
  245. DBusError *error)
  246. {
  247. DBusMessageIter variant_iter;
  248. if (!dbus_type_is_basic(type)) {
  249. dbus_set_error(error, DBUS_ERROR_FAILED,
  250. "%s: given type is not basic", __func__);
  251. return FALSE;
  252. }
  253. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  254. wpa_dbus_type_as_string(type),
  255. &variant_iter))
  256. goto error;
  257. if (!dbus_message_iter_append_basic(&variant_iter, type, val))
  258. goto error;
  259. if (!dbus_message_iter_close_container(iter, &variant_iter))
  260. goto error;
  261. return TRUE;
  262. error:
  263. dbus_set_error(error, DBUS_ERROR_FAILED,
  264. "%s: error constructing reply", __func__);
  265. return FALSE;
  266. }
  267. /**
  268. * wpas_dbus_simple_property_setter - Set basic type property
  269. * @message: Pointer to incoming dbus message
  270. * @type: DBus type of property (must be basic type)
  271. * @val: pointer to place where value being set will be stored
  272. * Returns: TRUE if the request was successful, FALSE if it failed
  273. *
  274. * Generic setter for basic type properties. Type is required to be basic.
  275. */
  276. dbus_bool_t wpas_dbus_simple_property_setter(DBusMessageIter *iter,
  277. DBusError *error,
  278. const int type, void *val)
  279. {
  280. DBusMessageIter variant_iter;
  281. if (!dbus_type_is_basic(type)) {
  282. dbus_set_error(error, DBUS_ERROR_FAILED,
  283. "%s: given type is not basic", __func__);
  284. return FALSE;
  285. }
  286. /* Look at the new value */
  287. dbus_message_iter_recurse(iter, &variant_iter);
  288. if (dbus_message_iter_get_arg_type(&variant_iter) != type) {
  289. dbus_set_error_const(error, DBUS_ERROR_FAILED,
  290. "wrong property type");
  291. return FALSE;
  292. }
  293. dbus_message_iter_get_basic(&variant_iter, val);
  294. return TRUE;
  295. }
  296. /**
  297. * wpas_dbus_simple_array_property_getter - Get array type property
  298. * @iter: Pointer to incoming dbus message iterator
  299. * @type: DBus type of property array elements (must be basic type)
  300. * @array: pointer to array of elements to put into response message
  301. * @array_len: length of above array
  302. * @error: a pointer to an error to fill on failure
  303. * Returns: TRUE if the request succeeded, FALSE if it failed
  304. *
  305. * Generic getter for array type properties. Array elements type is
  306. * required to be basic.
  307. */
  308. dbus_bool_t wpas_dbus_simple_array_property_getter(DBusMessageIter *iter,
  309. const int type,
  310. const void *array,
  311. size_t array_len,
  312. DBusError *error)
  313. {
  314. DBusMessageIter variant_iter, array_iter;
  315. char type_str[] = "a?"; /* ? will be replaced with subtype letter; */
  316. const char *sub_type_str;
  317. size_t element_size, i;
  318. if (!dbus_type_is_basic(type)) {
  319. dbus_set_error(error, DBUS_ERROR_FAILED,
  320. "%s: given type is not basic", __func__);
  321. return FALSE;
  322. }
  323. sub_type_str = wpa_dbus_type_as_string(type);
  324. type_str[1] = sub_type_str[0];
  325. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  326. type_str, &variant_iter)) {
  327. dbus_set_error(error, DBUS_ERROR_FAILED,
  328. "%s: failed to construct message 1", __func__);
  329. return FALSE;
  330. }
  331. if (!dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
  332. sub_type_str, &array_iter)) {
  333. dbus_set_error(error, DBUS_ERROR_FAILED,
  334. "%s: failed to construct message 2", __func__);
  335. return FALSE;
  336. }
  337. switch(type) {
  338. case DBUS_TYPE_BYTE:
  339. case DBUS_TYPE_BOOLEAN:
  340. element_size = 1;
  341. break;
  342. case DBUS_TYPE_INT16:
  343. case DBUS_TYPE_UINT16:
  344. element_size = sizeof(uint16_t);
  345. break;
  346. case DBUS_TYPE_INT32:
  347. case DBUS_TYPE_UINT32:
  348. element_size = sizeof(uint32_t);
  349. break;
  350. case DBUS_TYPE_INT64:
  351. case DBUS_TYPE_UINT64:
  352. element_size = sizeof(uint64_t);
  353. break;
  354. case DBUS_TYPE_DOUBLE:
  355. element_size = sizeof(double);
  356. break;
  357. case DBUS_TYPE_STRING:
  358. case DBUS_TYPE_OBJECT_PATH:
  359. element_size = sizeof(char *);
  360. break;
  361. default:
  362. dbus_set_error(error, DBUS_ERROR_FAILED,
  363. "%s: unknown element type %d", __func__, type);
  364. return FALSE;
  365. }
  366. for (i = 0; i < array_len; i++) {
  367. dbus_message_iter_append_basic(&array_iter, type,
  368. array + i * element_size);
  369. }
  370. if (!dbus_message_iter_close_container(&variant_iter, &array_iter)) {
  371. dbus_set_error(error, DBUS_ERROR_FAILED,
  372. "%s: failed to construct message 3", __func__);
  373. return FALSE;
  374. }
  375. if (!dbus_message_iter_close_container(iter, &variant_iter)) {
  376. dbus_set_error(error, DBUS_ERROR_FAILED,
  377. "%s: failed to construct message 4", __func__);
  378. return FALSE;
  379. }
  380. return TRUE;
  381. }
  382. /**
  383. * wpas_dbus_simple_array_array_property_getter - Get array array type property
  384. * @iter: Pointer to incoming dbus message iterator
  385. * @type: DBus type of property array elements (must be basic type)
  386. * @array: pointer to array of elements to put into response message
  387. * @array_len: length of above array
  388. * @error: a pointer to an error to fill on failure
  389. * Returns: TRUE if the request succeeded, FALSE if it failed
  390. *
  391. * Generic getter for array type properties. Array elements type is
  392. * required to be basic.
  393. */
  394. dbus_bool_t wpas_dbus_simple_array_array_property_getter(DBusMessageIter *iter,
  395. const int type,
  396. struct wpabuf **array,
  397. size_t array_len,
  398. DBusError *error)
  399. {
  400. DBusMessageIter variant_iter, array_iter;
  401. char type_str[] = "aa?";
  402. char inner_type_str[] = "a?";
  403. const char *sub_type_str;
  404. size_t i;
  405. if (!dbus_type_is_basic(type)) {
  406. dbus_set_error(error, DBUS_ERROR_FAILED,
  407. "%s: given type is not basic", __func__);
  408. return FALSE;
  409. }
  410. sub_type_str = wpa_dbus_type_as_string(type);
  411. type_str[2] = sub_type_str[0];
  412. inner_type_str[1] = sub_type_str[0];
  413. if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
  414. type_str, &variant_iter)) {
  415. dbus_set_error(error, DBUS_ERROR_FAILED,
  416. "%s: failed to construct message 1", __func__);
  417. return FALSE;
  418. }
  419. if (!dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
  420. inner_type_str, &array_iter)) {
  421. dbus_set_error(error, DBUS_ERROR_FAILED,
  422. "%s: failed to construct message 2", __func__);
  423. return FALSE;
  424. }
  425. for (i = 0; i < array_len; i++) {
  426. wpa_dbus_dict_bin_array_add_element(&array_iter,
  427. wpabuf_head(array[i]),
  428. wpabuf_len(array[i]));
  429. }
  430. if (!dbus_message_iter_close_container(&variant_iter, &array_iter)) {
  431. dbus_set_error(error, DBUS_ERROR_FAILED,
  432. "%s: failed to close message 2", __func__);
  433. return FALSE;
  434. }
  435. if (!dbus_message_iter_close_container(iter, &variant_iter)) {
  436. dbus_set_error(error, DBUS_ERROR_FAILED,
  437. "%s: failed to close message 1", __func__);
  438. return FALSE;
  439. }
  440. return TRUE;
  441. }
  442. /**
  443. * wpas_dbus_handler_create_interface - Request registration of a network iface
  444. * @message: Pointer to incoming dbus message
  445. * @global: %wpa_supplicant global data structure
  446. * Returns: The object path of the new interface object,
  447. * or a dbus error message with more information
  448. *
  449. * Handler function for "CreateInterface" method call. Handles requests
  450. * by dbus clients to register a network interface that wpa_supplicant
  451. * will manage.
  452. */
  453. DBusMessage * wpas_dbus_handler_create_interface(DBusMessage *message,
  454. struct wpa_global *global)
  455. {
  456. DBusMessageIter iter_dict;
  457. DBusMessage *reply = NULL;
  458. DBusMessageIter iter;
  459. struct wpa_dbus_dict_entry entry;
  460. char *driver = NULL;
  461. char *ifname = NULL;
  462. char *confname = NULL;
  463. char *bridge_ifname = NULL;
  464. dbus_message_iter_init(message, &iter);
  465. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  466. goto error;
  467. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  468. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  469. goto error;
  470. if (!os_strcmp(entry.key, "Driver") &&
  471. (entry.type == DBUS_TYPE_STRING)) {
  472. driver = os_strdup(entry.str_value);
  473. wpa_dbus_dict_entry_clear(&entry);
  474. if (driver == NULL)
  475. goto error;
  476. } else if (!os_strcmp(entry.key, "Ifname") &&
  477. (entry.type == DBUS_TYPE_STRING)) {
  478. ifname = os_strdup(entry.str_value);
  479. wpa_dbus_dict_entry_clear(&entry);
  480. if (ifname == NULL)
  481. goto error;
  482. } else if (!os_strcmp(entry.key, "ConfigFile") &&
  483. (entry.type == DBUS_TYPE_STRING)) {
  484. confname = os_strdup(entry.str_value);
  485. wpa_dbus_dict_entry_clear(&entry);
  486. if (confname == NULL)
  487. goto error;
  488. } else if (!os_strcmp(entry.key, "BridgeIfname") &&
  489. (entry.type == DBUS_TYPE_STRING)) {
  490. bridge_ifname = os_strdup(entry.str_value);
  491. wpa_dbus_dict_entry_clear(&entry);
  492. if (bridge_ifname == NULL)
  493. goto error;
  494. } else {
  495. wpa_dbus_dict_entry_clear(&entry);
  496. goto error;
  497. }
  498. }
  499. if (ifname == NULL)
  500. goto error; /* Required Ifname argument missing */
  501. /*
  502. * Try to get the wpa_supplicant record for this iface, return
  503. * an error if we already control it.
  504. */
  505. if (wpa_supplicant_get_iface(global, ifname) != NULL) {
  506. reply = dbus_message_new_error(message,
  507. WPAS_DBUS_ERROR_IFACE_EXISTS,
  508. "wpa_supplicant already "
  509. "controls this interface.");
  510. } else {
  511. struct wpa_supplicant *wpa_s;
  512. struct wpa_interface iface;
  513. os_memset(&iface, 0, sizeof(iface));
  514. iface.driver = driver;
  515. iface.ifname = ifname;
  516. iface.confname = confname;
  517. iface.bridge_ifname = bridge_ifname;
  518. /* Otherwise, have wpa_supplicant attach to it. */
  519. if ((wpa_s = wpa_supplicant_add_iface(global, &iface))) {
  520. const char *path = wpa_s->dbus_new_path;
  521. reply = dbus_message_new_method_return(message);
  522. dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
  523. &path, DBUS_TYPE_INVALID);
  524. } else {
  525. reply = wpas_dbus_error_unknown_error(
  526. message, "wpa_supplicant couldn't grab this "
  527. "interface.");
  528. }
  529. }
  530. out:
  531. os_free(driver);
  532. os_free(ifname);
  533. os_free(confname);
  534. os_free(bridge_ifname);
  535. return reply;
  536. error:
  537. reply = wpas_dbus_error_invalid_args(message, NULL);
  538. goto out;
  539. }
  540. /**
  541. * wpas_dbus_handler_remove_interface - Request deregistration of an interface
  542. * @message: Pointer to incoming dbus message
  543. * @global: wpa_supplicant global data structure
  544. * Returns: a dbus message containing a UINT32 indicating success (1) or
  545. * failure (0), or returns a dbus error message with more information
  546. *
  547. * Handler function for "removeInterface" method call. Handles requests
  548. * by dbus clients to deregister a network interface that wpa_supplicant
  549. * currently manages.
  550. */
  551. DBusMessage * wpas_dbus_handler_remove_interface(DBusMessage *message,
  552. struct wpa_global *global)
  553. {
  554. struct wpa_supplicant *wpa_s;
  555. char *path;
  556. DBusMessage *reply = NULL;
  557. dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
  558. DBUS_TYPE_INVALID);
  559. wpa_s = get_iface_by_dbus_path(global, path);
  560. if (wpa_s == NULL)
  561. reply = wpas_dbus_error_iface_unknown(message);
  562. else if (wpa_supplicant_remove_iface(global, wpa_s, 0)) {
  563. reply = wpas_dbus_error_unknown_error(
  564. message, "wpa_supplicant couldn't remove this "
  565. "interface.");
  566. }
  567. return reply;
  568. }
  569. /**
  570. * wpas_dbus_handler_get_interface - Get the object path for an interface name
  571. * @message: Pointer to incoming dbus message
  572. * @global: %wpa_supplicant global data structure
  573. * Returns: The object path of the interface object,
  574. * or a dbus error message with more information
  575. *
  576. * Handler function for "getInterface" method call.
  577. */
  578. DBusMessage * wpas_dbus_handler_get_interface(DBusMessage *message,
  579. struct wpa_global *global)
  580. {
  581. DBusMessage *reply = NULL;
  582. const char *ifname;
  583. const char *path;
  584. struct wpa_supplicant *wpa_s;
  585. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &ifname,
  586. DBUS_TYPE_INVALID);
  587. wpa_s = wpa_supplicant_get_iface(global, ifname);
  588. if (wpa_s == NULL)
  589. return wpas_dbus_error_iface_unknown(message);
  590. path = wpa_s->dbus_new_path;
  591. reply = dbus_message_new_method_return(message);
  592. if (reply == NULL)
  593. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  594. NULL);
  595. if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
  596. DBUS_TYPE_INVALID)) {
  597. dbus_message_unref(reply);
  598. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  599. NULL);
  600. }
  601. return reply;
  602. }
  603. /**
  604. * wpas_dbus_getter_debug_level - Get debug level
  605. * @iter: Pointer to incoming dbus message iter
  606. * @error: Location to store error on failure
  607. * @user_data: Function specific data
  608. * Returns: TRUE on success, FALSE on failure
  609. *
  610. * Getter for "DebugLevel" property.
  611. */
  612. dbus_bool_t wpas_dbus_getter_debug_level(DBusMessageIter *iter,
  613. DBusError *error,
  614. void *user_data)
  615. {
  616. const char *str;
  617. int idx = wpa_debug_level;
  618. if (idx < 0)
  619. idx = 0;
  620. if (idx > 5)
  621. idx = 5;
  622. str = debug_strings[idx];
  623. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  624. &str, error);
  625. }
  626. /**
  627. * wpas_dbus_getter_debug_timestamp - Get debug timestamp
  628. * @iter: Pointer to incoming dbus message iter
  629. * @error: Location to store error on failure
  630. * @user_data: Function specific data
  631. * Returns: TRUE on success, FALSE on failure
  632. *
  633. * Getter for "DebugTimestamp" property.
  634. */
  635. dbus_bool_t wpas_dbus_getter_debug_timestamp(DBusMessageIter *iter,
  636. DBusError *error,
  637. void *user_data)
  638. {
  639. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  640. &wpa_debug_timestamp, error);
  641. }
  642. /**
  643. * wpas_dbus_getter_debug_show_keys - Get debug show keys
  644. * @iter: Pointer to incoming dbus message iter
  645. * @error: Location to store error on failure
  646. * @user_data: Function specific data
  647. * Returns: TRUE on success, FALSE on failure
  648. *
  649. * Getter for "DebugShowKeys" property.
  650. */
  651. dbus_bool_t wpas_dbus_getter_debug_show_keys(DBusMessageIter *iter,
  652. DBusError *error,
  653. void *user_data)
  654. {
  655. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  656. &wpa_debug_show_keys, error);
  657. }
  658. /**
  659. * wpas_dbus_setter_debug_level - Set debug level
  660. * @iter: Pointer to incoming dbus message iter
  661. * @error: Location to store error on failure
  662. * @user_data: Function specific data
  663. * Returns: TRUE on success, FALSE on failure
  664. *
  665. * Setter for "DebugLevel" property.
  666. */
  667. dbus_bool_t wpas_dbus_setter_debug_level(DBusMessageIter *iter,
  668. DBusError *error, void *user_data)
  669. {
  670. struct wpa_global *global = user_data;
  671. const char *str = NULL;
  672. int i, val = -1;
  673. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_STRING,
  674. &str))
  675. return FALSE;
  676. for (i = 0; debug_strings[i]; i++)
  677. if (os_strcmp(debug_strings[i], str) == 0) {
  678. val = i;
  679. break;
  680. }
  681. if (val < 0 ||
  682. wpa_supplicant_set_debug_params(global, val, wpa_debug_timestamp,
  683. wpa_debug_show_keys)) {
  684. dbus_set_error_const(error, DBUS_ERROR_FAILED, "wrong debug "
  685. "level value");
  686. return FALSE;
  687. }
  688. return TRUE;
  689. }
  690. /**
  691. * wpas_dbus_setter_debug_timestamp - Set debug timestamp
  692. * @iter: Pointer to incoming dbus message iter
  693. * @error: Location to store error on failure
  694. * @user_data: Function specific data
  695. * Returns: TRUE on success, FALSE on failure
  696. *
  697. * Setter for "DebugTimestamp" property.
  698. */
  699. dbus_bool_t wpas_dbus_setter_debug_timestamp(DBusMessageIter *iter,
  700. DBusError *error,
  701. void *user_data)
  702. {
  703. struct wpa_global *global = user_data;
  704. dbus_bool_t val;
  705. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
  706. &val))
  707. return FALSE;
  708. wpa_supplicant_set_debug_params(global, wpa_debug_level, val ? 1 : 0,
  709. wpa_debug_show_keys);
  710. return TRUE;
  711. }
  712. /**
  713. * wpas_dbus_setter_debug_show_keys - Set debug show keys
  714. * @iter: Pointer to incoming dbus message iter
  715. * @error: Location to store error on failure
  716. * @user_data: Function specific data
  717. * Returns: TRUE on success, FALSE on failure
  718. *
  719. * Setter for "DebugShowKeys" property.
  720. */
  721. dbus_bool_t wpas_dbus_setter_debug_show_keys(DBusMessageIter *iter,
  722. DBusError *error,
  723. void *user_data)
  724. {
  725. struct wpa_global *global = user_data;
  726. dbus_bool_t val;
  727. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
  728. &val))
  729. return FALSE;
  730. wpa_supplicant_set_debug_params(global, wpa_debug_level,
  731. wpa_debug_timestamp,
  732. val ? 1 : 0);
  733. return TRUE;
  734. }
  735. /**
  736. * wpas_dbus_getter_interfaces - Request registered interfaces list
  737. * @iter: Pointer to incoming dbus message iter
  738. * @error: Location to store error on failure
  739. * @user_data: Function specific data
  740. * Returns: TRUE on success, FALSE on failure
  741. *
  742. * Getter for "Interfaces" property. Handles requests
  743. * by dbus clients to return list of registered interfaces objects
  744. * paths
  745. */
  746. dbus_bool_t wpas_dbus_getter_interfaces(DBusMessageIter *iter,
  747. DBusError *error,
  748. void *user_data)
  749. {
  750. struct wpa_global *global = user_data;
  751. struct wpa_supplicant *wpa_s;
  752. const char **paths;
  753. unsigned int i = 0, num = 0;
  754. dbus_bool_t success;
  755. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
  756. num++;
  757. paths = os_calloc(num, sizeof(char *));
  758. if (!paths) {
  759. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  760. return FALSE;
  761. }
  762. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
  763. paths[i++] = wpa_s->dbus_new_path;
  764. success = wpas_dbus_simple_array_property_getter(iter,
  765. DBUS_TYPE_OBJECT_PATH,
  766. paths, num, error);
  767. os_free(paths);
  768. return success;
  769. }
  770. /**
  771. * wpas_dbus_getter_eap_methods - Request supported EAP methods list
  772. * @iter: Pointer to incoming dbus message iter
  773. * @error: Location to store error on failure
  774. * @user_data: Function specific data
  775. * Returns: TRUE on success, FALSE on failure
  776. *
  777. * Getter for "EapMethods" property. Handles requests
  778. * by dbus clients to return list of strings with supported EAP methods
  779. */
  780. dbus_bool_t wpas_dbus_getter_eap_methods(DBusMessageIter *iter,
  781. DBusError *error, void *user_data)
  782. {
  783. char **eap_methods;
  784. size_t num_items = 0;
  785. dbus_bool_t success;
  786. eap_methods = eap_get_names_as_string_array(&num_items);
  787. if (!eap_methods) {
  788. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  789. return FALSE;
  790. }
  791. success = wpas_dbus_simple_array_property_getter(iter,
  792. DBUS_TYPE_STRING,
  793. eap_methods,
  794. num_items, error);
  795. while (num_items)
  796. os_free(eap_methods[--num_items]);
  797. os_free(eap_methods);
  798. return success;
  799. }
  800. /**
  801. * wpas_dbus_getter_global_capabilities - Request supported global capabilities
  802. * @iter: Pointer to incoming dbus message iter
  803. * @error: Location to store error on failure
  804. * @user_data: Function specific data
  805. * Returns: TRUE on success, FALSE on failure
  806. *
  807. * Getter for "Capabilities" property. Handles requests by dbus clients to
  808. * return a list of strings with supported capabilities like AP, RSN IBSS,
  809. * and P2P that are determined at compile time.
  810. */
  811. dbus_bool_t wpas_dbus_getter_global_capabilities(DBusMessageIter *iter,
  812. DBusError *error,
  813. void *user_data)
  814. {
  815. const char *capabilities[5] = { NULL, NULL, NULL, NULL, NULL };
  816. size_t num_items = 0;
  817. #ifdef CONFIG_AP
  818. capabilities[num_items++] = "ap";
  819. #endif /* CONFIG_AP */
  820. #ifdef CONFIG_IBSS_RSN
  821. capabilities[num_items++] = "ibss-rsn";
  822. #endif /* CONFIG_IBSS_RSN */
  823. #ifdef CONFIG_P2P
  824. capabilities[num_items++] = "p2p";
  825. #endif /* CONFIG_P2P */
  826. #ifdef CONFIG_INTERWORKING
  827. capabilities[num_items++] = "interworking";
  828. #endif /* CONFIG_INTERWORKING */
  829. return wpas_dbus_simple_array_property_getter(iter,
  830. DBUS_TYPE_STRING,
  831. capabilities,
  832. num_items, error);
  833. }
  834. static int wpas_dbus_get_scan_type(DBusMessage *message, DBusMessageIter *var,
  835. char **type, DBusMessage **reply)
  836. {
  837. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_STRING) {
  838. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  839. "Type must be a string");
  840. *reply = wpas_dbus_error_invalid_args(
  841. message, "Wrong Type value type. String required");
  842. return -1;
  843. }
  844. dbus_message_iter_get_basic(var, type);
  845. return 0;
  846. }
  847. static int wpas_dbus_get_scan_ssids(DBusMessage *message, DBusMessageIter *var,
  848. struct wpa_driver_scan_params *params,
  849. DBusMessage **reply)
  850. {
  851. struct wpa_driver_scan_ssid *ssids = params->ssids;
  852. size_t ssids_num = 0;
  853. u8 *ssid;
  854. DBusMessageIter array_iter, sub_array_iter;
  855. char *val;
  856. int len;
  857. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
  858. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ssids "
  859. "must be an array of arrays of bytes");
  860. *reply = wpas_dbus_error_invalid_args(
  861. message, "Wrong SSIDs value type. Array of arrays of "
  862. "bytes required");
  863. return -1;
  864. }
  865. dbus_message_iter_recurse(var, &array_iter);
  866. if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY ||
  867. dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_BYTE)
  868. {
  869. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ssids "
  870. "must be an array of arrays of bytes");
  871. *reply = wpas_dbus_error_invalid_args(
  872. message, "Wrong SSIDs value type. Array of arrays of "
  873. "bytes required");
  874. return -1;
  875. }
  876. while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_ARRAY)
  877. {
  878. if (ssids_num >= WPAS_MAX_SCAN_SSIDS) {
  879. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  880. "Too many ssids specified on scan dbus "
  881. "call");
  882. *reply = wpas_dbus_error_invalid_args(
  883. message, "Too many ssids specified. Specify "
  884. "at most four");
  885. return -1;
  886. }
  887. dbus_message_iter_recurse(&array_iter, &sub_array_iter);
  888. dbus_message_iter_get_fixed_array(&sub_array_iter, &val, &len);
  889. if (len > MAX_SSID_LEN) {
  890. wpa_printf(MSG_DEBUG,
  891. "wpas_dbus_handler_scan[dbus]: "
  892. "SSID too long (len=%d max_len=%d)",
  893. len, MAX_SSID_LEN);
  894. *reply = wpas_dbus_error_invalid_args(
  895. message, "Invalid SSID: too long");
  896. return -1;
  897. }
  898. if (len != 0) {
  899. ssid = os_malloc(len);
  900. if (ssid == NULL) {
  901. wpa_printf(MSG_DEBUG,
  902. "wpas_dbus_handler_scan[dbus]: "
  903. "out of memory. Cannot allocate "
  904. "memory for SSID");
  905. *reply = dbus_message_new_error(
  906. message, DBUS_ERROR_NO_MEMORY, NULL);
  907. return -1;
  908. }
  909. os_memcpy(ssid, val, len);
  910. } else {
  911. /* Allow zero-length SSIDs */
  912. ssid = NULL;
  913. }
  914. ssids[ssids_num].ssid = ssid;
  915. ssids[ssids_num].ssid_len = len;
  916. dbus_message_iter_next(&array_iter);
  917. ssids_num++;
  918. }
  919. params->num_ssids = ssids_num;
  920. return 0;
  921. }
  922. static int wpas_dbus_get_scan_ies(DBusMessage *message, DBusMessageIter *var,
  923. struct wpa_driver_scan_params *params,
  924. DBusMessage **reply)
  925. {
  926. u8 *ies = NULL, *nies;
  927. int ies_len = 0;
  928. DBusMessageIter array_iter, sub_array_iter;
  929. char *val;
  930. int len;
  931. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
  932. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ies must "
  933. "be an array of arrays of bytes");
  934. *reply = wpas_dbus_error_invalid_args(
  935. message, "Wrong IEs value type. Array of arrays of "
  936. "bytes required");
  937. return -1;
  938. }
  939. dbus_message_iter_recurse(var, &array_iter);
  940. if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY ||
  941. dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_BYTE)
  942. {
  943. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ies must "
  944. "be an array of arrays of bytes");
  945. *reply = wpas_dbus_error_invalid_args(
  946. message, "Wrong IEs value type. Array required");
  947. return -1;
  948. }
  949. while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_ARRAY)
  950. {
  951. dbus_message_iter_recurse(&array_iter, &sub_array_iter);
  952. dbus_message_iter_get_fixed_array(&sub_array_iter, &val, &len);
  953. if (len == 0) {
  954. dbus_message_iter_next(&array_iter);
  955. continue;
  956. }
  957. nies = os_realloc(ies, ies_len + len);
  958. if (nies == NULL) {
  959. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  960. "out of memory. Cannot allocate memory for "
  961. "IE");
  962. os_free(ies);
  963. *reply = dbus_message_new_error(
  964. message, DBUS_ERROR_NO_MEMORY, NULL);
  965. return -1;
  966. }
  967. ies = nies;
  968. os_memcpy(ies + ies_len, val, len);
  969. ies_len += len;
  970. dbus_message_iter_next(&array_iter);
  971. }
  972. params->extra_ies = ies;
  973. params->extra_ies_len = ies_len;
  974. return 0;
  975. }
  976. static int wpas_dbus_get_scan_channels(DBusMessage *message,
  977. DBusMessageIter *var,
  978. struct wpa_driver_scan_params *params,
  979. DBusMessage **reply)
  980. {
  981. DBusMessageIter array_iter, sub_array_iter;
  982. int *freqs = NULL, *nfreqs;
  983. int freqs_num = 0;
  984. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
  985. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  986. "Channels must be an array of structs");
  987. *reply = wpas_dbus_error_invalid_args(
  988. message, "Wrong Channels value type. Array of structs "
  989. "required");
  990. return -1;
  991. }
  992. dbus_message_iter_recurse(var, &array_iter);
  993. if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_STRUCT) {
  994. wpa_printf(MSG_DEBUG,
  995. "wpas_dbus_handler_scan[dbus]: Channels must be an "
  996. "array of structs");
  997. *reply = wpas_dbus_error_invalid_args(
  998. message, "Wrong Channels value type. Array of structs "
  999. "required");
  1000. return -1;
  1001. }
  1002. while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_STRUCT)
  1003. {
  1004. int freq, width;
  1005. dbus_message_iter_recurse(&array_iter, &sub_array_iter);
  1006. if (dbus_message_iter_get_arg_type(&sub_array_iter) !=
  1007. DBUS_TYPE_UINT32) {
  1008. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1009. "Channel must by specified by struct of "
  1010. "two UINT32s %c",
  1011. dbus_message_iter_get_arg_type(
  1012. &sub_array_iter));
  1013. *reply = wpas_dbus_error_invalid_args(
  1014. message, "Wrong Channel struct. Two UINT32s "
  1015. "required");
  1016. os_free(freqs);
  1017. return -1;
  1018. }
  1019. dbus_message_iter_get_basic(&sub_array_iter, &freq);
  1020. if (!dbus_message_iter_next(&sub_array_iter) ||
  1021. dbus_message_iter_get_arg_type(&sub_array_iter) !=
  1022. DBUS_TYPE_UINT32) {
  1023. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1024. "Channel must by specified by struct of "
  1025. "two UINT32s");
  1026. *reply = wpas_dbus_error_invalid_args(
  1027. message,
  1028. "Wrong Channel struct. Two UINT32s required");
  1029. os_free(freqs);
  1030. return -1;
  1031. }
  1032. dbus_message_iter_get_basic(&sub_array_iter, &width);
  1033. #define FREQS_ALLOC_CHUNK 32
  1034. if (freqs_num % FREQS_ALLOC_CHUNK == 0) {
  1035. nfreqs = os_realloc_array(
  1036. freqs, freqs_num + FREQS_ALLOC_CHUNK,
  1037. sizeof(int));
  1038. if (nfreqs == NULL)
  1039. os_free(freqs);
  1040. freqs = nfreqs;
  1041. }
  1042. if (freqs == NULL) {
  1043. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1044. "out of memory. can't allocate memory for "
  1045. "freqs");
  1046. *reply = dbus_message_new_error(
  1047. message, DBUS_ERROR_NO_MEMORY, NULL);
  1048. return -1;
  1049. }
  1050. freqs[freqs_num] = freq;
  1051. freqs_num++;
  1052. dbus_message_iter_next(&array_iter);
  1053. }
  1054. nfreqs = os_realloc_array(freqs, freqs_num + 1, sizeof(int));
  1055. if (nfreqs == NULL)
  1056. os_free(freqs);
  1057. freqs = nfreqs;
  1058. if (freqs == NULL) {
  1059. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1060. "out of memory. Can't allocate memory for freqs");
  1061. *reply = dbus_message_new_error(
  1062. message, DBUS_ERROR_NO_MEMORY, NULL);
  1063. return -1;
  1064. }
  1065. freqs[freqs_num] = 0;
  1066. params->freqs = freqs;
  1067. return 0;
  1068. }
  1069. /**
  1070. * wpas_dbus_handler_scan - Request a wireless scan on an interface
  1071. * @message: Pointer to incoming dbus message
  1072. * @wpa_s: wpa_supplicant structure for a network interface
  1073. * Returns: NULL indicating success or DBus error message on failure
  1074. *
  1075. * Handler function for "Scan" method call of a network device. Requests
  1076. * that wpa_supplicant perform a wireless scan as soon as possible
  1077. * on a particular wireless interface.
  1078. */
  1079. DBusMessage * wpas_dbus_handler_scan(DBusMessage *message,
  1080. struct wpa_supplicant *wpa_s)
  1081. {
  1082. DBusMessage *reply = NULL;
  1083. DBusMessageIter iter, dict_iter, entry_iter, variant_iter;
  1084. char *key = NULL, *type = NULL;
  1085. struct wpa_driver_scan_params params;
  1086. size_t i;
  1087. os_memset(&params, 0, sizeof(params));
  1088. dbus_message_iter_init(message, &iter);
  1089. dbus_message_iter_recurse(&iter, &dict_iter);
  1090. while (dbus_message_iter_get_arg_type(&dict_iter) ==
  1091. DBUS_TYPE_DICT_ENTRY) {
  1092. dbus_message_iter_recurse(&dict_iter, &entry_iter);
  1093. dbus_message_iter_get_basic(&entry_iter, &key);
  1094. dbus_message_iter_next(&entry_iter);
  1095. dbus_message_iter_recurse(&entry_iter, &variant_iter);
  1096. if (os_strcmp(key, "Type") == 0) {
  1097. if (wpas_dbus_get_scan_type(message, &variant_iter,
  1098. &type, &reply) < 0)
  1099. goto out;
  1100. } else if (os_strcmp(key, "SSIDs") == 0) {
  1101. if (wpas_dbus_get_scan_ssids(message, &variant_iter,
  1102. &params, &reply) < 0)
  1103. goto out;
  1104. } else if (os_strcmp(key, "IEs") == 0) {
  1105. if (wpas_dbus_get_scan_ies(message, &variant_iter,
  1106. &params, &reply) < 0)
  1107. goto out;
  1108. } else if (os_strcmp(key, "Channels") == 0) {
  1109. if (wpas_dbus_get_scan_channels(message, &variant_iter,
  1110. &params, &reply) < 0)
  1111. goto out;
  1112. } else {
  1113. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1114. "Unknown argument %s", key);
  1115. reply = wpas_dbus_error_invalid_args(message, key);
  1116. goto out;
  1117. }
  1118. dbus_message_iter_next(&dict_iter);
  1119. }
  1120. if (!type) {
  1121. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1122. "Scan type not specified");
  1123. reply = wpas_dbus_error_invalid_args(message, key);
  1124. goto out;
  1125. }
  1126. if (!os_strcmp(type, "passive")) {
  1127. if (params.num_ssids || params.extra_ies_len) {
  1128. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1129. "SSIDs or IEs specified for passive scan.");
  1130. reply = wpas_dbus_error_invalid_args(
  1131. message, "You can specify only Channels in "
  1132. "passive scan");
  1133. goto out;
  1134. } else if (params.freqs && params.freqs[0]) {
  1135. wpa_supplicant_trigger_scan(wpa_s, &params);
  1136. } else {
  1137. wpa_s->scan_req = MANUAL_SCAN_REQ;
  1138. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1139. }
  1140. } else if (!os_strcmp(type, "active")) {
  1141. if (!params.num_ssids) {
  1142. /* Add wildcard ssid */
  1143. params.num_ssids++;
  1144. }
  1145. #ifdef CONFIG_AUTOSCAN
  1146. autoscan_deinit(wpa_s);
  1147. #endif /* CONFIG_AUTOSCAN */
  1148. wpa_supplicant_trigger_scan(wpa_s, &params);
  1149. } else {
  1150. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1151. "Unknown scan type: %s", type);
  1152. reply = wpas_dbus_error_invalid_args(message,
  1153. "Wrong scan type");
  1154. goto out;
  1155. }
  1156. out:
  1157. for (i = 0; i < WPAS_MAX_SCAN_SSIDS; i++)
  1158. os_free((u8 *) params.ssids[i].ssid);
  1159. os_free((u8 *) params.extra_ies);
  1160. os_free(params.freqs);
  1161. return reply;
  1162. }
  1163. /*
  1164. * wpas_dbus_handler_disconnect - Terminate the current connection
  1165. * @message: Pointer to incoming dbus message
  1166. * @wpa_s: wpa_supplicant structure for a network interface
  1167. * Returns: NotConnected DBus error message if already not connected
  1168. * or NULL otherwise.
  1169. *
  1170. * Handler function for "Disconnect" method call of network interface.
  1171. */
  1172. DBusMessage * wpas_dbus_handler_disconnect(DBusMessage *message,
  1173. struct wpa_supplicant *wpa_s)
  1174. {
  1175. if (wpa_s->current_ssid != NULL) {
  1176. wpa_s->disconnected = 1;
  1177. wpa_supplicant_deauthenticate(wpa_s,
  1178. WLAN_REASON_DEAUTH_LEAVING);
  1179. return NULL;
  1180. }
  1181. return dbus_message_new_error(message, WPAS_DBUS_ERROR_NOT_CONNECTED,
  1182. "This interface is not connected");
  1183. }
  1184. /**
  1185. * wpas_dbus_new_iface_add_network - Add a new configured network
  1186. * @message: Pointer to incoming dbus message
  1187. * @wpa_s: wpa_supplicant structure for a network interface
  1188. * Returns: A dbus message containing the object path of the new network
  1189. *
  1190. * Handler function for "AddNetwork" method call of a network interface.
  1191. */
  1192. DBusMessage * wpas_dbus_handler_add_network(DBusMessage *message,
  1193. struct wpa_supplicant *wpa_s)
  1194. {
  1195. DBusMessage *reply = NULL;
  1196. DBusMessageIter iter;
  1197. struct wpa_ssid *ssid = NULL;
  1198. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *path = path_buf;
  1199. DBusError error;
  1200. dbus_message_iter_init(message, &iter);
  1201. ssid = wpa_config_add_network(wpa_s->conf);
  1202. if (ssid == NULL) {
  1203. wpa_printf(MSG_ERROR, "wpas_dbus_handler_add_network[dbus]: "
  1204. "can't add new interface.");
  1205. reply = wpas_dbus_error_unknown_error(
  1206. message,
  1207. "wpa_supplicant could not add "
  1208. "a network on this interface.");
  1209. goto err;
  1210. }
  1211. wpas_notify_network_added(wpa_s, ssid);
  1212. ssid->disabled = 1;
  1213. wpa_config_set_network_defaults(ssid);
  1214. dbus_error_init(&error);
  1215. if (!set_network_properties(wpa_s, ssid, &iter, &error)) {
  1216. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_add_network[dbus]:"
  1217. "control interface couldn't set network "
  1218. "properties");
  1219. reply = wpas_dbus_reply_new_from_error(message, &error,
  1220. DBUS_ERROR_INVALID_ARGS,
  1221. "Failed to add network");
  1222. dbus_error_free(&error);
  1223. goto err;
  1224. }
  1225. /* Construct the object path for this network. */
  1226. os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  1227. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
  1228. wpa_s->dbus_new_path, ssid->id);
  1229. reply = dbus_message_new_method_return(message);
  1230. if (reply == NULL) {
  1231. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1232. NULL);
  1233. goto err;
  1234. }
  1235. if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
  1236. DBUS_TYPE_INVALID)) {
  1237. dbus_message_unref(reply);
  1238. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1239. NULL);
  1240. goto err;
  1241. }
  1242. return reply;
  1243. err:
  1244. if (ssid) {
  1245. wpas_notify_network_removed(wpa_s, ssid);
  1246. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1247. }
  1248. return reply;
  1249. }
  1250. /**
  1251. * wpas_dbus_handler_reassociate - Reassociate to current AP
  1252. * @message: Pointer to incoming dbus message
  1253. * @wpa_s: wpa_supplicant structure for a network interface
  1254. * Returns: NotConnected DBus error message if not connected
  1255. * or NULL otherwise.
  1256. *
  1257. * Handler function for "Reassociate" method call of network interface.
  1258. */
  1259. DBusMessage * wpas_dbus_handler_reassociate(DBusMessage *message,
  1260. struct wpa_supplicant *wpa_s)
  1261. {
  1262. if (wpa_s->current_ssid != NULL) {
  1263. wpas_request_connection(wpa_s);
  1264. return NULL;
  1265. }
  1266. return dbus_message_new_error(message, WPAS_DBUS_ERROR_NOT_CONNECTED,
  1267. "This interface is not connected");
  1268. }
  1269. /**
  1270. * wpas_dbus_handler_remove_network - Remove a configured network
  1271. * @message: Pointer to incoming dbus message
  1272. * @wpa_s: wpa_supplicant structure for a network interface
  1273. * Returns: NULL on success or dbus error on failure
  1274. *
  1275. * Handler function for "RemoveNetwork" method call of a network interface.
  1276. */
  1277. DBusMessage * wpas_dbus_handler_remove_network(DBusMessage *message,
  1278. struct wpa_supplicant *wpa_s)
  1279. {
  1280. DBusMessage *reply = NULL;
  1281. const char *op;
  1282. char *iface = NULL, *net_id = NULL;
  1283. int id;
  1284. struct wpa_ssid *ssid;
  1285. dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
  1286. DBUS_TYPE_INVALID);
  1287. /* Extract the network ID and ensure the network */
  1288. /* is actually a child of this interface */
  1289. iface = wpas_dbus_new_decompose_object_path(op, 0, &net_id, NULL);
  1290. if (iface == NULL || net_id == NULL ||
  1291. os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1292. reply = wpas_dbus_error_invalid_args(message, op);
  1293. goto out;
  1294. }
  1295. errno = 0;
  1296. id = strtoul(net_id, NULL, 10);
  1297. if (errno != 0) {
  1298. reply = wpas_dbus_error_invalid_args(message, op);
  1299. goto out;
  1300. }
  1301. ssid = wpa_config_get_network(wpa_s->conf, id);
  1302. if (ssid == NULL) {
  1303. reply = wpas_dbus_error_network_unknown(message);
  1304. goto out;
  1305. }
  1306. wpas_notify_network_removed(wpa_s, ssid);
  1307. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  1308. wpa_printf(MSG_ERROR,
  1309. "wpas_dbus_handler_remove_network[dbus]: "
  1310. "error occurred when removing network %d", id);
  1311. reply = wpas_dbus_error_unknown_error(
  1312. message, "error removing the specified network on "
  1313. "this interface.");
  1314. goto out;
  1315. }
  1316. if (ssid == wpa_s->current_ssid)
  1317. wpa_supplicant_deauthenticate(wpa_s,
  1318. WLAN_REASON_DEAUTH_LEAVING);
  1319. out:
  1320. os_free(iface);
  1321. os_free(net_id);
  1322. return reply;
  1323. }
  1324. static void remove_network(void *arg, struct wpa_ssid *ssid)
  1325. {
  1326. struct wpa_supplicant *wpa_s = arg;
  1327. wpas_notify_network_removed(wpa_s, ssid);
  1328. if (wpa_config_remove_network(wpa_s->conf, ssid->id) < 0) {
  1329. wpa_printf(MSG_ERROR,
  1330. "wpas_dbus_handler_remove_all_networks[dbus]: "
  1331. "error occurred when removing network %d",
  1332. ssid->id);
  1333. return;
  1334. }
  1335. if (ssid == wpa_s->current_ssid)
  1336. wpa_supplicant_deauthenticate(wpa_s,
  1337. WLAN_REASON_DEAUTH_LEAVING);
  1338. }
  1339. /**
  1340. * wpas_dbus_handler_remove_all_networks - Remove all configured networks
  1341. * @message: Pointer to incoming dbus message
  1342. * @wpa_s: wpa_supplicant structure for a network interface
  1343. * Returns: NULL on success or dbus error on failure
  1344. *
  1345. * Handler function for "RemoveAllNetworks" method call of a network interface.
  1346. */
  1347. DBusMessage * wpas_dbus_handler_remove_all_networks(
  1348. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1349. {
  1350. /* NB: could check for failure and return an error */
  1351. wpa_config_foreach_network(wpa_s->conf, remove_network, wpa_s);
  1352. return NULL;
  1353. }
  1354. /**
  1355. * wpas_dbus_handler_select_network - Attempt association with a network
  1356. * @message: Pointer to incoming dbus message
  1357. * @wpa_s: wpa_supplicant structure for a network interface
  1358. * Returns: NULL on success or dbus error on failure
  1359. *
  1360. * Handler function for "SelectNetwork" method call of network interface.
  1361. */
  1362. DBusMessage * wpas_dbus_handler_select_network(DBusMessage *message,
  1363. struct wpa_supplicant *wpa_s)
  1364. {
  1365. DBusMessage *reply = NULL;
  1366. const char *op;
  1367. char *iface = NULL, *net_id = NULL;
  1368. int id;
  1369. struct wpa_ssid *ssid;
  1370. dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
  1371. DBUS_TYPE_INVALID);
  1372. /* Extract the network ID and ensure the network */
  1373. /* is actually a child of this interface */
  1374. iface = wpas_dbus_new_decompose_object_path(op, 0, &net_id, NULL);
  1375. if (iface == NULL || net_id == NULL ||
  1376. os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1377. reply = wpas_dbus_error_invalid_args(message, op);
  1378. goto out;
  1379. }
  1380. errno = 0;
  1381. id = strtoul(net_id, NULL, 10);
  1382. if (errno != 0) {
  1383. reply = wpas_dbus_error_invalid_args(message, op);
  1384. goto out;
  1385. }
  1386. ssid = wpa_config_get_network(wpa_s->conf, id);
  1387. if (ssid == NULL) {
  1388. reply = wpas_dbus_error_network_unknown(message);
  1389. goto out;
  1390. }
  1391. /* Finally, associate with the network */
  1392. wpa_supplicant_select_network(wpa_s, ssid);
  1393. out:
  1394. os_free(iface);
  1395. os_free(net_id);
  1396. return reply;
  1397. }
  1398. /**
  1399. * wpas_dbus_handler_network_reply - Reply to a NetworkRequest signal
  1400. * @message: Pointer to incoming dbus message
  1401. * @wpa_s: wpa_supplicant structure for a network interface
  1402. * Returns: NULL on success or dbus error on failure
  1403. *
  1404. * Handler function for "NetworkReply" method call of network interface.
  1405. */
  1406. DBusMessage * wpas_dbus_handler_network_reply(DBusMessage *message,
  1407. struct wpa_supplicant *wpa_s)
  1408. {
  1409. #ifdef IEEE8021X_EAPOL
  1410. DBusMessage *reply = NULL;
  1411. const char *op, *field, *value;
  1412. char *iface = NULL, *net_id = NULL;
  1413. int id;
  1414. struct wpa_ssid *ssid;
  1415. if (!dbus_message_get_args(message, NULL,
  1416. DBUS_TYPE_OBJECT_PATH, &op,
  1417. DBUS_TYPE_STRING, &field,
  1418. DBUS_TYPE_STRING, &value,
  1419. DBUS_TYPE_INVALID))
  1420. return wpas_dbus_error_invalid_args(message, NULL);
  1421. /* Extract the network ID and ensure the network */
  1422. /* is actually a child of this interface */
  1423. iface = wpas_dbus_new_decompose_object_path(op, 0, &net_id, NULL);
  1424. if (iface == NULL || net_id == NULL ||
  1425. os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1426. reply = wpas_dbus_error_invalid_args(message, op);
  1427. goto out;
  1428. }
  1429. errno = 0;
  1430. id = strtoul(net_id, NULL, 10);
  1431. if (errno != 0) {
  1432. reply = wpas_dbus_error_invalid_args(message, net_id);
  1433. goto out;
  1434. }
  1435. ssid = wpa_config_get_network(wpa_s->conf, id);
  1436. if (ssid == NULL) {
  1437. reply = wpas_dbus_error_network_unknown(message);
  1438. goto out;
  1439. }
  1440. if (wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid,
  1441. field, value) < 0)
  1442. reply = wpas_dbus_error_invalid_args(message, field);
  1443. else {
  1444. /* Tell EAP to retry immediately */
  1445. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  1446. }
  1447. out:
  1448. os_free(iface);
  1449. os_free(net_id);
  1450. return reply;
  1451. #else /* IEEE8021X_EAPOL */
  1452. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  1453. return wpas_dbus_error_unknown_error(message, "802.1X not included");
  1454. #endif /* IEEE8021X_EAPOL */
  1455. }
  1456. /**
  1457. * wpas_dbus_handler_add_blob - Store named binary blob (ie, for certificates)
  1458. * @message: Pointer to incoming dbus message
  1459. * @wpa_s: %wpa_supplicant data structure
  1460. * Returns: A dbus message containing an error on failure or NULL on success
  1461. *
  1462. * Asks wpa_supplicant to internally store a binary blobs.
  1463. */
  1464. DBusMessage * wpas_dbus_handler_add_blob(DBusMessage *message,
  1465. struct wpa_supplicant *wpa_s)
  1466. {
  1467. DBusMessage *reply = NULL;
  1468. DBusMessageIter iter, array_iter;
  1469. char *blob_name;
  1470. u8 *blob_data;
  1471. int blob_len;
  1472. struct wpa_config_blob *blob = NULL;
  1473. dbus_message_iter_init(message, &iter);
  1474. dbus_message_iter_get_basic(&iter, &blob_name);
  1475. if (wpa_config_get_blob(wpa_s->conf, blob_name)) {
  1476. return dbus_message_new_error(message,
  1477. WPAS_DBUS_ERROR_BLOB_EXISTS,
  1478. NULL);
  1479. }
  1480. dbus_message_iter_next(&iter);
  1481. dbus_message_iter_recurse(&iter, &array_iter);
  1482. dbus_message_iter_get_fixed_array(&array_iter, &blob_data, &blob_len);
  1483. blob = os_zalloc(sizeof(*blob));
  1484. if (!blob) {
  1485. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1486. NULL);
  1487. goto err;
  1488. }
  1489. blob->data = os_malloc(blob_len);
  1490. if (!blob->data) {
  1491. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1492. NULL);
  1493. goto err;
  1494. }
  1495. os_memcpy(blob->data, blob_data, blob_len);
  1496. blob->len = blob_len;
  1497. blob->name = os_strdup(blob_name);
  1498. if (!blob->name) {
  1499. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1500. NULL);
  1501. goto err;
  1502. }
  1503. wpa_config_set_blob(wpa_s->conf, blob);
  1504. wpas_notify_blob_added(wpa_s, blob->name);
  1505. return reply;
  1506. err:
  1507. if (blob) {
  1508. os_free(blob->name);
  1509. os_free(blob->data);
  1510. os_free(blob);
  1511. }
  1512. return reply;
  1513. }
  1514. /**
  1515. * wpas_dbus_handler_get_blob - Get named binary blob (ie, for certificates)
  1516. * @message: Pointer to incoming dbus message
  1517. * @wpa_s: %wpa_supplicant data structure
  1518. * Returns: A dbus message containing array of bytes (blob)
  1519. *
  1520. * Gets one wpa_supplicant's binary blobs.
  1521. */
  1522. DBusMessage * wpas_dbus_handler_get_blob(DBusMessage *message,
  1523. struct wpa_supplicant *wpa_s)
  1524. {
  1525. DBusMessage *reply = NULL;
  1526. DBusMessageIter iter, array_i

Large files files are truncated, but you can click here to view the full file