ctrl_iface_dbus_handlers.c 38 KB

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