dbus_old_handlers.c 41 KB

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