root/source4/param/util.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. lp_is_mydomain
  2. lp_is_myname
  3. lock_path
  4. config_path
  5. private_path
  6. smbd_tmp_path
  7. load_module
  8. load_modules
  9. run_init_functions
  10. modules_path
  11. load_samba_modules
  12. lp_messaging_path
  13. smb_iconv_convenience_init_lp

   1 /* 
   2    Unix SMB/CIFS implementation.
   3    Samba utility functions
   4    Copyright (C) Andrew Tridgell 1992-1998
   5    Copyright (C) Jeremy Allison 2001-2002
   6    Copyright (C) Simo Sorce 2001
   7    Copyright (C) Jim McDonough (jmcd@us.ibm.com)  2003.
   8    Copyright (C) James J Myers 2003
   9    Copyright (C) Jelmer Vernooij 2005-2007
  10    
  11    This program is free software; you can redistribute it and/or modify
  12    it under the terms of the GNU General Public License as published by
  13    the Free Software Foundation; either version 3 of the License, or
  14    (at your option) any later version.
  15    
  16    This program is distributed in the hope that it will be useful,
  17    but WITHOUT ANY WARRANTY; without even the implied warranty of
  18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19    GNU General Public License for more details.
  20    
  21    You should have received a copy of the GNU General Public License
  22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  23 */
  24 
  25 #include "includes.h"
  26 #include "dynconfig/dynconfig.h"
  27 #include "system/network.h"
  28 #include "system/filesys.h"
  29 #include "system/dir.h"
  30 #include "param/param.h"
  31 
  32 /**
  33  * @file
  34  * @brief Misc utility functions
  35  */
  36 
  37 
  38 bool lp_is_mydomain(struct loadparm_context *lp_ctx, 
     /* [<][>][^][v][top][bottom][index][help] */
  39                              const char *domain)
  40 {
  41         return strequal(lp_workgroup(lp_ctx), domain);
  42 }
  43 
  44 /**
  45   see if a string matches either our primary or one of our secondary 
  46   netbios aliases. do a case insensitive match
  47 */
  48 bool lp_is_myname(struct loadparm_context *lp_ctx, const char *name)
     /* [<][>][^][v][top][bottom][index][help] */
  49 {
  50         const char **aliases;
  51         int i;
  52 
  53         if (strcasecmp(name, lp_netbios_name(lp_ctx)) == 0) {
  54                 return true;
  55         }
  56 
  57         aliases = lp_netbios_aliases(lp_ctx);
  58         for (i=0; aliases && aliases[i]; i++) {
  59                 if (strcasecmp(name, aliases[i]) == 0) {
  60                         return true;
  61                 }
  62         }
  63 
  64         return false;
  65 }
  66 
  67 
  68 /**
  69  A useful function for returning a path in the Samba lock directory.
  70 **/
  71 char *lock_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
     /* [<][>][^][v][top][bottom][index][help] */
  72                          const char *name)
  73 {
  74         char *fname, *dname;
  75         if (name == NULL) {
  76                 return NULL;
  77         }
  78         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
  79                 return talloc_strdup(mem_ctx, name);
  80         }
  81 
  82         dname = talloc_strdup(mem_ctx, lp_lockdir(lp_ctx));
  83         trim_string(dname,"","/");
  84         
  85         if (!directory_exist(dname)) {
  86                 mkdir(dname,0755);
  87         }
  88         
  89         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
  90 
  91         talloc_free(dname);
  92 
  93         return fname;
  94 }
  95 
  96 /**
  97  * @brief Returns an absolute path to a file in the directory containing the current config file
  98  *
  99  * @param name File to find, relative to the config file directory.
 100  *
 101  * @retval Pointer to a talloc'ed string containing the full path.
 102  **/
 103 
 104 char *config_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
     /* [<][>][^][v][top][bottom][index][help] */
 105                            const char *name)
 106 {
 107         char *fname, *config_dir, *p;
 108         config_dir = talloc_strdup(mem_ctx, lp_configfile(lp_ctx));
 109         if (config_dir == NULL) {
 110                 config_dir = talloc_strdup(mem_ctx, lp_default_path());
 111         }
 112         p = strrchr(config_dir, '/');
 113         if (p == NULL) {
 114                 return NULL;
 115         }
 116         p[0] = '\0';
 117         fname = talloc_asprintf(mem_ctx, "%s/%s", config_dir, name);
 118         talloc_free(config_dir);
 119         return fname;
 120 }
 121 
 122 /**
 123  * @brief Returns an absolute path to a file in the Samba private directory.
 124  *
 125  * @param name File to find, relative to PRIVATEDIR.
 126  * if name is not relative, then use it as-is
 127  *
 128  * @retval Pointer to a talloc'ed string containing the full path.
 129  **/
 130 char *private_path(TALLOC_CTX* mem_ctx, 
     /* [<][>][^][v][top][bottom][index][help] */
 131                             struct loadparm_context *lp_ctx,
 132                             const char *name)
 133 {
 134         char *fname;
 135         if (name == NULL) {
 136                 return NULL;
 137         }
 138         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
 139                 return talloc_strdup(mem_ctx, name);
 140         }
 141         fname = talloc_asprintf(mem_ctx, "%s/%s", lp_private_dir(lp_ctx), name);
 142         return fname;
 143 }
 144 
 145 /**
 146   return a path in the smbd.tmp directory, where all temporary file
 147   for smbd go. If NULL is passed for name then return the directory 
 148   path itself
 149 */
 150 char *smbd_tmp_path(TALLOC_CTX *mem_ctx, 
     /* [<][>][^][v][top][bottom][index][help] */
 151                              struct loadparm_context *lp_ctx, 
 152                              const char *name)
 153 {
 154         char *fname, *dname;
 155 
 156         dname = private_path(mem_ctx, lp_ctx, "smbd.tmp");
 157         if (!directory_exist(dname)) {
 158                 mkdir(dname,0755);
 159         }
 160 
 161         if (name == NULL) {
 162                 return dname;
 163         }
 164 
 165         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
 166         talloc_free(dname);
 167 
 168         return fname;
 169 }
 170 
 171 /**
 172  * Obtain the init function from a shared library file
 173  */
 174 init_module_fn load_module(TALLOC_CTX *mem_ctx, const char *path)
     /* [<][>][^][v][top][bottom][index][help] */
 175 {
 176         void *handle;
 177         void *init_fn;
 178 
 179         handle = dlopen(path, RTLD_NOW);
 180         if (handle == NULL) {
 181                 DEBUG(0, ("Unable to open %s: %s\n", path, dlerror()));
 182                 return NULL;
 183         }
 184 
 185         init_fn = dlsym(handle, SAMBA_INIT_MODULE);
 186 
 187         if (init_fn == NULL) {
 188                 DEBUG(0, ("Unable to find %s() in %s: %s\n", 
 189                           SAMBA_INIT_MODULE, path, dlerror()));
 190                 DEBUG(1, ("Loading module '%s' failed\n", path));
 191                 dlclose(handle);
 192                 return NULL;
 193         }
 194 
 195         return (init_module_fn)init_fn;
 196 }
 197 
 198 /**
 199  * Obtain list of init functions from the modules in the specified
 200  * directory
 201  */
 202 init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path)
     /* [<][>][^][v][top][bottom][index][help] */
 203 {
 204         DIR *dir;
 205         struct dirent *entry;
 206         char *filename;
 207         int success = 0;
 208         init_module_fn *ret = talloc_array(mem_ctx, init_module_fn, 2);
 209 
 210         ret[0] = NULL;
 211         
 212         dir = opendir(path);
 213         if (dir == NULL) {
 214                 talloc_free(ret);
 215                 return NULL;
 216         }
 217 
 218         while((entry = readdir(dir))) {
 219                 if (ISDOT(entry->d_name) || ISDOTDOT(entry->d_name))
 220                         continue;
 221 
 222                 filename = talloc_asprintf(mem_ctx, "%s/%s", path, entry->d_name);
 223 
 224                 ret[success] = load_module(mem_ctx, filename);
 225                 if (ret[success]) {
 226                         ret = talloc_realloc(mem_ctx, ret, init_module_fn, success+2);
 227                         success++;
 228                         ret[success] = NULL;
 229                 }
 230 
 231                 talloc_free(filename);
 232         }
 233 
 234         closedir(dir);
 235 
 236         return ret;
 237 }
 238 
 239 /**
 240  * Run the specified init functions.
 241  *
 242  * @return true if all functions ran successfully, false otherwise
 243  */
 244 bool run_init_functions(init_module_fn *fns)
     /* [<][>][^][v][top][bottom][index][help] */
 245 {
 246         int i;
 247         bool ret = true;
 248         
 249         if (fns == NULL)
 250                 return true;
 251         
 252         for (i = 0; fns[i]; i++) { ret &= (bool)NT_STATUS_IS_OK(fns[i]()); }
 253 
 254         return ret;
 255 }
 256 
 257 static char *modules_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
     /* [<][>][^][v][top][bottom][index][help] */
 258                           const char *name)
 259 {
 260         const char *env_moduledir = getenv("LD_SAMBA_MODULE_PATH");
 261         return talloc_asprintf(mem_ctx, "%s/%s", 
 262                                env_moduledir?env_moduledir:lp_modulesdir(lp_ctx), 
 263                                name);
 264 }
 265 
 266 /**
 267  * Load the initialization functions from DSO files for a specific subsystem.
 268  *
 269  * Will return an array of function pointers to initialization functions
 270  */
 271 
 272 init_module_fn *load_samba_modules(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, const char *subsystem)
     /* [<][>][^][v][top][bottom][index][help] */
 273 {
 274         char *path = modules_path(mem_ctx, lp_ctx, subsystem);
 275         init_module_fn *ret;
 276 
 277         ret = load_modules(mem_ctx, path);
 278 
 279         talloc_free(path);
 280 
 281         return ret;
 282 }
 283 
 284 const char *lp_messaging_path(TALLOC_CTX *mem_ctx, 
     /* [<][>][^][v][top][bottom][index][help] */
 285                                        struct loadparm_context *lp_ctx)
 286 {
 287         return smbd_tmp_path(mem_ctx, lp_ctx, "messaging");
 288 }
 289 
 290 struct smb_iconv_convenience *smb_iconv_convenience_init_lp(TALLOC_CTX *mem_ctx,
     /* [<][>][^][v][top][bottom][index][help] */
 291                                                          struct loadparm_context *lp_ctx)
 292 {
 293         return smb_iconv_convenience_init(mem_ctx, lp_dos_charset(lp_ctx),
 294                                           lp_unix_charset(lp_ctx),
 295                 lp_parm_bool(lp_ctx, NULL, "iconv", "native", true));
 296 }
 297 
 298 

/* [<][>][^][v][top][bottom][index][help] */