r25026: Move param/param.h out of includes.h
[gd/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 sclassic_snum {
27         int snum;
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 sclassic_snum *s = talloc_get_type(scfg->opaque, struct sclassic_snum);
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->snum, 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->snum);
73         }
74
75         if (strcmp(opt_name, SHARE_COMMENT) == 0) {
76                 return lp_comment(s->snum);
77         }
78
79         if (strcmp(opt_name, SHARE_VOLUME) == 0) {
80                 return volume_label(s->snum);
81         }
82
83         if (strcmp(opt_name, SHARE_TYPE) == 0) {
84                 if (lp_print_ok(s->snum)) {
85                         return "PRINTER";
86                 }
87                 if (strcmp("NTFS", lp_fstype(s->snum)) == 0) {
88                         return "DISK";
89                 }
90                 return lp_fstype(s->snum);
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 int sclassic_int_option(struct share_config *scfg, const char *opt_name, int defval)
104 {
105         struct sclassic_snum *s = talloc_get_type(scfg->opaque, struct sclassic_snum);
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->snum, 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->snum);
128         }
129
130         if (strcmp(opt_name, SHARE_MAX_CONNECTIONS) == 0) {
131                 return lp_max_connections(s->snum);
132         }
133
134         if (strcmp(opt_name, SHARE_CREATE_MASK) == 0) {
135                 return lp_create_mask(s->snum);
136         }
137
138         if (strcmp(opt_name, SHARE_DIR_MASK) == 0) {
139                 return lp_dir_mask(s->snum);
140         }
141
142         if (strcmp(opt_name, SHARE_FORCE_DIR_MODE) == 0) {
143                 return lp_force_dir_mode(s->snum);
144         }
145
146         if (strcmp(opt_name, SHARE_FORCE_CREATE_MODE) == 0) {
147                 return lp_force_create_mode(s->snum);
148         }
149
150
151         DEBUG(0,("request for unknown share int option '%s'\n",
152                  opt_name));
153
154         return defval;
155 }
156
157 BOOL sclassic_bool_option(struct share_config *scfg, const char *opt_name, BOOL defval)
158 {
159         struct sclassic_snum *s = talloc_get_type(scfg->opaque, struct sclassic_snum);
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->snum, parm, val, defval);
173                 talloc_free(parm);
174                 return ret;
175         }
176
177         if (strcmp(opt_name, SHARE_AVAILABLE) == 0) {
178                 return lp_snum_ok(s->snum);
179         }
180
181         if (strcmp(opt_name, SHARE_BROWSEABLE) == 0) {
182                 return lp_browseable(s->snum);
183         }
184
185         if (strcmp(opt_name, SHARE_READONLY) == 0) {
186                 return lp_readonly(s->snum);
187         }
188
189         if (strcmp(opt_name, SHARE_MAP_SYSTEM) == 0) {
190                 return lp_map_system(s->snum);
191         }
192
193         if (strcmp(opt_name, SHARE_MAP_HIDDEN) == 0) {
194                 return lp_map_hidden(s->snum);
195         }
196
197         if (strcmp(opt_name, SHARE_MAP_ARCHIVE) == 0) {
198                 return lp_map_archive(s->snum);
199         }
200
201         if (strcmp(opt_name, SHARE_STRICT_LOCKING) == 0) {
202                 return lp_strict_locking(s->snum);
203         }
204
205         if (strcmp(opt_name, SHARE_STRICT_SYNC) == 0) {
206                 return lp_strict_sync(s->snum);
207         }
208
209         if (strcmp(opt_name, SHARE_MSDFS_ROOT) == 0) {
210                 return lp_msdfs_root(s->snum);
211         }
212
213         if (strcmp(opt_name, SHARE_CI_FILESYSTEM) == 0) {
214                 return lp_ci_filesystem(s->snum);
215         }
216
217         DEBUG(0,("request for unknown share bool option '%s'\n",
218                  opt_name));
219
220         return defval;
221 }
222
223 const char **sclassic_string_list_option(TALLOC_CTX *mem_ctx, struct share_config *scfg, const char *opt_name)
224 {
225         struct sclassic_snum *s = talloc_get_type(scfg->opaque, struct sclassic_snum);
226         char *parm, *val;
227         const char **ret;
228
229         if (strchr(opt_name, ':')) {
230                 parm = talloc_strdup(scfg, opt_name);
231                 if (!parm) {
232                         return NULL;
233                 }
234                 val = strchr(parm, ':');
235                 *val = '\0';
236                 val++;
237
238                 ret = lp_parm_string_list(s->snum, parm, val, ",;");
239                 talloc_free(parm);
240                 return ret;
241         }
242
243         if (strcmp(opt_name, SHARE_HOSTS_ALLOW) == 0) {
244                 return lp_hostsallow(s->snum);
245         }
246
247         if (strcmp(opt_name, SHARE_HOSTS_DENY) == 0) {
248                 return lp_hostsdeny(s->snum);
249         }
250
251         if (strcmp(opt_name, SHARE_NTVFS_HANDLER) == 0) {
252                 return lp_ntvfs_handler(s->snum);
253         }
254
255         DEBUG(0,("request for unknown share list option '%s'\n",
256                  opt_name));
257
258         return NULL;
259 }
260
261 NTSTATUS sclassic_list_all(TALLOC_CTX *mem_ctx,
262                                      struct share_context *ctx,
263                                      int *count,
264                                      const char ***names)
265 {
266         int i;
267         int num_services;
268         const char **n;
269        
270         num_services = lp_numservices();
271
272         n = talloc_array(mem_ctx, const char *, num_services);
273         if (!n) {
274                 DEBUG(0,("ERROR: Out of memory!\n"));
275                 return NT_STATUS_NO_MEMORY;
276         }
277
278         for (i = 0; i < num_services; i++) {
279                 n[i] = talloc_strdup(n, lp_servicename(i));
280                 if (!n[i]) {
281                         DEBUG(0,("ERROR: Out of memory!\n"));
282                         talloc_free(n);
283                         return NT_STATUS_NO_MEMORY;
284                 }
285         }
286
287         *names = n;
288         *count = num_services;
289
290         return NT_STATUS_OK;
291 }
292
293 NTSTATUS sclassic_get_config(TALLOC_CTX *mem_ctx,
294                              struct share_context *ctx,
295                              const char *name,
296                              struct share_config **scfg)
297 {
298         int i, snum;
299         struct share_config *s;
300         struct sclassic_snum *scnum;
301
302         snum = -1;
303         for (i = 0; i < lp_numservices(); i++) {
304                 if (strcasecmp_m(name, lp_servicename(i)) == 0) {
305                         snum = i;
306                         break;
307                 }
308         }
309
310         if (snum < 0) {
311                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
312         }
313
314         s = talloc(mem_ctx, struct share_config);
315         if (!s) {
316                 DEBUG(0,("ERROR: Out of memory!\n"));
317                 return NT_STATUS_NO_MEMORY;
318         }
319
320         s->name = talloc_strdup(s, lp_servicename(snum));
321         if (!s->name) {
322                 DEBUG(0,("ERROR: Out of memory!\n"));
323                 talloc_free(s);
324                 return NT_STATUS_NO_MEMORY;
325         }
326
327         scnum = talloc(s, struct sclassic_snum);
328         if (!scnum) {
329                 DEBUG(0,("ERROR: Out of memory!\n"));
330                 talloc_free(s);
331                 return NT_STATUS_NO_MEMORY;
332         }
333         scnum->snum = snum;
334
335         s->opaque = (void *)scnum;
336         s->ctx = ctx;
337         
338         *scfg = s;
339
340         return NT_STATUS_OK;
341 }
342
343 NTSTATUS share_classic_init(void)
344 {
345         static struct share_ops ops = {
346                 .name = "classic",
347                 .init = sclassic_init,
348                 .string_option = sclassic_string_option,
349                 .int_option = sclassic_int_option,
350                 .bool_option = sclassic_bool_option,
351                 .string_list_option = sclassic_string_list_option,
352                 .list_all = sclassic_list_all,
353                 .get_config = sclassic_get_config
354         };
355
356         return share_register(&ops);
357 }
358