dbus_old_handlers.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  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, NULL);
  153. if (wpa_s && wpa_s->dbus_path) {
  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 || !wpa_s->dbus_path) {
  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. if (!wpa_s->dbus_path)
  316. return dbus_message_new_error(message,
  317. WPAS_ERROR_INTERNAL_ERROR,
  318. "no D-Bus interface available");
  319. /* Create and initialize the return message */
  320. reply = dbus_message_new_method_return(message);
  321. dbus_message_iter_init_append(reply, &iter);
  322. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
  323. DBUS_TYPE_OBJECT_PATH_AS_STRING,
  324. &sub_iter))
  325. goto error;
  326. /* Loop through scan results and append each result's object path */
  327. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  328. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX];
  329. char *path = path_buf;
  330. /* Construct the object path for this network. Note that ':'
  331. * is not a valid character in dbus object paths.
  332. */
  333. os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  334. "%s/" WPAS_DBUS_BSSIDS_PART "/"
  335. WPAS_DBUS_BSSID_FORMAT,
  336. wpa_s->dbus_path, MAC2STR(bss->bssid));
  337. if (!dbus_message_iter_append_basic(&sub_iter,
  338. DBUS_TYPE_OBJECT_PATH,
  339. &path))
  340. goto error;
  341. }
  342. if (!dbus_message_iter_close_container(&iter, &sub_iter))
  343. goto error;
  344. return reply;
  345. error:
  346. dbus_message_unref(reply);
  347. return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
  348. "an internal error occurred returning scan results");
  349. }
  350. /**
  351. * wpas_dbus_bssid_properties - Return the properties of a scanned network
  352. * @message: Pointer to incoming dbus message
  353. * @wpa_s: wpa_supplicant structure for a network interface
  354. * @res: wpa_supplicant scan result for which to get properties
  355. * Returns: a dbus message containing the properties for the requested network
  356. *
  357. * Handler function for "properties" method call of a scanned network.
  358. * Returns a dbus message containing the the properties.
  359. */
  360. DBusMessage * wpas_dbus_bssid_properties(DBusMessage *message,
  361. struct wpa_supplicant *wpa_s,
  362. struct wpa_bss *bss)
  363. {
  364. DBusMessage *reply;
  365. DBusMessageIter iter, iter_dict;
  366. const u8 *wpa_ie, *rsn_ie, *wps_ie;
  367. /* Dump the properties into a dbus message */
  368. reply = dbus_message_new_method_return(message);
  369. wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  370. rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  371. wps_ie = wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE);
  372. dbus_message_iter_init_append(reply, &iter);
  373. if (!wpa_dbus_dict_open_write(&iter, &iter_dict) ||
  374. !wpa_dbus_dict_append_byte_array(&iter_dict, "bssid",
  375. (const char *) bss->bssid,
  376. ETH_ALEN) ||
  377. !wpa_dbus_dict_append_byte_array(&iter_dict, "ssid",
  378. (const char *) bss->ssid,
  379. bss->ssid_len) ||
  380. (wpa_ie &&
  381. !wpa_dbus_dict_append_byte_array(&iter_dict, "wpaie",
  382. (const char *) wpa_ie,
  383. wpa_ie[1] + 2)) ||
  384. (rsn_ie &&
  385. !wpa_dbus_dict_append_byte_array(&iter_dict, "rsnie",
  386. (const char *) rsn_ie,
  387. rsn_ie[1] + 2)) ||
  388. (wps_ie &&
  389. !wpa_dbus_dict_append_byte_array(&iter_dict, "wpsie",
  390. (const char *) wps_ie,
  391. wps_ie[1] + 2)) ||
  392. (bss->freq &&
  393. !wpa_dbus_dict_append_int32(&iter_dict, "frequency", bss->freq)) ||
  394. !wpa_dbus_dict_append_uint16(&iter_dict, "capabilities",
  395. bss->caps) ||
  396. (!(bss->flags & WPA_BSS_QUAL_INVALID) &&
  397. !wpa_dbus_dict_append_int32(&iter_dict, "quality", bss->qual)) ||
  398. (!(bss->flags & WPA_BSS_NOISE_INVALID) &&
  399. !wpa_dbus_dict_append_int32(&iter_dict, "noise", bss->noise)) ||
  400. (!(bss->flags & WPA_BSS_LEVEL_INVALID) &&
  401. !wpa_dbus_dict_append_int32(&iter_dict, "level", bss->level)) ||
  402. !wpa_dbus_dict_append_int32(&iter_dict, "maxrate",
  403. wpa_bss_get_max_rate(bss) * 500000) ||
  404. !wpa_dbus_dict_close_write(&iter, &iter_dict)) {
  405. if (reply)
  406. dbus_message_unref(reply);
  407. reply = dbus_message_new_error(
  408. message, WPAS_ERROR_INTERNAL_ERROR,
  409. "an internal error occurred returning BSSID properties.");
  410. }
  411. return reply;
  412. }
  413. /**
  414. * wpas_dbus_iface_capabilities - Return interface capabilities
  415. * @message: Pointer to incoming dbus message
  416. * @wpa_s: wpa_supplicant structure for a network interface
  417. * Returns: A dbus message containing a dict of strings
  418. *
  419. * Handler function for "capabilities" method call of an interface.
  420. */
  421. DBusMessage * wpas_dbus_iface_capabilities(DBusMessage *message,
  422. struct wpa_supplicant *wpa_s)
  423. {
  424. DBusMessage *reply = NULL;
  425. struct wpa_driver_capa capa;
  426. int res;
  427. DBusMessageIter iter, iter_dict;
  428. char **eap_methods;
  429. size_t num_items;
  430. dbus_bool_t strict = FALSE;
  431. DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
  432. if (!dbus_message_get_args(message, NULL,
  433. DBUS_TYPE_BOOLEAN, &strict,
  434. DBUS_TYPE_INVALID))
  435. strict = FALSE;
  436. reply = dbus_message_new_method_return(message);
  437. dbus_message_iter_init_append(reply, &iter);
  438. if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
  439. goto error;
  440. /* EAP methods */
  441. eap_methods = eap_get_names_as_string_array(&num_items);
  442. if (eap_methods) {
  443. dbus_bool_t success;
  444. size_t i = 0;
  445. success = wpa_dbus_dict_append_string_array(
  446. &iter_dict, "eap", (const char **) eap_methods,
  447. num_items);
  448. /* free returned method array */
  449. while (eap_methods[i])
  450. os_free(eap_methods[i++]);
  451. os_free(eap_methods);
  452. if (!success)
  453. goto error;
  454. }
  455. res = wpa_drv_get_capa(wpa_s, &capa);
  456. /***** pairwise cipher */
  457. if (res < 0) {
  458. if (!strict) {
  459. const char *args[] = {"CCMP", "TKIP", "NONE"};
  460. if (!wpa_dbus_dict_append_string_array(
  461. &iter_dict, "pairwise", args,
  462. ARRAY_SIZE(args)))
  463. goto error;
  464. }
  465. } else {
  466. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "pairwise",
  467. &iter_dict_entry,
  468. &iter_dict_val,
  469. &iter_array) ||
  470. ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) &&
  471. !wpa_dbus_dict_string_array_add_element(
  472. &iter_array, "CCMP")) ||
  473. ((capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) &&
  474. !wpa_dbus_dict_string_array_add_element(
  475. &iter_array, "TKIP")) ||
  476. ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) &&
  477. !wpa_dbus_dict_string_array_add_element(
  478. &iter_array, "NONE")) ||
  479. !wpa_dbus_dict_end_string_array(&iter_dict,
  480. &iter_dict_entry,
  481. &iter_dict_val,
  482. &iter_array))
  483. goto error;
  484. }
  485. /***** group cipher */
  486. if (res < 0) {
  487. if (!strict) {
  488. const char *args[] = {
  489. "CCMP", "TKIP", "WEP104", "WEP40"
  490. };
  491. if (!wpa_dbus_dict_append_string_array(
  492. &iter_dict, "group", args,
  493. ARRAY_SIZE(args)))
  494. goto error;
  495. }
  496. } else {
  497. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "group",
  498. &iter_dict_entry,
  499. &iter_dict_val,
  500. &iter_array))
  501. goto error;
  502. if (((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) &&
  503. !wpa_dbus_dict_string_array_add_element(
  504. &iter_array, "CCMP")) ||
  505. ((capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) &&
  506. !wpa_dbus_dict_string_array_add_element(
  507. &iter_array, "TKIP")) ||
  508. ((capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) &&
  509. !wpa_dbus_dict_string_array_add_element(
  510. &iter_array, "WEP104")) ||
  511. ((capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) &&
  512. !wpa_dbus_dict_string_array_add_element(
  513. &iter_array, "WEP40")) ||
  514. !wpa_dbus_dict_end_string_array(&iter_dict,
  515. &iter_dict_entry,
  516. &iter_dict_val,
  517. &iter_array))
  518. goto error;
  519. }
  520. /***** key management */
  521. if (res < 0) {
  522. if (!strict) {
  523. const char *args[] = {
  524. "WPA-PSK", "WPA-EAP", "IEEE8021X", "WPA-NONE",
  525. "NONE"
  526. };
  527. if (!wpa_dbus_dict_append_string_array(
  528. &iter_dict, "key_mgmt", args,
  529. ARRAY_SIZE(args)))
  530. goto error;
  531. }
  532. } else {
  533. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "key_mgmt",
  534. &iter_dict_entry,
  535. &iter_dict_val,
  536. &iter_array) ||
  537. !wpa_dbus_dict_string_array_add_element(&iter_array,
  538. "NONE") ||
  539. !wpa_dbus_dict_string_array_add_element(&iter_array,
  540. "IEEE8021X") ||
  541. ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  542. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) &&
  543. !wpa_dbus_dict_string_array_add_element(
  544. &iter_array, "WPA-EAP")) ||
  545. ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  546. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) &&
  547. !wpa_dbus_dict_string_array_add_element(
  548. &iter_array, "WPA-PSK")) ||
  549. ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) &&
  550. !wpa_dbus_dict_string_array_add_element(
  551. &iter_array, "WPA-NONE")) ||
  552. !wpa_dbus_dict_end_string_array(&iter_dict,
  553. &iter_dict_entry,
  554. &iter_dict_val,
  555. &iter_array))
  556. goto error;
  557. }
  558. /***** WPA protocol */
  559. if (res < 0) {
  560. if (!strict) {
  561. const char *args[] = { "RSN", "WPA" };
  562. if (!wpa_dbus_dict_append_string_array(
  563. &iter_dict, "proto", args,
  564. ARRAY_SIZE(args)))
  565. goto error;
  566. }
  567. } else {
  568. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "proto",
  569. &iter_dict_entry,
  570. &iter_dict_val,
  571. &iter_array) ||
  572. ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  573. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) &&
  574. !wpa_dbus_dict_string_array_add_element(
  575. &iter_array, "RSN")) ||
  576. ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  577. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) &&
  578. !wpa_dbus_dict_string_array_add_element(
  579. &iter_array, "WPA")) ||
  580. !wpa_dbus_dict_end_string_array(&iter_dict,
  581. &iter_dict_entry,
  582. &iter_dict_val,
  583. &iter_array))
  584. goto error;
  585. }
  586. /***** auth alg */
  587. if (res < 0) {
  588. if (!strict) {
  589. const char *args[] = { "OPEN", "SHARED", "LEAP" };
  590. if (!wpa_dbus_dict_append_string_array(
  591. &iter_dict, "auth_alg", args,
  592. ARRAY_SIZE(args)))
  593. goto error;
  594. }
  595. } else {
  596. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "auth_alg",
  597. &iter_dict_entry,
  598. &iter_dict_val,
  599. &iter_array) ||
  600. ((capa.auth & WPA_DRIVER_AUTH_OPEN) &&
  601. !wpa_dbus_dict_string_array_add_element(
  602. &iter_array, "OPEN")) ||
  603. ((capa.auth & WPA_DRIVER_AUTH_SHARED) &&
  604. !wpa_dbus_dict_string_array_add_element(
  605. &iter_array, "SHARED")) ||
  606. ((capa.auth & WPA_DRIVER_AUTH_LEAP) &&
  607. !wpa_dbus_dict_string_array_add_element(
  608. &iter_array, "LEAP")) ||
  609. !wpa_dbus_dict_end_string_array(&iter_dict,
  610. &iter_dict_entry,
  611. &iter_dict_val,
  612. &iter_array))
  613. goto error;
  614. }
  615. if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
  616. goto error;
  617. return reply;
  618. error:
  619. if (reply)
  620. dbus_message_unref(reply);
  621. return dbus_message_new_error(
  622. message, WPAS_ERROR_INTERNAL_ERROR,
  623. "an internal error occurred returning interface capabilities.");
  624. }
  625. /**
  626. * wpas_dbus_iface_add_network - Add a new configured network
  627. * @message: Pointer to incoming dbus message
  628. * @wpa_s: wpa_supplicant structure for a network interface
  629. * Returns: A dbus message containing the object path of the new network
  630. *
  631. * Handler function for "addNetwork" method call of a network interface.
  632. */
  633. DBusMessage * wpas_dbus_iface_add_network(DBusMessage *message,
  634. struct wpa_supplicant *wpa_s)
  635. {
  636. DBusMessage *reply = NULL;
  637. struct wpa_ssid *ssid = NULL;
  638. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *path = path_buf;
  639. if (wpa_s->dbus_path)
  640. ssid = wpa_supplicant_add_network(wpa_s);
  641. if (ssid == NULL) {
  642. reply = dbus_message_new_error(
  643. message, WPAS_ERROR_ADD_NETWORK_ERROR,
  644. "wpa_supplicant could not add a network on this interface.");
  645. goto out;
  646. }
  647. /* Construct the object path for this network. */
  648. os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  649. "%s/" WPAS_DBUS_NETWORKS_PART "/%d",
  650. wpa_s->dbus_path, ssid->id);
  651. reply = dbus_message_new_method_return(message);
  652. dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
  653. &path, DBUS_TYPE_INVALID);
  654. out:
  655. return reply;
  656. }
  657. /**
  658. * wpas_dbus_iface_remove_network - Remove a configured network
  659. * @message: Pointer to incoming dbus message
  660. * @wpa_s: wpa_supplicant structure for a network interface
  661. * Returns: A dbus message containing a UINT32 indicating success (1) or
  662. * failure (0)
  663. *
  664. * Handler function for "removeNetwork" method call of a network interface.
  665. */
  666. DBusMessage * wpas_dbus_iface_remove_network(DBusMessage *message,
  667. struct wpa_supplicant *wpa_s)
  668. {
  669. DBusMessage *reply = NULL;
  670. const char *op;
  671. char *iface = NULL, *net_id = NULL;
  672. int id;
  673. int result;
  674. if (!dbus_message_get_args(message, NULL,
  675. DBUS_TYPE_OBJECT_PATH, &op,
  676. DBUS_TYPE_INVALID)) {
  677. reply = wpas_dbus_new_invalid_opts_error(message, NULL);
  678. goto out;
  679. }
  680. /* Extract the network ID */
  681. iface = wpas_dbus_decompose_object_path(op, &net_id, NULL);
  682. if (iface == NULL || net_id == NULL) {
  683. reply = wpas_dbus_new_invalid_network_error(message);
  684. goto out;
  685. }
  686. /* Ensure the network is actually a child of this interface */
  687. if (!wpa_s->dbus_path || os_strcmp(iface, wpa_s->dbus_path) != 0) {
  688. reply = wpas_dbus_new_invalid_network_error(message);
  689. goto out;
  690. }
  691. id = strtoul(net_id, NULL, 10);
  692. result = wpa_supplicant_remove_network(wpa_s, id);
  693. if (result == -1) {
  694. reply = wpas_dbus_new_invalid_network_error(message);
  695. goto out;
  696. }
  697. if (result == -2) {
  698. reply = dbus_message_new_error(
  699. message, WPAS_ERROR_REMOVE_NETWORK_ERROR,
  700. "error removing the specified on this interface.");
  701. goto out;
  702. }
  703. reply = wpas_dbus_new_success_reply(message);
  704. out:
  705. os_free(iface);
  706. os_free(net_id);
  707. return reply;
  708. }
  709. static const char * const dont_quote[] = {
  710. "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
  711. "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
  712. "bssid", "scan_freq", "freq_list", NULL
  713. };
  714. static dbus_bool_t should_quote_opt(const char *key)
  715. {
  716. int i = 0;
  717. while (dont_quote[i] != NULL) {
  718. if (os_strcmp(key, dont_quote[i]) == 0)
  719. return FALSE;
  720. i++;
  721. }
  722. return TRUE;
  723. }
  724. /**
  725. * wpas_dbus_iface_set_network - Set options for a configured network
  726. * @message: Pointer to incoming dbus message
  727. * @wpa_s: wpa_supplicant structure for a network interface
  728. * @ssid: wpa_ssid structure for a configured network
  729. * Returns: a dbus message containing a UINT32 indicating success (1) or
  730. * failure (0)
  731. *
  732. * Handler function for "set" method call of a configured network.
  733. */
  734. DBusMessage * wpas_dbus_iface_set_network(DBusMessage *message,
  735. struct wpa_supplicant *wpa_s,
  736. struct wpa_ssid *ssid)
  737. {
  738. DBusMessage *reply = NULL;
  739. struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
  740. DBusMessageIter iter, iter_dict;
  741. dbus_message_iter_init(message, &iter);
  742. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL)) {
  743. reply = wpas_dbus_new_invalid_opts_error(message, NULL);
  744. goto out;
  745. }
  746. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  747. char *value = NULL;
  748. size_t size = 50;
  749. int ret;
  750. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
  751. reply = wpas_dbus_new_invalid_opts_error(message,
  752. NULL);
  753. goto out;
  754. }
  755. /* Type conversions, since wpa_supplicant wants strings */
  756. if (entry.type == DBUS_TYPE_ARRAY &&
  757. entry.array_type == DBUS_TYPE_BYTE) {
  758. if (entry.array_len <= 0)
  759. goto error;
  760. size = entry.array_len * 2 + 1;
  761. value = os_zalloc(size);
  762. if (value == NULL)
  763. goto error;
  764. ret = wpa_snprintf_hex(value, size,
  765. (u8 *) entry.bytearray_value,
  766. entry.array_len);
  767. if (ret <= 0)
  768. goto error;
  769. } else if (entry.type == DBUS_TYPE_STRING) {
  770. if (should_quote_opt(entry.key)) {
  771. size = os_strlen(entry.str_value);
  772. /* Zero-length option check */
  773. if (size == 0)
  774. goto error;
  775. size += 3; /* For quotes and terminator */
  776. value = os_zalloc(size);
  777. if (value == NULL)
  778. goto error;
  779. ret = os_snprintf(value, size, "\"%s\"",
  780. entry.str_value);
  781. if (os_snprintf_error(size, ret))
  782. goto error;
  783. } else {
  784. value = os_strdup(entry.str_value);
  785. if (value == NULL)
  786. goto error;
  787. }
  788. } else if (entry.type == DBUS_TYPE_UINT32) {
  789. value = os_zalloc(size);
  790. if (value == NULL)
  791. goto error;
  792. ret = os_snprintf(value, size, "%u",
  793. entry.uint32_value);
  794. if (os_snprintf_error(size, ret))
  795. goto error;
  796. } else if (entry.type == DBUS_TYPE_INT32) {
  797. value = os_zalloc(size);
  798. if (value == NULL)
  799. goto error;
  800. ret = os_snprintf(value, size, "%d",
  801. entry.int32_value);
  802. if (os_snprintf_error(size, ret))
  803. goto error;
  804. } else
  805. goto error;
  806. if (wpa_config_set(ssid, entry.key, value, 0) < 0)
  807. goto error;
  808. if ((os_strcmp(entry.key, "psk") == 0 &&
  809. value[0] == '"' && ssid->ssid_len) ||
  810. (os_strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
  811. wpa_config_update_psk(ssid);
  812. else if (os_strcmp(entry.key, "priority") == 0)
  813. wpa_config_update_prio_list(wpa_s->conf);
  814. os_free(value);
  815. wpa_dbus_dict_entry_clear(&entry);
  816. continue;
  817. error:
  818. os_free(value);
  819. reply = wpas_dbus_new_invalid_opts_error(message, entry.key);
  820. wpa_dbus_dict_entry_clear(&entry);
  821. break;
  822. }
  823. if (!reply)
  824. reply = wpas_dbus_new_success_reply(message);
  825. out:
  826. return reply;
  827. }
  828. /**
  829. * wpas_dbus_iface_enable_network - Mark a configured network as enabled
  830. * @message: Pointer to incoming dbus message
  831. * @wpa_s: wpa_supplicant structure for a network interface
  832. * @ssid: wpa_ssid structure for a configured network
  833. * Returns: A dbus message containing a UINT32 indicating success (1) or
  834. * failure (0)
  835. *
  836. * Handler function for "enable" method call of a configured network.
  837. */
  838. DBusMessage * wpas_dbus_iface_enable_network(DBusMessage *message,
  839. struct wpa_supplicant *wpa_s,
  840. struct wpa_ssid *ssid)
  841. {
  842. wpa_supplicant_enable_network(wpa_s, ssid);
  843. return wpas_dbus_new_success_reply(message);
  844. }
  845. /**
  846. * wpas_dbus_iface_disable_network - Mark a configured network as disabled
  847. * @message: Pointer to incoming dbus message
  848. * @wpa_s: wpa_supplicant structure for a network interface
  849. * @ssid: wpa_ssid structure for a configured network
  850. * Returns: A dbus message containing a UINT32 indicating success (1) or
  851. * failure (0)
  852. *
  853. * Handler function for "disable" method call of a configured network.
  854. */
  855. DBusMessage * wpas_dbus_iface_disable_network(DBusMessage *message,
  856. struct wpa_supplicant *wpa_s,
  857. struct wpa_ssid *ssid)
  858. {
  859. wpa_supplicant_disable_network(wpa_s, ssid);
  860. return wpas_dbus_new_success_reply(message);
  861. }
  862. /**
  863. * wpas_dbus_iface_select_network - Attempt association with a configured network
  864. * @message: Pointer to incoming dbus message
  865. * @wpa_s: wpa_supplicant structure for a network interface
  866. * Returns: A dbus message containing a UINT32 indicating success (1) or
  867. * failure (0)
  868. *
  869. * Handler function for "selectNetwork" method call of network interface.
  870. */
  871. DBusMessage * wpas_dbus_iface_select_network(DBusMessage *message,
  872. struct wpa_supplicant *wpa_s)
  873. {
  874. DBusMessage *reply = NULL;
  875. const char *op;
  876. struct wpa_ssid *ssid;
  877. char *iface_obj_path = NULL;
  878. char *network = NULL;
  879. if (os_strlen(dbus_message_get_signature(message)) == 0) {
  880. /* Any network */
  881. ssid = NULL;
  882. } else {
  883. int nid;
  884. if (!dbus_message_get_args(message, NULL,
  885. DBUS_TYPE_OBJECT_PATH, &op,
  886. DBUS_TYPE_INVALID)) {
  887. reply = wpas_dbus_new_invalid_opts_error(message,
  888. NULL);
  889. goto out;
  890. }
  891. /* Extract the network number */
  892. iface_obj_path = wpas_dbus_decompose_object_path(op,
  893. &network,
  894. NULL);
  895. if (iface_obj_path == NULL) {
  896. reply = wpas_dbus_new_invalid_iface_error(message);
  897. goto out;
  898. }
  899. /* Ensure the object path really points to this interface */
  900. if (network == NULL || !wpa_s->dbus_path ||
  901. os_strcmp(iface_obj_path, wpa_s->dbus_path) != 0) {
  902. reply = wpas_dbus_new_invalid_network_error(message);
  903. goto out;
  904. }
  905. nid = strtoul(network, NULL, 10);
  906. if (errno == EINVAL) {
  907. reply = wpas_dbus_new_invalid_network_error(message);
  908. goto out;
  909. }
  910. ssid = wpa_config_get_network(wpa_s->conf, nid);
  911. if (ssid == NULL) {
  912. reply = wpas_dbus_new_invalid_network_error(message);
  913. goto out;
  914. }
  915. }
  916. /* Finally, associate with the network */
  917. wpa_supplicant_select_network(wpa_s, ssid);
  918. reply = wpas_dbus_new_success_reply(message);
  919. out:
  920. os_free(iface_obj_path);
  921. os_free(network);
  922. return reply;
  923. }
  924. /**
  925. * wpas_dbus_iface_disconnect - Terminate the current connection
  926. * @message: Pointer to incoming dbus message
  927. * @wpa_s: wpa_supplicant structure for a network interface
  928. * Returns: A dbus message containing a UINT32 indicating success (1) or
  929. * failure (0)
  930. *
  931. * Handler function for "disconnect" method call of network interface.
  932. */
  933. DBusMessage * wpas_dbus_iface_disconnect(DBusMessage *message,
  934. struct wpa_supplicant *wpa_s)
  935. {
  936. wpas_request_disconnection(wpa_s);
  937. return wpas_dbus_new_success_reply(message);
  938. }
  939. /**
  940. * wpas_dbus_iface_set_ap_scan - Control roaming mode
  941. * @message: Pointer to incoming dbus message
  942. * @wpa_s: wpa_supplicant structure for a network interface
  943. * Returns: A dbus message containing a UINT32 indicating success (1) or
  944. * failure (0)
  945. *
  946. * Handler function for "setAPScan" method call.
  947. */
  948. DBusMessage * wpas_dbus_iface_set_ap_scan(DBusMessage *message,
  949. struct wpa_supplicant *wpa_s)
  950. {
  951. DBusMessage *reply = NULL;
  952. dbus_uint32_t ap_scan = 1;
  953. if (!dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &ap_scan,
  954. DBUS_TYPE_INVALID)) {
  955. reply = wpas_dbus_new_invalid_opts_error(message, NULL);
  956. goto out;
  957. }
  958. if (wpa_supplicant_set_ap_scan(wpa_s, ap_scan)) {
  959. reply = wpas_dbus_new_invalid_opts_error(message, NULL);
  960. goto out;
  961. }
  962. reply = wpas_dbus_new_success_reply(message);
  963. out:
  964. return reply;
  965. }
  966. /**
  967. * wpas_dbus_iface_set_smartcard_modules - Set smartcard related module paths
  968. * @message: Pointer to incoming dbus message
  969. * @wpa_s: wpa_supplicant structure for a network interface
  970. * Returns: A dbus message containing a UINT32 indicating success (1) or
  971. * failure (0)
  972. *
  973. * Handler function for "setSmartcardModules" method call.
  974. */
  975. DBusMessage * wpas_dbus_iface_set_smartcard_modules(
  976. DBusMessage *message, struct wpa_supplicant *wpa_s)
  977. {
  978. DBusMessageIter iter, iter_dict;
  979. char *opensc_engine_path = NULL;
  980. char *pkcs11_engine_path = NULL;
  981. char *pkcs11_module_path = NULL;
  982. struct wpa_dbus_dict_entry entry;
  983. if (!dbus_message_iter_init(message, &iter))
  984. goto error;
  985. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  986. goto error;
  987. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  988. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  989. goto error;
  990. if (!strcmp(entry.key, "opensc_engine_path") &&
  991. entry.type == DBUS_TYPE_STRING) {
  992. os_free(opensc_engine_path);
  993. opensc_engine_path = os_strdup(entry.str_value);
  994. wpa_dbus_dict_entry_clear(&entry);
  995. if (opensc_engine_path == NULL)
  996. goto error;
  997. } else if (!strcmp(entry.key, "pkcs11_engine_path") &&
  998. entry.type == DBUS_TYPE_STRING) {
  999. os_free(pkcs11_engine_path);
  1000. pkcs11_engine_path = os_strdup(entry.str_value);
  1001. wpa_dbus_dict_entry_clear(&entry);
  1002. if (pkcs11_engine_path == NULL)
  1003. goto error;
  1004. } else if (!strcmp(entry.key, "pkcs11_module_path") &&
  1005. entry.type == DBUS_TYPE_STRING) {
  1006. os_free(pkcs11_module_path);
  1007. pkcs11_module_path = os_strdup(entry.str_value);
  1008. wpa_dbus_dict_entry_clear(&entry);
  1009. if (pkcs11_module_path == NULL)
  1010. goto error;
  1011. } else {
  1012. wpa_dbus_dict_entry_clear(&entry);
  1013. goto error;
  1014. }
  1015. }
  1016. os_free(wpa_s->conf->opensc_engine_path);
  1017. wpa_s->conf->opensc_engine_path = opensc_engine_path;
  1018. os_free(wpa_s->conf->pkcs11_engine_path);
  1019. wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path;
  1020. os_free(wpa_s->conf->pkcs11_module_path);
  1021. wpa_s->conf->pkcs11_module_path = pkcs11_module_path;
  1022. wpa_sm_set_eapol(wpa_s->wpa, NULL);
  1023. eapol_sm_deinit(wpa_s->eapol);
  1024. wpa_s->eapol = NULL;
  1025. wpa_supplicant_init_eapol(wpa_s);
  1026. wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
  1027. return wpas_dbus_new_success_reply(message);
  1028. error:
  1029. os_free(opensc_engine_path);
  1030. os_free(pkcs11_engine_path);
  1031. os_free(pkcs11_module_path);
  1032. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1033. }
  1034. /**
  1035. * wpas_dbus_iface_get_state - Get interface state
  1036. * @message: Pointer to incoming dbus message
  1037. * @wpa_s: wpa_supplicant structure for a network interface
  1038. * Returns: A dbus message containing a STRING representing the current
  1039. * interface state
  1040. *
  1041. * Handler function for "state" method call.
  1042. */
  1043. DBusMessage * wpas_dbus_iface_get_state(DBusMessage *message,
  1044. struct wpa_supplicant *wpa_s)
  1045. {
  1046. DBusMessage *reply = NULL;
  1047. const char *str_state;
  1048. reply = dbus_message_new_method_return(message);
  1049. if (reply != NULL) {
  1050. str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
  1051. dbus_message_append_args(reply, DBUS_TYPE_STRING, &str_state,
  1052. DBUS_TYPE_INVALID);
  1053. }
  1054. return reply;
  1055. }
  1056. /**
  1057. * wpas_dbus_iface_get_scanning - Get interface scanning state
  1058. * @message: Pointer to incoming dbus message
  1059. * @wpa_s: wpa_supplicant structure for a network interface
  1060. * Returns: A dbus message containing whether the interface is scanning
  1061. *
  1062. * Handler function for "scanning" method call.
  1063. */
  1064. DBusMessage * wpas_dbus_iface_get_scanning(DBusMessage *message,
  1065. struct wpa_supplicant *wpa_s)
  1066. {
  1067. DBusMessage *reply = NULL;
  1068. dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
  1069. reply = dbus_message_new_method_return(message);
  1070. if (reply != NULL) {
  1071. dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &scanning,
  1072. DBUS_TYPE_INVALID);
  1073. } else {
  1074. wpa_printf(MSG_ERROR,
  1075. "dbus: Not enough memory to return scanning state");
  1076. }
  1077. return reply;
  1078. }
  1079. #ifndef CONFIG_NO_CONFIG_BLOBS
  1080. /**
  1081. * wpas_dbus_iface_set_blobs - Store named binary blobs (ie, for certificates)
  1082. * @message: Pointer to incoming dbus message
  1083. * @wpa_s: %wpa_supplicant data structure
  1084. * Returns: A dbus message containing a UINT32 indicating success (1) or
  1085. * failure (0)
  1086. *
  1087. * Asks wpa_supplicant to internally store a one or more binary blobs.
  1088. */
  1089. DBusMessage * wpas_dbus_iface_set_blobs(DBusMessage *message,
  1090. struct wpa_supplicant *wpa_s)
  1091. {
  1092. DBusMessage *reply = NULL;
  1093. struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
  1094. DBusMessageIter iter, iter_dict;
  1095. dbus_message_iter_init(message, &iter);
  1096. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  1097. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1098. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1099. struct wpa_config_blob *blob;
  1100. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
  1101. reply = wpas_dbus_new_invalid_opts_error(message,
  1102. NULL);
  1103. break;
  1104. }
  1105. if (entry.type != DBUS_TYPE_ARRAY ||
  1106. entry.array_type != DBUS_TYPE_BYTE) {
  1107. reply = wpas_dbus_new_invalid_opts_error(
  1108. message, "Byte array expected.");
  1109. break;
  1110. }
  1111. if ((entry.array_len <= 0) || (entry.array_len > 65536) ||
  1112. !strlen(entry.key)) {
  1113. reply = wpas_dbus_new_invalid_opts_error(
  1114. message, "Invalid array size.");
  1115. break;
  1116. }
  1117. blob = os_zalloc(sizeof(*blob));
  1118. if (blob == NULL) {
  1119. reply = dbus_message_new_error(
  1120. message, WPAS_ERROR_ADD_ERROR,
  1121. "Not enough memory to add blob.");
  1122. break;
  1123. }
  1124. blob->data = os_zalloc(entry.array_len);
  1125. if (blob->data == NULL) {
  1126. reply = dbus_message_new_error(
  1127. message, WPAS_ERROR_ADD_ERROR,
  1128. "Not enough memory to add blob data.");
  1129. os_free(blob);
  1130. break;
  1131. }
  1132. blob->name = os_strdup(entry.key);
  1133. blob->len = entry.array_len;
  1134. os_memcpy(blob->data, (u8 *) entry.bytearray_value,
  1135. entry.array_len);
  1136. if (blob->name == NULL) {
  1137. wpa_config_free_blob(blob);
  1138. reply = dbus_message_new_error(
  1139. message, WPAS_ERROR_ADD_ERROR,
  1140. "Error adding blob.");
  1141. break;
  1142. }
  1143. /* Success */
  1144. if (!wpa_config_remove_blob(wpa_s->conf, blob->name))
  1145. wpas_notify_blob_removed(wpa_s, blob->name);
  1146. wpa_config_set_blob(wpa_s->conf, blob);
  1147. wpas_notify_blob_added(wpa_s, blob->name);
  1148. wpa_dbus_dict_entry_clear(&entry);
  1149. }
  1150. wpa_dbus_dict_entry_clear(&entry);
  1151. return reply ? reply : wpas_dbus_new_success_reply(message);
  1152. }
  1153. /**
  1154. * wpas_dbus_iface_remove_blob - Remove named binary blobs
  1155. * @message: Pointer to incoming dbus message
  1156. * @wpa_s: %wpa_supplicant data structure
  1157. * Returns: A dbus message containing a UINT32 indicating success (1) or
  1158. * failure (0)
  1159. *
  1160. * Asks wpa_supplicant to remove one or more previously stored binary blobs.
  1161. */
  1162. DBusMessage * wpas_dbus_iface_remove_blobs(DBusMessage *message,
  1163. struct wpa_supplicant *wpa_s)
  1164. {
  1165. DBusMessageIter iter, array;
  1166. char *err_msg = NULL;
  1167. dbus_message_iter_init(message, &iter);
  1168. if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
  1169. dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
  1170. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1171. dbus_message_iter_recurse(&iter, &array);
  1172. while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
  1173. const char *name;
  1174. dbus_message_iter_get_basic(&array, &name);
  1175. if (!os_strlen(name))
  1176. err_msg = "Invalid blob name.";
  1177. else if (wpa_config_remove_blob(wpa_s->conf, name) != 0)
  1178. err_msg = "Error removing blob.";
  1179. else
  1180. wpas_notify_blob_removed(wpa_s, name);
  1181. dbus_message_iter_next(&array);
  1182. }
  1183. if (err_msg)
  1184. return dbus_message_new_error(message, WPAS_ERROR_REMOVE_ERROR,
  1185. err_msg);
  1186. return wpas_dbus_new_success_reply(message);
  1187. }
  1188. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1189. /**
  1190. * wpas_dbus_iface_flush - Clear BSS of old or all inactive entries
  1191. * @message: Pointer to incoming dbus message
  1192. * @wpa_s: %wpa_supplicant data structure
  1193. * Returns: a dbus message containing a UINT32 indicating success (1) or
  1194. * failure (0), or returns a dbus error message with more information
  1195. *
  1196. * Handler function for "flush" method call. Handles requests for an
  1197. * interface with an optional "age" parameter that specifies the minimum
  1198. * age of a BSS to be flushed.
  1199. */
  1200. DBusMessage * wpas_dbus_iface_flush(DBusMessage *message,
  1201. struct wpa_supplicant *wpa_s)
  1202. {
  1203. int flush_age = 0;
  1204. if (os_strlen(dbus_message_get_signature(message)) != 0 &&
  1205. !dbus_message_get_args(message, NULL,
  1206. DBUS_TYPE_INT32, &flush_age,
  1207. DBUS_TYPE_INVALID)) {
  1208. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1209. }
  1210. if (flush_age == 0)
  1211. wpa_bss_flush(wpa_s);
  1212. else
  1213. wpa_bss_flush_by_age(wpa_s, flush_age);
  1214. return wpas_dbus_new_success_reply(message);
  1215. }