dbus_handlers.c 41 KB

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