r25035: Fix some more warnings, use service pointer rather than service number in...
[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 struct service {
27         struct service *service;
28 };
29
30 static NTSTATUS sclassic_init(TALLOC_CTX *mem_ctx, const struct share_ops *ops, struct share_context **ctx)
31 {
32         *ctx = talloc(mem_ctx, struct share_context);
33         if (!*ctx) {
34                 DEBUG(0, ("ERROR: Out of memory!\n"));
35                 return NT_STATUS_NO_MEMORY;
36         }
37
38         (*ctx)->ops = ops;
39         (*ctx)->priv_data = NULL;
40
41         return NT_STATUS_OK;
42 }
43
44 static const char *sclassic_string_option(struct share_config *scfg, const char *opt_name, const char *defval)
45 {
46         struct service *s = talloc_get_type(scfg->opaque, struct service);
47         char *parm, *val;
48         const char *ret;
49
50         if (strchr(opt_name, ':')) {
51                 parm = talloc_strdup(scfg, opt_name);
52                 if (!parm) {
53                         return NULL;
54                 }
55                 val = strchr(parm, ':');
56                 *val = '\0';
57                 val++;
58
59                 ret = lp_parm_string(s, parm, val);
60                 if (!ret) {
61                         ret = defval;
62                 }
63                 talloc_free(parm);
64                 return ret;
65         }
66
67         if (strcmp(opt_name, SHARE_NAME) == 0) {
68                 return scfg->name;
69         }
70
71         if (strcmp(opt_name, SHARE_PATH) == 0) {
72                 return lp_pathname(s);
73         }
74
75         if (strcmp(opt_name, SHARE_COMMENT) == 0) {
76                 return lp_comment(s);
77         }
78
79         if (strcmp(opt_name, SHARE_VOLUME) == 0) {
80                 return volume_label(s);
81         }
82
83         if (strcmp(opt_name, SHARE_TYPE) == 0) {
84                 if (lp_print_ok(s)) {
85                         return "PRINTER";
86                 }
87                 if (strcmp("NTFS", lp_fstype(s)) == 0) {
88                         return "DISK";
89                 }
90                 return lp_fstype(s);
91         }
92
93         if (strcmp(opt_name, SHARE_PASSWORD) == 0) {
94                 return defval;
95         }
96
97         DEBUG(0,("request for unknown share string option '%s'\n",
98                  opt_name));
99
100         return defval;
101 }
102
103 static int sclassic_int_option(struct share_config *scfg, const char *opt_name, int defval)
104 {
105         struct service *s = talloc_get_type(scfg->opaque, struct service);
106         char *parm, *val;
107         int ret;
108
109         if (strchr(opt_name, ':')) {
110                 parm = talloc_strdup(scfg, opt_name);
111                 if (!parm) {
112                         return -1;
113                 }
114                 val = strchr(parm, ':');
115                 *val = '\0';
116                 val++;
117
118                 ret = lp_parm_int(s, parm, val, defval);
119                 if (!ret) {
120                         ret = defval;
121                 }
122                 talloc_free(parm);
123                 return ret;
124         }
125
126         if (strcmp(opt_name, SHARE_CSC_POLICY) == 0) {
127                 return lp_csc_policy(s);
128         }
129
130         if (strcmp(opt_name, SHARE_MAX_CONNECTIONS) == 0) {
131                 return lp_max_connections(s);
132         }
133
134         if (strcmp(opt_name, SHARE_CREATE_MASK) == 0) {
135                 return lp_create_mask(s);
136         }
137
138         if (strcmp(opt_name, SHARE_DIR_MASK) == 0) {
139                 return lp_dir_mask(s);
140         }
141
142         if (strcmp(opt_name, SHARE_FORCE_DIR_MODE) == 0) {
143                 return lp_force_dir_mode(s);
144         }
145
146         if (strcmp(opt_name, SHARE_FORCE_CREATE_MODE) == 0) {
147                 return lp_force_create_mode(s);
148         }
149
150
151         DEBUG(0,("request for unknown share int option '%s'\n",
152                  opt_name));
153
154         return defval;
155 }
156
157 static bool sclassic_bool_option(struct share_config *scfg, const char *opt_name, 
158                           bool defval)
159 {
160         struct service *s = talloc_get_type(scfg->opaque, struct service);
161         char *parm, *val;
162         BOOL ret;
163
164         if (strchr(opt_name, ':')) {
165                 parm = talloc_strdup(scfg, opt_name);
166                 if(!parm) {
167                         return False;
168                 }
169                 val = strchr(parm, ':');
170                 *val = '\0';
171                 val++;
172
173                 ret = lp_parm_bool(s, parm, val, defval);
174                 talloc_free(parm);
175                 return ret;
176         }
177
178         if (strcmp(opt_name, SHARE_AVAILABLE) == 0) {
179                 return s != NULL;
180         }
181
182         if (strcmp(opt_name, SHARE_BROWSEABLE) == 0) {
183                 return lp_browseable(s);
184         }
185
186         if (strcmp(opt_name, SHARE_READONLY) == 0) {
187                 return lp_readonly(s);
188         }
189
190         if (strcmp(opt_name, SHARE_MAP_SYSTEM) == 0) {
191                 return lp_map_system(s);
192         }
193
194         if (strcmp(opt_name, SHARE_MAP_HIDDEN) == 0) {
195                 return lp_map_hidden(s);
196         }
197
198         if (strcmp(opt_name, SHARE_MAP_ARCHIVE) == 0) {
199                 return lp_map_archive(s);
200         }
201
202         if (strcmp(opt_name, SHARE_STRICT_LOCKING) == 0) {
203                 return lp_strict_locking(s);
204         }
205
206         if (strcmp(opt_name, SHARE_STRICT_SYNC) == 0) {
207                 return lp_strict_sync(s);
208         }
209
210         if (strcmp(opt_name, SHARE_MSDFS_ROOT) == 0) {
211                 return lp_msdfs_root(s);
212         }
213
214         if (strcmp(opt_name, SHARE_CI_FILESYSTEM) == 0) {
215                 return lp_ci_filesystem(s);
216         }
217
218         DEBUG(0,("request for unknown share bool option '%s'\n",
219                  opt_name));
220
221         return defval;
222 }
223
224 static const char **sclassic_string_list_option(TALLOC_CTX *mem_ctx, struct share_config *scfg, const char *opt_name)
225 {
226         struct service *s = talloc_get_type(scfg->opaque, struct 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 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