ctrl_iface_dbus_handlers.c 44 KB

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