]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
sharesec: Add function to check existence of share from config
authorVinit Agnihotri <vagnihot@redhat.com>
Tue, 3 Dec 2024 05:49:09 +0000 (11:19 +0530)
committerJule Anger <janger@samba.org>
Tue, 21 Jan 2025 07:38:18 +0000 (07:38 +0000)
Add function to detect if a share name exists in the registry or config file.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=15780

Signed-off-by: Vinit Agnihotri <vagnihot@redhat.com>
Reviewed-by: John Mulligan <jmulligan@samba.org>
Reviewed-by: Guenther Deschner <gd@samba.org>
(cherry picked from commit 78eb293e1cdd3635de0bcf46ffb9d842f27bcc9f)

source3/utils/sharesec.c

index 3c93893e17890a04abbc2548cb6ea1ea66a65401..6094746e3ccb80d83e74bdf3122d40c24d07d69c 100644 (file)
@@ -31,6 +31,8 @@ struct cli_state;
 #include "cmdline_contexts.h"
 #include "lib/util/string_wrappers.h"
 #include "lib/param/param.h"
+#include "lib/smbconf/smbconf.h"
+#include "lib/smbconf/smbconf_init.h"
 
 static TALLOC_CTX *ctx;
 
@@ -316,6 +318,80 @@ static int view_sharesec_sddl(const char *sharename)
        return 0;
 }
 
+static bool registry_share(const char *sharename)
+{
+       sbcErr err;
+       TALLOC_CTX *mem_ctx = talloc_stackframe();
+       struct smbconf_ctx *conf_ctx;
+       bool ret_status = true;
+
+       err = smbconf_init(mem_ctx, &conf_ctx, "registry:");
+       if (!SBC_ERROR_IS_OK(err)) {
+               DEBUG(0, ("Unable to init smbconf registry. Err:%s\n",
+                     sbcErrorString(err)));
+               ret_status = false;
+               goto out;
+       }
+
+       if(!smbconf_share_exists(conf_ctx, sharename)) {
+               ret_status = false;
+               goto done;
+       }
+done:
+       smbconf_shutdown(conf_ctx);
+out:
+       talloc_free(mem_ctx);
+       return ret_status;
+}
+
+static bool txt_share(const char *sharename)
+{
+       sbcErr err;
+       TALLOC_CTX *mem_ctx = talloc_stackframe();
+       struct smbconf_ctx *conf_ctx;
+       bool ret_status = true;
+       char *fconf_path;
+
+       fconf_path = talloc_asprintf(mem_ctx, "file:%s", get_dyn_CONFIGFILE());
+       if (fconf_path == NULL) {
+               DEBUG(0, ("Not enough memory for conf file path"));
+               ret_status = false;
+               goto out;
+       }
+
+       err = smbconf_init(mem_ctx, &conf_ctx, fconf_path);
+       if (!SBC_ERROR_IS_OK(err)) {
+               DEBUG(0, ("Unable to init smbconf file. Err:%s\n",
+                     sbcErrorString(err)));
+               ret_status = false;
+               goto out;
+       }
+
+       if(!smbconf_share_exists(conf_ctx, sharename)) {
+               ret_status = false;
+               goto done;
+       }
+done:
+       smbconf_shutdown(conf_ctx);
+out:
+       talloc_free(mem_ctx);
+       return ret_status;
+}
+
+static bool share_exists(const char *sharename)
+{
+       bool ret_status = false;
+
+       if ((lp_config_backend() == CONFIG_BACKEND_REGISTRY) ||
+           (lp_registry_shares() == true))
+               ret_status = registry_share(sharename);
+
+       if (!ret_status) {
+               ret_status = txt_share(sharename);
+       }
+       return ret_status;
+}
+
 /********************************************************************
   main program
 ********************************************************************/