r25398: Parse loadparm context to all lp_*() functions.
[jelmer/samba4-debian.git] / source / param / util.c
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.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 /**
39   see if a string matches either our primary or one of our secondary 
40   netbios aliases. do a case insensitive match
41 */
42 _PUBLIC_ bool is_myname(const char *name)
43 {
44         const char **aliases;
45         int i;
46
47         if (strcasecmp(name, lp_netbios_name(global_loadparm)) == 0) {
48                 return True;
49         }
50
51         aliases = lp_netbios_aliases(global_loadparm);
52         for (i=0; aliases && aliases[i]; i++) {
53                 if (strcasecmp(name, aliases[i]) == 0) {
54                         return True;
55                 }
56         }
57
58         return False;
59 }
60
61
62 /**
63  A useful function for returning a path in the Samba lock directory.
64 **/
65 _PUBLIC_ char *lock_path(TALLOC_CTX* mem_ctx, const char *name)
66 {
67         char *fname, *dname;
68         if (name == NULL) {
69                 return NULL;
70         }
71         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
72                 return talloc_strdup(mem_ctx, name);
73         }
74
75         dname = talloc_strdup(mem_ctx, lp_lockdir(global_loadparm));
76         trim_string(dname,"","/");
77         
78         if (!directory_exist(dname)) {
79                 mkdir(dname,0755);
80         }
81         
82         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
83
84         talloc_free(dname);
85
86         return fname;
87 }
88
89
90 /**
91  A useful function for returning a path in the Samba piddir directory.
92 **/
93 static char *pid_path(TALLOC_CTX* mem_ctx, const char *name)
94 {
95         char *fname, *dname;
96
97         dname = talloc_strdup(mem_ctx, lp_piddir(global_loadparm));
98         trim_string(dname,"","/");
99         
100         if (!directory_exist(dname)) {
101                 mkdir(dname,0755);
102         }
103         
104         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
105
106         talloc_free(dname);
107
108         return fname;
109 }
110
111
112 /**
113  * @brief Returns an absolute path to a file in the Samba lib directory.
114  *
115  * @param name File to find, relative to DATADIR.
116  *
117  * @retval Pointer to a talloc'ed string containing the full path.
118  **/
119
120 _PUBLIC_ char *data_path(TALLOC_CTX* mem_ctx, const char *name)
121 {
122         char *fname;
123         fname = talloc_asprintf(mem_ctx, "%s/%s", dyn_DATADIR, name);
124         return fname;
125 }
126
127 /**
128  * @brief Returns an absolute path to a file in the directory containing the current config file
129  *
130  * @param name File to find, relative to the config file directory.
131  *
132  * @retval Pointer to a talloc'ed string containing the full path.
133  **/
134
135 _PUBLIC_ char *config_path(TALLOC_CTX* mem_ctx, const char *name)
136 {
137         char *fname, *config_dir, *p;
138         config_dir = talloc_strdup(mem_ctx, lp_configfile(global_loadparm));
139         p = strrchr(config_dir, '/');
140         if (!p) {
141                 return NULL;
142         }
143         p[0] = '\0';
144         fname = talloc_asprintf(mem_ctx, "%s/%s", config_dir, name);
145         talloc_free(config_dir);
146         return fname;
147 }
148
149 /**
150  * @brief Returns an absolute path to a file in the Samba private directory.
151  *
152  * @param name File to find, relative to PRIVATEDIR.
153  * if name is not relative, then use it as-is
154  *
155  * @retval Pointer to a talloc'ed string containing the full path.
156  **/
157 _PUBLIC_ char *private_path(TALLOC_CTX* mem_ctx, const char *name)
158 {
159         char *fname;
160         if (name == NULL) {
161                 return NULL;
162         }
163         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
164                 return talloc_strdup(mem_ctx, name);
165         }
166         fname = talloc_asprintf(mem_ctx, "%s/%s", lp_private_dir(global_loadparm), name);
167         return fname;
168 }
169
170 /**
171   return a path in the smbd.tmp directory, where all temporary file
172   for smbd go. If NULL is passed for name then return the directory 
173   path itself
174 */
175 _PUBLIC_ char *smbd_tmp_path(TALLOC_CTX *mem_ctx, const char *name)
176 {
177         char *fname, *dname;
178
179         dname = pid_path(mem_ctx, "smbd.tmp");
180         if (!directory_exist(dname)) {
181                 mkdir(dname,0755);
182         }
183
184         if (name == NULL) {
185                 return dname;
186         }
187
188         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
189         talloc_free(dname);
190
191         return fname;
192 }
193
194 /**
195  * Obtain the init function from a shared library file
196  */
197 _PUBLIC_ init_module_fn load_module(TALLOC_CTX *mem_ctx, const char *path)
198 {
199         void *handle;
200         void *init_fn;
201
202         handle = dlopen(path, RTLD_NOW);
203         if (handle == NULL) {
204                 DEBUG(0, ("Unable to open %s: %s\n", path, dlerror()));
205                 return NULL;
206         }
207
208         init_fn = dlsym(handle, "init_module");
209
210         if (init_fn == NULL) {
211                 DEBUG(0, ("Unable to find init_module() in %s: %s\n", path, dlerror()));
212                 DEBUG(1, ("Loading module '%s' failed\n", path));
213                 dlclose(handle);
214                 return NULL;
215         }
216
217         return (init_module_fn)init_fn;
218 }
219
220 /**
221  * Obtain list of init functions from the modules in the specified
222  * directory
223  */
224 _PUBLIC_ init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path)
225 {
226         DIR *dir;
227         struct dirent *entry;
228         char *filename;
229         int success = 0;
230         init_module_fn *ret = talloc_array(mem_ctx, init_module_fn, 2);
231
232         ret[0] = NULL;
233         
234         dir = opendir(path);
235         if (dir == NULL) {
236                 talloc_free(ret);
237                 return NULL;
238         }
239
240         while((entry = readdir(dir))) {
241                 if (ISDOT(entry->d_name) || ISDOTDOT(entry->d_name))
242                         continue;
243
244                 filename = talloc_asprintf(mem_ctx, "%s/%s", path, entry->d_name);
245
246                 ret[success] = load_module(mem_ctx, filename);
247                 if (ret[success]) {
248                         ret = talloc_realloc(mem_ctx, ret, init_module_fn, success+2);
249                         success++;
250                         ret[success] = NULL;
251                 }
252
253                 talloc_free(filename);
254         }
255
256         closedir(dir);
257
258         return ret;
259 }
260
261 /**
262  * Run the specified init functions.
263  *
264  * @return true if all functions ran successfully, false otherwise
265  */
266 _PUBLIC_ bool run_init_functions(init_module_fn *fns)
267 {
268         int i;
269         bool ret = true;
270         
271         if (fns == NULL)
272                 return true;
273         
274         for (i = 0; fns[i]; i++) { ret &= (bool)NT_STATUS_IS_OK(fns[i]()); }
275
276         return ret;
277 }
278
279 static char *modules_path(TALLOC_CTX* mem_ctx, const char *name)
280 {
281         const char *env_moduledir = getenv("LD_SAMBA_MODULE_PATH");
282         return talloc_asprintf(mem_ctx, "%s/%s", 
283                                                    env_moduledir?env_moduledir:lp_modulesdir(global_loadparm), 
284                                                    name);
285 }
286
287 /**
288  * Load the initialization functions from DSO files for a specific subsystem.
289  *
290  * Will return an array of function pointers to initialization functions
291  */
292
293 _PUBLIC_ init_module_fn *load_samba_modules(TALLOC_CTX *mem_ctx, const char *subsystem)
294 {
295         char *path = modules_path(mem_ctx, subsystem);
296         init_module_fn *ret;
297
298         ret = load_modules(mem_ctx, path);
299
300         talloc_free(path);
301
302         return ret;
303 }
304
305