dbus_new_handlers.c 77 KB

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