dbus_new_handlers.c 84 KB

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