68f7dfc9028e6c6aa05291e7b8f1d34ab8b26888
[ira/wip.git] / 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 sbcErr 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
36         return SBC_ERR_OK;
37 }
38
39
40 /**********************************************************************
41  *
42  * The actual libsmbconf API functions that are exported.
43  *
44  **********************************************************************/
45
46 const char *sbcErrorString(sbcErr error)
47 {
48         switch (error) {
49                 case SBC_ERR_OK:
50                         return "SBC_ERR_OK";
51                 case SBC_ERR_NOT_IMPLEMENTED:
52                         return "SBC_ERR_NOT_IMPLEMENTED";
53                 case SBC_ERR_NOT_SUPPORTED:
54                         return "SBC_ERR_NOT_SUPPORTED";
55                 case SBC_ERR_UNKNOWN_FAILURE:
56                         return "SBC_ERR_UNKNOWN_FAILURE";
57                 case SBC_ERR_NOMEM:
58                         return "SBC_ERR_NOMEM";
59                 case SBC_ERR_INVALID_PARAM:
60                         return "SBC_ERR_INVALID_PARAM";
61                 case SBC_ERR_BADFILE:
62                         return "SBC_ERR_BADFILE";
63                 case SBC_ERR_NO_SUCH_SERVICE:
64                         return "SBC_ERR_NO_SUCH_SERVICE";
65                 case SBC_ERR_IO_FAILURE:
66                         return "SBC_ERR_IO_FAILURE";
67                 case SBC_ERR_CAN_NOT_COMPLETE:
68                         return "SBC_ERR_CAN_NOT_COMPLETE";
69                 case SBC_ERR_NO_MORE_ITEMS:
70                         return "SBC_ERR_NO_MORE_ITEMS";
71                 case SBC_ERR_FILE_EXISTS:
72                         return "SBC_ERR_FILE_EXISTS";
73                 case SBC_ERR_ACCESS_DENIED:
74                         return "SBC_ERR_ACCESS_DENIED";
75         }
76
77         return "unknown sbcErr value";
78 }
79
80
81 /**
82  * Tell whether the backend requires messaging to be set up
83  * for the backend to work correctly.
84  */
85 bool smbconf_backend_requires_messaging(struct smbconf_ctx *ctx)
86 {
87         return ctx->ops->requires_messaging(ctx);
88 }
89
90 /**
91  * Tell whether the source is writeable.
92  */
93 bool smbconf_is_writeable(struct smbconf_ctx *ctx)
94 {
95         return ctx->ops->is_writeable(ctx);
96 }
97
98 /**
99  * Close the configuration.
100  */
101 void smbconf_shutdown(struct smbconf_ctx *ctx)
102 {
103         talloc_free(ctx);
104 }
105
106 /**
107  * Detect changes in the configuration.
108  * The given csn struct is filled with the current csn.
109  * smbconf_changed() can also be used for initial retrieval
110  * of the csn.
111  */
112 bool smbconf_changed(struct smbconf_ctx *ctx, struct smbconf_csn *csn,
113                      const char *service, const char *param)
114 {
115         struct smbconf_csn old_csn;
116
117         if (csn == NULL) {
118                 return false;
119         }
120
121         old_csn = *csn;
122
123         ctx->ops->get_csn(ctx, csn, service, param);
124         return (csn->csn != old_csn.csn);
125 }
126
127 /**
128  * Drop the whole configuration (restarting empty).
129  */
130 sbcErr smbconf_drop(struct smbconf_ctx *ctx)
131 {
132         return ctx->ops->drop(ctx);
133 }
134
135 /**
136  * Get the whole configuration as lists of strings with counts:
137  *
138  *  num_shares   : number of shares
139  *  share_names  : list of length num_shares of share names
140  *  num_params   : list of length num_shares of parameter counts for each share
141  *  param_names  : list of lists of parameter names for each share
142  *  param_values : list of lists of parameter values for each share
143  */
144 WERROR smbconf_get_config(struct smbconf_ctx *ctx,
145                           TALLOC_CTX *mem_ctx,
146                           uint32_t *num_shares,
147                           struct smbconf_service ***services)
148 {
149         WERROR werr = WERR_OK;
150         sbcErr err;
151         TALLOC_CTX *tmp_ctx = NULL;
152         uint32_t tmp_num_shares;
153         char **tmp_share_names;
154         struct smbconf_service **tmp_services;
155         uint32_t count;
156
157         if ((num_shares == NULL) || (services == NULL)) {
158                 werr = WERR_INVALID_PARAM;
159                 goto done;
160         }
161
162         tmp_ctx = talloc_stackframe();
163
164         err = smbconf_get_share_names(ctx, tmp_ctx, &tmp_num_shares,
165                                       &tmp_share_names);
166         if (!SBC_ERROR_IS_OK(err)) {
167                 werr = WERR_GENERAL_FAILURE;
168                 goto done;
169         }
170
171         tmp_services = talloc_array(tmp_ctx, struct smbconf_service *,
172                                     tmp_num_shares);
173
174         if (tmp_services == NULL) {
175                 werr = WERR_NOMEM;
176                 goto done;
177         }
178
179         for (count = 0; count < tmp_num_shares; count++) {
180                 werr = smbconf_get_share(ctx, tmp_services,
181                                          tmp_share_names[count],
182                                          &tmp_services[count]);
183                 if (!W_ERROR_IS_OK(werr)) {
184                         goto done;
185                 }
186         }
187
188         werr = WERR_OK;
189
190         *num_shares = tmp_num_shares;
191         if (tmp_num_shares > 0) {
192                 *services = talloc_move(mem_ctx, &tmp_services);
193         } else {
194                 *services = NULL;
195         }
196
197 done:
198         talloc_free(tmp_ctx);
199         return werr;
200 }
201
202 /**
203  * get the list of share names defined in the configuration.
204  */
205 sbcErr smbconf_get_share_names(struct smbconf_ctx *ctx,
206                                TALLOC_CTX *mem_ctx,
207                                uint32_t *num_shares,
208                                char ***share_names)
209 {
210         return ctx->ops->get_share_names(ctx, mem_ctx, num_shares,
211                                          share_names);
212 }
213
214 /**
215  * check if a share/service of a given name exists
216  */
217 bool smbconf_share_exists(struct smbconf_ctx *ctx,
218                           const char *servicename)
219 {
220         return ctx->ops->share_exists(ctx, servicename);
221 }
222
223 /**
224  * Add a service if it does not already exist.
225  */
226 sbcErr smbconf_create_share(struct smbconf_ctx *ctx,
227                             const char *servicename)
228 {
229         if ((servicename != NULL) && smbconf_share_exists(ctx, servicename)) {
230                 return SBC_ERR_FILE_EXISTS;
231         }
232
233         return ctx->ops->create_share(ctx, servicename);
234 }
235
236 /**
237  * get a definition of a share (service) from configuration.
238  */
239 WERROR smbconf_get_share(struct smbconf_ctx *ctx,
240                          TALLOC_CTX *mem_ctx,
241                          const char *servicename,
242                          struct smbconf_service **service)
243 {
244         return ctx->ops->get_share(ctx, mem_ctx, servicename, service);
245 }
246
247 /**
248  * delete a service from configuration
249  */
250 WERROR smbconf_delete_share(struct smbconf_ctx *ctx, const char *servicename)
251 {
252         if (!smbconf_share_exists(ctx, servicename)) {
253                 return WERR_NO_SUCH_SERVICE;
254         }
255
256         return ctx->ops->delete_share(ctx, servicename);
257 }
258
259 /**
260  * set a configuration parameter to the value provided.
261  */
262 WERROR smbconf_set_parameter(struct smbconf_ctx *ctx,
263                              const char *service,
264                              const char *param,
265                              const char *valstr)
266 {
267         return ctx->ops->set_parameter(ctx, service, param, valstr);
268 }
269
270 /**
271  * Set a global parameter
272  * (i.e. a parameter in the [global] service).
273  *
274  * This also creates [global] when it does not exist.
275  */
276 WERROR smbconf_set_global_parameter(struct smbconf_ctx *ctx,
277                                     const char *param, const char *val)
278 {
279         WERROR werr = WERR_OK;
280         sbcErr err;
281
282         err = smbconf_global_check(ctx);
283         if (!SBC_ERROR_IS_OK(err)) {
284                 return WERR_GENERAL_FAILURE;
285         }
286         werr = smbconf_set_parameter(ctx, GLOBAL_NAME, param, val);
287
288         return werr;
289 }
290
291 /**
292  * get the value of a configuration parameter as a string
293  */
294 WERROR smbconf_get_parameter(struct smbconf_ctx *ctx,
295                              TALLOC_CTX *mem_ctx,
296                              const char *service,
297                              const char *param,
298                              char **valstr)
299 {
300         if (valstr == NULL) {
301                 return WERR_INVALID_PARAM;
302         }
303
304         return ctx->ops->get_parameter(ctx, mem_ctx, service, param, valstr);
305 }
306
307 /**
308  * Get the value of a global parameter.
309  *
310  * Create [global] if it does not exist.
311  */
312 WERROR smbconf_get_global_parameter(struct smbconf_ctx *ctx,
313                                     TALLOC_CTX *mem_ctx,
314                                     const char *param,
315                                     char **valstr)
316 {
317         WERROR werr;
318         sbcErr err;
319
320         err = smbconf_global_check(ctx);
321         if (!SBC_ERROR_IS_OK(err)) {
322                 return WERR_GENERAL_FAILURE;
323         }
324
325         werr = smbconf_get_parameter(ctx, mem_ctx, GLOBAL_NAME, param,
326                                      valstr);
327
328         return werr;
329 }
330
331 /**
332  * delete a parameter from configuration
333  */
334 WERROR smbconf_delete_parameter(struct smbconf_ctx *ctx,
335                                 const char *service, const char *param)
336 {
337         return ctx->ops->delete_parameter(ctx, service, param);
338 }
339
340 /**
341  * Delete a global parameter.
342  *
343  * Create [global] if it does not exist.
344  */
345 WERROR smbconf_delete_global_parameter(struct smbconf_ctx *ctx,
346                                        const char *param)
347 {
348         WERROR werr;
349         sbcErr err;
350
351         err = smbconf_global_check(ctx);
352         if (!SBC_ERROR_IS_OK(err)) {
353                 return WERR_GENERAL_FAILURE;
354         }
355         werr = smbconf_delete_parameter(ctx, GLOBAL_NAME, param);
356
357         return werr;
358 }
359
360 WERROR smbconf_get_includes(struct smbconf_ctx *ctx,
361                             TALLOC_CTX *mem_ctx,
362                             const char *service,
363                             uint32_t *num_includes, char ***includes)
364 {
365         return ctx->ops->get_includes(ctx, mem_ctx, service, num_includes,
366                                       includes);
367 }
368
369 WERROR smbconf_get_global_includes(struct smbconf_ctx *ctx,
370                                    TALLOC_CTX *mem_ctx,
371                                    uint32_t *num_includes, char ***includes)
372 {
373         WERROR werr;
374         sbcErr err;
375
376         err = smbconf_global_check(ctx);
377         if (SBC_ERROR_IS_OK(err)) {
378                 return WERR_GENERAL_FAILURE;
379         }
380         werr = smbconf_get_includes(ctx, mem_ctx, GLOBAL_NAME,
381                                     num_includes, includes);
382
383         return werr;
384 }
385
386 WERROR smbconf_set_includes(struct smbconf_ctx *ctx,
387                             const char *service,
388                             uint32_t num_includes, const char **includes)
389 {
390         return ctx->ops->set_includes(ctx, service, num_includes, includes);
391 }
392
393 WERROR smbconf_set_global_includes(struct smbconf_ctx *ctx,
394                                    uint32_t num_includes,
395                                    const char **includes)
396 {
397         WERROR werr;
398         sbcErr err;
399
400         err = smbconf_global_check(ctx);
401         if (!SBC_ERROR_IS_OK(err)) {
402                 return WERR_GENERAL_FAILURE;
403         }
404         werr = smbconf_set_includes(ctx, GLOBAL_NAME,
405                                     num_includes, includes);
406
407         return werr;
408 }
409
410
411 WERROR smbconf_delete_includes(struct smbconf_ctx *ctx, const char *service)
412 {
413         return ctx->ops->delete_includes(ctx, service);
414 }
415
416 WERROR smbconf_delete_global_includes(struct smbconf_ctx *ctx)
417 {
418         WERROR werr;
419         sbcErr err;
420
421         err = smbconf_global_check(ctx);
422         if (!SBC_ERROR_IS_OK(err)) {
423                 return WERR_GENERAL_FAILURE;
424         }
425         werr = smbconf_delete_includes(ctx, GLOBAL_NAME);
426
427         return werr;
428 }
429
430 WERROR smbconf_transaction_start(struct smbconf_ctx *ctx)
431 {
432         return ctx->ops->transaction_start(ctx);
433 }
434
435 WERROR smbconf_transaction_commit(struct smbconf_ctx *ctx)
436 {
437         return ctx->ops->transaction_commit(ctx);
438 }
439
440 WERROR smbconf_transaction_cancel(struct smbconf_ctx *ctx)
441 {
442         return ctx->ops->transaction_cancel(ctx);
443 }