r25039: Rename service -> loadparm_service, use context more.
[sfrench/samba-autobuild/.git] / source4 / param / share_classic.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    Classic file based services 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 static NTSTATUS sclassic_init(TALLOC_CTX *mem_ctx, const struct share_ops *ops, struct share_context **ctx)
27 {
28         *ctx = talloc(mem_ctx, struct share_context);
29         if (!*ctx) {
30                 DEBUG(0, ("ERROR: Out of memory!\n"));
31                 return NT_STATUS_NO_MEMORY;
32         }
33
34         (*ctx)->ops = ops;
35         (*ctx)->priv_data = NULL;
36
37         return NT_STATUS_OK;
38 }
39
40 static const char *sclassic_string_option(struct share_config *scfg, const char *opt_name, const char *defval)
41 {
42         struct loadparm_service *s = talloc_get_type(scfg->opaque, 
43                                                      struct loadparm_service);
44         char *parm, *val;
45         const char *ret;
46
47         if (strchr(opt_name, ':')) {
48                 parm = talloc_strdup(scfg, opt_name);
49                 if (!parm) {
50                         return NULL;
51                 }
52                 val = strchr(parm, ':');
53                 *val = '\0';
54                 val++;
55
56                 ret = lp_parm_string(s, parm, val);
57                 if (!ret) {
58                         ret = defval;
59                 }
60                 talloc_free(parm);
61                 return ret;
62         }
63
64         if (strcmp(opt_name, SHARE_NAME) == 0) {
65                 return scfg->name;
66         }
67
68         if (strcmp(opt_name, SHARE_PATH) == 0) {
69                 return lp_pathname(s);
70         }
71
72         if (strcmp(opt_name, SHARE_COMMENT) == 0) {
73                 return lp_comment(s);
74         }
75
76         if (strcmp(opt_name, SHARE_VOLUME) == 0) {
77                 return volume_label(s);
78         }
79
80         if (strcmp(opt_name, SHARE_TYPE) == 0) {
81                 if (lp_print_ok(s)) {
82                         return "PRINTER";
83                 }
84                 if (strcmp("NTFS", lp_fstype(s)) == 0) {
85                         return "DISK";
86                 }
87                 return lp_fstype(s);
88         }
89
90         if (strcmp(opt_name, SHARE_PASSWORD) == 0) {
91                 return defval;
92         }
93
94         DEBUG(0,("request for unknown share string option '%s'\n",
95                  opt_name));
96
97         return defval;
98 }
99
100 static int sclassic_int_option(struct share_config *scfg, const char *opt_name, int defval)
101 {
102         struct loadparm_service *s = talloc_get_type(scfg->opaque, 
103                                                      struct loadparm_service);
104         char *parm, *val;
105         int ret;
106
107         if (strchr(opt_name, ':')) {
108                 parm = talloc_strdup(scfg, opt_name);
109                 if (!parm) {
110                         return -1;
111                 }
112                 val = strchr(parm, ':');
113                 *val = '\0';
114                 val++;
115
116                 ret = lp_parm_int(s, parm, val, defval);
117                 if (!ret) {
118                         ret = defval;
119                 }
120                 talloc_free(parm);
121                 return ret;
122         }
123
124         if (strcmp(opt_name, SHARE_CSC_POLICY) == 0) {
125                 return lp_csc_policy(s);
126         }
127
128         if (strcmp(opt_name, SHARE_MAX_CONNECTIONS) == 0) {
129                 return lp_max_connections(s);
130         }
131
132         if (strcmp(opt_name, SHARE_CREATE_MASK) == 0) {
133                 return lp_create_mask(s);
134         }
135
136         if (strcmp(opt_name, SHARE_DIR_MASK) == 0) {
137                 return lp_dir_mask(s);
138         }
139
140         if (strcmp(opt_name, SHARE_FORCE_DIR_MODE) == 0) {
141                 return lp_force_dir_mode(s);
142         }
143
144         if (strcmp(opt_name, SHARE_FORCE_CREATE_MODE) == 0) {
145                 return lp_force_create_mode(s);
146         }
147
148
149         DEBUG(0,("request for unknown share int option '%s'\n",
150                  opt_name));
151
152         return defval;
153 }
154
155 static bool sclassic_bool_option(struct share_config *scfg, const char *opt_name, 
156                           bool defval)
157 {
158         struct loadparm_service *s = talloc_get_type(scfg->opaque, 
159                                                      struct loadparm_service);
160         char *parm, *val;
161         BOOL ret;
162
163         if (strchr(opt_name, ':')) {
164                 parm = talloc_strdup(scfg, opt_name);
165                 if(!parm) {
166                         return False;
167                 }
168                 val = strchr(parm, ':');
169                 *val = '\0';
170                 val++;
171
172                 ret = lp_parm_bool(s, parm, val, defval);
173                 talloc_free(parm);
174                 return ret;
175         }
176
177         if (strcmp(opt_name, SHARE_AVAILABLE) == 0) {
178                 return s != NULL;
179         }
180
181         if (strcmp(opt_name, SHARE_BROWSEABLE) == 0) {
182                 return lp_browseable(s);
183         }
184
185         if (strcmp(opt_name, SHARE_READONLY) == 0) {
186                 return lp_readonly(s);
187         }
188
189         if (strcmp(opt_name, SHARE_MAP_SYSTEM) == 0) {
190                 return lp_map_system(s);
191         }
192
193         if (strcmp(opt_name, SHARE_MAP_HIDDEN) == 0) {
194                 return lp_map_hidden(s);
195         }
196
197         if (strcmp(opt_name, SHARE_MAP_ARCHIVE) == 0) {
198                 return lp_map_archive(s);
199         }
200
201         if (strcmp(opt_name, SHARE_STRICT_LOCKING) == 0) {
202                 return lp_strict_locking(s);
203         }
204
205         if (strcmp(opt_name, SHARE_STRICT_SYNC) == 0) {
206                 return lp_strict_sync(s);
207         }
208
209         if (strcmp(opt_name, SHARE_MSDFS_ROOT) == 0) {
210                 return lp_msdfs_root(s);
211         }
212
213         if (strcmp(opt_name, SHARE_CI_FILESYSTEM) == 0) {
214                 return lp_ci_filesystem(s);
215         }
216
217         DEBUG(0,("request for unknown share bool option '%s'\n",
218                  opt_name));
219
220         return defval;
221 }
222
223 static const char **sclassic_string_list_option(TALLOC_CTX *mem_ctx, struct share_config *scfg, const char *opt_name)
224 {
225         struct loadparm_service *s = talloc_get_type(scfg->opaque, 
226                                                      struct loadparm_service);
227         char *parm, *val;
228         const char **ret;
229
230         if (strchr(opt_name, ':')) {
231                 parm = talloc_strdup(scfg, opt_name);
232                 if (!parm) {
233                         return NULL;
234                 }
235                 val = strchr(parm, ':');
236                 *val = '\0';
237                 val++;
238
239                 ret = lp_parm_string_list(s, parm, val, ",;");
240                 talloc_free(parm);
241                 return ret;
242         }
243
244         if (strcmp(opt_name, SHARE_HOSTS_ALLOW) == 0) {
245                 return lp_hostsallow(s);
246         }
247
248         if (strcmp(opt_name, SHARE_HOSTS_DENY) == 0) {
249                 return lp_hostsdeny(s);
250         }
251
252         if (strcmp(opt_name, SHARE_NTVFS_HANDLER) == 0) {
253                 return lp_ntvfs_handler(s);
254         }
255
256         DEBUG(0,("request for unknown share list option '%s'\n",
257                  opt_name));
258
259         return NULL;
260 }
261
262 static NTSTATUS sclassic_list_all(TALLOC_CTX *mem_ctx,
263                                   struct share_context *ctx,
264                                   int *count,
265                                   const char ***names)
266 {
267         int i;
268         int num_services;
269         const char **n;
270        
271         num_services = lp_numservices();
272
273         n = talloc_array(mem_ctx, const char *, num_services);
274         if (!n) {
275                 DEBUG(0,("ERROR: Out of memory!\n"));
276                 return NT_STATUS_NO_MEMORY;
277         }
278
279         for (i = 0; i < num_services; i++) {
280                 n[i] = talloc_strdup(n, lp_servicename(lp_servicebynum(i)));
281                 if (!n[i]) {
282                         DEBUG(0,("ERROR: Out of memory!\n"));
283                         talloc_free(n);
284                         return NT_STATUS_NO_MEMORY;
285                 }
286         }
287
288         *names = n;
289         *count = num_services;
290
291         return NT_STATUS_OK;
292 }
293
294 static NTSTATUS sclassic_get_config(TALLOC_CTX *mem_ctx,
295                                     struct share_context *ctx,
296                                     const char *name,
297                                     struct share_config **scfg)
298 {
299         struct share_config *s;
300         struct loadparm_service *service;
301
302         service = lp_service(name);
303
304         if (service == NULL) {
305                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
306         }
307
308         s = talloc(mem_ctx, struct share_config);
309         if (!s) {
310                 DEBUG(0,("ERROR: Out of memory!\n"));
311                 return NT_STATUS_NO_MEMORY;
312         }
313
314         s->name = talloc_strdup(s, lp_servicename(service));
315         if (!s->name) {
316                 DEBUG(0,("ERROR: Out of memory!\n"));
317                 talloc_free(s);
318                 return NT_STATUS_NO_MEMORY;
319         }
320
321         s->opaque = (void *)service;
322         s->ctx = ctx;
323         
324         *scfg = s;
325
326         return NT_STATUS_OK;
327 }
328
329 NTSTATUS share_classic_init(void)
330 {
331         static struct share_ops ops = {
332                 .name = "classic",
333                 .init = sclassic_init,
334                 .string_option = sclassic_string_option,
335                 .int_option = sclassic_int_option,
336                 .bool_option = sclassic_bool_option,
337                 .string_list_option = sclassic_string_list_option,
338                 .list_all = sclassic_list_all,
339                 .get_config = sclassic_get_config
340         };
341
342         return share_register(&ops);
343 }
344