You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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