dbus_old_handlers.c 40 KB

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