summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorYun-Hao Chung <howardchung@chromium.org>2021-08-03 19:43:15 +0800
committerLuiz Augusto von Dentz <luiz.von.dentz@intel.com>2021-08-04 15:01:33 -0700
commitb97174f2ea60165060932f1c75897ee878d534fa (patch)
treef0aaa3b77e756ea14b500db5c8ccdb974e51bd98 /plugins
parent3c258b15a5e5e2899336a4afe422f32b9c010b06 (diff)
downloadbluez-b97174f2ea60165060932f1c75897ee878d534fa.tar.gz
plugins/admin: persist policy settings
This adds code to store the ServiceAllowlist to file /var/lib/bluetooth/{MAC_ADDR}/admin_policy The stored settings will be loaded upon admin_policy initialized. Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
Diffstat (limited to 'plugins')
-rw-r--r--plugins/admin.c165
1 files changed, 164 insertions, 1 deletions
diff --git a/plugins/admin.c b/plugins/admin.c
index e0279f803..428a5528c 100644
--- a/plugins/admin.c
+++ b/plugins/admin.c
@@ -14,6 +14,9 @@
#include <dbus/dbus.h>
#include <gdbus/gdbus.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <errno.h>
#include "lib/bluetooth.h"
#include "lib/uuid.h"
@@ -24,11 +27,13 @@
#include "src/error.h"
#include "src/log.h"
#include "src/plugin.h"
+#include "src/textfile.h"
#include "src/shared/queue.h"
#define ADMIN_POLICY_SET_INTERFACE "org.bluez.AdminPolicySet1"
#define ADMIN_POLICY_STATUS_INTERFACE "org.bluez.AdminPolicyStatus1"
+#define ADMIN_POLICY_STORAGE STORAGEDIR "/admin_policy_settings"
#define DBUS_BLUEZ_SERVICE "org.bluez"
#define BTD_DEVICE_INTERFACE "org.bluez.Device1"
@@ -161,6 +166,161 @@ static void update_device_affected(void *data, void *user_data)
ADMIN_POLICY_STATUS_INTERFACE, "AffectedByPolicy");
}
+static void free_uuid_strings(char **uuid_strs, gsize num)
+{
+ gsize i;
+
+ for (i = 0; i < num; i++)
+ g_free(uuid_strs[i]);
+ g_free(uuid_strs);
+}
+
+static char **new_uuid_strings(struct queue *allowlist, gsize *num)
+{
+ const struct queue_entry *entry = NULL;
+ bt_uuid_t *uuid = NULL;
+ char **uuid_strs = NULL;
+ gsize i = 0, allowlist_num;
+
+ /* Set num to a non-zero number so that whoever call this could know if
+ * this function success or not
+ */
+ *num = 1;
+
+ allowlist_num = queue_length(allowlist);
+ uuid_strs = g_try_malloc_n(allowlist_num, sizeof(char *));
+ if (!uuid_strs)
+ return NULL;
+
+ for (entry = queue_get_entries(allowlist); entry != NULL;
+ entry = entry->next) {
+ uuid = entry->data;
+ uuid_strs[i] = g_try_malloc0(MAX_LEN_UUID_STR * sizeof(char));
+
+ if (!uuid_strs[i])
+ goto failed;
+
+ bt_uuid_to_string(uuid, uuid_strs[i], MAX_LEN_UUID_STR);
+ i++;
+ }
+
+ *num = allowlist_num;
+ return uuid_strs;
+
+failed:
+ free_uuid_strings(uuid_strs, i);
+
+ return NULL;
+}
+
+static void store_policy_settings(struct btd_admin_policy *admin_policy)
+{
+ GKeyFile *key_file = NULL;
+ char *filename = ADMIN_POLICY_STORAGE;
+ char *key_file_data = NULL;
+ char **uuid_strs = NULL;
+ gsize length, num_uuids;
+
+ key_file = g_key_file_new();
+
+ uuid_strs = new_uuid_strings(admin_policy->service_allowlist,
+ &num_uuids);
+
+ if (!uuid_strs && num_uuids) {
+ btd_error(admin_policy->adapter_id,
+ "Failed to allocate uuid strings");
+ goto failed;
+ }
+
+ g_key_file_set_string_list(key_file, "General", "ServiceAllowlist",
+ (const gchar * const *)uuid_strs,
+ num_uuids);
+
+ if (create_file(ADMIN_POLICY_STORAGE, 0600) < 0) {
+ btd_error(admin_policy->adapter_id, "create %s failed, %s",
+ filename, strerror(errno));
+ goto failed;
+ }
+
+ key_file_data = g_key_file_to_data(key_file, &length, NULL);
+ g_file_set_contents(ADMIN_POLICY_STORAGE, key_file_data, length, NULL);
+
+ g_free(key_file_data);
+ free_uuid_strings(uuid_strs, num_uuids);
+
+failed:
+ g_key_file_free(key_file);
+}
+
+static void key_file_load_service_allowlist(GKeyFile *key_file,
+ struct btd_admin_policy *admin_policy)
+{
+ GError *gerr = NULL;
+ struct queue *uuid_list = NULL;
+ gchar **uuids = NULL;
+ gsize num, i;
+
+ uuids = g_key_file_get_string_list(key_file, "General",
+ "ServiceAllowlist", &num, &gerr);
+
+ if (gerr) {
+ btd_error(admin_policy->adapter_id,
+ "Failed to load ServiceAllowlist");
+ g_error_free(gerr);
+ return;
+ }
+
+ uuid_list = queue_new();
+ for (i = 0; i < num; i++) {
+ bt_uuid_t *uuid = g_try_malloc(sizeof(*uuid));
+
+ if (!uuid)
+ goto failed;
+
+ if (bt_string_to_uuid(uuid, *uuids)) {
+
+ btd_error(admin_policy->adapter_id,
+ "Failed to convert '%s' to uuid struct",
+ *uuids);
+
+ g_free(uuid);
+ goto failed;
+ }
+
+ queue_push_tail(uuid_list, uuid);
+ uuids++;
+ }
+
+ if (!service_allowlist_set(admin_policy, uuid_list))
+ goto failed;
+
+ return;
+failed:
+ free_service_allowlist(uuid_list);
+}
+
+static void load_policy_settings(struct btd_admin_policy *admin_policy)
+{
+ GKeyFile *key_file;
+ char *filename = ADMIN_POLICY_STORAGE;
+ struct stat st;
+
+ if (stat(filename, &st) < 0) {
+ btd_error(admin_policy->adapter_id,
+ "Failed to get file %s information",
+ filename);
+ return;
+ }
+
+ key_file = g_key_file_new();
+
+ g_key_file_load_from_file(key_file, filename, 0, NULL);
+
+ key_file_load_service_allowlist(key_file, admin_policy);
+
+ g_key_file_free(key_file);
+}
+
static DBusMessage *set_service_allowlist(DBusConnection *conn,
DBusMessage *msg, void *user_data)
{
@@ -179,7 +339,9 @@ static DBusMessage *set_service_allowlist(DBusConnection *conn,
return btd_error_invalid_args(msg);
}
- if (!service_allowlist_set(admin_policy, uuid_list)) {
+ if (service_allowlist_set(admin_policy, uuid_list)) {
+ store_policy_settings(admin_policy);
+ } else {
free_service_allowlist(uuid_list);
return btd_error_failed(msg, "service_allowlist_set failed");
}
@@ -305,6 +467,7 @@ static int admin_policy_adapter_probe(struct btd_adapter *adapter)
if (!policy_data)
return -ENOMEM;
+ load_policy_settings(policy_data);
adapter_path = adapter_get_path(adapter);
if (!g_dbus_register_interface(dbus_conn, adapter_path,