dbus_new_handlers.c 95 KB

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