dbus_old_handlers.c 39 KB

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