dbus_new_handlers.c 75 KB

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