libsmbconf: Convert smbconf_drop() to sbcErr.
[samba.git] / lib / smbconf / smbconf_txt.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  libsmbconf - Samba configuration library, text backend
4  *  Copyright (C) Michael Adam 2008
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /*
21  * This is a sample implementation of a libsmbconf text backend
22  * using the params.c parser.
23  *
24  * It is read only.
25  * Don't expect brilliant performance, since it is not hashing the lists.
26  */
27
28 #include "includes.h"
29 #include "smbconf_private.h"
30 #include "lib/smbconf/smbconf_txt.h"
31
32 struct txt_cache {
33         uint32_t current_share;
34         uint32_t num_shares;
35         char **share_names;
36         uint32_t *num_params;
37         char ***param_names;
38         char ***param_values;
39 };
40
41 struct txt_private_data {
42         struct txt_cache *cache;
43         uint64_t csn;
44         bool verbatim;
45 };
46
47 /**********************************************************************
48  *
49  * helper functions
50  *
51  **********************************************************************/
52
53 /**
54  * a convenience helper to cast the private data structure
55  */
56 static struct txt_private_data *pd(struct smbconf_ctx *ctx)
57 {
58         return (struct txt_private_data *)(ctx->data);
59 }
60
61 static bool smbconf_txt_do_section(const char *section, void *private_data)
62 {
63         WERROR werr;
64         uint32_t idx;
65         struct txt_private_data *tpd = (struct txt_private_data *)private_data;
66         struct txt_cache *cache = tpd->cache;
67
68         if (smbconf_find_in_array(section, cache->share_names,
69                                   cache->num_shares, &idx))
70         {
71                 cache->current_share = idx;
72                 return true;
73         }
74
75         werr = smbconf_add_string_to_array(cache, &(cache->share_names),
76                                            cache->num_shares, section);
77         if (!W_ERROR_IS_OK(werr)) {
78                 return false;
79         }
80         cache->current_share = cache->num_shares;
81         cache->num_shares++;
82
83         cache->param_names = talloc_realloc(cache,
84                                                   cache->param_names,
85                                                   char **,
86                                                   cache->num_shares);
87         if (cache->param_names == NULL) {
88                 return false;
89         }
90         cache->param_names[cache->current_share] = NULL;
91
92         cache->param_values = talloc_realloc(cache,
93                                                    cache->param_values,
94                                                    char **,
95                                                    cache->num_shares);
96         if (cache->param_values == NULL) {
97                 return false;
98         }
99         cache->param_values[cache->current_share] = NULL;
100
101         cache->num_params = talloc_realloc(cache,
102                                                  cache->num_params,
103                                                  uint32_t,
104                                                  cache->num_shares);
105         if (cache->num_params == NULL) {
106                 return false;
107         }
108         cache->num_params[cache->current_share] = 0;
109
110         return true;
111 }
112
113 static bool smbconf_txt_do_parameter(const char *param_name,
114                                      const char *param_value,
115                                      void *private_data)
116 {
117         WERROR werr;
118         char **param_names, **param_values;
119         uint32_t num_params;
120         uint32_t idx;
121         struct txt_private_data *tpd = (struct txt_private_data *)private_data;
122         struct txt_cache *cache = tpd->cache;
123
124         if (cache->num_shares == 0) {
125                 /*
126                  * not in any share yet,
127                  * initialize the "empty" section (NULL):
128                  * parameters without a previous [section] are stored here.
129                  */
130                 if (!smbconf_txt_do_section(NULL, private_data)) {
131                         return false;
132                 }
133         }
134
135         param_names  = cache->param_names[cache->current_share];
136         param_values = cache->param_values[cache->current_share];
137         num_params   = cache->num_params[cache->current_share];
138
139         if (!(tpd->verbatim) &&
140             smbconf_find_in_array(param_name, param_names, num_params, &idx))
141         {
142                 talloc_free(param_values[idx]);
143                 param_values[idx] = talloc_strdup(cache, param_value);
144                 if (param_values[idx] == NULL) {
145                         return false;
146                 }
147                 return true;
148         }
149         werr = smbconf_add_string_to_array(cache,
150                                 &(cache->param_names[cache->current_share]),
151                                 num_params, param_name);
152         if (!W_ERROR_IS_OK(werr)) {
153                 return false;
154         }
155         werr = smbconf_add_string_to_array(cache,
156                                 &(cache->param_values[cache->current_share]),
157                                 num_params, param_value);
158         cache->num_params[cache->current_share]++;
159         return W_ERROR_IS_OK(werr);
160 }
161
162 static void smbconf_txt_flush_cache(struct smbconf_ctx *ctx)
163 {
164         talloc_free(pd(ctx)->cache);
165         pd(ctx)->cache = NULL;
166 }
167
168 static sbcErr smbconf_txt_init_cache(struct smbconf_ctx *ctx)
169 {
170         if (pd(ctx)->cache != NULL) {
171                 smbconf_txt_flush_cache(ctx);
172         }
173
174         pd(ctx)->cache = talloc_zero(pd(ctx), struct txt_cache);
175
176         if (pd(ctx)->cache == NULL) {
177                 return SBC_ERR_NOMEM;
178         }
179
180         return SBC_ERR_OK;
181 }
182
183 static sbcErr smbconf_txt_load_file(struct smbconf_ctx *ctx)
184 {
185         sbcErr err;
186         uint64_t new_csn;
187
188         if (!file_exist(ctx->path)) {
189                 return SBC_ERR_BADFILE;
190         }
191
192         new_csn = (uint64_t)file_modtime(ctx->path);
193         if (new_csn == pd(ctx)->csn) {
194                 return SBC_ERR_OK;
195         }
196
197         err = smbconf_txt_init_cache(ctx);
198         if (!SBC_ERROR_IS_OK(err)) {
199                 return err;
200         }
201
202         if (!pm_process(ctx->path, smbconf_txt_do_section,
203                         smbconf_txt_do_parameter, pd(ctx)))
204         {
205                 return SBC_ERR_CAN_NOT_COMPLETE;
206         }
207
208         pd(ctx)->csn = new_csn;
209
210         return SBC_ERR_OK;
211 }
212
213
214 /**********************************************************************
215  *
216  * smbconf operations: text backend implementations
217  *
218  **********************************************************************/
219
220 /**
221  * initialize the text based smbconf backend
222  */
223 static sbcErr smbconf_txt_init(struct smbconf_ctx *ctx, const char *path)
224 {
225         if (path == NULL) {
226                 return SBC_ERR_BADFILE;
227         }
228         ctx->path = talloc_strdup(ctx, path);
229         if (ctx->path == NULL) {
230                 return SBC_ERR_NOMEM;
231         }
232
233         ctx->data = talloc_zero(ctx, struct txt_private_data);
234         if (ctx->data == NULL) {
235                 return SBC_ERR_NOMEM;
236         }
237
238         pd(ctx)->verbatim = true;
239
240         return SBC_ERR_OK;
241 }
242
243 static int smbconf_txt_shutdown(struct smbconf_ctx *ctx)
244 {
245         return ctx->ops->close_conf(ctx);
246 }
247
248 static bool smbconf_txt_requires_messaging(struct smbconf_ctx *ctx)
249 {
250         return false;
251 }
252
253 static bool smbconf_txt_is_writeable(struct smbconf_ctx *ctx)
254 {
255         /* no write support in this backend yet... */
256         return false;
257 }
258
259 static sbcErr smbconf_txt_open(struct smbconf_ctx *ctx)
260 {
261         return smbconf_txt_load_file(ctx);
262 }
263
264 static int smbconf_txt_close(struct smbconf_ctx *ctx)
265 {
266         smbconf_txt_flush_cache(ctx);
267         return 0;
268 }
269
270 /**
271  * Get the change sequence number of the given service/parameter.
272  * service and parameter strings may be NULL.
273  */
274 static void smbconf_txt_get_csn(struct smbconf_ctx *ctx,
275                                 struct smbconf_csn *csn,
276                                 const char *service, const char *param)
277 {
278         if (csn == NULL) {
279                 return;
280         }
281
282         csn->csn = (uint64_t)file_modtime(ctx->path);
283 }
284
285 /**
286  * Drop the whole configuration (restarting empty)
287  */
288 static sbcErr smbconf_txt_drop(struct smbconf_ctx *ctx)
289 {
290         return SBC_ERR_NOT_SUPPORTED;
291 }
292
293 /**
294  * get the list of share names defined in the configuration.
295  */
296 static WERROR smbconf_txt_get_share_names(struct smbconf_ctx *ctx,
297                                           TALLOC_CTX *mem_ctx,
298                                           uint32_t *num_shares,
299                                           char ***share_names)
300 {
301         uint32_t count;
302         uint32_t added_count = 0;
303         TALLOC_CTX *tmp_ctx = NULL;
304         WERROR werr = WERR_OK;
305         sbcErr err = SBC_ERR_OK;
306         char **tmp_share_names = NULL;
307
308         if ((num_shares == NULL) || (share_names == NULL)) {
309                 werr = WERR_INVALID_PARAM;
310                 goto done;
311         }
312
313         err = smbconf_txt_load_file(ctx);
314         if (!SBC_ERROR_IS_OK(err)) {
315                 return WERR_GENERAL_FAILURE;
316         }
317
318         tmp_ctx = talloc_stackframe();
319
320         /* make sure "global" is always listed first,
321          * possibly after NULL section */
322
323         if (smbconf_share_exists(ctx, NULL)) {
324                 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
325                                                    0, NULL);
326                 if (!W_ERROR_IS_OK(werr)) {
327                         goto done;
328                 }
329                 added_count++;
330         }
331
332         if (smbconf_share_exists(ctx, GLOBAL_NAME)) {
333                 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
334                                                    added_count, GLOBAL_NAME);
335                 if (!W_ERROR_IS_OK(werr)) {
336                         goto done;
337                 }
338                 added_count++;
339         }
340
341         for (count = 0; count < pd(ctx)->cache->num_shares; count++) {
342                 if (strequal(pd(ctx)->cache->share_names[count], GLOBAL_NAME) ||
343                     (pd(ctx)->cache->share_names[count] == NULL))
344                 {
345                         continue;
346                 }
347
348                 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
349                                         added_count,
350                                         pd(ctx)->cache->share_names[count]);
351                 if (!W_ERROR_IS_OK(werr)) {
352                         goto done;
353                 }
354                 added_count++;
355         }
356
357         *num_shares = added_count;
358         if (added_count > 0) {
359                 *share_names = talloc_move(mem_ctx, &tmp_share_names);
360         } else {
361                 *share_names = NULL;
362         }
363
364 done:
365         talloc_free(tmp_ctx);
366         return werr;
367 }
368
369 /**
370  * check if a share/service of a given name exists
371  */
372 static bool smbconf_txt_share_exists(struct smbconf_ctx *ctx,
373                                      const char *servicename)
374 {
375         sbcErr err;
376
377         err = smbconf_txt_load_file(ctx);
378         if (!SBC_ERROR_IS_OK(err)) {
379                 return false;
380         }
381
382         return smbconf_find_in_array(servicename,
383                                      pd(ctx)->cache->share_names,
384                                      pd(ctx)->cache->num_shares, NULL);
385 }
386
387 /**
388  * Add a service if it does not already exist
389  */
390 static WERROR smbconf_txt_create_share(struct smbconf_ctx *ctx,
391                                        const char *servicename)
392 {
393         return WERR_NOT_SUPPORTED;
394 }
395
396 /**
397  * get a definition of a share (service) from configuration.
398  */
399 static WERROR smbconf_txt_get_share(struct smbconf_ctx *ctx,
400                                     TALLOC_CTX *mem_ctx,
401                                     const char *servicename,
402                                     struct smbconf_service **service)
403 {
404         WERROR werr;
405         sbcErr err;
406         uint32_t sidx, count;
407         bool found;
408         TALLOC_CTX *tmp_ctx = NULL;
409         struct smbconf_service *tmp_service = NULL;
410
411         err = smbconf_txt_load_file(ctx);
412         if (!SBC_ERROR_IS_OK(err)) {
413                 return WERR_GENERAL_FAILURE;
414         }
415
416         found = smbconf_find_in_array(servicename,
417                                       pd(ctx)->cache->share_names,
418                                       pd(ctx)->cache->num_shares,
419                                       &sidx);
420         if (!found) {
421                 return WERR_NO_SUCH_SERVICE;
422         }
423
424         tmp_ctx = talloc_stackframe();
425
426         tmp_service = talloc_zero(tmp_ctx, struct smbconf_service);
427         if (tmp_service == NULL) {
428                 werr = WERR_NOMEM;
429                 goto done;
430         }
431
432         if (servicename != NULL) {
433                 tmp_service->name = talloc_strdup(tmp_service, servicename);
434                 if (tmp_service->name == NULL) {
435                         werr = WERR_NOMEM;
436                         goto done;
437                 }
438         }
439
440         for (count = 0; count < pd(ctx)->cache->num_params[sidx]; count++) {
441                 werr = smbconf_add_string_to_array(tmp_service,
442                                 &(tmp_service->param_names),
443                                 count,
444                                 pd(ctx)->cache->param_names[sidx][count]);
445                 if (!W_ERROR_IS_OK(werr)) {
446                         goto done;
447                 }
448                 werr = smbconf_add_string_to_array(tmp_service,
449                                 &(tmp_service->param_values),
450                                 count,
451                                 pd(ctx)->cache->param_values[sidx][count]);
452                 if (!W_ERROR_IS_OK(werr)) {
453                         goto done;
454                 }
455         }
456
457         tmp_service->num_params = count;
458         *service = talloc_move(mem_ctx, &tmp_service);
459
460 done:
461         talloc_free(tmp_ctx);
462         return werr;
463 }
464
465 /**
466  * delete a service from configuration
467  */
468 static WERROR smbconf_txt_delete_share(struct smbconf_ctx *ctx,
469                                        const char *servicename)
470 {
471         return WERR_NOT_SUPPORTED;
472 }
473
474 /**
475  * set a configuration parameter to the value provided.
476  */
477 static WERROR smbconf_txt_set_parameter(struct smbconf_ctx *ctx,
478                                         const char *service,
479                                         const char *param,
480                                         const char *valstr)
481 {
482         return WERR_NOT_SUPPORTED;
483 }
484
485 /**
486  * get the value of a configuration parameter as a string
487  */
488 static WERROR smbconf_txt_get_parameter(struct smbconf_ctx *ctx,
489                                         TALLOC_CTX *mem_ctx,
490                                         const char *service,
491                                         const char *param,
492                                         char **valstr)
493 {
494         sbcErr err;
495         bool found;
496         uint32_t share_index, param_index;
497
498         err = smbconf_txt_load_file(ctx);
499         if (!SBC_ERROR_IS_OK(err)) {
500                 return WERR_GENERAL_FAILURE;
501         }
502
503         found = smbconf_find_in_array(service,
504                                       pd(ctx)->cache->share_names,
505                                       pd(ctx)->cache->num_shares,
506                                       &share_index);
507         if (!found) {
508                 return WERR_NO_SUCH_SERVICE;
509         }
510
511         found = smbconf_reverse_find_in_array(param,
512                                 pd(ctx)->cache->param_names[share_index],
513                                 pd(ctx)->cache->num_params[share_index],
514                                 &param_index);
515         if (!found) {
516                 return WERR_INVALID_PARAM;
517         }
518
519         *valstr = talloc_strdup(mem_ctx,
520                         pd(ctx)->cache->param_values[share_index][param_index]);
521
522         if (*valstr == NULL) {
523                 return WERR_NOMEM;
524         }
525
526         return WERR_OK;
527 }
528
529 /**
530  * delete a parameter from configuration
531  */
532 static WERROR smbconf_txt_delete_parameter(struct smbconf_ctx *ctx,
533                                            const char *service,
534                                            const char *param)
535 {
536         return WERR_NOT_SUPPORTED;
537 }
538
539 static WERROR smbconf_txt_get_includes(struct smbconf_ctx *ctx,
540                                        TALLOC_CTX *mem_ctx,
541                                        const char *service,
542                                        uint32_t *num_includes,
543                                        char ***includes)
544 {
545         WERROR werr;
546         sbcErr err;
547         bool found;
548         uint32_t sidx, count;
549         TALLOC_CTX *tmp_ctx = NULL;
550         uint32_t tmp_num_includes = 0;
551         char **tmp_includes = NULL;
552
553         err = smbconf_txt_load_file(ctx);
554         if (!SBC_ERROR_IS_OK(err)) {
555                 return WERR_GENERAL_FAILURE;
556         }
557
558         found = smbconf_find_in_array(service,
559                                       pd(ctx)->cache->share_names,
560                                       pd(ctx)->cache->num_shares,
561                                       &sidx);
562         if (!found) {
563                 return WERR_NO_SUCH_SERVICE;
564         }
565
566         tmp_ctx = talloc_stackframe();
567
568         for (count = 0; count < pd(ctx)->cache->num_params[sidx]; count++) {
569                 if (strequal(pd(ctx)->cache->param_names[sidx][count],
570                              "include"))
571                 {
572                         werr = smbconf_add_string_to_array(tmp_ctx,
573                                 &tmp_includes,
574                                 tmp_num_includes,
575                                 pd(ctx)->cache->param_values[sidx][count]);
576                         if (!W_ERROR_IS_OK(werr)) {
577                                 goto done;
578                         }
579                         tmp_num_includes++;
580                 }
581         }
582
583         *num_includes = tmp_num_includes;
584         if (*num_includes > 0) {
585                 *includes = talloc_move(mem_ctx, &tmp_includes);
586                 if (*includes == NULL) {
587                         werr = WERR_NOMEM;
588                         goto done;
589                 }
590         } else {
591                 *includes = NULL;
592         }
593
594         werr = WERR_OK;
595
596 done:
597         talloc_free(tmp_ctx);
598         return werr;
599 }
600
601 static WERROR smbconf_txt_set_includes(struct smbconf_ctx *ctx,
602                                        const char *service,
603                                        uint32_t num_includes,
604                                        const char **includes)
605 {
606         return WERR_NOT_SUPPORTED;
607 }
608
609 static WERROR smbconf_txt_delete_includes(struct smbconf_ctx *ctx,
610                                           const char *service)
611 {
612         return WERR_NOT_SUPPORTED;
613 }
614
615 static WERROR smbconf_txt_transaction_start(struct smbconf_ctx *ctx)
616 {
617         return WERR_OK;
618 }
619
620 static WERROR smbconf_txt_transaction_commit(struct smbconf_ctx *ctx)
621 {
622         return WERR_OK;
623 }
624
625 static WERROR smbconf_txt_transaction_cancel(struct smbconf_ctx *ctx)
626 {
627         return WERR_OK;
628 }
629
630 static struct smbconf_ops smbconf_ops_txt = {
631         .init                   = smbconf_txt_init,
632         .shutdown               = smbconf_txt_shutdown,
633         .requires_messaging     = smbconf_txt_requires_messaging,
634         .is_writeable           = smbconf_txt_is_writeable,
635         .open_conf              = smbconf_txt_open,
636         .close_conf             = smbconf_txt_close,
637         .get_csn                = smbconf_txt_get_csn,
638         .drop                   = smbconf_txt_drop,
639         .get_share_names        = smbconf_txt_get_share_names,
640         .share_exists           = smbconf_txt_share_exists,
641         .create_share           = smbconf_txt_create_share,
642         .get_share              = smbconf_txt_get_share,
643         .delete_share           = smbconf_txt_delete_share,
644         .set_parameter          = smbconf_txt_set_parameter,
645         .get_parameter          = smbconf_txt_get_parameter,
646         .delete_parameter       = smbconf_txt_delete_parameter,
647         .get_includes           = smbconf_txt_get_includes,
648         .set_includes           = smbconf_txt_set_includes,
649         .delete_includes        = smbconf_txt_delete_includes,
650         .transaction_start      = smbconf_txt_transaction_start,
651         .transaction_commit     = smbconf_txt_transaction_commit,
652         .transaction_cancel     = smbconf_txt_transaction_cancel,
653 };
654
655
656 /**
657  * initialize the smbconf text backend
658  * the only function that is exported from this module
659  */
660 sbcErr smbconf_init_txt(TALLOC_CTX *mem_ctx,
661                         struct smbconf_ctx **conf_ctx,
662                         const char *path)
663 {
664         sbcErr err;
665
666         err = smbconf_init_internal(mem_ctx, conf_ctx, path, &smbconf_ops_txt);
667         if (!SBC_ERROR_IS_OK(err)) {
668                 return err;
669         }
670
671         return smbconf_txt_load_file(*conf_ctx);
672 }