dbus_new_helpers.c 30 KB

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