dbus_new_handlers.c 76 KB

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