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