dbus_old_handlers.c 40 KB

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