ctrl_iface_dbus_handlers.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  1. /*
  2. * WPA Supplicant / dbus-based control interface
  3. * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "config.h"
  17. #include "wpa_supplicant_i.h"
  18. #include "driver_i.h"
  19. #include "ctrl_iface_dbus.h"
  20. #include "ctrl_iface_dbus_handlers.h"
  21. #include "notify.h"
  22. #include "eap_peer/eap_methods.h"
  23. #include "dbus_dict_helpers.h"
  24. #include "ieee802_11_defs.h"
  25. #include "wpas_glue.h"
  26. #include "eapol_supp/eapol_supp_sm.h"
  27. #include "wps_supplicant.h"
  28. #include "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. #ifdef EAP_TLS_OPENSSL
  1100. os_free(wpa_s->conf->opensc_engine_path);
  1101. wpa_s->conf->opensc_engine_path = opensc_engine_path;
  1102. os_free(wpa_s->conf->pkcs11_engine_path);
  1103. wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path;
  1104. os_free(wpa_s->conf->pkcs11_module_path);
  1105. wpa_s->conf->pkcs11_module_path = pkcs11_module_path;
  1106. #endif /* EAP_TLS_OPENSSL */
  1107. wpa_sm_set_eapol(wpa_s->wpa, NULL);
  1108. eapol_sm_deinit(wpa_s->eapol);
  1109. wpa_s->eapol = NULL;
  1110. wpa_supplicant_init_eapol(wpa_s);
  1111. wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
  1112. return wpas_dbus_new_success_reply(message);
  1113. error:
  1114. os_free(opensc_engine_path);
  1115. os_free(pkcs11_engine_path);
  1116. os_free(pkcs11_module_path);
  1117. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1118. }
  1119. /**
  1120. * wpas_dbus_iface_get_state - Get interface state
  1121. * @message: Pointer to incoming dbus message
  1122. * @wpa_s: wpa_supplicant structure for a network interface
  1123. * Returns: A dbus message containing a STRING representing the current
  1124. * interface state
  1125. *
  1126. * Handler function for "state" method call.
  1127. */
  1128. DBusMessage * wpas_dbus_iface_get_state(DBusMessage *message,
  1129. struct wpa_supplicant *wpa_s)
  1130. {
  1131. DBusMessage *reply = NULL;
  1132. const char *str_state;
  1133. reply = dbus_message_new_method_return(message);
  1134. if (reply != NULL) {
  1135. str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
  1136. dbus_message_append_args(reply, DBUS_TYPE_STRING, &str_state,
  1137. DBUS_TYPE_INVALID);
  1138. }
  1139. return reply;
  1140. }
  1141. /**
  1142. * wpas_dbus_iface_get_scanning - Get interface scanning state
  1143. * @message: Pointer to incoming dbus message
  1144. * @wpa_s: wpa_supplicant structure for a network interface
  1145. * Returns: A dbus message containing whether the interface is scanning
  1146. *
  1147. * Handler function for "scanning" method call.
  1148. */
  1149. DBusMessage * wpas_dbus_iface_get_scanning(DBusMessage *message,
  1150. struct wpa_supplicant *wpa_s)
  1151. {
  1152. DBusMessage *reply = NULL;
  1153. dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
  1154. reply = dbus_message_new_method_return(message);
  1155. if (reply != NULL) {
  1156. dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &scanning,
  1157. DBUS_TYPE_INVALID);
  1158. } else {
  1159. perror("wpas_dbus_iface_get_scanning[dbus]: out of "
  1160. "memory.");
  1161. wpa_printf(MSG_ERROR, "dbus control interface: not enough "
  1162. "memory to return scanning state.");
  1163. }
  1164. return reply;
  1165. }
  1166. /**
  1167. * wpas_dbus_iface_set_blobs - Store named binary blobs (ie, for certificates)
  1168. * @message: Pointer to incoming dbus message
  1169. * @wpa_s: %wpa_supplicant data structure
  1170. * Returns: A dbus message containing a UINT32 indicating success (1) or
  1171. * failure (0)
  1172. *
  1173. * Asks wpa_supplicant to internally store a one or more binary blobs.
  1174. */
  1175. DBusMessage * wpas_dbus_iface_set_blobs(DBusMessage *message,
  1176. struct wpa_supplicant *wpa_s)
  1177. {
  1178. DBusMessage *reply = NULL;
  1179. struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
  1180. DBusMessageIter iter, iter_dict;
  1181. dbus_message_iter_init(message, &iter);
  1182. if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
  1183. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1184. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1185. struct wpa_config_blob *blob;
  1186. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
  1187. reply = wpas_dbus_new_invalid_opts_error(message,
  1188. NULL);
  1189. break;
  1190. }
  1191. if (entry.type != DBUS_TYPE_ARRAY ||
  1192. entry.array_type != DBUS_TYPE_BYTE) {
  1193. reply = wpas_dbus_new_invalid_opts_error(
  1194. message, "Byte array expected.");
  1195. break;
  1196. }
  1197. if ((entry.array_len <= 0) || (entry.array_len > 65536) ||
  1198. !strlen(entry.key)) {
  1199. reply = wpas_dbus_new_invalid_opts_error(
  1200. message, "Invalid array size.");
  1201. break;
  1202. }
  1203. blob = os_zalloc(sizeof(*blob));
  1204. if (blob == NULL) {
  1205. reply = dbus_message_new_error(
  1206. message, WPAS_ERROR_ADD_ERROR,
  1207. "Not enough memory to add blob.");
  1208. break;
  1209. }
  1210. blob->data = os_zalloc(entry.array_len);
  1211. if (blob->data == NULL) {
  1212. reply = dbus_message_new_error(
  1213. message, WPAS_ERROR_ADD_ERROR,
  1214. "Not enough memory to add blob data.");
  1215. os_free(blob);
  1216. break;
  1217. }
  1218. blob->name = os_strdup(entry.key);
  1219. blob->len = entry.array_len;
  1220. os_memcpy(blob->data, (u8 *) entry.bytearray_value,
  1221. entry.array_len);
  1222. if (blob->name == NULL || blob->data == NULL) {
  1223. wpa_config_free_blob(blob);
  1224. reply = dbus_message_new_error(
  1225. message, WPAS_ERROR_ADD_ERROR,
  1226. "Error adding blob.");
  1227. break;
  1228. }
  1229. /* Success */
  1230. if (!wpa_config_remove_blob(wpa_s->conf, blob->name))
  1231. wpas_notify_blob_removed(wpa_s, blob->name);
  1232. wpa_config_set_blob(wpa_s->conf, blob);
  1233. wpas_notify_blob_added(wpa_s, blob->name);
  1234. wpa_dbus_dict_entry_clear(&entry);
  1235. }
  1236. wpa_dbus_dict_entry_clear(&entry);
  1237. return reply ? reply : wpas_dbus_new_success_reply(message);
  1238. }
  1239. /**
  1240. * wpas_dbus_iface_remove_blob - Remove named binary blobs
  1241. * @message: Pointer to incoming dbus message
  1242. * @wpa_s: %wpa_supplicant data structure
  1243. * Returns: A dbus message containing a UINT32 indicating success (1) or
  1244. * failure (0)
  1245. *
  1246. * Asks wpa_supplicant to remove one or more previously stored binary blobs.
  1247. */
  1248. DBusMessage * wpas_dbus_iface_remove_blobs(DBusMessage *message,
  1249. struct wpa_supplicant *wpa_s)
  1250. {
  1251. DBusMessageIter iter, array;
  1252. char *err_msg = NULL;
  1253. dbus_message_iter_init(message, &iter);
  1254. if ((dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY) ||
  1255. (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_STRING))
  1256. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1257. dbus_message_iter_recurse(&iter, &array);
  1258. while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
  1259. const char *name;
  1260. dbus_message_iter_get_basic(&array, &name);
  1261. if (!os_strlen(name))
  1262. err_msg = "Invalid blob name.";
  1263. if (wpa_config_remove_blob(wpa_s->conf, name) != 0)
  1264. err_msg = "Error removing blob.";
  1265. else
  1266. wpas_notify_blob_removed(wpa_s, name);
  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. return wpas_dbus_new_success_reply(message);
  1273. }
  1274. #ifdef CONFIG_WPS
  1275. /**
  1276. * wpas_dbus_iface_wps_pbc - Request credentials using WPS PBC method
  1277. * @message: Pointer to incoming dbus message
  1278. * @wpa_s: %wpa_supplicant data structure
  1279. * Returns: A dbus message containing a UINT32 indicating success (1) or
  1280. * failure (0)
  1281. *
  1282. * Handler function for "wpsPbc" method call
  1283. */
  1284. DBusMessage * wpas_dbus_iface_wps_pbc(DBusMessage *message,
  1285. struct wpa_supplicant *wpa_s)
  1286. {
  1287. char *arg_bssid = NULL;
  1288. u8 bssid[ETH_ALEN];
  1289. int ret = 0;
  1290. if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg_bssid,
  1291. DBUS_TYPE_INVALID))
  1292. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1293. if (!os_strcmp(arg_bssid, "any"))
  1294. ret = wpas_wps_start_pbc(wpa_s, NULL);
  1295. else if (!hwaddr_aton(arg_bssid, bssid))
  1296. ret = wpas_wps_start_pbc(wpa_s, bssid);
  1297. else {
  1298. return wpas_dbus_new_invalid_opts_error(message,
  1299. "Invalid BSSID");
  1300. }
  1301. if (ret < 0) {
  1302. return dbus_message_new_error(message,
  1303. WPAS_ERROR_WPS_PBC_ERROR,
  1304. "Could not start PBC "
  1305. "negotiation");
  1306. }
  1307. return wpas_dbus_new_success_reply(message);
  1308. }
  1309. /**
  1310. * wpas_dbus_iface_wps_pin - Establish the PIN number of the enrollee
  1311. * @message: Pointer to incoming dbus message
  1312. * @wpa_s: %wpa_supplicant data structure
  1313. * Returns: A dbus message containing a UINT32 indicating success (1) or
  1314. * failure (0)
  1315. *
  1316. * Handler function for "wpsPin" method call
  1317. */
  1318. DBusMessage * wpas_dbus_iface_wps_pin(DBusMessage *message,
  1319. struct wpa_supplicant *wpa_s)
  1320. {
  1321. DBusMessage *reply = NULL;
  1322. char *arg_bssid;
  1323. char *pin = NULL;
  1324. u8 bssid[ETH_ALEN], *_bssid = NULL;
  1325. int ret = 0;
  1326. if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg_bssid,
  1327. DBUS_TYPE_STRING, &pin, DBUS_TYPE_INVALID))
  1328. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1329. if (!os_strcmp(arg_bssid, "any"))
  1330. _bssid = NULL;
  1331. else if (!hwaddr_aton(arg_bssid, bssid))
  1332. _bssid = bssid;
  1333. else {
  1334. return wpas_dbus_new_invalid_opts_error(message,
  1335. "Invalid BSSID");
  1336. }
  1337. if (os_strlen(pin) > 0)
  1338. ret = wpas_wps_start_pin(wpa_s, _bssid, pin);
  1339. else
  1340. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL);
  1341. if (ret < 0) {
  1342. return dbus_message_new_error(message,
  1343. WPAS_ERROR_WPS_PIN_ERROR,
  1344. "Could not init PIN");
  1345. }
  1346. reply = dbus_message_new_method_return(message);
  1347. if (reply == NULL)
  1348. return NULL;
  1349. if (ret == 0) {
  1350. dbus_message_append_args(reply, DBUS_TYPE_STRING, &pin,
  1351. DBUS_TYPE_INVALID);
  1352. } else {
  1353. char npin[9];
  1354. os_snprintf(npin, sizeof(npin), "%08d", ret);
  1355. dbus_message_append_args(reply, DBUS_TYPE_STRING, &npin,
  1356. DBUS_TYPE_INVALID);
  1357. }
  1358. return reply;
  1359. }
  1360. /**
  1361. * wpas_dbus_iface_wps_reg - Request credentials using the PIN of the AP
  1362. * @message: Pointer to incoming dbus message
  1363. * @wpa_s: %wpa_supplicant data structure
  1364. * Returns: A dbus message containing a UINT32 indicating success (1) or
  1365. * failure (0)
  1366. *
  1367. * Handler function for "wpsReg" method call
  1368. */
  1369. DBusMessage * wpas_dbus_iface_wps_reg(DBusMessage *message,
  1370. struct wpa_supplicant *wpa_s)
  1371. {
  1372. char *arg_bssid;
  1373. char *pin = NULL;
  1374. u8 bssid[ETH_ALEN];
  1375. int ret = 0;
  1376. if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg_bssid,
  1377. DBUS_TYPE_STRING, &pin, DBUS_TYPE_INVALID))
  1378. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1379. if (!os_strcmp(arg_bssid, "any"))
  1380. ret = wpas_wps_start_reg(wpa_s, NULL, pin, NULL);
  1381. else if (!hwaddr_aton(arg_bssid, bssid))
  1382. ret = wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
  1383. else {
  1384. return wpas_dbus_new_invalid_opts_error(message,
  1385. "Invalid BSSID");
  1386. }
  1387. if (ret < 0) {
  1388. return dbus_message_new_error(message,
  1389. WPAS_ERROR_WPS_PBC_ERROR,
  1390. "Could not request credentials");
  1391. }
  1392. return wpas_dbus_new_success_reply(message);
  1393. }
  1394. #endif /* CONFIG_WPS */