From f2e9323ecfcd034cd012e768d3a3d73927179387 Mon Sep 17 00:00:00 2001 From: Karol Lewandowski Date: Thu, 28 Apr 2016 14:06:38 +0200 Subject: [PATCH] Initialize policy database based on bus path Currently library has no reliable way of determining which path was already opened by libdbus/glib when only bus type is given. This commit changes API to allow client libraries pass bus path directly eliminating possible ambiguities. Change-Id: I732c391898f661b923fa61d4a2aebb402fb0ec3c --- src/dbuspolicy1/libdbuspolicy1.h | 16 +++++++++--- src/libdbuspolicy1.c | 53 ++++++++++++++++++++++++++++++++-------- 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/src/dbuspolicy1/libdbuspolicy1.h b/src/dbuspolicy1/libdbuspolicy1.h index a0f7dcd..0037fc7 100644 --- a/src/dbuspolicy1/libdbuspolicy1.h +++ b/src/dbuspolicy1/libdbuspolicy1.h @@ -46,13 +46,21 @@ extern "C" { struct udesc; /*! - libdbuspolicy init - \param bus_type bus type (SYSTEM or SESSION) + Initialize libdbuspolicy configuration context + \param bus_path path to the kdbus bus (system or session) + + \note This function should be called only on well known kdbus buses + - the system bus (/sys/fs/kdbus/0-system/bus) and session bus + (/sys/fs/kdbus/getuid()-user/bus). If any other bus is specified + function will not succeed. + + \return On success pointer to configuration context is returned. On + error NULL is returned. */ -void* dbuspolicy1_init(unsigned int bus_type); +void* dbuspolicy1_init(const char *bus_path); /*! - libdbuspolicy free + Free libdbuspolicy configuration context \param configuration pointer with policy configuration acquired using dbuspolicy1_init */ void dbuspolicy1_free(void* configuration); diff --git a/src/libdbuspolicy1.c b/src/libdbuspolicy1.c index e10d43b..68f13a9 100644 --- a/src/libdbuspolicy1.c +++ b/src/libdbuspolicy1.c @@ -30,12 +30,15 @@ #include #include #include +#include #include #include "libdbuspolicy1-private.h" #include "internal/internal.h" +#define KDBUS_PATH_PREFIX "/sys/fs/kdbus/" #define KDBUS_SYSTEM_BUS_PATH "/sys/fs/kdbus/0-system/bus" + #define KDBUS_POOL_SIZE (16 * 1024UL * 1024UL) #define ALIGN8(l) (((l) + 7) & ~7) @@ -90,9 +93,9 @@ struct udesc { }; -static int kdbus_open_system_bus(void) +static int kdbus_open_bus(const char *path) { - return open(KDBUS_SYSTEM_BUS_PATH, O_RDWR|O_NOCTTY|O_LARGEFILE|O_CLOEXEC ); + return open(path, O_RDWR|O_NOCTTY|O_LARGEFILE|O_CLOEXEC); } static int kdbus_hello(struct kconn *kc, uint64_t hello_flags, uint64_t attach_flags_send, uint64_t attach_flags_recv) @@ -262,25 +265,55 @@ static int dbuspolicy_init_udesc(struct kconn* kc, unsigned int bus_type, struct return 0; } -/** - * dbuspolicy1_init - * @config_name: name of the XML configuration file - * - * Set the configuration file used by the calling application - **/ -DBUSPOLICY1_EXPORT void* dbuspolicy1_init(unsigned int bus_type) +static int bus_path_resolve(const char *bus_path, char *resolved_path, unsigned resolved_path_size, unsigned int *bus_type) +{ + char rp[PATH_MAX]; + char *p; + const char user_suffix[] = "-user/bus"; + int suffix_pos; + + p = realpath(bus_path, rp); + if (!p) + return -1; + + if (0 != strncmp(p, KDBUS_PATH_PREFIX, strlen(KDBUS_PATH_PREFIX))) + return -1; + + if (0 == strcmp(p, KDBUS_SYSTEM_BUS_PATH)) { + *bus_type = SYSTEM_BUS; + } else { + suffix_pos = strlen(p) - strlen(user_suffix); + if (suffix_pos < 0) + return -1; + + if (0 != strcmp(p + suffix_pos, user_suffix)) + return -1; + + *bus_type = SESSION_BUS; + } + + snprintf(resolved_path, resolved_path_size, "%s", p); + return 0; +} + +DBUSPOLICY1_EXPORT void* dbuspolicy1_init(const char *bus_path) { uint64_t hello_flags = 0; uint64_t attach_flags_send = _KDBUS_ATTACH_ANY; uint64_t attach_flags_recv = _KDBUS_ATTACH_ALL; struct kconn* kc = NULL; struct udesc* p_udesc = NULL; + unsigned int bus_type = -1; + char resolved_path[PATH_MAX] = { 0 }; + + if (bus_path_resolve(bus_path, resolved_path, sizeof(resolved_path), &bus_type) < 0) + goto err; kc = (struct kconn*) calloc(1, sizeof(struct kconn)); if (!kc) goto err; - if ((kc->fd = kdbus_open_system_bus()) < 0) + if ((kc->fd = kdbus_open_bus(resolved_path)) < 0) goto err; if (kdbus_hello(kc, hello_flags, attach_flags_send, attach_flags_recv) < 0) -- 2.7.4