dbus_new_helpers.c 31 KB

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