[PATCH] Add messaging_init_client() function

Jeremy Allison jra at samba.org
Mon Nov 14 19:21:54 UTC 2016


On Mon, Nov 14, 2016 at 12:16:43PM +0100, Andreas Schneider wrote:
> 
> We can also just have messaging_init() return NTSTATUS and in case of 
> NT_STATUS_ACCESS_DENIED do not fail ...

Here is a version that does exacatly that (well it also checks
geteuid() != 0 && NT_STATUS_ACCESS_DENIED before ignoring the
fail).

If you're happy please +1 and push.

Cheers,

	Jeremy.
-------------- next part --------------
From 55475ea28e72b494387057444f685d0b074337bf Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn at samba.org>
Date: Mon, 14 Nov 2016 11:31:20 +0100
Subject: [PATCH 1/6] lib:util: Don't print lstat warning on ERROR debug level

If we are a client and can't access the lock directory don't confuse a
user.

Signed-off-by: Andreas Schneider <asn at samba.org>
Reviewed-by: Jeremy Allison <jra at samba.org>
---
 lib/util/util.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/util/util.c b/lib/util/util.c
index a8bbc15..56056a3 100644
--- a/lib/util/util.c
+++ b/lib/util/util.c
@@ -202,8 +202,8 @@ _PUBLIC_ bool directory_create_or_exist(const char *dname,
 	}
 
 	if (errno != ENOENT) {
-		DEBUG(0, ("lstat failed on directory %s: %s\n",
-			  dname, strerror(errno)));
+		DBG_WARNING("lstat failed on directory %s: %s\n",
+			    dname, strerror(errno));
 		return false;
 	}
 
-- 
2.8.0.rc3.226.g39d4020


From a5f6f03b6c95aebe1cfb9ef213bcbf9ce921ab08 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn at samba.org>
Date: Mon, 14 Nov 2016 09:42:51 +0100
Subject: [PATCH 2/6] s3:messaging: Create an messaging_init_internal()
 returning NTSTATUS

Signed-off-by: Andreas Schneider <asn at samba.org>
Reviewed-by: Jeremy Allison <jra at samba.org>
---
 source3/lib/messages.c | 116 ++++++++++++++++++++++++++++++++-----------------
 1 file changed, 75 insertions(+), 41 deletions(-)

diff --git a/source3/lib/messages.c b/source3/lib/messages.c
index 08942a6..881847b 100644
--- a/source3/lib/messages.c
+++ b/source3/lib/messages.c
@@ -234,67 +234,77 @@ static const char *private_path(const char *name)
 	return talloc_asprintf(talloc_tos(), "%s/%s", lp_private_dir(), name);
 }
 
-struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, 
-					 struct tevent_context *ev)
+static NTSTATUS messaging_init_internal(TALLOC_CTX *mem_ctx,
+					struct tevent_context *ev,
+					struct messaging_context **pmsg_ctx)
 {
+	TALLOC_CTX *frame;
 	struct messaging_context *ctx;
+	NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
 	int ret;
 	const char *lck_path;
 	const char *priv_path;
 	bool ok;
 
-	if (!(ctx = talloc_zero(mem_ctx, struct messaging_context))) {
-		return NULL;
-	}
-
-	ctx->id = (struct server_id) {
-		.pid = getpid(), .vnn = NONCLUSTER_VNN
-	};
-
-	ctx->event_ctx = ev;
-
-	sec_init();
-
 	lck_path = lock_path("msg.lock");
 	if (lck_path == NULL) {
-		TALLOC_FREE(ctx);
-		return NULL;
+		return NT_STATUS_NO_MEMORY;
 	}
 
-	ok = directory_create_or_exist_strict(lck_path, sec_initial_uid(),
+	ok = directory_create_or_exist_strict(lck_path,
+					      sec_initial_uid(),
 					      0755);
 	if (!ok) {
-		DEBUG(10, ("%s: Could not create lock directory: %s\n",
-			   __func__, strerror(errno)));
-		TALLOC_FREE(ctx);
-		return NULL;
+		DBG_DEBUG("Could not create lock directory: %s\n",
+			  strerror(errno));
+		return NT_STATUS_ACCESS_DENIED;
 	}
 
 	priv_path = private_path("msg.sock");
 	if (priv_path == NULL) {
-		TALLOC_FREE(ctx);
-		return NULL;
+		return NT_STATUS_NO_MEMORY;
 	}
 
 	ok = directory_create_or_exist_strict(priv_path, sec_initial_uid(),
 					      0700);
 	if (!ok) {
-		DEBUG(10, ("%s: Could not create msg directory: %s\n",
-			   __func__, strerror(errno)));
-		TALLOC_FREE(ctx);
-		return NULL;
+		DBG_DEBUG("Could not create msg directory: %s\n",
+			  strerror(errno));
+		return NT_STATUS_ACCESS_DENIED;
+	}
+
+	frame = talloc_stackframe();
+	if (frame == NULL) {
+		return NT_STATUS_NO_MEMORY;
+	}
+
+	ctx = talloc_zero(frame, struct messaging_context);
+	if (ctx == NULL) {
+		status = NT_STATUS_NO_MEMORY;
+		goto done;
 	}
 
-	ctx->msg_dgm_ref = messaging_dgm_ref(
-		ctx, ctx->event_ctx, &ctx->id.unique_id,
-		priv_path, lck_path, messaging_recv_cb, ctx, &ret);
+	ctx->id = (struct server_id) {
+		.pid = getpid(), .vnn = NONCLUSTER_VNN
+	};
+
+	ctx->event_ctx = ev;
 
+	sec_init();
+
+	ctx->msg_dgm_ref = messaging_dgm_ref(ctx,
+					     ctx->event_ctx,
+					     &ctx->id.unique_id,
+					     priv_path,
+					     lck_path,
+					     messaging_recv_cb,
+					     ctx,
+					     &ret);
 	if (ctx->msg_dgm_ref == NULL) {
 		DEBUG(2, ("messaging_dgm_ref failed: %s\n", strerror(ret)));
-		TALLOC_FREE(ctx);
-		return NULL;
+		status = NT_STATUS_INTERNAL_ERROR;
+		goto done;
 	}
-
 	talloc_set_destructor(ctx, messaging_context_destructor);
 
 	if (lp_clustering()) {
@@ -303,19 +313,21 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
 		if (ret != 0) {
 			DEBUG(2, ("messaging_ctdbd_init failed: %s\n",
 				  strerror(ret)));
-			TALLOC_FREE(ctx);
-			return NULL;
+			status = NT_STATUS_INTERNAL_ERROR;
+			goto done;
 		}
 	}
 	ctx->id.vnn = get_my_vnn();
 
-	ctx->names_db = server_id_db_init(
-		ctx, ctx->id, lp_lock_directory(), 0,
-		TDB_INCOMPATIBLE_HASH|TDB_CLEAR_IF_FIRST);
+	ctx->names_db = server_id_db_init(ctx,
+					  ctx->id,
+					  lp_lock_directory(),
+					  0,
+					  TDB_INCOMPATIBLE_HASH|TDB_CLEAR_IF_FIRST);
 	if (ctx->names_db == NULL) {
-		DEBUG(10, ("%s: server_id_db_init failed\n", __func__));
-		TALLOC_FREE(ctx);
-		return NULL;
+		DBG_DEBUG("server_id_db_init failed\n");
+		status = NT_STATUS_INTERNAL_ERROR;
+		goto done;
 	}
 
 	messaging_register(ctx, NULL, MSG_PING, ping_message);
@@ -331,6 +343,28 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
 		DBG_DEBUG("my id: %s\n", server_id_str_buf(ctx->id, &tmp));
 	}
 
+	*pmsg_ctx = talloc_steal(mem_ctx, ctx);
+
+	status = NT_STATUS_OK;
+done:
+	TALLOC_FREE(frame);
+
+	return status;
+}
+
+struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
+					 struct tevent_context *ev)
+{
+	struct messaging_context *ctx = NULL;
+	NTSTATUS status;
+
+	status = messaging_init_internal(mem_ctx,
+					 ev,
+					 &ctx);
+	if (!NT_STATUS_IS_OK(status)) {
+		return NULL;
+	}
+
 	return ctx;
 }
 
-- 
2.8.0.rc3.226.g39d4020


From 17d735ae4f1c9e2460919fb56b4c9af1473fbd5d Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn at samba.org>
Date: Mon, 14 Nov 2016 09:49:20 +0100
Subject: [PATCH 3/6] s3:messaging: Add messaging_init_client() function

Signed-off-by: Andreas Schneider <asn at samba.org>
Reviewed-by: Jeremy Allison <jra at samba.org>
---
 source3/include/messages.h | 3 +++
 source3/lib/messages.c     | 9 +++++++++
 2 files changed, 12 insertions(+)

diff --git a/source3/include/messages.h b/source3/include/messages.h
index 1b4e2f6..ea89383 100644
--- a/source3/include/messages.h
+++ b/source3/include/messages.h
@@ -75,6 +75,9 @@ struct ctdbd_connection *messaging_ctdbd_connection(void);
 
 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, 
 					 struct tevent_context *ev);
+NTSTATUS messaging_init_client(TALLOC_CTX *mem_ctx,
+			       struct tevent_context *ev,
+			       struct messaging_context **pmsg_ctx);
 
 struct server_id messaging_server_id(const struct messaging_context *msg_ctx);
 struct tevent_context *messaging_tevent_context(
diff --git a/source3/lib/messages.c b/source3/lib/messages.c
index 881847b..505eb66 100644
--- a/source3/lib/messages.c
+++ b/source3/lib/messages.c
@@ -368,6 +368,15 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
 	return ctx;
 }
 
+NTSTATUS messaging_init_client(TALLOC_CTX *mem_ctx,
+			       struct tevent_context *ev,
+			       struct messaging_context **pmsg_ctx)
+{
+	return messaging_init_internal(mem_ctx,
+					ev,
+					pmsg_ctx);
+}
+
 struct server_id messaging_server_id(const struct messaging_context *msg_ctx)
 {
 	return msg_ctx->id;
-- 
2.8.0.rc3.226.g39d4020


From cbb4d5cd525ba2ea61cce24f2414f7d1edcaa293 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn at samba.org>
Date: Mon, 14 Nov 2016 09:51:52 +0100
Subject: [PATCH 4/6] s3:rpcclient: Use messaging_init_client()

Signed-off-by: Andreas Schneider <asn at samba.org>
Signed-off-by: Jeremy Allison <jra at samba.org>
---
 source3/rpcclient/rpcclient.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/source3/rpcclient/rpcclient.c b/source3/rpcclient/rpcclient.c
index 554620b..ff41e53 100644
--- a/source3/rpcclient/rpcclient.c
+++ b/source3/rpcclient/rpcclient.c
@@ -1030,9 +1030,19 @@ out_free:
 		goto done;
 	}
 
-	rpcclient_msg_ctx = messaging_init(talloc_autofree_context(),
-			samba_tevent_context_init(talloc_autofree_context()));
-	if (rpcclient_msg_ctx == NULL) {
+	nt_status = messaging_init_client(talloc_autofree_context(),
+					  samba_tevent_context_init(talloc_autofree_context()),
+					  &rpcclient_msg_ctx);
+	if (geteuid() != 0 &&
+			NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
+		/*
+		 * Normal to fail to initialize messaging context
+		 * if we're not root as we don't have ability to
+		 * read lock directory.
+		 */
+		DBG_NOTICE("Unable to initialize messaging context. "
+			"Must be root to do that.\n");
+	} else if (!NT_STATUS_IS_OK(nt_status)) {
 		fprintf(stderr, "Could not init messaging context\n");
 		result = 1;
 		goto done;
-- 
2.8.0.rc3.226.g39d4020


From d959864c9f9a8d62e0d18d7723d5b36c2404f057 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn at samba.org>
Date: Mon, 14 Nov 2016 11:36:03 +0100
Subject: [PATCH 5/6] s3:rpcclient: Print a new line on exit

If you press 'ctrl+d' print a new line for the shell.

Signed-off-by: Andreas Schneider <asn at samba.org>
Reviewed-by: Jeremy Allison <jra at samba.org>
---
 source3/rpcclient/rpcclient.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/source3/rpcclient/rpcclient.c b/source3/rpcclient/rpcclient.c
index ff41e53..930cab3 100644
--- a/source3/rpcclient/rpcclient.c
+++ b/source3/rpcclient/rpcclient.c
@@ -1252,8 +1252,10 @@ out_free:
 
 		line = smb_readline("rpcclient $> ", NULL, completion_fn);
 
-		if (line == NULL)
+		if (line == NULL) {
+			printf("\n");
 			break;
+		}
 
 		if (line[0] != '\n')
 			process_cmd(rpcclient_auth_info, cli, binding, line);
-- 
2.8.0.rc3.226.g39d4020


From 27d98ae51dca2446c671245d30624227a086bef0 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn at samba.org>
Date: Mon, 14 Nov 2016 09:54:53 +0100
Subject: [PATCH 6/6] s3:net: Use messaging_init_client()

Signed-off-by: Andreas Schneider <asn at samba.org>
Signed-off-by: Jeremy Allison <jra at samba.org>
---
 source3/utils/net.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/source3/utils/net.c b/source3/utils/net.c
index 3d0940d..beb8760 100644
--- a/source3/utils/net.c
+++ b/source3/utils/net.c
@@ -786,6 +786,7 @@ static struct functable net_func[] = {
 	poptContext pc;
 	TALLOC_CTX *frame = talloc_stackframe();
 	struct net_context *c = talloc_zero(frame, struct net_context);
+	NTSTATUS status;
 
 	struct poptOption long_options[] = {
 		{"help",	'h', POPT_ARG_NONE,   0, 'h'},
@@ -905,11 +906,22 @@ static struct functable net_func[] = {
 		exit(1);
 	}
 
-	/*
-	 * Failing to init the msg_ctx isn't a fatal error. Only root-level
-	 * things (joining/leaving domains etc.) will be denied.
-	 */
-	c->msg_ctx = messaging_init(c, samba_tevent_context_init(c));
+	status = messaging_init_client(c,
+				       samba_tevent_context_init(c),
+				       &c->msg_ctx);
+	if (geteuid() != 0 &&
+			NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
+		/*
+		 * Normal to fail to initialize messaging context
+		 * if we're not root as we don't have ability to
+		 * read lock directory.
+		 */
+		DBG_NOTICE("Unable to initialize messaging context. "
+			"Must be root to do that.\n");
+	} else if (!NT_STATUS_IS_OK(status)) {
+		d_fprintf(stderr, "Failed to init messaging context\n");
+		exit(1);
+	}
 
 	if (!lp_load_global(get_dyn_CONFIGFILE())) {
 		d_fprintf(stderr, "Can't load %s - run testparm to debug it\n",
-- 
2.8.0.rc3.226.g39d4020



More information about the samba-technical mailing list