ctrl_iface_dbus_new_handlers.c 98 KB

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