Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

dbus_new_handlers.c 88KB

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