ctrl_iface_dbus_handlers.c 39 KB

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