Refactor the dns_open_connection code so that duplicate code is removed and ensure...
[sfrench/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 #include "libds/common/roles.h"
32
33 /**
34  * @file
35  * @brief Misc utility functions
36  */
37
38
39 bool lpcfg_is_mydomain(struct loadparm_context *lp_ctx,
40                              const char *domain)
41 {
42         return strequal(lpcfg_workgroup(lp_ctx), domain);
43 }
44
45 bool lpcfg_is_my_domain_or_realm(struct loadparm_context *lp_ctx,
46                              const char *domain)
47 {
48         return strequal(lpcfg_workgroup(lp_ctx), domain) ||
49                 strequal(lpcfg_realm(lp_ctx), domain);
50 }
51
52 /**
53   see if a string matches either our primary or one of our secondary 
54   netbios aliases. do a case insensitive match
55 */
56 bool lpcfg_is_myname(struct loadparm_context *lp_ctx, const char *name)
57 {
58         const char **aliases;
59         int i;
60
61         if (strcasecmp_m(name, lpcfg_netbios_name(lp_ctx)) == 0) {
62                 return true;
63         }
64
65         aliases = lpcfg_netbios_aliases(lp_ctx);
66         for (i=0; aliases && aliases[i]; i++) {
67                 if (strcasecmp_m(name, aliases[i]) == 0) {
68                         return true;
69                 }
70         }
71
72         return false;
73 }
74
75 static char *lpcfg_common_path(TALLOC_CTX* mem_ctx,
76                                const char *parent,
77                                const char *name)
78 {
79         char *fname, *dname;
80         bool ok;
81
82         if (name == NULL) {
83                 return NULL;
84         }
85         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
86                 return talloc_strdup(mem_ctx, name);
87         }
88
89         dname = talloc_strdup(mem_ctx, parent);
90         if (dname == NULL) {
91                 return NULL;
92         }
93         trim_string(dname,"","/");
94
95         ok = directory_create_or_exist(dname, 0755);
96         if (!ok) {
97                 DEBUG(1, ("Unable to create directory %s for file %s. "
98                           "Error was %s\n", dname, name, strerror(errno)));
99                 return NULL;
100         }
101
102         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
103         if (fname == NULL) {
104                 return dname;
105         }
106         talloc_free(dname);
107
108         return fname;
109 }
110
111
112 /**
113  A useful function for returning a path in the Samba lock directory.
114 **/
115 char *lpcfg_lock_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
116                          const char *name)
117 {
118         return lpcfg_common_path(mem_ctx, lpcfg_lock_directory(lp_ctx), name);
119 }
120
121 /**
122  A useful function for returning a path in the Samba state directory.
123 **/
124 char *lpcfg_state_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
125                        const char *name)
126 {
127         return lpcfg_common_path(mem_ctx, lpcfg_state_directory(lp_ctx), name);
128 }
129
130 /**
131  A useful function for returning a path in the Samba cache directory.
132 **/
133 char *lpcfg_cache_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
134                        const char *name)
135 {
136         return lpcfg_common_path(mem_ctx, lpcfg_cache_directory(lp_ctx), name);
137 }
138
139 /**
140  * @brief Returns an absolute path to a file in the directory containing the current config file
141  *
142  * @param name File to find, relative to the config file directory.
143  *
144  * @retval Pointer to a talloc'ed string containing the full path.
145  **/
146
147 char *lpcfg_config_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
148                            const char *name)
149 {
150         char *fname, *config_dir, *p;
151         config_dir = talloc_strdup(mem_ctx, lpcfg_configfile(lp_ctx));
152         if (config_dir == NULL) {
153                 config_dir = talloc_strdup(mem_ctx, lp_default_path());
154         }
155         p = strrchr(config_dir, '/');
156         if (p == NULL) {
157                 talloc_free(config_dir);
158                 config_dir = talloc_strdup(mem_ctx, ".");
159                 if (config_dir == NULL) {
160                         return NULL;
161                 }
162         } else {
163                 p[0] = '\0';
164         }
165         fname = talloc_asprintf(mem_ctx, "%s/%s", config_dir, name);
166         talloc_free(config_dir);
167         return fname;
168 }
169
170 /**
171  * @brief Returns an absolute path to a file in the Samba private directory.
172  *
173  * @param name File to find, relative to PRIVATEDIR.
174  * if name is not relative, then use it as-is
175  *
176  * @retval Pointer to a talloc'ed string containing the full path.
177  **/
178 char *lpcfg_private_path(TALLOC_CTX* mem_ctx,
179                             struct loadparm_context *lp_ctx,
180                             const char *name)
181 {
182         char *fname;
183         if (name == NULL) {
184                 return NULL;
185         }
186         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
187                 return talloc_strdup(mem_ctx, name);
188         }
189         fname = talloc_asprintf(mem_ctx, "%s/%s", lpcfg_private_dir(lp_ctx), name);
190         return fname;
191 }
192
193 /**
194  * @brief Returns an absolute path to a NTDB or TDB file in the Samba
195  * private directory.
196  *
197  * @param name File to find, relative to PRIVATEDIR, without .tdb extension.
198  *
199  * @retval Pointer to a talloc'ed string containing the full path, for
200  * use with dbwrap_local_open().
201  **/
202 char *lpcfg_private_db_path(TALLOC_CTX *mem_ctx,
203                             struct loadparm_context *lp_ctx,
204                             const char *name)
205 {
206         return talloc_asprintf(mem_ctx, "%s/%s.tdb",
207                                lpcfg_private_dir(lp_ctx), name);
208 }
209
210 /**
211   return a path in the smbd.tmp directory, where all temporary file
212   for smbd go. If NULL is passed for name then return the directory 
213   path itself
214 */
215 char *smbd_tmp_path(TALLOC_CTX *mem_ctx, 
216                              struct loadparm_context *lp_ctx,
217                              const char *name)
218 {
219         char *fname, *dname;
220         bool ok;
221
222         dname = lpcfg_private_path(mem_ctx, lp_ctx, "smbd.tmp");
223         if (dname == NULL) {
224                 return NULL;
225         }
226
227         ok = directory_create_or_exist(dname, 0755);
228         if (!ok) {
229                 return NULL;
230         }
231
232         if (name == NULL) {
233                 return dname;
234         }
235
236         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
237         if (fname == NULL) {
238                 return dname;
239         }
240         talloc_free(dname);
241
242         return fname;
243 }
244
245 const char *lpcfg_imessaging_path(TALLOC_CTX *mem_ctx,
246                                        struct loadparm_context *lp_ctx)
247 {
248         return smbd_tmp_path(mem_ctx, lp_ctx, "msg");
249 }
250
251 struct smb_iconv_handle *smb_iconv_handle_reinit_lp(TALLOC_CTX *mem_ctx,
252                                                               struct loadparm_context *lp_ctx,
253                                                               struct smb_iconv_handle *old_ic)
254 {
255         return smb_iconv_handle_reinit(mem_ctx, lpcfg_dos_charset(lp_ctx),
256                                        lpcfg_unix_charset(lp_ctx),
257                                        true,
258                                        old_ic);
259 }
260
261
262 const char *lpcfg_sam_name(struct loadparm_context *lp_ctx)
263 {
264         switch (lpcfg_server_role(lp_ctx)) {
265         case ROLE_DOMAIN_BDC:
266         case ROLE_DOMAIN_PDC:
267         case ROLE_ACTIVE_DIRECTORY_DC:
268                 return lpcfg_workgroup(lp_ctx);
269         default:
270                 return lpcfg_netbios_name(lp_ctx);
271         }
272 }
273
274 void lpcfg_default_kdc_policy(struct loadparm_context *lp_ctx,
275                                 time_t *svc_tkt_lifetime,
276                                 time_t *usr_tkt_lifetime,
277                                 time_t *renewal_lifetime)
278 {
279         long val;
280
281         val = lpcfg_parm_long(lp_ctx, NULL,
282                                 "kdc", "service ticket lifetime", 10);
283         *svc_tkt_lifetime = val * 60 * 60;
284
285         val = lpcfg_parm_long(lp_ctx, NULL,
286                                 "kdc", "user ticket lifetime", 10);
287         *usr_tkt_lifetime = val * 60 * 60;
288
289         val = lpcfg_parm_long(lp_ctx, NULL,
290                                 "kdc", "renewal lifetime", 24 * 7);
291         *renewal_lifetime = val * 60 * 60;
292 }