ctrl_iface_dbus_handlers.c 36 KB

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