dbus_old_handlers.c 40 KB

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