dbus_new_handlers.c 88 KB

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