ctrl_iface_dbus_new_handlers.c 95 KB

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