r17206: Add a modular API for share configuration.
[samba.git] / source4 / param / share.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    Modular services configuration system
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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "param/share.h"
25 #include "build.h"
26
27 const char *share_string_option(struct share_config *scfg, const char *opt_name, const char *defval)
28 {
29         return scfg->ctx->ops->string_option(scfg, opt_name, defval);
30 }
31
32 int share_int_option(struct share_config *scfg, const char *opt_name, int defval)
33 {
34         return scfg->ctx->ops->int_option(scfg, opt_name, defval);
35 }
36
37 BOOL share_bool_option(struct share_config *scfg, const char *opt_name, BOOL defval)
38 {
39         return scfg->ctx->ops->bool_option(scfg, opt_name, defval);
40 }
41
42 const char **share_string_list_option(TALLOC_CTX *mem_ctx, struct share_config *scfg, const char *opt_name)
43 {
44         return scfg->ctx->ops->string_list_option(mem_ctx, scfg, opt_name);
45 }
46
47 NTSTATUS share_list_all(TALLOC_CTX *mem_ctx, struct share_context *sctx, int *count, const char ***names)
48 {
49         return sctx->ops->list_all(mem_ctx, sctx, count, names);
50 }
51
52 NTSTATUS share_get_config(TALLOC_CTX *mem_ctx, struct share_context *sctx, const char *name, struct share_config **scfg)
53 {
54         return sctx->ops->get_config(mem_ctx, sctx, name, scfg);
55 }
56
57 /* List of currently available share backends */
58 static struct share_ops **backends = NULL;
59
60 static const struct share_ops *share_backend_by_name(const char *name)
61 {
62         int i;
63
64         for (i = 0; backends && backends[i]; i++) {
65                 if (strcmp(backends[i]->name, name) == 0) {
66                         return backends[i];
67                 }
68         }
69
70         return NULL;
71 }
72
73 /*
74   Register the share backend
75 */
76 NTSTATUS share_register(const struct share_ops *ops)
77 {
78         int i;
79
80         if (share_backend_by_name(ops->name) != NULL) {
81                 DEBUG(0,("SHARE backend [%s] already registered\n", ops->name));
82                 return NT_STATUS_OBJECT_NAME_COLLISION;
83         }
84
85         i = 0;
86         while (backends && backends[i]) {
87                 i++;
88         }
89
90         backends = realloc_p(backends, struct share_ops *, i + 2);
91         if (!backends) {
92                 smb_panic("out of memory in share_register");
93         }
94
95         backends[i] = malloc(sizeof(struct share_ops));
96         if (!backends[i]) {
97                 smb_panic("out of memory in share_register");
98         }
99
100         backends[i] = smb_xmemdup(ops, sizeof(*ops));
101         backends[i]->name = smb_xstrdup(ops->name);
102
103         backends[i + 1] = NULL;
104
105         DEBUG(3, ("SHARE backend [%s] registered.\n", ops->name));
106
107         return NT_STATUS_OK;
108 }
109
110 NTSTATUS share_get_context(TALLOC_CTX *mem_ctx, struct share_context **ctx)
111 {
112         const struct share_ops *ops;
113
114         ops = share_backend_by_name(lp_share_backend());
115         if (!ops) {
116                 DEBUG(0, ("share_init_connection: share backend [%s] not found!\n", lp_share_backend()));
117                 return NT_STATUS_INTERNAL_ERROR;
118         }
119
120         return ops->init(mem_ctx, ops, ctx);
121 }
122
123 /*
124   initialise the SHARE subsystem
125 */
126 NTSTATUS share_init(void)
127 {
128         init_module_fn static_init[] = STATIC_share_MODULES;
129         init_module_fn *shared_init = load_samba_modules(NULL, "share");
130
131         run_init_functions(static_init);
132         run_init_functions(shared_init);
133
134         talloc_free(shared_init);
135
136         return NT_STATUS_OK;
137 }