dbus_new_helpers.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Alternatively, this software may be distributed under the terms of BSD
  11. * license.
  12. *
  13. * See README and COPYING for more details.
  14. */
  15. #include "utils/includes.h"
  16. #include "utils/common.h"
  17. #include "utils/eloop.h"
  18. #include "dbus_common.h"
  19. #include "dbus_common_i.h"
  20. #include "dbus_new.h"
  21. #include "dbus_new_helpers.h"
  22. #include "dbus_dict_helpers.h"
  23. static dbus_bool_t fill_dict_with_properties(
  24. DBusMessageIter *dict_iter,
  25. const struct wpa_dbus_property_desc *props,
  26. const char *interface, void *user_data, DBusError *error)
  27. {
  28. DBusMessageIter entry_iter;
  29. const struct wpa_dbus_property_desc *dsc;
  30. for (dsc = props; dsc && dsc->dbus_property; dsc++) {
  31. /* Only return properties for the requested D-Bus interface */
  32. if (os_strncmp(dsc->dbus_interface, interface,
  33. WPAS_DBUS_INTERFACE_MAX) != 0)
  34. continue;
  35. /* Handle access permissions or missing getter */
  36. if (dsc->getter == NULL || dsc->access == W)
  37. continue;
  38. if (!dbus_message_iter_open_container(dict_iter,
  39. DBUS_TYPE_DICT_ENTRY,
  40. NULL, &entry_iter)) {
  41. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  42. "no memory");
  43. return FALSE;
  44. }
  45. if (!dbus_message_iter_append_basic(&entry_iter,
  46. DBUS_TYPE_STRING,
  47. &dsc->dbus_property)) {
  48. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  49. "no memory");
  50. return FALSE;
  51. }
  52. /* An error getting a property fails the request entirely */
  53. if (!dsc->getter(&entry_iter, error, user_data))
  54. return FALSE;
  55. dbus_message_iter_close_container(dict_iter, &entry_iter);
  56. }
  57. return TRUE;
  58. }
  59. /**
  60. * get_all_properties - Responds for GetAll properties calls on object
  61. * @message: Message with GetAll call
  62. * @interface: interface name which properties will be returned
  63. * @property_dsc: list of object's properties
  64. * Returns: Message with dict of variants as argument with properties values
  65. *
  66. * Iterates over all properties registered with object and execute getters
  67. * of those, which are readable and which interface matches interface
  68. * specified as argument. Returned message contains one dict argument
  69. * with properties names as keys and theirs values as values.
  70. */
  71. static DBusMessage * get_all_properties(DBusMessage *message, char *interface,
  72. struct wpa_dbus_object_desc *obj_dsc)
  73. {
  74. DBusMessage *reply;
  75. DBusMessageIter iter, dict_iter;
  76. DBusError error;
  77. reply = dbus_message_new_method_return(message);
  78. if (reply == NULL) {
  79. wpa_printf(MSG_ERROR, "%s: out of memory creating dbus reply",
  80. __func__);
  81. return NULL;
  82. }
  83. dbus_message_iter_init_append(reply, &iter);
  84. if (!wpa_dbus_dict_open_write(&iter, &dict_iter)) {
  85. wpa_printf(MSG_ERROR, "%s: out of memory creating reply",
  86. __func__);
  87. dbus_message_unref(reply);
  88. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  89. "out of memory");
  90. return reply;
  91. }
  92. dbus_error_init(&error);
  93. if (!fill_dict_with_properties(&dict_iter, obj_dsc->properties,
  94. interface, obj_dsc->user_data, &error))
  95. {
  96. dbus_message_unref(reply);
  97. reply = wpas_dbus_reply_new_from_error(message, &error,
  98. DBUS_ERROR_INVALID_ARGS,
  99. "No readable properties"
  100. " in this interface");
  101. dbus_error_free(&error);
  102. return reply;
  103. }
  104. wpa_dbus_dict_close_write(&iter, &dict_iter);
  105. return reply;
  106. }
  107. static int is_signature_correct(DBusMessage *message,
  108. const struct wpa_dbus_method_desc *method_dsc)
  109. {
  110. /* According to DBus documentation max length of signature is 255 */
  111. #define MAX_SIG_LEN 256
  112. char registered_sig[MAX_SIG_LEN], *pos;
  113. const char *sig = dbus_message_get_signature(message);
  114. int ret;
  115. const struct wpa_dbus_argument *arg;
  116. pos = registered_sig;
  117. *pos = '\0';
  118. for (arg = method_dsc->args; arg && arg->name; arg++) {
  119. if (arg->dir == ARG_IN) {
  120. size_t blen = registered_sig + MAX_SIG_LEN - pos;
  121. ret = os_snprintf(pos, blen, "%s", arg->type);
  122. if (ret < 0 || (size_t) ret >= blen)
  123. return 0;
  124. pos += ret;
  125. }
  126. }
  127. return !os_strncmp(registered_sig, sig, MAX_SIG_LEN);
  128. }
  129. static DBusMessage * properties_get_all(DBusMessage *message, char *interface,
  130. struct wpa_dbus_object_desc *obj_dsc)
  131. {
  132. if (os_strcmp(dbus_message_get_signature(message), "s") != 0)
  133. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  134. NULL);
  135. return get_all_properties(message, interface, obj_dsc);
  136. }
  137. static DBusMessage * properties_get(DBusMessage *message,
  138. const struct wpa_dbus_property_desc *dsc,
  139. void *user_data)
  140. {
  141. DBusMessage *reply;
  142. DBusMessageIter iter;
  143. DBusError error;
  144. if (os_strcmp(dbus_message_get_signature(message), "ss")) {
  145. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  146. NULL);
  147. }
  148. if (dsc->access == W || dsc->getter == NULL) {
  149. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  150. "Property is write-only");
  151. }
  152. reply = dbus_message_new_method_return(message);
  153. dbus_message_iter_init_append(reply, &iter);
  154. dbus_error_init(&error);
  155. if (dsc->getter(&iter, &error, user_data) == FALSE) {
  156. dbus_message_unref(reply);
  157. reply = wpas_dbus_reply_new_from_error(
  158. message, &error, DBUS_ERROR_FAILED,
  159. "Failed to read property");
  160. dbus_error_free(&error);
  161. }
  162. return reply;
  163. }
  164. static DBusMessage * properties_set(DBusMessage *message,
  165. const struct wpa_dbus_property_desc *dsc,
  166. void *user_data)
  167. {
  168. DBusMessage *reply;
  169. DBusMessageIter iter;
  170. DBusError error;
  171. if (os_strcmp(dbus_message_get_signature(message), "ssv")) {
  172. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  173. NULL);
  174. }
  175. if (dsc->access == R || dsc->setter == NULL) {
  176. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  177. "Property is read-only");
  178. }
  179. dbus_message_iter_init(message, &iter);
  180. /* Skip the interface name and the property name */
  181. dbus_message_iter_next(&iter);
  182. dbus_message_iter_next(&iter);
  183. /* Iter will now point to the property's new value */
  184. dbus_error_init(&error);
  185. if (dsc->setter(&iter, &error, user_data) == TRUE) {
  186. /* Success */
  187. reply = dbus_message_new_method_return(message);
  188. } else {
  189. reply = wpas_dbus_reply_new_from_error(
  190. message, &error, DBUS_ERROR_FAILED,
  191. "Failed to set property");
  192. dbus_error_free(&error);
  193. }
  194. return reply;
  195. }
  196. static DBusMessage *
  197. properties_get_or_set(DBusMessage *message, DBusMessageIter *iter,
  198. char *interface,
  199. struct wpa_dbus_object_desc *obj_dsc)
  200. {
  201. const struct wpa_dbus_property_desc *property_dsc;
  202. char *property;
  203. const char *method;
  204. method = dbus_message_get_member(message);
  205. property_dsc = obj_dsc->properties;
  206. /* Second argument: property name (DBUS_TYPE_STRING) */
  207. if (!dbus_message_iter_next(iter) ||
  208. dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING) {
  209. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  210. NULL);
  211. }
  212. dbus_message_iter_get_basic(iter, &property);
  213. while (property_dsc && property_dsc->dbus_property) {
  214. /* compare property names and
  215. * interfaces */
  216. if (!os_strncmp(property_dsc->dbus_property, property,
  217. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  218. !os_strncmp(property_dsc->dbus_interface, interface,
  219. WPAS_DBUS_INTERFACE_MAX))
  220. break;
  221. property_dsc++;
  222. }
  223. if (property_dsc == NULL || property_dsc->dbus_property == NULL) {
  224. wpa_printf(MSG_DEBUG, "no property handler for %s.%s on %s",
  225. interface, property,
  226. dbus_message_get_path(message));
  227. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  228. "No such property");
  229. }
  230. if (os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
  231. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) == 0)
  232. return properties_get(message, property_dsc,
  233. obj_dsc->user_data);
  234. return properties_set(message, property_dsc, obj_dsc->user_data);
  235. }
  236. static DBusMessage * properties_handler(DBusMessage *message,
  237. struct wpa_dbus_object_desc *obj_dsc)
  238. {
  239. DBusMessageIter iter;
  240. char *interface;
  241. const char *method;
  242. method = dbus_message_get_member(message);
  243. dbus_message_iter_init(message, &iter);
  244. if (!os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
  245. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
  246. !os_strncmp(WPA_DBUS_PROPERTIES_SET, method,
  247. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
  248. !os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
  249. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
  250. /* First argument: interface name (DBUS_TYPE_STRING) */
  251. if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
  252. {
  253. return dbus_message_new_error(message,
  254. DBUS_ERROR_INVALID_ARGS,
  255. NULL);
  256. }
  257. dbus_message_iter_get_basic(&iter, &interface);
  258. if (!os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
  259. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
  260. /* GetAll */
  261. return properties_get_all(message, interface, obj_dsc);
  262. }
  263. /* Get or Set */
  264. return properties_get_or_set(message, &iter, interface,
  265. obj_dsc);
  266. }
  267. return dbus_message_new_error(message, DBUS_ERROR_UNKNOWN_METHOD,
  268. NULL);
  269. }
  270. static DBusMessage * msg_method_handler(DBusMessage *message,
  271. struct wpa_dbus_object_desc *obj_dsc)
  272. {
  273. const struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
  274. const char *method;
  275. const char *msg_interface;
  276. method = dbus_message_get_member(message);
  277. msg_interface = dbus_message_get_interface(message);
  278. /* try match call to any registered method */
  279. while (method_dsc && method_dsc->dbus_method) {
  280. /* compare method names and interfaces */
  281. if (!os_strncmp(method_dsc->dbus_method, method,
  282. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  283. !os_strncmp(method_dsc->dbus_interface, msg_interface,
  284. WPAS_DBUS_INTERFACE_MAX))
  285. break;
  286. method_dsc++;
  287. }
  288. if (method_dsc == NULL || method_dsc->dbus_method == NULL) {
  289. wpa_printf(MSG_DEBUG, "no method handler for %s.%s on %s",
  290. msg_interface, method,
  291. dbus_message_get_path(message));
  292. return dbus_message_new_error(message,
  293. DBUS_ERROR_UNKNOWN_METHOD, NULL);
  294. }
  295. if (!is_signature_correct(message, method_dsc)) {
  296. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  297. NULL);
  298. }
  299. return method_dsc->method_handler(message,
  300. obj_dsc->user_data);
  301. }
  302. /**
  303. * message_handler - Handles incoming DBus messages
  304. * @connection: DBus connection on which message was received
  305. * @message: Received message
  306. * @user_data: pointer to description of object to which message was sent
  307. * Returns: Returns information whether message was handled or not
  308. *
  309. * Reads message interface and method name, then checks if they matches one
  310. * of the special cases i.e. introspection call or properties get/getall/set
  311. * methods and handles it. Else it iterates over registered methods list
  312. * and tries to match method's name and interface to those read from message
  313. * If appropriate method was found its handler function is called and
  314. * response is sent. Otherwise, the DBUS_ERROR_UNKNOWN_METHOD error message
  315. * will be sent.
  316. */
  317. static DBusHandlerResult message_handler(DBusConnection *connection,
  318. DBusMessage *message, void *user_data)
  319. {
  320. struct wpa_dbus_object_desc *obj_dsc = user_data;
  321. const char *method;
  322. const char *path;
  323. const char *msg_interface;
  324. DBusMessage *reply;
  325. /* get method, interface and path the message is addressed to */
  326. method = dbus_message_get_member(message);
  327. path = dbus_message_get_path(message);
  328. msg_interface = dbus_message_get_interface(message);
  329. if (!method || !path || !msg_interface)
  330. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  331. wpa_printf(MSG_MSGDUMP, "dbus: %s.%s (%s)",
  332. msg_interface, method, path);
  333. /* if message is introspection method call */
  334. if (!os_strncmp(WPA_DBUS_INTROSPECTION_METHOD, method,
  335. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  336. !os_strncmp(WPA_DBUS_INTROSPECTION_INTERFACE, msg_interface,
  337. WPAS_DBUS_INTERFACE_MAX)) {
  338. #ifdef CONFIG_CTRL_IFACE_DBUS_INTRO
  339. reply = wpa_dbus_introspect(message, obj_dsc);
  340. #else /* CONFIG_CTRL_IFACE_DBUS_INTRO */
  341. reply = dbus_message_new_error(
  342. message, DBUS_ERROR_UNKNOWN_METHOD,
  343. "wpa_supplicant was compiled without "
  344. "introspection support.");
  345. #endif /* CONFIG_CTRL_IFACE_DBUS_INTRO */
  346. } else if (!os_strncmp(WPA_DBUS_PROPERTIES_INTERFACE, msg_interface,
  347. WPAS_DBUS_INTERFACE_MAX)) {
  348. /* if message is properties method call */
  349. reply = properties_handler(message, obj_dsc);
  350. } else {
  351. reply = msg_method_handler(message, obj_dsc);
  352. }
  353. /* If handler succeed returning NULL, reply empty message */
  354. if (!reply)
  355. reply = dbus_message_new_method_return(message);
  356. if (reply) {
  357. if (!dbus_message_get_no_reply(message))
  358. dbus_connection_send(connection, reply, NULL);
  359. dbus_message_unref(reply);
  360. }
  361. wpa_dbus_flush_all_changed_properties(connection);
  362. return DBUS_HANDLER_RESULT_HANDLED;
  363. }
  364. /**
  365. * free_dbus_object_desc - Frees object description data structure
  366. * @connection: DBus connection
  367. * @obj_dsc: Object description to free
  368. *
  369. * Frees each of properties, methods and signals description lists and
  370. * the object description structure itself.
  371. */
  372. void free_dbus_object_desc(struct wpa_dbus_object_desc *obj_dsc)
  373. {
  374. if (!obj_dsc)
  375. return;
  376. /* free handler's argument */
  377. if (obj_dsc->user_data_free_func)
  378. obj_dsc->user_data_free_func(obj_dsc->user_data);
  379. os_free(obj_dsc->path);
  380. os_free(obj_dsc->prop_changed_flags);
  381. os_free(obj_dsc);
  382. }
  383. static void free_dbus_object_desc_cb(DBusConnection *connection, void *obj_dsc)
  384. {
  385. free_dbus_object_desc(obj_dsc);
  386. }
  387. /**
  388. * wpa_dbus_ctrl_iface_init - Initialize dbus control interface
  389. * @application_data: Pointer to application specific data structure
  390. * @dbus_path: DBus path to interface object
  391. * @dbus_service: DBus service name to register with
  392. * @messageHandler: a pointer to function which will handle dbus messages
  393. * coming on interface
  394. * Returns: 0 on success, -1 on failure
  395. *
  396. * Initialize the dbus control interface and start receiving commands from
  397. * external programs over the bus.
  398. */
  399. int wpa_dbus_ctrl_iface_init(struct wpas_dbus_priv *iface,
  400. char *dbus_path, char *dbus_service,
  401. struct wpa_dbus_object_desc *obj_desc)
  402. {
  403. DBusError error;
  404. int ret = -1;
  405. DBusObjectPathVTable wpa_vtable = {
  406. &free_dbus_object_desc_cb, &message_handler,
  407. NULL, NULL, NULL, NULL
  408. };
  409. obj_desc->connection = iface->con;
  410. obj_desc->path = os_strdup(dbus_path);
  411. /* Register the message handler for the global dbus interface */
  412. if (!dbus_connection_register_object_path(iface->con,
  413. dbus_path, &wpa_vtable,
  414. obj_desc)) {
  415. wpa_printf(MSG_ERROR, "dbus: Could not set up message "
  416. "handler");
  417. return -1;
  418. }
  419. /* Register our service with the message bus */
  420. dbus_error_init(&error);
  421. switch (dbus_bus_request_name(iface->con, dbus_service,
  422. 0, &error)) {
  423. case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
  424. ret = 0;
  425. break;
  426. case DBUS_REQUEST_NAME_REPLY_EXISTS:
  427. case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
  428. case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
  429. wpa_printf(MSG_ERROR, "dbus: Could not request service name: "
  430. "already registered");
  431. break;
  432. default:
  433. wpa_printf(MSG_ERROR, "dbus: Could not request service name: "
  434. "%s %s", error.name, error.message);
  435. break;
  436. }
  437. dbus_error_free(&error);
  438. if (ret != 0)
  439. return -1;
  440. wpa_printf(MSG_DEBUG, "Providing DBus service '%s'.", dbus_service);
  441. return 0;
  442. }
  443. /**
  444. * wpa_dbus_register_object_per_iface - Register a new object with dbus
  445. * @ctrl_iface: pointer to dbus private data
  446. * @path: DBus path to object
  447. * @ifname: interface name
  448. * @obj_desc: description of object's methods, signals and properties
  449. * Returns: 0 on success, -1 on error
  450. *
  451. * Registers a new interface with dbus and assigns it a dbus object path.
  452. */
  453. int wpa_dbus_register_object_per_iface(
  454. struct wpas_dbus_priv *ctrl_iface,
  455. const char *path, const char *ifname,
  456. struct wpa_dbus_object_desc *obj_desc)
  457. {
  458. DBusConnection *con;
  459. DBusError error;
  460. DBusObjectPathVTable vtable = {
  461. &free_dbus_object_desc_cb, &message_handler,
  462. NULL, NULL, NULL, NULL
  463. };
  464. /* Do nothing if the control interface is not turned on */
  465. if (ctrl_iface == NULL)
  466. return 0;
  467. con = ctrl_iface->con;
  468. obj_desc->connection = con;
  469. obj_desc->path = os_strdup(path);
  470. dbus_error_init(&error);
  471. /* Register the message handler for the interface functions */
  472. if (!dbus_connection_try_register_object_path(con, path, &vtable,
  473. obj_desc, &error)) {
  474. if (!os_strcmp(error.name, DBUS_ERROR_OBJECT_PATH_IN_USE)) {
  475. wpa_printf(MSG_DEBUG, "dbus: %s", error.message);
  476. } else {
  477. wpa_printf(MSG_ERROR, "dbus: Could not set up message "
  478. "handler for interface %s object %s",
  479. ifname, path);
  480. wpa_printf(MSG_ERROR, "dbus error: %s", error.name);
  481. wpa_printf(MSG_ERROR, "dbus: %s", error.message);
  482. }
  483. dbus_error_free(&error);
  484. return -1;
  485. }
  486. dbus_error_free(&error);
  487. return 0;
  488. }
  489. static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx);
  490. /**
  491. * wpa_dbus_unregister_object_per_iface - Unregisters DBus object
  492. * @ctrl_iface: Pointer to dbus private data
  493. * @path: DBus path to object which will be unregistered
  494. * Returns: Zero on success and -1 on failure
  495. *
  496. * Unregisters DBus object given by its path
  497. */
  498. int wpa_dbus_unregister_object_per_iface(
  499. struct wpas_dbus_priv *ctrl_iface, const char *path)
  500. {
  501. DBusConnection *con = ctrl_iface->con;
  502. struct wpa_dbus_object_desc *obj_desc = NULL;
  503. dbus_connection_get_object_path_data(con, path, (void **) &obj_desc);
  504. if (!obj_desc) {
  505. wpa_printf(MSG_ERROR, "dbus: %s: Could not obtain object's "
  506. "private data: %s", __func__, path);
  507. } else {
  508. eloop_cancel_timeout(flush_object_timeout_handler, con,
  509. obj_desc);
  510. }
  511. if (!dbus_connection_unregister_object_path(con, path))
  512. return -1;
  513. return 0;
  514. }
  515. static dbus_bool_t put_changed_properties(
  516. const struct wpa_dbus_object_desc *obj_dsc, const char *interface,
  517. DBusMessageIter *dict_iter)
  518. {
  519. DBusMessageIter entry_iter;
  520. const struct wpa_dbus_property_desc *dsc;
  521. int i;
  522. DBusError error;
  523. for (dsc = obj_dsc->properties, i = 0; dsc && dsc->dbus_property;
  524. dsc++, i++) {
  525. if (obj_dsc->prop_changed_flags == NULL ||
  526. !obj_dsc->prop_changed_flags[i])
  527. continue;
  528. if (os_strcmp(dsc->dbus_interface, interface) != 0)
  529. continue;
  530. obj_dsc->prop_changed_flags[i] = 0;
  531. if (!dbus_message_iter_open_container(dict_iter,
  532. DBUS_TYPE_DICT_ENTRY,
  533. NULL, &entry_iter))
  534. return FALSE;
  535. if (!dbus_message_iter_append_basic(&entry_iter,
  536. DBUS_TYPE_STRING,
  537. &dsc->dbus_property))
  538. return FALSE;
  539. dbus_error_init(&error);
  540. if (!dsc->getter(&entry_iter, &error, obj_dsc->user_data)) {
  541. if (dbus_error_is_set (&error)) {
  542. wpa_printf(MSG_ERROR, "dbus: %s: Cannot get "
  543. "new value of property %s: (%s) %s",
  544. __func__, dsc->dbus_property,
  545. error.name, error.message);
  546. } else {
  547. wpa_printf(MSG_ERROR, "dbus: %s: Cannot get "
  548. "new value of property %s",
  549. __func__, dsc->dbus_property);
  550. }
  551. dbus_error_free(&error);
  552. return FALSE;
  553. }
  554. if (!dbus_message_iter_close_container(dict_iter, &entry_iter))
  555. return FALSE;
  556. }
  557. return TRUE;
  558. }
  559. static void send_prop_changed_signal(
  560. DBusConnection *con, const char *path, const char *interface,
  561. const struct wpa_dbus_object_desc *obj_dsc)
  562. {
  563. DBusMessage *msg;
  564. DBusMessageIter signal_iter, dict_iter;
  565. msg = dbus_message_new_signal(path, interface, "PropertiesChanged");
  566. if (msg == NULL)
  567. return;
  568. dbus_message_iter_init_append(msg, &signal_iter);
  569. if (!dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
  570. "{sv}", &dict_iter))
  571. goto err;
  572. if (!put_changed_properties(obj_dsc, interface, &dict_iter))
  573. goto err;
  574. if (!dbus_message_iter_close_container(&signal_iter, &dict_iter))
  575. goto err;
  576. dbus_connection_send(con, msg, NULL);
  577. out:
  578. dbus_message_unref(msg);
  579. return;
  580. err:
  581. wpa_printf(MSG_DEBUG, "dbus: %s: Failed to construct signal",
  582. __func__);
  583. goto out;
  584. }
  585. static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx)
  586. {
  587. DBusConnection *con = eloop_ctx;
  588. struct wpa_dbus_object_desc *obj_desc = timeout_ctx;
  589. wpa_printf(MSG_DEBUG, "dbus: %s: Timeout - sending changed properties "
  590. "of object %s", __func__, obj_desc->path);
  591. wpa_dbus_flush_object_changed_properties(con, obj_desc->path);
  592. }
  593. static void recursive_flush_changed_properties(DBusConnection *con,
  594. const char *path)
  595. {
  596. char **objects = NULL;
  597. char subobj_path[WPAS_DBUS_OBJECT_PATH_MAX];
  598. int i;
  599. wpa_dbus_flush_object_changed_properties(con, path);
  600. if (!dbus_connection_list_registered(con, path, &objects))
  601. goto out;
  602. for (i = 0; objects[i]; i++) {
  603. os_snprintf(subobj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  604. "%s/%s", path, objects[i]);
  605. recursive_flush_changed_properties(con, subobj_path);
  606. }
  607. out:
  608. dbus_free_string_array(objects);
  609. }
  610. /**
  611. * wpa_dbus_flush_all_changed_properties - Send all PropertiesChanged signals
  612. * @con: DBus connection
  613. *
  614. * Traverses through all registered objects and sends PropertiesChanged for
  615. * each properties.
  616. */
  617. void wpa_dbus_flush_all_changed_properties(DBusConnection *con)
  618. {
  619. recursive_flush_changed_properties(con, WPAS_DBUS_NEW_PATH);
  620. }
  621. /**
  622. * wpa_dbus_flush_object_changed_properties - Send PropertiesChanged for object
  623. * @con: DBus connection
  624. * @path: path to a DBus object for which PropertiesChanged will be sent.
  625. *
  626. * Iterates over all properties registered with object and for each interface
  627. * containing properties marked as changed, sends a PropertiesChanged signal
  628. * containing names and new values of properties that have changed.
  629. *
  630. * You need to call this function after wpa_dbus_mark_property_changed()
  631. * if you want to send PropertiesChanged signal immediately (i.e., without
  632. * waiting timeout to expire). PropertiesChanged signal for an object is sent
  633. * automatically short time after first marking property as changed. All
  634. * PropertiesChanged signals are sent automatically after responding on DBus
  635. * message, so if you marked a property changed as a result of DBus call
  636. * (e.g., param setter), you usually do not need to call this function.
  637. */
  638. void wpa_dbus_flush_object_changed_properties(DBusConnection *con,
  639. const char *path)
  640. {
  641. struct wpa_dbus_object_desc *obj_desc = NULL;
  642. const struct wpa_dbus_property_desc *dsc;
  643. int i;
  644. dbus_connection_get_object_path_data(con, path, (void **) &obj_desc);
  645. if (!obj_desc)
  646. return;
  647. eloop_cancel_timeout(flush_object_timeout_handler, con, obj_desc);
  648. dsc = obj_desc->properties;
  649. for (dsc = obj_desc->properties, i = 0; dsc && dsc->dbus_property;
  650. dsc++, i++) {
  651. if (obj_desc->prop_changed_flags == NULL ||
  652. !obj_desc->prop_changed_flags[i])
  653. continue;
  654. send_prop_changed_signal(con, path, dsc->dbus_interface,
  655. obj_desc);
  656. }
  657. }
  658. #define WPA_DBUS_SEND_PROP_CHANGED_TIMEOUT 5000
  659. /**
  660. * wpa_dbus_mark_property_changed - Mark a property as changed and
  661. * @iface: dbus priv struct
  662. * @path: path to DBus object which property has changed
  663. * @interface: interface containing changed property
  664. * @property: property name which has changed
  665. *
  666. * Iterates over all properties registered with an object and marks the one
  667. * given in parameters as changed. All parameters registered for an object
  668. * within a single interface will be aggregated together and sent in one
  669. * PropertiesChanged signal when function
  670. * wpa_dbus_flush_object_changed_properties() is called.
  671. */
  672. void wpa_dbus_mark_property_changed(struct wpas_dbus_priv *iface,
  673. const char *path, const char *interface,
  674. const char *property)
  675. {
  676. struct wpa_dbus_object_desc *obj_desc = NULL;
  677. const struct wpa_dbus_property_desc *dsc;
  678. int i = 0;
  679. if (iface == NULL)
  680. return;
  681. dbus_connection_get_object_path_data(iface->con, path,
  682. (void **) &obj_desc);
  683. if (!obj_desc) {
  684. wpa_printf(MSG_ERROR, "dbus: wpa_dbus_property_changed: "
  685. "could not obtain object's private data: %s", path);
  686. return;
  687. }
  688. for (dsc = obj_desc->properties; dsc && dsc->dbus_property; dsc++, i++)
  689. if (os_strcmp(property, dsc->dbus_property) == 0 &&
  690. os_strcmp(interface, dsc->dbus_interface) == 0) {
  691. if (obj_desc->prop_changed_flags)
  692. obj_desc->prop_changed_flags[i] = 1;
  693. break;
  694. }
  695. if (!dsc || !dsc->dbus_property) {
  696. wpa_printf(MSG_ERROR, "dbus: wpa_dbus_property_changed: "
  697. "no property %s in object %s", property, path);
  698. return;
  699. }
  700. if (!eloop_is_timeout_registered(flush_object_timeout_handler,
  701. iface->con, obj_desc->path)) {
  702. eloop_register_timeout(0, WPA_DBUS_SEND_PROP_CHANGED_TIMEOUT,
  703. flush_object_timeout_handler,
  704. iface->con, obj_desc);
  705. }
  706. }
  707. /**
  708. * wpa_dbus_get_object_properties - Put object's properties into dictionary
  709. * @iface: dbus priv struct
  710. * @path: path to DBus object which properties will be obtained
  711. * @interface: interface name which properties will be obtained
  712. * @iter: DBus message iter at which to append property dictionary.
  713. *
  714. * Iterates over all properties registered with object and execute getters
  715. * of those, which are readable and which interface matches interface
  716. * specified as argument. Obtained properties values are stored in
  717. * dict_iter dictionary.
  718. */
  719. dbus_bool_t wpa_dbus_get_object_properties(struct wpas_dbus_priv *iface,
  720. const char *path,
  721. const char *interface,
  722. DBusMessageIter *iter)
  723. {
  724. struct wpa_dbus_object_desc *obj_desc = NULL;
  725. DBusMessageIter dict_iter;
  726. DBusError error;
  727. dbus_connection_get_object_path_data(iface->con, path,
  728. (void **) &obj_desc);
  729. if (!obj_desc) {
  730. wpa_printf(MSG_ERROR, "dbus: %s: could not obtain object's "
  731. "private data: %s", __func__, path);
  732. return FALSE;
  733. }
  734. if (!wpa_dbus_dict_open_write(iter, &dict_iter)) {
  735. wpa_printf(MSG_ERROR, "dbus: %s: failed to open message dict",
  736. __func__);
  737. return FALSE;
  738. }
  739. dbus_error_init(&error);
  740. if (!fill_dict_with_properties(&dict_iter, obj_desc->properties,
  741. interface, obj_desc->user_data,
  742. &error)) {
  743. wpa_printf(MSG_ERROR, "dbus: %s: failed to get object"
  744. " properties: (%s) %s", __func__,
  745. dbus_error_is_set(&error) ? error.name : "none",
  746. dbus_error_is_set(&error) ? error.message : "none");
  747. dbus_error_free(&error);
  748. return FALSE;
  749. }
  750. return wpa_dbus_dict_close_write(iter, &dict_iter);
  751. }
  752. /**
  753. * wpas_dbus_new_decompose_object_path - Decompose an interface object path into parts
  754. * @path: The dbus object path
  755. * @p2p_persistent_group: indicates whether to parse the path as a P2P
  756. * persistent group object
  757. * @network: (out) the configured network this object path refers to, if any
  758. * @bssid: (out) the scanned bssid this object path refers to, if any
  759. * Returns: The object path of the network interface this path refers to
  760. *
  761. * For a given object path, decomposes the object path into object id, network,
  762. * and BSSID parts, if those parts exist.
  763. */
  764. char *wpas_dbus_new_decompose_object_path(const char *path,
  765. int p2p_persistent_group,
  766. char **network,
  767. char **bssid)
  768. {
  769. const unsigned int dev_path_prefix_len =
  770. os_strlen(WPAS_DBUS_NEW_PATH_INTERFACES "/");
  771. char *obj_path_only;
  772. char *next_sep;
  773. /* Be a bit paranoid about path */
  774. if (!path || os_strncmp(path, WPAS_DBUS_NEW_PATH_INTERFACES "/",
  775. dev_path_prefix_len))
  776. return NULL;
  777. /* Ensure there's something at the end of the path */
  778. if ((path + dev_path_prefix_len)[0] == '\0')
  779. return NULL;
  780. obj_path_only = os_strdup(path);
  781. if (obj_path_only == NULL)
  782. return NULL;
  783. next_sep = os_strchr(obj_path_only + dev_path_prefix_len, '/');
  784. if (next_sep != NULL) {
  785. const char *net_part = os_strstr(
  786. next_sep, p2p_persistent_group ?
  787. WPAS_DBUS_NEW_PERSISTENT_GROUPS_PART "/" :
  788. WPAS_DBUS_NEW_NETWORKS_PART "/");
  789. const char *bssid_part = os_strstr(
  790. next_sep, WPAS_DBUS_NEW_BSSIDS_PART "/");
  791. if (network && net_part) {
  792. /* Deal with a request for a configured network */
  793. const char *net_name = net_part +
  794. os_strlen(p2p_persistent_group ?
  795. WPAS_DBUS_NEW_PERSISTENT_GROUPS_PART
  796. "/" :
  797. WPAS_DBUS_NEW_NETWORKS_PART "/");
  798. *network = NULL;
  799. if (os_strlen(net_name))
  800. *network = os_strdup(net_name);
  801. } else if (bssid && bssid_part) {
  802. /* Deal with a request for a scanned BSSID */
  803. const char *bssid_name = bssid_part +
  804. os_strlen(WPAS_DBUS_NEW_BSSIDS_PART "/");
  805. if (os_strlen(bssid_name))
  806. *bssid = os_strdup(bssid_name);
  807. else
  808. *bssid = NULL;
  809. }
  810. /* Cut off interface object path before "/" */
  811. *next_sep = '\0';
  812. }
  813. return obj_path_only;
  814. }
  815. /**
  816. * wpas_dbus_reply_new_from_error - Create a new D-Bus error message from a
  817. * dbus error structure
  818. * @message: The original request message for which the error is a reply
  819. * @error: The error containing a name and a descriptive error cause
  820. * @fallback_name: A generic error name if @error was not set
  821. * @fallback_string: A generic error string if @error was not set
  822. * Returns: A new D-Bus error message
  823. *
  824. * Given a DBusMessage structure, creates a new D-Bus error message using
  825. * the error name and string contained in that structure.
  826. */
  827. DBusMessage * wpas_dbus_reply_new_from_error(DBusMessage *message,
  828. DBusError *error,
  829. const char *fallback_name,
  830. const char *fallback_string)
  831. {
  832. if (error && error->name && error->message) {
  833. return dbus_message_new_error(message, error->name,
  834. error->message);
  835. }
  836. if (fallback_name && fallback_string) {
  837. return dbus_message_new_error(message, fallback_name,
  838. fallback_string);
  839. }
  840. return NULL;
  841. }