Linux-Bluetooth Archive on lore.kernel.org
 help / color / Atom feed
From: Inga Stotland <inga.stotland@intel.com>
To: linux-bluetooth@vger.kernel.org
Cc: brian.gix@intel.com, michal.lowas-rzechonek@silvair.com,
	jakub.witowski@silvair.com,
	Inga Stotland <inga.stotland@intel.com>
Subject: [PATCH BlueZ 09/10 v2] mesh: Create or re-use a node storage directory for keyring
Date: Sat, 13 Jul 2019 23:28:11 -0700
Message-ID: <20190714062812.31041-10-inga.stotland@intel.com> (raw)
In-Reply-To: <20190714062812.31041-1-inga.stotland@intel.com>

This adds APIs to set a directory for storing node's key ring info.
The directory is named MESH_STORAGE_DIR/<node uuid>. This directory
may contain additional node info plus node configuration, if node
configuration storage follows the same layout (as it does when
JSON based config file format is used).
---
 mesh/keyring.c | 36 ++++++++++++++++++------------------
 mesh/node.c    | 41 ++++++++++++++++++++++++++++++++---------
 mesh/node.h    |  4 ++--
 3 files changed, 52 insertions(+), 29 deletions(-)

diff --git a/mesh/keyring.c b/mesh/keyring.c
index 4c6d2986d..3ea83194c 100644
--- a/mesh/keyring.c
+++ b/mesh/keyring.c
@@ -43,7 +43,7 @@ const char *net_key_dir = "/net_keys";
 bool keyring_put_net_key(struct mesh_node *node, uint16_t net_idx,
 						struct keyring_net_key *key)
 {
-	char *node_path;
+	const char *node_path;
 	char key_file[PATH_MAX];
 	bool result = false;
 	int fd;
@@ -51,7 +51,7 @@ bool keyring_put_net_key(struct mesh_node *node, uint16_t net_idx,
 	if (!node || !key)
 		return false;
 
-	node_path = node_path_get(node);
+	node_path = node_get_storage_dir(node);
 
 	if (strlen(node_path) + strlen(net_key_dir) + 1 + 3 >= PATH_MAX)
 		return false;
@@ -76,7 +76,7 @@ bool keyring_put_net_key(struct mesh_node *node, uint16_t net_idx,
 bool keyring_put_app_key(struct mesh_node *node, uint16_t app_idx,
 				uint16_t net_idx, struct keyring_app_key *key)
 {
-	char *node_path;
+	const char *node_path;
 	char key_file[PATH_MAX];
 	bool result = false;
 	int fd;
@@ -84,7 +84,7 @@ bool keyring_put_app_key(struct mesh_node *node, uint16_t app_idx,
 	if (!node || !key)
 		return false;
 
-	node_path = node_path_get(node);
+	node_path = node_get_storage_dir(node);
 
 	if (strlen(node_path) + strlen(app_key_dir) + 1 + 3 >= PATH_MAX)
 		return false;
@@ -123,7 +123,7 @@ bool keyring_put_app_key(struct mesh_node *node, uint16_t app_idx,
 bool keyring_put_remote_dev_key(struct mesh_node *node, uint16_t unicast,
 					uint8_t count, uint8_t dev_key[16])
 {
-	char *node_path;
+	const char *node_path;
 	char key_file[PATH_MAX];
 	bool result = true;
 	int fd, i;
@@ -131,7 +131,7 @@ bool keyring_put_remote_dev_key(struct mesh_node *node, uint16_t unicast,
 	if (!node)
 		return false;
 
-	node_path = node_path_get(node);
+	node_path = node_get_storage_dir(node);
 
 	if (strlen(node_path) + strlen(dev_key_dir) + 1 + 4 >= PATH_MAX)
 		return false;
@@ -161,7 +161,7 @@ bool keyring_put_remote_dev_key(struct mesh_node *node, uint16_t unicast,
 bool keyring_get_net_key(struct mesh_node *node, uint16_t net_idx,
 						struct keyring_net_key *key)
 {
-	char *node_path;
+	const char *node_path;
 	char key_file[PATH_MAX];
 	bool result = false;
 	int fd;
@@ -169,7 +169,7 @@ bool keyring_get_net_key(struct mesh_node *node, uint16_t net_idx,
 	if (!node || !key)
 		return false;
 
-	node_path = node_path_get(node);
+	node_path = node_get_storage_dir(node);
 	snprintf(key_file, PATH_MAX, "%s%s/%3.3x", node_path, net_key_dir,
 								net_idx);
 
@@ -187,7 +187,7 @@ bool keyring_get_net_key(struct mesh_node *node, uint16_t net_idx,
 bool keyring_get_app_key(struct mesh_node *node, uint16_t app_idx,
 						struct keyring_app_key *key)
 {
-	char *node_path;
+	const char *node_path;
 	char key_file[PATH_MAX];
 	bool result = false;
 	int fd;
@@ -195,7 +195,7 @@ bool keyring_get_app_key(struct mesh_node *node, uint16_t app_idx,
 	if (!node || !key)
 		return false;
 
-	node_path = node_path_get(node);
+	node_path = node_get_storage_dir(node);
 	snprintf(key_file, PATH_MAX, "%s%s/%3.3x", node_path, app_key_dir,
 								app_idx);
 
@@ -213,7 +213,7 @@ bool keyring_get_app_key(struct mesh_node *node, uint16_t app_idx,
 bool keyring_get_remote_dev_key(struct mesh_node *node, uint16_t unicast,
 							uint8_t dev_key[16])
 {
-	char *node_path;
+	const char *node_path;
 	char key_file[PATH_MAX];
 	bool result = false;
 	int fd;
@@ -221,7 +221,7 @@ bool keyring_get_remote_dev_key(struct mesh_node *node, uint16_t unicast,
 	if (!node)
 		return false;
 
-	node_path = node_path_get(node);
+	node_path = node_get_storage_dir(node);
 	snprintf(key_file, PATH_MAX, "%s%s/%4.4x", node_path, dev_key_dir,
 								unicast);
 
@@ -238,13 +238,13 @@ bool keyring_get_remote_dev_key(struct mesh_node *node, uint16_t unicast,
 
 bool keyring_del_net_key(struct mesh_node *node, uint16_t net_idx)
 {
-	char *node_path;
+	const char *node_path;
 	char key_file[PATH_MAX];
 
 	if (!node)
 		return false;
 
-	node_path = node_path_get(node);
+	node_path = node_get_storage_dir(node);
 	snprintf(key_file, PATH_MAX, "%s%s/%3.3x", node_path, net_key_dir,
 								net_idx);
 	l_debug("RM Net Key %s", key_file);
@@ -258,13 +258,13 @@ bool keyring_del_net_key(struct mesh_node *node, uint16_t net_idx)
 
 bool keyring_del_app_key(struct mesh_node *node, uint16_t app_idx)
 {
-	char *node_path;
+	const char *node_path;
 	char key_file[PATH_MAX];
 
 	if (!node)
 		return false;
 
-	node_path = node_path_get(node);
+	node_path = node_get_storage_dir(node);
 	snprintf(key_file, PATH_MAX, "%s%s/%3.3x", node_path, app_key_dir,
 								app_idx);
 	l_debug("RM App Key %s", key_file);
@@ -276,14 +276,14 @@ bool keyring_del_app_key(struct mesh_node *node, uint16_t app_idx)
 bool keyring_del_remote_dev_key(struct mesh_node *node, uint16_t unicast,
 								uint8_t count)
 {
-	char *node_path;
+	const char *node_path;
 	char key_file[PATH_MAX];
 	int i;
 
 	if (!node)
 		return false;
 
-	node_path = node_path_get(node);
+	node_path = node_get_storage_dir(node);
 	for (i = 0; i < count; i++) {
 		snprintf(key_file, PATH_MAX, "%s%s/%4.4x", node_path,
 						dev_key_dir, unicast + i);
diff --git a/mesh/node.c b/mesh/node.c
index decd3d6fc..0f4569e36 100644
--- a/mesh/node.c
+++ b/mesh/node.c
@@ -22,6 +22,8 @@
 #endif
 
 #define _GNU_SOURCE
+#include <dirent.h>
+#include <stdio.h>
 
 #include <sys/time.h>
 
@@ -81,7 +83,7 @@ struct mesh_node {
 	char *owner;
 	char *path;
 	struct mesh_config *cfg;
-	char *node_path;
+	char *storage_dir;
 	uint32_t disc_watch;
 	time_t upd_sec;
 	uint32_t seq_number;
@@ -267,6 +269,7 @@ static void free_node_resources(void *data)
 
 	mesh_net_free(node->net);
 	l_free(node->comp);
+	l_free(node->storage_dir);
 	l_free(node);
 }
 
@@ -1511,6 +1514,29 @@ static bool add_local_node(struct mesh_node *node, uint16_t unicast, bool kr,
 	return true;
 }
 
+static bool init_storage_dir(struct mesh_node *node)
+{
+	char uuid[33];
+	char dir_name[PATH_MAX];
+
+	if (node->storage_dir)
+		return true;
+
+	if (!hex2str(node->uuid, 16, uuid, sizeof(uuid)))
+		return false;
+
+	snprintf(dir_name, PATH_MAX, "%s/%s", mesh_get_storage_dir(), uuid);
+
+	if (strlen(dir_name) >= PATH_MAX)
+		return false;
+
+	create_dir(dir_name);
+
+	node->storage_dir = l_strdup(dir_name);
+
+	return true;
+}
+
 static void get_managed_objects_cb(struct l_dbus_message *msg, void *user_data)
 {
 	struct l_dbus_message_iter objects, interfaces;
@@ -1663,6 +1689,9 @@ static void get_managed_objects_cb(struct l_dbus_message *msg, void *user_data)
 						net_key.old_key))
 			goto fail;
 
+		/* Initialize directory for storing keyring info */
+		init_storage_dir(node);
+
 		if (!keyring_put_remote_dev_key(node, DEFAULT_NEW_UNICAST,
 							num_ele, dev_key))
 			goto fail;
@@ -2063,15 +2092,9 @@ struct mesh_config *node_config_get(struct mesh_node *node)
 	return node->cfg;
 }
 
-void node_path_set(struct mesh_node *node, char *path)
-{
-	l_free(node->node_path);
-	node->node_path = l_strdup(path);
-}
-
-char *node_path_get(struct mesh_node *node)
+const char *node_get_storage_dir(struct mesh_node *node)
 {
-	return node->node_path;
+	return node->storage_dir;
 }
 
 struct mesh_net *node_get_net(struct mesh_node *node)
diff --git a/mesh/node.h b/mesh/node.h
index 17c89da26..baa0eb315 100644
--- a/mesh/node.h
+++ b/mesh/node.h
@@ -98,6 +98,6 @@ uint16_t node_id_get(struct mesh_node *node);
 bool node_dbus_init(struct l_dbus *bus);
 void node_cleanup_all(void);
 struct mesh_config *node_config_get(struct mesh_node *node);
-void node_path_set(struct mesh_node *node, char *path);
-char *node_path_get(struct mesh_node *node);
+const char *node_get_storage_dir(struct mesh_node *node);
+bool node_init_storage_dir(struct mesh_node *node);
 bool node_load_from_storage(const char *storage_dir);
-- 
2.21.0


  parent reply index

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-14  6:28 [PATCH BlueZ 00/10 v2] mesh: Configuration storage re-org Inga Stotland
2019-07-14  6:28 ` [PATCH BlueZ 01/10 v2] mesh: Move network config setup from storage.c to node.c Inga Stotland
2019-07-14  6:28 ` [PATCH BlueZ 02/10 v2] mesh: Rename mesh-db.c to mesh-config-json.c Inga Stotland
2019-07-14  6:28 ` [PATCH BlueZ 03/10 v2] mesh: Change mesh_db prefix to mesh_config Inga Stotland
2019-07-14  6:28 ` [PATCH BlueZ 04/10 v2] mesh: Move load from storage functionality into node.c Inga Stotland
2019-07-14  6:28 ` [PATCH BlueZ 05/10 v2] mesh: Confine dependency on json-c to mesh-config-json.c Inga Stotland
2019-07-14  6:28 ` [PATCH BlueZ 06/10 v2] mesh: Replace storage_save_config with mesh_config_save_config Inga Stotland
2019-07-14  6:28 ` [PATCH BlueZ 07/10 v2] mesh: Use mesh_config APIs to store node configuration Inga Stotland
2019-07-14  6:28 ` [PATCH BlueZ 08/10 v2] mesh: Manage node config directory in mesh-config Inga Stotland
2019-07-14  6:28 ` Inga Stotland [this message]
2019-07-14  6:28 ` [PATCH BlueZ 10/10 v2] mesh: Rename mesh_config_srv_init() to cfgmod_server_init() Inga Stotland

Reply instructions:

You may reply publically to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190714062812.31041-10-inga.stotland@intel.com \
    --to=inga.stotland@intel.com \
    --cc=brian.gix@intel.com \
    --cc=jakub.witowski@silvair.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=michal.lowas-rzechonek@silvair.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux-Bluetooth Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-bluetooth/0 linux-bluetooth/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-bluetooth linux-bluetooth/ https://lore.kernel.org/linux-bluetooth \
		linux-bluetooth@vger.kernel.org linux-bluetooth@archiver.kernel.org
	public-inbox-index linux-bluetooth


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-bluetooth


AGPL code for this site: git clone https://public-inbox.org/ public-inbox