dbus_new_handlers.c 86 KB

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