dbus_old_handlers.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  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) {
  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) {
  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. /* Create and initialize the return message */
  316. reply = dbus_message_new_method_return(message);
  317. dbus_message_iter_init_append(reply, &iter);
  318. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
  319. DBUS_TYPE_OBJECT_PATH_AS_STRING,
  320. &sub_iter))
  321. goto error;
  322. /* Loop through scan results and append each result's object path */
  323. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  324. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX];
  325. char *path = path_buf;
  326. /* Construct the object path for this network. Note that ':'
  327. * is not a valid character in dbus object paths.
  328. */
  329. os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  330. "%s/" WPAS_DBUS_BSSIDS_PART "/"
  331. WPAS_DBUS_BSSID_FORMAT,
  332. wpa_s->dbus_path, MAC2STR(bss->bssid));
  333. if (!dbus_message_iter_append_basic(&sub_iter,
  334. DBUS_TYPE_OBJECT_PATH,
  335. &path))
  336. goto error;
  337. }
  338. if (!dbus_message_iter_close_container(&iter, &sub_iter))
  339. goto error;
  340. return reply;
  341. error:
  342. dbus_message_unref(reply);
  343. return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
  344. "an internal error occurred returning scan results");
  345. }
  346. /**
  347. * wpas_dbus_bssid_properties - Return the properties of a scanned network
  348. * @message: Pointer to incoming dbus message
  349. * @wpa_s: wpa_supplicant structure for a network interface
  350. * @res: wpa_supplicant scan result for which to get properties
  351. * Returns: a dbus message containing the properties for the requested network
  352. *
  353. * Handler function for "properties" method call of a scanned network.
  354. * Returns a dbus message containing the the properties.
  355. */
  356. DBusMessage * wpas_dbus_bssid_properties(DBusMessage *message,
  357. struct wpa_supplicant *wpa_s,
  358. struct wpa_bss *bss)
  359. {
  360. DBusMessage *reply;
  361. DBusMessageIter iter, iter_dict;
  362. const u8 *wpa_ie, *rsn_ie, *wps_ie;
  363. /* Dump the properties into a dbus message */
  364. reply = dbus_message_new_method_return(message);
  365. wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  366. rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  367. wps_ie = wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE);
  368. dbus_message_iter_init_append(reply, &iter);
  369. if (!wpa_dbus_dict_open_write(&iter, &iter_dict) ||
  370. !wpa_dbus_dict_append_byte_array(&iter_dict, "bssid",
  371. (const char *) bss->bssid,
  372. ETH_ALEN) ||
  373. !wpa_dbus_dict_append_byte_array(&iter_dict, "ssid",
  374. (const char *) bss->ssid,
  375. bss->ssid_len) ||
  376. (wpa_ie &&
  377. !wpa_dbus_dict_append_byte_array(&iter_dict, "wpaie",
  378. (const char *) wpa_ie,
  379. wpa_ie[1] + 2)) ||
  380. (rsn_ie &&
  381. !wpa_dbus_dict_append_byte_array(&iter_dict, "rsnie",
  382. (const char *) rsn_ie,
  383. rsn_ie[1] + 2)) ||
  384. (wps_ie &&
  385. !wpa_dbus_dict_append_byte_array(&iter_dict, "wpsie",
  386. (const char *) wps_ie,
  387. wps_ie[1] + 2)) ||
  388. (bss->freq &&
  389. !wpa_dbus_dict_append_int32(&iter_dict, "frequency", bss->freq)) ||
  390. !wpa_dbus_dict_append_uint16(&iter_dict, "capabilities",
  391. bss->caps) ||
  392. (!(bss->flags & WPA_BSS_QUAL_INVALID) &&
  393. !wpa_dbus_dict_append_int32(&iter_dict, "quality", bss->qual)) ||
  394. (!(bss->flags & WPA_BSS_NOISE_INVALID) &&
  395. !wpa_dbus_dict_append_int32(&iter_dict, "noise", bss->noise)) ||
  396. (!(bss->flags & WPA_BSS_LEVEL_INVALID) &&
  397. !wpa_dbus_dict_append_int32(&iter_dict, "level", bss->level)) ||
  398. !wpa_dbus_dict_append_int32(&iter_dict, "maxrate",
  399. wpa_bss_get_max_rate(bss) * 500000) ||
  400. !wpa_dbus_dict_close_write(&iter, &iter_dict)) {
  401. if (reply)
  402. dbus_message_unref(reply);
  403. reply = dbus_message_new_error(
  404. message, WPAS_ERROR_INTERNAL_ERROR,
  405. "an internal error occurred returning BSSID properties.");
  406. }
  407. return reply;
  408. }
  409. /**
  410. * wpas_dbus_iface_capabilities - Return interface capabilities
  411. * @message: Pointer to incoming dbus message
  412. * @wpa_s: wpa_supplicant structure for a network interface
  413. * Returns: A dbus message containing a dict of strings
  414. *
  415. * Handler function for "capabilities" method call of an interface.
  416. */
  417. DBusMessage * wpas_dbus_iface_capabilities(DBusMessage *message,
  418. struct wpa_supplicant *wpa_s)
  419. {
  420. DBusMessage *reply = NULL;
  421. struct wpa_driver_capa capa;
  422. int res;
  423. DBusMessageIter iter, iter_dict;
  424. char **eap_methods;
  425. size_t num_items;
  426. dbus_bool_t strict = FALSE;
  427. DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
  428. if (!dbus_message_get_args(message, NULL,
  429. DBUS_TYPE_BOOLEAN, &strict,
  430. DBUS_TYPE_INVALID))
  431. strict = FALSE;
  432. reply = dbus_message_new_method_return(message);
  433. dbus_message_iter_init_append(reply, &iter);
  434. if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
  435. goto error;
  436. /* EAP methods */
  437. eap_methods = eap_get_names_as_string_array(&num_items);
  438. if (eap_methods) {
  439. dbus_bool_t success = FALSE;
  440. size_t i = 0;
  441. success = wpa_dbus_dict_append_string_array(
  442. &iter_dict, "eap", (const char **) eap_methods,
  443. num_items);
  444. /* free returned method array */
  445. while (eap_methods[i])
  446. os_free(eap_methods[i++]);
  447. os_free(eap_methods);
  448. if (!success)
  449. goto error;
  450. }
  451. res = wpa_drv_get_capa(wpa_s, &capa);
  452. /***** pairwise cipher */
  453. if (res < 0) {
  454. if (!strict) {
  455. const char *args[] = {"CCMP", "TKIP", "NONE"};
  456. if (!wpa_dbus_dict_append_string_array(
  457. &iter_dict, "pairwise", args,
  458. ARRAY_SIZE(args)))
  459. goto error;
  460. }
  461. } else {
  462. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "pairwise",
  463. &iter_dict_entry,
  464. &iter_dict_val,
  465. &iter_array) ||
  466. ((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) &&
  467. !wpa_dbus_dict_string_array_add_element(
  468. &iter_array, "CCMP")) ||
  469. ((capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) &&
  470. !wpa_dbus_dict_string_array_add_element(
  471. &iter_array, "TKIP")) ||
  472. ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) &&
  473. !wpa_dbus_dict_string_array_add_element(
  474. &iter_array, "NONE")) ||
  475. !wpa_dbus_dict_end_string_array(&iter_dict,
  476. &iter_dict_entry,
  477. &iter_dict_val,
  478. &iter_array))
  479. goto error;
  480. }
  481. /***** group cipher */
  482. if (res < 0) {
  483. if (!strict) {
  484. const char *args[] = {
  485. "CCMP", "TKIP", "WEP104", "WEP40"
  486. };
  487. if (!wpa_dbus_dict_append_string_array(
  488. &iter_dict, "group", args,
  489. ARRAY_SIZE(args)))
  490. goto error;
  491. }
  492. } else {
  493. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "group",
  494. &iter_dict_entry,
  495. &iter_dict_val,
  496. &iter_array))
  497. goto error;
  498. if (((capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) &&
  499. !wpa_dbus_dict_string_array_add_element(
  500. &iter_array, "CCMP")) ||
  501. ((capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) &&
  502. !wpa_dbus_dict_string_array_add_element(
  503. &iter_array, "TKIP")) ||
  504. ((capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) &&
  505. !wpa_dbus_dict_string_array_add_element(
  506. &iter_array, "WEP104")) ||
  507. ((capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) &&
  508. !wpa_dbus_dict_string_array_add_element(
  509. &iter_array, "WEP40")) ||
  510. !wpa_dbus_dict_end_string_array(&iter_dict,
  511. &iter_dict_entry,
  512. &iter_dict_val,
  513. &iter_array))
  514. goto error;
  515. }
  516. /***** key management */
  517. if (res < 0) {
  518. if (!strict) {
  519. const char *args[] = {
  520. "WPA-PSK", "WPA-EAP", "IEEE8021X", "WPA-NONE",
  521. "NONE"
  522. };
  523. if (!wpa_dbus_dict_append_string_array(
  524. &iter_dict, "key_mgmt", args,
  525. ARRAY_SIZE(args)))
  526. goto error;
  527. }
  528. } else {
  529. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "key_mgmt",
  530. &iter_dict_entry,
  531. &iter_dict_val,
  532. &iter_array) ||
  533. !wpa_dbus_dict_string_array_add_element(&iter_array,
  534. "NONE") ||
  535. !wpa_dbus_dict_string_array_add_element(&iter_array,
  536. "IEEE8021X") ||
  537. ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  538. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) &&
  539. !wpa_dbus_dict_string_array_add_element(
  540. &iter_array, "WPA-EAP")) ||
  541. ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  542. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) &&
  543. !wpa_dbus_dict_string_array_add_element(
  544. &iter_array, "WPA-PSK")) ||
  545. ((capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) &&
  546. !wpa_dbus_dict_string_array_add_element(
  547. &iter_array, "WPA-NONE")) ||
  548. !wpa_dbus_dict_end_string_array(&iter_dict,
  549. &iter_dict_entry,
  550. &iter_dict_val,
  551. &iter_array))
  552. goto error;
  553. }
  554. /***** WPA protocol */
  555. if (res < 0) {
  556. if (!strict) {
  557. const char *args[] = { "RSN", "WPA" };
  558. if (!wpa_dbus_dict_append_string_array(
  559. &iter_dict, "proto", args,
  560. ARRAY_SIZE(args)))
  561. goto error;
  562. }
  563. } else {
  564. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "proto",
  565. &iter_dict_entry,
  566. &iter_dict_val,
  567. &iter_array) ||
  568. ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  569. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) &&
  570. !wpa_dbus_dict_string_array_add_element(
  571. &iter_array, "RSN")) ||
  572. ((capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  573. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) &&
  574. !wpa_dbus_dict_string_array_add_element(
  575. &iter_array, "WPA")) ||
  576. !wpa_dbus_dict_end_string_array(&iter_dict,
  577. &iter_dict_entry,
  578. &iter_dict_val,
  579. &iter_array))
  580. goto error;
  581. }
  582. /***** auth alg */
  583. if (res < 0) {
  584. if (!strict) {
  585. const char *args[] = { "OPEN", "SHARED", "LEAP" };
  586. if (!wpa_dbus_dict_append_string_array(
  587. &iter_dict, "auth_alg", args,
  588. ARRAY_SIZE(args)))
  589. goto error;
  590. }
  591. } else {
  592. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "auth_alg",
  593. &iter_dict_entry,
  594. &iter_dict_val,
  595. &iter_array) ||
  596. ((capa.auth & WPA_DRIVER_AUTH_OPEN) &&
  597. !wpa_dbus_dict_string_array_add_element(
  598. &iter_array, "OPEN")) ||
  599. ((capa.auth & WPA_DRIVER_AUTH_SHARED) &&
  600. !wpa_dbus_dict_string_array_add_element(
  601. &iter_array, "SHARED")) ||
  602. ((capa.auth & WPA_DRIVER_AUTH_LEAP) &&
  603. !wpa_dbus_dict_string_array_add_element(
  604. &iter_array, "LEAP")) ||
  605. !wpa_dbus_dict_end_string_array(&iter_dict,
  606. &iter_dict_entry,
  607. &iter_dict_val,
  608. &iter_array))
  609. goto error;
  610. }
  611. if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
  612. goto error;
  613. return reply;
  614. error:
  615. if (reply)
  616. dbus_message_unref(reply);
  617. return dbus_message_new_error(
  618. message, WPAS_ERROR_INTERNAL_ERROR,
  619. "an internal error occurred returning interface capabilities.");
  620. }
  621. /**
  622. * wpas_dbus_iface_add_network - Add a new configured network
  623. * @message: Pointer to incoming dbus message
  624. * @wpa_s: wpa_supplicant structure for a network interface
  625. * Returns: A dbus message containing the object path of the new network
  626. *
  627. * Handler function for "addNetwork" method call of a network interface.
  628. */
  629. DBusMessage * wpas_dbus_iface_add_network(DBusMessage *message,
  630. struct wpa_supplicant *wpa_s)
  631. {
  632. DBusMessage *reply = NULL;
  633. struct wpa_ssid *ssid;
  634. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *path = path_buf;
  635. ssid = wpa_config_add_network(wpa_s->conf);
  636. if (ssid == NULL) {
  637. reply = dbus_message_new_error(
  638. message, WPAS_ERROR_ADD_NETWORK_ERROR,
  639. "wpa_supplicant could not add a network on this interface.");
  640. goto out;
  641. }
  642. wpas_notify_network_added(wpa_s, ssid);
  643. ssid->disabled = 1;
  644. wpa_config_set_network_defaults(ssid);
  645. /* Construct the object path for this network. */
  646. os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  647. "%s/" WPAS_DBUS_NETWORKS_PART "/%d",
  648. wpa_s->dbus_path, ssid->id);
  649. reply = dbus_message_new_method_return(message);
  650. dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
  651. &path, DBUS_TYPE_INVALID);
  652. out:
  653. return reply;
  654. }
  655. /**
  656. * wpas_dbus_iface_remove_network - Remove a configured network
  657. * @message: Pointer to incoming dbus message
  658. * @wpa_s: wpa_supplicant structure for a network interface
  659. * Returns: A dbus message containing a UINT32 indicating success (1) or
  660. * failure (0)
  661. *
  662. * Handler function for "removeNetwork" method call of a network interface.
  663. */
  664. DBusMessage * wpas_dbus_iface_remove_network(DBusMessage *message,
  665. struct wpa_supplicant *wpa_s)
  666. {
  667. DBusMessage *reply = NULL;
  668. const char *op;
  669. char *iface = NULL, *net_id = NULL;
  670. int id;
  671. struct wpa_ssid *ssid;
  672. if (!dbus_message_get_args(message, NULL,
  673. DBUS_TYPE_OBJECT_PATH, &op,
  674. DBUS_TYPE_INVALID)) {
  675. reply = wpas_dbus_new_invalid_opts_error(message, NULL);
  676. goto out;
  677. }
  678. /* Extract the network ID */
  679. iface = wpas_dbus_decompose_object_path(op, &net_id, NULL);
  680. if (iface == NULL || net_id == NULL) {
  681. reply = wpas_dbus_new_invalid_network_error(message);
  682. goto out;
  683. }
  684. /* Ensure the network is actually a child of this interface */
  685. if (os_strcmp(iface, wpa_s->dbus_path) != 0) {
  686. reply = wpas_dbus_new_invalid_network_error(message);
  687. goto out;
  688. }
  689. id = strtoul(net_id, NULL, 10);
  690. ssid = wpa_config_get_network(wpa_s->conf, id);
  691. if (ssid == NULL) {
  692. reply = wpas_dbus_new_invalid_network_error(message);
  693. goto out;
  694. }
  695. wpas_notify_network_removed(wpa_s, ssid);
  696. if (ssid == wpa_s->current_ssid)
  697. wpa_supplicant_deauthenticate(wpa_s,
  698. WLAN_REASON_DEAUTH_LEAVING);
  699. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  700. reply = dbus_message_new_error(
  701. message, WPAS_ERROR_REMOVE_NETWORK_ERROR,
  702. "error removing the specified on this interface.");
  703. goto out;
  704. }
  705. reply = wpas_dbus_new_success_reply(message);
  706. out:
  707. os_free(iface);
  708. os_free(net_id);
  709. return reply;
  710. }
  711. static const char const *dont_quote[] = {
  712. "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
  713. "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
  714. "bssid", NULL
  715. };
  716. static dbus_bool_t should_quote_opt(const char *key)
  717. {
  718. int i = 0;
  719. while (dont_quote[i] != NULL) {
  720. if (os_strcmp(key, dont_quote[i]) == 0)
  721. return FALSE;
  722. i++;
  723. }
  724. return TRUE;
  725. }
  726. /**
  727. * wpas_dbus_iface_set_network - Set options for a configured network
  728. * @message: Pointer to incoming dbus message
  729. * @wpa_s: wpa_supplicant structure for a network interface
  730. * @ssid: wpa_ssid structure for a configured network
  731. * Returns: a dbus message containing a UINT32 indicating success (1) or
  732. * failure (0)
  733. *
  734. * Handler function for "set" method call of a configured network.
  735. */
  736. DBusMessage * wpas_dbus_iface_set_network(DBusMessage *message,
  737. struct wpa_supplicant *wpa_s,
  738. struct wpa_ssid *ssid)
  739. {
  740. DBusMessage *reply = NULL;
  741. struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
  742. DBusMessageIter iter, iter_dict;
  743. dbus_message_iter_init(message, &iter);
  744. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL)) {
  745. reply = wpas_dbus_new_invalid_opts_error(message, NULL);
  746. goto out;
  747. }
  748. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  749. char *value = NULL;
  750. size_t size = 50;
  751. int ret;
  752. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
  753. reply = wpas_dbus_new_invalid_opts_error(message,
  754. NULL);
  755. goto out;
  756. }
  757. /* Type conversions, since wpa_supplicant wants strings */
  758. if (entry.type == DBUS_TYPE_ARRAY &&
  759. entry.array_type == DBUS_TYPE_BYTE) {
  760. if (entry.array_len <= 0)
  761. goto error;
  762. size = entry.array_len * 2 + 1;
  763. value = os_zalloc(size);
  764. if (value == NULL)
  765. goto error;
  766. ret = wpa_snprintf_hex(value, size,
  767. (u8 *) entry.bytearray_value,
  768. entry.array_len);
  769. if (ret <= 0)
  770. goto error;
  771. } else if (entry.type == DBUS_TYPE_STRING) {
  772. if (should_quote_opt(entry.key)) {
  773. size = os_strlen(entry.str_value);
  774. /* Zero-length option check */
  775. if (size <= 0)
  776. goto error;
  777. size += 3; /* For quotes and terminator */
  778. value = os_zalloc(size);
  779. if (value == NULL)
  780. goto error;
  781. ret = os_snprintf(value, size, "\"%s\"",
  782. entry.str_value);
  783. if (os_snprintf_error(size, ret))
  784. goto error;
  785. } else {
  786. value = os_strdup(entry.str_value);
  787. if (value == NULL)
  788. goto error;
  789. }
  790. } else if (entry.type == DBUS_TYPE_UINT32) {
  791. value = os_zalloc(size);
  792. if (value == NULL)
  793. goto error;
  794. ret = os_snprintf(value, size, "%u",
  795. entry.uint32_value);
  796. if (os_snprintf_error(size, ret))
  797. goto error;
  798. } else if (entry.type == DBUS_TYPE_INT32) {
  799. value = os_zalloc(size);
  800. if (value == NULL)
  801. goto error;
  802. ret = os_snprintf(value, size, "%d",
  803. entry.int32_value);
  804. if (os_snprintf_error(size, ret))
  805. goto error;
  806. } else
  807. goto error;
  808. if (wpa_config_set(ssid, entry.key, value, 0) < 0)
  809. goto error;
  810. if ((os_strcmp(entry.key, "psk") == 0 &&
  811. value[0] == '"' && ssid->ssid_len) ||
  812. (os_strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
  813. wpa_config_update_psk(ssid);
  814. else if (os_strcmp(entry.key, "priority") == 0)
  815. wpa_config_update_prio_list(wpa_s->conf);
  816. os_free(value);
  817. wpa_dbus_dict_entry_clear(&entry);
  818. continue;
  819. error:
  820. os_free(value);
  821. reply = wpas_dbus_new_invalid_opts_error(message, entry.key);
  822. wpa_dbus_dict_entry_clear(&entry);
  823. break;
  824. }
  825. if (!reply)
  826. reply = wpas_dbus_new_success_reply(message);
  827. out:
  828. return reply;
  829. }
  830. /**
  831. * wpas_dbus_iface_enable_network - Mark a configured network as enabled
  832. * @message: Pointer to incoming dbus message
  833. * @wpa_s: wpa_supplicant structure for a network interface
  834. * @ssid: wpa_ssid structure for a configured network
  835. * Returns: A dbus message containing a UINT32 indicating success (1) or
  836. * failure (0)
  837. *
  838. * Handler function for "enable" method call of a configured network.
  839. */
  840. DBusMessage * wpas_dbus_iface_enable_network(DBusMessage *message,
  841. struct wpa_supplicant *wpa_s,
  842. struct wpa_ssid *ssid)
  843. {
  844. wpa_supplicant_enable_network(wpa_s, ssid);
  845. return wpas_dbus_new_success_reply(message);
  846. }
  847. /**
  848. * wpas_dbus_iface_disable_network - Mark a configured network as disabled
  849. * @message: Pointer to incoming dbus message
  850. * @wpa_s: wpa_supplicant structure for a network interface
  851. * @ssid: wpa_ssid structure for a configured network
  852. * Returns: A dbus message containing a UINT32 indicating success (1) or
  853. * failure (0)
  854. *
  855. * Handler function for "disable" method call of a configured network.
  856. */
  857. DBusMessage * wpas_dbus_iface_disable_network(DBusMessage *message,
  858. struct wpa_supplicant *wpa_s,
  859. struct wpa_ssid *ssid)
  860. {
  861. wpa_supplicant_disable_network(wpa_s, ssid);
  862. return wpas_dbus_new_success_reply(message);
  863. }
  864. /**
  865. * wpas_dbus_iface_select_network - Attempt association with a configured network
  866. * @message: Pointer to incoming dbus message
  867. * @wpa_s: wpa_supplicant structure for a network interface
  868. * Returns: A dbus message containing a UINT32 indicating success (1) or
  869. * failure (0)
  870. *
  871. * Handler function for "selectNetwork" method call of network interface.
  872. */
  873. DBusMessage * wpas_dbus_iface_select_network(DBusMessage *message,
  874. struct wpa_supplicant *wpa_s)
  875. {
  876. DBusMessage *reply = NULL;
  877. const char *op;
  878. struct wpa_ssid *ssid;
  879. char *iface_obj_path = NULL;
  880. char *network = NULL;
  881. if (os_strlen(dbus_message_get_signature(message)) == 0) {
  882. /* Any network */
  883. ssid = NULL;
  884. } else {
  885. int nid;
  886. if (!dbus_message_get_args(message, NULL,
  887. DBUS_TYPE_OBJECT_PATH, &op,
  888. DBUS_TYPE_INVALID)) {
  889. reply = wpas_dbus_new_invalid_opts_error(message,
  890. NULL);
  891. goto out;
  892. }
  893. /* Extract the network number */
  894. iface_obj_path = wpas_dbus_decompose_object_path(op,
  895. &network,
  896. NULL);
  897. if (iface_obj_path == NULL) {
  898. reply = wpas_dbus_new_invalid_iface_error(message);
  899. goto out;
  900. }
  901. /* Ensure the object path really points to this interface */
  902. if (network == NULL ||
  903. os_strcmp(iface_obj_path, wpa_s->dbus_path) != 0) {
  904. reply = wpas_dbus_new_invalid_network_error(message);
  905. goto out;
  906. }
  907. nid = strtoul(network, NULL, 10);
  908. if (errno == EINVAL) {
  909. reply = wpas_dbus_new_invalid_network_error(message);
  910. goto out;
  911. }
  912. ssid = wpa_config_get_network(wpa_s->conf, nid);
  913. if (ssid == NULL) {
  914. reply = wpas_dbus_new_invalid_network_error(message);
  915. goto out;
  916. }
  917. }
  918. /* Finally, associate with the network */
  919. wpa_supplicant_select_network(wpa_s, ssid);
  920. reply = wpas_dbus_new_success_reply(message);
  921. out:
  922. os_free(iface_obj_path);
  923. os_free(network);
  924. return reply;
  925. }
  926. /**
  927. * wpas_dbus_iface_disconnect - Terminate the current connection
  928. * @message: Pointer to incoming dbus message
  929. * @wpa_s: wpa_supplicant structure for a network interface
  930. * Returns: A dbus message containing a UINT32 indicating success (1) or
  931. * failure (0)
  932. *
  933. * Handler function for "disconnect" method call of network interface.
  934. */
  935. DBusMessage * wpas_dbus_iface_disconnect(DBusMessage *message,
  936. struct wpa_supplicant *wpa_s)
  937. {
  938. wpa_s->disconnected = 1;
  939. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  940. return wpas_dbus_new_success_reply(message);
  941. }
  942. /**
  943. * wpas_dbus_iface_set_ap_scan - Control roaming mode
  944. * @message: Pointer to incoming dbus message
  945. * @wpa_s: wpa_supplicant structure for a network interface
  946. * Returns: A dbus message containing a UINT32 indicating success (1) or
  947. * failure (0)
  948. *
  949. * Handler function for "setAPScan" method call.
  950. */
  951. DBusMessage * wpas_dbus_iface_set_ap_scan(DBusMessage *message,
  952. struct wpa_supplicant *wpa_s)
  953. {
  954. DBusMessage *reply = NULL;
  955. dbus_uint32_t ap_scan = 1;
  956. if (!dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &ap_scan,
  957. DBUS_TYPE_INVALID)) {
  958. reply = wpas_dbus_new_invalid_opts_error(message, NULL);
  959. goto out;
  960. }
  961. if (wpa_supplicant_set_ap_scan(wpa_s, ap_scan)) {
  962. reply = wpas_dbus_new_invalid_opts_error(message, NULL);
  963. goto out;
  964. }
  965. reply = wpas_dbus_new_success_reply(message);
  966. out:
  967. return reply;
  968. }
  969. /**
  970. * wpas_dbus_iface_set_smartcard_modules - Set smartcard related module paths
  971. * @message: Pointer to incoming dbus message
  972. * @wpa_s: wpa_supplicant structure for a network interface
  973. * Returns: A dbus message containing a UINT32 indicating success (1) or
  974. * failure (0)
  975. *
  976. * Handler function for "setSmartcardModules" method call.
  977. */
  978. DBusMessage * wpas_dbus_iface_set_smartcard_modules(
  979. DBusMessage *message, struct wpa_supplicant *wpa_s)
  980. {
  981. DBusMessageIter iter, iter_dict;
  982. char *opensc_engine_path = NULL;
  983. char *pkcs11_engine_path = NULL;
  984. char *pkcs11_module_path = NULL;
  985. struct wpa_dbus_dict_entry entry;
  986. if (!dbus_message_iter_init(message, &iter))
  987. goto error;
  988. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  989. goto error;
  990. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  991. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  992. goto error;
  993. if (!strcmp(entry.key, "opensc_engine_path") &&
  994. entry.type == DBUS_TYPE_STRING) {
  995. os_free(opensc_engine_path);
  996. opensc_engine_path = os_strdup(entry.str_value);
  997. wpa_dbus_dict_entry_clear(&entry);
  998. if (opensc_engine_path == NULL)
  999. goto error;
  1000. } else if (!strcmp(entry.key, "pkcs11_engine_path") &&
  1001. entry.type == DBUS_TYPE_STRING) {
  1002. os_free(pkcs11_engine_path);
  1003. pkcs11_engine_path = os_strdup(entry.str_value);
  1004. wpa_dbus_dict_entry_clear(&entry);
  1005. if (pkcs11_engine_path == NULL)
  1006. goto error;
  1007. } else if (!strcmp(entry.key, "pkcs11_module_path") &&
  1008. entry.type == DBUS_TYPE_STRING) {
  1009. os_free(pkcs11_module_path);
  1010. pkcs11_module_path = os_strdup(entry.str_value);
  1011. wpa_dbus_dict_entry_clear(&entry);
  1012. if (pkcs11_module_path == NULL)
  1013. goto error;
  1014. } else {
  1015. wpa_dbus_dict_entry_clear(&entry);
  1016. goto error;
  1017. }
  1018. }
  1019. os_free(wpa_s->conf->opensc_engine_path);
  1020. wpa_s->conf->opensc_engine_path = opensc_engine_path;
  1021. os_free(wpa_s->conf->pkcs11_engine_path);
  1022. wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path;
  1023. os_free(wpa_s->conf->pkcs11_module_path);
  1024. wpa_s->conf->pkcs11_module_path = pkcs11_module_path;
  1025. wpa_sm_set_eapol(wpa_s->wpa, NULL);
  1026. eapol_sm_deinit(wpa_s->eapol);
  1027. wpa_s->eapol = NULL;
  1028. wpa_supplicant_init_eapol(wpa_s);
  1029. wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
  1030. return wpas_dbus_new_success_reply(message);
  1031. error:
  1032. os_free(opensc_engine_path);
  1033. os_free(pkcs11_engine_path);
  1034. os_free(pkcs11_module_path);
  1035. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1036. }
  1037. /**
  1038. * wpas_dbus_iface_get_state - Get interface state
  1039. * @message: Pointer to incoming dbus message
  1040. * @wpa_s: wpa_supplicant structure for a network interface
  1041. * Returns: A dbus message containing a STRING representing the current
  1042. * interface state
  1043. *
  1044. * Handler function for "state" method call.
  1045. */
  1046. DBusMessage * wpas_dbus_iface_get_state(DBusMessage *message,
  1047. struct wpa_supplicant *wpa_s)
  1048. {
  1049. DBusMessage *reply = NULL;
  1050. const char *str_state;
  1051. reply = dbus_message_new_method_return(message);
  1052. if (reply != NULL) {
  1053. str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
  1054. dbus_message_append_args(reply, DBUS_TYPE_STRING, &str_state,
  1055. DBUS_TYPE_INVALID);
  1056. }
  1057. return reply;
  1058. }
  1059. /**
  1060. * wpas_dbus_iface_get_scanning - Get interface scanning state
  1061. * @message: Pointer to incoming dbus message
  1062. * @wpa_s: wpa_supplicant structure for a network interface
  1063. * Returns: A dbus message containing whether the interface is scanning
  1064. *
  1065. * Handler function for "scanning" method call.
  1066. */
  1067. DBusMessage * wpas_dbus_iface_get_scanning(DBusMessage *message,
  1068. struct wpa_supplicant *wpa_s)
  1069. {
  1070. DBusMessage *reply = NULL;
  1071. dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
  1072. reply = dbus_message_new_method_return(message);
  1073. if (reply != NULL) {
  1074. dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &scanning,
  1075. DBUS_TYPE_INVALID);
  1076. } else {
  1077. wpa_printf(MSG_ERROR,
  1078. "dbus: Not enough memory to return scanning state");
  1079. }
  1080. return reply;
  1081. }
  1082. #ifndef CONFIG_NO_CONFIG_BLOBS
  1083. /**
  1084. * wpas_dbus_iface_set_blobs - Store named binary blobs (ie, for certificates)
  1085. * @message: Pointer to incoming dbus message
  1086. * @wpa_s: %wpa_supplicant data structure
  1087. * Returns: A dbus message containing a UINT32 indicating success (1) or
  1088. * failure (0)
  1089. *
  1090. * Asks wpa_supplicant to internally store a one or more binary blobs.
  1091. */
  1092. DBusMessage * wpas_dbus_iface_set_blobs(DBusMessage *message,
  1093. struct wpa_supplicant *wpa_s)
  1094. {
  1095. DBusMessage *reply = NULL;
  1096. struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
  1097. DBusMessageIter iter, iter_dict;
  1098. dbus_message_iter_init(message, &iter);
  1099. if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
  1100. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1101. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1102. struct wpa_config_blob *blob;
  1103. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
  1104. reply = wpas_dbus_new_invalid_opts_error(message,
  1105. NULL);
  1106. break;
  1107. }
  1108. if (entry.type != DBUS_TYPE_ARRAY ||
  1109. entry.array_type != DBUS_TYPE_BYTE) {
  1110. reply = wpas_dbus_new_invalid_opts_error(
  1111. message, "Byte array expected.");
  1112. break;
  1113. }
  1114. if ((entry.array_len <= 0) || (entry.array_len > 65536) ||
  1115. !strlen(entry.key)) {
  1116. reply = wpas_dbus_new_invalid_opts_error(
  1117. message, "Invalid array size.");
  1118. break;
  1119. }
  1120. blob = os_zalloc(sizeof(*blob));
  1121. if (blob == NULL) {
  1122. reply = dbus_message_new_error(
  1123. message, WPAS_ERROR_ADD_ERROR,
  1124. "Not enough memory to add blob.");
  1125. break;
  1126. }
  1127. blob->data = os_zalloc(entry.array_len);
  1128. if (blob->data == NULL) {
  1129. reply = dbus_message_new_error(
  1130. message, WPAS_ERROR_ADD_ERROR,
  1131. "Not enough memory to add blob data.");
  1132. os_free(blob);
  1133. break;
  1134. }
  1135. blob->name = os_strdup(entry.key);
  1136. blob->len = entry.array_len;
  1137. os_memcpy(blob->data, (u8 *) entry.bytearray_value,
  1138. entry.array_len);
  1139. if (blob->name == NULL) {
  1140. wpa_config_free_blob(blob);
  1141. reply = dbus_message_new_error(
  1142. message, WPAS_ERROR_ADD_ERROR,
  1143. "Error adding blob.");
  1144. break;
  1145. }
  1146. /* Success */
  1147. if (!wpa_config_remove_blob(wpa_s->conf, blob->name))
  1148. wpas_notify_blob_removed(wpa_s, blob->name);
  1149. wpa_config_set_blob(wpa_s->conf, blob);
  1150. wpas_notify_blob_added(wpa_s, blob->name);
  1151. wpa_dbus_dict_entry_clear(&entry);
  1152. }
  1153. wpa_dbus_dict_entry_clear(&entry);
  1154. return reply ? reply : wpas_dbus_new_success_reply(message);
  1155. }
  1156. /**
  1157. * wpas_dbus_iface_remove_blob - Remove named binary blobs
  1158. * @message: Pointer to incoming dbus message
  1159. * @wpa_s: %wpa_supplicant data structure
  1160. * Returns: A dbus message containing a UINT32 indicating success (1) or
  1161. * failure (0)
  1162. *
  1163. * Asks wpa_supplicant to remove one or more previously stored binary blobs.
  1164. */
  1165. DBusMessage * wpas_dbus_iface_remove_blobs(DBusMessage *message,
  1166. struct wpa_supplicant *wpa_s)
  1167. {
  1168. DBusMessageIter iter, array;
  1169. char *err_msg = NULL;
  1170. dbus_message_iter_init(message, &iter);
  1171. if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
  1172. dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
  1173. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1174. dbus_message_iter_recurse(&iter, &array);
  1175. while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
  1176. const char *name;
  1177. dbus_message_iter_get_basic(&array, &name);
  1178. if (!os_strlen(name))
  1179. err_msg = "Invalid blob name.";
  1180. else if (wpa_config_remove_blob(wpa_s->conf, name) != 0)
  1181. err_msg = "Error removing blob.";
  1182. else
  1183. wpas_notify_blob_removed(wpa_s, name);
  1184. dbus_message_iter_next(&array);
  1185. }
  1186. if (err_msg)
  1187. return dbus_message_new_error(message, WPAS_ERROR_REMOVE_ERROR,
  1188. err_msg);
  1189. return wpas_dbus_new_success_reply(message);
  1190. }
  1191. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1192. /**
  1193. * wpas_dbus_iface_flush - Clear BSS of old or all inactive entries
  1194. * @message: Pointer to incoming dbus message
  1195. * @wpa_s: %wpa_supplicant data structure
  1196. * Returns: a dbus message containing a UINT32 indicating success (1) or
  1197. * failure (0), or returns a dbus error message with more information
  1198. *
  1199. * Handler function for "flush" method call. Handles requests for an
  1200. * interface with an optional "age" parameter that specifies the minimum
  1201. * age of a BSS to be flushed.
  1202. */
  1203. DBusMessage * wpas_dbus_iface_flush(DBusMessage *message,
  1204. struct wpa_supplicant *wpa_s)
  1205. {
  1206. int flush_age = 0;
  1207. if (os_strlen(dbus_message_get_signature(message)) != 0 &&
  1208. !dbus_message_get_args(message, NULL,
  1209. DBUS_TYPE_INT32, &flush_age,
  1210. DBUS_TYPE_INVALID)) {
  1211. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1212. }
  1213. if (flush_age == 0)
  1214. wpa_bss_flush(wpa_s);
  1215. else
  1216. wpa_bss_flush_by_age(wpa_s, flush_age);
  1217. return wpas_dbus_new_success_reply(message);
  1218. }