ctrl_iface_dbus_handlers.c 39 KB

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