[s3]libsmbconf: add backend_requires_messaging() method to libsmbconf.
[samba.git] / source3 / lib / smbconf / smbconf.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  libsmbconf - Samba configuration library
4  *  Copyright (C) Michael Adam 2007-2008
5  *  Copyright (C) Guenther Deschner 2007
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "includes.h"
22 #include "smbconf_private.h"
23
24 /**********************************************************************
25  *
26  * internal helper functions
27  *
28  **********************************************************************/
29
30 static WERROR smbconf_global_check(struct smbconf_ctx *ctx)
31 {
32         if (!smbconf_share_exists(ctx, GLOBAL_NAME)) {
33                 return smbconf_create_share(ctx, GLOBAL_NAME);
34         }
35         return WERR_OK;
36 }
37
38
39 /**********************************************************************
40  *
41  * The actual libsmbconf API functions that are exported.
42  *
43  **********************************************************************/
44
45 /**
46  * Tell whether the backend requires messaging to be set up
47  * for the backend to work correctly.
48  */
49 bool smbconf_backend_requires_messaging(struct smbconf_ctx *ctx)
50 {
51         return ctx->ops->requires_messaging(ctx);
52 }
53
54 /**
55  * Close the configuration.
56  */
57 void smbconf_shutdown(struct smbconf_ctx *ctx)
58 {
59         TALLOC_FREE(ctx);
60 }
61
62 /**
63  * Detect changes in the configuration.
64  * The given csn struct is filled with the current csn.
65  * smbconf_changed() can also be used for initial retrieval
66  * of the csn.
67  */
68 bool smbconf_changed(struct smbconf_ctx *ctx, struct smbconf_csn *csn,
69                      const char *service, const char *param)
70 {
71         struct smbconf_csn old_csn;
72
73         if (csn == NULL) {
74                 return false;
75         }
76
77         old_csn = *csn;
78
79         ctx->ops->get_csn(ctx, csn, service, param);
80         return (csn->csn != old_csn.csn);
81 }
82
83 /**
84  * Drop the whole configuration (restarting empty).
85  */
86 WERROR smbconf_drop(struct smbconf_ctx *ctx)
87 {
88         return ctx->ops->drop(ctx);
89 }
90
91 /**
92  * Get the whole configuration as lists of strings with counts:
93  *
94  *  num_shares   : number of shares
95  *  share_names  : list of length num_shares of share names
96  *  num_params   : list of length num_shares of parameter counts for each share
97  *  param_names  : list of lists of parameter names for each share
98  *  param_values : list of lists of parameter values for each share
99  */
100 WERROR smbconf_get_config(struct smbconf_ctx *ctx,
101                           TALLOC_CTX *mem_ctx,
102                           uint32_t *num_shares,
103                           struct smbconf_service ***services)
104 {
105         WERROR werr = WERR_OK;
106         TALLOC_CTX *tmp_ctx = NULL;
107         uint32_t tmp_num_shares;
108         char **tmp_share_names;
109         struct smbconf_service **tmp_services;
110         uint32_t count;
111
112         if ((num_shares == NULL) || (services == NULL)) {
113                 werr = WERR_INVALID_PARAM;
114                 goto done;
115         }
116
117         tmp_ctx = talloc_stackframe();
118
119         werr = smbconf_get_share_names(ctx, tmp_ctx, &tmp_num_shares,
120                                        &tmp_share_names);
121         if (!W_ERROR_IS_OK(werr)) {
122                 goto done;
123         }
124
125         tmp_services = TALLOC_ARRAY(tmp_ctx, struct smbconf_service *,
126                                     tmp_num_shares);
127
128         if (tmp_services == NULL) {
129                 werr = WERR_NOMEM;
130                 goto done;
131         }
132
133         for (count = 0; count < tmp_num_shares; count++) {
134                 werr = smbconf_get_share(ctx, tmp_services,
135                                          tmp_share_names[count],
136                                          &tmp_services[count]);
137                 if (!W_ERROR_IS_OK(werr)) {
138                         goto done;
139                 }
140         }
141
142         werr = WERR_OK;
143
144         *num_shares = tmp_num_shares;
145         if (tmp_num_shares > 0) {
146                 *services = talloc_move(mem_ctx, &tmp_services);
147         } else {
148                 *services = NULL;
149         }
150
151 done:
152         TALLOC_FREE(tmp_ctx);
153         return werr;
154 }
155
156 /**
157  * get the list of share names defined in the configuration.
158  */
159 WERROR smbconf_get_share_names(struct smbconf_ctx *ctx,
160                                TALLOC_CTX *mem_ctx,
161                                uint32_t *num_shares,
162                                char ***share_names)
163 {
164         return ctx->ops->get_share_names(ctx, mem_ctx, num_shares,
165                                          share_names);
166 }
167
168 /**
169  * check if a share/service of a given name exists
170  */
171 bool smbconf_share_exists(struct smbconf_ctx *ctx,
172                           const char *servicename)
173 {
174         return ctx->ops->share_exists(ctx, servicename);
175 }
176
177 /**
178  * Add a service if it does not already exist.
179  */
180 WERROR smbconf_create_share(struct smbconf_ctx *ctx,
181                             const char *servicename)
182 {
183         if ((servicename != NULL) && smbconf_share_exists(ctx, servicename)) {
184                 return WERR_FILE_EXISTS;
185         }
186
187         return ctx->ops->create_share(ctx, servicename);
188 }
189
190 /**
191  * get a definition of a share (service) from configuration.
192  */
193 WERROR smbconf_get_share(struct smbconf_ctx *ctx,
194                          TALLOC_CTX *mem_ctx,
195                          const char *servicename,
196                          struct smbconf_service **service)
197 {
198         if (!smbconf_share_exists(ctx, servicename)) {
199                 return WERR_NO_SUCH_SERVICE;
200         }
201
202         return ctx->ops->get_share(ctx, mem_ctx, servicename, service);
203 }
204
205 /**
206  * delete a service from configuration
207  */
208 WERROR smbconf_delete_share(struct smbconf_ctx *ctx, const char *servicename)
209 {
210         if (!smbconf_share_exists(ctx, servicename)) {
211                 return WERR_NO_SUCH_SERVICE;
212         }
213
214         return ctx->ops->delete_share(ctx, servicename);
215 }
216
217 /**
218  * set a configuration parameter to the value provided.
219  */
220 WERROR smbconf_set_parameter(struct smbconf_ctx *ctx,
221                              const char *service,
222                              const char *param,
223                              const char *valstr)
224 {
225         if (!smbconf_share_exists(ctx, service)) {
226                 return WERR_NO_SUCH_SERVICE;
227         }
228
229         return ctx->ops->set_parameter(ctx, service, param, valstr);
230 }
231
232 /**
233  * Set a global parameter
234  * (i.e. a parameter in the [global] service).
235  *
236  * This also creates [global] when it does not exist.
237  */
238 WERROR smbconf_set_global_parameter(struct smbconf_ctx *ctx,
239                                     const char *param, const char *val)
240 {
241         WERROR werr;
242
243         werr = smbconf_global_check(ctx);
244         if (W_ERROR_IS_OK(werr)) {
245                 werr = smbconf_set_parameter(ctx, GLOBAL_NAME, param, val);
246         }
247
248         return werr;
249 }
250
251 /**
252  * get the value of a configuration parameter as a string
253  */
254 WERROR smbconf_get_parameter(struct smbconf_ctx *ctx,
255                              TALLOC_CTX *mem_ctx,
256                              const char *service,
257                              const char *param,
258                              char **valstr)
259 {
260         if (valstr == NULL) {
261                 return WERR_INVALID_PARAM;
262         }
263
264         if (!smbconf_share_exists(ctx, service)) {
265                 return WERR_NO_SUCH_SERVICE;
266         }
267
268         return ctx->ops->get_parameter(ctx, mem_ctx, service, param, valstr);
269 }
270
271 /**
272  * Get the value of a global parameter.
273  *
274  * Create [global] if it does not exist.
275  */
276 WERROR smbconf_get_global_parameter(struct smbconf_ctx *ctx,
277                                     TALLOC_CTX *mem_ctx,
278                                     const char *param,
279                                     char **valstr)
280 {
281         WERROR werr;
282
283         werr = smbconf_global_check(ctx);
284         if (W_ERROR_IS_OK(werr)) {
285                 werr = smbconf_get_parameter(ctx, mem_ctx, GLOBAL_NAME, param,
286                                              valstr);
287         }
288
289         return werr;
290 }
291
292 /**
293  * delete a parameter from configuration
294  */
295 WERROR smbconf_delete_parameter(struct smbconf_ctx *ctx,
296                                 const char *service, const char *param)
297 {
298         if (!smbconf_share_exists(ctx, service)) {
299                 return WERR_NO_SUCH_SERVICE;
300         }
301
302         return ctx->ops->delete_parameter(ctx, service, param);
303 }
304
305 /**
306  * Delete a global parameter.
307  *
308  * Create [global] if it does not exist.
309  */
310 WERROR smbconf_delete_global_parameter(struct smbconf_ctx *ctx,
311                                        const char *param)
312 {
313         WERROR werr;
314
315         werr = smbconf_global_check(ctx);
316         if (W_ERROR_IS_OK(werr)) {
317                 werr = smbconf_delete_parameter(ctx, GLOBAL_NAME, param);
318         }
319
320         return werr;
321 }
322
323 WERROR smbconf_get_includes(struct smbconf_ctx *ctx,
324                             TALLOC_CTX *mem_ctx,
325                             const char *service,
326                             uint32_t *num_includes, char ***includes)
327 {
328         if (!smbconf_share_exists(ctx, service)) {
329                 return WERR_NO_SUCH_SERVICE;
330         }
331
332         return ctx->ops->get_includes(ctx, mem_ctx, service, num_includes,
333                                       includes);
334 }
335
336 WERROR smbconf_get_global_includes(struct smbconf_ctx *ctx,
337                                    TALLOC_CTX *mem_ctx,
338                                    uint32_t *num_includes, char ***includes)
339 {
340         WERROR werr;
341
342         werr = smbconf_global_check(ctx);
343         if (W_ERROR_IS_OK(werr)) {
344                 werr = smbconf_get_includes(ctx, mem_ctx, GLOBAL_NAME,
345                                             num_includes, includes);
346         }
347
348         return werr;
349 }
350
351 WERROR smbconf_set_includes(struct smbconf_ctx *ctx,
352                             const char *service,
353                             uint32_t num_includes, const char **includes)
354 {
355         if (!smbconf_share_exists(ctx, service)) {
356                 return WERR_NO_SUCH_SERVICE;
357         }
358
359         return ctx->ops->set_includes(ctx, service, num_includes, includes);
360 }
361
362 WERROR smbconf_set_global_includes(struct smbconf_ctx *ctx,
363                                    uint32_t num_includes,
364                                    const char **includes)
365 {
366         WERROR werr;
367
368         werr = smbconf_global_check(ctx);
369         if (W_ERROR_IS_OK(werr)) {
370                 werr = smbconf_set_includes(ctx, GLOBAL_NAME,
371                                             num_includes, includes);
372         }
373
374         return werr;
375 }
376
377
378 WERROR smbconf_delete_includes(struct smbconf_ctx *ctx, const char *service)
379 {
380         if (!smbconf_share_exists(ctx, service)) {
381                 return WERR_NO_SUCH_SERVICE;
382         }
383
384         return ctx->ops->delete_includes(ctx, service);
385 }
386
387 WERROR smbconf_delete_global_includes(struct smbconf_ctx *ctx)
388 {
389         WERROR werr;
390
391         werr = smbconf_global_check(ctx);
392         if (W_ERROR_IS_OK(werr)) {
393                 werr = smbconf_delete_includes(ctx, GLOBAL_NAME);
394         }
395
396         return werr;
397 }