dbus_new_handlers.c 88 KB

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