lib/util: consolidate module loading into common code
[samba.git] / source4 / param / util.c
index 7af2693876caca3a3263bb38978b2196e545b3ef..472096f60d295ccdd08b071a7165de82d09406db 100644 (file)
@@ -6,6 +6,7 @@
    Copyright (C) Simo Sorce 2001
    Copyright (C) Jim McDonough (jmcd@us.ibm.com)  2003.
    Copyright (C) James J Myers 2003
+   Copyright (C) Jelmer Vernooij 2005-2007
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 */
 
 #include "includes.h"
-#include "dynconfig.h"
+#include "dynconfig/dynconfig.h"
 #include "system/network.h"
 #include "system/filesys.h"
+#include "system/dir.h"
+#include "param/param.h"
 
 /**
  * @file
  */
 
 
+bool lpcfg_is_mydomain(struct loadparm_context *lp_ctx,
+                            const char *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 is_myname(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()) == 0) {
-               return True;
+       if (strcasecmp(name, lpcfg_netbios_name(lp_ctx)) == 0) {
+               return true;
        }
 
-       aliases = lp_netbios_aliases();
+       aliases = lpcfg_netbios_aliases(lp_ctx);
        for (i=0; aliases && aliases[i]; i++) {
                if (strcasecmp(name, aliases[i]) == 0) {
-                       return True;
+                       return true;
                }
        }
 
-       return False;
+       return false;
 }
 
 
 /**
  A useful function for returning a path in the Samba lock directory.
 **/
-_PUBLIC_ char *lock_path(TALLOC_CTX* mem_ctx, const char *name)
+char *lpcfg_lock_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
+                        const char *name)
 {
        char *fname, *dname;
        if (name == NULL) {
@@ -69,7 +86,7 @@ _PUBLIC_ char *lock_path(TALLOC_CTX* mem_ctx, const char *name)
                return talloc_strdup(mem_ctx, name);
        }
 
-       dname = talloc_strdup(mem_ctx, lp_lockdir());
+       dname = talloc_strdup(mem_ctx, lpcfg_lockdir(lp_ctx));
        trim_string(dname,"","/");
        
        if (!directory_exist(dname)) {
@@ -83,21 +100,27 @@ _PUBLIC_ char *lock_path(TALLOC_CTX* mem_ctx, const char *name)
        return fname;
 }
 
-
 /**
- A useful function for returning a path in the Samba piddir directory.
+ A useful function for returning a path in the Samba state directory.
 **/
-static char *pid_path(TALLOC_CTX* mem_ctx, const char *name)
+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, lp_piddir());
+       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);
@@ -105,19 +128,31 @@ static char *pid_path(TALLOC_CTX* mem_ctx, const char *name)
        return fname;
 }
 
-
 /**
- * @brief Returns an absolute path to a file in the Samba lib directory.
- *
- * @param name File to find, relative to DATADIR.
- *
- * @retval Pointer to a talloc'ed string containing the full path.
- **/
-
-_PUBLIC_ char *data_path(TALLOC_CTX* mem_ctx, const char *name)
+ 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;
-       fname = talloc_asprintf(mem_ctx, "%s/%s", dyn_DATADIR, 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;
 }
 
@@ -129,15 +164,24 @@ _PUBLIC_ char *data_path(TALLOC_CTX* mem_ctx, const char *name)
  * @retval Pointer to a talloc'ed string containing the full path.
  **/
 
-_PUBLIC_ char *config_path(TALLOC_CTX* mem_ctx, const char *name)
+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());
+       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) {
-               return NULL;
+       if (p == 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;
@@ -151,7 +195,9 @@ _PUBLIC_ char *config_path(TALLOC_CTX* mem_ctx, const char *name)
  *
  * @retval Pointer to a talloc'ed string containing the full path.
  **/
-_PUBLIC_ char *private_path(TALLOC_CTX* mem_ctx, const char *name)
+char *lpcfg_private_path(TALLOC_CTX* mem_ctx,
+                           struct loadparm_context *lp_ctx,
+                           const char *name)
 {
        char *fname;
        if (name == NULL) {
@@ -160,7 +206,7 @@ _PUBLIC_ char *private_path(TALLOC_CTX* mem_ctx, const char *name)
        if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
                return talloc_strdup(mem_ctx, name);
        }
-       fname = talloc_asprintf(mem_ctx, "%s/%s", lp_private_dir(), name);
+       fname = talloc_asprintf(mem_ctx, "%s/%s", lpcfg_private_dir(lp_ctx), name);
        return fname;
 }
 
@@ -169,11 +215,13 @@ _PUBLIC_ char *private_path(TALLOC_CTX* mem_ctx, const char *name)
   for smbd go. If NULL is passed for name then return the directory 
   path itself
 */
-_PUBLIC_ char *smbd_tmp_path(TALLOC_CTX *mem_ctx, const char *name)
+char *smbd_tmp_path(TALLOC_CTX *mem_ctx, 
+                            struct loadparm_context *lp_ctx,
+                            const char *name)
 {
        char *fname, *dname;
 
-       dname = pid_path(mem_ctx, "smbd.tmp");
+       dname = lpcfg_private_path(mem_ctx, lp_ctx, "smbd.tmp");
        if (!directory_exist(dname)) {
                mkdir(dname,0755);
        }
@@ -188,30 +236,31 @@ _PUBLIC_ char *smbd_tmp_path(TALLOC_CTX *mem_ctx, const char *name)
        return fname;
 }
 
-static char *modules_path(TALLOC_CTX* mem_ctx, const char *name)
+const char *lpcfg_imessaging_path(TALLOC_CTX *mem_ctx,
+                                      struct loadparm_context *lp_ctx)
 {
-       const char *env_moduledir = getenv("LD_SAMBA_MODULE_PATH");
-       return talloc_asprintf(mem_ctx, "%s/%s", 
-                                                  env_moduledir?env_moduledir:lp_modulesdir(), 
-                                                  name);
+       return smbd_tmp_path(mem_ctx, lp_ctx, "msg");
 }
 
-/**
- * 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, const char *subsystem)
+struct smb_iconv_handle *smb_iconv_handle_reinit_lp(TALLOC_CTX *mem_ctx,
+                                                             struct loadparm_context *lp_ctx,
+                                                             struct smb_iconv_handle *old_ic)
 {
-       char *path = modules_path(mem_ctx, subsystem);
-       init_module_fn *ret;
-
-       ret = load_modules(mem_ctx, path);
+       return smb_iconv_handle_reinit(mem_ctx, lpcfg_dos_charset(lp_ctx),
+                                      lpcfg_unix_charset(lp_ctx),
+                                      true,
+                                      old_ic);
+}
 
-       talloc_free(path);
 
-       return ret;
+const char *lpcfg_sam_name(struct loadparm_context *lp_ctx)
+{
+       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);
+       }
 }
 
-