dbus_new_helpers.c 30 KB

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