dbus_new_handlers.c 75 KB

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