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"
31 #include "libds/common/roles.h"
36 * @brief Misc utility functions
40 bool lpcfg_is_mydomain(struct loadparm_context
*lp_ctx
,
43 return strequal(lpcfg_workgroup(lp_ctx
), domain
);
46 bool lpcfg_is_my_domain_or_realm(struct loadparm_context
*lp_ctx
,
49 return strequal(lpcfg_workgroup(lp_ctx
), domain
) ||
50 strequal(lpcfg_realm(lp_ctx
), domain
);
54 see if a string matches either our primary or one of our secondary
55 netbios aliases. do a case insensitive match
57 bool lpcfg_is_myname(struct loadparm_context
*lp_ctx
, const char *name
)
62 if (strcasecmp_m(name
, lpcfg_netbios_name(lp_ctx
)) == 0) {
66 aliases
= lpcfg_netbios_aliases(lp_ctx
);
67 for (i
=0; aliases
&& aliases
[i
]; i
++) {
68 if (strcasecmp_m(name
, aliases
[i
]) == 0) {
76 static char *lpcfg_common_path(TALLOC_CTX
* mem_ctx
,
86 if (name
[0] == 0 || name
[0] == '/' || strstr(name
, ":/")) {
87 return talloc_strdup(mem_ctx
, name
);
90 dname
= talloc_strdup(mem_ctx
, parent
);
94 trim_string(dname
,"","/");
96 ok
= directory_create_or_exist(dname
, 0755);
98 DEBUG(1, ("Unable to create directory %s for file %s. "
99 "Error was %s\n", dname
, name
, strerror(errno
)));
103 fname
= talloc_asprintf(mem_ctx
, "%s/%s", dname
, name
);
114 A useful function for returning a path in the Samba lock directory.
116 char *lpcfg_lock_path(TALLOC_CTX
* mem_ctx
, struct loadparm_context
*lp_ctx
,
119 return lpcfg_common_path(mem_ctx
, lpcfg_lock_directory(lp_ctx
), name
);
123 A useful function for returning a path in the Samba state directory.
125 char *lpcfg_state_path(TALLOC_CTX
* mem_ctx
, struct loadparm_context
*lp_ctx
,
128 return lpcfg_common_path(mem_ctx
, lpcfg_state_directory(lp_ctx
), name
);
132 A useful function for returning a path in the Samba cache directory.
134 char *lpcfg_cache_path(TALLOC_CTX
* mem_ctx
, struct loadparm_context
*lp_ctx
,
137 return lpcfg_common_path(mem_ctx
, lpcfg_cache_directory(lp_ctx
), name
);
141 * @brief Returns an absolute path to a file in the directory containing the current config file
143 * @param name File to find, relative to the config file directory.
145 * @retval Pointer to a talloc'ed string containing the full path.
148 char *lpcfg_config_path(TALLOC_CTX
* mem_ctx
, struct loadparm_context
*lp_ctx
,
151 char *fname
, *config_dir
, *p
;
152 config_dir
= talloc_strdup(mem_ctx
, lpcfg_configfile(lp_ctx
));
153 if (config_dir
== NULL
) {
154 config_dir
= talloc_strdup(mem_ctx
, lp_default_path());
156 p
= strrchr(config_dir
, '/');
158 talloc_free(config_dir
);
159 config_dir
= talloc_strdup(mem_ctx
, ".");
160 if (config_dir
== NULL
) {
166 fname
= talloc_asprintf(mem_ctx
, "%s/%s", config_dir
, name
);
167 talloc_free(config_dir
);
172 * @brief Returns an absolute path to a file in the Samba private directory.
174 * @param name File to find, relative to PRIVATEDIR.
175 * if name is not relative, then use it as-is
177 * @retval Pointer to a talloc'ed string containing the full path.
179 char *lpcfg_private_path(TALLOC_CTX
* mem_ctx
,
180 struct loadparm_context
*lp_ctx
,
187 if (name
[0] == 0 || name
[0] == '/' || strstr(name
, ":/")) {
188 return talloc_strdup(mem_ctx
, name
);
190 fname
= talloc_asprintf(mem_ctx
, "%s/%s", lpcfg_private_dir(lp_ctx
), name
);
195 * @brief Returns an absolute path to a NTDB or TDB file in the Samba
198 * @param name File to find, relative to PRIVATEDIR, without .tdb extension.
200 * @retval Pointer to a talloc'ed string containing the full path, for
201 * use with dbwrap_local_open().
203 char *lpcfg_private_db_path(TALLOC_CTX
*mem_ctx
,
204 struct loadparm_context
*lp_ctx
,
207 return talloc_asprintf(mem_ctx
, "%s/%s.tdb",
208 lpcfg_private_dir(lp_ctx
), name
);
212 return a path in the smbd.tmp directory, where all temporary file
213 for smbd go. If NULL is passed for name then return the directory
216 char *smbd_tmp_path(TALLOC_CTX
*mem_ctx
,
217 struct loadparm_context
*lp_ctx
,
223 dname
= lpcfg_private_path(mem_ctx
, lp_ctx
, "smbd.tmp");
228 ok
= directory_create_or_exist(dname
, 0755);
237 fname
= talloc_asprintf(mem_ctx
, "%s/%s", dname
, name
);
246 const char *lpcfg_imessaging_path(TALLOC_CTX
*mem_ctx
,
247 struct loadparm_context
*lp_ctx
)
249 return smbd_tmp_path(mem_ctx
, lp_ctx
, "msg");
252 const char *lpcfg_sam_name(struct loadparm_context
*lp_ctx
)
254 switch (lpcfg_server_role(lp_ctx
)) {
255 case ROLE_DOMAIN_BDC
:
256 case ROLE_DOMAIN_PDC
:
257 case ROLE_ACTIVE_DIRECTORY_DC
:
259 return lpcfg_workgroup(lp_ctx
);
261 return lpcfg_netbios_name(lp_ctx
);
265 const char *lpcfg_sam_dnsname(struct loadparm_context
*lp_ctx
)
267 switch (lpcfg_server_role(lp_ctx
)) {
268 case ROLE_ACTIVE_DIRECTORY_DC
:
269 return lpcfg_dnsdomain(lp_ctx
);
275 static long tdb_fetch_lifetime(TALLOC_CTX
*mem_ctx
, struct tdb_context
*tdb
, const char *keystr
)
282 key
.dptr
= discard_const_p(unsigned char, keystr
);
283 key
.dsize
= strlen(keystr
);
288 ret
= tdb_fetch(tdb
, key
);
292 tmp
= talloc_realloc(mem_ctx
, tmp
, char, ret
.dsize
+1);
293 memset(tmp
, 0, ret
.dsize
+1);
294 memcpy(tmp
, ret
.dptr
, ret
.dsize
);
302 void lpcfg_default_kdc_policy(TALLOC_CTX
*mem_ctx
,
303 struct loadparm_context
*lp_ctx
,
304 time_t *svc_tkt_lifetime
,
305 time_t *usr_tkt_lifetime
,
306 time_t *renewal_lifetime
)
309 TDB_CONTEXT
*ctx
= NULL
;
310 const char *kdc_tdb
= NULL
;
312 kdc_tdb
= lpcfg_cache_path(mem_ctx
, lp_ctx
, "gpo.tdb");
314 ctx
= tdb_open(kdc_tdb
, 0, TDB_DEFAULT
, O_RDWR
, 0600);
316 if (!ctx
|| ( val
= tdb_fetch_lifetime(mem_ctx
, ctx
, "kdc:service_ticket_lifetime") ) == -1 )
317 val
= lpcfg_parm_long(lp_ctx
, NULL
, "kdc", "service ticket lifetime", 10);
318 *svc_tkt_lifetime
= val
* 60 * 60;
320 if (!ctx
|| ( val
= tdb_fetch_lifetime(mem_ctx
, ctx
, "kdc:user_ticket_lifetime") ) == -1 )
321 val
= lpcfg_parm_long(lp_ctx
, NULL
, "kdc", "user ticket lifetime", 10);
322 *usr_tkt_lifetime
= val
* 60 * 60;
324 if (!ctx
|| ( val
= tdb_fetch_lifetime(mem_ctx
, ctx
, "kdc:renewal_lifetime") ) == -1 )
325 val
= lpcfg_parm_long(lp_ctx
, NULL
, "kdc", "renewal lifetime", 24 * 7);
326 *renewal_lifetime
= val
* 60 * 60;