dbus_new_handlers.c 101 KB

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