lib/param: Remove special handling for 'state dir' and 'cache dir'
[kai/samba-autobuild/.git] / lib / 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_m(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_m(name, aliases[i]) == 0) {
67                         return true;
68                 }
69         }
70
71         return false;
72 }
73
74 static char *lpcfg_common_path(TALLOC_CTX* mem_ctx,
75                                const char *parent,
76                                const char *name)
77 {
78         char *fname, *dname;
79         bool ok;
80
81         if (name == NULL) {
82                 return NULL;
83         }
84         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
85                 return talloc_strdup(mem_ctx, name);
86         }
87
88         dname = talloc_strdup(mem_ctx, parent);
89         if (dname == NULL) {
90                 return NULL;
91         }
92         trim_string(dname,"","/");
93
94         ok = directory_create_or_exist(dname, geteuid(), 0755);
95         if (!ok) {
96                 DEBUG(1, ("Unable to create directory %s for file %s. "
97                           "Error was %s\n", dname, name, strerror(errno)));
98                 return NULL;
99         }
100
101         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
102         if (fname == NULL) {
103                 return dname;
104         }
105         talloc_free(dname);
106
107         return fname;
108 }
109
110
111 /**
112  A useful function for returning a path in the Samba lock directory.
113 **/
114 char *lpcfg_lock_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
115                          const char *name)
116 {
117         return lpcfg_common_path(mem_ctx, lpcfg_lock_directory(lp_ctx), name);
118 }
119
120 /**
121  A useful function for returning a path in the Samba state directory.
122 **/
123 char *lpcfg_state_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
124                        const char *name)
125 {
126         return lpcfg_common_path(mem_ctx, lpcfg_state_directory(lp_ctx), name);
127 }
128
129 /**
130  A useful function for returning a path in the Samba cache directory.
131 **/
132 char *lpcfg_cache_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
133                        const char *name)
134 {
135         return lpcfg_common_path(mem_ctx, lpcfg_cache_directory(lp_ctx), name);
136 }
137
138 /**
139  * @brief Returns an absolute path to a file in the directory containing the current config file
140  *
141  * @param name File to find, relative to the config file directory.
142  *
143  * @retval Pointer to a talloc'ed string containing the full path.
144  **/
145
146 char *lpcfg_config_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
147                            const char *name)
148 {
149         char *fname, *config_dir, *p;
150         config_dir = talloc_strdup(mem_ctx, lpcfg_configfile(lp_ctx));
151         if (config_dir == NULL) {
152                 config_dir = talloc_strdup(mem_ctx, lp_default_path());
153         }
154         p = strrchr(config_dir, '/');
155         if (p == NULL) {
156                 talloc_free(config_dir);
157                 config_dir = talloc_strdup(mem_ctx, ".");
158                 if (config_dir == NULL) {
159                         return NULL;
160                 }
161         } else {
162                 p[0] = '\0';
163         }
164         fname = talloc_asprintf(mem_ctx, "%s/%s", config_dir, name);
165         talloc_free(config_dir);
166         return fname;
167 }
168
169 /**
170  * @brief Returns an absolute path to a file in the Samba private directory.
171  *
172  * @param name File to find, relative to PRIVATEDIR.
173  * if name is not relative, then use it as-is
174  *
175  * @retval Pointer to a talloc'ed string containing the full path.
176  **/
177 char *lpcfg_private_path(TALLOC_CTX* mem_ctx,
178                             struct loadparm_context *lp_ctx,
179                             const char *name)
180 {
181         char *fname;
182         if (name == NULL) {
183                 return NULL;
184         }
185         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
186                 return talloc_strdup(mem_ctx, name);
187         }
188         fname = talloc_asprintf(mem_ctx, "%s/%s", lpcfg_private_dir(lp_ctx), name);
189         return fname;
190 }
191
192 /**
193  * @brief Returns an absolute path to a NTDB or TDB file in the Samba
194  * private directory.
195  *
196  * @param name File to find, relative to PRIVATEDIR, without .(n)tdb extension.
197  * Only provide fixed-string names which are supposed to change with "use ntdb"
198  * option.
199  *
200  * @retval Pointer to a talloc'ed string containing the full path, for
201  * use with dbwrap_local_open().
202  **/
203 char *lpcfg_private_db_path(TALLOC_CTX *mem_ctx,
204                             struct loadparm_context *lp_ctx,
205                             const char *name)
206 {
207         const char *extension = ".tdb";
208
209         if (lpcfg_use_ntdb(lp_ctx)) {
210                 extension = ".ntdb";
211         }
212
213         return talloc_asprintf(mem_ctx, "%s/%s%s",
214                                lpcfg_private_dir(lp_ctx), name, extension);
215 }
216
217 /**
218   return a path in the smbd.tmp directory, where all temporary file
219   for smbd go. If NULL is passed for name then return the directory 
220   path itself
221 */
222 char *smbd_tmp_path(TALLOC_CTX *mem_ctx, 
223                              struct loadparm_context *lp_ctx,
224                              const char *name)
225 {
226         char *fname, *dname;
227         bool ok;
228
229         dname = lpcfg_private_path(mem_ctx, lp_ctx, "smbd.tmp");
230         if (dname == NULL) {
231                 return NULL;
232         }
233
234         ok = directory_create_or_exist(dname, geteuid(), 0755);
235         if (!ok) {
236                 return NULL;
237         }
238
239         if (name == NULL) {
240                 return dname;
241         }
242
243         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
244         if (fname == NULL) {
245                 return dname;
246         }
247         talloc_free(dname);
248
249         return fname;
250 }
251
252 const char *lpcfg_imessaging_path(TALLOC_CTX *mem_ctx,
253                                        struct loadparm_context *lp_ctx)
254 {
255         return smbd_tmp_path(mem_ctx, lp_ctx, "msg");
256 }
257
258 struct smb_iconv_handle *smb_iconv_handle_reinit_lp(TALLOC_CTX *mem_ctx,
259                                                               struct loadparm_context *lp_ctx,
260                                                               struct smb_iconv_handle *old_ic)
261 {
262         return smb_iconv_handle_reinit(mem_ctx, lpcfg_dos_charset(lp_ctx),
263                                        lpcfg_unix_charset(lp_ctx),
264                                        true,
265                                        old_ic);
266 }
267
268
269 const char *lpcfg_sam_name(struct loadparm_context *lp_ctx)
270 {
271         switch (lpcfg_server_role(lp_ctx)) {
272         case ROLE_DOMAIN_BDC:
273         case ROLE_DOMAIN_PDC:
274         case ROLE_ACTIVE_DIRECTORY_DC:
275                 return lpcfg_workgroup(lp_ctx);
276         default:
277                 return lpcfg_netbios_name(lp_ctx);
278         }
279 }
280
281 void lpcfg_default_kdc_policy(struct loadparm_context *lp_ctx,
282                                 time_t *svc_tkt_lifetime,
283                                 time_t *usr_tkt_lifetime,
284                                 time_t *renewal_lifetime)
285 {
286         long val;
287
288         val = lpcfg_parm_long(lp_ctx, NULL,
289                                 "kdc", "service ticket lifetime", 10);
290         *svc_tkt_lifetime = val * 60 * 60;
291
292         val = lpcfg_parm_long(lp_ctx, NULL,
293                                 "kdc", "user ticket lifetime", 10);
294         *usr_tkt_lifetime = val * 60 * 60;
295
296         val = lpcfg_parm_long(lp_ctx, NULL,
297                                 "kdc", "renewal lifetime", 24 * 7);
298         *renewal_lifetime = val * 60 * 60;
299 }