dbus_new_handlers.c 79 KB

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