s4: pass down a memory context when performing share_string_option, to allow substitu...
[kai/samba-autobuild/.git] / source4 / param / share_classic.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    Classic file based shares configuration
5    
6    Copyright (C) Simo Sorce     2006
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "param/share.h"
24 #include "param/param.h"
25
26 NTSTATUS share_classic_init(void);
27
28 static NTSTATUS sclassic_init(TALLOC_CTX *mem_ctx, 
29                               const struct share_ops *ops, 
30                               struct tevent_context *event_ctx,
31                               struct loadparm_context *lp_ctx,
32                               struct share_context **ctx)
33 {
34         *ctx = talloc(mem_ctx, struct share_context);
35         if (!*ctx) {
36                 DEBUG(0, ("ERROR: Out of memory!\n"));
37                 return NT_STATUS_NO_MEMORY;
38         }
39
40         (*ctx)->ops = ops;
41         (*ctx)->priv_data = lp_ctx;
42
43         return NT_STATUS_OK;
44 }
45
46 static char *sclassic_string_option(TALLOC_CTX *mem_ctx,
47                                     struct share_config *scfg,
48                                     const char *opt_name,
49                                     const char *defval)
50 {
51         struct loadparm_service *s = talloc_get_type(scfg->opaque, 
52                                                      struct loadparm_service);
53         struct loadparm_context *lp_ctx = talloc_get_type(scfg->ctx->priv_data,
54                                                           struct loadparm_context);
55         char *parm, *val;
56         const char *ret;
57
58         if (strchr(opt_name, ':')) {
59                 parm = talloc_strdup(scfg, opt_name);
60                 if (!parm) {
61                         return NULL;
62                 }
63                 val = strchr(parm, ':');
64                 *val = '\0';
65                 val++;
66
67                 ret = lpcfg_parm_string(lp_ctx, s, parm, val);
68                 if (!ret) {
69                         ret = defval;
70                 }
71                 talloc_free(parm);
72                 return talloc_strdup(mem_ctx, ret);
73         }
74
75         if (strcmp(opt_name, SHARE_NAME) == 0) {
76                 return talloc_strdup(mem_ctx, scfg->name);
77         }
78
79         if (strcmp(opt_name, SHARE_PATH) == 0) {
80                 return lpcfg_path(s, lpcfg_default_service(lp_ctx));
81         }
82
83         if (strcmp(opt_name, SHARE_COMMENT) == 0) {
84                 return lpcfg_comment(s, lpcfg_default_service(lp_ctx));
85         }
86
87         if (strcmp(opt_name, SHARE_VOLUME) == 0) {
88                 return talloc_strdup(mem_ctx, lpcfg_volume_label(s, lpcfg_default_service(lp_ctx)));
89         }
90
91         if (strcmp(opt_name, SHARE_TYPE) == 0) {
92                 if (lpcfg_printable(s, lpcfg_default_service(lp_ctx))) {
93                         return talloc_strdup(mem_ctx, "PRINTER");
94                 }
95                 if (strcmp("NTFS", lpcfg_fstype(s, lpcfg_default_service(lp_ctx))) == 0) {
96                         return talloc_strdup(mem_ctx, "DISK");
97                 }
98                 return talloc_strdup(mem_ctx, lpcfg_fstype(s, lpcfg_default_service(lp_ctx)));
99         }
100
101         if (strcmp(opt_name, SHARE_PASSWORD) == 0) {
102                 return talloc_strdup(mem_ctx, defval);
103         }
104
105         DEBUG(0,("request for unknown share string option '%s'\n",
106                  opt_name));
107
108         return talloc_strdup(mem_ctx, defval);
109 }
110
111 static int sclassic_int_option(struct share_config *scfg, const char *opt_name, int defval)
112 {
113         struct loadparm_service *s = talloc_get_type(scfg->opaque, 
114                                                      struct loadparm_service);
115         struct loadparm_context *lp_ctx = talloc_get_type(scfg->ctx->priv_data,
116                                                           struct loadparm_context);
117         char *parm, *val;
118         int ret;
119
120         if (strchr(opt_name, ':')) {
121                 parm = talloc_strdup(scfg, opt_name);
122                 if (!parm) {
123                         return -1;
124                 }
125                 val = strchr(parm, ':');
126                 *val = '\0';
127                 val++;
128
129                 ret = lpcfg_parm_int(lp_ctx, s, parm, val, defval);
130                 if (!ret) {
131                         ret = defval;
132                 }
133                 talloc_free(parm);
134                 return ret;
135         }
136
137         if (strcmp(opt_name, SHARE_CSC_POLICY) == 0) {
138                 return lpcfg_csc_policy(s, lpcfg_default_service(lp_ctx));
139         }
140
141         if (strcmp(opt_name, SHARE_MAX_CONNECTIONS) == 0) {
142                 return lpcfg_max_connections(s, lpcfg_default_service(lp_ctx));
143         }
144
145         if (strcmp(opt_name, SHARE_CREATE_MASK) == 0) {
146                 return lpcfg_create_mask(s, lpcfg_default_service(lp_ctx));
147         }
148
149         if (strcmp(opt_name, SHARE_DIR_MASK) == 0) {
150                 return lpcfg_directory_mask(s, lpcfg_default_service(lp_ctx));
151         }
152
153         if (strcmp(opt_name, SHARE_FORCE_DIR_MODE) == 0) {
154                 return lpcfg_force_directory_mode(s, lpcfg_default_service(lp_ctx));
155         }
156
157         if (strcmp(opt_name, SHARE_FORCE_CREATE_MODE) == 0) {
158                 return lpcfg_force_create_mode(s, lpcfg_default_service(lp_ctx));
159         }
160
161
162         DEBUG(0,("request for unknown share int option '%s'\n",
163                  opt_name));
164
165         return defval;
166 }
167
168 static bool sclassic_bool_option(struct share_config *scfg, const char *opt_name, 
169                           bool defval)
170 {
171         struct loadparm_service *s = talloc_get_type(scfg->opaque, 
172                                                      struct loadparm_service);
173         struct loadparm_context *lp_ctx = talloc_get_type(scfg->ctx->priv_data,
174                                                           struct loadparm_context);
175         char *parm, *val;
176         bool ret;
177
178         if (strchr(opt_name, ':')) {
179                 parm = talloc_strdup(scfg, opt_name);
180                 if(!parm) {
181                         return false;
182                 }
183                 val = strchr(parm, ':');
184                 *val = '\0';
185                 val++;
186
187                 ret = lpcfg_parm_bool(lp_ctx, s, parm, val, defval);
188                 talloc_free(parm);
189                 return ret;
190         }
191
192         if (strcmp(opt_name, SHARE_AVAILABLE) == 0) {
193                 return s != NULL;
194         }
195
196         if (strcmp(opt_name, SHARE_BROWSEABLE) == 0) {
197                 return lpcfg_browseable(s, lpcfg_default_service(lp_ctx));
198         }
199
200         if (strcmp(opt_name, SHARE_READONLY) == 0) {
201                 return lpcfg_read_only(s, lpcfg_default_service(lp_ctx));
202         }
203
204         if (strcmp(opt_name, SHARE_MAP_SYSTEM) == 0) {
205                 return lpcfg_map_system(s, lpcfg_default_service(lp_ctx));
206         }
207
208         if (strcmp(opt_name, SHARE_MAP_HIDDEN) == 0) {
209                 return lpcfg_map_hidden(s, lpcfg_default_service(lp_ctx));
210         }
211
212         if (strcmp(opt_name, SHARE_MAP_ARCHIVE) == 0) {
213                 return lpcfg_map_archive(s, lpcfg_default_service(lp_ctx));
214         }
215
216         if (strcmp(opt_name, SHARE_STRICT_LOCKING) == 0) {
217                 return lpcfg_strict_locking(s, lpcfg_default_service(lp_ctx));
218         }
219
220         if (strcmp(opt_name, SHARE_OPLOCKS) == 0) {
221                 return lpcfg_oplocks(s, lpcfg_default_service(lp_ctx));
222         }
223
224         if (strcmp(opt_name, SHARE_STRICT_SYNC) == 0) {
225                 return lpcfg_strict_sync(s, lpcfg_default_service(lp_ctx));
226         }
227
228         if (strcmp(opt_name, SHARE_MSDFS_ROOT) == 0) {
229                 return lpcfg_msdfs_root(s, lpcfg_default_service(lp_ctx));
230         }
231
232         if (strcmp(opt_name, SHARE_CI_FILESYSTEM) == 0) {
233                 int case_sensitive = lpcfg_case_sensitive(s, lpcfg_default_service(lp_ctx));
234                 /*
235                  * Yes, this confusingly named option means Samba acts
236                  * case sensitive, so that the filesystem can act case
237                  * insensitive.
238                  *
239                  */
240                 if (case_sensitive == Auto) {
241                         /* Auto is for unix extensions and unix
242                          * clients, which we don't support here.
243                          * Samba needs to do the case changing,
244                          * because the filesystem is case
245                          * sensitive  */
246                         return false;
247                 } else if (case_sensitive) {
248                         /* True means that Samba won't do anything to
249                          * change the case of incoming requests.
250                          * Essentially this means we trust the file
251                          * system to be case insensitive */
252                         return true;
253                 } else {
254                         /* False means that Smaba needs to do the case
255                          * changing, because the filesystem is case
256                          * sensitive */
257                         return false;
258                 }
259         }
260
261         DEBUG(0,("request for unknown share bool option '%s'\n",
262                  opt_name));
263
264         return defval;
265 }
266
267 static const char **sclassic_string_list_option(TALLOC_CTX *mem_ctx, struct share_config *scfg, const char *opt_name)
268 {
269         struct loadparm_service *s = talloc_get_type(scfg->opaque, 
270                                                      struct loadparm_service);
271         struct loadparm_context *lp_ctx = talloc_get_type(scfg->ctx->priv_data,
272                                                           struct loadparm_context);
273         char *parm, *val;
274         const char **ret;
275
276         if (strchr(opt_name, ':')) {
277                 parm = talloc_strdup(scfg, opt_name);
278                 if (!parm) {
279                         return NULL;
280                 }
281                 val = strchr(parm, ':');
282                 *val = '\0';
283                 val++;
284
285                 ret = lpcfg_parm_string_list(mem_ctx, lp_ctx, s, parm, val, ",;");
286                 talloc_free(parm);
287                 return ret;
288         }
289
290         if (strcmp(opt_name, SHARE_HOSTS_ALLOW) == 0) {
291                 return lpcfg_hosts_allow(s, lpcfg_default_service(lp_ctx));
292         }
293
294         if (strcmp(opt_name, SHARE_HOSTS_DENY) == 0) {
295                 return lpcfg_hosts_deny(s, lpcfg_default_service(lp_ctx));
296         }
297
298         if (strcmp(opt_name, SHARE_NTVFS_HANDLER) == 0) {
299                 return lpcfg_ntvfs_handler(s, lpcfg_default_service(lp_ctx));
300         }
301
302         DEBUG(0,("request for unknown share list option '%s'\n",
303                  opt_name));
304
305         return NULL;
306 }
307
308 static NTSTATUS sclassic_list_all(TALLOC_CTX *mem_ctx,
309                                   struct share_context *ctx,
310                                   int *count,
311                                   const char ***names)
312 {
313         int i;
314         int num_services;
315         const char **n;
316        
317         num_services = lpcfg_numservices((struct loadparm_context *)ctx->priv_data);
318
319         n = talloc_array(mem_ctx, const char *, num_services);
320         if (!n) {
321                 DEBUG(0,("ERROR: Out of memory!\n"));
322                 return NT_STATUS_NO_MEMORY;
323         }
324
325         for (i = 0; i < num_services; i++) {
326                 n[i] = talloc_strdup(n, lpcfg_servicename(lpcfg_servicebynum((struct loadparm_context *)ctx->priv_data, i)));
327                 if (!n[i]) {
328                         DEBUG(0,("ERROR: Out of memory!\n"));
329                         talloc_free(n);
330                         return NT_STATUS_NO_MEMORY;
331                 }
332         }
333
334         *names = n;
335         *count = num_services;
336
337         return NT_STATUS_OK;
338 }
339
340 static NTSTATUS sclassic_get_config(TALLOC_CTX *mem_ctx,
341                                     struct share_context *ctx,
342                                     const char *name,
343                                     struct share_config **scfg)
344 {
345         struct share_config *s;
346         struct loadparm_service *service;
347
348         service = lpcfg_service((struct loadparm_context *)ctx->priv_data, name);
349
350         if (service == NULL) {
351                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
352         }
353
354         s = talloc(mem_ctx, struct share_config);
355         if (!s) {
356                 DEBUG(0,("ERROR: Out of memory!\n"));
357                 return NT_STATUS_NO_MEMORY;
358         }
359
360         s->name = talloc_strdup(s, lpcfg_servicename(service));
361         if (!s->name) {
362                 DEBUG(0,("ERROR: Out of memory!\n"));
363                 talloc_free(s);
364                 return NT_STATUS_NO_MEMORY;
365         }
366
367         s->opaque = (void *)service;
368         s->ctx = ctx;
369         
370         *scfg = s;
371
372         return NT_STATUS_OK;
373 }
374
375 static const struct share_ops ops = {
376         .name = "classic",
377         .init = sclassic_init,
378         .string_option = sclassic_string_option,
379         .int_option = sclassic_int_option,
380         .bool_option = sclassic_bool_option,
381         .string_list_option = sclassic_string_list_option,
382         .list_all = sclassic_list_all,
383         .get_config = sclassic_get_config
384 };
385
386 NTSTATUS share_classic_init(void)
387 {
388         return share_register(&ops);
389 }
390