dbus_new_handlers.c 80 KB

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