dbus_old_handlers.c 40 KB

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