ctrl_iface_dbus_handlers.c 44 KB

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