dbus_old_handlers.c 41 KB

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