ctrl_iface_dbus_handlers.c 39 KB

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