dbus_new_helpers.c 31 KB

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