dbus_new_handlers.c 76 KB

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