dbus_new_handlers.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046
  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. /**
  1276. * wpas_dbus_handler_select_network - Attempt association with a network
  1277. * @message: Pointer to incoming dbus message
  1278. * @wpa_s: wpa_supplicant structure for a network interface
  1279. * Returns: NULL on success or dbus error on failure
  1280. *
  1281. * Handler function for "SelectNetwork" method call of network interface.
  1282. */
  1283. DBusMessage * wpas_dbus_handler_select_network(DBusMessage *message,
  1284. struct wpa_supplicant *wpa_s)
  1285. {
  1286. DBusMessage *reply = NULL;
  1287. const char *op;
  1288. char *iface = NULL, *net_id = NULL;
  1289. int id;
  1290. struct wpa_ssid *ssid;
  1291. dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
  1292. DBUS_TYPE_INVALID);
  1293. /* Extract the network ID and ensure the network */
  1294. /* is actually a child of this interface */
  1295. iface = wpas_dbus_new_decompose_object_path(op, &net_id, NULL);
  1296. if (iface == NULL || os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1297. reply = wpas_dbus_error_invalid_args(message, op);
  1298. goto out;
  1299. }
  1300. id = strtoul(net_id, NULL, 10);
  1301. if (errno == EINVAL) {
  1302. reply = wpas_dbus_error_invalid_args(message, op);
  1303. goto out;
  1304. }
  1305. ssid = wpa_config_get_network(wpa_s->conf, id);
  1306. if (ssid == NULL) {
  1307. reply = wpas_dbus_error_network_unknown(message);
  1308. goto out;
  1309. }
  1310. /* Finally, associate with the network */
  1311. wpa_supplicant_select_network(wpa_s, ssid);
  1312. out:
  1313. os_free(iface);
  1314. os_free(net_id);
  1315. return reply;
  1316. }
  1317. /**
  1318. * wpas_dbus_handler_add_blob - Store named binary blob (ie, for certificates)
  1319. * @message: Pointer to incoming dbus message
  1320. * @wpa_s: %wpa_supplicant data structure
  1321. * Returns: A dbus message containing an error on failure or NULL on success
  1322. *
  1323. * Asks wpa_supplicant to internally store a binary blobs.
  1324. */
  1325. DBusMessage * wpas_dbus_handler_add_blob(DBusMessage *message,
  1326. struct wpa_supplicant *wpa_s)
  1327. {
  1328. DBusMessage *reply = NULL;
  1329. DBusMessageIter iter, array_iter;
  1330. char *blob_name;
  1331. u8 *blob_data;
  1332. int blob_len;
  1333. struct wpa_config_blob *blob = NULL;
  1334. dbus_message_iter_init(message, &iter);
  1335. dbus_message_iter_get_basic(&iter, &blob_name);
  1336. if (wpa_config_get_blob(wpa_s->conf, blob_name)) {
  1337. return dbus_message_new_error(message,
  1338. WPAS_DBUS_ERROR_BLOB_EXISTS,
  1339. NULL);
  1340. }
  1341. dbus_message_iter_next(&iter);
  1342. dbus_message_iter_recurse(&iter, &array_iter);
  1343. dbus_message_iter_get_fixed_array(&array_iter, &blob_data, &blob_len);
  1344. blob = os_zalloc(sizeof(*blob));
  1345. if (!blob) {
  1346. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1347. NULL);
  1348. goto err;
  1349. }
  1350. blob->data = os_malloc(blob_len);
  1351. if (!blob->data) {
  1352. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1353. NULL);
  1354. goto err;
  1355. }
  1356. os_memcpy(blob->data, blob_data, blob_len);
  1357. blob->len = blob_len;
  1358. blob->name = os_strdup(blob_name);
  1359. if (!blob->name) {
  1360. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1361. NULL);
  1362. goto err;
  1363. }
  1364. wpa_config_set_blob(wpa_s->conf, blob);
  1365. wpas_notify_blob_added(wpa_s, blob->name);
  1366. return reply;
  1367. err:
  1368. if (blob) {
  1369. os_free(blob->name);
  1370. os_free(blob->data);
  1371. os_free(blob);
  1372. }
  1373. return reply;
  1374. }
  1375. /**
  1376. * wpas_dbus_handler_get_blob - Get named binary blob (ie, for certificates)
  1377. * @message: Pointer to incoming dbus message
  1378. * @wpa_s: %wpa_supplicant data structure
  1379. * Returns: A dbus message containing array of bytes (blob)
  1380. *
  1381. * Gets one wpa_supplicant's binary blobs.
  1382. */
  1383. DBusMessage * wpas_dbus_handler_get_blob(DBusMessage *message,
  1384. struct wpa_supplicant *wpa_s)
  1385. {
  1386. DBusMessage *reply = NULL;
  1387. DBusMessageIter iter, array_iter;
  1388. char *blob_name;
  1389. const struct wpa_config_blob *blob;
  1390. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &blob_name,
  1391. DBUS_TYPE_INVALID);
  1392. blob = wpa_config_get_blob(wpa_s->conf, blob_name);
  1393. if (!blob) {
  1394. return dbus_message_new_error(message,
  1395. WPAS_DBUS_ERROR_BLOB_UNKNOWN,
  1396. "Blob id not set");
  1397. }
  1398. reply = dbus_message_new_method_return(message);
  1399. if (!reply) {
  1400. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1401. NULL);
  1402. goto out;
  1403. }
  1404. dbus_message_iter_init_append(reply, &iter);
  1405. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
  1406. DBUS_TYPE_BYTE_AS_STRING,
  1407. &array_iter)) {
  1408. dbus_message_unref(reply);
  1409. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1410. NULL);
  1411. goto out;
  1412. }
  1413. if (!dbus_message_iter_append_fixed_array(&array_iter, DBUS_TYPE_BYTE,
  1414. &(blob->data), blob->len)) {
  1415. dbus_message_unref(reply);
  1416. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1417. NULL);
  1418. goto out;
  1419. }
  1420. if (!dbus_message_iter_close_container(&iter, &array_iter)) {
  1421. dbus_message_unref(reply);
  1422. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1423. NULL);
  1424. goto out;
  1425. }
  1426. out:
  1427. return reply;
  1428. }
  1429. /**
  1430. * wpas_remove_handler_remove_blob - Remove named binary blob
  1431. * @message: Pointer to incoming dbus message
  1432. * @wpa_s: %wpa_supplicant data structure
  1433. * Returns: NULL on success or dbus error
  1434. *
  1435. * Asks wpa_supplicant to internally remove a binary blobs.
  1436. */
  1437. DBusMessage * wpas_dbus_handler_remove_blob(DBusMessage *message,
  1438. struct wpa_supplicant *wpa_s)
  1439. {
  1440. DBusMessage *reply = NULL;
  1441. char *blob_name;
  1442. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &blob_name,
  1443. DBUS_TYPE_INVALID);
  1444. if (wpa_config_remove_blob(wpa_s->conf, blob_name)) {
  1445. return dbus_message_new_error(message,
  1446. WPAS_DBUS_ERROR_BLOB_UNKNOWN,
  1447. "Blob id not set");
  1448. }
  1449. wpas_notify_blob_removed(wpa_s, blob_name);
  1450. return reply;
  1451. }
  1452. /*
  1453. * wpas_dbus_handler_flush_bss - Flush the BSS cache
  1454. * @message: Pointer to incoming dbus message
  1455. * @wpa_s: wpa_supplicant structure for a network interface
  1456. * Returns: NULL
  1457. *
  1458. * Handler function for "FlushBSS" method call of network interface.
  1459. */
  1460. DBusMessage * wpas_dbus_handler_flush_bss(DBusMessage *message,
  1461. struct wpa_supplicant *wpa_s)
  1462. {
  1463. dbus_uint32_t age;
  1464. dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &age,
  1465. DBUS_TYPE_INVALID);
  1466. if (age == 0)
  1467. wpa_bss_flush(wpa_s);
  1468. else
  1469. wpa_bss_flush_by_age(wpa_s, age);
  1470. return NULL;
  1471. }
  1472. /**
  1473. * wpas_dbus_getter_capabilities - Return interface capabilities
  1474. * @message: Pointer to incoming dbus message
  1475. * @wpa_s: wpa_supplicant structure for a network interface
  1476. * Returns: A dbus message containing a dict of strings
  1477. *
  1478. * Getter for "Capabilities" property of an interface.
  1479. */
  1480. DBusMessage * wpas_dbus_getter_capabilities(DBusMessage *message,
  1481. struct wpa_supplicant *wpa_s)
  1482. {
  1483. DBusMessage *reply = NULL;
  1484. struct wpa_driver_capa capa;
  1485. int res;
  1486. DBusMessageIter iter, iter_dict;
  1487. DBusMessageIter iter_dict_entry, iter_dict_val, iter_array,
  1488. variant_iter;
  1489. const char *scans[] = { "active", "passive", "ssid" };
  1490. const char *modes[] = { "infrastructure", "ad-hoc", "ap" };
  1491. int n = sizeof(modes) / sizeof(char *);
  1492. if (message == NULL)
  1493. reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
  1494. else
  1495. reply = dbus_message_new_method_return(message);
  1496. if (!reply)
  1497. goto nomem;
  1498. dbus_message_iter_init_append(reply, &iter);
  1499. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
  1500. "a{sv}", &variant_iter))
  1501. goto nomem;
  1502. if (!wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
  1503. goto nomem;
  1504. res = wpa_drv_get_capa(wpa_s, &capa);
  1505. /***** pairwise cipher */
  1506. if (res < 0) {
  1507. const char *args[] = {"ccmp", "tkip", "none"};
  1508. if (!wpa_dbus_dict_append_string_array(
  1509. &iter_dict, "Pairwise", args,
  1510. sizeof(args) / sizeof(char*)))
  1511. goto nomem;
  1512. } else {
  1513. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Pairwise",
  1514. &iter_dict_entry,
  1515. &iter_dict_val,
  1516. &iter_array))
  1517. goto nomem;
  1518. if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1519. if (!wpa_dbus_dict_string_array_add_element(
  1520. &iter_array, "ccmp"))
  1521. goto nomem;
  1522. }
  1523. if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1524. if (!wpa_dbus_dict_string_array_add_element(
  1525. &iter_array, "tkip"))
  1526. goto nomem;
  1527. }
  1528. if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1529. if (!wpa_dbus_dict_string_array_add_element(
  1530. &iter_array, "none"))
  1531. goto nomem;
  1532. }
  1533. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  1534. &iter_dict_entry,
  1535. &iter_dict_val,
  1536. &iter_array))
  1537. goto nomem;
  1538. }
  1539. /***** group cipher */
  1540. if (res < 0) {
  1541. const char *args[] = {
  1542. "ccmp", "tkip", "wep104", "wep40"
  1543. };
  1544. if (!wpa_dbus_dict_append_string_array(
  1545. &iter_dict, "Group", args,
  1546. sizeof(args) / sizeof(char*)))
  1547. goto nomem;
  1548. } else {
  1549. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Group",
  1550. &iter_dict_entry,
  1551. &iter_dict_val,
  1552. &iter_array))
  1553. goto nomem;
  1554. if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1555. if (!wpa_dbus_dict_string_array_add_element(
  1556. &iter_array, "ccmp"))
  1557. goto nomem;
  1558. }
  1559. if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1560. if (!wpa_dbus_dict_string_array_add_element(
  1561. &iter_array, "tkip"))
  1562. goto nomem;
  1563. }
  1564. if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  1565. if (!wpa_dbus_dict_string_array_add_element(
  1566. &iter_array, "wep104"))
  1567. goto nomem;
  1568. }
  1569. if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  1570. if (!wpa_dbus_dict_string_array_add_element(
  1571. &iter_array, "wep40"))
  1572. goto nomem;
  1573. }
  1574. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  1575. &iter_dict_entry,
  1576. &iter_dict_val,
  1577. &iter_array))
  1578. goto nomem;
  1579. }
  1580. /***** key management */
  1581. if (res < 0) {
  1582. const char *args[] = {
  1583. "wpa-psk", "wpa-eap", "ieee8021x", "wpa-none",
  1584. #ifdef CONFIG_WPS
  1585. "wps",
  1586. #endif /* CONFIG_WPS */
  1587. "none"
  1588. };
  1589. if (!wpa_dbus_dict_append_string_array(
  1590. &iter_dict, "KeyMgmt", args,
  1591. sizeof(args) / sizeof(char*)))
  1592. goto nomem;
  1593. } else {
  1594. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "KeyMgmt",
  1595. &iter_dict_entry,
  1596. &iter_dict_val,
  1597. &iter_array))
  1598. goto nomem;
  1599. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  1600. "none"))
  1601. goto nomem;
  1602. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  1603. "ieee8021x"))
  1604. goto nomem;
  1605. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1606. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  1607. if (!wpa_dbus_dict_string_array_add_element(
  1608. &iter_array, "wpa-eap"))
  1609. goto nomem;
  1610. if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT)
  1611. if (!wpa_dbus_dict_string_array_add_element(
  1612. &iter_array, "wpa-ft-eap"))
  1613. goto nomem;
  1614. /* TODO: Ensure that driver actually supports sha256 encryption. */
  1615. #ifdef CONFIG_IEEE80211W
  1616. if (!wpa_dbus_dict_string_array_add_element(
  1617. &iter_array, "wpa-eap-sha256"))
  1618. goto nomem;
  1619. #endif /* CONFIG_IEEE80211W */
  1620. }
  1621. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  1622. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1623. if (!wpa_dbus_dict_string_array_add_element(
  1624. &iter_array, "wpa-psk"))
  1625. goto nomem;
  1626. if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK)
  1627. if (!wpa_dbus_dict_string_array_add_element(
  1628. &iter_array, "wpa-ft-psk"))
  1629. goto nomem;
  1630. /* TODO: Ensure that driver actually supports sha256 encryption. */
  1631. #ifdef CONFIG_IEEE80211W
  1632. if (!wpa_dbus_dict_string_array_add_element(
  1633. &iter_array, "wpa-psk-sha256"))
  1634. goto nomem;
  1635. #endif /* CONFIG_IEEE80211W */
  1636. }
  1637. if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1638. if (!wpa_dbus_dict_string_array_add_element(
  1639. &iter_array, "wpa-none"))
  1640. goto nomem;
  1641. }
  1642. #ifdef CONFIG_WPS
  1643. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  1644. "wps"))
  1645. goto nomem;
  1646. #endif /* CONFIG_WPS */
  1647. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  1648. &iter_dict_entry,
  1649. &iter_dict_val,
  1650. &iter_array))
  1651. goto nomem;
  1652. }
  1653. /***** WPA protocol */
  1654. if (res < 0) {
  1655. const char *args[] = { "rsn", "wpa" };
  1656. if (!wpa_dbus_dict_append_string_array(
  1657. &iter_dict, "Protocol", args,
  1658. sizeof(args) / sizeof(char*)))
  1659. goto nomem;
  1660. } else {
  1661. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Protocol",
  1662. &iter_dict_entry,
  1663. &iter_dict_val,
  1664. &iter_array))
  1665. goto nomem;
  1666. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  1667. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1668. if (!wpa_dbus_dict_string_array_add_element(
  1669. &iter_array, "rsn"))
  1670. goto nomem;
  1671. }
  1672. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1673. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  1674. if (!wpa_dbus_dict_string_array_add_element(
  1675. &iter_array, "wpa"))
  1676. goto nomem;
  1677. }
  1678. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  1679. &iter_dict_entry,
  1680. &iter_dict_val,
  1681. &iter_array))
  1682. goto nomem;
  1683. }
  1684. /***** auth alg */
  1685. if (res < 0) {
  1686. const char *args[] = { "open", "shared", "leap" };
  1687. if (!wpa_dbus_dict_append_string_array(
  1688. &iter_dict, "AuthAlg", args,
  1689. sizeof(args) / sizeof(char*)))
  1690. goto nomem;
  1691. } else {
  1692. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "AuthAlg",
  1693. &iter_dict_entry,
  1694. &iter_dict_val,
  1695. &iter_array))
  1696. goto nomem;
  1697. if (capa.auth & (WPA_DRIVER_AUTH_OPEN)) {
  1698. if (!wpa_dbus_dict_string_array_add_element(
  1699. &iter_array, "open"))
  1700. goto nomem;
  1701. }
  1702. if (capa.auth & (WPA_DRIVER_AUTH_SHARED)) {
  1703. if (!wpa_dbus_dict_string_array_add_element(
  1704. &iter_array, "shared"))
  1705. goto nomem;
  1706. }
  1707. if (capa.auth & (WPA_DRIVER_AUTH_LEAP)) {
  1708. if (!wpa_dbus_dict_string_array_add_element(
  1709. &iter_array, "leap"))
  1710. goto nomem;
  1711. }
  1712. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  1713. &iter_dict_entry,
  1714. &iter_dict_val,
  1715. &iter_array))
  1716. goto nomem;
  1717. }
  1718. /***** Scan */
  1719. if (!wpa_dbus_dict_append_string_array(&iter_dict, "Scan", scans,
  1720. sizeof(scans) / sizeof(char *)))
  1721. goto nomem;
  1722. /***** Modes */
  1723. if (res < 0 || !(capa.flags & WPA_DRIVER_FLAGS_AP))
  1724. n--; /* exclude ap mode if it is not supported by the driver */
  1725. if (!wpa_dbus_dict_append_string_array(&iter_dict, "Modes", modes, n))
  1726. goto nomem;
  1727. if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict))
  1728. goto nomem;
  1729. if (!dbus_message_iter_close_container(&iter, &variant_iter))
  1730. goto nomem;
  1731. return reply;
  1732. nomem:
  1733. if (reply)
  1734. dbus_message_unref(reply);
  1735. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY, NULL);
  1736. }
  1737. /**
  1738. * wpas_dbus_getter_state - Get interface state
  1739. * @message: Pointer to incoming dbus message
  1740. * @wpa_s: wpa_supplicant structure for a network interface
  1741. * Returns: A dbus message containing a STRING representing the current
  1742. * interface state
  1743. *
  1744. * Getter for "State" property.
  1745. */
  1746. DBusMessage * wpas_dbus_getter_state(DBusMessage *message,
  1747. struct wpa_supplicant *wpa_s)
  1748. {
  1749. DBusMessage *reply = NULL;
  1750. const char *str_state;
  1751. char *state_ls, *tmp;
  1752. str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
  1753. /* make state string lowercase to fit new DBus API convention
  1754. */
  1755. state_ls = tmp = os_strdup(str_state);
  1756. if (!tmp) {
  1757. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1758. NULL);
  1759. }
  1760. while (*tmp) {
  1761. *tmp = tolower(*tmp);
  1762. tmp++;
  1763. }
  1764. reply = wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
  1765. &state_ls);
  1766. os_free(state_ls);
  1767. return reply;
  1768. }
  1769. /**
  1770. * wpas_dbus_new_iface_get_scanning - Get interface scanning state
  1771. * @message: Pointer to incoming dbus message
  1772. * @wpa_s: wpa_supplicant structure for a network interface
  1773. * Returns: A dbus message containing whether the interface is scanning
  1774. *
  1775. * Getter for "scanning" property.
  1776. */
  1777. DBusMessage * wpas_dbus_getter_scanning(DBusMessage *message,
  1778. struct wpa_supplicant *wpa_s)
  1779. {
  1780. dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
  1781. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
  1782. &scanning);
  1783. }
  1784. /**
  1785. * wpas_dbus_getter_ap_scan - Control roaming mode
  1786. * @message: Pointer to incoming dbus message
  1787. * @wpa_s: wpa_supplicant structure for a network interface
  1788. * Returns: A message containong value of ap_scan variable
  1789. *
  1790. * Getter function for "ApScan" property.
  1791. */
  1792. DBusMessage * wpas_dbus_getter_ap_scan(DBusMessage *message,
  1793. struct wpa_supplicant *wpa_s)
  1794. {
  1795. dbus_uint32_t ap_scan = wpa_s->conf->ap_scan;
  1796. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_UINT32,
  1797. &ap_scan);
  1798. }
  1799. /**
  1800. * wpas_dbus_setter_ap_scan - Control roaming mode
  1801. * @message: Pointer to incoming dbus message
  1802. * @wpa_s: wpa_supplicant structure for a network interface
  1803. * Returns: NULL
  1804. *
  1805. * Setter function for "ApScan" property.
  1806. */
  1807. DBusMessage * wpas_dbus_setter_ap_scan(DBusMessage *message,
  1808. struct wpa_supplicant *wpa_s)
  1809. {
  1810. DBusMessage *reply = NULL;
  1811. dbus_uint32_t ap_scan;
  1812. reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_UINT32,
  1813. &ap_scan);
  1814. if (reply)
  1815. return reply;
  1816. if (wpa_supplicant_set_ap_scan(wpa_s, ap_scan)) {
  1817. return wpas_dbus_error_invalid_args(
  1818. message, "ap_scan must equal 0, 1 or 2");
  1819. }
  1820. return NULL;
  1821. }
  1822. /**
  1823. * wpas_dbus_getter_ifname - Get interface name
  1824. * @message: Pointer to incoming dbus message
  1825. * @wpa_s: wpa_supplicant structure for a network interface
  1826. * Returns: A dbus message containing a name of network interface
  1827. * associated with with wpa_s
  1828. *
  1829. * Getter for "Ifname" property.
  1830. */
  1831. DBusMessage * wpas_dbus_getter_ifname(DBusMessage *message,
  1832. struct wpa_supplicant *wpa_s)
  1833. {
  1834. const char *ifname = wpa_s->ifname;
  1835. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
  1836. &ifname);
  1837. }
  1838. /**
  1839. * wpas_dbus_getter_driver - Get interface name
  1840. * @message: Pointer to incoming dbus message
  1841. * @wpa_s: wpa_supplicant structure for a network interface
  1842. * Returns: A dbus message containing a name of network interface
  1843. * driver associated with with wpa_s
  1844. *
  1845. * Getter for "Driver" property.
  1846. */
  1847. DBusMessage * wpas_dbus_getter_driver(DBusMessage *message,
  1848. struct wpa_supplicant *wpa_s)
  1849. {
  1850. const char *driver;
  1851. if (wpa_s->driver == NULL || wpa_s->driver->name == NULL) {
  1852. wpa_printf(MSG_DEBUG, "wpas_dbus_getter_driver[dbus]: "
  1853. "wpa_s has no driver set");
  1854. return wpas_dbus_error_unknown_error(message, NULL);
  1855. }
  1856. driver = wpa_s->driver->name;
  1857. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
  1858. &driver);
  1859. }
  1860. /**
  1861. * wpas_dbus_getter_current_bss - Get current bss object path
  1862. * @message: Pointer to incoming dbus message
  1863. * @wpa_s: wpa_supplicant structure for a network interface
  1864. * Returns: A dbus message containing a DBus object path to
  1865. * current BSS
  1866. *
  1867. * Getter for "CurrentBSS" property.
  1868. */
  1869. DBusMessage * wpas_dbus_getter_current_bss(DBusMessage *message,
  1870. struct wpa_supplicant *wpa_s)
  1871. {
  1872. DBusMessage *reply;
  1873. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *bss_obj_path = path_buf;
  1874. if (wpa_s->current_bss)
  1875. os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  1876. "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
  1877. wpa_s->dbus_new_path, wpa_s->current_bss->id);
  1878. else
  1879. os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
  1880. reply = wpas_dbus_simple_property_getter(message,
  1881. DBUS_TYPE_OBJECT_PATH,
  1882. &bss_obj_path);
  1883. return reply;
  1884. }
  1885. /**
  1886. * wpas_dbus_getter_current_network - Get current network object path
  1887. * @message: Pointer to incoming dbus message
  1888. * @wpa_s: wpa_supplicant structure for a network interface
  1889. * Returns: A dbus message containing a DBus object path to
  1890. * current network
  1891. *
  1892. * Getter for "CurrentNetwork" property.
  1893. */
  1894. DBusMessage * wpas_dbus_getter_current_network(DBusMessage *message,
  1895. struct wpa_supplicant *wpa_s)
  1896. {
  1897. DBusMessage *reply;
  1898. char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *net_obj_path = path_buf;
  1899. if (wpa_s->current_ssid)
  1900. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  1901. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
  1902. wpa_s->dbus_new_path, wpa_s->current_ssid->id);
  1903. else
  1904. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
  1905. reply = wpas_dbus_simple_property_getter(message,
  1906. DBUS_TYPE_OBJECT_PATH,
  1907. &net_obj_path);
  1908. return reply;
  1909. }
  1910. /**
  1911. * wpas_dbus_getter_current_auth_mode - Get current authentication type
  1912. * @message: Pointer to incoming dbus message
  1913. * @wpa_s: wpa_supplicant structure for a network interface
  1914. * Returns: A dbus message containing a string indicating the current
  1915. * authentication type.
  1916. *
  1917. * Getter for "CurrentAuthMode" property.
  1918. */
  1919. DBusMessage * wpas_dbus_getter_current_auth_mode(DBusMessage *message,
  1920. struct wpa_supplicant *wpa_s)
  1921. {
  1922. DBusMessage *reply;
  1923. const char *eap_mode;
  1924. const char *auth_mode;
  1925. char eap_mode_buf[WPAS_DBUS_AUTH_MODE_MAX];
  1926. if (wpa_s->wpa_state != WPA_COMPLETED) {
  1927. auth_mode = "INACTIVE";
  1928. } else if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
  1929. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1930. eap_mode = wpa_supplicant_get_eap_mode(wpa_s);
  1931. os_snprintf(eap_mode_buf, WPAS_DBUS_AUTH_MODE_MAX,
  1932. "EAP-%s", eap_mode);
  1933. auth_mode = eap_mode_buf;
  1934. } else {
  1935. auth_mode = wpa_key_mgmt_txt(wpa_s->key_mgmt,
  1936. wpa_s->current_ssid->proto);
  1937. }
  1938. reply = wpas_dbus_simple_property_getter(message,
  1939. DBUS_TYPE_STRING,
  1940. &auth_mode);
  1941. return reply;
  1942. }
  1943. /**
  1944. * wpas_dbus_getter_bridge_ifname - Get interface name
  1945. * @message: Pointer to incoming dbus message
  1946. * @wpa_s: wpa_supplicant structure for a network interface
  1947. * Returns: A dbus message containing a name of bridge network
  1948. * interface associated with with wpa_s
  1949. *
  1950. * Getter for "BridgeIfname" property.
  1951. */
  1952. DBusMessage * wpas_dbus_getter_bridge_ifname(DBusMessage *message,
  1953. struct wpa_supplicant *wpa_s)
  1954. {
  1955. const char *bridge_ifname = NULL;
  1956. bridge_ifname = wpa_s->bridge_ifname;
  1957. if (bridge_ifname == NULL) {
  1958. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bridge_ifname[dbus]: "
  1959. "wpa_s has no bridge interface name set");
  1960. return wpas_dbus_error_unknown_error(message, NULL);
  1961. }
  1962. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
  1963. &bridge_ifname);
  1964. }
  1965. /**
  1966. * wpas_dbus_getter_bsss - Get array of BSSs objects
  1967. * @message: Pointer to incoming dbus message
  1968. * @wpa_s: wpa_supplicant structure for a network interface
  1969. * Returns: a dbus message containing an array of all known BSS objects
  1970. * dbus paths
  1971. *
  1972. * Getter for "BSSs" property.
  1973. */
  1974. DBusMessage * wpas_dbus_getter_bsss(DBusMessage *message,
  1975. struct wpa_supplicant *wpa_s)
  1976. {
  1977. DBusMessage *reply = NULL;
  1978. struct wpa_bss *bss;
  1979. char **paths;
  1980. unsigned int i = 0;
  1981. paths = os_zalloc(wpa_s->num_bss * sizeof(char *));
  1982. if (!paths) {
  1983. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1984. NULL);
  1985. }
  1986. /* Loop through scan results and append each result's object path */
  1987. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  1988. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  1989. if (paths[i] == NULL) {
  1990. reply = dbus_message_new_error(message,
  1991. DBUS_ERROR_NO_MEMORY,
  1992. NULL);
  1993. goto out;
  1994. }
  1995. /* Construct the object path for this BSS. */
  1996. os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
  1997. "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
  1998. wpa_s->dbus_new_path, bss->id);
  1999. }
  2000. reply = wpas_dbus_simple_array_property_getter(message,
  2001. DBUS_TYPE_OBJECT_PATH,
  2002. paths, wpa_s->num_bss);
  2003. out:
  2004. while (i)
  2005. os_free(paths[--i]);
  2006. os_free(paths);
  2007. return reply;
  2008. }
  2009. /**
  2010. * wpas_dbus_getter_networks - Get array of networks objects
  2011. * @message: Pointer to incoming dbus message
  2012. * @wpa_s: wpa_supplicant structure for a network interface
  2013. * Returns: a dbus message containing an array of all configured
  2014. * networks dbus object paths.
  2015. *
  2016. * Getter for "Networks" property.
  2017. */
  2018. DBusMessage * wpas_dbus_getter_networks(DBusMessage *message,
  2019. struct wpa_supplicant *wpa_s)
  2020. {
  2021. DBusMessage *reply = NULL;
  2022. struct wpa_ssid *ssid;
  2023. char **paths;
  2024. unsigned int i = 0, num = 0;
  2025. if (wpa_s->conf == NULL) {
  2026. wpa_printf(MSG_ERROR, "wpas_dbus_getter_networks[dbus]: "
  2027. "An error occurred getting networks list.");
  2028. return wpas_dbus_error_unknown_error(message, NULL);
  2029. }
  2030. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  2031. num++;
  2032. paths = os_zalloc(num * sizeof(char *));
  2033. if (!paths) {
  2034. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2035. NULL);
  2036. }
  2037. /* Loop through configured networks and append object path of each */
  2038. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  2039. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  2040. if (paths[i] == NULL) {
  2041. reply = dbus_message_new_error(message,
  2042. DBUS_ERROR_NO_MEMORY,
  2043. NULL);
  2044. goto out;
  2045. }
  2046. /* Construct the object path for this network. */
  2047. os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
  2048. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
  2049. wpa_s->dbus_new_path, ssid->id);
  2050. }
  2051. reply = wpas_dbus_simple_array_property_getter(message,
  2052. DBUS_TYPE_OBJECT_PATH,
  2053. paths, num);
  2054. out:
  2055. while (i)
  2056. os_free(paths[--i]);
  2057. os_free(paths);
  2058. return reply;
  2059. }
  2060. /**
  2061. * wpas_dbus_getter_blobs - Get all blobs defined for this interface
  2062. * @message: Pointer to incoming dbus message
  2063. * @wpa_s: wpa_supplicant structure for a network interface
  2064. * Returns: a dbus message containing a dictionary of pairs (blob_name, blob)
  2065. *
  2066. * Getter for "Blobs" property.
  2067. */
  2068. DBusMessage * wpas_dbus_getter_blobs(DBusMessage *message,
  2069. struct wpa_supplicant *wpa_s)
  2070. {
  2071. DBusMessage *reply = NULL;
  2072. DBusMessageIter iter, variant_iter, dict_iter, entry_iter, array_iter;
  2073. struct wpa_config_blob *blob;
  2074. if (message == NULL)
  2075. reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
  2076. else
  2077. reply = dbus_message_new_method_return(message);
  2078. if (!reply)
  2079. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2080. NULL);
  2081. dbus_message_iter_init_append(reply, &iter);
  2082. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
  2083. "a{say}", &variant_iter) ||
  2084. !dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
  2085. "{say}", &dict_iter)) {
  2086. dbus_message_unref(reply);
  2087. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2088. NULL);
  2089. }
  2090. blob = wpa_s->conf->blobs;
  2091. while (blob) {
  2092. if (!dbus_message_iter_open_container(&dict_iter,
  2093. DBUS_TYPE_DICT_ENTRY,
  2094. NULL, &entry_iter) ||
  2095. !dbus_message_iter_append_basic(&entry_iter,
  2096. DBUS_TYPE_STRING,
  2097. &(blob->name)) ||
  2098. !dbus_message_iter_open_container(&entry_iter,
  2099. DBUS_TYPE_ARRAY,
  2100. DBUS_TYPE_BYTE_AS_STRING,
  2101. &array_iter) ||
  2102. !dbus_message_iter_append_fixed_array(&array_iter,
  2103. DBUS_TYPE_BYTE,
  2104. &(blob->data),
  2105. blob->len) ||
  2106. !dbus_message_iter_close_container(&entry_iter,
  2107. &array_iter) ||
  2108. !dbus_message_iter_close_container(&dict_iter,
  2109. &entry_iter)) {
  2110. dbus_message_unref(reply);
  2111. return dbus_message_new_error(message,
  2112. DBUS_ERROR_NO_MEMORY,
  2113. NULL);
  2114. }
  2115. blob = blob->next;
  2116. }
  2117. if (!dbus_message_iter_close_container(&variant_iter, &dict_iter) ||
  2118. !dbus_message_iter_close_container(&iter, &variant_iter)) {
  2119. dbus_message_unref(reply);
  2120. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2121. NULL);
  2122. }
  2123. return reply;
  2124. }
  2125. /**
  2126. * wpas_dbus_getter_bss_bssid - Return the BSSID of a BSS
  2127. * @message: Pointer to incoming dbus message
  2128. * @bss: a pair of interface describing structure and bss's id
  2129. * Returns: a dbus message containing the bssid for the requested bss
  2130. *
  2131. * Getter for "BSSID" property.
  2132. */
  2133. DBusMessage * wpas_dbus_getter_bss_bssid(DBusMessage *message,
  2134. struct bss_handler_args *bss)
  2135. {
  2136. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2137. if (!res) {
  2138. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_bssid[dbus]: no "
  2139. "bss with id %d found", bss->id);
  2140. return NULL;
  2141. }
  2142. return wpas_dbus_simple_array_property_getter(message, DBUS_TYPE_BYTE,
  2143. res->bssid, ETH_ALEN);
  2144. }
  2145. /**
  2146. * wpas_dbus_getter_bss_ssid - Return the SSID of a BSS
  2147. * @message: Pointer to incoming dbus message
  2148. * @bss: a pair of interface describing structure and bss's id
  2149. * Returns: a dbus message containing the ssid for the requested bss
  2150. *
  2151. * Getter for "SSID" property.
  2152. */
  2153. DBusMessage * wpas_dbus_getter_bss_ssid(DBusMessage *message,
  2154. struct bss_handler_args *bss)
  2155. {
  2156. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2157. if (!res) {
  2158. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_ssid[dbus]: no "
  2159. "bss with id %d found", bss->id);
  2160. return NULL;
  2161. }
  2162. return wpas_dbus_simple_array_property_getter(message, DBUS_TYPE_BYTE,
  2163. res->ssid,
  2164. res->ssid_len);
  2165. }
  2166. /**
  2167. * wpas_dbus_getter_bss_privacy - Return the privacy flag of a BSS
  2168. * @message: Pointer to incoming dbus message
  2169. * @bss: a pair of interface describing structure and bss's id
  2170. * Returns: a dbus message containing the privacy flag value of requested bss
  2171. *
  2172. * Getter for "Privacy" property.
  2173. */
  2174. DBusMessage * wpas_dbus_getter_bss_privacy(DBusMessage *message,
  2175. struct bss_handler_args *bss)
  2176. {
  2177. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2178. dbus_bool_t privacy;
  2179. if (!res) {
  2180. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_privacy[dbus]: no "
  2181. "bss with id %d found", bss->id);
  2182. return NULL;
  2183. }
  2184. privacy = (res->caps & IEEE80211_CAP_PRIVACY) ? TRUE : FALSE;
  2185. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
  2186. &privacy);
  2187. }
  2188. /**
  2189. * wpas_dbus_getter_bss_mode - Return the mode of a BSS
  2190. * @message: Pointer to incoming dbus message
  2191. * @bss: a pair of interface describing structure and bss's id
  2192. * Returns: a dbus message containing the mode of requested bss
  2193. *
  2194. * Getter for "Mode" property.
  2195. */
  2196. DBusMessage * wpas_dbus_getter_bss_mode(DBusMessage *message,
  2197. struct bss_handler_args *bss)
  2198. {
  2199. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2200. const char *mode;
  2201. if (!res) {
  2202. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_mode[dbus]: no "
  2203. "bss with id %d found", bss->id);
  2204. return NULL;
  2205. }
  2206. if (res->caps & IEEE80211_CAP_IBSS)
  2207. mode = "ad-hoc";
  2208. else
  2209. mode = "infrastructure";
  2210. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
  2211. &mode);
  2212. }
  2213. /**
  2214. * wpas_dbus_getter_bss_level - Return the signal strength of a BSS
  2215. * @message: Pointer to incoming dbus message
  2216. * @bss: a pair of interface describing structure and bss's id
  2217. * Returns: a dbus message containing the signal strength of requested bss
  2218. *
  2219. * Getter for "Level" property.
  2220. */
  2221. DBusMessage * wpas_dbus_getter_bss_signal(DBusMessage *message,
  2222. struct bss_handler_args *bss)
  2223. {
  2224. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2225. if (!res) {
  2226. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_signal[dbus]: no "
  2227. "bss with id %d found", bss->id);
  2228. return NULL;
  2229. }
  2230. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_INT16,
  2231. &res->level);
  2232. }
  2233. /**
  2234. * wpas_dbus_getter_bss_frequency - Return the frequency of a BSS
  2235. * @message: Pointer to incoming dbus message
  2236. * @bss: a pair of interface describing structure and bss's id
  2237. * Returns: a dbus message containing the frequency of requested bss
  2238. *
  2239. * Getter for "Frequency" property.
  2240. */
  2241. DBusMessage * wpas_dbus_getter_bss_frequency(DBusMessage *message,
  2242. struct bss_handler_args *bss)
  2243. {
  2244. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2245. if (!res) {
  2246. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_frequency[dbus]: "
  2247. "no bss with id %d found", bss->id);
  2248. return NULL;
  2249. }
  2250. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_UINT16,
  2251. &res->freq);
  2252. }
  2253. static int cmp_u8s_desc(const void *a, const void *b)
  2254. {
  2255. return (*(u8 *) b - *(u8 *) a);
  2256. }
  2257. /**
  2258. * wpas_dbus_getter_bss_rates - Return available bit rates of a BSS
  2259. * @message: Pointer to incoming dbus message
  2260. * @bss: a pair of interface describing structure and bss's id
  2261. * Returns: a dbus message containing sorted array of bit rates
  2262. *
  2263. * Getter for "Rates" property.
  2264. */
  2265. DBusMessage * wpas_dbus_getter_bss_rates(DBusMessage *message,
  2266. struct bss_handler_args *bss)
  2267. {
  2268. DBusMessage *reply;
  2269. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2270. u8 *ie_rates = NULL;
  2271. u32 *real_rates;
  2272. int rates_num, i;
  2273. if (!res) {
  2274. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_rates[dbus]: "
  2275. "no bss with id %d found", bss->id);
  2276. return NULL;
  2277. }
  2278. rates_num = wpa_bss_get_bit_rates(res, &ie_rates);
  2279. if (rates_num < 0)
  2280. return NULL;
  2281. qsort(ie_rates, rates_num, 1, cmp_u8s_desc);
  2282. real_rates = os_malloc(sizeof(u32) * rates_num);
  2283. if (!real_rates) {
  2284. os_free(ie_rates);
  2285. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2286. NULL);
  2287. }
  2288. for (i = 0; i < rates_num; i++)
  2289. real_rates[i] = ie_rates[i] * 500000;
  2290. reply = wpas_dbus_simple_array_property_getter(message,
  2291. DBUS_TYPE_UINT32,
  2292. real_rates, rates_num);
  2293. os_free(ie_rates);
  2294. os_free(real_rates);
  2295. return reply;
  2296. }
  2297. static DBusMessage * wpas_dbus_get_bss_security_prop(
  2298. DBusMessage *message, struct wpa_ie_data *ie_data)
  2299. {
  2300. DBusMessage *reply;
  2301. DBusMessageIter iter, iter_dict, variant_iter;
  2302. const char *group;
  2303. const char *pairwise[2]; /* max 2 pairwise ciphers is supported */
  2304. const char *key_mgmt[7]; /* max 7 key managements may be supported */
  2305. int n;
  2306. if (message == NULL)
  2307. reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
  2308. else
  2309. reply = dbus_message_new_method_return(message);
  2310. if (!reply)
  2311. goto nomem;
  2312. dbus_message_iter_init_append(reply, &iter);
  2313. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
  2314. "a{sv}", &variant_iter))
  2315. goto nomem;
  2316. if (!wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
  2317. goto nomem;
  2318. /* KeyMgmt */
  2319. n = 0;
  2320. if (ie_data->key_mgmt & WPA_KEY_MGMT_PSK)
  2321. key_mgmt[n++] = "wpa-psk";
  2322. if (ie_data->key_mgmt & WPA_KEY_MGMT_FT_PSK)
  2323. key_mgmt[n++] = "wpa-ft-psk";
  2324. if (ie_data->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
  2325. key_mgmt[n++] = "wpa-psk-sha256";
  2326. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  2327. key_mgmt[n++] = "wpa-eap";
  2328. if (ie_data->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
  2329. key_mgmt[n++] = "wpa-ft-eap";
  2330. if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
  2331. key_mgmt[n++] = "wpa-eap-sha256";
  2332. if (ie_data->key_mgmt & WPA_KEY_MGMT_NONE)
  2333. key_mgmt[n++] = "wpa-none";
  2334. if (!wpa_dbus_dict_append_string_array(&iter_dict, "KeyMgmt",
  2335. key_mgmt, n))
  2336. goto nomem;
  2337. /* Group */
  2338. switch (ie_data->group_cipher) {
  2339. case WPA_CIPHER_WEP40:
  2340. group = "wep40";
  2341. break;
  2342. case WPA_CIPHER_TKIP:
  2343. group = "tkip";
  2344. break;
  2345. case WPA_CIPHER_CCMP:
  2346. group = "ccmp";
  2347. break;
  2348. case WPA_CIPHER_WEP104:
  2349. group = "wep104";
  2350. break;
  2351. default:
  2352. group = "";
  2353. break;
  2354. }
  2355. if (!wpa_dbus_dict_append_string(&iter_dict, "Group", group))
  2356. goto nomem;
  2357. /* Pairwise */
  2358. n = 0;
  2359. if (ie_data->pairwise_cipher & WPA_CIPHER_TKIP)
  2360. pairwise[n++] = "tkip";
  2361. if (ie_data->pairwise_cipher & WPA_CIPHER_CCMP)
  2362. pairwise[n++] = "ccmp";
  2363. if (!wpa_dbus_dict_append_string_array(&iter_dict, "Pairwise",
  2364. pairwise, n))
  2365. goto nomem;
  2366. /* Management group (RSN only) */
  2367. if (ie_data->proto == WPA_PROTO_RSN) {
  2368. switch (ie_data->mgmt_group_cipher) {
  2369. #ifdef CONFIG_IEEE80211W
  2370. case WPA_CIPHER_AES_128_CMAC:
  2371. group = "aes128cmac";
  2372. break;
  2373. #endif /* CONFIG_IEEE80211W */
  2374. default:
  2375. group = "";
  2376. break;
  2377. }
  2378. if (!wpa_dbus_dict_append_string(&iter_dict, "MgmtGroup",
  2379. group))
  2380. goto nomem;
  2381. }
  2382. if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict))
  2383. goto nomem;
  2384. if (!dbus_message_iter_close_container(&iter, &variant_iter))
  2385. goto nomem;
  2386. return reply;
  2387. nomem:
  2388. if (reply)
  2389. dbus_message_unref(reply);
  2390. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY, NULL);
  2391. }
  2392. /**
  2393. * wpas_dbus_getter_bss_wpa - Return the WPA options of a BSS
  2394. * @message: Pointer to incoming dbus message
  2395. * @bss: a pair of interface describing structure and bss's id
  2396. * Returns: a dbus message containing the WPA options of requested bss
  2397. *
  2398. * Getter for "WPA" property.
  2399. */
  2400. DBusMessage * wpas_dbus_getter_bss_wpa(DBusMessage *message,
  2401. struct bss_handler_args *bss)
  2402. {
  2403. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2404. struct wpa_ie_data wpa_data;
  2405. const u8 *ie;
  2406. if (!res) {
  2407. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_wpa[dbus]: no "
  2408. "bss with id %d found", bss->id);
  2409. return NULL;
  2410. }
  2411. os_memset(&wpa_data, 0, sizeof(wpa_data));
  2412. ie = wpa_bss_get_vendor_ie(res, WPA_IE_VENDOR_TYPE);
  2413. if (ie) {
  2414. if (wpa_parse_wpa_ie(ie, 2 + ie[1], &wpa_data) < 0)
  2415. return wpas_dbus_error_unknown_error(message,
  2416. "invalid WPA IE");
  2417. }
  2418. return wpas_dbus_get_bss_security_prop(message, &wpa_data);
  2419. }
  2420. /**
  2421. * wpas_dbus_getter_bss_rsn - Return the RSN options of a BSS
  2422. * @message: Pointer to incoming dbus message
  2423. * @bss: a pair of interface describing structure and bss's id
  2424. * Returns: a dbus message containing the RSN options of requested bss
  2425. *
  2426. * Getter for "RSN" property.
  2427. */
  2428. DBusMessage * wpas_dbus_getter_bss_rsn(DBusMessage *message,
  2429. struct bss_handler_args *bss)
  2430. {
  2431. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2432. struct wpa_ie_data wpa_data;
  2433. const u8 *ie;
  2434. if (!res) {
  2435. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_rsn[dbus]: no "
  2436. "bss with id %d found", bss->id);
  2437. return NULL;
  2438. }
  2439. os_memset(&wpa_data, 0, sizeof(wpa_data));
  2440. ie = wpa_bss_get_ie(res, WLAN_EID_RSN);
  2441. if (ie) {
  2442. if (wpa_parse_wpa_ie(ie, 2 + ie[1], &wpa_data) < 0)
  2443. return wpas_dbus_error_unknown_error(message,
  2444. "invalid RSN IE");
  2445. }
  2446. return wpas_dbus_get_bss_security_prop(message, &wpa_data);
  2447. }
  2448. /**
  2449. * wpas_dbus_getter_bss_ies - Return all IEs of a BSS
  2450. * @message: Pointer to incoming dbus message
  2451. * @bss: a pair of interface describing structure and bss's id
  2452. * Returns: a dbus message containing IEs byte array
  2453. *
  2454. * Getter for "IEs" property.
  2455. */
  2456. DBusMessage * wpas_dbus_getter_bss_ies(DBusMessage *message,
  2457. struct bss_handler_args *bss)
  2458. {
  2459. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2460. if (!res) {
  2461. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_ies[dbus]: no "
  2462. "bss with id %d found", bss->id);
  2463. return NULL;
  2464. }
  2465. return wpas_dbus_simple_array_property_getter(message, DBUS_TYPE_BYTE,
  2466. res + 1, res->ie_len);
  2467. }
  2468. /**
  2469. * wpas_dbus_getter_enabled - Check whether network is enabled or disabled
  2470. * @message: Pointer to incoming dbus message
  2471. * @wpas_dbus_setter_enabled: wpa_supplicant structure for a network interface
  2472. * and wpa_ssid structure for a configured network
  2473. * Returns: DBus message with boolean indicating state of configured network
  2474. * or DBus error on failure
  2475. *
  2476. * Getter for "enabled" property of a configured network.
  2477. */
  2478. DBusMessage * wpas_dbus_getter_enabled(DBusMessage *message,
  2479. struct network_handler_args *net)
  2480. {
  2481. dbus_bool_t enabled = net->ssid->disabled ? FALSE : TRUE;
  2482. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
  2483. &enabled);
  2484. }
  2485. /**
  2486. * wpas_dbus_setter_enabled - Mark a configured network as enabled or disabled
  2487. * @message: Pointer to incoming dbus message
  2488. * @wpas_dbus_setter_enabled: wpa_supplicant structure for a network interface
  2489. * and wpa_ssid structure for a configured network
  2490. * Returns: NULL indicating success or DBus error on failure
  2491. *
  2492. * Setter for "Enabled" property of a configured network.
  2493. */
  2494. DBusMessage * wpas_dbus_setter_enabled(DBusMessage *message,
  2495. struct network_handler_args *net)
  2496. {
  2497. DBusMessage *reply = NULL;
  2498. struct wpa_supplicant *wpa_s;
  2499. struct wpa_ssid *ssid;
  2500. dbus_bool_t enable;
  2501. reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_BOOLEAN,
  2502. &enable);
  2503. if (reply)
  2504. return reply;
  2505. wpa_s = net->wpa_s;
  2506. ssid = net->ssid;
  2507. if (enable)
  2508. wpa_supplicant_enable_network(wpa_s, ssid);
  2509. else
  2510. wpa_supplicant_disable_network(wpa_s, ssid);
  2511. return NULL;
  2512. }
  2513. /**
  2514. * wpas_dbus_getter_network_properties - Get options for a configured network
  2515. * @message: Pointer to incoming dbus message
  2516. * @net: wpa_supplicant structure for a network interface and
  2517. * wpa_ssid structure for a configured network
  2518. * Returns: DBus message with network properties or DBus error on failure
  2519. *
  2520. * Getter for "Properties" property of a configured network.
  2521. */
  2522. DBusMessage * wpas_dbus_getter_network_properties(
  2523. DBusMessage *message, struct network_handler_args *net)
  2524. {
  2525. DBusMessage *reply = NULL;
  2526. DBusMessageIter iter, variant_iter, dict_iter;
  2527. char **iterator;
  2528. char **props = wpa_config_get_all(net->ssid, 1);
  2529. if (!props)
  2530. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2531. NULL);
  2532. if (message == NULL)
  2533. reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
  2534. else
  2535. reply = dbus_message_new_method_return(message);
  2536. if (!reply) {
  2537. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2538. NULL);
  2539. goto out;
  2540. }
  2541. dbus_message_iter_init_append(reply, &iter);
  2542. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
  2543. "a{sv}", &variant_iter) ||
  2544. !wpa_dbus_dict_open_write(&variant_iter, &dict_iter)) {
  2545. dbus_message_unref(reply);
  2546. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2547. NULL);
  2548. goto out;
  2549. }
  2550. iterator = props;
  2551. while (*iterator) {
  2552. if (!wpa_dbus_dict_append_string(&dict_iter, *iterator,
  2553. *(iterator + 1))) {
  2554. dbus_message_unref(reply);
  2555. reply = dbus_message_new_error(message,
  2556. DBUS_ERROR_NO_MEMORY,
  2557. NULL);
  2558. goto out;
  2559. }
  2560. iterator += 2;
  2561. }
  2562. if (!wpa_dbus_dict_close_write(&variant_iter, &dict_iter) ||
  2563. !dbus_message_iter_close_container(&iter, &variant_iter)) {
  2564. dbus_message_unref(reply);
  2565. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2566. NULL);
  2567. goto out;
  2568. }
  2569. out:
  2570. iterator = props;
  2571. while (*iterator) {
  2572. os_free(*iterator);
  2573. iterator++;
  2574. }
  2575. os_free(props);
  2576. return reply;
  2577. }
  2578. /**
  2579. * wpas_dbus_setter_network_properties - Set options for a configured network
  2580. * @message: Pointer to incoming dbus message
  2581. * @net: wpa_supplicant structure for a network interface and
  2582. * wpa_ssid structure for a configured network
  2583. * Returns: NULL indicating success or DBus error on failure
  2584. *
  2585. * Setter for "Properties" property of a configured network.
  2586. */
  2587. DBusMessage * wpas_dbus_setter_network_properties(
  2588. DBusMessage *message, struct network_handler_args *net)
  2589. {
  2590. struct wpa_ssid *ssid = net->ssid;
  2591. DBusMessage *reply = NULL;
  2592. DBusMessageIter iter, variant_iter;
  2593. dbus_message_iter_init(message, &iter);
  2594. dbus_message_iter_next(&iter);
  2595. dbus_message_iter_next(&iter);
  2596. dbus_message_iter_recurse(&iter, &variant_iter);
  2597. reply = set_network_properties(message, net->wpa_s, ssid,
  2598. &variant_iter);
  2599. if (reply)
  2600. wpa_printf(MSG_DEBUG, "dbus control interface couldn't set "
  2601. "network properties");
  2602. return reply;
  2603. }