lib/param: swap preferred name for 'lock dir' to match s3
[nivanova/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
75 /**
76  A useful function for returning a path in the Samba lock directory.
77 **/
78 char *lpcfg_lock_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
79                          const char *name)
80 {
81         char *fname, *dname;
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, lpcfg_lockdir(lp_ctx));
90         trim_string(dname,"","/");
91         
92         if (!directory_exist(dname)) {
93                 if (!mkdir(dname,0755))
94                         DEBUG(1, ("Unable to create directory %s for file %s. "
95                               "Error was %s\n", dname, name, strerror(errno)));
96         }
97         
98         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
99
100         talloc_free(dname);
101
102         return fname;
103 }
104
105 /**
106  A useful function for returning a path in the Samba state directory.
107 **/
108 char *lpcfg_state_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
109                        const char *name)
110 {
111         char *fname, *dname;
112         if (name == NULL) {
113                 return NULL;
114         }
115         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
116                 return talloc_strdup(mem_ctx, name);
117         }
118
119         dname = talloc_strdup(mem_ctx, lpcfg_statedir(lp_ctx));
120         trim_string(dname,"","/");
121
122         if (!directory_exist(dname)) {
123                 mkdir(dname,0755);
124         }
125
126         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
127
128         talloc_free(dname);
129
130         return fname;
131 }
132
133 /**
134  A useful function for returning a path in the Samba cache directory.
135 **/
136 char *lpcfg_cache_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
137                        const char *name)
138 {
139         char *fname, *dname;
140         if (name == NULL) {
141                 return NULL;
142         }
143         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
144                 return talloc_strdup(mem_ctx, name);
145         }
146
147         dname = talloc_strdup(mem_ctx, lpcfg_cachedir(lp_ctx));
148         trim_string(dname,"","/");
149
150         if (!directory_exist(dname)) {
151                 mkdir(dname,0755);
152         }
153
154         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
155
156         talloc_free(dname);
157
158         return fname;
159 }
160
161 /**
162  * @brief Returns an absolute path to a file in the directory containing the current config file
163  *
164  * @param name File to find, relative to the config file directory.
165  *
166  * @retval Pointer to a talloc'ed string containing the full path.
167  **/
168
169 char *lpcfg_config_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
170                            const char *name)
171 {
172         char *fname, *config_dir, *p;
173         config_dir = talloc_strdup(mem_ctx, lpcfg_configfile(lp_ctx));
174         if (config_dir == NULL) {
175                 config_dir = talloc_strdup(mem_ctx, lp_default_path());
176         }
177         p = strrchr(config_dir, '/');
178         if (p == NULL) {
179                 talloc_free(config_dir);
180                 config_dir = talloc_strdup(mem_ctx, ".");
181                 if (config_dir == NULL) {
182                         return NULL;
183                 }
184         } else {
185                 p[0] = '\0';
186         }
187         fname = talloc_asprintf(mem_ctx, "%s/%s", config_dir, name);
188         talloc_free(config_dir);
189         return fname;
190 }
191
192 /**
193  * @brief Returns an absolute path to a file in the Samba private directory.
194  *
195  * @param name File to find, relative to PRIVATEDIR.
196  * if name is not relative, then use it as-is
197  *
198  * @retval Pointer to a talloc'ed string containing the full path.
199  **/
200 char *lpcfg_private_path(TALLOC_CTX* mem_ctx,
201                             struct loadparm_context *lp_ctx,
202                             const char *name)
203 {
204         char *fname;
205         if (name == NULL) {
206                 return NULL;
207         }
208         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
209                 return talloc_strdup(mem_ctx, name);
210         }
211         fname = talloc_asprintf(mem_ctx, "%s/%s", lpcfg_private_dir(lp_ctx), name);
212         return fname;
213 }
214
215 /**
216   return a path in the smbd.tmp directory, where all temporary file
217   for smbd go. If NULL is passed for name then return the directory 
218   path itself
219 */
220 char *smbd_tmp_path(TALLOC_CTX *mem_ctx, 
221                              struct loadparm_context *lp_ctx,
222                              const char *name)
223 {
224         char *fname, *dname;
225
226         dname = lpcfg_private_path(mem_ctx, lp_ctx, "smbd.tmp");
227         if (!directory_exist(dname)) {
228                 mkdir(dname,0755);
229         }
230
231         if (name == NULL) {
232                 return dname;
233         }
234
235         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
236         talloc_free(dname);
237
238         return fname;
239 }
240
241 const char *lpcfg_imessaging_path(TALLOC_CTX *mem_ctx,
242                                        struct loadparm_context *lp_ctx)
243 {
244         return smbd_tmp_path(mem_ctx, lp_ctx, "msg");
245 }
246
247 struct smb_iconv_handle *smb_iconv_handle_reinit_lp(TALLOC_CTX *mem_ctx,
248                                                               struct loadparm_context *lp_ctx,
249                                                               struct smb_iconv_handle *old_ic)
250 {
251         return smb_iconv_handle_reinit(mem_ctx, lpcfg_dos_charset(lp_ctx),
252                                        lpcfg_unix_charset(lp_ctx),
253                                        true,
254                                        old_ic);
255 }
256
257
258 const char *lpcfg_sam_name(struct loadparm_context *lp_ctx)
259 {
260         switch (lpcfg_server_role(lp_ctx)) {
261         case ROLE_DOMAIN_BDC:
262         case ROLE_DOMAIN_PDC:
263                 return lpcfg_workgroup(lp_ctx);
264         default:
265                 return lpcfg_netbios_name(lp_ctx);
266         }
267 }
268