dbus_new_handlers.c 81 KB

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