d5b9583795869b436a8dbaae16647079dce1cf30
[samba.git] / source4 / 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/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 lpcfg_is_mydomain(struct loadparm_context *lp_ctx,
39                              const char *domain)
40 {
41         return strequal(lpcfg_workgroup(lp_ctx), domain);
42 }
43
44 bool lpcfg_is_my_domain_or_realm(struct loadparm_context *lp_ctx,
45                              const char *domain)
46 {
47         return strequal(lpcfg_workgroup(lp_ctx), domain) ||
48                 strequal(lpcfg_realm(lp_ctx), domain);
49 }
50
51 /**
52   see if a string matches either our primary or one of our secondary 
53   netbios aliases. do a case insensitive match
54 */
55 bool lpcfg_is_myname(struct loadparm_context *lp_ctx, const char *name)
56 {
57         const char **aliases;
58         int i;
59
60         if (strcasecmp(name, lpcfg_netbios_name(lp_ctx)) == 0) {
61                 return true;
62         }
63
64         aliases = lpcfg_netbios_aliases(lp_ctx);
65         for (i=0; aliases && aliases[i]; i++) {
66                 if (strcasecmp(name, aliases[i]) == 0) {
67                         return true;
68                 }
69         }
70
71         return false;
72 }
73
74
75 /**
76  A useful function for returning a path in the Samba lock directory.
77 **/
78 char *lpcfg_lock_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
79                          const char *name)
80 {
81         char *fname, *dname;
82         if (name == NULL) {
83                 return NULL;
84         }
85         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
86                 return talloc_strdup(mem_ctx, name);
87         }
88
89         dname = talloc_strdup(mem_ctx, lpcfg_lockdir(lp_ctx));
90         trim_string(dname,"","/");
91         
92         if (!directory_exist(dname)) {
93                 mkdir(dname,0755);
94         }
95         
96         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
97
98         talloc_free(dname);
99
100         return fname;
101 }
102
103 /**
104  A useful function for returning a path in the Samba state directory.
105 **/
106 char *lpcfg_state_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
107                        const char *name)
108 {
109         char *fname, *dname;
110         if (name == NULL) {
111                 return NULL;
112         }
113         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
114                 return talloc_strdup(mem_ctx, name);
115         }
116
117         dname = talloc_strdup(mem_ctx, lpcfg_statedir(lp_ctx));
118         trim_string(dname,"","/");
119
120         if (!directory_exist(dname)) {
121                 mkdir(dname,0755);
122         }
123
124         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
125
126         talloc_free(dname);
127
128         return fname;
129 }
130
131 /**
132  A useful function for returning a path in the Samba cache directory.
133 **/
134 char *lpcfg_cache_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
135                        const char *name)
136 {
137         char *fname, *dname;
138         if (name == NULL) {
139                 return NULL;
140         }
141         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
142                 return talloc_strdup(mem_ctx, name);
143         }
144
145         dname = talloc_strdup(mem_ctx, lpcfg_cachedir(lp_ctx));
146         trim_string(dname,"","/");
147
148         if (!directory_exist(dname)) {
149                 mkdir(dname,0755);
150         }
151
152         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
153
154         talloc_free(dname);
155
156         return fname;
157 }
158
159 /**
160  * @brief Returns an absolute path to a file in the directory containing the current config file
161  *
162  * @param name File to find, relative to the config file directory.
163  *
164  * @retval Pointer to a talloc'ed string containing the full path.
165  **/
166
167 char *lpcfg_config_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
168                            const char *name)
169 {
170         char *fname, *config_dir, *p;
171         config_dir = talloc_strdup(mem_ctx, lpcfg_configfile(lp_ctx));
172         if (config_dir == NULL) {
173                 config_dir = talloc_strdup(mem_ctx, lp_default_path());
174         }
175         p = strrchr(config_dir, '/');
176         if (p == NULL) {
177                 talloc_free(config_dir);
178                 config_dir = talloc_strdup(mem_ctx, ".");
179                 if (config_dir == NULL) {
180                         return NULL;
181                 }
182         } else {
183                 p[0] = '\0';
184         }
185         fname = talloc_asprintf(mem_ctx, "%s/%s", config_dir, name);
186         talloc_free(config_dir);
187         return fname;
188 }
189
190 /**
191  * @brief Returns an absolute path to a file in the Samba private directory.
192  *
193  * @param name File to find, relative to PRIVATEDIR.
194  * if name is not relative, then use it as-is
195  *
196  * @retval Pointer to a talloc'ed string containing the full path.
197  **/
198 char *lpcfg_private_path(TALLOC_CTX* mem_ctx,
199                             struct loadparm_context *lp_ctx,
200                             const char *name)
201 {
202         char *fname;
203         if (name == NULL) {
204                 return NULL;
205         }
206         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
207                 return talloc_strdup(mem_ctx, name);
208         }
209         fname = talloc_asprintf(mem_ctx, "%s/%s", lpcfg_private_dir(lp_ctx), name);
210         return fname;
211 }
212
213 /**
214   return a path in the smbd.tmp directory, where all temporary file
215   for smbd go. If NULL is passed for name then return the directory 
216   path itself
217 */
218 char *smbd_tmp_path(TALLOC_CTX *mem_ctx, 
219                              struct loadparm_context *lp_ctx,
220                              const char *name)
221 {
222         char *fname, *dname;
223
224         dname = lpcfg_private_path(mem_ctx, lp_ctx, "smbd.tmp");
225         if (!directory_exist(dname)) {
226                 mkdir(dname,0755);
227         }
228
229         if (name == NULL) {
230                 return dname;
231         }
232
233         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
234         talloc_free(dname);
235
236         return fname;
237 }
238
239 /**
240  * Obtain the init function from a shared library file
241  */
242 init_module_fn load_module(TALLOC_CTX *mem_ctx, const char *path)
243 {
244         void *handle;
245         void *init_fn;
246
247         handle = dlopen(path, RTLD_NOW);
248         if (handle == NULL) {
249                 DEBUG(0, ("Unable to open %s: %s\n", path, dlerror()));
250                 return NULL;
251         }
252
253         init_fn = dlsym(handle, SAMBA_INIT_MODULE);
254
255         if (init_fn == NULL) {
256                 DEBUG(0, ("Unable to find %s() in %s: %s\n", 
257                           SAMBA_INIT_MODULE, path, dlerror()));
258                 DEBUG(1, ("Loading module '%s' failed\n", path));
259                 dlclose(handle);
260                 return NULL;
261         }
262
263         return (init_module_fn)init_fn;
264 }
265
266 /**
267  * Obtain list of init functions from the modules in the specified
268  * directory
269  */
270 init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path)
271 {
272         DIR *dir;
273         struct dirent *entry;
274         char *filename;
275         int success = 0;
276         init_module_fn *ret = talloc_array(mem_ctx, init_module_fn, 2);
277
278         ret[0] = NULL;
279         
280         dir = opendir(path);
281         if (dir == NULL) {
282                 talloc_free(ret);
283                 return NULL;
284         }
285
286         while((entry = readdir(dir))) {
287                 if (ISDOT(entry->d_name) || ISDOTDOT(entry->d_name))
288                         continue;
289
290                 filename = talloc_asprintf(mem_ctx, "%s/%s", path, entry->d_name);
291
292                 ret[success] = load_module(mem_ctx, filename);
293                 if (ret[success]) {
294                         ret = talloc_realloc(mem_ctx, ret, init_module_fn, success+2);
295                         success++;
296                         ret[success] = NULL;
297                 }
298
299                 talloc_free(filename);
300         }
301
302         closedir(dir);
303
304         return ret;
305 }
306
307 /**
308  * Run the specified init functions.
309  *
310  * @return true if all functions ran successfully, false otherwise
311  */
312 bool run_init_functions(init_module_fn *fns)
313 {
314         int i;
315         bool ret = true;
316         
317         if (fns == NULL)
318                 return true;
319         
320         for (i = 0; fns[i]; i++) { ret &= (bool)NT_STATUS_IS_OK(fns[i]()); }
321
322         return ret;
323 }
324
325 /**
326  * Load the initialization functions from DSO files for a specific subsystem.
327  *
328  * Will return an array of function pointers to initialization functions
329  */
330
331 init_module_fn *load_samba_modules(TALLOC_CTX *mem_ctx, const char *subsystem)
332 {
333         char *path = modules_path(mem_ctx, subsystem);
334         init_module_fn *ret;
335
336         ret = load_modules(mem_ctx, path);
337
338         talloc_free(path);
339
340         return ret;
341 }
342
343 const char *lpcfg_imessaging_path(TALLOC_CTX *mem_ctx,
344                                        struct loadparm_context *lp_ctx)
345 {
346         return smbd_tmp_path(mem_ctx, lp_ctx, "msg");
347 }
348
349 struct smb_iconv_handle *smb_iconv_handle_reinit_lp(TALLOC_CTX *mem_ctx,
350                                                               struct loadparm_context *lp_ctx,
351                                                               struct smb_iconv_handle *old_ic)
352 {
353         return smb_iconv_handle_reinit(mem_ctx, lpcfg_dos_charset(lp_ctx),
354                                        lpcfg_unix_charset(lp_ctx),
355                                        true,
356                                        old_ic);
357 }
358
359
360 const char *lpcfg_sam_name(struct loadparm_context *lp_ctx)
361 {
362         switch (lpcfg_server_role(lp_ctx)) {
363         case ROLE_DOMAIN_BDC:
364         case ROLE_DOMAIN_PDC:
365                 return lpcfg_workgroup(lp_ctx);
366         default:
367                 return lpcfg_netbios_name(lp_ctx);
368         }
369 }
370