lib/util: consolidate module loading into common code
[samba.git] / source4 / param / util.c
index 281d32f35f8ed9dd6c3d0de911ee77f3860e96c8..472096f60d295ccdd08b071a7165de82d09406db 100644 (file)
@@ -23,7 +23,7 @@
 */
 
 #include "includes.h"
-#include "dynconfig.h"
+#include "dynconfig/dynconfig.h"
 #include "system/network.h"
 #include "system/filesys.h"
 #include "system/dir.h"
  */
 
 
-_PUBLIC_ bool lp_is_mydomain(struct loadparm_context *lp_ctx, 
+bool lpcfg_is_mydomain(struct loadparm_context *lp_ctx,
                             const char *domain)
 {
-       return strequal(lp_workgroup(lp_ctx), domain);
+       return strequal(lpcfg_workgroup(lp_ctx), domain);
+}
+
+bool lpcfg_is_my_domain_or_realm(struct loadparm_context *lp_ctx,
+                            const char *domain)
+{
+       return strequal(lpcfg_workgroup(lp_ctx), domain) ||
+               strequal(lpcfg_realm(lp_ctx), domain);
 }
 
 /**
   see if a string matches either our primary or one of our secondary 
   netbios aliases. do a case insensitive match
 */
-_PUBLIC_ bool lp_is_myname(struct loadparm_context *lp_ctx, const char *name)
+bool lpcfg_is_myname(struct loadparm_context *lp_ctx, const char *name)
 {
        const char **aliases;
        int i;
 
-       if (strcasecmp(name, lp_netbios_name(lp_ctx)) == 0) {
+       if (strcasecmp(name, lpcfg_netbios_name(lp_ctx)) == 0) {
                return true;
        }
 
-       aliases = lp_netbios_aliases(lp_ctx);
+       aliases = lpcfg_netbios_aliases(lp_ctx);
        for (i=0; aliases && aliases[i]; i++) {
                if (strcasecmp(name, aliases[i]) == 0) {
                        return true;
@@ -68,7 +75,7 @@ _PUBLIC_ bool lp_is_myname(struct loadparm_context *lp_ctx, const char *name)
 /**
  A useful function for returning a path in the Samba lock directory.
 **/
-_PUBLIC_ char *lock_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
+char *lpcfg_lock_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
                         const char *name)
 {
        char *fname, *dname;
@@ -79,7 +86,7 @@ _PUBLIC_ char *lock_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
                return talloc_strdup(mem_ctx, name);
        }
 
-       dname = talloc_strdup(mem_ctx, lp_lockdir(lp_ctx));
+       dname = talloc_strdup(mem_ctx, lpcfg_lockdir(lp_ctx));
        trim_string(dname,"","/");
        
        if (!directory_exist(dname)) {
@@ -93,6 +100,62 @@ _PUBLIC_ char *lock_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
        return fname;
 }
 
+/**
+ A useful function for returning a path in the Samba state directory.
+**/
+char *lpcfg_state_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
+                      const char *name)
+{
+       char *fname, *dname;
+       if (name == NULL) {
+               return NULL;
+       }
+       if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
+               return talloc_strdup(mem_ctx, name);
+       }
+
+       dname = talloc_strdup(mem_ctx, lpcfg_statedir(lp_ctx));
+       trim_string(dname,"","/");
+
+       if (!directory_exist(dname)) {
+               mkdir(dname,0755);
+       }
+
+       fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
+
+       talloc_free(dname);
+
+       return fname;
+}
+
+/**
+ A useful function for returning a path in the Samba cache directory.
+**/
+char *lpcfg_cache_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
+                      const char *name)
+{
+       char *fname, *dname;
+       if (name == NULL) {
+               return NULL;
+       }
+       if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
+               return talloc_strdup(mem_ctx, name);
+       }
+
+       dname = talloc_strdup(mem_ctx, lpcfg_cachedir(lp_ctx));
+       trim_string(dname,"","/");
+
+       if (!directory_exist(dname)) {
+               mkdir(dname,0755);
+       }
+
+       fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
+
+       talloc_free(dname);
+
+       return fname;
+}
+
 /**
  * @brief Returns an absolute path to a file in the directory containing the current config file
  *
@@ -101,16 +164,24 @@ _PUBLIC_ char *lock_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
  * @retval Pointer to a talloc'ed string containing the full path.
  **/
 
-_PUBLIC_ char *config_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
+char *lpcfg_config_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
                           const char *name)
 {
        char *fname, *config_dir, *p;
-       config_dir = talloc_strdup(mem_ctx, lp_configfile(lp_ctx));
+       config_dir = talloc_strdup(mem_ctx, lpcfg_configfile(lp_ctx));
+       if (config_dir == NULL) {
+               config_dir = talloc_strdup(mem_ctx, lp_default_path());
+       }
        p = strrchr(config_dir, '/');
        if (p == NULL) {
-               return NULL;
+               talloc_free(config_dir);
+               config_dir = talloc_strdup(mem_ctx, ".");
+               if (config_dir == NULL) {
+                       return NULL;
+               }
+       } else {
+               p[0] = '\0';
        }
-       p[0] = '\0';
        fname = talloc_asprintf(mem_ctx, "%s/%s", config_dir, name);
        talloc_free(config_dir);
        return fname;
@@ -124,7 +195,7 @@ _PUBLIC_ char *config_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
  *
  * @retval Pointer to a talloc'ed string containing the full path.
  **/
-_PUBLIC_ char *private_path(TALLOC_CTX* mem_ctx, 
+char *lpcfg_private_path(TALLOC_CTX* mem_ctx,
                            struct loadparm_context *lp_ctx,
                            const char *name)
 {
@@ -135,7 +206,7 @@ _PUBLIC_ char *private_path(TALLOC_CTX* mem_ctx,
        if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
                return talloc_strdup(mem_ctx, name);
        }
-       fname = talloc_asprintf(mem_ctx, "%s/%s", lp_private_dir(lp_ctx), name);
+       fname = talloc_asprintf(mem_ctx, "%s/%s", lpcfg_private_dir(lp_ctx), name);
        return fname;
 }
 
@@ -144,13 +215,13 @@ _PUBLIC_ char *private_path(TALLOC_CTX* mem_ctx,
   for smbd go. If NULL is passed for name then return the directory 
   path itself
 */
-_PUBLIC_ char *smbd_tmp_path(TALLOC_CTX *mem_ctx, 
-                            struct loadparm_context *lp_ctx, 
+char *smbd_tmp_path(TALLOC_CTX *mem_ctx, 
+                            struct loadparm_context *lp_ctx,
                             const char *name)
 {
        char *fname, *dname;
 
-       dname = private_path(mem_ctx, lp_ctx, "smbd.tmp");
+       dname = lpcfg_private_path(mem_ctx, lp_ctx, "smbd.tmp");
        if (!directory_exist(dname)) {
                mkdir(dname,0755);
        }
@@ -165,142 +236,31 @@ _PUBLIC_ char *smbd_tmp_path(TALLOC_CTX *mem_ctx,
        return fname;
 }
 
-/**
- * Obtain the init function from a shared library file
- */
-_PUBLIC_ init_module_fn load_module(TALLOC_CTX *mem_ctx, const char *path)
-{
-       void *handle;
-       void *init_fn;
-
-       handle = dlopen(path, RTLD_NOW);
-       if (handle == NULL) {
-               DEBUG(0, ("Unable to open %s: %s\n", path, dlerror()));
-               return NULL;
-       }
-
-       init_fn = dlsym(handle, "init_module");
-
-       if (init_fn == NULL) {
-               DEBUG(0, ("Unable to find init_module() in %s: %s\n", path, dlerror()));
-               DEBUG(1, ("Loading module '%s' failed\n", path));
-               dlclose(handle);
-               return NULL;
-       }
-
-       return (init_module_fn)init_fn;
-}
-
-/**
- * Obtain list of init functions from the modules in the specified
- * directory
- */
-_PUBLIC_ init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path)
-{
-       DIR *dir;
-       struct dirent *entry;
-       char *filename;
-       int success = 0;
-       init_module_fn *ret = talloc_array(mem_ctx, init_module_fn, 2);
-
-       ret[0] = NULL;
-       
-       dir = opendir(path);
-       if (dir == NULL) {
-               talloc_free(ret);
-               return NULL;
-       }
-
-       while((entry = readdir(dir))) {
-               if (ISDOT(entry->d_name) || ISDOTDOT(entry->d_name))
-                       continue;
-
-               filename = talloc_asprintf(mem_ctx, "%s/%s", path, entry->d_name);
-
-               ret[success] = load_module(mem_ctx, filename);
-               if (ret[success]) {
-                       ret = talloc_realloc(mem_ctx, ret, init_module_fn, success+2);
-                       success++;
-                       ret[success] = NULL;
-               }
-
-               talloc_free(filename);
-       }
-
-       closedir(dir);
-
-       return ret;
-}
-
-/**
- * Run the specified init functions.
- *
- * @return true if all functions ran successfully, false otherwise
- */
-_PUBLIC_ bool run_init_functions(init_module_fn *fns)
-{
-       int i;
-       bool ret = true;
-       
-       if (fns == NULL)
-               return true;
-       
-       for (i = 0; fns[i]; i++) { ret &= (bool)NT_STATUS_IS_OK(fns[i]()); }
-
-       return ret;
-}
-
-static char *modules_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
-                         const char *name)
-{
-       const char *env_moduledir = getenv("LD_SAMBA_MODULE_PATH");
-       return talloc_asprintf(mem_ctx, "%s/%s", 
-                              env_moduledir?env_moduledir:lp_modulesdir(lp_ctx), 
-                              name);
-}
-
-/**
- * Load the initialization functions from DSO files for a specific subsystem.
- *
- * Will return an array of function pointers to initialization functions
- */
-
-_PUBLIC_ init_module_fn *load_samba_modules(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, const char *subsystem)
-{
-       char *path = modules_path(mem_ctx, lp_ctx, subsystem);
-       init_module_fn *ret;
-
-       ret = load_modules(mem_ctx, path);
-
-       talloc_free(path);
-
-       return ret;
-}
-
-_PUBLIC_ const char *lp_messaging_path(TALLOC_CTX *mem_ctx, 
+const char *lpcfg_imessaging_path(TALLOC_CTX *mem_ctx,
                                       struct loadparm_context *lp_ctx)
 {
-       return smbd_tmp_path(mem_ctx, lp_ctx, "messaging");
+       return smbd_tmp_path(mem_ctx, lp_ctx, "msg");
 }
 
-struct smb_iconv_convenience *global_smb_iconv_convenience = NULL;
-
-struct smb_iconv_convenience *lp_iconv_convenience(struct loadparm_context *lp_ctx)
+struct smb_iconv_handle *smb_iconv_handle_reinit_lp(TALLOC_CTX *mem_ctx,
+                                                             struct loadparm_context *lp_ctx,
+                                                             struct smb_iconv_handle *old_ic)
 {
-       return global_smb_iconv_convenience;
+       return smb_iconv_handle_reinit(mem_ctx, lpcfg_dos_charset(lp_ctx),
+                                      lpcfg_unix_charset(lp_ctx),
+                                      true,
+                                      old_ic);
 }
 
-struct smb_iconv_convenience *smb_iconv_convenience_init_lp(TALLOC_CTX *mem_ctx,
-                                                        struct loadparm_context *lp_ctx)
-{
-       return smb_iconv_convenience_init(mem_ctx, lp_dos_charset(lp_ctx),
-                                         lp_unix_charset(lp_ctx),
-                                         lp_display_charset(lp_ctx),
-               lp_parm_bool(lp_ctx, NULL, "iconv", "native", true));
-}
 
-_PUBLIC_ void reload_charcnv(void)
+const char *lpcfg_sam_name(struct loadparm_context *lp_ctx)
 {
-       talloc_free(global_smb_iconv_convenience);
-       global_smb_iconv_convenience = smb_iconv_convenience_init_lp(talloc_autofree_context(), global_loadparm);
+       switch (lpcfg_server_role(lp_ctx)) {
+       case ROLE_DOMAIN_BDC:
+       case ROLE_DOMAIN_PDC:
+               return lpcfg_workgroup(lp_ctx);
+       default:
+               return lpcfg_netbios_name(lp_ctx);
+       }
 }
+