dbus_old_handlers.c 40 KB

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