dbus_new_handlers.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082
  1. /*
  2. * WPA Supplicant / dbus-based control interface
  3. * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
  4. * Copyright (c) 2009-2010, Witold Sowa <witold.sowa@gmail.com>
  5. * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Alternatively, this software may be distributed under the terms of BSD
  12. * license.
  13. *
  14. * See README and COPYING for more details.
  15. */
  16. #include "includes.h"
  17. #include "common.h"
  18. #include "common/ieee802_11_defs.h"
  19. #include "eap_peer/eap_methods.h"
  20. #include "eapol_supp/eapol_supp_sm.h"
  21. #include "rsn_supp/wpa.h"
  22. #include "../config.h"
  23. #include "../wpa_supplicant_i.h"
  24. #include "../driver_i.h"
  25. #include "../notify.h"
  26. #include "../wpas_glue.h"
  27. #include "../bss.h"
  28. #include "../scan.h"
  29. #include "dbus_new_helpers.h"
  30. #include "dbus_new.h"
  31. #include "dbus_new_handlers.h"
  32. #include "dbus_dict_helpers.h"
  33. extern int wpa_debug_level;
  34. extern int wpa_debug_show_keys;
  35. extern int wpa_debug_timestamp;
  36. static const char *debug_strings[] = {
  37. "msgdump", "debug", "info", "warning", "error", NULL
  38. };
  39. /**
  40. * wpas_dbus_new_decompose_object_path - Decompose an interface object path into parts
  41. * @path: The dbus object path
  42. * @network: (out) the configured network this object path refers to, if any
  43. * @bssid: (out) the scanned bssid this object path refers to, if any
  44. * Returns: The object path of the network interface this path refers to
  45. *
  46. * For a given object path, decomposes the object path into object id, network,
  47. * and BSSID parts, if those parts exist.
  48. */
  49. static char * wpas_dbus_new_decompose_object_path(const char *path,
  50. char **network,
  51. char **bssid)
  52. {
  53. const unsigned int dev_path_prefix_len =
  54. strlen(WPAS_DBUS_NEW_PATH_INTERFACES "/");
  55. char *obj_path_only;
  56. char *next_sep;
  57. /* Be a bit paranoid about path */
  58. if (!path || os_strncmp(path, WPAS_DBUS_NEW_PATH_INTERFACES "/",
  59. dev_path_prefix_len))
  60. return NULL;
  61. /* Ensure there's something at the end of the path */
  62. if ((path + dev_path_prefix_len)[0] == '\0')
  63. return NULL;
  64. obj_path_only = os_strdup(path);
  65. if (obj_path_only == NULL)
  66. return NULL;
  67. next_sep = os_strchr(obj_path_only + dev_path_prefix_len, '/');
  68. if (next_sep != NULL) {
  69. const char *net_part = os_strstr(
  70. next_sep, WPAS_DBUS_NEW_NETWORKS_PART "/");
  71. const char *bssid_part = os_strstr(
  72. next_sep, WPAS_DBUS_NEW_BSSIDS_PART "/");
  73. if (network && net_part) {
  74. /* Deal with a request for a configured network */
  75. const char *net_name = net_part +
  76. os_strlen(WPAS_DBUS_NEW_NETWORKS_PART "/");
  77. *network = NULL;
  78. if (os_strlen(net_name))
  79. *network = os_strdup(net_name);
  80. } else if (bssid && bssid_part) {
  81. /* Deal with a request for a scanned BSSID */
  82. const char *bssid_name = bssid_part +
  83. os_strlen(WPAS_DBUS_NEW_BSSIDS_PART "/");
  84. if (strlen(bssid_name))
  85. *bssid = os_strdup(bssid_name);
  86. else
  87. *bssid = NULL;
  88. }
  89. /* Cut off interface object path before "/" */
  90. *next_sep = '\0';
  91. }
  92. return obj_path_only;
  93. }
  94. /**
  95. * wpas_dbus_error_unknown_error - Return a new InvalidArgs error message
  96. * @message: Pointer to incoming dbus message this error refers to
  97. * @arg: Optional string appended to error message
  98. * Returns: a dbus error message
  99. *
  100. * Convenience function to create and return an UnknownError
  101. */
  102. DBusMessage * wpas_dbus_error_unknown_error(DBusMessage *message,
  103. const char *arg)
  104. {
  105. /*
  106. * This function can be called as a result of a failure
  107. * within internal getter calls, which will call this function
  108. * with a NULL message parameter. However, dbus_message_new_error
  109. * looks very unkindly (i.e, abort()) on a NULL message, so
  110. * in this case, we should not call it.
  111. */
  112. if (message == NULL) {
  113. wpa_printf(MSG_INFO, "dbus: wpas_dbus_error_unknown_error "
  114. "called with NULL message (arg=%s)",
  115. arg ? arg : "N/A");
  116. return NULL;
  117. }
  118. return dbus_message_new_error(message, WPAS_DBUS_ERROR_UNKNOWN_ERROR,
  119. arg);
  120. }
  121. /**
  122. * wpas_dbus_error_iface_unknown - Return a new invalid interface error message
  123. * @message: Pointer to incoming dbus message this error refers to
  124. * Returns: A dbus error message
  125. *
  126. * Convenience function to create and return an invalid interface error
  127. */
  128. static DBusMessage * wpas_dbus_error_iface_unknown(DBusMessage *message)
  129. {
  130. return dbus_message_new_error(message, WPAS_DBUS_ERROR_IFACE_UNKNOWN,
  131. "wpa_supplicant knows nothing about "
  132. "this interface.");
  133. }
  134. /**
  135. * wpas_dbus_error_network_unknown - Return a new NetworkUnknown error message
  136. * @message: Pointer to incoming dbus message this error refers to
  137. * Returns: a dbus error message
  138. *
  139. * Convenience function to create and return an invalid network error
  140. */
  141. static DBusMessage * wpas_dbus_error_network_unknown(DBusMessage *message)
  142. {
  143. return dbus_message_new_error(message, WPAS_DBUS_ERROR_NETWORK_UNKNOWN,
  144. "There is no such a network in this "
  145. "interface.");
  146. }
  147. /**
  148. * wpas_dbus_error_invalid_args - Return a new InvalidArgs error message
  149. * @message: Pointer to incoming dbus message this error refers to
  150. * Returns: a dbus error message
  151. *
  152. * Convenience function to create and return an invalid options error
  153. */
  154. DBusMessage * wpas_dbus_error_invalid_args(DBusMessage *message,
  155. const char *arg)
  156. {
  157. DBusMessage *reply;
  158. reply = dbus_message_new_error(message, WPAS_DBUS_ERROR_INVALID_ARGS,
  159. "Did not receive correct message "
  160. "arguments.");
  161. if (arg != NULL)
  162. dbus_message_append_args(reply, DBUS_TYPE_STRING, &arg,
  163. DBUS_TYPE_INVALID);
  164. return reply;
  165. }
  166. static const char *dont_quote[] = {
  167. "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
  168. "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
  169. "bssid", NULL
  170. };
  171. static dbus_bool_t should_quote_opt(const char *key)
  172. {
  173. int i = 0;
  174. while (dont_quote[i] != NULL) {
  175. if (os_strcmp(key, dont_quote[i]) == 0)
  176. return FALSE;
  177. i++;
  178. }
  179. return TRUE;
  180. }
  181. /**
  182. * get_iface_by_dbus_path - Get a new network interface
  183. * @global: Pointer to global data from wpa_supplicant_init()
  184. * @path: Pointer to a dbus object path representing an interface
  185. * Returns: Pointer to the interface or %NULL if not found
  186. */
  187. static struct wpa_supplicant * get_iface_by_dbus_path(
  188. struct wpa_global *global, const char *path)
  189. {
  190. struct wpa_supplicant *wpa_s;
  191. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  192. if (os_strcmp(wpa_s->dbus_new_path, path) == 0)
  193. return wpa_s;
  194. }
  195. return NULL;
  196. }
  197. /**
  198. * set_network_properties - Set properties of a configured network
  199. * @message: Pointer to incoming dbus message
  200. * @wpa_s: wpa_supplicant structure for a network interface
  201. * @ssid: wpa_ssid structure for a configured network
  202. * @iter: DBus message iterator containing dictionary of network
  203. * properties to set.
  204. * Returns: NULL when succeed or DBus error on failure
  205. *
  206. * Sets network configuration with parameters given id DBus dictionary
  207. */
  208. static DBusMessage * set_network_properties(DBusMessage *message,
  209. struct wpa_supplicant *wpa_s,
  210. struct wpa_ssid *ssid,
  211. DBusMessageIter *iter)
  212. {
  213. struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
  214. DBusMessage *reply = NULL;
  215. DBusMessageIter iter_dict;
  216. if (!wpa_dbus_dict_open_read(iter, &iter_dict))
  217. return wpas_dbus_error_invalid_args(message, NULL);
  218. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  219. char *value = NULL;
  220. size_t size = 50;
  221. int ret;
  222. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
  223. reply = wpas_dbus_error_invalid_args(message, NULL);
  224. break;
  225. }
  226. if (entry.type == DBUS_TYPE_ARRAY &&
  227. entry.array_type == DBUS_TYPE_BYTE) {
  228. if (entry.array_len <= 0)
  229. goto error;
  230. size = entry.array_len * 2 + 1;
  231. value = os_zalloc(size);
  232. if (value == NULL)
  233. goto error;
  234. ret = wpa_snprintf_hex(value, size,
  235. (u8 *) entry.bytearray_value,
  236. entry.array_len);
  237. if (ret <= 0)
  238. goto error;
  239. } else if (entry.type == DBUS_TYPE_STRING) {
  240. if (should_quote_opt(entry.key)) {
  241. size = os_strlen(entry.str_value);
  242. if (size <= 0)
  243. goto error;
  244. size += 3;
  245. value = os_zalloc(size);
  246. if (value == NULL)
  247. goto error;
  248. ret = os_snprintf(value, size, "\"%s\"",
  249. entry.str_value);
  250. if (ret < 0 || (size_t) ret != (size - 1))
  251. goto error;
  252. } else {
  253. value = os_strdup(entry.str_value);
  254. if (value == NULL)
  255. goto error;
  256. }
  257. } else if (entry.type == DBUS_TYPE_UINT32) {
  258. value = os_zalloc(size);
  259. if (value == NULL)
  260. goto error;
  261. ret = os_snprintf(value, size, "%u",
  262. entry.uint32_value);
  263. if (ret <= 0)
  264. goto error;
  265. } else if (entry.type == DBUS_TYPE_INT32) {
  266. value = os_zalloc(size);
  267. if (value == NULL)
  268. goto error;
  269. ret = os_snprintf(value, size, "%d",
  270. entry.int32_value);
  271. if (ret <= 0)
  272. goto error;
  273. } else
  274. goto error;
  275. if (wpa_config_set(ssid, entry.key, value, 0) < 0)
  276. goto error;
  277. if ((os_strcmp(entry.key, "psk") == 0 &&
  278. value[0] == '"' && ssid->ssid_len) ||
  279. (strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
  280. wpa_config_update_psk(ssid);
  281. else if (os_strcmp(entry.key, "priority") == 0)
  282. wpa_config_update_prio_list(wpa_s->conf);
  283. os_free(value);
  284. wpa_dbus_dict_entry_clear(&entry);
  285. continue;
  286. error:
  287. os_free(value);
  288. reply = wpas_dbus_error_invalid_args(message, entry.key);
  289. wpa_dbus_dict_entry_clear(&entry);
  290. break;
  291. }
  292. return reply;
  293. }
  294. /**
  295. * wpas_dbus_simple_property_getter - Get basic type property
  296. * @message: Pointer to incoming dbus message
  297. * @type: DBus type of property (must be basic type)
  298. * @val: pointer to place holding property value
  299. * Returns: The DBus message containing response for Properties.Get call
  300. * or DBus error message if error occurred.
  301. *
  302. * Generic getter for basic type properties. Type is required to be basic.
  303. */
  304. DBusMessage * wpas_dbus_simple_property_getter(DBusMessage *message,
  305. const int type, const void *val)
  306. {
  307. DBusMessage *reply = NULL;
  308. DBusMessageIter iter, variant_iter;
  309. if (!dbus_type_is_basic(type)) {
  310. wpa_printf(MSG_ERROR, "dbus: wpas_dbus_simple_property_getter:"
  311. " given type is not basic");
  312. return wpas_dbus_error_unknown_error(message, NULL);
  313. }
  314. if (message == NULL)
  315. reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
  316. else
  317. reply = dbus_message_new_method_return(message);
  318. if (reply != NULL) {
  319. dbus_message_iter_init_append(reply, &iter);
  320. if (!dbus_message_iter_open_container(
  321. &iter, DBUS_TYPE_VARIANT,
  322. wpa_dbus_type_as_string(type), &variant_iter) ||
  323. !dbus_message_iter_append_basic(&variant_iter, type,
  324. val) ||
  325. !dbus_message_iter_close_container(&iter, &variant_iter)) {
  326. wpa_printf(MSG_ERROR, "dbus: "
  327. "wpas_dbus_simple_property_getter: out of "
  328. "memory to put property value into "
  329. "message");
  330. dbus_message_unref(reply);
  331. reply = dbus_message_new_error(message,
  332. DBUS_ERROR_NO_MEMORY,
  333. NULL);
  334. }
  335. } else {
  336. wpa_printf(MSG_ERROR, "dbus: wpas_dbus_simple_property_getter:"
  337. " out of memory to return property value");
  338. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  339. NULL);
  340. }
  341. return reply;
  342. }
  343. /**
  344. * wpas_dbus_simple_property_setter - Set basic type property
  345. * @message: Pointer to incoming dbus message
  346. * @type: DBus type of property (must be basic type)
  347. * @val: pointer to place where value being set will be stored
  348. * Returns: NULL or DBus error message if error occurred.
  349. *
  350. * Generic setter for basic type properties. Type is required to be basic.
  351. */
  352. DBusMessage * wpas_dbus_simple_property_setter(DBusMessage *message,
  353. const int type, void *val)
  354. {
  355. DBusMessageIter iter, variant_iter;
  356. if (!dbus_type_is_basic(type)) {
  357. wpa_printf(MSG_ERROR, "dbus: wpas_dbus_simple_property_setter:"
  358. " given type is not basic");
  359. return wpas_dbus_error_unknown_error(message, NULL);
  360. }
  361. if (!dbus_message_iter_init(message, &iter)) {
  362. wpa_printf(MSG_ERROR, "dbus: wpas_dbus_simple_property_setter:"
  363. " out of memory to return scanning state");
  364. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  365. NULL);
  366. }
  367. /* omit first and second argument and get value from third */
  368. dbus_message_iter_next(&iter);
  369. dbus_message_iter_next(&iter);
  370. dbus_message_iter_recurse(&iter, &variant_iter);
  371. if (dbus_message_iter_get_arg_type(&variant_iter) != type) {
  372. wpa_printf(MSG_DEBUG, "dbus: wpas_dbus_simple_property_setter:"
  373. " wrong property type");
  374. return wpas_dbus_error_invalid_args(message,
  375. "wrong property type");
  376. }
  377. dbus_message_iter_get_basic(&variant_iter, val);
  378. return NULL;
  379. }
  380. /**
  381. * wpas_dbus_simple_array_property_getter - Get array type property
  382. * @message: Pointer to incoming dbus message
  383. * @type: DBus type of property array elements (must be basic type)
  384. * @array: pointer to array of elements to put into response message
  385. * @array_len: length of above array
  386. * Returns: The DBus message containing response for Properties.Get call
  387. * or DBus error message if error occurred.
  388. *
  389. * Generic getter for array type properties. Array elements type is
  390. * required to be basic.
  391. */
  392. DBusMessage * wpas_dbus_simple_array_property_getter(DBusMessage *message,
  393. const int type,
  394. const void *array,
  395. size_t array_len)
  396. {
  397. DBusMessage *reply = NULL;
  398. DBusMessageIter iter, variant_iter, array_iter;
  399. char type_str[] = "a?"; /* ? will be replaced with subtype letter; */
  400. const char *sub_type_str;
  401. size_t element_size, i;
  402. if (!dbus_type_is_basic(type)) {
  403. wpa_printf(MSG_ERROR, "dbus: "
  404. "wpas_dbus_simple_array_property_getter: given "
  405. "type is not basic");
  406. return wpas_dbus_error_unknown_error(message, NULL);
  407. }
  408. sub_type_str = wpa_dbus_type_as_string(type);
  409. type_str[1] = sub_type_str[0];
  410. if (message == NULL)
  411. reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
  412. else
  413. reply = dbus_message_new_method_return(message);
  414. if (reply == NULL) {
  415. wpa_printf(MSG_ERROR, "dbus: "
  416. "wpas_dbus_simple_array_property_getter: out of "
  417. "memory to create return message");
  418. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  419. NULL);
  420. }
  421. dbus_message_iter_init_append(reply, &iter);
  422. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
  423. type_str, &variant_iter) ||
  424. !dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
  425. sub_type_str, &array_iter)) {
  426. wpa_printf(MSG_ERROR, "dbus: "
  427. "wpas_dbus_simple_array_property_getter: out of "
  428. "memory to open container");
  429. dbus_message_unref(reply);
  430. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  431. NULL);
  432. }
  433. switch(type) {
  434. case DBUS_TYPE_BYTE:
  435. case DBUS_TYPE_BOOLEAN:
  436. element_size = 1;
  437. break;
  438. case DBUS_TYPE_INT16:
  439. case DBUS_TYPE_UINT16:
  440. element_size = sizeof(uint16_t);
  441. break;
  442. case DBUS_TYPE_INT32:
  443. case DBUS_TYPE_UINT32:
  444. element_size = sizeof(uint32_t);
  445. break;
  446. case DBUS_TYPE_INT64:
  447. case DBUS_TYPE_UINT64:
  448. element_size = sizeof(uint64_t);
  449. break;
  450. case DBUS_TYPE_DOUBLE:
  451. element_size = sizeof(double);
  452. break;
  453. case DBUS_TYPE_STRING:
  454. case DBUS_TYPE_OBJECT_PATH:
  455. element_size = sizeof(char *);
  456. break;
  457. default:
  458. wpa_printf(MSG_ERROR, "dbus: "
  459. "wpas_dbus_simple_array_property_getter: "
  460. "fatal: unknown element type");
  461. element_size = 1;
  462. break;
  463. }
  464. for (i = 0; i < array_len; i++) {
  465. dbus_message_iter_append_basic(&array_iter, type,
  466. array + i * element_size);
  467. }
  468. if (!dbus_message_iter_close_container(&variant_iter, &array_iter) ||
  469. !dbus_message_iter_close_container(&iter, &variant_iter)) {
  470. wpa_printf(MSG_ERROR, "dbus: "
  471. "wpas_dbus_simple_array_property_getter: out of "
  472. "memory to close container");
  473. dbus_message_unref(reply);
  474. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  475. NULL);
  476. }
  477. return reply;
  478. }
  479. /**
  480. * wpas_dbus_handler_create_interface - Request registration of a network iface
  481. * @message: Pointer to incoming dbus message
  482. * @global: %wpa_supplicant global data structure
  483. * Returns: The object path of the new interface object,
  484. * or a dbus error message with more information
  485. *
  486. * Handler function for "CreateInterface" method call. Handles requests
  487. * by dbus clients to register a network interface that wpa_supplicant
  488. * will manage.
  489. */
  490. DBusMessage * wpas_dbus_handler_create_interface(DBusMessage *message,
  491. struct wpa_global *global)
  492. {
  493. DBusMessageIter iter_dict;
  494. DBusMessage *reply = NULL;
  495. DBusMessageIter iter;
  496. struct wpa_dbus_dict_entry entry;
  497. char *driver = NULL;
  498. char *ifname = NULL;
  499. char *confname = NULL;
  500. char *bridge_ifname = NULL;
  501. dbus_message_iter_init(message, &iter);
  502. if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
  503. goto error;
  504. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  505. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  506. goto error;
  507. if (!strcmp(entry.key, "Driver") &&
  508. (entry.type == DBUS_TYPE_STRING)) {
  509. driver = os_strdup(entry.str_value);
  510. wpa_dbus_dict_entry_clear(&entry);
  511. if (driver == NULL)
  512. goto error;
  513. } else if (!strcmp(entry.key, "Ifname") &&
  514. (entry.type == DBUS_TYPE_STRING)) {
  515. ifname = os_strdup(entry.str_value);
  516. wpa_dbus_dict_entry_clear(&entry);
  517. if (ifname == NULL)
  518. goto error;
  519. } else if (!strcmp(entry.key, "ConfigFile") &&
  520. (entry.type == DBUS_TYPE_STRING)) {
  521. confname = os_strdup(entry.str_value);
  522. wpa_dbus_dict_entry_clear(&entry);
  523. if (confname == NULL)
  524. goto error;
  525. } else if (!strcmp(entry.key, "BridgeIfname") &&
  526. (entry.type == DBUS_TYPE_STRING)) {
  527. bridge_ifname = os_strdup(entry.str_value);
  528. wpa_dbus_dict_entry_clear(&entry);
  529. if (bridge_ifname == NULL)
  530. goto error;
  531. } else {
  532. wpa_dbus_dict_entry_clear(&entry);
  533. goto error;
  534. }
  535. }
  536. if (ifname == NULL)
  537. goto error; /* Required Ifname argument missing */
  538. /*
  539. * Try to get the wpa_supplicant record for this iface, return
  540. * an error if we already control it.
  541. */
  542. if (wpa_supplicant_get_iface(global, ifname) != NULL) {
  543. reply = dbus_message_new_error(message,
  544. WPAS_DBUS_ERROR_IFACE_EXISTS,
  545. "wpa_supplicant already "
  546. "controls this interface.");
  547. } else {
  548. struct wpa_supplicant *wpa_s;
  549. struct wpa_interface iface;
  550. os_memset(&iface, 0, sizeof(iface));
  551. iface.driver = driver;
  552. iface.ifname = ifname;
  553. iface.confname = confname;
  554. iface.bridge_ifname = bridge_ifname;
  555. /* Otherwise, have wpa_supplicant attach to it. */
  556. if ((wpa_s = wpa_supplicant_add_iface(global, &iface))) {
  557. const char *path = wpa_s->dbus_new_path;
  558. reply = dbus_message_new_method_return(message);
  559. dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
  560. &path, DBUS_TYPE_INVALID);
  561. } else {
  562. reply = wpas_dbus_error_unknown_error(
  563. message, "wpa_supplicant couldn't grab this "
  564. "interface.");
  565. }
  566. }
  567. out:
  568. os_free(driver);
  569. os_free(ifname);
  570. os_free(bridge_ifname);
  571. return reply;
  572. error:
  573. reply = wpas_dbus_error_invalid_args(message, NULL);
  574. goto out;
  575. }
  576. /**
  577. * wpas_dbus_handler_remove_interface - Request deregistration of an interface
  578. * @message: Pointer to incoming dbus message
  579. * @global: wpa_supplicant global data structure
  580. * Returns: a dbus message containing a UINT32 indicating success (1) or
  581. * failure (0), or returns a dbus error message with more information
  582. *
  583. * Handler function for "removeInterface" method call. Handles requests
  584. * by dbus clients to deregister a network interface that wpa_supplicant
  585. * currently manages.
  586. */
  587. DBusMessage * wpas_dbus_handler_remove_interface(DBusMessage *message,
  588. struct wpa_global *global)
  589. {
  590. struct wpa_supplicant *wpa_s;
  591. char *path;
  592. DBusMessage *reply = NULL;
  593. dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
  594. DBUS_TYPE_INVALID);
  595. wpa_s = get_iface_by_dbus_path(global, path);
  596. if (wpa_s == NULL)
  597. reply = wpas_dbus_error_iface_unknown(message);
  598. else if (wpa_supplicant_remove_iface(global, wpa_s)) {
  599. reply = wpas_dbus_error_unknown_error(
  600. message, "wpa_supplicant couldn't remove this "
  601. "interface.");
  602. }
  603. return reply;
  604. }
  605. /**
  606. * wpas_dbus_handler_get_interface - Get the object path for an interface name
  607. * @message: Pointer to incoming dbus message
  608. * @global: %wpa_supplicant global data structure
  609. * Returns: The object path of the interface object,
  610. * or a dbus error message with more information
  611. *
  612. * Handler function for "getInterface" method call.
  613. */
  614. DBusMessage * wpas_dbus_handler_get_interface(DBusMessage *message,
  615. struct wpa_global *global)
  616. {
  617. DBusMessage *reply = NULL;
  618. const char *ifname;
  619. const char *path;
  620. struct wpa_supplicant *wpa_s;
  621. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &ifname,
  622. DBUS_TYPE_INVALID);
  623. wpa_s = wpa_supplicant_get_iface(global, ifname);
  624. if (wpa_s == NULL)
  625. return wpas_dbus_error_iface_unknown(message);
  626. path = wpa_s->dbus_new_path;
  627. reply = dbus_message_new_method_return(message);
  628. if (reply == NULL)
  629. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  630. NULL);
  631. if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
  632. DBUS_TYPE_INVALID)) {
  633. dbus_message_unref(reply);
  634. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  635. NULL);
  636. }
  637. return reply;
  638. }
  639. /**
  640. * wpas_dbus_getter_debug_level - Get debug level
  641. * @message: Pointer to incoming dbus message
  642. * @global: %wpa_supplicant global data structure
  643. * Returns: DBus message with value of debug level
  644. *
  645. * Getter for "DebugLevel" property.
  646. */
  647. DBusMessage * wpas_dbus_getter_debug_level(DBusMessage *message,
  648. struct wpa_global *global)
  649. {
  650. const char *str;
  651. int idx = wpa_debug_level;
  652. if (idx < 0)
  653. idx = 0;
  654. if (idx > 4)
  655. idx = 4;
  656. str = debug_strings[idx];
  657. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
  658. &str);
  659. }
  660. /**
  661. * wpas_dbus_getter_debug_timestamp - Get debug timestamp
  662. * @message: Pointer to incoming dbus message
  663. * @global: %wpa_supplicant global data structure
  664. * Returns: DBus message with value of debug timestamp
  665. *
  666. * Getter for "DebugTimestamp" property.
  667. */
  668. DBusMessage * wpas_dbus_getter_debug_timestamp(DBusMessage *message,
  669. struct wpa_global *global)
  670. {
  671. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
  672. &wpa_debug_timestamp);
  673. }
  674. /**
  675. * wpas_dbus_getter_debug_show_keys - Get debug show keys
  676. * @message: Pointer to incoming dbus message
  677. * @global: %wpa_supplicant global data structure
  678. * Returns: DBus message with value of debug show_keys
  679. *
  680. * Getter for "DebugShowKeys" property.
  681. */
  682. DBusMessage * wpas_dbus_getter_debug_show_keys(DBusMessage *message,
  683. struct wpa_global *global)
  684. {
  685. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
  686. &wpa_debug_show_keys);
  687. }
  688. /**
  689. * wpas_dbus_setter_debug_level - Set debug level
  690. * @message: Pointer to incoming dbus message
  691. * @global: %wpa_supplicant global data structure
  692. * Returns: %NULL or DBus error message
  693. *
  694. * Setter for "DebugLevel" property.
  695. */
  696. DBusMessage * wpas_dbus_setter_debug_level(DBusMessage *message,
  697. struct wpa_global *global)
  698. {
  699. DBusMessage *reply;
  700. const char *str = NULL;
  701. int i, val = -1;
  702. reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_STRING,
  703. &str);
  704. if (reply)
  705. return reply;
  706. for (i = 0; debug_strings[i]; i++)
  707. if (os_strcmp(debug_strings[i], str) == 0) {
  708. val = i;
  709. break;
  710. }
  711. if (val < 0 ||
  712. wpa_supplicant_set_debug_params(global, val, wpa_debug_timestamp,
  713. wpa_debug_show_keys)) {
  714. return wpas_dbus_error_invalid_args(
  715. message, "Wrong debug level value");
  716. }
  717. return NULL;
  718. }
  719. /**
  720. * wpas_dbus_setter_debug_timestamp - Set debug timestamp
  721. * @message: Pointer to incoming dbus message
  722. * @global: %wpa_supplicant global data structure
  723. * Returns: %NULL or DBus error message
  724. *
  725. * Setter for "DebugTimestamp" property.
  726. */
  727. DBusMessage * wpas_dbus_setter_debug_timestamp(DBusMessage *message,
  728. struct wpa_global *global)
  729. {
  730. DBusMessage *reply;
  731. dbus_bool_t val;
  732. reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_BOOLEAN,
  733. &val);
  734. if (reply)
  735. return reply;
  736. wpa_supplicant_set_debug_params(global, wpa_debug_level, val ? 1 : 0,
  737. wpa_debug_show_keys);
  738. return NULL;
  739. }
  740. /**
  741. * wpas_dbus_setter_debug_show_keys - Set debug show keys
  742. * @message: Pointer to incoming dbus message
  743. * @global: %wpa_supplicant global data structure
  744. * Returns: %NULL or DBus error message
  745. *
  746. * Setter for "DebugShowKeys" property.
  747. */
  748. DBusMessage * wpas_dbus_setter_debug_show_keys(DBusMessage *message,
  749. struct wpa_global *global)
  750. {
  751. DBusMessage *reply;
  752. dbus_bool_t val;
  753. reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_BOOLEAN,
  754. &val);
  755. if (reply)
  756. return reply;
  757. wpa_supplicant_set_debug_params(global, wpa_debug_level,
  758. wpa_debug_timestamp,
  759. val ? 1 : 0);
  760. return NULL;
  761. }
  762. /**
  763. * wpas_dbus_getter_interfaces - Request registered interfaces list
  764. * @message: Pointer to incoming dbus message
  765. * @global: %wpa_supplicant global data structure
  766. * Returns: The object paths array containing registered interfaces
  767. * objects paths or DBus error on failure
  768. *
  769. * Getter for "Interfaces" property. Handles requests
  770. * by dbus clients to return list of registered interfaces objects
  771. * paths
  772. */
  773. DBusMessage * wpas_dbus_getter_interfaces(DBusMessage *message,
  774. struct wpa_global *global)
  775. {
  776. DBusMessage *reply = NULL;
  777. struct wpa_supplicant *wpa_s;
  778. const char **paths;
  779. unsigned int i = 0, num = 0;
  780. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
  781. num++;
  782. paths = os_zalloc(num * sizeof(char*));
  783. if (!paths) {
  784. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  785. NULL);
  786. }
  787. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
  788. paths[i++] = wpa_s->dbus_new_path;
  789. reply = wpas_dbus_simple_array_property_getter(message,
  790. DBUS_TYPE_OBJECT_PATH,
  791. paths, num);
  792. os_free(paths);
  793. return reply;
  794. }
  795. /**
  796. * wpas_dbus_getter_eap_methods - Request supported EAP methods list
  797. * @message: Pointer to incoming dbus message
  798. * @nothing: not used argument. may be NULL or anything else
  799. * Returns: The object paths array containing supported EAP methods
  800. * represented by strings or DBus error on failure
  801. *
  802. * Getter for "EapMethods" property. Handles requests
  803. * by dbus clients to return list of strings with supported EAP methods
  804. */
  805. DBusMessage * wpas_dbus_getter_eap_methods(DBusMessage *message, void *nothing)
  806. {
  807. DBusMessage *reply = NULL;
  808. char **eap_methods;
  809. size_t num_items = 0;
  810. eap_methods = eap_get_names_as_string_array(&num_items);
  811. if (!eap_methods) {
  812. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  813. NULL);
  814. }
  815. reply = wpas_dbus_simple_array_property_getter(message,
  816. DBUS_TYPE_STRING,
  817. eap_methods, num_items);
  818. while (num_items)
  819. os_free(eap_methods[--num_items]);
  820. os_free(eap_methods);
  821. return reply;
  822. }
  823. static int wpas_dbus_get_scan_type(DBusMessage *message, DBusMessageIter *var,
  824. char **type, DBusMessage **reply)
  825. {
  826. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_STRING) {
  827. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  828. "Type must be a string");
  829. *reply = wpas_dbus_error_invalid_args(
  830. message, "Wrong Type value type. String required");
  831. return -1;
  832. }
  833. dbus_message_iter_get_basic(var, type);
  834. return 0;
  835. }
  836. static int wpas_dbus_get_scan_ssids(DBusMessage *message, DBusMessageIter *var,
  837. struct wpa_driver_scan_params *params,
  838. DBusMessage **reply)
  839. {
  840. struct wpa_driver_scan_ssid *ssids = params->ssids;
  841. size_t ssids_num = 0;
  842. u8 *ssid;
  843. DBusMessageIter array_iter, sub_array_iter;
  844. char *val;
  845. int len;
  846. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
  847. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ssids "
  848. "must be an array of arrays of bytes");
  849. *reply = wpas_dbus_error_invalid_args(
  850. message, "Wrong SSIDs value type. Array of arrays of "
  851. "bytes required");
  852. return -1;
  853. }
  854. dbus_message_iter_recurse(var, &array_iter);
  855. if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY ||
  856. dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_BYTE)
  857. {
  858. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ssids "
  859. "must be an array of arrays of bytes");
  860. *reply = wpas_dbus_error_invalid_args(
  861. message, "Wrong SSIDs value type. Array of arrays of "
  862. "bytes required");
  863. return -1;
  864. }
  865. while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_ARRAY)
  866. {
  867. if (ssids_num >= WPAS_MAX_SCAN_SSIDS) {
  868. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  869. "Too many ssids specified on scan dbus "
  870. "call");
  871. *reply = wpas_dbus_error_invalid_args(
  872. message, "Too many ssids specified. Specify "
  873. "at most four");
  874. return -1;
  875. }
  876. dbus_message_iter_recurse(&array_iter, &sub_array_iter);
  877. dbus_message_iter_get_fixed_array(&sub_array_iter, &val, &len);
  878. if (len != 0) {
  879. ssid = os_malloc(len);
  880. if (ssid == NULL) {
  881. wpa_printf(MSG_DEBUG,
  882. "wpas_dbus_handler_scan[dbus]: "
  883. "out of memory. Cannot allocate "
  884. "memory for SSID");
  885. *reply = dbus_message_new_error(
  886. message, DBUS_ERROR_NO_MEMORY, NULL);
  887. return -1;
  888. }
  889. os_memcpy(ssid, val, len);
  890. } else {
  891. /* Allow zero-length SSIDs */
  892. ssid = NULL;
  893. }
  894. ssids[ssids_num].ssid = ssid;
  895. ssids[ssids_num].ssid_len = len;
  896. dbus_message_iter_next(&array_iter);
  897. ssids_num++;
  898. }
  899. params->num_ssids = ssids_num;
  900. return 0;
  901. }
  902. static int wpas_dbus_get_scan_ies(DBusMessage *message, DBusMessageIter *var,
  903. struct wpa_driver_scan_params *params,
  904. DBusMessage **reply)
  905. {
  906. u8 *ies = NULL, *nies;
  907. int ies_len = 0;
  908. DBusMessageIter array_iter, sub_array_iter;
  909. char *val;
  910. int len;
  911. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
  912. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ies must "
  913. "be an array of arrays of bytes");
  914. *reply = wpas_dbus_error_invalid_args(
  915. message, "Wrong IEs value type. Array of arrays of "
  916. "bytes required");
  917. return -1;
  918. }
  919. dbus_message_iter_recurse(var, &array_iter);
  920. if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY ||
  921. dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_BYTE)
  922. {
  923. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ies must "
  924. "be an array of arrays of bytes");
  925. *reply = wpas_dbus_error_invalid_args(
  926. message, "Wrong IEs value type. Array required");
  927. return -1;
  928. }
  929. while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_ARRAY)
  930. {
  931. dbus_message_iter_recurse(&array_iter, &sub_array_iter);
  932. dbus_message_iter_get_fixed_array(&sub_array_iter, &val, &len);
  933. if (len == 0) {
  934. dbus_message_iter_next(&array_iter);
  935. continue;
  936. }
  937. nies = os_realloc(ies, ies_len + len);
  938. if (nies == NULL) {
  939. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  940. "out of memory. Cannot allocate memory for "
  941. "IE");
  942. os_free(ies);
  943. *reply = dbus_message_new_error(
  944. message, DBUS_ERROR_NO_MEMORY, NULL);
  945. return -1;
  946. }
  947. ies = nies;
  948. os_memcpy(ies + ies_len, val, len);
  949. ies_len += len;
  950. dbus_message_iter_next(&array_iter);
  951. }
  952. params->extra_ies = ies;
  953. params->extra_ies_len = ies_len;
  954. return 0;
  955. }
  956. static int wpas_dbus_get_scan_channels(DBusMessage *message,
  957. DBusMessageIter *var,
  958. struct wpa_driver_scan_params *params,
  959. DBusMessage **reply)
  960. {
  961. DBusMessageIter array_iter, sub_array_iter;
  962. int *freqs = NULL, *nfreqs;
  963. int freqs_num = 0;
  964. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
  965. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  966. "Channels must be an array of structs");
  967. *reply = wpas_dbus_error_invalid_args(
  968. message, "Wrong Channels value type. Array of structs "
  969. "required");
  970. return -1;
  971. }
  972. dbus_message_iter_recurse(var, &array_iter);
  973. if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_STRUCT) {
  974. wpa_printf(MSG_DEBUG,
  975. "wpas_dbus_handler_scan[dbus]: Channels must be an "
  976. "array of structs");
  977. *reply = wpas_dbus_error_invalid_args(
  978. message, "Wrong Channels value type. Array of structs "
  979. "required");
  980. return -1;
  981. }
  982. while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_STRUCT)
  983. {
  984. int freq, width;
  985. dbus_message_iter_recurse(&array_iter, &sub_array_iter);
  986. if (dbus_message_iter_get_arg_type(&sub_array_iter) !=
  987. DBUS_TYPE_UINT32) {
  988. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  989. "Channel must by specified by struct of "
  990. "two UINT32s %c",
  991. dbus_message_iter_get_arg_type(
  992. &sub_array_iter));
  993. *reply = wpas_dbus_error_invalid_args(
  994. message, "Wrong Channel struct. Two UINT32s "
  995. "required");
  996. os_free(freqs);
  997. return -1;
  998. }
  999. dbus_message_iter_get_basic(&sub_array_iter, &freq);
  1000. if (!dbus_message_iter_next(&sub_array_iter) ||
  1001. dbus_message_iter_get_arg_type(&sub_array_iter) !=
  1002. DBUS_TYPE_UINT32) {
  1003. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1004. "Channel must by specified by struct of "
  1005. "two UINT32s");
  1006. *reply = wpas_dbus_error_invalid_args(
  1007. message,
  1008. "Wrong Channel struct. Two UINT32s required");
  1009. os_free(freqs);
  1010. return -1;
  1011. }
  1012. dbus_message_iter_get_basic(&sub_array_iter, &width);
  1013. #define FREQS_ALLOC_CHUNK 32
  1014. if (freqs_num % FREQS_ALLOC_CHUNK == 0) {
  1015. nfreqs = os_realloc(freqs, sizeof(int) *
  1016. (freqs_num + FREQS_ALLOC_CHUNK));
  1017. if (nfreqs == NULL)
  1018. os_free(freqs);
  1019. freqs = nfreqs;
  1020. }
  1021. if (freqs == NULL) {
  1022. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1023. "out of memory. can't allocate memory for "
  1024. "freqs");
  1025. *reply = dbus_message_new_error(
  1026. message, DBUS_ERROR_NO_MEMORY, NULL);
  1027. return -1;
  1028. }
  1029. freqs[freqs_num] = freq;
  1030. freqs_num++;
  1031. dbus_message_iter_next(&array_iter);
  1032. }
  1033. nfreqs = os_realloc(freqs,
  1034. sizeof(int) * (freqs_num + 1));
  1035. if (nfreqs == NULL)
  1036. os_free(freqs);
  1037. freqs = nfreqs;
  1038. if (freqs == NULL) {
  1039. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1040. "out of memory. Can't allocate memory for freqs");
  1041. *reply = dbus_message_new_error(
  1042. message, DBUS_ERROR_NO_MEMORY, NULL);
  1043. return -1;
  1044. }
  1045. freqs[freqs_num] = 0;
  1046. params->freqs = freqs;
  1047. return 0;
  1048. }
  1049. /**
  1050. * wpas_dbus_handler_scan - Request a wireless scan on an interface
  1051. * @message: Pointer to incoming dbus message
  1052. * @wpa_s: wpa_supplicant structure for a network interface
  1053. * Returns: NULL indicating success or DBus error message on failure
  1054. *
  1055. * Handler function for "Scan" method call of a network device. Requests
  1056. * that wpa_supplicant perform a wireless scan as soon as possible
  1057. * on a particular wireless interface.
  1058. */
  1059. DBusMessage * wpas_dbus_handler_scan(DBusMessage *message,
  1060. struct wpa_supplicant *wpa_s)
  1061. {
  1062. DBusMessage *reply = NULL;
  1063. DBusMessageIter iter, dict_iter, entry_iter, variant_iter;
  1064. char *key = NULL, *type = NULL;
  1065. struct wpa_driver_scan_params params;
  1066. size_t i;
  1067. os_memset(&params, 0, sizeof(params));
  1068. dbus_message_iter_init(message, &iter);
  1069. dbus_message_iter_recurse(&iter, &dict_iter);
  1070. while (dbus_message_iter_get_arg_type(&dict_iter) ==
  1071. DBUS_TYPE_DICT_ENTRY) {
  1072. dbus_message_iter_recurse(&dict_iter, &entry_iter);
  1073. dbus_message_iter_get_basic(&entry_iter, &key);
  1074. dbus_message_iter_next(&entry_iter);
  1075. dbus_message_iter_recurse(&entry_iter, &variant_iter);
  1076. if (os_strcmp(key, "Type") == 0) {
  1077. if (wpas_dbus_get_scan_type(message, &variant_iter,
  1078. &type, &reply) < 0)
  1079. goto out;
  1080. } else if (os_strcmp(key, "SSIDs") == 0) {
  1081. if (wpas_dbus_get_scan_ssids(message, &variant_iter,
  1082. &params, &reply) < 0)
  1083. goto out;
  1084. } else if (os_strcmp(key, "IEs") == 0) {
  1085. if (wpas_dbus_get_scan_ies(message, &variant_iter,
  1086. &params, &reply) < 0)
  1087. goto out;
  1088. } else if (os_strcmp(key, "Channels") == 0) {
  1089. if (wpas_dbus_get_scan_channels(message, &variant_iter,
  1090. &params, &reply) < 0)
  1091. goto out;
  1092. } else {
  1093. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1094. "Unknown argument %s", key);
  1095. reply = wpas_dbus_error_invalid_args(message, key);
  1096. goto out;
  1097. }
  1098. dbus_message_iter_next(&dict_iter);
  1099. }
  1100. if (!type) {
  1101. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1102. "Scan type not specified");
  1103. reply = wpas_dbus_error_invalid_args(message, key);
  1104. goto out;
  1105. }
  1106. if (!os_strcmp(type, "passive")) {
  1107. if (params.num_ssids || params.extra_ies_len) {
  1108. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1109. "SSIDs or IEs specified for passive scan.");
  1110. reply = wpas_dbus_error_invalid_args(
  1111. message, "You can specify only Channels in "
  1112. "passive scan");
  1113. goto out;
  1114. } else if (params.freqs && params.freqs[0]) {
  1115. wpa_supplicant_trigger_scan(wpa_s, &params);
  1116. } else {
  1117. wpa_s->scan_req = 2;
  1118. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1119. }
  1120. } else if (!os_strcmp(type, "active")) {
  1121. if (!params.num_ssids) {
  1122. /* Add wildcard ssid */
  1123. params.num_ssids++;
  1124. }
  1125. wpa_supplicant_trigger_scan(wpa_s, &params);
  1126. } else {
  1127. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1128. "Unknown scan type: %s", type);
  1129. reply = wpas_dbus_error_invalid_args(message,
  1130. "Wrong scan type");
  1131. goto out;
  1132. }
  1133. out:
  1134. for (i = 0; i < WPAS_MAX_SCAN_SSIDS; i++)
  1135. os_free((u8 *) params.ssids[i].ssid);
  1136. os_free((u8 *) params.extra_ies);
  1137. os_free(params.freqs);
  1138. return reply;
  1139. }
  1140. /*
  1141. * wpas_dbus_handler_disconnect - Terminate the current connection
  1142. * @message: Pointer to incoming dbus message
  1143. * @wpa_s: wpa_supplicant structure for a network interface
  1144. * Returns: NotConnected DBus error message if already not connected
  1145. * or NULL otherwise.
  1146. *
  1147. * Handler function for "Disconnect" method call of network interface.
  1148. */
  1149. DBusMessage * wpas_dbus_handler_disconnect(DBusMessage *message,
  1150. struct wpa_supplicant *wpa_s)
  1151. {
  1152. if (wpa_s->current_ssid != NULL) {
  1153. wpa_s->disconnected = 1;
  1154. wpa_supplicant_deauthenticate(wpa_s,
  1155. WLAN_REASON_DEAUTH_LEAVING);
  1156. return NULL;
  1157. }
  1158. return dbus_message_new_error(message, WPAS_DBUS_ERROR_NOT_CONNECTED,
  1159. "This interface is not connected");
  1160. }
  1161. /**
  1162. * wpas_dbus_new_iface_add_network - Add a new configured network
  1163. * @message: Pointer to incoming dbus message
  1164. * @wpa_s: wpa_supplicant structure for a network interface
  1165. * Returns: A dbus message containing the object path of the new network
  1166. *
  1167. * Handler function for "AddNetwork" method call of a network interface.
  1168. */
  1169. DBusMessage * wpas_dbus_handler_add_network(DBusMessage *message,
  1170. struct wpa_supplicant *wpa_s)
  1171. {
  1172. DBusMessage *reply = NULL;
  1173. DBusMessageIter iter;
  1174. struct wpa_ssid *ssid = NULL;
  1175. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *path = path_buf;
  1176. dbus_message_iter_init(message, &iter);
  1177. ssid = wpa_config_add_network(wpa_s->conf);
  1178. if (ssid == NULL) {
  1179. wpa_printf(MSG_ERROR, "wpas_dbus_handler_add_network[dbus]: "
  1180. "can't add new interface.");
  1181. reply = wpas_dbus_error_unknown_error(
  1182. message,
  1183. "wpa_supplicant could not add "
  1184. "a network on this interface.");
  1185. goto err;
  1186. }
  1187. wpas_notify_network_added(wpa_s, ssid);
  1188. ssid->disabled = 1;
  1189. wpa_config_set_network_defaults(ssid);
  1190. reply = set_network_properties(message, wpa_s, ssid, &iter);
  1191. if (reply) {
  1192. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_add_network[dbus]:"
  1193. "control interface couldn't set network "
  1194. "properties");
  1195. goto err;
  1196. }
  1197. /* Construct the object path for this network. */
  1198. os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  1199. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
  1200. wpa_s->dbus_new_path, ssid->id);
  1201. reply = dbus_message_new_method_return(message);
  1202. if (reply == NULL) {
  1203. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1204. NULL);
  1205. goto err;
  1206. }
  1207. if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
  1208. DBUS_TYPE_INVALID)) {
  1209. dbus_message_unref(reply);
  1210. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1211. NULL);
  1212. goto err;
  1213. }
  1214. return reply;
  1215. err:
  1216. if (ssid) {
  1217. wpas_notify_network_removed(wpa_s, ssid);
  1218. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1219. }
  1220. return reply;
  1221. }
  1222. /**
  1223. * wpas_dbus_handler_remove_network - Remove a configured network
  1224. * @message: Pointer to incoming dbus message
  1225. * @wpa_s: wpa_supplicant structure for a network interface
  1226. * Returns: NULL on success or dbus error on failure
  1227. *
  1228. * Handler function for "RemoveNetwork" method call of a network interface.
  1229. */
  1230. DBusMessage * wpas_dbus_handler_remove_network(DBusMessage *message,
  1231. struct wpa_supplicant *wpa_s)
  1232. {
  1233. DBusMessage *reply = NULL;
  1234. const char *op;
  1235. char *iface = NULL, *net_id = NULL;
  1236. int id;
  1237. struct wpa_ssid *ssid;
  1238. dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
  1239. DBUS_TYPE_INVALID);
  1240. /* Extract the network ID and ensure the network */
  1241. /* is actually a child of this interface */
  1242. iface = wpas_dbus_new_decompose_object_path(op, &net_id, NULL);
  1243. if (iface == NULL || os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1244. reply = wpas_dbus_error_invalid_args(message, op);
  1245. goto out;
  1246. }
  1247. id = strtoul(net_id, NULL, 10);
  1248. if (errno == EINVAL) {
  1249. reply = wpas_dbus_error_invalid_args(message, op);
  1250. goto out;
  1251. }
  1252. ssid = wpa_config_get_network(wpa_s->conf, id);
  1253. if (ssid == NULL) {
  1254. reply = wpas_dbus_error_network_unknown(message);
  1255. goto out;
  1256. }
  1257. wpas_notify_network_removed(wpa_s, ssid);
  1258. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  1259. wpa_printf(MSG_ERROR,
  1260. "wpas_dbus_handler_remove_network[dbus]: "
  1261. "error occurred when removing network %d", id);
  1262. reply = wpas_dbus_error_unknown_error(
  1263. message, "error removing the specified network on "
  1264. "this interface.");
  1265. goto out;
  1266. }
  1267. if (ssid == wpa_s->current_ssid)
  1268. wpa_supplicant_deauthenticate(wpa_s,
  1269. WLAN_REASON_DEAUTH_LEAVING);
  1270. out:
  1271. os_free(iface);
  1272. os_free(net_id);
  1273. return reply;
  1274. }
  1275. static void remove_network(void *arg, struct wpa_ssid *ssid)
  1276. {
  1277. struct wpa_supplicant *wpa_s = arg;
  1278. wpas_notify_network_removed(wpa_s, ssid);
  1279. if (wpa_config_remove_network(wpa_s->conf, ssid->id) < 0) {
  1280. wpa_printf(MSG_ERROR,
  1281. "wpas_dbus_handler_remove_all_networks[dbus]: "
  1282. "error occurred when removing network %d",
  1283. ssid->id);
  1284. return;
  1285. }
  1286. if (ssid == wpa_s->current_ssid)
  1287. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1288. }
  1289. /**
  1290. * wpas_dbus_handler_remove_all_networks - Remove all configured networks
  1291. * @message: Pointer to incoming dbus message
  1292. * @wpa_s: wpa_supplicant structure for a network interface
  1293. * Returns: NULL on success or dbus error on failure
  1294. *
  1295. * Handler function for "RemoveAllNetworks" method call of a network interface.
  1296. */
  1297. DBusMessage * wpas_dbus_handler_remove_all_networks(
  1298. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1299. {
  1300. /* NB: could check for failure and return an error */
  1301. wpa_config_foreach_network(wpa_s->conf, remove_network, wpa_s);
  1302. return NULL;
  1303. }
  1304. /**
  1305. * wpas_dbus_handler_select_network - Attempt association with a network
  1306. * @message: Pointer to incoming dbus message
  1307. * @wpa_s: wpa_supplicant structure for a network interface
  1308. * Returns: NULL on success or dbus error on failure
  1309. *
  1310. * Handler function for "SelectNetwork" method call of network interface.
  1311. */
  1312. DBusMessage * wpas_dbus_handler_select_network(DBusMessage *message,
  1313. struct wpa_supplicant *wpa_s)
  1314. {
  1315. DBusMessage *reply = NULL;
  1316. const char *op;
  1317. char *iface = NULL, *net_id = NULL;
  1318. int id;
  1319. struct wpa_ssid *ssid;
  1320. dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
  1321. DBUS_TYPE_INVALID);
  1322. /* Extract the network ID and ensure the network */
  1323. /* is actually a child of this interface */
  1324. iface = wpas_dbus_new_decompose_object_path(op, &net_id, NULL);
  1325. if (iface == NULL || os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1326. reply = wpas_dbus_error_invalid_args(message, op);
  1327. goto out;
  1328. }
  1329. id = strtoul(net_id, NULL, 10);
  1330. if (errno == EINVAL) {
  1331. reply = wpas_dbus_error_invalid_args(message, op);
  1332. goto out;
  1333. }
  1334. ssid = wpa_config_get_network(wpa_s->conf, id);
  1335. if (ssid == NULL) {
  1336. reply = wpas_dbus_error_network_unknown(message);
  1337. goto out;
  1338. }
  1339. /* Finally, associate with the network */
  1340. wpa_supplicant_select_network(wpa_s, ssid);
  1341. out:
  1342. os_free(iface);
  1343. os_free(net_id);
  1344. return reply;
  1345. }
  1346. /**
  1347. * wpas_dbus_handler_add_blob - Store named binary blob (ie, for certificates)
  1348. * @message: Pointer to incoming dbus message
  1349. * @wpa_s: %wpa_supplicant data structure
  1350. * Returns: A dbus message containing an error on failure or NULL on success
  1351. *
  1352. * Asks wpa_supplicant to internally store a binary blobs.
  1353. */
  1354. DBusMessage * wpas_dbus_handler_add_blob(DBusMessage *message,
  1355. struct wpa_supplicant *wpa_s)
  1356. {
  1357. DBusMessage *reply = NULL;
  1358. DBusMessageIter iter, array_iter;
  1359. char *blob_name;
  1360. u8 *blob_data;
  1361. int blob_len;
  1362. struct wpa_config_blob *blob = NULL;
  1363. dbus_message_iter_init(message, &iter);
  1364. dbus_message_iter_get_basic(&iter, &blob_name);
  1365. if (wpa_config_get_blob(wpa_s->conf, blob_name)) {
  1366. return dbus_message_new_error(message,
  1367. WPAS_DBUS_ERROR_BLOB_EXISTS,
  1368. NULL);
  1369. }
  1370. dbus_message_iter_next(&iter);
  1371. dbus_message_iter_recurse(&iter, &array_iter);
  1372. dbus_message_iter_get_fixed_array(&array_iter, &blob_data, &blob_len);
  1373. blob = os_zalloc(sizeof(*blob));
  1374. if (!blob) {
  1375. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1376. NULL);
  1377. goto err;
  1378. }
  1379. blob->data = os_malloc(blob_len);
  1380. if (!blob->data) {
  1381. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1382. NULL);
  1383. goto err;
  1384. }
  1385. os_memcpy(blob->data, blob_data, blob_len);
  1386. blob->len = blob_len;
  1387. blob->name = os_strdup(blob_name);
  1388. if (!blob->name) {
  1389. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1390. NULL);
  1391. goto err;
  1392. }
  1393. wpa_config_set_blob(wpa_s->conf, blob);
  1394. wpas_notify_blob_added(wpa_s, blob->name);
  1395. return reply;
  1396. err:
  1397. if (blob) {
  1398. os_free(blob->name);
  1399. os_free(blob->data);
  1400. os_free(blob);
  1401. }
  1402. return reply;
  1403. }
  1404. /**
  1405. * wpas_dbus_handler_get_blob - Get named binary blob (ie, for certificates)
  1406. * @message: Pointer to incoming dbus message
  1407. * @wpa_s: %wpa_supplicant data structure
  1408. * Returns: A dbus message containing array of bytes (blob)
  1409. *
  1410. * Gets one wpa_supplicant's binary blobs.
  1411. */
  1412. DBusMessage * wpas_dbus_handler_get_blob(DBusMessage *message,
  1413. struct wpa_supplicant *wpa_s)
  1414. {
  1415. DBusMessage *reply = NULL;
  1416. DBusMessageIter iter, array_iter;
  1417. char *blob_name;
  1418. const struct wpa_config_blob *blob;
  1419. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &blob_name,
  1420. DBUS_TYPE_INVALID);
  1421. blob = wpa_config_get_blob(wpa_s->conf, blob_name);
  1422. if (!blob) {
  1423. return dbus_message_new_error(message,
  1424. WPAS_DBUS_ERROR_BLOB_UNKNOWN,
  1425. "Blob id not set");
  1426. }
  1427. reply = dbus_message_new_method_return(message);
  1428. if (!reply) {
  1429. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1430. NULL);
  1431. goto out;
  1432. }
  1433. dbus_message_iter_init_append(reply, &iter);
  1434. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
  1435. DBUS_TYPE_BYTE_AS_STRING,
  1436. &array_iter)) {
  1437. dbus_message_unref(reply);
  1438. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1439. NULL);
  1440. goto out;
  1441. }
  1442. if (!dbus_message_iter_append_fixed_array(&array_iter, DBUS_TYPE_BYTE,
  1443. &(blob->data), blob->len)) {
  1444. dbus_message_unref(reply);
  1445. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1446. NULL);
  1447. goto out;
  1448. }
  1449. if (!dbus_message_iter_close_container(&iter, &array_iter)) {
  1450. dbus_message_unref(reply);
  1451. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1452. NULL);
  1453. goto out;
  1454. }
  1455. out:
  1456. return reply;
  1457. }
  1458. /**
  1459. * wpas_remove_handler_remove_blob - Remove named binary blob
  1460. * @message: Pointer to incoming dbus message
  1461. * @wpa_s: %wpa_supplicant data structure
  1462. * Returns: NULL on success or dbus error
  1463. *
  1464. * Asks wpa_supplicant to internally remove a binary blobs.
  1465. */
  1466. DBusMessage * wpas_dbus_handler_remove_blob(DBusMessage *message,
  1467. struct wpa_supplicant *wpa_s)
  1468. {
  1469. DBusMessage *reply = NULL;
  1470. char *blob_name;
  1471. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &blob_name,
  1472. DBUS_TYPE_INVALID);
  1473. if (wpa_config_remove_blob(wpa_s->conf, blob_name)) {
  1474. return dbus_message_new_error(message,
  1475. WPAS_DBUS_ERROR_BLOB_UNKNOWN,
  1476. "Blob id not set");
  1477. }
  1478. wpas_notify_blob_removed(wpa_s, blob_name);
  1479. return reply;
  1480. }
  1481. /*
  1482. * wpas_dbus_handler_flush_bss - Flush the BSS cache
  1483. * @message: Pointer to incoming dbus message
  1484. * @wpa_s: wpa_supplicant structure for a network interface
  1485. * Returns: NULL
  1486. *
  1487. * Handler function for "FlushBSS" method call of network interface.
  1488. */
  1489. DBusMessage * wpas_dbus_handler_flush_bss(DBusMessage *message,
  1490. struct wpa_supplicant *wpa_s)
  1491. {
  1492. dbus_uint32_t age;
  1493. dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &age,
  1494. DBUS_TYPE_INVALID);
  1495. if (age == 0)
  1496. wpa_bss_flush(wpa_s);
  1497. else
  1498. wpa_bss_flush_by_age(wpa_s, age);
  1499. return NULL;
  1500. }
  1501. /**
  1502. * wpas_dbus_getter_capabilities - Return interface capabilities
  1503. * @message: Pointer to incoming dbus message
  1504. * @wpa_s: wpa_supplicant structure for a network interface
  1505. * Returns: A dbus message containing a dict of strings
  1506. *
  1507. * Getter for "Capabilities" property of an interface.
  1508. */
  1509. DBusMessage * wpas_dbus_getter_capabilities(DBusMessage *message,
  1510. struct wpa_supplicant *wpa_s)
  1511. {
  1512. DBusMessage *reply = NULL;
  1513. struct wpa_driver_capa capa;
  1514. int res;
  1515. DBusMessageIter iter, iter_dict;
  1516. DBusMessageIter iter_dict_entry, iter_dict_val, iter_array,
  1517. variant_iter;
  1518. const char *scans[] = { "active", "passive", "ssid" };
  1519. const char *modes[] = { "infrastructure", "ad-hoc", "ap" };
  1520. int n = sizeof(modes) / sizeof(char *);
  1521. if (message == NULL)
  1522. reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
  1523. else
  1524. reply = dbus_message_new_method_return(message);
  1525. if (!reply)
  1526. goto nomem;
  1527. dbus_message_iter_init_append(reply, &iter);
  1528. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
  1529. "a{sv}", &variant_iter))
  1530. goto nomem;
  1531. if (!wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
  1532. goto nomem;
  1533. res = wpa_drv_get_capa(wpa_s, &capa);
  1534. /***** pairwise cipher */
  1535. if (res < 0) {
  1536. const char *args[] = {"ccmp", "tkip", "none"};
  1537. if (!wpa_dbus_dict_append_string_array(
  1538. &iter_dict, "Pairwise", args,
  1539. sizeof(args) / sizeof(char*)))
  1540. goto nomem;
  1541. } else {
  1542. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Pairwise",
  1543. &iter_dict_entry,
  1544. &iter_dict_val,
  1545. &iter_array))
  1546. goto nomem;
  1547. if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1548. if (!wpa_dbus_dict_string_array_add_element(
  1549. &iter_array, "ccmp"))
  1550. goto nomem;
  1551. }
  1552. if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1553. if (!wpa_dbus_dict_string_array_add_element(
  1554. &iter_array, "tkip"))
  1555. goto nomem;
  1556. }
  1557. if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1558. if (!wpa_dbus_dict_string_array_add_element(
  1559. &iter_array, "none"))
  1560. goto nomem;
  1561. }
  1562. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  1563. &iter_dict_entry,
  1564. &iter_dict_val,
  1565. &iter_array))
  1566. goto nomem;
  1567. }
  1568. /***** group cipher */
  1569. if (res < 0) {
  1570. const char *args[] = {
  1571. "ccmp", "tkip", "wep104", "wep40"
  1572. };
  1573. if (!wpa_dbus_dict_append_string_array(
  1574. &iter_dict, "Group", args,
  1575. sizeof(args) / sizeof(char*)))
  1576. goto nomem;
  1577. } else {
  1578. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Group",
  1579. &iter_dict_entry,
  1580. &iter_dict_val,
  1581. &iter_array))
  1582. goto nomem;
  1583. if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1584. if (!wpa_dbus_dict_string_array_add_element(
  1585. &iter_array, "ccmp"))
  1586. goto nomem;
  1587. }
  1588. if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1589. if (!wpa_dbus_dict_string_array_add_element(
  1590. &iter_array, "tkip"))
  1591. goto nomem;
  1592. }
  1593. if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  1594. if (!wpa_dbus_dict_string_array_add_element(
  1595. &iter_array, "wep104"))
  1596. goto nomem;
  1597. }
  1598. if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  1599. if (!wpa_dbus_dict_string_array_add_element(
  1600. &iter_array, "wep40"))
  1601. goto nomem;
  1602. }
  1603. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  1604. &iter_dict_entry,
  1605. &iter_dict_val,
  1606. &iter_array))
  1607. goto nomem;
  1608. }
  1609. /***** key management */
  1610. if (res < 0) {
  1611. const char *args[] = {
  1612. "wpa-psk", "wpa-eap", "ieee8021x", "wpa-none",
  1613. #ifdef CONFIG_WPS
  1614. "wps",
  1615. #endif /* CONFIG_WPS */
  1616. "none"
  1617. };
  1618. if (!wpa_dbus_dict_append_string_array(
  1619. &iter_dict, "KeyMgmt", args,
  1620. sizeof(args) / sizeof(char*)))
  1621. goto nomem;
  1622. } else {
  1623. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "KeyMgmt",
  1624. &iter_dict_entry,
  1625. &iter_dict_val,
  1626. &iter_array))
  1627. goto nomem;
  1628. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  1629. "none"))
  1630. goto nomem;
  1631. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  1632. "ieee8021x"))
  1633. goto nomem;
  1634. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1635. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  1636. if (!wpa_dbus_dict_string_array_add_element(
  1637. &iter_array, "wpa-eap"))
  1638. goto nomem;
  1639. if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT)
  1640. if (!wpa_dbus_dict_string_array_add_element(
  1641. &iter_array, "wpa-ft-eap"))
  1642. goto nomem;
  1643. /* TODO: Ensure that driver actually supports sha256 encryption. */
  1644. #ifdef CONFIG_IEEE80211W
  1645. if (!wpa_dbus_dict_string_array_add_element(
  1646. &iter_array, "wpa-eap-sha256"))
  1647. goto nomem;
  1648. #endif /* CONFIG_IEEE80211W */
  1649. }
  1650. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  1651. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1652. if (!wpa_dbus_dict_string_array_add_element(
  1653. &iter_array, "wpa-psk"))
  1654. goto nomem;
  1655. if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK)
  1656. if (!wpa_dbus_dict_string_array_add_element(
  1657. &iter_array, "wpa-ft-psk"))
  1658. goto nomem;
  1659. /* TODO: Ensure that driver actually supports sha256 encryption. */
  1660. #ifdef CONFIG_IEEE80211W
  1661. if (!wpa_dbus_dict_string_array_add_element(
  1662. &iter_array, "wpa-psk-sha256"))
  1663. goto nomem;
  1664. #endif /* CONFIG_IEEE80211W */
  1665. }
  1666. if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1667. if (!wpa_dbus_dict_string_array_add_element(
  1668. &iter_array, "wpa-none"))
  1669. goto nomem;
  1670. }
  1671. #ifdef CONFIG_WPS
  1672. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  1673. "wps"))
  1674. goto nomem;
  1675. #endif /* CONFIG_WPS */
  1676. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  1677. &iter_dict_entry,
  1678. &iter_dict_val,
  1679. &iter_array))
  1680. goto nomem;
  1681. }
  1682. /***** WPA protocol */
  1683. if (res < 0) {
  1684. const char *args[] = { "rsn", "wpa" };
  1685. if (!wpa_dbus_dict_append_string_array(
  1686. &iter_dict, "Protocol", args,
  1687. sizeof(args) / sizeof(char*)))
  1688. goto nomem;
  1689. } else {
  1690. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Protocol",
  1691. &iter_dict_entry,
  1692. &iter_dict_val,
  1693. &iter_array))
  1694. goto nomem;
  1695. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  1696. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1697. if (!wpa_dbus_dict_string_array_add_element(
  1698. &iter_array, "rsn"))
  1699. goto nomem;
  1700. }
  1701. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1702. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  1703. if (!wpa_dbus_dict_string_array_add_element(
  1704. &iter_array, "wpa"))
  1705. goto nomem;
  1706. }
  1707. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  1708. &iter_dict_entry,
  1709. &iter_dict_val,
  1710. &iter_array))
  1711. goto nomem;
  1712. }
  1713. /***** auth alg */
  1714. if (res < 0) {
  1715. const char *args[] = { "open", "shared", "leap" };
  1716. if (!wpa_dbus_dict_append_string_array(
  1717. &iter_dict, "AuthAlg", args,
  1718. sizeof(args) / sizeof(char*)))
  1719. goto nomem;
  1720. } else {
  1721. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "AuthAlg",
  1722. &iter_dict_entry,
  1723. &iter_dict_val,
  1724. &iter_array))
  1725. goto nomem;
  1726. if (capa.auth & (WPA_DRIVER_AUTH_OPEN)) {
  1727. if (!wpa_dbus_dict_string_array_add_element(
  1728. &iter_array, "open"))
  1729. goto nomem;
  1730. }
  1731. if (capa.auth & (WPA_DRIVER_AUTH_SHARED)) {
  1732. if (!wpa_dbus_dict_string_array_add_element(
  1733. &iter_array, "shared"))
  1734. goto nomem;
  1735. }
  1736. if (capa.auth & (WPA_DRIVER_AUTH_LEAP)) {
  1737. if (!wpa_dbus_dict_string_array_add_element(
  1738. &iter_array, "leap"))
  1739. goto nomem;
  1740. }
  1741. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  1742. &iter_dict_entry,
  1743. &iter_dict_val,
  1744. &iter_array))
  1745. goto nomem;
  1746. }
  1747. /***** Scan */
  1748. if (!wpa_dbus_dict_append_string_array(&iter_dict, "Scan", scans,
  1749. sizeof(scans) / sizeof(char *)))
  1750. goto nomem;
  1751. /***** Modes */
  1752. if (res < 0 || !(capa.flags & WPA_DRIVER_FLAGS_AP))
  1753. n--; /* exclude ap mode if it is not supported by the driver */
  1754. if (!wpa_dbus_dict_append_string_array(&iter_dict, "Modes", modes, n))
  1755. goto nomem;
  1756. if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict))
  1757. goto nomem;
  1758. if (!dbus_message_iter_close_container(&iter, &variant_iter))
  1759. goto nomem;
  1760. return reply;
  1761. nomem:
  1762. if (reply)
  1763. dbus_message_unref(reply);
  1764. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY, NULL);
  1765. }
  1766. /**
  1767. * wpas_dbus_getter_state - Get interface state
  1768. * @message: Pointer to incoming dbus message
  1769. * @wpa_s: wpa_supplicant structure for a network interface
  1770. * Returns: A dbus message containing a STRING representing the current
  1771. * interface state
  1772. *
  1773. * Getter for "State" property.
  1774. */
  1775. DBusMessage * wpas_dbus_getter_state(DBusMessage *message,
  1776. struct wpa_supplicant *wpa_s)
  1777. {
  1778. DBusMessage *reply = NULL;
  1779. const char *str_state;
  1780. char *state_ls, *tmp;
  1781. str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
  1782. /* make state string lowercase to fit new DBus API convention
  1783. */
  1784. state_ls = tmp = os_strdup(str_state);
  1785. if (!tmp) {
  1786. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1787. NULL);
  1788. }
  1789. while (*tmp) {
  1790. *tmp = tolower(*tmp);
  1791. tmp++;
  1792. }
  1793. reply = wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
  1794. &state_ls);
  1795. os_free(state_ls);
  1796. return reply;
  1797. }
  1798. /**
  1799. * wpas_dbus_new_iface_get_scanning - Get interface scanning state
  1800. * @message: Pointer to incoming dbus message
  1801. * @wpa_s: wpa_supplicant structure for a network interface
  1802. * Returns: A dbus message containing whether the interface is scanning
  1803. *
  1804. * Getter for "scanning" property.
  1805. */
  1806. DBusMessage * wpas_dbus_getter_scanning(DBusMessage *message,
  1807. struct wpa_supplicant *wpa_s)
  1808. {
  1809. dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
  1810. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
  1811. &scanning);
  1812. }
  1813. /**
  1814. * wpas_dbus_getter_ap_scan - Control roaming mode
  1815. * @message: Pointer to incoming dbus message
  1816. * @wpa_s: wpa_supplicant structure for a network interface
  1817. * Returns: A message containong value of ap_scan variable
  1818. *
  1819. * Getter function for "ApScan" property.
  1820. */
  1821. DBusMessage * wpas_dbus_getter_ap_scan(DBusMessage *message,
  1822. struct wpa_supplicant *wpa_s)
  1823. {
  1824. dbus_uint32_t ap_scan = wpa_s->conf->ap_scan;
  1825. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_UINT32,
  1826. &ap_scan);
  1827. }
  1828. /**
  1829. * wpas_dbus_setter_ap_scan - Control roaming mode
  1830. * @message: Pointer to incoming dbus message
  1831. * @wpa_s: wpa_supplicant structure for a network interface
  1832. * Returns: NULL
  1833. *
  1834. * Setter function for "ApScan" property.
  1835. */
  1836. DBusMessage * wpas_dbus_setter_ap_scan(DBusMessage *message,
  1837. struct wpa_supplicant *wpa_s)
  1838. {
  1839. DBusMessage *reply = NULL;
  1840. dbus_uint32_t ap_scan;
  1841. reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_UINT32,
  1842. &ap_scan);
  1843. if (reply)
  1844. return reply;
  1845. if (wpa_supplicant_set_ap_scan(wpa_s, ap_scan)) {
  1846. return wpas_dbus_error_invalid_args(
  1847. message, "ap_scan must equal 0, 1 or 2");
  1848. }
  1849. return NULL;
  1850. }
  1851. /**
  1852. * wpas_dbus_getter_ifname - Get interface name
  1853. * @message: Pointer to incoming dbus message
  1854. * @wpa_s: wpa_supplicant structure for a network interface
  1855. * Returns: A dbus message containing a name of network interface
  1856. * associated with with wpa_s
  1857. *
  1858. * Getter for "Ifname" property.
  1859. */
  1860. DBusMessage * wpas_dbus_getter_ifname(DBusMessage *message,
  1861. struct wpa_supplicant *wpa_s)
  1862. {
  1863. const char *ifname = wpa_s->ifname;
  1864. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
  1865. &ifname);
  1866. }
  1867. /**
  1868. * wpas_dbus_getter_driver - Get interface name
  1869. * @message: Pointer to incoming dbus message
  1870. * @wpa_s: wpa_supplicant structure for a network interface
  1871. * Returns: A dbus message containing a name of network interface
  1872. * driver associated with with wpa_s
  1873. *
  1874. * Getter for "Driver" property.
  1875. */
  1876. DBusMessage * wpas_dbus_getter_driver(DBusMessage *message,
  1877. struct wpa_supplicant *wpa_s)
  1878. {
  1879. const char *driver;
  1880. if (wpa_s->driver == NULL || wpa_s->driver->name == NULL) {
  1881. wpa_printf(MSG_DEBUG, "wpas_dbus_getter_driver[dbus]: "
  1882. "wpa_s has no driver set");
  1883. return wpas_dbus_error_unknown_error(message, NULL);
  1884. }
  1885. driver = wpa_s->driver->name;
  1886. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
  1887. &driver);
  1888. }
  1889. /**
  1890. * wpas_dbus_getter_current_bss - Get current bss object path
  1891. * @message: Pointer to incoming dbus message
  1892. * @wpa_s: wpa_supplicant structure for a network interface
  1893. * Returns: A dbus message containing a DBus object path to
  1894. * current BSS
  1895. *
  1896. * Getter for "CurrentBSS" property.
  1897. */
  1898. DBusMessage * wpas_dbus_getter_current_bss(DBusMessage *message,
  1899. struct wpa_supplicant *wpa_s)
  1900. {
  1901. DBusMessage *reply;
  1902. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *bss_obj_path = path_buf;
  1903. if (wpa_s->current_bss)
  1904. os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  1905. "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
  1906. wpa_s->dbus_new_path, wpa_s->current_bss->id);
  1907. else
  1908. os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
  1909. reply = wpas_dbus_simple_property_getter(message,
  1910. DBUS_TYPE_OBJECT_PATH,
  1911. &bss_obj_path);
  1912. return reply;
  1913. }
  1914. /**
  1915. * wpas_dbus_getter_current_network - Get current network object path
  1916. * @message: Pointer to incoming dbus message
  1917. * @wpa_s: wpa_supplicant structure for a network interface
  1918. * Returns: A dbus message containing a DBus object path to
  1919. * current network
  1920. *
  1921. * Getter for "CurrentNetwork" property.
  1922. */
  1923. DBusMessage * wpas_dbus_getter_current_network(DBusMessage *message,
  1924. struct wpa_supplicant *wpa_s)
  1925. {
  1926. DBusMessage *reply;
  1927. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *net_obj_path = path_buf;
  1928. if (wpa_s->current_ssid)
  1929. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  1930. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
  1931. wpa_s->dbus_new_path, wpa_s->current_ssid->id);
  1932. else
  1933. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
  1934. reply = wpas_dbus_simple_property_getter(message,
  1935. DBUS_TYPE_OBJECT_PATH,
  1936. &net_obj_path);
  1937. return reply;
  1938. }
  1939. /**
  1940. * wpas_dbus_getter_current_auth_mode - Get current authentication type
  1941. * @message: Pointer to incoming dbus message
  1942. * @wpa_s: wpa_supplicant structure for a network interface
  1943. * Returns: A dbus message containing a string indicating the current
  1944. * authentication type.
  1945. *
  1946. * Getter for "CurrentAuthMode" property.
  1947. */
  1948. DBusMessage * wpas_dbus_getter_current_auth_mode(DBusMessage *message,
  1949. struct wpa_supplicant *wpa_s)
  1950. {
  1951. DBusMessage *reply;
  1952. const char *eap_mode;
  1953. const char *auth_mode;
  1954. char eap_mode_buf[WPAS_DBUS_AUTH_MODE_MAX];
  1955. if (wpa_s->wpa_state != WPA_COMPLETED) {
  1956. auth_mode = "INACTIVE";
  1957. } else if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
  1958. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1959. eap_mode = wpa_supplicant_get_eap_mode(wpa_s);
  1960. os_snprintf(eap_mode_buf, WPAS_DBUS_AUTH_MODE_MAX,
  1961. "EAP-%s", eap_mode);
  1962. auth_mode = eap_mode_buf;
  1963. } else {
  1964. auth_mode = wpa_key_mgmt_txt(wpa_s->key_mgmt,
  1965. wpa_s->current_ssid->proto);
  1966. }
  1967. reply = wpas_dbus_simple_property_getter(message,
  1968. DBUS_TYPE_STRING,
  1969. &auth_mode);
  1970. return reply;
  1971. }
  1972. /**
  1973. * wpas_dbus_getter_bridge_ifname - Get interface name
  1974. * @message: Pointer to incoming dbus message
  1975. * @wpa_s: wpa_supplicant structure for a network interface
  1976. * Returns: A dbus message containing a name of bridge network
  1977. * interface associated with with wpa_s
  1978. *
  1979. * Getter for "BridgeIfname" property.
  1980. */
  1981. DBusMessage * wpas_dbus_getter_bridge_ifname(DBusMessage *message,
  1982. struct wpa_supplicant *wpa_s)
  1983. {
  1984. const char *bridge_ifname = NULL;
  1985. bridge_ifname = wpa_s->bridge_ifname;
  1986. if (bridge_ifname == NULL) {
  1987. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bridge_ifname[dbus]: "
  1988. "wpa_s has no bridge interface name set");
  1989. return wpas_dbus_error_unknown_error(message, NULL);
  1990. }
  1991. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
  1992. &bridge_ifname);
  1993. }
  1994. /**
  1995. * wpas_dbus_getter_bsss - Get array of BSSs objects
  1996. * @message: Pointer to incoming dbus message
  1997. * @wpa_s: wpa_supplicant structure for a network interface
  1998. * Returns: a dbus message containing an array of all known BSS objects
  1999. * dbus paths
  2000. *
  2001. * Getter for "BSSs" property.
  2002. */
  2003. DBusMessage * wpas_dbus_getter_bsss(DBusMessage *message,
  2004. struct wpa_supplicant *wpa_s)
  2005. {
  2006. DBusMessage *reply = NULL;
  2007. struct wpa_bss *bss;
  2008. char **paths;
  2009. unsigned int i = 0;
  2010. paths = os_zalloc(wpa_s->num_bss * sizeof(char *));
  2011. if (!paths) {
  2012. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2013. NULL);
  2014. }
  2015. /* Loop through scan results and append each result's object path */
  2016. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  2017. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  2018. if (paths[i] == NULL) {
  2019. reply = dbus_message_new_error(message,
  2020. DBUS_ERROR_NO_MEMORY,
  2021. NULL);
  2022. goto out;
  2023. }
  2024. /* Construct the object path for this BSS. */
  2025. os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
  2026. "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
  2027. wpa_s->dbus_new_path, bss->id);
  2028. }
  2029. reply = wpas_dbus_simple_array_property_getter(message,
  2030. DBUS_TYPE_OBJECT_PATH,
  2031. paths, wpa_s->num_bss);
  2032. out:
  2033. while (i)
  2034. os_free(paths[--i]);
  2035. os_free(paths);
  2036. return reply;
  2037. }
  2038. /**
  2039. * wpas_dbus_getter_networks - Get array of networks objects
  2040. * @message: Pointer to incoming dbus message
  2041. * @wpa_s: wpa_supplicant structure for a network interface
  2042. * Returns: a dbus message containing an array of all configured
  2043. * networks dbus object paths.
  2044. *
  2045. * Getter for "Networks" property.
  2046. */
  2047. DBusMessage * wpas_dbus_getter_networks(DBusMessage *message,
  2048. struct wpa_supplicant *wpa_s)
  2049. {
  2050. DBusMessage *reply = NULL;
  2051. struct wpa_ssid *ssid;
  2052. char **paths;
  2053. unsigned int i = 0, num = 0;
  2054. if (wpa_s->conf == NULL) {
  2055. wpa_printf(MSG_ERROR, "wpas_dbus_getter_networks[dbus]: "
  2056. "An error occurred getting networks list.");
  2057. return wpas_dbus_error_unknown_error(message, NULL);
  2058. }
  2059. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  2060. num++;
  2061. paths = os_zalloc(num * sizeof(char *));
  2062. if (!paths) {
  2063. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2064. NULL);
  2065. }
  2066. /* Loop through configured networks and append object path of each */
  2067. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  2068. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  2069. if (paths[i] == NULL) {
  2070. reply = dbus_message_new_error(message,
  2071. DBUS_ERROR_NO_MEMORY,
  2072. NULL);
  2073. goto out;
  2074. }
  2075. /* Construct the object path for this network. */
  2076. os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
  2077. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
  2078. wpa_s->dbus_new_path, ssid->id);
  2079. }
  2080. reply = wpas_dbus_simple_array_property_getter(message,
  2081. DBUS_TYPE_OBJECT_PATH,
  2082. paths, num);
  2083. out:
  2084. while (i)
  2085. os_free(paths[--i]);
  2086. os_free(paths);
  2087. return reply;
  2088. }
  2089. /**
  2090. * wpas_dbus_getter_blobs - Get all blobs defined for this interface
  2091. * @message: Pointer to incoming dbus message
  2092. * @wpa_s: wpa_supplicant structure for a network interface
  2093. * Returns: a dbus message containing a dictionary of pairs (blob_name, blob)
  2094. *
  2095. * Getter for "Blobs" property.
  2096. */
  2097. DBusMessage * wpas_dbus_getter_blobs(DBusMessage *message,
  2098. struct wpa_supplicant *wpa_s)
  2099. {
  2100. DBusMessage *reply = NULL;
  2101. DBusMessageIter iter, variant_iter, dict_iter, entry_iter, array_iter;
  2102. struct wpa_config_blob *blob;
  2103. if (message == NULL)
  2104. reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
  2105. else
  2106. reply = dbus_message_new_method_return(message);
  2107. if (!reply)
  2108. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2109. NULL);
  2110. dbus_message_iter_init_append(reply, &iter);
  2111. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
  2112. "a{say}", &variant_iter) ||
  2113. !dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
  2114. "{say}", &dict_iter)) {
  2115. dbus_message_unref(reply);
  2116. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2117. NULL);
  2118. }
  2119. blob = wpa_s->conf->blobs;
  2120. while (blob) {
  2121. if (!dbus_message_iter_open_container(&dict_iter,
  2122. DBUS_TYPE_DICT_ENTRY,
  2123. NULL, &entry_iter) ||
  2124. !dbus_message_iter_append_basic(&entry_iter,
  2125. DBUS_TYPE_STRING,
  2126. &(blob->name)) ||
  2127. !dbus_message_iter_open_container(&entry_iter,
  2128. DBUS_TYPE_ARRAY,
  2129. DBUS_TYPE_BYTE_AS_STRING,
  2130. &array_iter) ||
  2131. !dbus_message_iter_append_fixed_array(&array_iter,
  2132. DBUS_TYPE_BYTE,
  2133. &(blob->data),
  2134. blob->len) ||
  2135. !dbus_message_iter_close_container(&entry_iter,
  2136. &array_iter) ||
  2137. !dbus_message_iter_close_container(&dict_iter,
  2138. &entry_iter)) {
  2139. dbus_message_unref(reply);
  2140. return dbus_message_new_error(message,
  2141. DBUS_ERROR_NO_MEMORY,
  2142. NULL);
  2143. }
  2144. blob = blob->next;
  2145. }
  2146. if (!dbus_message_iter_close_container(&variant_iter, &dict_iter) ||
  2147. !dbus_message_iter_close_container(&iter, &variant_iter)) {
  2148. dbus_message_unref(reply);
  2149. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2150. NULL);
  2151. }
  2152. return reply;
  2153. }
  2154. /**
  2155. * wpas_dbus_getter_bss_bssid - Return the BSSID of a BSS
  2156. * @message: Pointer to incoming dbus message
  2157. * @bss: a pair of interface describing structure and bss's id
  2158. * Returns: a dbus message containing the bssid for the requested bss
  2159. *
  2160. * Getter for "BSSID" property.
  2161. */
  2162. DBusMessage * wpas_dbus_getter_bss_bssid(DBusMessage *message,
  2163. struct bss_handler_args *bss)
  2164. {
  2165. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2166. if (!res) {
  2167. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_bssid[dbus]: no "
  2168. "bss with id %d found", bss->id);
  2169. return NULL;
  2170. }
  2171. return wpas_dbus_simple_array_property_getter(message, DBUS_TYPE_BYTE,
  2172. res->bssid, ETH_ALEN);
  2173. }
  2174. /**
  2175. * wpas_dbus_getter_bss_ssid - Return the SSID of a BSS
  2176. * @message: Pointer to incoming dbus message
  2177. * @bss: a pair of interface describing structure and bss's id
  2178. * Returns: a dbus message containing the ssid for the requested bss
  2179. *
  2180. * Getter for "SSID" property.
  2181. */
  2182. DBusMessage * wpas_dbus_getter_bss_ssid(DBusMessage *message,
  2183. struct bss_handler_args *bss)
  2184. {
  2185. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2186. if (!res) {
  2187. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_ssid[dbus]: no "
  2188. "bss with id %d found", bss->id);
  2189. return NULL;
  2190. }
  2191. return wpas_dbus_simple_array_property_getter(message, DBUS_TYPE_BYTE,
  2192. res->ssid,
  2193. res->ssid_len);
  2194. }
  2195. /**
  2196. * wpas_dbus_getter_bss_privacy - Return the privacy flag of a BSS
  2197. * @message: Pointer to incoming dbus message
  2198. * @bss: a pair of interface describing structure and bss's id
  2199. * Returns: a dbus message containing the privacy flag value of requested bss
  2200. *
  2201. * Getter for "Privacy" property.
  2202. */
  2203. DBusMessage * wpas_dbus_getter_bss_privacy(DBusMessage *message,
  2204. struct bss_handler_args *bss)
  2205. {
  2206. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2207. dbus_bool_t privacy;
  2208. if (!res) {
  2209. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_privacy[dbus]: no "
  2210. "bss with id %d found", bss->id);
  2211. return NULL;
  2212. }
  2213. privacy = (res->caps & IEEE80211_CAP_PRIVACY) ? TRUE : FALSE;
  2214. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
  2215. &privacy);
  2216. }
  2217. /**
  2218. * wpas_dbus_getter_bss_mode - Return the mode of a BSS
  2219. * @message: Pointer to incoming dbus message
  2220. * @bss: a pair of interface describing structure and bss's id
  2221. * Returns: a dbus message containing the mode of requested bss
  2222. *
  2223. * Getter for "Mode" property.
  2224. */
  2225. DBusMessage * wpas_dbus_getter_bss_mode(DBusMessage *message,
  2226. struct bss_handler_args *bss)
  2227. {
  2228. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2229. const char *mode;
  2230. if (!res) {
  2231. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_mode[dbus]: no "
  2232. "bss with id %d found", bss->id);
  2233. return NULL;
  2234. }
  2235. if (res->caps & IEEE80211_CAP_IBSS)
  2236. mode = "ad-hoc";
  2237. else
  2238. mode = "infrastructure";
  2239. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
  2240. &mode);
  2241. }
  2242. /**
  2243. * wpas_dbus_getter_bss_level - Return the signal strength of a BSS
  2244. * @message: Pointer to incoming dbus message
  2245. * @bss: a pair of interface describing structure and bss's id
  2246. * Returns: a dbus message containing the signal strength of requested bss
  2247. *
  2248. * Getter for "Level" property.
  2249. */
  2250. DBusMessage * wpas_dbus_getter_bss_signal(DBusMessage *message,
  2251. struct bss_handler_args *bss)
  2252. {
  2253. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2254. if (!res) {
  2255. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_signal[dbus]: no "
  2256. "bss with id %d found", bss->id);
  2257. return NULL;
  2258. }
  2259. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_INT16,
  2260. &res->level);
  2261. }
  2262. /**
  2263. * wpas_dbus_getter_bss_frequency - Return the frequency of a BSS
  2264. * @message: Pointer to incoming dbus message
  2265. * @bss: a pair of interface describing structure and bss's id
  2266. * Returns: a dbus message containing the frequency of requested bss
  2267. *
  2268. * Getter for "Frequency" property.
  2269. */
  2270. DBusMessage * wpas_dbus_getter_bss_frequency(DBusMessage *message,
  2271. struct bss_handler_args *bss)
  2272. {
  2273. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2274. if (!res) {
  2275. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_frequency[dbus]: "
  2276. "no bss with id %d found", bss->id);
  2277. return NULL;
  2278. }
  2279. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_UINT16,
  2280. &res->freq);
  2281. }
  2282. static int cmp_u8s_desc(const void *a, const void *b)
  2283. {
  2284. return (*(u8 *) b - *(u8 *) a);
  2285. }
  2286. /**
  2287. * wpas_dbus_getter_bss_rates - Return available bit rates of a BSS
  2288. * @message: Pointer to incoming dbus message
  2289. * @bss: a pair of interface describing structure and bss's id
  2290. * Returns: a dbus message containing sorted array of bit rates
  2291. *
  2292. * Getter for "Rates" property.
  2293. */
  2294. DBusMessage * wpas_dbus_getter_bss_rates(DBusMessage *message,
  2295. struct bss_handler_args *bss)
  2296. {
  2297. DBusMessage *reply;
  2298. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2299. u8 *ie_rates = NULL;
  2300. u32 *real_rates;
  2301. int rates_num, i;
  2302. if (!res) {
  2303. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_rates[dbus]: "
  2304. "no bss with id %d found", bss->id);
  2305. return NULL;
  2306. }
  2307. rates_num = wpa_bss_get_bit_rates(res, &ie_rates);
  2308. if (rates_num < 0)
  2309. return NULL;
  2310. qsort(ie_rates, rates_num, 1, cmp_u8s_desc);
  2311. real_rates = os_malloc(sizeof(u32) * rates_num);
  2312. if (!real_rates) {
  2313. os_free(ie_rates);
  2314. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2315. NULL);
  2316. }
  2317. for (i = 0; i < rates_num; i++)
  2318. real_rates[i] = ie_rates[i] * 500000;
  2319. reply = wpas_dbus_simple_array_property_getter(message,
  2320. DBUS_TYPE_UINT32,
  2321. real_rates, rates_num);
  2322. os_free(ie_rates);
  2323. os_free(real_rates);
  2324. return reply;
  2325. }
  2326. static DBusMessage * wpas_dbus_get_bss_security_prop(
  2327. DBusMessage *message, struct wpa_ie_data *ie_data)
  2328. {
  2329. DBusMessage *reply;
  2330. DBusMessageIter iter, iter_dict, variant_iter;
  2331. const char *group;
  2332. const char *pairwise[2]; /* max 2 pairwise ciphers is supported */
  2333. const char *key_mgmt[7]; /* max 7 key managements may be supported */
  2334. int n;
  2335. if (message == NULL)
  2336. reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
  2337. else
  2338. reply = dbus_message_new_method_return(message);
  2339. if (!reply)
  2340. goto nomem;
  2341. dbus_message_iter_init_append(reply, &iter);
  2342. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
  2343. "a{sv}", &variant_iter))
  2344. goto nomem;
  2345. if (!wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
  2346. goto nomem;
  2347. /* KeyMgmt */
  2348. n = 0;
  2349. if (ie_data->key_mgmt & WPA_KEY_MGMT_PSK)
  2350. key_mgmt[n++] = "wpa-psk";
  2351. if (ie_data->key_mgmt & WPA_KEY_MGMT_FT_PSK)
  2352. key_mgmt[n++] = "wpa-ft-psk";
  2353. if (ie_data->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
  2354. key_mgmt[n++] = "wpa-psk-sha256";
  2355. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  2356. key_mgmt[n++] = "wpa-eap";
  2357. if (ie_data->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
  2358. key_mgmt[n++] = "wpa-ft-eap";
  2359. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
  2360. key_mgmt[n++] = "wpa-eap-sha256";
  2361. if (ie_data->key_mgmt & WPA_KEY_MGMT_NONE)
  2362. key_mgmt[n++] = "wpa-none";
  2363. if (!wpa_dbus_dict_append_string_array(&iter_dict, "KeyMgmt",
  2364. key_mgmt, n))
  2365. goto nomem;
  2366. /* Group */
  2367. switch (ie_data->group_cipher) {
  2368. case WPA_CIPHER_WEP40:
  2369. group = "wep40";
  2370. break;
  2371. case WPA_CIPHER_TKIP:
  2372. group = "tkip";
  2373. break;
  2374. case WPA_CIPHER_CCMP:
  2375. group = "ccmp";
  2376. break;
  2377. case WPA_CIPHER_WEP104:
  2378. group = "wep104";
  2379. break;
  2380. default:
  2381. group = "";
  2382. break;
  2383. }
  2384. if (!wpa_dbus_dict_append_string(&iter_dict, "Group", group))
  2385. goto nomem;
  2386. /* Pairwise */
  2387. n = 0;
  2388. if (ie_data->pairwise_cipher & WPA_CIPHER_TKIP)
  2389. pairwise[n++] = "tkip";
  2390. if (ie_data->pairwise_cipher & WPA_CIPHER_CCMP)
  2391. pairwise[n++] = "ccmp";
  2392. if (!wpa_dbus_dict_append_string_array(&iter_dict, "Pairwise",
  2393. pairwise, n))
  2394. goto nomem;
  2395. /* Management group (RSN only) */
  2396. if (ie_data->proto == WPA_PROTO_RSN) {
  2397. switch (ie_data->mgmt_group_cipher) {
  2398. #ifdef CONFIG_IEEE80211W
  2399. case WPA_CIPHER_AES_128_CMAC:
  2400. group = "aes128cmac";
  2401. break;
  2402. #endif /* CONFIG_IEEE80211W */
  2403. default:
  2404. group = "";
  2405. break;
  2406. }
  2407. if (!wpa_dbus_dict_append_string(&iter_dict, "MgmtGroup",
  2408. group))
  2409. goto nomem;
  2410. }
  2411. if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict))
  2412. goto nomem;
  2413. if (!dbus_message_iter_close_container(&iter, &variant_iter))
  2414. goto nomem;
  2415. return reply;
  2416. nomem:
  2417. if (reply)
  2418. dbus_message_unref(reply);
  2419. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY, NULL);
  2420. }
  2421. /**
  2422. * wpas_dbus_getter_bss_wpa - Return the WPA options of a BSS
  2423. * @message: Pointer to incoming dbus message
  2424. * @bss: a pair of interface describing structure and bss's id
  2425. * Returns: a dbus message containing the WPA options of requested bss
  2426. *
  2427. * Getter for "WPA" property.
  2428. */
  2429. DBusMessage * wpas_dbus_getter_bss_wpa(DBusMessage *message,
  2430. struct bss_handler_args *bss)
  2431. {
  2432. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2433. struct wpa_ie_data wpa_data;
  2434. const u8 *ie;
  2435. if (!res) {
  2436. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_wpa[dbus]: no "
  2437. "bss with id %d found", bss->id);
  2438. return NULL;
  2439. }
  2440. os_memset(&wpa_data, 0, sizeof(wpa_data));
  2441. ie = wpa_bss_get_vendor_ie(res, WPA_IE_VENDOR_TYPE);
  2442. if (ie) {
  2443. if (wpa_parse_wpa_ie(ie, 2 + ie[1], &wpa_data) < 0)
  2444. return wpas_dbus_error_unknown_error(message,
  2445. "invalid WPA IE");
  2446. }
  2447. return wpas_dbus_get_bss_security_prop(message, &wpa_data);
  2448. }
  2449. /**
  2450. * wpas_dbus_getter_bss_rsn - Return the RSN options of a BSS
  2451. * @message: Pointer to incoming dbus message
  2452. * @bss: a pair of interface describing structure and bss's id
  2453. * Returns: a dbus message containing the RSN options of requested bss
  2454. *
  2455. * Getter for "RSN" property.
  2456. */
  2457. DBusMessage * wpas_dbus_getter_bss_rsn(DBusMessage *message,
  2458. struct bss_handler_args *bss)
  2459. {
  2460. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2461. struct wpa_ie_data wpa_data;
  2462. const u8 *ie;
  2463. if (!res) {
  2464. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_rsn[dbus]: no "
  2465. "bss with id %d found", bss->id);
  2466. return NULL;
  2467. }
  2468. os_memset(&wpa_data, 0, sizeof(wpa_data));
  2469. ie = wpa_bss_get_ie(res, WLAN_EID_RSN);
  2470. if (ie) {
  2471. if (wpa_parse_wpa_ie(ie, 2 + ie[1], &wpa_data) < 0)
  2472. return wpas_dbus_error_unknown_error(message,
  2473. "invalid RSN IE");
  2474. }
  2475. return wpas_dbus_get_bss_security_prop(message, &wpa_data);
  2476. }
  2477. /**
  2478. * wpas_dbus_getter_bss_ies - Return all IEs of a BSS
  2479. * @message: Pointer to incoming dbus message
  2480. * @bss: a pair of interface describing structure and bss's id
  2481. * Returns: a dbus message containing IEs byte array
  2482. *
  2483. * Getter for "IEs" property.
  2484. */
  2485. DBusMessage * wpas_dbus_getter_bss_ies(DBusMessage *message,
  2486. struct bss_handler_args *bss)
  2487. {
  2488. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2489. if (!res) {
  2490. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_ies[dbus]: no "
  2491. "bss with id %d found", bss->id);
  2492. return NULL;
  2493. }
  2494. return wpas_dbus_simple_array_property_getter(message, DBUS_TYPE_BYTE,
  2495. res + 1, res->ie_len);
  2496. }
  2497. /**
  2498. * wpas_dbus_getter_enabled - Check whether network is enabled or disabled
  2499. * @message: Pointer to incoming dbus message
  2500. * @wpas_dbus_setter_enabled: wpa_supplicant structure for a network interface
  2501. * and wpa_ssid structure for a configured network
  2502. * Returns: DBus message with boolean indicating state of configured network
  2503. * or DBus error on failure
  2504. *
  2505. * Getter for "enabled" property of a configured network.
  2506. */
  2507. DBusMessage * wpas_dbus_getter_enabled(DBusMessage *message,
  2508. struct network_handler_args *net)
  2509. {
  2510. dbus_bool_t enabled = net->ssid->disabled ? FALSE : TRUE;
  2511. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
  2512. &enabled);
  2513. }
  2514. /**
  2515. * wpas_dbus_setter_enabled - Mark a configured network as enabled or disabled
  2516. * @message: Pointer to incoming dbus message
  2517. * @wpas_dbus_setter_enabled: wpa_supplicant structure for a network interface
  2518. * and wpa_ssid structure for a configured network
  2519. * Returns: NULL indicating success or DBus error on failure
  2520. *
  2521. * Setter for "Enabled" property of a configured network.
  2522. */
  2523. DBusMessage * wpas_dbus_setter_enabled(DBusMessage *message,
  2524. struct network_handler_args *net)
  2525. {
  2526. DBusMessage *reply = NULL;
  2527. struct wpa_supplicant *wpa_s;
  2528. struct wpa_ssid *ssid;
  2529. dbus_bool_t enable;
  2530. reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_BOOLEAN,
  2531. &enable);
  2532. if (reply)
  2533. return reply;
  2534. wpa_s = net->wpa_s;
  2535. ssid = net->ssid;
  2536. if (enable)
  2537. wpa_supplicant_enable_network(wpa_s, ssid);
  2538. else
  2539. wpa_supplicant_disable_network(wpa_s, ssid);
  2540. return NULL;
  2541. }
  2542. /**
  2543. * wpas_dbus_getter_network_properties - Get options for a configured network
  2544. * @message: Pointer to incoming dbus message
  2545. * @net: wpa_supplicant structure for a network interface and
  2546. * wpa_ssid structure for a configured network
  2547. * Returns: DBus message with network properties or DBus error on failure
  2548. *
  2549. * Getter for "Properties" property of a configured network.
  2550. */
  2551. DBusMessage * wpas_dbus_getter_network_properties(
  2552. DBusMessage *message, struct network_handler_args *net)
  2553. {
  2554. DBusMessage *reply = NULL;
  2555. DBusMessageIter iter, variant_iter, dict_iter;
  2556. char **iterator;
  2557. char **props = wpa_config_get_all(net->ssid, 1);
  2558. if (!props)
  2559. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2560. NULL);
  2561. if (message == NULL)
  2562. reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
  2563. else
  2564. reply = dbus_message_new_method_return(message);
  2565. if (!reply) {
  2566. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2567. NULL);
  2568. goto out;
  2569. }
  2570. dbus_message_iter_init_append(reply, &iter);
  2571. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
  2572. "a{sv}", &variant_iter) ||
  2573. !wpa_dbus_dict_open_write(&variant_iter, &dict_iter)) {
  2574. dbus_message_unref(reply);
  2575. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2576. NULL);
  2577. goto out;
  2578. }
  2579. iterator = props;
  2580. while (*iterator) {
  2581. if (!wpa_dbus_dict_append_string(&dict_iter, *iterator,
  2582. *(iterator + 1))) {
  2583. dbus_message_unref(reply);
  2584. reply = dbus_message_new_error(message,
  2585. DBUS_ERROR_NO_MEMORY,
  2586. NULL);
  2587. goto out;
  2588. }
  2589. iterator += 2;
  2590. }
  2591. if (!wpa_dbus_dict_close_write(&variant_iter, &dict_iter) ||
  2592. !dbus_message_iter_close_container(&iter, &variant_iter)) {
  2593. dbus_message_unref(reply);
  2594. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2595. NULL);
  2596. goto out;
  2597. }
  2598. out:
  2599. iterator = props;
  2600. while (*iterator) {
  2601. os_free(*iterator);
  2602. iterator++;
  2603. }
  2604. os_free(props);
  2605. return reply;
  2606. }
  2607. /**
  2608. * wpas_dbus_setter_network_properties - Set options for a configured network
  2609. * @message: Pointer to incoming dbus message
  2610. * @net: wpa_supplicant structure for a network interface and
  2611. * wpa_ssid structure for a configured network
  2612. * Returns: NULL indicating success or DBus error on failure
  2613. *
  2614. * Setter for "Properties" property of a configured network.
  2615. */
  2616. DBusMessage * wpas_dbus_setter_network_properties(
  2617. DBusMessage *message, struct network_handler_args *net)
  2618. {
  2619. struct wpa_ssid *ssid = net->ssid;
  2620. DBusMessage *reply = NULL;
  2621. DBusMessageIter iter, variant_iter;
  2622. dbus_message_iter_init(message, &iter);
  2623. dbus_message_iter_next(&iter);
  2624. dbus_message_iter_next(&iter);
  2625. dbus_message_iter_recurse(&iter, &variant_iter);
  2626. reply = set_network_properties(message, net->wpa_s, ssid,
  2627. &variant_iter);
  2628. if (reply)
  2629. wpa_printf(MSG_DEBUG, "dbus control interface couldn't set "
  2630. "network properties");
  2631. return reply;
  2632. }