dbus_new_handlers.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815
  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_invalid_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_invalid_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_invalid_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_invalid_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_invalid_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_invalid_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_invalid_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. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  627. NULL);
  628. if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
  629. DBUS_TYPE_INVALID)) {
  630. dbus_message_unref(reply);
  631. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  632. NULL);
  633. }
  634. return reply;
  635. }
  636. /**
  637. * wpas_dbus_getter_debug_level - Get debug level
  638. * @message: Pointer to incoming dbus message
  639. * @global: %wpa_supplicant global data structure
  640. * Returns: DBus message with value of debug level
  641. *
  642. * Getter for "DebugLevel" property.
  643. */
  644. DBusMessage * wpas_dbus_getter_debug_level(DBusMessage *message,
  645. struct wpa_global *global)
  646. {
  647. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BYTE,
  648. &wpa_debug_level);
  649. }
  650. /**
  651. * wpas_dbus_getter_debug_timestamp - Get debug timestamp
  652. * @message: Pointer to incoming dbus message
  653. * @global: %wpa_supplicant global data structure
  654. * Returns: DBus message with value of debug timestamp
  655. *
  656. * Getter for "DebugTimestamp" property.
  657. */
  658. DBusMessage * wpas_dbus_getter_debug_timestamp(DBusMessage *message,
  659. struct wpa_global *global)
  660. {
  661. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
  662. &wpa_debug_timestamp);
  663. }
  664. /**
  665. * wpas_dbus_getter_debug_show_keys - Get debug show keys
  666. * @message: Pointer to incoming dbus message
  667. * @global: %wpa_supplicant global data structure
  668. * Returns: DBus message with value of debug show_keys
  669. *
  670. * Getter for "DebugShowKeys" property.
  671. */
  672. DBusMessage * wpas_dbus_getter_debug_show_keys(DBusMessage *message,
  673. struct wpa_global *global)
  674. {
  675. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
  676. &wpa_debug_show_keys);
  677. }
  678. /**
  679. * wpas_dbus_setter_debug_level - Set debug level
  680. * @message: Pointer to incoming dbus message
  681. * @global: %wpa_supplicant global data structure
  682. * Returns: %NULL or DBus error message
  683. *
  684. * Setter for "DebugLevel" property.
  685. */
  686. DBusMessage * wpas_dbus_setter_debug_level(DBusMessage *message,
  687. struct wpa_global *global)
  688. {
  689. DBusMessage *reply = NULL;
  690. dbus_uint16_t val;
  691. reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_INT16,
  692. &val);
  693. if (reply)
  694. return reply;
  695. if (wpa_supplicant_set_debug_params(global, val, wpa_debug_timestamp,
  696. wpa_debug_show_keys)) {
  697. dbus_message_unref(reply);
  698. return wpas_dbus_error_invalid_args(
  699. message, "Wrong debug level value");
  700. }
  701. return NULL;
  702. }
  703. /**
  704. * wpas_dbus_setter_debug_timestamp - Set debug timestamp
  705. * @message: Pointer to incoming dbus message
  706. * @global: %wpa_supplicant global data structure
  707. * Returns: %NULL or DBus error message
  708. *
  709. * Setter for "DebugTimestamp" property.
  710. */
  711. DBusMessage * wpas_dbus_setter_debug_timestamp(DBusMessage *message,
  712. struct wpa_global *global)
  713. {
  714. DBusMessage *reply = NULL;
  715. dbus_bool_t val;
  716. reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_BOOLEAN,
  717. &val);
  718. if (reply)
  719. return reply;
  720. wpa_supplicant_set_debug_params(global, wpa_debug_level, val ? 1 : 0,
  721. wpa_debug_show_keys);
  722. return NULL;
  723. }
  724. /**
  725. * wpas_dbus_setter_debug_show_keys - Set debug show keys
  726. * @message: Pointer to incoming dbus message
  727. * @global: %wpa_supplicant global data structure
  728. * Returns: %NULL or DBus error message
  729. *
  730. * Setter for "DebugShowKeys" property.
  731. */
  732. DBusMessage * wpas_dbus_setter_debug_show_keys(DBusMessage *message,
  733. struct wpa_global *global)
  734. {
  735. DBusMessage *reply = NULL;
  736. dbus_bool_t val;
  737. reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_BOOLEAN,
  738. &val);
  739. if (reply)
  740. return reply;
  741. wpa_supplicant_set_debug_params(global, wpa_debug_level,
  742. wpa_debug_timestamp,
  743. val ? 1 : 0);
  744. return NULL;
  745. }
  746. /**
  747. * wpas_dbus_getter_interfaces - Request registered interfaces list
  748. * @message: Pointer to incoming dbus message
  749. * @global: %wpa_supplicant global data structure
  750. * Returns: The object paths array containing registered interfaces
  751. * objects paths or DBus error on failure
  752. *
  753. * Getter for "Interfaces" property. Handles requests
  754. * by dbus clients to return list of registered interfaces objects
  755. * paths
  756. */
  757. DBusMessage * wpas_dbus_getter_interfaces(DBusMessage *message,
  758. struct wpa_global *global)
  759. {
  760. DBusMessage *reply = NULL;
  761. struct wpa_supplicant *wpa_s;
  762. const char **paths;
  763. unsigned int i = 0, num = 0;
  764. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
  765. num++;
  766. paths = os_zalloc(num * sizeof(char*));
  767. if (!paths) {
  768. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  769. NULL);
  770. }
  771. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
  772. paths[i] = wpa_s->dbus_new_path;
  773. reply = wpas_dbus_simple_array_property_getter(message,
  774. DBUS_TYPE_OBJECT_PATH,
  775. paths, num);
  776. os_free(paths);
  777. return reply;
  778. }
  779. /**
  780. * wpas_dbus_getter_eap_methods - Request supported EAP methods list
  781. * @message: Pointer to incoming dbus message
  782. * @nothing: not used argument. may be NULL or anything else
  783. * Returns: The object paths array containing supported EAP methods
  784. * represented by strings or DBus error on failure
  785. *
  786. * Getter for "EapMethods" property. Handles requests
  787. * by dbus clients to return list of strings with supported EAP methods
  788. */
  789. DBusMessage * wpas_dbus_getter_eap_methods(DBusMessage *message, void *nothing)
  790. {
  791. DBusMessage *reply = NULL;
  792. char **eap_methods;
  793. size_t num_items = 0;
  794. eap_methods = eap_get_names_as_string_array(&num_items);
  795. if (!eap_methods) {
  796. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  797. NULL);
  798. }
  799. reply = wpas_dbus_simple_array_property_getter(message,
  800. DBUS_TYPE_STRING,
  801. eap_methods, num_items);
  802. while (num_items)
  803. os_free(eap_methods[--num_items]);
  804. os_free(eap_methods);
  805. return reply;
  806. }
  807. static int wpas_dbus_get_scan_type(DBusMessage *message, DBusMessageIter *var,
  808. char **type, DBusMessage **reply)
  809. {
  810. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_STRING) {
  811. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  812. "Type must be a string");
  813. *reply = wpas_dbus_error_invalid_args(
  814. message, "Wrong Type value type. String required");
  815. return -1;
  816. }
  817. dbus_message_iter_get_basic(var, type);
  818. return 0;
  819. }
  820. static int wpas_dbus_get_scan_ssids(DBusMessage *message, DBusMessageIter *var,
  821. struct wpa_driver_scan_params *params,
  822. DBusMessage **reply)
  823. {
  824. struct wpa_driver_scan_ssid *ssids = params->ssids;
  825. size_t ssids_num = 0;
  826. u8 *ssid;
  827. DBusMessageIter array_iter, sub_array_iter;
  828. char *val;
  829. int len;
  830. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
  831. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ssids "
  832. "must be an array of arrays of bytes");
  833. *reply = wpas_dbus_error_invalid_args(
  834. message, "Wrong SSIDs value type. Array of arrays of "
  835. "bytes required");
  836. return -1;
  837. }
  838. dbus_message_iter_recurse(var, &array_iter);
  839. if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY ||
  840. dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_BYTE)
  841. {
  842. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ssids "
  843. "must be an array of arrays of bytes");
  844. *reply = wpas_dbus_error_invalid_args(
  845. message, "Wrong SSIDs value type. Array of arrays of "
  846. "bytes required");
  847. return -1;
  848. }
  849. while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_ARRAY)
  850. {
  851. if (ssids_num >= WPAS_MAX_SCAN_SSIDS) {
  852. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  853. "Too many ssids specified on scan dbus "
  854. "call");
  855. *reply = wpas_dbus_error_invalid_args(
  856. message, "Too many ssids specified. Specify "
  857. "at most four");
  858. return -1;
  859. }
  860. dbus_message_iter_recurse(&array_iter, &sub_array_iter);
  861. dbus_message_iter_get_fixed_array(&sub_array_iter, &val, &len);
  862. if (len == 0) {
  863. dbus_message_iter_next(&array_iter);
  864. continue;
  865. }
  866. ssid = os_malloc(len);
  867. if (ssid == NULL) {
  868. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  869. "out of memory. Cannot allocate memory for "
  870. "SSID");
  871. *reply = dbus_message_new_error(
  872. message, DBUS_ERROR_NO_MEMORY, NULL);
  873. return -1;
  874. }
  875. os_memcpy(ssid, val, len);
  876. ssids[ssids_num].ssid = ssid;
  877. ssids[ssids_num].ssid_len = len;
  878. dbus_message_iter_next(&array_iter);
  879. ssids_num++;
  880. }
  881. params->num_ssids = ssids_num;
  882. return 0;
  883. }
  884. static int wpas_dbus_get_scan_ies(DBusMessage *message, DBusMessageIter *var,
  885. struct wpa_driver_scan_params *params,
  886. DBusMessage **reply)
  887. {
  888. u8 *ies = NULL, *nies;
  889. int ies_len = 0;
  890. DBusMessageIter array_iter, sub_array_iter;
  891. char *val;
  892. int len;
  893. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
  894. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ies must "
  895. "be an array of arrays of bytes");
  896. *reply = wpas_dbus_error_invalid_args(
  897. message, "Wrong IEs value type. Array of arrays of "
  898. "bytes required");
  899. return -1;
  900. }
  901. dbus_message_iter_recurse(var, &array_iter);
  902. if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY ||
  903. dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_BYTE)
  904. {
  905. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ies must "
  906. "be an array of arrays of bytes");
  907. *reply = wpas_dbus_error_invalid_args(
  908. message, "Wrong IEs value type. Array required");
  909. return -1;
  910. }
  911. while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_ARRAY)
  912. {
  913. dbus_message_iter_recurse(&array_iter, &sub_array_iter);
  914. dbus_message_iter_get_fixed_array(&sub_array_iter, &val, &len);
  915. if (len == 0) {
  916. dbus_message_iter_next(&array_iter);
  917. continue;
  918. }
  919. nies = os_realloc(ies, ies_len + len);
  920. if (nies == NULL) {
  921. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  922. "out of memory. Cannot allocate memory for "
  923. "IE");
  924. os_free(ies);
  925. *reply = dbus_message_new_error(
  926. message, DBUS_ERROR_NO_MEMORY, NULL);
  927. return -1;
  928. }
  929. ies = nies;
  930. os_memcpy(ies + ies_len, val, len);
  931. ies_len += len;
  932. dbus_message_iter_next(&array_iter);
  933. }
  934. params->extra_ies = ies;
  935. params->extra_ies_len = ies_len;
  936. return 0;
  937. }
  938. static int wpas_dbus_get_scan_channels(DBusMessage *message,
  939. DBusMessageIter *var,
  940. struct wpa_driver_scan_params *params,
  941. DBusMessage **reply)
  942. {
  943. DBusMessageIter array_iter, sub_array_iter;
  944. int *freqs = NULL, *nfreqs;
  945. int freqs_num = 0;
  946. if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
  947. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  948. "Channels must be an array of structs");
  949. *reply = wpas_dbus_error_invalid_args(
  950. message, "Wrong Channels value type. Array of structs "
  951. "required");
  952. return -1;
  953. }
  954. dbus_message_iter_recurse(var, &array_iter);
  955. if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_STRUCT) {
  956. wpa_printf(MSG_DEBUG,
  957. "wpas_dbus_handler_scan[dbus]: Channels must be an "
  958. "array of structs");
  959. *reply = wpas_dbus_error_invalid_args(
  960. message, "Wrong Channels value type. Array of structs "
  961. "required");
  962. return -1;
  963. }
  964. while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_STRUCT)
  965. {
  966. int freq, width;
  967. dbus_message_iter_recurse(&array_iter, &sub_array_iter);
  968. if (dbus_message_iter_get_arg_type(&sub_array_iter) !=
  969. DBUS_TYPE_UINT32) {
  970. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  971. "Channel must by specified by struct of "
  972. "two UINT32s %c",
  973. dbus_message_iter_get_arg_type(
  974. &sub_array_iter));
  975. *reply = wpas_dbus_error_invalid_args(
  976. message, "Wrong Channel struct. Two UINT32s "
  977. "required");
  978. os_free(freqs);
  979. return -1;
  980. }
  981. dbus_message_iter_get_basic(&sub_array_iter, &freq);
  982. if (!dbus_message_iter_next(&sub_array_iter) ||
  983. dbus_message_iter_get_arg_type(&sub_array_iter) !=
  984. DBUS_TYPE_UINT32) {
  985. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  986. "Channel must by specified by struct of "
  987. "two UINT32s");
  988. *reply = wpas_dbus_error_invalid_args(
  989. message,
  990. "Wrong Channel struct. Two UINT32s required");
  991. os_free(freqs);
  992. return -1;
  993. }
  994. dbus_message_iter_get_basic(&sub_array_iter, &width);
  995. #define FREQS_ALLOC_CHUNK 32
  996. if (freqs_num % FREQS_ALLOC_CHUNK == 0) {
  997. nfreqs = os_realloc(freqs, sizeof(int) *
  998. (freqs_num + FREQS_ALLOC_CHUNK));
  999. if (nfreqs == NULL)
  1000. os_free(freqs);
  1001. freqs = nfreqs;
  1002. }
  1003. if (freqs == NULL) {
  1004. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1005. "out of memory. can't allocate memory for "
  1006. "freqs");
  1007. *reply = dbus_message_new_error(
  1008. message, DBUS_ERROR_NO_MEMORY, NULL);
  1009. return -1;
  1010. }
  1011. freqs[freqs_num] = freq;
  1012. freqs_num++;
  1013. dbus_message_iter_next(&array_iter);
  1014. }
  1015. nfreqs = os_realloc(freqs,
  1016. sizeof(int) * (freqs_num + 1));
  1017. if (nfreqs == NULL)
  1018. os_free(freqs);
  1019. freqs = nfreqs;
  1020. if (freqs == NULL) {
  1021. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1022. "out of memory. Can't allocate memory for freqs");
  1023. *reply = dbus_message_new_error(
  1024. message, DBUS_ERROR_NO_MEMORY, NULL);
  1025. return -1;
  1026. }
  1027. freqs[freqs_num] = 0;
  1028. params->freqs = freqs;
  1029. return 0;
  1030. }
  1031. /**
  1032. * wpas_dbus_handler_scan - Request a wireless scan on an interface
  1033. * @message: Pointer to incoming dbus message
  1034. * @wpa_s: wpa_supplicant structure for a network interface
  1035. * Returns: NULL indicating success or DBus error message on failure
  1036. *
  1037. * Handler function for "Scan" method call of a network device. Requests
  1038. * that wpa_supplicant perform a wireless scan as soon as possible
  1039. * on a particular wireless interface.
  1040. */
  1041. DBusMessage * wpas_dbus_handler_scan(DBusMessage *message,
  1042. struct wpa_supplicant *wpa_s)
  1043. {
  1044. DBusMessage *reply = NULL;
  1045. DBusMessageIter iter, dict_iter, entry_iter, variant_iter;
  1046. char *key = NULL, *type = NULL;
  1047. struct wpa_driver_scan_params params;
  1048. size_t i;
  1049. os_memset(&params, 0, sizeof(params));
  1050. dbus_message_iter_init(message, &iter);
  1051. dbus_message_iter_recurse(&iter, &dict_iter);
  1052. while (dbus_message_iter_get_arg_type(&dict_iter) ==
  1053. DBUS_TYPE_DICT_ENTRY) {
  1054. dbus_message_iter_recurse(&dict_iter, &entry_iter);
  1055. dbus_message_iter_get_basic(&entry_iter, &key);
  1056. dbus_message_iter_next(&entry_iter);
  1057. dbus_message_iter_recurse(&entry_iter, &variant_iter);
  1058. if (os_strcmp(key, "Type") == 0) {
  1059. if (wpas_dbus_get_scan_type(message, &variant_iter,
  1060. &type, &reply) < 0)
  1061. goto out;
  1062. } else if (os_strcmp(key, "SSIDs") == 0) {
  1063. if (wpas_dbus_get_scan_ssids(message, &variant_iter,
  1064. &params, &reply) < 0)
  1065. goto out;
  1066. } else if (os_strcmp(key, "IEs") == 0) {
  1067. if (wpas_dbus_get_scan_ies(message, &variant_iter,
  1068. &params, &reply) < 0)
  1069. goto out;
  1070. } else if (os_strcmp(key, "Channels") == 0) {
  1071. if (wpas_dbus_get_scan_channels(message, &variant_iter,
  1072. &params, &reply) < 0)
  1073. goto out;
  1074. } else {
  1075. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1076. "Unknown argument %s", key);
  1077. reply = wpas_dbus_error_invalid_args(message, key);
  1078. goto out;
  1079. }
  1080. dbus_message_iter_next(&dict_iter);
  1081. }
  1082. if (!type) {
  1083. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1084. "Scan type not specified");
  1085. reply = wpas_dbus_error_invalid_args(message, key);
  1086. goto out;
  1087. }
  1088. if (!os_strcmp(type, "passive")) {
  1089. if (params.num_ssids || params.extra_ies_len) {
  1090. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1091. "SSIDs or IEs specified for passive scan.");
  1092. reply = wpas_dbus_error_invalid_args(
  1093. message, "You can specify only Channels in "
  1094. "passive scan");
  1095. goto out;
  1096. } else if (params.freqs && params.freqs[0]) {
  1097. /* wildcard ssid */
  1098. params.num_ssids++;
  1099. wpa_supplicant_trigger_scan(wpa_s, &params);
  1100. } else {
  1101. wpa_s->scan_req = 2;
  1102. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1103. }
  1104. } else if (!os_strcmp(type, "active")) {
  1105. wpa_supplicant_trigger_scan(wpa_s, &params);
  1106. } else {
  1107. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
  1108. "Unknown scan type: %s", type);
  1109. reply = wpas_dbus_error_invalid_args(message,
  1110. "Wrong scan type");
  1111. goto out;
  1112. }
  1113. out:
  1114. for (i = 0; i < WPAS_MAX_SCAN_SSIDS; i++)
  1115. os_free((u8 *) params.ssids[i].ssid);
  1116. os_free((u8 *) params.extra_ies);
  1117. os_free(params.freqs);
  1118. return reply;
  1119. }
  1120. /*
  1121. * wpas_dbus_handler_disconnect - Terminate the current connection
  1122. * @message: Pointer to incoming dbus message
  1123. * @wpa_s: wpa_supplicant structure for a network interface
  1124. * Returns: NotConnected DBus error message if already not connected
  1125. * or NULL otherwise.
  1126. *
  1127. * Handler function for "Disconnect" method call of network interface.
  1128. */
  1129. DBusMessage * wpas_dbus_handler_disconnect(DBusMessage *message,
  1130. struct wpa_supplicant *wpa_s)
  1131. {
  1132. if (wpa_s->current_ssid != NULL) {
  1133. wpa_s->disconnected = 1;
  1134. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1135. return NULL;
  1136. }
  1137. return dbus_message_new_error(message, WPAS_DBUS_ERROR_NOT_CONNECTED,
  1138. "This interface is not connected");
  1139. }
  1140. /**
  1141. * wpas_dbus_new_iface_add_network - Add a new configured network
  1142. * @message: Pointer to incoming dbus message
  1143. * @wpa_s: wpa_supplicant structure for a network interface
  1144. * Returns: A dbus message containing the object path of the new network
  1145. *
  1146. * Handler function for "AddNetwork" method call of a network interface.
  1147. */
  1148. DBusMessage * wpas_dbus_handler_add_network(DBusMessage *message,
  1149. struct wpa_supplicant *wpa_s)
  1150. {
  1151. DBusMessage *reply = NULL;
  1152. DBusMessageIter iter;
  1153. struct wpa_ssid *ssid = NULL;
  1154. char *path = NULL;
  1155. path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  1156. if (path == NULL) {
  1157. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1158. NULL);
  1159. goto err;
  1160. }
  1161. dbus_message_iter_init(message, &iter);
  1162. ssid = wpa_config_add_network(wpa_s->conf);
  1163. if (ssid == NULL) {
  1164. wpa_printf(MSG_ERROR, "wpas_dbus_handler_add_network[dbus]: "
  1165. "can't add new interface.");
  1166. reply = wpas_dbus_error_unknown_error(
  1167. message,
  1168. "wpa_supplicant could not add "
  1169. "a network on this interface.");
  1170. goto err;
  1171. }
  1172. wpas_notify_network_added(wpa_s, ssid);
  1173. ssid->disabled = 1;
  1174. wpa_config_set_network_defaults(ssid);
  1175. reply = set_network_properties(message, ssid, &iter);
  1176. if (reply) {
  1177. wpa_printf(MSG_DEBUG, "wpas_dbus_handler_add_network[dbus]:"
  1178. "control interface couldn't set network "
  1179. "properties");
  1180. goto err;
  1181. }
  1182. /* Construct the object path for this network. */
  1183. os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  1184. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
  1185. wpa_s->dbus_new_path, ssid->id);
  1186. reply = dbus_message_new_method_return(message);
  1187. if (reply == NULL) {
  1188. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1189. NULL);
  1190. goto err;
  1191. }
  1192. if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
  1193. DBUS_TYPE_INVALID)) {
  1194. dbus_message_unref(reply);
  1195. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1196. NULL);
  1197. goto err;
  1198. }
  1199. os_free(path);
  1200. return reply;
  1201. err:
  1202. if (ssid) {
  1203. wpas_notify_network_removed(wpa_s, ssid);
  1204. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1205. }
  1206. os_free(path);
  1207. return reply;
  1208. }
  1209. /**
  1210. * wpas_dbus_handler_remove_network - Remove a configured network
  1211. * @message: Pointer to incoming dbus message
  1212. * @wpa_s: wpa_supplicant structure for a network interface
  1213. * Returns: NULL on success or dbus error on failure
  1214. *
  1215. * Handler function for "RemoveNetwork" method call of a network interface.
  1216. */
  1217. DBusMessage * wpas_dbus_handler_remove_network(DBusMessage *message,
  1218. struct wpa_supplicant *wpa_s)
  1219. {
  1220. DBusMessage *reply = NULL;
  1221. const char *op;
  1222. char *iface = NULL, *net_id = NULL;
  1223. int id;
  1224. struct wpa_ssid *ssid;
  1225. dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
  1226. DBUS_TYPE_INVALID);
  1227. /* Extract the network ID and ensure the network */
  1228. /* is actually a child of this interface */
  1229. iface = wpas_dbus_new_decompose_object_path(op, &net_id, NULL);
  1230. if (iface == NULL || os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1231. reply = wpas_dbus_error_invalid_args(message, op);
  1232. goto out;
  1233. }
  1234. id = strtoul(net_id, NULL, 10);
  1235. if (errno == EINVAL) {
  1236. reply = wpas_dbus_error_invalid_args(message, op);
  1237. goto out;
  1238. }
  1239. ssid = wpa_config_get_network(wpa_s->conf, id);
  1240. if (ssid == NULL) {
  1241. reply = wpas_dbus_error_network_unknown(message);
  1242. goto out;
  1243. }
  1244. wpas_notify_network_removed(wpa_s, ssid);
  1245. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  1246. wpa_printf(MSG_ERROR,
  1247. "wpas_dbus_handler_remove_network[dbus]: "
  1248. "error occurred when removing network %d", id);
  1249. reply = wpas_dbus_error_unknown_error(
  1250. message, "error removing the specified network on "
  1251. "this interface.");
  1252. goto out;
  1253. }
  1254. if (ssid == wpa_s->current_ssid)
  1255. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1256. out:
  1257. os_free(iface);
  1258. os_free(net_id);
  1259. return reply;
  1260. }
  1261. /**
  1262. * wpas_dbus_handler_select_network - Attempt association with a network
  1263. * @message: Pointer to incoming dbus message
  1264. * @wpa_s: wpa_supplicant structure for a network interface
  1265. * Returns: NULL on success or dbus error on failure
  1266. *
  1267. * Handler function for "SelectNetwork" method call of network interface.
  1268. */
  1269. DBusMessage * wpas_dbus_handler_select_network(DBusMessage *message,
  1270. struct wpa_supplicant *wpa_s)
  1271. {
  1272. DBusMessage *reply = NULL;
  1273. const char *op;
  1274. char *iface = NULL, *net_id = NULL;
  1275. int id;
  1276. struct wpa_ssid *ssid;
  1277. dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
  1278. DBUS_TYPE_INVALID);
  1279. /* Extract the network ID and ensure the network */
  1280. /* is actually a child of this interface */
  1281. iface = wpas_dbus_new_decompose_object_path(op, &net_id, NULL);
  1282. if (iface == NULL || os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
  1283. reply = wpas_dbus_error_invalid_args(message, op);
  1284. goto out;
  1285. }
  1286. id = strtoul(net_id, NULL, 10);
  1287. if (errno == EINVAL) {
  1288. reply = wpas_dbus_error_invalid_args(message, op);
  1289. goto out;
  1290. }
  1291. ssid = wpa_config_get_network(wpa_s->conf, id);
  1292. if (ssid == NULL) {
  1293. reply = wpas_dbus_error_network_unknown(message);
  1294. goto out;
  1295. }
  1296. /* Finally, associate with the network */
  1297. wpa_supplicant_select_network(wpa_s, ssid);
  1298. out:
  1299. os_free(iface);
  1300. os_free(net_id);
  1301. return reply;
  1302. }
  1303. /**
  1304. * wpas_dbus_handler_add_blob - Store named binary blob (ie, for certificates)
  1305. * @message: Pointer to incoming dbus message
  1306. * @wpa_s: %wpa_supplicant data structure
  1307. * Returns: A dbus message containing an error on failure or NULL on success
  1308. *
  1309. * Asks wpa_supplicant to internally store a binary blobs.
  1310. */
  1311. DBusMessage * wpas_dbus_handler_add_blob(DBusMessage *message,
  1312. struct wpa_supplicant *wpa_s)
  1313. {
  1314. DBusMessage *reply = NULL;
  1315. DBusMessageIter iter, array_iter;
  1316. char *blob_name;
  1317. u8 *blob_data;
  1318. int blob_len;
  1319. struct wpa_config_blob *blob = NULL;
  1320. dbus_message_iter_init(message, &iter);
  1321. dbus_message_iter_get_basic(&iter, &blob_name);
  1322. if (wpa_config_get_blob(wpa_s->conf, blob_name)) {
  1323. return dbus_message_new_error(message,
  1324. WPAS_DBUS_ERROR_BLOB_EXISTS,
  1325. NULL);
  1326. }
  1327. dbus_message_iter_next(&iter);
  1328. dbus_message_iter_recurse(&iter, &array_iter);
  1329. dbus_message_iter_get_fixed_array(&array_iter, &blob_data, &blob_len);
  1330. blob = os_zalloc(sizeof(*blob));
  1331. if (!blob) {
  1332. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1333. NULL);
  1334. goto err;
  1335. }
  1336. blob->data = os_malloc(blob_len);
  1337. if (!blob->data) {
  1338. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1339. NULL);
  1340. goto err;
  1341. }
  1342. os_memcpy(blob->data, blob_data, blob_len);
  1343. blob->len = blob_len;
  1344. blob->name = os_strdup(blob_name);
  1345. if (!blob->name) {
  1346. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1347. NULL);
  1348. goto err;
  1349. }
  1350. wpa_config_set_blob(wpa_s->conf, blob);
  1351. wpas_notify_blob_added(wpa_s, blob->name);
  1352. return reply;
  1353. err:
  1354. if (blob) {
  1355. os_free(blob->name);
  1356. os_free(blob->data);
  1357. os_free(blob);
  1358. }
  1359. return reply;
  1360. }
  1361. /**
  1362. * wpas_dbus_handler_get_blob - Get named binary blob (ie, for certificates)
  1363. * @message: Pointer to incoming dbus message
  1364. * @wpa_s: %wpa_supplicant data structure
  1365. * Returns: A dbus message containing array of bytes (blob)
  1366. *
  1367. * Gets one wpa_supplicant's binary blobs.
  1368. */
  1369. DBusMessage * wpas_dbus_handler_get_blob(DBusMessage *message,
  1370. struct wpa_supplicant *wpa_s)
  1371. {
  1372. DBusMessage *reply = NULL;
  1373. DBusMessageIter iter, array_iter;
  1374. char *blob_name;
  1375. const struct wpa_config_blob *blob;
  1376. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &blob_name,
  1377. DBUS_TYPE_INVALID);
  1378. blob = wpa_config_get_blob(wpa_s->conf, blob_name);
  1379. if (!blob) {
  1380. return dbus_message_new_error(message,
  1381. WPAS_DBUS_ERROR_BLOB_UNKNOWN,
  1382. "Blob id not set");
  1383. }
  1384. reply = dbus_message_new_method_return(message);
  1385. if (!reply) {
  1386. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1387. NULL);
  1388. goto out;
  1389. }
  1390. dbus_message_iter_init_append(reply, &iter);
  1391. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
  1392. DBUS_TYPE_BYTE_AS_STRING,
  1393. &array_iter)) {
  1394. dbus_message_unref(reply);
  1395. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1396. NULL);
  1397. goto out;
  1398. }
  1399. if (!dbus_message_iter_append_fixed_array(&array_iter, DBUS_TYPE_BYTE,
  1400. &(blob->data), blob->len)) {
  1401. dbus_message_unref(reply);
  1402. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1403. NULL);
  1404. goto out;
  1405. }
  1406. if (!dbus_message_iter_close_container(&iter, &array_iter)) {
  1407. dbus_message_unref(reply);
  1408. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1409. NULL);
  1410. goto out;
  1411. }
  1412. out:
  1413. return reply;
  1414. }
  1415. /**
  1416. * wpas_remove_handler_remove_blob - Remove named binary blob
  1417. * @message: Pointer to incoming dbus message
  1418. * @wpa_s: %wpa_supplicant data structure
  1419. * Returns: NULL on success or dbus error
  1420. *
  1421. * Asks wpa_supplicant to internally remove a binary blobs.
  1422. */
  1423. DBusMessage * wpas_dbus_handler_remove_blob(DBusMessage *message,
  1424. struct wpa_supplicant *wpa_s)
  1425. {
  1426. DBusMessage *reply = NULL;
  1427. char *blob_name;
  1428. dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &blob_name,
  1429. DBUS_TYPE_INVALID);
  1430. if (wpa_config_remove_blob(wpa_s->conf, blob_name)) {
  1431. return dbus_message_new_error(message,
  1432. WPAS_DBUS_ERROR_BLOB_UNKNOWN,
  1433. "Blob id not set");
  1434. }
  1435. wpas_notify_blob_removed(wpa_s, blob_name);
  1436. return reply;
  1437. }
  1438. /**
  1439. * wpas_dbus_getter_capabilities - Return interface capabilities
  1440. * @message: Pointer to incoming dbus message
  1441. * @wpa_s: wpa_supplicant structure for a network interface
  1442. * Returns: A dbus message containing a dict of strings
  1443. *
  1444. * Getter for "Capabilities" property of an interface.
  1445. */
  1446. DBusMessage * wpas_dbus_getter_capabilities(DBusMessage *message,
  1447. struct wpa_supplicant *wpa_s)
  1448. {
  1449. DBusMessage *reply = NULL;
  1450. struct wpa_driver_capa capa;
  1451. int res;
  1452. DBusMessageIter iter, iter_dict;
  1453. DBusMessageIter iter_dict_entry, iter_dict_val, iter_array,
  1454. variant_iter;
  1455. const char *scans[] = { "active", "passive", "ssid" };
  1456. const char *modes[] = { "infrastructure", "ad-hoc", "ap" };
  1457. int n = sizeof(modes) / sizeof(char *);
  1458. if (message == NULL)
  1459. reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
  1460. else
  1461. reply = dbus_message_new_method_return(message);
  1462. if (!reply)
  1463. goto nomem;
  1464. dbus_message_iter_init_append(reply, &iter);
  1465. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
  1466. "a{sv}", &variant_iter))
  1467. goto nomem;
  1468. if (!wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
  1469. goto nomem;
  1470. res = wpa_drv_get_capa(wpa_s, &capa);
  1471. /***** pairwise cipher */
  1472. if (res < 0) {
  1473. const char *args[] = {"ccmp", "tkip", "none"};
  1474. if (!wpa_dbus_dict_append_string_array(
  1475. &iter_dict, "Pairwise", args,
  1476. sizeof(args) / sizeof(char*)))
  1477. goto nomem;
  1478. } else {
  1479. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Pairwise",
  1480. &iter_dict_entry,
  1481. &iter_dict_val,
  1482. &iter_array))
  1483. goto nomem;
  1484. if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1485. if (!wpa_dbus_dict_string_array_add_element(
  1486. &iter_array, "ccmp"))
  1487. goto nomem;
  1488. }
  1489. if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1490. if (!wpa_dbus_dict_string_array_add_element(
  1491. &iter_array, "tkip"))
  1492. goto nomem;
  1493. }
  1494. if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1495. if (!wpa_dbus_dict_string_array_add_element(
  1496. &iter_array, "none"))
  1497. goto nomem;
  1498. }
  1499. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  1500. &iter_dict_entry,
  1501. &iter_dict_val,
  1502. &iter_array))
  1503. goto nomem;
  1504. }
  1505. /***** group cipher */
  1506. if (res < 0) {
  1507. const char *args[] = {
  1508. "ccmp", "tkip", "wep104", "wep40"
  1509. };
  1510. if (!wpa_dbus_dict_append_string_array(
  1511. &iter_dict, "Group", args,
  1512. sizeof(args) / sizeof(char*)))
  1513. goto nomem;
  1514. } else {
  1515. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Group",
  1516. &iter_dict_entry,
  1517. &iter_dict_val,
  1518. &iter_array))
  1519. goto nomem;
  1520. if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1521. if (!wpa_dbus_dict_string_array_add_element(
  1522. &iter_array, "ccmp"))
  1523. goto nomem;
  1524. }
  1525. if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1526. if (!wpa_dbus_dict_string_array_add_element(
  1527. &iter_array, "tkip"))
  1528. goto nomem;
  1529. }
  1530. if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  1531. if (!wpa_dbus_dict_string_array_add_element(
  1532. &iter_array, "wep104"))
  1533. goto nomem;
  1534. }
  1535. if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  1536. if (!wpa_dbus_dict_string_array_add_element(
  1537. &iter_array, "wep40"))
  1538. goto nomem;
  1539. }
  1540. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  1541. &iter_dict_entry,
  1542. &iter_dict_val,
  1543. &iter_array))
  1544. goto nomem;
  1545. }
  1546. /***** key management */
  1547. if (res < 0) {
  1548. const char *args[] = {
  1549. "wpa-psk", "wpa-eap", "ieee8021x", "wpa-none",
  1550. #ifdef CONFIG_WPS
  1551. "wps",
  1552. #endif /* CONFIG_WPS */
  1553. "none"
  1554. };
  1555. if (!wpa_dbus_dict_append_string_array(
  1556. &iter_dict, "KeyMgmt", args,
  1557. sizeof(args) / sizeof(char*)))
  1558. goto nomem;
  1559. } else {
  1560. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "KeyMgmt",
  1561. &iter_dict_entry,
  1562. &iter_dict_val,
  1563. &iter_array))
  1564. goto nomem;
  1565. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  1566. "none"))
  1567. goto nomem;
  1568. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  1569. "ieee8021x"))
  1570. goto nomem;
  1571. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1572. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  1573. if (!wpa_dbus_dict_string_array_add_element(
  1574. &iter_array, "wpa-eap"))
  1575. goto nomem;
  1576. }
  1577. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  1578. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1579. if (!wpa_dbus_dict_string_array_add_element(
  1580. &iter_array, "wpa-psk"))
  1581. goto nomem;
  1582. }
  1583. if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1584. if (!wpa_dbus_dict_string_array_add_element(
  1585. &iter_array, "wpa-none"))
  1586. goto nomem;
  1587. }
  1588. #ifdef CONFIG_WPS
  1589. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  1590. "wps"))
  1591. goto nomem;
  1592. #endif /* CONFIG_WPS */
  1593. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  1594. &iter_dict_entry,
  1595. &iter_dict_val,
  1596. &iter_array))
  1597. goto nomem;
  1598. }
  1599. /***** WPA protocol */
  1600. if (res < 0) {
  1601. const char *args[] = { "rsn", "wpa" };
  1602. if (!wpa_dbus_dict_append_string_array(
  1603. &iter_dict, "Protocol", args,
  1604. sizeof(args) / sizeof(char*)))
  1605. goto nomem;
  1606. } else {
  1607. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Protocol",
  1608. &iter_dict_entry,
  1609. &iter_dict_val,
  1610. &iter_array))
  1611. goto nomem;
  1612. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  1613. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1614. if (!wpa_dbus_dict_string_array_add_element(
  1615. &iter_array, "rsn"))
  1616. goto nomem;
  1617. }
  1618. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1619. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  1620. if (!wpa_dbus_dict_string_array_add_element(
  1621. &iter_array, "wpa"))
  1622. goto nomem;
  1623. }
  1624. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  1625. &iter_dict_entry,
  1626. &iter_dict_val,
  1627. &iter_array))
  1628. goto nomem;
  1629. }
  1630. /***** auth alg */
  1631. if (res < 0) {
  1632. const char *args[] = { "open", "shared", "leap" };
  1633. if (!wpa_dbus_dict_append_string_array(
  1634. &iter_dict, "AuthAlg", args,
  1635. sizeof(args) / sizeof(char*)))
  1636. goto nomem;
  1637. } else {
  1638. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "AuthAlg",
  1639. &iter_dict_entry,
  1640. &iter_dict_val,
  1641. &iter_array))
  1642. goto nomem;
  1643. if (capa.auth & (WPA_DRIVER_AUTH_OPEN)) {
  1644. if (!wpa_dbus_dict_string_array_add_element(
  1645. &iter_array, "open"))
  1646. goto nomem;
  1647. }
  1648. if (capa.auth & (WPA_DRIVER_AUTH_SHARED)) {
  1649. if (!wpa_dbus_dict_string_array_add_element(
  1650. &iter_array, "shared"))
  1651. goto nomem;
  1652. }
  1653. if (capa.auth & (WPA_DRIVER_AUTH_LEAP)) {
  1654. if (!wpa_dbus_dict_string_array_add_element(
  1655. &iter_array, "leap"))
  1656. goto nomem;
  1657. }
  1658. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  1659. &iter_dict_entry,
  1660. &iter_dict_val,
  1661. &iter_array))
  1662. goto nomem;
  1663. }
  1664. /***** Scan */
  1665. if (!wpa_dbus_dict_append_string_array(&iter_dict, "Scan", scans,
  1666. sizeof(scans) / sizeof(char *)))
  1667. goto nomem;
  1668. /***** Modes */
  1669. if (res < 0 || !(capa.flags & WPA_DRIVER_FLAGS_AP))
  1670. n--; /* exclude ap mode if it is not supported by the driver */
  1671. if (!wpa_dbus_dict_append_string_array(&iter_dict, "Modes", modes, n))
  1672. goto nomem;
  1673. if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict))
  1674. goto nomem;
  1675. if (!dbus_message_iter_close_container(&iter, &variant_iter))
  1676. goto nomem;
  1677. return reply;
  1678. nomem:
  1679. if (reply)
  1680. dbus_message_unref(reply);
  1681. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY, NULL);
  1682. }
  1683. /**
  1684. * wpas_dbus_getter_state - Get interface state
  1685. * @message: Pointer to incoming dbus message
  1686. * @wpa_s: wpa_supplicant structure for a network interface
  1687. * Returns: A dbus message containing a STRING representing the current
  1688. * interface state
  1689. *
  1690. * Getter for "State" property.
  1691. */
  1692. DBusMessage * wpas_dbus_getter_state(DBusMessage *message,
  1693. struct wpa_supplicant *wpa_s)
  1694. {
  1695. DBusMessage *reply = NULL;
  1696. const char *str_state;
  1697. char *state_ls, *tmp;
  1698. str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
  1699. /* make state string lowercase to fit new DBus API convention
  1700. */
  1701. state_ls = tmp = os_strdup(str_state);
  1702. if (!tmp) {
  1703. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1704. NULL);
  1705. }
  1706. while (*tmp) {
  1707. *tmp = tolower(*tmp);
  1708. tmp++;
  1709. }
  1710. reply = wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
  1711. &state_ls);
  1712. os_free(state_ls);
  1713. return reply;
  1714. }
  1715. /**
  1716. * wpas_dbus_new_iface_get_scanning - Get interface scanning state
  1717. * @message: Pointer to incoming dbus message
  1718. * @wpa_s: wpa_supplicant structure for a network interface
  1719. * Returns: A dbus message containing whether the interface is scanning
  1720. *
  1721. * Getter for "scanning" property.
  1722. */
  1723. DBusMessage * wpas_dbus_getter_scanning(DBusMessage *message,
  1724. struct wpa_supplicant *wpa_s)
  1725. {
  1726. dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
  1727. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
  1728. &scanning);
  1729. }
  1730. /**
  1731. * wpas_dbus_getter_ap_scan - Control roaming mode
  1732. * @message: Pointer to incoming dbus message
  1733. * @wpa_s: wpa_supplicant structure for a network interface
  1734. * Returns: A message containong value of ap_scan variable
  1735. *
  1736. * Getter function for "ApScan" property.
  1737. */
  1738. DBusMessage * wpas_dbus_getter_ap_scan(DBusMessage *message,
  1739. struct wpa_supplicant *wpa_s)
  1740. {
  1741. dbus_uint32_t ap_scan = wpa_s->conf->ap_scan;
  1742. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_UINT32,
  1743. &ap_scan);
  1744. }
  1745. /**
  1746. * wpas_dbus_setter_ap_scan - Control roaming mode
  1747. * @message: Pointer to incoming dbus message
  1748. * @wpa_s: wpa_supplicant structure for a network interface
  1749. * Returns: NULL
  1750. *
  1751. * Setter function for "ApScan" property.
  1752. */
  1753. DBusMessage * wpas_dbus_setter_ap_scan(DBusMessage *message,
  1754. struct wpa_supplicant *wpa_s)
  1755. {
  1756. DBusMessage *reply = NULL;
  1757. dbus_uint32_t ap_scan;
  1758. reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_UINT32,
  1759. &ap_scan);
  1760. if (reply)
  1761. return reply;
  1762. if (wpa_supplicant_set_ap_scan(wpa_s, ap_scan)) {
  1763. return wpas_dbus_error_invalid_args(
  1764. message, "ap_scan must equal 0, 1 or 2");
  1765. }
  1766. return NULL;
  1767. }
  1768. /**
  1769. * wpas_dbus_getter_ifname - Get interface name
  1770. * @message: Pointer to incoming dbus message
  1771. * @wpa_s: wpa_supplicant structure for a network interface
  1772. * Returns: A dbus message containing a name of network interface
  1773. * associated with with wpa_s
  1774. *
  1775. * Getter for "Ifname" property.
  1776. */
  1777. DBusMessage * wpas_dbus_getter_ifname(DBusMessage *message,
  1778. struct wpa_supplicant *wpa_s)
  1779. {
  1780. const char *ifname = wpa_s->ifname;
  1781. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
  1782. &ifname);
  1783. }
  1784. /**
  1785. * wpas_dbus_getter_driver - Get interface name
  1786. * @message: Pointer to incoming dbus message
  1787. * @wpa_s: wpa_supplicant structure for a network interface
  1788. * Returns: A dbus message containing a name of network interface
  1789. * driver associated with with wpa_s
  1790. *
  1791. * Getter for "Driver" property.
  1792. */
  1793. DBusMessage * wpas_dbus_getter_driver(DBusMessage *message,
  1794. struct wpa_supplicant *wpa_s)
  1795. {
  1796. const char *driver;
  1797. if (wpa_s->driver == NULL || wpa_s->driver->name == NULL) {
  1798. wpa_printf(MSG_DEBUG, "wpas_dbus_getter_driver[dbus]: "
  1799. "wpa_s has no driver set");
  1800. return wpas_dbus_error_unknown_error(message, NULL);
  1801. }
  1802. driver = wpa_s->driver->name;
  1803. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
  1804. &driver);
  1805. }
  1806. /**
  1807. * wpas_dbus_getter_current_bss - Get current bss object path
  1808. * @message: Pointer to incoming dbus message
  1809. * @wpa_s: wpa_supplicant structure for a network interface
  1810. * Returns: A dbus message containing a DBus object path to
  1811. * current BSS
  1812. *
  1813. * Getter for "CurrentBSS" property.
  1814. */
  1815. DBusMessage * wpas_dbus_getter_current_bss(DBusMessage *message,
  1816. struct wpa_supplicant *wpa_s)
  1817. {
  1818. DBusMessage *reply = NULL;
  1819. char *bss_obj_path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  1820. struct wpa_bss *bss = NULL;
  1821. if (bss_obj_path == NULL)
  1822. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1823. NULL);
  1824. /* TODO: store current BSS or BSS id in wpa_s */
  1825. if (!is_zero_ether_addr(wpa_s->bssid))
  1826. bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
  1827. if (bss)
  1828. os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  1829. "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
  1830. wpa_s->dbus_new_path, bss->id);
  1831. else
  1832. os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
  1833. reply = wpas_dbus_simple_property_getter(message,
  1834. DBUS_TYPE_OBJECT_PATH,
  1835. &bss_obj_path);
  1836. os_free(bss_obj_path);
  1837. return reply;
  1838. }
  1839. /**
  1840. * wpas_dbus_getter_current_network - Get current network object path
  1841. * @message: Pointer to incoming dbus message
  1842. * @wpa_s: wpa_supplicant structure for a network interface
  1843. * Returns: A dbus message containing a DBus object path to
  1844. * current network
  1845. *
  1846. * Getter for "CurrentNetwork" property.
  1847. */
  1848. DBusMessage * wpas_dbus_getter_current_network(DBusMessage *message,
  1849. struct wpa_supplicant *wpa_s)
  1850. {
  1851. DBusMessage *reply = NULL;
  1852. char *net_obj_path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  1853. if (net_obj_path == NULL)
  1854. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1855. NULL);
  1856. if (wpa_s->current_ssid)
  1857. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  1858. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
  1859. wpa_s->dbus_new_path, wpa_s->current_ssid->id);
  1860. else
  1861. os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
  1862. reply = wpas_dbus_simple_property_getter(message,
  1863. DBUS_TYPE_OBJECT_PATH,
  1864. &net_obj_path);
  1865. os_free(net_obj_path);
  1866. return reply;
  1867. }
  1868. /**
  1869. * wpas_dbus_getter_bridge_ifname - Get interface name
  1870. * @message: Pointer to incoming dbus message
  1871. * @wpa_s: wpa_supplicant structure for a network interface
  1872. * Returns: A dbus message containing a name of bridge network
  1873. * interface associated with with wpa_s
  1874. *
  1875. * Getter for "BridgeIfname" property.
  1876. */
  1877. DBusMessage * wpas_dbus_getter_bridge_ifname(DBusMessage *message,
  1878. struct wpa_supplicant *wpa_s)
  1879. {
  1880. const char *bridge_ifname = NULL;
  1881. bridge_ifname = wpa_s->bridge_ifname;
  1882. if (bridge_ifname == NULL) {
  1883. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bridge_ifname[dbus]: "
  1884. "wpa_s has no bridge interface name set");
  1885. return wpas_dbus_error_unknown_error(message, NULL);
  1886. }
  1887. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
  1888. &bridge_ifname);
  1889. }
  1890. /**
  1891. * wpas_dbus_getter_bsss - Get array of BSSs objects
  1892. * @message: Pointer to incoming dbus message
  1893. * @wpa_s: wpa_supplicant structure for a network interface
  1894. * Returns: a dbus message containing an array of all known BSS objects
  1895. * dbus paths
  1896. *
  1897. * Getter for "BSSs" property.
  1898. */
  1899. DBusMessage * wpas_dbus_getter_bsss(DBusMessage *message,
  1900. struct wpa_supplicant *wpa_s)
  1901. {
  1902. DBusMessage *reply = NULL;
  1903. struct wpa_bss *bss;
  1904. char **paths;
  1905. unsigned int i = 0;
  1906. paths = os_zalloc(wpa_s->num_bss * sizeof(char *));
  1907. if (!paths) {
  1908. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1909. NULL);
  1910. }
  1911. /* Loop through scan results and append each result's object path */
  1912. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  1913. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  1914. if (paths[i] == NULL) {
  1915. reply = dbus_message_new_error(message,
  1916. DBUS_ERROR_NO_MEMORY,
  1917. NULL);
  1918. goto out;
  1919. }
  1920. /* Construct the object path for this BSS. */
  1921. os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
  1922. "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
  1923. wpa_s->dbus_new_path, bss->id);
  1924. }
  1925. reply = wpas_dbus_simple_array_property_getter(message,
  1926. DBUS_TYPE_OBJECT_PATH,
  1927. paths, wpa_s->num_bss);
  1928. out:
  1929. while(i)
  1930. os_free(paths[--i]);
  1931. os_free(paths);
  1932. return reply;
  1933. }
  1934. /**
  1935. * wpas_dbus_getter_networks - Get array of networks objects
  1936. * @message: Pointer to incoming dbus message
  1937. * @wpa_s: wpa_supplicant structure for a network interface
  1938. * Returns: a dbus message containing an array of all configured
  1939. * networks dbus object paths.
  1940. *
  1941. * Getter for "Networks" property.
  1942. */
  1943. DBusMessage * wpas_dbus_getter_networks(DBusMessage *message,
  1944. struct wpa_supplicant *wpa_s)
  1945. {
  1946. DBusMessage *reply = NULL;
  1947. struct wpa_ssid *ssid;
  1948. char **paths;
  1949. unsigned int i = 0, num = 0;
  1950. if (wpa_s->conf == NULL) {
  1951. wpa_printf(MSG_ERROR, "wpas_dbus_getter_networks[dbus]: "
  1952. "An error occurred getting networks list.");
  1953. return wpas_dbus_error_unknown_error(message, NULL);
  1954. }
  1955. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  1956. num++;
  1957. paths = os_zalloc(num * sizeof(char *));
  1958. if (!paths) {
  1959. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  1960. NULL);
  1961. }
  1962. /* Loop through configured networks and append object path of each */
  1963. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1964. paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  1965. if (paths[i] == NULL) {
  1966. reply = dbus_message_new_error(message,
  1967. DBUS_ERROR_NO_MEMORY,
  1968. NULL);
  1969. goto out;
  1970. }
  1971. /* Construct the object path for this network. */
  1972. os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
  1973. "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
  1974. wpa_s->dbus_new_path, ssid->id);
  1975. }
  1976. reply = wpas_dbus_simple_array_property_getter(message,
  1977. DBUS_TYPE_OBJECT_PATH,
  1978. paths, num);
  1979. out:
  1980. while (i)
  1981. os_free(paths[--i]);
  1982. os_free(paths);
  1983. return reply;
  1984. }
  1985. /**
  1986. * wpas_dbus_getter_blobs - Get all blobs defined for this interface
  1987. * @message: Pointer to incoming dbus message
  1988. * @wpa_s: wpa_supplicant structure for a network interface
  1989. * Returns: a dbus message containing a dictionary of pairs (blob_name, blob)
  1990. *
  1991. * Getter for "Blobs" property.
  1992. */
  1993. DBusMessage * wpas_dbus_getter_blobs(DBusMessage *message,
  1994. struct wpa_supplicant *wpa_s)
  1995. {
  1996. DBusMessage *reply = NULL;
  1997. DBusMessageIter iter, variant_iter, dict_iter, entry_iter, array_iter;
  1998. struct wpa_config_blob *blob;
  1999. if (message == NULL)
  2000. reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
  2001. else
  2002. reply = dbus_message_new_method_return(message);
  2003. if (!reply)
  2004. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2005. NULL);
  2006. dbus_message_iter_init_append(reply, &iter);
  2007. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
  2008. "a{say}", &variant_iter) ||
  2009. !dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
  2010. "{say}", &dict_iter)) {
  2011. dbus_message_unref(reply);
  2012. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2013. NULL);
  2014. }
  2015. blob = wpa_s->conf->blobs;
  2016. while (blob) {
  2017. if (!dbus_message_iter_open_container(&dict_iter,
  2018. DBUS_TYPE_DICT_ENTRY,
  2019. NULL, &entry_iter) ||
  2020. !dbus_message_iter_append_basic(&entry_iter,
  2021. DBUS_TYPE_STRING,
  2022. &(blob->name)) ||
  2023. !dbus_message_iter_open_container(&entry_iter,
  2024. DBUS_TYPE_ARRAY,
  2025. DBUS_TYPE_BYTE_AS_STRING,
  2026. &array_iter) ||
  2027. !dbus_message_iter_append_fixed_array(&array_iter,
  2028. DBUS_TYPE_BYTE,
  2029. &(blob->data),
  2030. blob->len) ||
  2031. !dbus_message_iter_close_container(&entry_iter,
  2032. &array_iter) ||
  2033. !dbus_message_iter_close_container(&dict_iter,
  2034. &entry_iter)) {
  2035. dbus_message_unref(reply);
  2036. return dbus_message_new_error(message,
  2037. DBUS_ERROR_NO_MEMORY,
  2038. NULL);
  2039. }
  2040. blob = blob->next;
  2041. }
  2042. if (!dbus_message_iter_close_container(&variant_iter, &dict_iter) ||
  2043. !dbus_message_iter_close_container(&iter, &variant_iter)) {
  2044. dbus_message_unref(reply);
  2045. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2046. NULL);
  2047. }
  2048. return reply;
  2049. }
  2050. /**
  2051. * wpas_dbus_getter_bss_bssid - Return the BSSID of a BSS
  2052. * @message: Pointer to incoming dbus message
  2053. * @bss: a pair of interface describing structure and bss's id
  2054. * Returns: a dbus message containing the bssid for the requested bss
  2055. *
  2056. * Getter for "BSSID" property.
  2057. */
  2058. DBusMessage * wpas_dbus_getter_bss_bssid(DBusMessage *message,
  2059. struct bss_handler_args *bss)
  2060. {
  2061. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2062. if (!res) {
  2063. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_bssid[dbus]: no "
  2064. "bss with id %d found", bss->id);
  2065. return NULL;
  2066. }
  2067. return wpas_dbus_simple_array_property_getter(message, DBUS_TYPE_BYTE,
  2068. res->bssid, ETH_ALEN);
  2069. }
  2070. /**
  2071. * wpas_dbus_getter_bss_ssid - Return the SSID of a BSS
  2072. * @message: Pointer to incoming dbus message
  2073. * @bss: a pair of interface describing structure and bss's id
  2074. * Returns: a dbus message containing the ssid for the requested bss
  2075. *
  2076. * Getter for "SSID" property.
  2077. */
  2078. DBusMessage * wpas_dbus_getter_bss_ssid(DBusMessage *message,
  2079. struct bss_handler_args *bss)
  2080. {
  2081. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2082. if (!res) {
  2083. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_ssid[dbus]: no "
  2084. "bss with id %d found", bss->id);
  2085. return NULL;
  2086. }
  2087. return wpas_dbus_simple_array_property_getter(message, DBUS_TYPE_BYTE,
  2088. res->ssid,
  2089. res->ssid_len);
  2090. }
  2091. /**
  2092. * wpas_dbus_getter_bss_privacy - Return the privacy flag of a BSS
  2093. * @message: Pointer to incoming dbus message
  2094. * @bss: a pair of interface describing structure and bss's id
  2095. * Returns: a dbus message containing the privacy flag value of requested bss
  2096. *
  2097. * Getter for "Privacy" property.
  2098. */
  2099. DBusMessage * wpas_dbus_getter_bss_privacy(DBusMessage *message,
  2100. struct bss_handler_args *bss)
  2101. {
  2102. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2103. dbus_bool_t privacy;
  2104. if (!res) {
  2105. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_privacy[dbus]: no "
  2106. "bss with id %d found", bss->id);
  2107. return NULL;
  2108. }
  2109. privacy = res->caps && IEEE80211_CAP_PRIVACY ? TRUE : FALSE;
  2110. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
  2111. &privacy);
  2112. }
  2113. /**
  2114. * wpas_dbus_getter_bss_mode - Return the mode of a BSS
  2115. * @message: Pointer to incoming dbus message
  2116. * @bss: a pair of interface describing structure and bss's id
  2117. * Returns: a dbus message containing the mode of requested bss
  2118. *
  2119. * Getter for "Mode" property.
  2120. */
  2121. DBusMessage * wpas_dbus_getter_bss_mode(DBusMessage *message,
  2122. struct bss_handler_args *bss)
  2123. {
  2124. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2125. const char *mode;
  2126. if (!res) {
  2127. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_mode[dbus]: no "
  2128. "bss with id %d found", bss->id);
  2129. return NULL;
  2130. }
  2131. if (res->caps & IEEE80211_CAP_IBSS)
  2132. mode = "ad-hoc";
  2133. else
  2134. mode = "infrastructure";
  2135. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
  2136. &mode);
  2137. }
  2138. /**
  2139. * wpas_dbus_getter_bss_level - Return the signal strength of a BSS
  2140. * @message: Pointer to incoming dbus message
  2141. * @bss: a pair of interface describing structure and bss's id
  2142. * Returns: a dbus message containing the signal strength of requested bss
  2143. *
  2144. * Getter for "Level" property.
  2145. */
  2146. DBusMessage * wpas_dbus_getter_bss_signal(DBusMessage *message,
  2147. struct bss_handler_args *bss)
  2148. {
  2149. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2150. if (!res) {
  2151. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_signal[dbus]: no "
  2152. "bss with id %d found", bss->id);
  2153. return NULL;
  2154. }
  2155. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_INT16,
  2156. &res->level);
  2157. }
  2158. /**
  2159. * wpas_dbus_getter_bss_frequency - Return the frequency of a BSS
  2160. * @message: Pointer to incoming dbus message
  2161. * @bss: a pair of interface describing structure and bss's id
  2162. * Returns: a dbus message containing the frequency of requested bss
  2163. *
  2164. * Getter for "Frequency" property.
  2165. */
  2166. DBusMessage * wpas_dbus_getter_bss_frequency(DBusMessage *message,
  2167. struct bss_handler_args *bss)
  2168. {
  2169. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2170. if (!res) {
  2171. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_frequency[dbus]: "
  2172. "no bss with id %d found", bss->id);
  2173. return NULL;
  2174. }
  2175. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_UINT16,
  2176. &res->freq);
  2177. }
  2178. /**
  2179. * wpas_dbus_getter_bss_max_rate - Return the maximal rate of a BSS
  2180. * @message: Pointer to incoming dbus message
  2181. * @bss: a pair of interface describing structure and bss's id
  2182. * Returns: a dbus message containing the maximal data rate of requested bss
  2183. *
  2184. * Getter for "MaxRate" property.
  2185. */
  2186. DBusMessage * wpas_dbus_getter_bss_max_rate(DBusMessage *message,
  2187. struct bss_handler_args *bss)
  2188. {
  2189. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2190. int max_rate;
  2191. if (!res) {
  2192. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_max_rate[dbus]: "
  2193. "no bss with id %d found", bss->id);
  2194. return NULL;
  2195. }
  2196. max_rate = wpa_bss_get_max_rate(res);
  2197. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_UINT16,
  2198. &max_rate);
  2199. }
  2200. /**
  2201. * wpas_dbus_getter_bss_wpaie - Return the WPA IE of a BSS
  2202. * @message: Pointer to incoming dbus message
  2203. * @bss: a pair of interface describing structure and bss's id
  2204. * Returns: a dbus message containing the WPA information elements
  2205. * of requested bss
  2206. *
  2207. * Getter for "WPAIE" property.
  2208. */
  2209. DBusMessage * wpas_dbus_getter_bss_wpaie(DBusMessage *message,
  2210. struct bss_handler_args *bss)
  2211. {
  2212. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2213. const u8 *ie;
  2214. if (!res) {
  2215. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_wpaie[dbus]: no "
  2216. "bss with id %d found", bss->id);
  2217. return NULL;
  2218. }
  2219. ie = wpa_bss_get_vendor_ie(res, WPA_IE_VENDOR_TYPE);
  2220. if (!ie)
  2221. return NULL;
  2222. return wpas_dbus_simple_array_property_getter(message, DBUS_TYPE_BYTE,
  2223. ie, ie[1] + 2);
  2224. }
  2225. /**
  2226. * wpas_dbus_getter_bss_rsnie - Return the RSN IE of a BSS
  2227. * @message: Pointer to incoming dbus message
  2228. * @bss: a pair of interface describing structure and bss's id
  2229. * Returns: a dbus message containing the RSN information elements
  2230. * of requested bss
  2231. *
  2232. * Getter for "RSNIE" property.
  2233. */
  2234. DBusMessage * wpas_dbus_getter_bss_rsnie(DBusMessage *message,
  2235. struct bss_handler_args *bss)
  2236. {
  2237. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2238. const u8 *ie;
  2239. if (!res) {
  2240. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_rsnie[dbus]: no "
  2241. "bss with id %d found", bss->id);
  2242. return NULL;
  2243. }
  2244. ie = wpa_bss_get_ie(res, WLAN_EID_RSN);
  2245. if (!ie)
  2246. return NULL;
  2247. return wpas_dbus_simple_array_property_getter(message, DBUS_TYPE_BYTE,
  2248. ie, ie[1] + 2);
  2249. }
  2250. /**
  2251. * wpas_dbus_getter_bss_wpsie - Return the WPS IE of a BSS
  2252. * @message: Pointer to incoming dbus message
  2253. * @bss: a pair of interface describing structure and bss's id
  2254. * Returns: a dbus message containing the WPS information elements
  2255. * of requested bss
  2256. *
  2257. * Getter for "WPSIE" property.
  2258. */
  2259. DBusMessage * wpas_dbus_getter_bss_wpsie(DBusMessage *message,
  2260. struct bss_handler_args *bss)
  2261. {
  2262. struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
  2263. const u8 *ie;
  2264. if (!res) {
  2265. wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_wpsie[dbus]: no "
  2266. "bss with id %d found", bss->id);
  2267. return NULL;
  2268. }
  2269. ie = wpa_bss_get_vendor_ie(res, WPS_IE_VENDOR_TYPE);
  2270. if (!ie)
  2271. return NULL;
  2272. return wpas_dbus_simple_array_property_getter(message, DBUS_TYPE_BYTE,
  2273. ie, ie[1] + 2);
  2274. }
  2275. /**
  2276. * wpas_dbus_getter_enabled - Check whether network is enabled or disabled
  2277. * @message: Pointer to incoming dbus message
  2278. * @wpas_dbus_setter_enabled: wpa_supplicant structure for a network interface
  2279. * and wpa_ssid structure for a configured network
  2280. * Returns: DBus message with boolean indicating state of configured network
  2281. * or DBus error on failure
  2282. *
  2283. * Getter for "enabled" property of a configured network.
  2284. */
  2285. DBusMessage * wpas_dbus_getter_enabled(DBusMessage *message,
  2286. struct network_handler_args *net)
  2287. {
  2288. dbus_bool_t enabled = net->ssid->disabled ? FALSE : TRUE;
  2289. return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
  2290. &enabled);
  2291. }
  2292. /**
  2293. * wpas_dbus_setter_enabled - Mark a configured network as enabled or disabled
  2294. * @message: Pointer to incoming dbus message
  2295. * @wpas_dbus_setter_enabled: wpa_supplicant structure for a network interface
  2296. * and wpa_ssid structure for a configured network
  2297. * Returns: NULL indicating success or DBus error on failure
  2298. *
  2299. * Setter for "Enabled" property of a configured network.
  2300. */
  2301. DBusMessage * wpas_dbus_setter_enabled(DBusMessage *message,
  2302. struct network_handler_args *net)
  2303. {
  2304. DBusMessage *reply = NULL;
  2305. struct wpa_supplicant *wpa_s;
  2306. struct wpa_ssid *ssid;
  2307. dbus_bool_t enable;
  2308. reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_BOOLEAN,
  2309. &enable);
  2310. if (reply)
  2311. return reply;
  2312. wpa_s = net->wpa_s;
  2313. ssid = net->ssid;
  2314. if (enable)
  2315. wpa_supplicant_enable_network(wpa_s, ssid);
  2316. else
  2317. wpa_supplicant_disable_network(wpa_s, ssid);
  2318. return NULL;
  2319. }
  2320. /**
  2321. * wpas_dbus_getter_network_properties - Get options for a configured network
  2322. * @message: Pointer to incoming dbus message
  2323. * @net: wpa_supplicant structure for a network interface and
  2324. * wpa_ssid structure for a configured network
  2325. * Returns: DBus message with network properties or DBus error on failure
  2326. *
  2327. * Getter for "Properties" property of a configured network.
  2328. */
  2329. DBusMessage * wpas_dbus_getter_network_properties(
  2330. DBusMessage *message, struct network_handler_args *net)
  2331. {
  2332. DBusMessage *reply = NULL;
  2333. DBusMessageIter iter, variant_iter, dict_iter;
  2334. char **iterator;
  2335. char **props = wpa_config_get_all(net->ssid, 0);
  2336. if (!props)
  2337. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2338. NULL);
  2339. if (message == NULL)
  2340. reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
  2341. else
  2342. reply = dbus_message_new_method_return(message);
  2343. if (!reply) {
  2344. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2345. NULL);
  2346. goto out;
  2347. }
  2348. dbus_message_iter_init_append(reply, &iter);
  2349. if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
  2350. "a{sv}", &variant_iter) ||
  2351. !wpa_dbus_dict_open_write(&variant_iter, &dict_iter)) {
  2352. dbus_message_unref(reply);
  2353. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2354. NULL);
  2355. goto out;
  2356. }
  2357. iterator = props;
  2358. while (*iterator) {
  2359. if (!wpa_dbus_dict_append_string(&dict_iter, *iterator,
  2360. *(iterator + 1))) {
  2361. dbus_message_unref(reply);
  2362. reply = dbus_message_new_error(message,
  2363. DBUS_ERROR_NO_MEMORY,
  2364. NULL);
  2365. goto out;
  2366. }
  2367. iterator += 2;
  2368. }
  2369. if (!wpa_dbus_dict_close_write(&variant_iter, &dict_iter) ||
  2370. !dbus_message_iter_close_container(&iter, &variant_iter)) {
  2371. dbus_message_unref(reply);
  2372. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  2373. NULL);
  2374. goto out;
  2375. }
  2376. out:
  2377. iterator = props;
  2378. while (*iterator) {
  2379. os_free(*iterator);
  2380. iterator++;
  2381. }
  2382. os_free(props);
  2383. return reply;
  2384. }
  2385. /**
  2386. * wpas_dbus_setter_network_properties - Set options for a configured network
  2387. * @message: Pointer to incoming dbus message
  2388. * @net: wpa_supplicant structure for a network interface and
  2389. * wpa_ssid structure for a configured network
  2390. * Returns: NULL indicating success or DBus error on failure
  2391. *
  2392. * Setter for "Properties" property of a configured network.
  2393. */
  2394. DBusMessage * wpas_dbus_setter_network_properties(
  2395. DBusMessage *message, struct network_handler_args *net)
  2396. {
  2397. struct wpa_ssid *ssid = net->ssid;
  2398. DBusMessage *reply = NULL;
  2399. DBusMessageIter iter, variant_iter;
  2400. dbus_message_iter_init(message, &iter);
  2401. dbus_message_iter_next(&iter);
  2402. dbus_message_iter_next(&iter);
  2403. dbus_message_iter_recurse(&iter, &variant_iter);
  2404. reply = set_network_properties(message, ssid, &variant_iter);
  2405. if (reply)
  2406. wpa_printf(MSG_DEBUG, "dbus control interface couldn't set "
  2407. "network properties");
  2408. return reply;
  2409. }