Add smbcli_transport_dead callback
[Samba/vfs_proxy.git] / source4 / param / util.c
blobec192939d0a1933d78379211747b1e682c458a1b
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
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/>.
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"
32 /**
33 * @file
34 * @brief Misc utility functions
38 bool lp_is_mydomain(struct loadparm_context *lp_ctx,
39 const char *domain)
41 return strequal(lp_workgroup(lp_ctx), domain);
44 /**
45 see if a string matches either our primary or one of our secondary
46 netbios aliases. do a case insensitive match
48 bool lp_is_myname(struct loadparm_context *lp_ctx, const char *name)
50 const char **aliases;
51 int i;
53 if (strcasecmp(name, lp_netbios_name(lp_ctx)) == 0) {
54 return true;
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;
64 return false;
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,
72 const char *name)
74 char *fname, *dname;
75 if (name == NULL) {
76 return NULL;
78 if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
79 return talloc_strdup(mem_ctx, name);
82 dname = talloc_strdup(mem_ctx, lp_lockdir(lp_ctx));
83 trim_string(dname,"","/");
85 if (!directory_exist(dname)) {
86 mkdir(dname,0755);
89 fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
91 talloc_free(dname);
93 return fname;
96 /**
97 * @brief Returns an absolute path to a file in the directory containing the current config file
99 * @param name File to find, relative to the config file directory.
101 * @retval Pointer to a talloc'ed string containing the full path.
104 char *config_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
105 const char *name)
107 char *fname, *config_dir, *p;
108 config_dir = talloc_strdup(mem_ctx, lp_configfile(lp_ctx));
109 if (config_dir == NULL) {
110 return NULL;
112 p = strrchr(config_dir, '/');
113 if (p == NULL) {
114 return NULL;
116 p[0] = '\0';
117 fname = talloc_asprintf(mem_ctx, "%s/%s", config_dir, name);
118 talloc_free(config_dir);
119 return fname;
123 * @brief Returns an absolute path to a file in the Samba private directory.
125 * @param name File to find, relative to PRIVATEDIR.
126 * if name is not relative, then use it as-is
128 * @retval Pointer to a talloc'ed string containing the full path.
130 char *private_path(TALLOC_CTX* mem_ctx,
131 struct loadparm_context *lp_ctx,
132 const char *name)
134 char *fname;
135 if (name == NULL) {
136 return NULL;
138 if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
139 return talloc_strdup(mem_ctx, name);
141 fname = talloc_asprintf(mem_ctx, "%s/%s", lp_private_dir(lp_ctx), name);
142 return fname;
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
150 char *smbd_tmp_path(TALLOC_CTX *mem_ctx,
151 struct loadparm_context *lp_ctx,
152 const char *name)
154 char *fname, *dname;
156 dname = private_path(mem_ctx, lp_ctx, "smbd.tmp");
157 if (!directory_exist(dname)) {
158 mkdir(dname,0755);
161 if (name == NULL) {
162 return dname;
165 fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
166 talloc_free(dname);
168 return fname;
172 * Obtain the init function from a shared library file
174 init_module_fn load_module(TALLOC_CTX *mem_ctx, const char *path)
176 void *handle;
177 void *init_fn;
179 handle = dlopen(path, RTLD_NOW);
180 if (handle == NULL) {
181 DEBUG(0, ("Unable to open %s: %s\n", path, dlerror()));
182 return NULL;
185 init_fn = dlsym(handle, SAMBA_INIT_MODULE);
187 if (init_fn == NULL) {
188 DEBUG(0, ("Unable to find init_module() in %s: %s\n", path, dlerror()));
189 DEBUG(1, ("Loading module '%s' failed\n", path));
190 dlclose(handle);
191 return NULL;
194 return (init_module_fn)init_fn;
198 * Obtain list of init functions from the modules in the specified
199 * directory
201 init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path)
203 DIR *dir;
204 struct dirent *entry;
205 char *filename;
206 int success = 0;
207 init_module_fn *ret = talloc_array(mem_ctx, init_module_fn, 2);
209 ret[0] = NULL;
211 dir = opendir(path);
212 if (dir == NULL) {
213 talloc_free(ret);
214 return NULL;
217 while((entry = readdir(dir))) {
218 if (ISDOT(entry->d_name) || ISDOTDOT(entry->d_name))
219 continue;
221 filename = talloc_asprintf(mem_ctx, "%s/%s", path, entry->d_name);
223 ret[success] = load_module(mem_ctx, filename);
224 if (ret[success]) {
225 ret = talloc_realloc(mem_ctx, ret, init_module_fn, success+2);
226 success++;
227 ret[success] = NULL;
230 talloc_free(filename);
233 closedir(dir);
235 return ret;
239 * Run the specified init functions.
241 * @return true if all functions ran successfully, false otherwise
243 bool run_init_functions(init_module_fn *fns)
245 int i;
246 bool ret = true;
248 if (fns == NULL)
249 return true;
251 for (i = 0; fns[i]; i++) { ret &= (bool)NT_STATUS_IS_OK(fns[i]()); }
253 return ret;
256 static char *modules_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
257 const char *name)
259 const char *env_moduledir = getenv("LD_SAMBA_MODULE_PATH");
260 return talloc_asprintf(mem_ctx, "%s/%s",
261 env_moduledir?env_moduledir:lp_modulesdir(lp_ctx),
262 name);
266 * Load the initialization functions from DSO files for a specific subsystem.
268 * Will return an array of function pointers to initialization functions
271 init_module_fn *load_samba_modules(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, const char *subsystem)
273 char *path = modules_path(mem_ctx, lp_ctx, subsystem);
274 init_module_fn *ret;
276 ret = load_modules(mem_ctx, path);
278 talloc_free(path);
280 return ret;
283 const char *lp_messaging_path(TALLOC_CTX *mem_ctx,
284 struct loadparm_context *lp_ctx)
286 return smbd_tmp_path(mem_ctx, lp_ctx, "messaging");
289 struct smb_iconv_convenience *smb_iconv_convenience_init_lp(TALLOC_CTX *mem_ctx,
290 struct loadparm_context *lp_ctx)
292 return smb_iconv_convenience_init(mem_ctx, lp_dos_charset(lp_ctx),
293 lp_unix_charset(lp_ctx),
294 lp_parm_bool(lp_ctx, NULL, "iconv", "native", true));