dbus_new_helpers.c 31 KB

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