dbus_new_handlers.c 76 KB

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