ctrl_iface_dbus_handlers.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  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 "common/ieee802_11_defs.h"
  25. #include "wpas_glue.h"
  26. #include "eapol_supp/eapol_supp_sm.h"
  27. #include "wps_supplicant.h"
  28. #include "rsn_supp/wpa.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. static 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. static 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. static void wpas_dbus_free_wpa_interface(struct wpa_interface *iface)
  69. {
  70. os_free((char *) iface->driver);
  71. os_free((char *) iface->driver_param);
  72. os_free((char *) iface->confname);
  73. os_free((char *) iface->bridge_ifname);
  74. }
  75. /**
  76. * wpas_dbus_global_add_interface - Request registration of a network interface
  77. * @message: Pointer to incoming dbus message
  78. * @global: %wpa_supplicant global data structure
  79. * Returns: The object path of the new interface object,
  80. * or a dbus error message with more information
  81. *
  82. * Handler function for "addInterface" method call. Handles requests
  83. * by dbus clients to register a network interface that wpa_supplicant
  84. * will manage.
  85. */
  86. DBusMessage * wpas_dbus_global_add_interface(DBusMessage *message,
  87. struct wpa_global *global)
  88. {
  89. struct wpa_interface iface;
  90. char *ifname = NULL;
  91. DBusMessage *reply = NULL;
  92. DBusMessageIter iter;
  93. os_memset(&iface, 0, sizeof(iface));
  94. dbus_message_iter_init(message, &iter);
  95. /* First argument: interface name (DBUS_TYPE_STRING)
  96. * Required; must be non-zero length
  97. */
  98. if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
  99. goto error;
  100. dbus_message_iter_get_basic(&iter, &ifname);
  101. if (!os_strlen(ifname))
  102. goto error;
  103. iface.ifname = ifname;
  104. /* Second argument: dict of options */
  105. if (dbus_message_iter_next(&iter)) {
  106. DBusMessageIter iter_dict;
  107. struct wpa_dbus_dict_entry entry;
  108. if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
  109. goto error;
  110. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  111. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  112. goto error;
  113. if (!strcmp(entry.key, "driver") &&
  114. (entry.type == DBUS_TYPE_STRING)) {
  115. iface.driver = strdup(entry.str_value);
  116. if (iface.driver == NULL)
  117. goto error;
  118. } else if (!strcmp(entry.key, "driver-params") &&
  119. (entry.type == DBUS_TYPE_STRING)) {
  120. iface.driver_param = strdup(entry.str_value);
  121. if (iface.driver_param == NULL)
  122. goto error;
  123. } else if (!strcmp(entry.key, "config-file") &&
  124. (entry.type == DBUS_TYPE_STRING)) {
  125. iface.confname = strdup(entry.str_value);
  126. if (iface.confname == NULL)
  127. goto error;
  128. } else if (!strcmp(entry.key, "bridge-ifname") &&
  129. (entry.type == DBUS_TYPE_STRING)) {
  130. iface.bridge_ifname = strdup(entry.str_value);
  131. if (iface.bridge_ifname == NULL)
  132. goto error;
  133. } else {
  134. wpa_dbus_dict_entry_clear(&entry);
  135. goto error;
  136. }
  137. wpa_dbus_dict_entry_clear(&entry);
  138. }
  139. }
  140. /*
  141. * Try to get the wpa_supplicant record for this iface, return
  142. * an error if we already control it.
  143. */
  144. if (wpa_supplicant_get_iface(global, iface.ifname) != NULL) {
  145. reply = dbus_message_new_error(message,
  146. WPAS_ERROR_EXISTS_ERROR,
  147. "wpa_supplicant already "
  148. "controls this interface.");
  149. } else {
  150. struct wpa_supplicant *wpa_s;
  151. /* Otherwise, have wpa_supplicant attach to it. */
  152. if ((wpa_s = wpa_supplicant_add_iface(global, &iface))) {
  153. const char *path = wpa_supplicant_get_dbus_path(wpa_s);
  154. reply = dbus_message_new_method_return(message);
  155. dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
  156. &path, DBUS_TYPE_INVALID);
  157. } else {
  158. reply = dbus_message_new_error(message,
  159. WPAS_ERROR_ADD_ERROR,
  160. "wpa_supplicant "
  161. "couldn't grab this "
  162. "interface.");
  163. }
  164. }
  165. wpas_dbus_free_wpa_interface(&iface);
  166. return reply;
  167. error:
  168. wpas_dbus_free_wpa_interface(&iface);
  169. return wpas_dbus_new_invalid_opts_error(message, NULL);
  170. }
  171. /**
  172. * wpas_dbus_global_remove_interface - Request deregistration of an interface
  173. * @message: Pointer to incoming dbus message
  174. * @global: wpa_supplicant global data structure
  175. * Returns: a dbus message containing a UINT32 indicating success (1) or
  176. * failure (0), or returns a dbus error message with more information
  177. *
  178. * Handler function for "removeInterface" method call. Handles requests
  179. * by dbus clients to deregister a network interface that wpa_supplicant
  180. * currently manages.
  181. */
  182. DBusMessage * wpas_dbus_global_remove_interface(DBusMessage *message,
  183. struct wpa_global *global)
  184. {
  185. struct wpa_supplicant *wpa_s;
  186. char *path;
  187. DBusMessage *reply = NULL;
  188. if (!dbus_message_get_args(message, NULL,
  189. DBUS_TYPE_OBJECT_PATH, &path,
  190. DBUS_TYPE_INVALID)) {
  191. reply = wpas_dbus_new_invalid_opts_error(message, NULL);
  192. goto out;
  193. }
  194. wpa_s = wpa_supplicant_get_iface_by_dbus_path(global, path);
  195. if (wpa_s == NULL) {
  196. reply = wpas_dbus_new_invalid_iface_error(message);
  197. goto out;
  198. }
  199. if (!wpa_supplicant_remove_iface(global, wpa_s)) {
  200. reply = wpas_dbus_new_success_reply(message);
  201. } else {
  202. reply = dbus_message_new_error(message,
  203. WPAS_ERROR_REMOVE_ERROR,
  204. "wpa_supplicant couldn't "
  205. "remove this interface.");
  206. }
  207. out:
  208. return reply;
  209. }
  210. /**
  211. * wpas_dbus_global_get_interface - Get the object path for an interface name
  212. * @message: Pointer to incoming dbus message
  213. * @global: %wpa_supplicant global data structure
  214. * Returns: The object path of the interface object,
  215. * or a dbus error message with more information
  216. *
  217. * Handler function for "getInterface" method call. Handles requests
  218. * by dbus clients for the object path of an specific network interface.
  219. */
  220. DBusMessage * wpas_dbus_global_get_interface(DBusMessage *message,
  221. struct wpa_global *global)
  222. {
  223. DBusMessage *reply = NULL;
  224. const char *ifname;
  225. const char *path;
  226. struct wpa_supplicant *wpa_s;
  227. if (!dbus_message_get_args(message, NULL,
  228. DBUS_TYPE_STRING, &ifname,
  229. DBUS_TYPE_INVALID)) {
  230. reply = wpas_dbus_new_invalid_opts_error(message, NULL);
  231. goto out;
  232. }
  233. wpa_s = wpa_supplicant_get_iface(global, ifname);
  234. if (wpa_s == NULL) {
  235. reply = wpas_dbus_new_invalid_iface_error(message);
  236. goto out;
  237. }
  238. path = wpa_supplicant_get_dbus_path(wpa_s);
  239. if (path == NULL) {
  240. reply = dbus_message_new_error(message,
  241. WPAS_ERROR_INTERNAL_ERROR,
  242. "an internal error occurred "
  243. "getting the interface.");
  244. goto out;
  245. }
  246. reply = dbus_message_new_method_return(message);
  247. dbus_message_append_args(reply,
  248. DBUS_TYPE_OBJECT_PATH, &path,
  249. DBUS_TYPE_INVALID);
  250. out:
  251. return reply;
  252. }
  253. /**
  254. * wpas_dbus_global_set_debugparams- Set the debug params
  255. * @message: Pointer to incoming dbus message
  256. * @global: %wpa_supplicant global data structure
  257. * Returns: a dbus message containing a UINT32 indicating success (1) or
  258. * failure (0), or returns a dbus error message with more information
  259. *
  260. * Handler function for "setDebugParams" method call. Handles requests
  261. * by dbus clients for the object path of an specific network interface.
  262. */
  263. DBusMessage * wpas_dbus_global_set_debugparams(DBusMessage *message,
  264. struct wpa_global *global)
  265. {
  266. DBusMessage *reply = NULL;
  267. int debug_level;
  268. dbus_bool_t debug_timestamp;
  269. dbus_bool_t debug_show_keys;
  270. if (!dbus_message_get_args(message, NULL,
  271. DBUS_TYPE_INT32, &debug_level,
  272. DBUS_TYPE_BOOLEAN, &debug_timestamp,
  273. DBUS_TYPE_BOOLEAN, &debug_show_keys,
  274. DBUS_TYPE_INVALID)) {
  275. return wpas_dbus_new_invalid_opts_error(message, NULL);
  276. }
  277. if (wpa_supplicant_set_debug_params(global, debug_level,
  278. debug_timestamp ? 1 : 0,
  279. debug_show_keys ? 1 : 0)) {
  280. return wpas_dbus_new_invalid_opts_error(message, NULL);
  281. }
  282. reply = wpas_dbus_new_success_reply(message);
  283. return reply;
  284. }
  285. /**
  286. * wpas_dbus_iface_scan - Request a wireless scan on an interface
  287. * @message: Pointer to incoming dbus message
  288. * @wpa_s: wpa_supplicant structure for a network interface
  289. * Returns: a dbus message containing a UINT32 indicating success (1) or
  290. * failure (0)
  291. *
  292. * Handler function for "scan" method call of a network device. Requests
  293. * that wpa_supplicant perform a wireless scan as soon as possible
  294. * on a particular wireless interface.
  295. */
  296. DBusMessage * wpas_dbus_iface_scan(DBusMessage *message,
  297. struct wpa_supplicant *wpa_s)
  298. {
  299. wpa_s->scan_req = 2;
  300. wpa_supplicant_req_scan(wpa_s, 0, 0);
  301. return wpas_dbus_new_success_reply(message);
  302. }
  303. /**
  304. * wpas_dbus_iface_scan_results - Get the results of a recent scan request
  305. * @message: Pointer to incoming dbus message
  306. * @wpa_s: wpa_supplicant structure for a network interface
  307. * Returns: a dbus message containing a dbus array of objects paths, or returns
  308. * a dbus error message if not scan results could be found
  309. *
  310. * Handler function for "scanResults" method call of a network device. Returns
  311. * a dbus message containing the object paths of wireless networks found.
  312. */
  313. DBusMessage * wpas_dbus_iface_scan_results(DBusMessage *message,
  314. struct wpa_supplicant *wpa_s)
  315. {
  316. DBusMessage *reply = NULL;
  317. DBusMessageIter iter;
  318. DBusMessageIter sub_iter;
  319. size_t i;
  320. /* Ensure we've actually got scan results to return */
  321. if (wpa_s->scan_res == NULL &&
  322. wpa_supplicant_get_scan_results(wpa_s) < 0) {
  323. reply = dbus_message_new_error(message, WPAS_ERROR_SCAN_ERROR,
  324. "An error ocurred getting scan "
  325. "results.");
  326. goto out;
  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. perror("wpas_dbus_iface_scan_results[dbus]: out of "
  341. "memory.");
  342. wpa_printf(MSG_ERROR, "dbus control interface: not "
  343. "enough memory to send scan results "
  344. "signal.");
  345. break;
  346. }
  347. /* Construct the object path for this network. Note that ':'
  348. * is not a valid character in dbus object paths.
  349. */
  350. os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  351. "%s/" WPAS_DBUS_BSSIDS_PART "/"
  352. WPAS_DBUS_BSSID_FORMAT,
  353. wpa_supplicant_get_dbus_path(wpa_s),
  354. MAC2STR(res->bssid));
  355. dbus_message_iter_append_basic(&sub_iter,
  356. DBUS_TYPE_OBJECT_PATH, &path);
  357. os_free(path);
  358. }
  359. dbus_message_iter_close_container(&iter, &sub_iter);
  360. out:
  361. return reply;
  362. }
  363. /**
  364. * wpas_dbus_bssid_properties - Return the properties of a scanned network
  365. * @message: Pointer to incoming dbus message
  366. * @wpa_s: wpa_supplicant structure for a network interface
  367. * @res: wpa_supplicant scan result for which to get properties
  368. * Returns: a dbus message containing the properties for the requested network
  369. *
  370. * Handler function for "properties" method call of a scanned network.
  371. * Returns a dbus message containing the the properties.
  372. */
  373. DBusMessage * wpas_dbus_bssid_properties(DBusMessage *message,
  374. struct wpa_supplicant *wpa_s,
  375. struct wpa_scan_res *res)
  376. {
  377. DBusMessage *reply = NULL;
  378. DBusMessageIter iter, iter_dict;
  379. const u8 *ie;
  380. /* Dump the properties into a dbus message */
  381. reply = dbus_message_new_method_return(message);
  382. dbus_message_iter_init_append(reply, &iter);
  383. if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
  384. goto error;
  385. if (!wpa_dbus_dict_append_byte_array(&iter_dict, "bssid",
  386. (const char *) res->bssid,
  387. ETH_ALEN))
  388. goto error;
  389. ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
  390. if (ie) {
  391. if (!wpa_dbus_dict_append_byte_array(&iter_dict, "ssid",
  392. (const char *) (ie + 2),
  393. ie[1]))
  394. goto error;
  395. }
  396. ie = wpa_scan_get_vendor_ie(res, WPA_IE_VENDOR_TYPE);
  397. if (ie) {
  398. if (!wpa_dbus_dict_append_byte_array(&iter_dict, "wpaie",
  399. (const char *) ie,
  400. ie[1] + 2))
  401. goto error;
  402. }
  403. ie = wpa_scan_get_ie(res, WLAN_EID_RSN);
  404. if (ie) {
  405. if (!wpa_dbus_dict_append_byte_array(&iter_dict, "rsnie",
  406. (const char *) ie,
  407. ie[1] + 2))
  408. goto error;
  409. }
  410. ie = wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE);
  411. if (ie) {
  412. if (!wpa_dbus_dict_append_byte_array(&iter_dict, "wpsie",
  413. (const char *) ie,
  414. ie[1] + 2))
  415. goto error;
  416. }
  417. if (res->freq) {
  418. if (!wpa_dbus_dict_append_int32(&iter_dict, "frequency",
  419. res->freq))
  420. goto error;
  421. }
  422. if (!wpa_dbus_dict_append_uint16(&iter_dict, "capabilities",
  423. res->caps))
  424. goto error;
  425. if (!(res->flags & WPA_SCAN_QUAL_INVALID) &&
  426. !wpa_dbus_dict_append_int32(&iter_dict, "quality", res->qual))
  427. goto error;
  428. if (!(res->flags & WPA_SCAN_NOISE_INVALID) &&
  429. !wpa_dbus_dict_append_int32(&iter_dict, "noise", res->noise))
  430. goto error;
  431. if (!(res->flags & WPA_SCAN_LEVEL_INVALID) &&
  432. !wpa_dbus_dict_append_int32(&iter_dict, "level", res->level))
  433. goto error;
  434. if (!wpa_dbus_dict_append_int32(&iter_dict, "maxrate",
  435. wpa_scan_get_max_rate(res) * 500000))
  436. goto error;
  437. if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
  438. goto error;
  439. return reply;
  440. error:
  441. if (reply)
  442. dbus_message_unref(reply);
  443. return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
  444. "an internal error occurred returning "
  445. "BSSID properties.");
  446. }
  447. /**
  448. * wpas_dbus_iface_capabilities - Return interface capabilities
  449. * @message: Pointer to incoming dbus message
  450. * @wpa_s: wpa_supplicant structure for a network interface
  451. * Returns: A dbus message containing a dict of strings
  452. *
  453. * Handler function for "capabilities" method call of an interface.
  454. */
  455. DBusMessage * wpas_dbus_iface_capabilities(DBusMessage *message,
  456. struct wpa_supplicant *wpa_s)
  457. {
  458. DBusMessage *reply = NULL;
  459. struct wpa_driver_capa capa;
  460. int res;
  461. DBusMessageIter iter, iter_dict;
  462. char **eap_methods;
  463. size_t num_items;
  464. dbus_bool_t strict = FALSE;
  465. DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
  466. if (!dbus_message_get_args(message, NULL,
  467. DBUS_TYPE_BOOLEAN, &strict,
  468. DBUS_TYPE_INVALID))
  469. strict = FALSE;
  470. reply = dbus_message_new_method_return(message);
  471. dbus_message_iter_init_append(reply, &iter);
  472. if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
  473. goto error;
  474. /* EAP methods */
  475. eap_methods = eap_get_names_as_string_array(&num_items);
  476. if (eap_methods) {
  477. dbus_bool_t success = FALSE;
  478. size_t i = 0;
  479. success = wpa_dbus_dict_append_string_array(
  480. &iter_dict, "eap", (const char **) eap_methods,
  481. num_items);
  482. /* free returned method array */
  483. while (eap_methods[i])
  484. os_free(eap_methods[i++]);
  485. os_free(eap_methods);
  486. if (!success)
  487. goto error;
  488. }
  489. res = wpa_drv_get_capa(wpa_s, &capa);
  490. /***** pairwise cipher */
  491. if (res < 0) {
  492. if (!strict) {
  493. const char *args[] = {"CCMP", "TKIP", "NONE"};
  494. if (!wpa_dbus_dict_append_string_array(
  495. &iter_dict, "pairwise", args,
  496. sizeof(args) / sizeof(char*)))
  497. goto error;
  498. }
  499. } else {
  500. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "pairwise",
  501. &iter_dict_entry,
  502. &iter_dict_val,
  503. &iter_array))
  504. goto error;
  505. if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  506. if (!wpa_dbus_dict_string_array_add_element(
  507. &iter_array, "CCMP"))
  508. goto error;
  509. }
  510. if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  511. if (!wpa_dbus_dict_string_array_add_element(
  512. &iter_array, "TKIP"))
  513. goto error;
  514. }
  515. if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  516. if (!wpa_dbus_dict_string_array_add_element(
  517. &iter_array, "NONE"))
  518. goto error;
  519. }
  520. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  521. &iter_dict_entry,
  522. &iter_dict_val,
  523. &iter_array))
  524. goto error;
  525. }
  526. /***** group cipher */
  527. if (res < 0) {
  528. if (!strict) {
  529. const char *args[] = {
  530. "CCMP", "TKIP", "WEP104", "WEP40"
  531. };
  532. if (!wpa_dbus_dict_append_string_array(
  533. &iter_dict, "group", args,
  534. sizeof(args) / sizeof(char*)))
  535. goto error;
  536. }
  537. } else {
  538. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "group",
  539. &iter_dict_entry,
  540. &iter_dict_val,
  541. &iter_array))
  542. goto error;
  543. if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  544. if (!wpa_dbus_dict_string_array_add_element(
  545. &iter_array, "CCMP"))
  546. goto error;
  547. }
  548. if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  549. if (!wpa_dbus_dict_string_array_add_element(
  550. &iter_array, "TKIP"))
  551. goto error;
  552. }
  553. if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  554. if (!wpa_dbus_dict_string_array_add_element(
  555. &iter_array, "WEP104"))
  556. goto error;
  557. }
  558. if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  559. if (!wpa_dbus_dict_string_array_add_element(
  560. &iter_array, "WEP40"))
  561. goto error;
  562. }
  563. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  564. &iter_dict_entry,
  565. &iter_dict_val,
  566. &iter_array))
  567. goto error;
  568. }
  569. /***** key management */
  570. if (res < 0) {
  571. if (!strict) {
  572. const char *args[] = {
  573. "WPA-PSK", "WPA-EAP", "IEEE8021X", "WPA-NONE",
  574. "NONE"
  575. };
  576. if (!wpa_dbus_dict_append_string_array(
  577. &iter_dict, "key_mgmt", args,
  578. sizeof(args) / sizeof(char*)))
  579. goto error;
  580. }
  581. } else {
  582. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "key_mgmt",
  583. &iter_dict_entry,
  584. &iter_dict_val,
  585. &iter_array))
  586. goto error;
  587. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  588. "NONE"))
  589. goto error;
  590. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  591. "IEEE8021X"))
  592. goto error;
  593. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  594. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  595. if (!wpa_dbus_dict_string_array_add_element(
  596. &iter_array, "WPA-EAP"))
  597. goto error;
  598. }
  599. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  600. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  601. if (!wpa_dbus_dict_string_array_add_element(
  602. &iter_array, "WPA-PSK"))
  603. goto error;
  604. }
  605. if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  606. if (!wpa_dbus_dict_string_array_add_element(
  607. &iter_array, "WPA-NONE"))
  608. goto error;
  609. }
  610. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  611. &iter_dict_entry,
  612. &iter_dict_val,
  613. &iter_array))
  614. goto error;
  615. }
  616. /***** WPA protocol */
  617. if (res < 0) {
  618. if (!strict) {
  619. const char *args[] = { "RSN", "WPA" };
  620. if (!wpa_dbus_dict_append_string_array(
  621. &iter_dict, "proto", args,
  622. sizeof(args) / sizeof(char*)))
  623. goto error;
  624. }
  625. } else {
  626. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "proto",
  627. &iter_dict_entry,
  628. &iter_dict_val,
  629. &iter_array))
  630. goto error;
  631. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  632. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  633. if (!wpa_dbus_dict_string_array_add_element(
  634. &iter_array, "RSN"))
  635. goto error;
  636. }
  637. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  638. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  639. if (!wpa_dbus_dict_string_array_add_element(
  640. &iter_array, "WPA"))
  641. goto error;
  642. }
  643. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  644. &iter_dict_entry,
  645. &iter_dict_val,
  646. &iter_array))
  647. goto error;
  648. }
  649. /***** auth alg */
  650. if (res < 0) {
  651. if (!strict) {
  652. const char *args[] = { "OPEN", "SHARED", "LEAP" };
  653. if (!wpa_dbus_dict_append_string_array(
  654. &iter_dict, "auth_alg", args,
  655. sizeof(args) / sizeof(char*)))
  656. goto error;
  657. }
  658. } else {
  659. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "auth_alg",
  660. &iter_dict_entry,
  661. &iter_dict_val,
  662. &iter_array))
  663. goto error;
  664. if (capa.auth & (WPA_DRIVER_AUTH_OPEN)) {
  665. if (!wpa_dbus_dict_string_array_add_element(
  666. &iter_array, "OPEN"))
  667. goto error;
  668. }
  669. if (capa.auth & (WPA_DRIVER_AUTH_SHARED)) {
  670. if (!wpa_dbus_dict_string_array_add_element(
  671. &iter_array, "SHARED"))
  672. goto error;
  673. }
  674. if (capa.auth & (WPA_DRIVER_AUTH_LEAP)) {
  675. if (!wpa_dbus_dict_string_array_add_element(
  676. &iter_array, "LEAP"))
  677. goto error;
  678. }
  679. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  680. &iter_dict_entry,
  681. &iter_dict_val,
  682. &iter_array))
  683. goto error;
  684. }
  685. if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
  686. goto error;
  687. return reply;
  688. error:
  689. if (reply)
  690. dbus_message_unref(reply);
  691. return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
  692. "an internal error occurred returning "
  693. "interface capabilities.");
  694. }
  695. /**
  696. * wpas_dbus_iface_add_network - Add a new configured network
  697. * @message: Pointer to incoming dbus message
  698. * @wpa_s: wpa_supplicant structure for a network interface
  699. * Returns: A dbus message containing the object path of the new network
  700. *
  701. * Handler function for "addNetwork" method call of a network interface.
  702. */
  703. DBusMessage * wpas_dbus_iface_add_network(DBusMessage *message,
  704. struct wpa_supplicant *wpa_s)
  705. {
  706. DBusMessage *reply = NULL;
  707. struct wpa_ssid *ssid;
  708. char *path = NULL;
  709. path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  710. if (path == NULL) {
  711. perror("wpas_dbus_iface_scan_results[dbus]: out of "
  712. "memory.");
  713. wpa_printf(MSG_ERROR, "dbus control interface: not "
  714. "enough memory to send scan results "
  715. "signal.");
  716. goto out;
  717. }
  718. ssid = wpa_config_add_network(wpa_s->conf);
  719. if (ssid == NULL) {
  720. reply = dbus_message_new_error(message,
  721. WPAS_ERROR_ADD_NETWORK_ERROR,
  722. "wpa_supplicant could not add "
  723. "a network on this interface.");
  724. goto out;
  725. }
  726. wpas_notify_network_added(wpa_s, ssid);
  727. ssid->disabled = 1;
  728. wpa_config_set_network_defaults(ssid);
  729. /* Construct the object path for this network. */
  730. os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  731. "%s/" WPAS_DBUS_NETWORKS_PART "/%d",
  732. wpa_supplicant_get_dbus_path(wpa_s),
  733. ssid->id);
  734. reply = dbus_message_new_method_return(message);
  735. dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
  736. &path, DBUS_TYPE_INVALID);
  737. out:
  738. os_free(path);
  739. return reply;
  740. }
  741. /**
  742. * wpas_dbus_iface_remove_network - Remove a configured network
  743. * @message: Pointer to incoming dbus message
  744. * @wpa_s: wpa_supplicant structure for a network interface
  745. * Returns: A dbus message containing a UINT32 indicating success (1) or
  746. * failure (0)
  747. *
  748. * Handler function for "removeNetwork" method call of a network interface.
  749. */
  750. DBusMessage * wpas_dbus_iface_remove_network(DBusMessage *message,
  751. struct wpa_supplicant *wpa_s)
  752. {
  753. DBusMessage *reply = NULL;
  754. const char *op;
  755. char *iface = NULL, *net_id = NULL;
  756. int id;
  757. struct wpa_ssid *ssid;
  758. if (!dbus_message_get_args(message, NULL,
  759. DBUS_TYPE_OBJECT_PATH, &op,
  760. DBUS_TYPE_INVALID)) {
  761. reply = wpas_dbus_new_invalid_opts_error(message, NULL);
  762. goto out;
  763. }
  764. /* Extract the network ID */
  765. iface = wpas_dbus_decompose_object_path(op, &net_id, NULL);
  766. if (iface == NULL) {
  767. reply = wpas_dbus_new_invalid_network_error(message);
  768. goto out;
  769. }
  770. /* Ensure the network is actually a child of this interface */
  771. if (strcmp(iface, wpa_supplicant_get_dbus_path(wpa_s)) != 0) {
  772. reply = wpas_dbus_new_invalid_network_error(message);
  773. goto out;
  774. }
  775. id = strtoul(net_id, NULL, 10);
  776. ssid = wpa_config_get_network(wpa_s->conf, id);
  777. if (ssid == NULL) {
  778. reply = wpas_dbus_new_invalid_network_error(message);
  779. goto out;
  780. }
  781. wpas_notify_network_removed(wpa_s, ssid);
  782. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  783. reply = dbus_message_new_error(message,
  784. WPAS_ERROR_REMOVE_NETWORK_ERROR,
  785. "error removing the specified "
  786. "on this interface.");
  787. goto out;
  788. }
  789. if (ssid == wpa_s->current_ssid)
  790. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  791. reply = wpas_dbus_new_success_reply(message);
  792. out:
  793. os_free(iface);
  794. os_free(net_id);
  795. return reply;
  796. }
  797. static const char *dont_quote[] = {
  798. "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
  799. "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
  800. "bssid", NULL
  801. };
  802. static dbus_bool_t should_quote_opt(const char *key)
  803. {
  804. int i = 0;
  805. while (dont_quote[i] != NULL) {
  806. if (strcmp(key, dont_quote[i]) == 0)
  807. return FALSE;
  808. i++;
  809. }
  810. return TRUE;
  811. }
  812. /**
  813. * wpas_dbus_iface_set_network - Set options for a configured network
  814. * @message: Pointer to incoming dbus message
  815. * @wpa_s: wpa_supplicant structure for a network interface
  816. * @ssid: wpa_ssid structure for a configured network
  817. * Returns: a dbus message containing a UINT32 indicating success (1) or
  818. * failure (0)
  819. *
  820. * Handler function for "set" method call of a configured network.
  821. */
  822. DBusMessage * wpas_dbus_iface_set_network(DBusMessage *message,
  823. struct wpa_supplicant *wpa_s,
  824. struct wpa_ssid *ssid)
  825. {
  826. DBusMessage *reply = NULL;
  827. struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
  828. DBusMessageIter iter, iter_dict;
  829. dbus_message_iter_init(message, &iter);
  830. if (!wpa_dbus_dict_open_read(&iter, &iter_dict)) {
  831. reply = wpas_dbus_new_invalid_opts_error(message, NULL);
  832. goto out;
  833. }
  834. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  835. char *value = NULL;
  836. size_t size = 50;
  837. int ret;
  838. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
  839. reply = wpas_dbus_new_invalid_opts_error(message,
  840. NULL);
  841. goto out;
  842. }
  843. /* Type conversions, since wpa_supplicant wants strings */
  844. if (entry.type == DBUS_TYPE_ARRAY &&
  845. entry.array_type == DBUS_TYPE_BYTE) {
  846. if (entry.array_len <= 0)
  847. goto error;
  848. size = entry.array_len * 2 + 1;
  849. value = os_zalloc(size);
  850. if (value == NULL)
  851. goto error;
  852. ret = wpa_snprintf_hex(value, size,
  853. (u8 *) entry.bytearray_value,
  854. entry.array_len);
  855. if (ret <= 0)
  856. goto error;
  857. } else if (entry.type == DBUS_TYPE_STRING) {
  858. if (should_quote_opt(entry.key)) {
  859. size = os_strlen(entry.str_value);
  860. /* Zero-length option check */
  861. if (size <= 0)
  862. goto error;
  863. size += 3; /* For quotes and terminator */
  864. value = os_zalloc(size);
  865. if (value == NULL)
  866. goto error;
  867. ret = os_snprintf(value, size, "\"%s\"",
  868. entry.str_value);
  869. if (ret < 0 || (size_t) ret != (size - 1))
  870. goto error;
  871. } else {
  872. value = os_strdup(entry.str_value);
  873. if (value == NULL)
  874. goto error;
  875. }
  876. } else if (entry.type == DBUS_TYPE_UINT32) {
  877. value = os_zalloc(size);
  878. if (value == NULL)
  879. goto error;
  880. ret = os_snprintf(value, size, "%u",
  881. entry.uint32_value);
  882. if (ret <= 0)
  883. goto error;
  884. } else if (entry.type == DBUS_TYPE_INT32) {
  885. value = os_zalloc(size);
  886. if (value == NULL)
  887. goto error;
  888. ret = os_snprintf(value, size, "%d",
  889. 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 ((os_strcmp(entry.key, "psk") == 0 &&
  897. value[0] == '"' && ssid->ssid_len) ||
  898. (os_strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
  899. wpa_config_update_psk(ssid);
  900. os_free(value);
  901. wpa_dbus_dict_entry_clear(&entry);
  902. continue;
  903. error:
  904. os_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 (os_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 (os_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. os_free(iface_obj_path);
  1008. os_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. os_free(wpa_s->conf->opensc_engine_path);
  1100. wpa_s->conf->opensc_engine_path = opensc_engine_path;
  1101. os_free(wpa_s->conf->pkcs11_engine_path);
  1102. wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path;
  1103. os_free(wpa_s->conf->pkcs11_module_path);
  1104. wpa_s->conf->pkcs11_module_path = pkcs11_module_path;
  1105. wpa_sm_set_eapol(wpa_s->wpa, NULL);
  1106. eapol_sm_deinit(wpa_s->eapol);
  1107. wpa_s->eapol = NULL;
  1108. wpa_supplicant_init_eapol(wpa_s);
  1109. wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
  1110. return wpas_dbus_new_success_reply(message);
  1111. error:
  1112. os_free(opensc_engine_path);
  1113. os_free(pkcs11_engine_path);
  1114. os_free(pkcs11_module_path);
  1115. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1116. }
  1117. /**
  1118. * wpas_dbus_iface_get_state - Get interface state
  1119. * @message: Pointer to incoming dbus message
  1120. * @wpa_s: wpa_supplicant structure for a network interface
  1121. * Returns: A dbus message containing a STRING representing the current
  1122. * interface state
  1123. *
  1124. * Handler function for "state" method call.
  1125. */
  1126. DBusMessage * wpas_dbus_iface_get_state(DBusMessage *message,
  1127. struct wpa_supplicant *wpa_s)
  1128. {
  1129. DBusMessage *reply = NULL;
  1130. const char *str_state;
  1131. reply = dbus_message_new_method_return(message);
  1132. if (reply != NULL) {
  1133. str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
  1134. dbus_message_append_args(reply, DBUS_TYPE_STRING, &str_state,
  1135. DBUS_TYPE_INVALID);
  1136. }
  1137. return reply;
  1138. }
  1139. /**
  1140. * wpas_dbus_iface_get_scanning - Get interface scanning state
  1141. * @message: Pointer to incoming dbus message
  1142. * @wpa_s: wpa_supplicant structure for a network interface
  1143. * Returns: A dbus message containing whether the interface is scanning
  1144. *
  1145. * Handler function for "scanning" method call.
  1146. */
  1147. DBusMessage * wpas_dbus_iface_get_scanning(DBusMessage *message,
  1148. struct wpa_supplicant *wpa_s)
  1149. {
  1150. DBusMessage *reply = NULL;
  1151. dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
  1152. reply = dbus_message_new_method_return(message);
  1153. if (reply != NULL) {
  1154. dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &scanning,
  1155. DBUS_TYPE_INVALID);
  1156. } else {
  1157. perror("wpas_dbus_iface_get_scanning[dbus]: out of "
  1158. "memory.");
  1159. wpa_printf(MSG_ERROR, "dbus control interface: not enough "
  1160. "memory to return scanning state.");
  1161. }
  1162. return reply;
  1163. }
  1164. /**
  1165. * wpas_dbus_iface_set_blobs - Store named binary blobs (ie, for certificates)
  1166. * @message: Pointer to incoming dbus message
  1167. * @wpa_s: %wpa_supplicant data structure
  1168. * Returns: A dbus message containing a UINT32 indicating success (1) or
  1169. * failure (0)
  1170. *
  1171. * Asks wpa_supplicant to internally store a one or more binary blobs.
  1172. */
  1173. DBusMessage * wpas_dbus_iface_set_blobs(DBusMessage *message,
  1174. struct wpa_supplicant *wpa_s)
  1175. {
  1176. DBusMessage *reply = NULL;
  1177. struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
  1178. DBusMessageIter iter, iter_dict;
  1179. dbus_message_iter_init(message, &iter);
  1180. if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
  1181. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1182. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1183. struct wpa_config_blob *blob;
  1184. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
  1185. reply = wpas_dbus_new_invalid_opts_error(message,
  1186. NULL);
  1187. break;
  1188. }
  1189. if (entry.type != DBUS_TYPE_ARRAY ||
  1190. entry.array_type != DBUS_TYPE_BYTE) {
  1191. reply = wpas_dbus_new_invalid_opts_error(
  1192. message, "Byte array expected.");
  1193. break;
  1194. }
  1195. if ((entry.array_len <= 0) || (entry.array_len > 65536) ||
  1196. !strlen(entry.key)) {
  1197. reply = wpas_dbus_new_invalid_opts_error(
  1198. message, "Invalid array size.");
  1199. break;
  1200. }
  1201. blob = os_zalloc(sizeof(*blob));
  1202. if (blob == NULL) {
  1203. reply = dbus_message_new_error(
  1204. message, WPAS_ERROR_ADD_ERROR,
  1205. "Not enough memory to add blob.");
  1206. break;
  1207. }
  1208. blob->data = os_zalloc(entry.array_len);
  1209. if (blob->data == NULL) {
  1210. reply = dbus_message_new_error(
  1211. message, WPAS_ERROR_ADD_ERROR,
  1212. "Not enough memory to add blob data.");
  1213. os_free(blob);
  1214. break;
  1215. }
  1216. blob->name = os_strdup(entry.key);
  1217. blob->len = entry.array_len;
  1218. os_memcpy(blob->data, (u8 *) entry.bytearray_value,
  1219. entry.array_len);
  1220. if (blob->name == NULL || blob->data == NULL) {
  1221. wpa_config_free_blob(blob);
  1222. reply = dbus_message_new_error(
  1223. message, WPAS_ERROR_ADD_ERROR,
  1224. "Error adding blob.");
  1225. break;
  1226. }
  1227. /* Success */
  1228. if (!wpa_config_remove_blob(wpa_s->conf, blob->name))
  1229. wpas_notify_blob_removed(wpa_s, blob->name);
  1230. wpa_config_set_blob(wpa_s->conf, blob);
  1231. wpas_notify_blob_added(wpa_s, blob->name);
  1232. wpa_dbus_dict_entry_clear(&entry);
  1233. }
  1234. wpa_dbus_dict_entry_clear(&entry);
  1235. return reply ? reply : wpas_dbus_new_success_reply(message);
  1236. }
  1237. /**
  1238. * wpas_dbus_iface_remove_blob - Remove named binary blobs
  1239. * @message: Pointer to incoming dbus message
  1240. * @wpa_s: %wpa_supplicant data structure
  1241. * Returns: A dbus message containing a UINT32 indicating success (1) or
  1242. * failure (0)
  1243. *
  1244. * Asks wpa_supplicant to remove one or more previously stored binary blobs.
  1245. */
  1246. DBusMessage * wpas_dbus_iface_remove_blobs(DBusMessage *message,
  1247. struct wpa_supplicant *wpa_s)
  1248. {
  1249. DBusMessageIter iter, array;
  1250. char *err_msg = NULL;
  1251. dbus_message_iter_init(message, &iter);
  1252. if ((dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY) ||
  1253. (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_STRING))
  1254. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1255. dbus_message_iter_recurse(&iter, &array);
  1256. while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
  1257. const char *name;
  1258. dbus_message_iter_get_basic(&array, &name);
  1259. if (!os_strlen(name))
  1260. err_msg = "Invalid blob name.";
  1261. if (wpa_config_remove_blob(wpa_s->conf, name) != 0)
  1262. err_msg = "Error removing blob.";
  1263. else
  1264. wpas_notify_blob_removed(wpa_s, name);
  1265. dbus_message_iter_next(&array);
  1266. }
  1267. if (err_msg)
  1268. return dbus_message_new_error(message, WPAS_ERROR_REMOVE_ERROR,
  1269. err_msg);
  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. os_snprintf(npin, sizeof(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 */