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
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.
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.
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/>.
26 #include "dynconfig.h"
27 #include "system/network.h"
28 #include "system/filesys.h"
29 #include "system/dir.h"
30 #include "param/param.h"
34 * @brief Misc utility functions
39 see if a string matches either our primary or one of our secondary
40 netbios aliases. do a case insensitive match
42 _PUBLIC_ bool is_myname(const char *name)
47 if (strcasecmp(name, lp_netbios_name(global_loadparm)) == 0) {
51 aliases = lp_netbios_aliases(global_loadparm);
52 for (i=0; aliases && aliases[i]; i++) {
53 if (strcasecmp(name, aliases[i]) == 0) {
63 A useful function for returning a path in the Samba lock directory.
65 _PUBLIC_ char *lock_path(TALLOC_CTX* mem_ctx, const char *name)
71 if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
72 return talloc_strdup(mem_ctx, name);
75 dname = talloc_strdup(mem_ctx, lp_lockdir(global_loadparm));
76 trim_string(dname,"","/");
78 if (!directory_exist(dname)) {
82 fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
91 A useful function for returning a path in the Samba piddir directory.
93 static char *pid_path(TALLOC_CTX* mem_ctx, const char *name)
97 dname = talloc_strdup(mem_ctx, lp_piddir(global_loadparm));
98 trim_string(dname,"","/");
100 if (!directory_exist(dname)) {
104 fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
113 * @brief Returns an absolute path to a file in the Samba lib directory.
115 * @param name File to find, relative to DATADIR.
117 * @retval Pointer to a talloc'ed string containing the full path.
120 _PUBLIC_ char *data_path(TALLOC_CTX* mem_ctx, const char *name)
123 fname = talloc_asprintf(mem_ctx, "%s/%s", dyn_DATADIR, name);
128 * @brief Returns an absolute path to a file in the directory containing the current config file
130 * @param name File to find, relative to the config file directory.
132 * @retval Pointer to a talloc'ed string containing the full path.
135 _PUBLIC_ char *config_path(TALLOC_CTX* mem_ctx, const char *name)
137 char *fname, *config_dir, *p;
138 config_dir = talloc_strdup(mem_ctx, lp_configfile(global_loadparm));
139 p = strrchr(config_dir, '/');
144 fname = talloc_asprintf(mem_ctx, "%s/%s", config_dir, name);
145 talloc_free(config_dir);
150 * @brief Returns an absolute path to a file in the Samba private directory.
152 * @param name File to find, relative to PRIVATEDIR.
153 * if name is not relative, then use it as-is
155 * @retval Pointer to a talloc'ed string containing the full path.
157 _PUBLIC_ char *private_path(TALLOC_CTX* mem_ctx, const char *name)
163 if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
164 return talloc_strdup(mem_ctx, name);
166 fname = talloc_asprintf(mem_ctx, "%s/%s", lp_private_dir(global_loadparm), name);
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
175 _PUBLIC_ char *smbd_tmp_path(TALLOC_CTX *mem_ctx, const char *name)
179 dname = pid_path(mem_ctx, "smbd.tmp");
180 if (!directory_exist(dname)) {
188 fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
195 * Obtain the init function from a shared library file
197 _PUBLIC_ init_module_fn load_module(TALLOC_CTX *mem_ctx, const char *path)
202 handle = dlopen(path, RTLD_NOW);
203 if (handle == NULL) {
204 DEBUG(0, ("Unable to open %s: %s\n", path, dlerror()));
208 init_fn = dlsym(handle, "init_module");
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));
217 return (init_module_fn)init_fn;
221 * Obtain list of init functions from the modules in the specified
224 _PUBLIC_ init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path)
227 struct dirent *entry;
230 init_module_fn *ret = talloc_array(mem_ctx, init_module_fn, 2);
240 while((entry = readdir(dir))) {
241 if (ISDOT(entry->d_name) || ISDOTDOT(entry->d_name))
244 filename = talloc_asprintf(mem_ctx, "%s/%s", path, entry->d_name);
246 ret[success] = load_module(mem_ctx, filename);
248 ret = talloc_realloc(mem_ctx, ret, init_module_fn, success+2);
253 talloc_free(filename);
262 * Run the specified init functions.
264 * @return true if all functions ran successfully, false otherwise
266 _PUBLIC_ bool run_init_functions(init_module_fn *fns)
274 for (i = 0; fns[i]; i++) { ret &= (bool)NT_STATUS_IS_OK(fns[i]()); }
279 static char *modules_path(TALLOC_CTX* mem_ctx, const char *name)
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),
288 * Load the initialization functions from DSO files for a specific subsystem.
290 * Will return an array of function pointers to initialization functions
293 _PUBLIC_ init_module_fn *load_samba_modules(TALLOC_CTX *mem_ctx, const char *subsystem)
295 char *path = modules_path(mem_ctx, subsystem);
298 ret = load_modules(mem_ctx, path);