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/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
38 bool lp_is_mydomain(struct loadparm_context
*lp_ctx
,
41 return strequal(lp_workgroup(lp_ctx
), domain
);
44 bool lp_is_my_domain_or_realm(struct loadparm_context
*lp_ctx
,
47 return strequal(lp_workgroup(lp_ctx
), domain
) ||
48 strequal(lp_realm(lp_ctx
), domain
);
52 see if a string matches either our primary or one of our secondary
53 netbios aliases. do a case insensitive match
55 bool lp_is_myname(struct loadparm_context
*lp_ctx
, const char *name
)
60 if (strcasecmp(name
, lp_netbios_name(lp_ctx
)) == 0) {
64 aliases
= lp_netbios_aliases(lp_ctx
);
65 for (i
=0; aliases
&& aliases
[i
]; i
++) {
66 if (strcasecmp(name
, aliases
[i
]) == 0) {
76 A useful function for returning a path in the Samba lock directory.
78 char *lock_path(TALLOC_CTX
* mem_ctx
, struct loadparm_context
*lp_ctx
,
85 if (name
[0] == 0 || name
[0] == '/' || strstr(name
, ":/")) {
86 return talloc_strdup(mem_ctx
, name
);
89 dname
= talloc_strdup(mem_ctx
, lp_lockdir(lp_ctx
));
90 trim_string(dname
,"","/");
92 if (!directory_exist(dname
)) {
96 fname
= talloc_asprintf(mem_ctx
, "%s/%s", dname
, name
);
104 * @brief Returns an absolute path to a file in the directory containing the current config file
106 * @param name File to find, relative to the config file directory.
108 * @retval Pointer to a talloc'ed string containing the full path.
111 char *config_path(TALLOC_CTX
* mem_ctx
, struct loadparm_context
*lp_ctx
,
114 char *fname
, *config_dir
, *p
;
115 config_dir
= talloc_strdup(mem_ctx
, lp_configfile(lp_ctx
));
116 if (config_dir
== NULL
) {
117 config_dir
= talloc_strdup(mem_ctx
, lp_default_path());
119 p
= strrchr(config_dir
, '/');
124 fname
= talloc_asprintf(mem_ctx
, "%s/%s", config_dir
, name
);
125 talloc_free(config_dir
);
130 * @brief Returns an absolute path to a file in the Samba private directory.
132 * @param name File to find, relative to PRIVATEDIR.
133 * if name is not relative, then use it as-is
135 * @retval Pointer to a talloc'ed string containing the full path.
137 char *private_path(TALLOC_CTX
* mem_ctx
,
138 struct loadparm_context
*lp_ctx
,
145 if (name
[0] == 0 || name
[0] == '/' || strstr(name
, ":/")) {
146 return talloc_strdup(mem_ctx
, name
);
148 fname
= talloc_asprintf(mem_ctx
, "%s/%s", lp_private_dir(lp_ctx
), name
);
153 return a path in the smbd.tmp directory, where all temporary file
154 for smbd go. If NULL is passed for name then return the directory
157 char *smbd_tmp_path(TALLOC_CTX
*mem_ctx
,
158 struct loadparm_context
*lp_ctx
,
163 dname
= private_path(mem_ctx
, lp_ctx
, "smbd.tmp");
164 if (!directory_exist(dname
)) {
172 fname
= talloc_asprintf(mem_ctx
, "%s/%s", dname
, name
);
179 * Obtain the init function from a shared library file
181 init_module_fn
load_module(TALLOC_CTX
*mem_ctx
, const char *path
)
186 handle
= dlopen(path
, RTLD_NOW
);
187 if (handle
== NULL
) {
188 DEBUG(0, ("Unable to open %s: %s\n", path
, dlerror()));
192 init_fn
= dlsym(handle
, SAMBA_INIT_MODULE
);
194 if (init_fn
== NULL
) {
195 DEBUG(0, ("Unable to find %s() in %s: %s\n",
196 SAMBA_INIT_MODULE
, path
, dlerror()));
197 DEBUG(1, ("Loading module '%s' failed\n", path
));
202 return (init_module_fn
)init_fn
;
206 * Obtain list of init functions from the modules in the specified
209 init_module_fn
*load_modules(TALLOC_CTX
*mem_ctx
, const char *path
)
212 struct dirent
*entry
;
215 init_module_fn
*ret
= talloc_array(mem_ctx
, init_module_fn
, 2);
225 while((entry
= readdir(dir
))) {
226 if (ISDOT(entry
->d_name
) || ISDOTDOT(entry
->d_name
))
229 filename
= talloc_asprintf(mem_ctx
, "%s/%s", path
, entry
->d_name
);
231 ret
[success
] = load_module(mem_ctx
, filename
);
233 ret
= talloc_realloc(mem_ctx
, ret
, init_module_fn
, success
+2);
238 talloc_free(filename
);
247 * Run the specified init functions.
249 * @return true if all functions ran successfully, false otherwise
251 bool run_init_functions(init_module_fn
*fns
)
259 for (i
= 0; fns
[i
]; i
++) { ret
&= (bool)NT_STATUS_IS_OK(fns
[i
]()); }
264 static char *modules_path(TALLOC_CTX
* mem_ctx
, struct loadparm_context
*lp_ctx
,
267 const char *env_moduledir
= getenv("LD_SAMBA_MODULE_PATH");
268 return talloc_asprintf(mem_ctx
, "%s/%s",
269 env_moduledir
?env_moduledir
:lp_modulesdir(lp_ctx
),
274 * Load the initialization functions from DSO files for a specific subsystem.
276 * Will return an array of function pointers to initialization functions
279 init_module_fn
*load_samba_modules(TALLOC_CTX
*mem_ctx
, struct loadparm_context
*lp_ctx
, const char *subsystem
)
281 char *path
= modules_path(mem_ctx
, lp_ctx
, subsystem
);
284 ret
= load_modules(mem_ctx
, path
);
291 const char *lp_messaging_path(TALLOC_CTX
*mem_ctx
,
292 struct loadparm_context
*lp_ctx
)
294 return smbd_tmp_path(mem_ctx
, lp_ctx
, "messaging");
297 struct smb_iconv_convenience
*smb_iconv_convenience_init_lp(TALLOC_CTX
*mem_ctx
,
298 struct loadparm_context
*lp_ctx
)
300 return smb_iconv_convenience_init(mem_ctx
, lp_dos_charset(lp_ctx
),
301 lp_unix_charset(lp_ctx
),
302 lp_parm_bool(lp_ctx
, NULL
, "iconv", "native", true));
306 const char *lp_sam_name(struct loadparm_context
*lp_ctx
)
308 switch (lp_server_role(lp_ctx
)) {
309 case ROLE_DOMAIN_CONTROLLER
:
310 return lp_workgroup(lp_ctx
);
312 return lp_netbios_name(lp_ctx
);