dbus_new_handlers.c 89 KB

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