dbus_handlers.c 41 KB

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