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_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(dsc, &entry_iter, error, user_data)) {
  42. wpa_printf(MSG_INFO,
  43. "dbus: %s dbus_interface=%s dbus_property=%s getter failed",
  44. __func__, dsc->dbus_interface,
  45. dsc->dbus_property);
  46. return FALSE;
  47. }
  48. if (!dbus_message_iter_close_container(dict_iter, &entry_iter))
  49. goto error;
  50. }
  51. return TRUE;
  52. error:
  53. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
  54. return FALSE;
  55. }
  56. /**
  57. * get_all_properties - Responds for GetAll properties calls on object
  58. * @message: Message with GetAll call
  59. * @interface: interface name which properties will be returned
  60. * @property_dsc: list of object's properties
  61. * Returns: Message with dict of variants as argument with properties values
  62. *
  63. * Iterates over all properties registered with object and execute getters
  64. * of those, which are readable and which interface matches interface
  65. * specified as argument. Returned message contains one dict argument
  66. * with properties names as keys and theirs values as values.
  67. */
  68. static DBusMessage * get_all_properties(DBusMessage *message, char *interface,
  69. struct wpa_dbus_object_desc *obj_dsc)
  70. {
  71. DBusMessage *reply;
  72. DBusMessageIter iter, dict_iter;
  73. DBusError error;
  74. reply = dbus_message_new_method_return(message);
  75. if (reply == NULL)
  76. return wpas_dbus_error_no_memory(message);
  77. dbus_message_iter_init_append(reply, &iter);
  78. if (!wpa_dbus_dict_open_write(&iter, &dict_iter)) {
  79. dbus_message_unref(reply);
  80. return wpas_dbus_error_no_memory(message);
  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 wpas_dbus_error_no_memory(message);
  95. }
  96. return reply;
  97. }
  98. static int is_signature_correct(DBusMessage *message,
  99. const struct wpa_dbus_method_desc *method_dsc)
  100. {
  101. /* According to DBus documentation max length of signature is 255 */
  102. #define MAX_SIG_LEN 256
  103. char registered_sig[MAX_SIG_LEN], *pos;
  104. const char *sig = dbus_message_get_signature(message);
  105. int ret;
  106. const struct wpa_dbus_argument *arg;
  107. pos = registered_sig;
  108. *pos = '\0';
  109. for (arg = method_dsc->args; arg && arg->name; arg++) {
  110. if (arg->dir == ARG_IN) {
  111. size_t blen = registered_sig + MAX_SIG_LEN - pos;
  112. ret = os_snprintf(pos, blen, "%s", arg->type);
  113. if (os_snprintf_error(blen, ret))
  114. return 0;
  115. pos += ret;
  116. }
  117. }
  118. return !os_strncmp(registered_sig, sig, MAX_SIG_LEN);
  119. }
  120. static DBusMessage * properties_get_all(DBusMessage *message, char *interface,
  121. struct wpa_dbus_object_desc *obj_dsc)
  122. {
  123. if (os_strcmp(dbus_message_get_signature(message), "s") != 0)
  124. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  125. NULL);
  126. return get_all_properties(message, interface, obj_dsc);
  127. }
  128. static DBusMessage * properties_get(DBusMessage *message,
  129. const struct wpa_dbus_property_desc *dsc,
  130. void *user_data)
  131. {
  132. DBusMessage *reply;
  133. DBusMessageIter iter;
  134. DBusError error;
  135. if (os_strcmp(dbus_message_get_signature(message), "ss")) {
  136. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  137. NULL);
  138. }
  139. if (dsc->getter == NULL) {
  140. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  141. "Property is write-only");
  142. }
  143. reply = dbus_message_new_method_return(message);
  144. dbus_message_iter_init_append(reply, &iter);
  145. dbus_error_init(&error);
  146. if (dsc->getter(dsc, &iter, &error, user_data) == FALSE) {
  147. dbus_message_unref(reply);
  148. reply = wpas_dbus_reply_new_from_error(
  149. message, &error, DBUS_ERROR_FAILED,
  150. "Failed to read property");
  151. dbus_error_free(&error);
  152. }
  153. return reply;
  154. }
  155. static DBusMessage * properties_set(DBusMessage *message,
  156. const struct wpa_dbus_property_desc *dsc,
  157. void *user_data)
  158. {
  159. DBusMessage *reply;
  160. DBusMessageIter iter;
  161. DBusError error;
  162. if (os_strcmp(dbus_message_get_signature(message), "ssv")) {
  163. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  164. NULL);
  165. }
  166. if (dsc->setter == NULL) {
  167. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  168. "Property is read-only");
  169. }
  170. dbus_message_iter_init(message, &iter);
  171. /* Skip the interface name and the property name */
  172. dbus_message_iter_next(&iter);
  173. dbus_message_iter_next(&iter);
  174. /* Iter will now point to the property's new value */
  175. dbus_error_init(&error);
  176. if (dsc->setter(dsc, &iter, &error, user_data) == TRUE) {
  177. /* Success */
  178. reply = dbus_message_new_method_return(message);
  179. } else {
  180. reply = wpas_dbus_reply_new_from_error(
  181. message, &error, DBUS_ERROR_FAILED,
  182. "Failed to set property");
  183. dbus_error_free(&error);
  184. }
  185. return reply;
  186. }
  187. static DBusMessage *
  188. properties_get_or_set(DBusMessage *message, DBusMessageIter *iter,
  189. char *interface,
  190. struct wpa_dbus_object_desc *obj_dsc)
  191. {
  192. const struct wpa_dbus_property_desc *property_dsc;
  193. char *property;
  194. const char *method;
  195. method = dbus_message_get_member(message);
  196. property_dsc = obj_dsc->properties;
  197. /* Second argument: property name (DBUS_TYPE_STRING) */
  198. if (!dbus_message_iter_next(iter) ||
  199. dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING) {
  200. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  201. NULL);
  202. }
  203. dbus_message_iter_get_basic(iter, &property);
  204. while (property_dsc && property_dsc->dbus_property) {
  205. /* compare property names and
  206. * interfaces */
  207. if (!os_strncmp(property_dsc->dbus_property, property,
  208. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  209. !os_strncmp(property_dsc->dbus_interface, interface,
  210. WPAS_DBUS_INTERFACE_MAX))
  211. break;
  212. property_dsc++;
  213. }
  214. if (property_dsc == NULL || property_dsc->dbus_property == NULL) {
  215. wpa_printf(MSG_DEBUG, "no property handler for %s.%s on %s",
  216. interface, property,
  217. dbus_message_get_path(message));
  218. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  219. "No such property");
  220. }
  221. if (os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
  222. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) == 0) {
  223. wpa_printf(MSG_MSGDUMP, "%s: Get(%s)", __func__, property);
  224. return properties_get(message, property_dsc,
  225. obj_dsc->user_data);
  226. }
  227. wpa_printf(MSG_MSGDUMP, "%s: Set(%s)", __func__, property);
  228. return properties_set(message, property_dsc, obj_dsc->user_data);
  229. }
  230. static DBusMessage * properties_handler(DBusMessage *message,
  231. struct wpa_dbus_object_desc *obj_dsc)
  232. {
  233. DBusMessageIter iter;
  234. char *interface;
  235. const char *method;
  236. method = dbus_message_get_member(message);
  237. dbus_message_iter_init(message, &iter);
  238. if (!os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
  239. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
  240. !os_strncmp(WPA_DBUS_PROPERTIES_SET, method,
  241. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
  242. !os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
  243. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
  244. /* First argument: interface name (DBUS_TYPE_STRING) */
  245. if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) {
  246. return dbus_message_new_error(message,
  247. DBUS_ERROR_INVALID_ARGS,
  248. NULL);
  249. }
  250. dbus_message_iter_get_basic(&iter, &interface);
  251. if (!os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
  252. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
  253. /* GetAll */
  254. return properties_get_all(message, interface, obj_dsc);
  255. }
  256. /* Get or Set */
  257. return properties_get_or_set(message, &iter, interface,
  258. obj_dsc);
  259. }
  260. return dbus_message_new_error(message, DBUS_ERROR_UNKNOWN_METHOD,
  261. NULL);
  262. }
  263. static DBusMessage * msg_method_handler(DBusMessage *message,
  264. struct wpa_dbus_object_desc *obj_dsc)
  265. {
  266. const struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
  267. const char *method;
  268. const char *msg_interface;
  269. method = dbus_message_get_member(message);
  270. msg_interface = dbus_message_get_interface(message);
  271. /* try match call to any registered method */
  272. while (method_dsc && method_dsc->dbus_method) {
  273. /* compare method names and interfaces */
  274. if (!os_strncmp(method_dsc->dbus_method, method,
  275. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  276. !os_strncmp(method_dsc->dbus_interface, msg_interface,
  277. WPAS_DBUS_INTERFACE_MAX))
  278. break;
  279. method_dsc++;
  280. }
  281. if (method_dsc == NULL || method_dsc->dbus_method == NULL) {
  282. wpa_printf(MSG_DEBUG, "no method handler for %s.%s on %s",
  283. msg_interface, method,
  284. dbus_message_get_path(message));
  285. return dbus_message_new_error(message,
  286. DBUS_ERROR_UNKNOWN_METHOD, NULL);
  287. }
  288. if (!is_signature_correct(message, method_dsc)) {
  289. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  290. NULL);
  291. }
  292. return method_dsc->method_handler(message, obj_dsc->user_data);
  293. }
  294. /**
  295. * message_handler - Handles incoming DBus messages
  296. * @connection: DBus connection on which message was received
  297. * @message: Received message
  298. * @user_data: pointer to description of object to which message was sent
  299. * Returns: Returns information whether message was handled or not
  300. *
  301. * Reads message interface and method name, then checks if they matches one
  302. * of the special cases i.e. introspection call or properties get/getall/set
  303. * methods and handles it. Else it iterates over registered methods list
  304. * and tries to match method's name and interface to those read from message
  305. * If appropriate method was found its handler function is called and
  306. * response is sent. Otherwise, the DBUS_ERROR_UNKNOWN_METHOD error message
  307. * will be sent.
  308. */
  309. static DBusHandlerResult message_handler(DBusConnection *connection,
  310. DBusMessage *message, void *user_data)
  311. {
  312. struct wpa_dbus_object_desc *obj_dsc = user_data;
  313. const char *method;
  314. const char *path;
  315. const char *msg_interface;
  316. DBusMessage *reply;
  317. /* get method, interface and path the message is addressed to */
  318. method = dbus_message_get_member(message);
  319. path = dbus_message_get_path(message);
  320. msg_interface = dbus_message_get_interface(message);
  321. if (!method || !path || !msg_interface)
  322. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  323. wpa_printf(MSG_MSGDUMP, "dbus: %s.%s (%s) [%s]",
  324. msg_interface, method, path,
  325. dbus_message_get_signature(message));
  326. /* if message is introspection method call */
  327. if (!os_strncmp(WPA_DBUS_INTROSPECTION_METHOD, method,
  328. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  329. !os_strncmp(WPA_DBUS_INTROSPECTION_INTERFACE, msg_interface,
  330. WPAS_DBUS_INTERFACE_MAX)) {
  331. #ifdef CONFIG_CTRL_IFACE_DBUS_INTRO
  332. reply = wpa_dbus_introspect(message, obj_dsc);
  333. #else /* CONFIG_CTRL_IFACE_DBUS_INTRO */
  334. reply = dbus_message_new_error(
  335. message, DBUS_ERROR_UNKNOWN_METHOD,
  336. "wpa_supplicant was compiled without introspection support.");
  337. #endif /* CONFIG_CTRL_IFACE_DBUS_INTRO */
  338. } else if (!os_strncmp(WPA_DBUS_PROPERTIES_INTERFACE, msg_interface,
  339. WPAS_DBUS_INTERFACE_MAX)) {
  340. /* if message is properties method call */
  341. reply = properties_handler(message, obj_dsc);
  342. } else {
  343. reply = msg_method_handler(message, obj_dsc);
  344. }
  345. /* If handler succeed returning NULL, reply empty message */
  346. if (!reply)
  347. reply = dbus_message_new_method_return(message);
  348. if (reply) {
  349. if (!dbus_message_get_no_reply(message))
  350. dbus_connection_send(connection, reply, NULL);
  351. dbus_message_unref(reply);
  352. }
  353. wpa_dbus_flush_all_changed_properties(connection);
  354. return DBUS_HANDLER_RESULT_HANDLED;
  355. }
  356. /**
  357. * free_dbus_object_desc - Frees object description data structure
  358. * @connection: DBus connection
  359. * @obj_dsc: Object description to free
  360. *
  361. * Frees each of properties, methods and signals description lists and
  362. * the object description structure itself.
  363. */
  364. void free_dbus_object_desc(struct wpa_dbus_object_desc *obj_dsc)
  365. {
  366. if (!obj_dsc)
  367. return;
  368. /* free handler's argument */
  369. if (obj_dsc->user_data_free_func)
  370. obj_dsc->user_data_free_func(obj_dsc->user_data);
  371. os_free(obj_dsc->path);
  372. os_free(obj_dsc->prop_changed_flags);
  373. os_free(obj_dsc);
  374. }
  375. static void free_dbus_object_desc_cb(DBusConnection *connection, void *obj_dsc)
  376. {
  377. free_dbus_object_desc(obj_dsc);
  378. }
  379. /**
  380. * wpa_dbus_ctrl_iface_init - Initialize dbus control interface
  381. * @application_data: Pointer to application specific data structure
  382. * @dbus_path: DBus path to interface object
  383. * @dbus_service: DBus service name to register with
  384. * @messageHandler: a pointer to function which will handle dbus messages
  385. * coming on interface
  386. * Returns: 0 on success, -1 on failure
  387. *
  388. * Initialize the dbus control interface and start receiving commands from
  389. * external programs over the bus.
  390. */
  391. int wpa_dbus_ctrl_iface_init(struct wpas_dbus_priv *iface,
  392. char *dbus_path, char *dbus_service,
  393. struct wpa_dbus_object_desc *obj_desc)
  394. {
  395. DBusError error;
  396. int ret = -1;
  397. DBusObjectPathVTable wpa_vtable = {
  398. &free_dbus_object_desc_cb, &message_handler,
  399. NULL, NULL, NULL, NULL
  400. };
  401. obj_desc->connection = iface->con;
  402. obj_desc->path = os_strdup(dbus_path);
  403. /* Register the message handler for the global dbus interface */
  404. if (!dbus_connection_register_object_path(iface->con, dbus_path,
  405. &wpa_vtable, obj_desc)) {
  406. wpa_printf(MSG_ERROR, "dbus: Could not set up message handler");
  407. return -1;
  408. }
  409. /* Register our service with the message bus */
  410. dbus_error_init(&error);
  411. switch (dbus_bus_request_name(iface->con, dbus_service, 0, &error)) {
  412. case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
  413. ret = 0;
  414. break;
  415. case DBUS_REQUEST_NAME_REPLY_EXISTS:
  416. case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
  417. case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
  418. wpa_printf(MSG_ERROR,
  419. "dbus: Could not request service name: already registered");
  420. break;
  421. default:
  422. wpa_printf(MSG_ERROR,
  423. "dbus: Could not request service name: %s %s",
  424. error.name, error.message);
  425. break;
  426. }
  427. dbus_error_free(&error);
  428. if (ret != 0)
  429. return -1;
  430. wpa_printf(MSG_DEBUG, "Providing DBus service '%s'.", dbus_service);
  431. return 0;
  432. }
  433. /**
  434. * wpa_dbus_register_object_per_iface - Register a new object with dbus
  435. * @ctrl_iface: pointer to dbus private data
  436. * @path: DBus path to object
  437. * @ifname: interface name
  438. * @obj_desc: description of object's methods, signals and properties
  439. * Returns: 0 on success, -1 on error
  440. *
  441. * Registers a new interface with dbus and assigns it a dbus object path.
  442. */
  443. int wpa_dbus_register_object_per_iface(struct wpas_dbus_priv *ctrl_iface,
  444. const char *path, const char *ifname,
  445. struct wpa_dbus_object_desc *obj_desc)
  446. {
  447. DBusConnection *con;
  448. DBusError error;
  449. DBusObjectPathVTable vtable = {
  450. &free_dbus_object_desc_cb, &message_handler,
  451. NULL, NULL, NULL, NULL
  452. };
  453. /* Do nothing if the control interface is not turned on */
  454. if (ctrl_iface == NULL)
  455. return 0;
  456. con = ctrl_iface->con;
  457. obj_desc->connection = con;
  458. obj_desc->path = os_strdup(path);
  459. dbus_error_init(&error);
  460. /* Register the message handler for the interface functions */
  461. if (!dbus_connection_try_register_object_path(con, path, &vtable,
  462. obj_desc, &error)) {
  463. if (os_strcmp(error.name, DBUS_ERROR_OBJECT_PATH_IN_USE) == 0) {
  464. wpa_printf(MSG_DEBUG, "dbus: %s", error.message);
  465. } else {
  466. wpa_printf(MSG_ERROR,
  467. "dbus: Could not set up message handler for interface %s object %s (error: %s message: %s)",
  468. ifname, path, error.name, error.message);
  469. }
  470. dbus_error_free(&error);
  471. return -1;
  472. }
  473. dbus_error_free(&error);
  474. return 0;
  475. }
  476. static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx);
  477. /**
  478. * wpa_dbus_unregister_object_per_iface - Unregisters DBus object
  479. * @ctrl_iface: Pointer to dbus private data
  480. * @path: DBus path to object which will be unregistered
  481. * Returns: Zero on success and -1 on failure
  482. *
  483. * Unregisters DBus object given by its path
  484. */
  485. int wpa_dbus_unregister_object_per_iface(
  486. struct wpas_dbus_priv *ctrl_iface, const char *path)
  487. {
  488. DBusConnection *con = ctrl_iface->con;
  489. struct wpa_dbus_object_desc *obj_desc = NULL;
  490. dbus_connection_get_object_path_data(con, path, (void **) &obj_desc);
  491. if (!obj_desc) {
  492. wpa_printf(MSG_ERROR,
  493. "dbus: %s: Could not obtain object's private data: %s",
  494. __func__, path);
  495. return 0;
  496. }
  497. eloop_cancel_timeout(flush_object_timeout_handler, con, obj_desc);
  498. if (!dbus_connection_unregister_object_path(con, path))
  499. return -1;
  500. return 0;
  501. }
  502. static dbus_bool_t put_changed_properties(
  503. const struct wpa_dbus_object_desc *obj_dsc, const char *interface,
  504. DBusMessageIter *dict_iter, int clear_changed)
  505. {
  506. DBusMessageIter entry_iter;
  507. const struct wpa_dbus_property_desc *dsc;
  508. int i;
  509. DBusError error;
  510. for (dsc = obj_dsc->properties, i = 0; dsc && dsc->dbus_property;
  511. dsc++, i++) {
  512. if (obj_dsc->prop_changed_flags == NULL ||
  513. !obj_dsc->prop_changed_flags[i])
  514. continue;
  515. if (os_strcmp(dsc->dbus_interface, interface) != 0)
  516. continue;
  517. if (clear_changed)
  518. obj_dsc->prop_changed_flags[i] = 0;
  519. if (!dbus_message_iter_open_container(dict_iter,
  520. DBUS_TYPE_DICT_ENTRY,
  521. NULL, &entry_iter) ||
  522. !dbus_message_iter_append_basic(&entry_iter,
  523. DBUS_TYPE_STRING,
  524. &dsc->dbus_property))
  525. return FALSE;
  526. dbus_error_init(&error);
  527. if (!dsc->getter(dsc, &entry_iter, &error, obj_dsc->user_data))
  528. {
  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. }