ctrl_iface_dbus_new_handlers.c 98 KB

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