dbus_new_handlers.c 86 KB

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