dbus_old_handlers.c 40 KB

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