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