libsmbconf: rewrite API to use smbconf_service struct
[Samba.git] / source / lib / smbconf / smbconf_txt.c
blobc4c636d0bcf1def3309ea088c4c039a2e73ef82b
1 /*
2 * Unix SMB/CIFS implementation.
3 * libsmbconf - Samba configuration library, text backend
4 * Copyright (C) Michael Adam 2008
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.
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.
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/>.
21 * This is a sample implementation of a libsmbconf text backend
22 * using the params.c parser.
24 * It is read only.
25 * Don't expect brilliant performance, since it is not hashing the lists.
28 #include "includes.h"
29 #include "smbconf_private.h"
31 struct txt_cache {
32 uint32_t current_share;
33 uint32_t num_shares;
34 char **share_names;
35 uint32_t *num_params;
36 char ***param_names;
37 char ***param_values;
40 struct txt_private_data {
41 struct txt_cache *cache;
42 uint64_t csn;
43 bool verbatim;
46 /**********************************************************************
48 * helper functions
50 **********************************************************************/
52 /**
53 * a convenience helper to cast the private data structure
55 static struct txt_private_data *pd(struct smbconf_ctx *ctx)
57 return (struct txt_private_data *)(ctx->data);
60 static bool smbconf_txt_do_section(const char *section, void *private_data)
62 WERROR werr;
63 uint32_t idx;
64 struct txt_private_data *tpd = (struct txt_private_data *)private_data;
65 struct txt_cache *cache = tpd->cache;
67 if (smbconf_find_in_array(section, cache->share_names,
68 cache->num_shares, &idx))
70 cache->current_share = idx;
71 return true;
74 werr = smbconf_add_string_to_array(cache, &(cache->share_names),
75 cache->num_shares, section);
76 if (!W_ERROR_IS_OK(werr)) {
77 return false;
79 cache->current_share = cache->num_shares;
80 cache->num_shares++;
82 cache->param_names = TALLOC_REALLOC_ARRAY(cache,
83 cache->param_names,
84 char **,
85 cache->num_shares);
86 if (cache->param_names == NULL) {
87 return false;
89 cache->param_names[cache->current_share] = NULL;
91 cache->param_values = TALLOC_REALLOC_ARRAY(cache,
92 cache->param_values,
93 char **,
94 cache->num_shares);
95 if (cache->param_values == NULL) {
96 return false;
98 cache->param_values[cache->current_share] = NULL;
100 cache->num_params = TALLOC_REALLOC_ARRAY(cache,
101 cache->num_params,
102 uint32_t,
103 cache->num_shares);
104 if (cache->num_params == NULL) {
105 return false;
107 cache->num_params[cache->current_share] = 0;
109 return true;
112 static bool smbconf_txt_do_parameter(const char *param_name,
113 const char *param_value,
114 void *private_data)
116 WERROR werr;
117 char **param_names, **param_values;
118 uint32_t num_params;
119 uint32_t idx;
120 struct txt_private_data *tpd = (struct txt_private_data *)private_data;
121 struct txt_cache *cache = tpd->cache;
123 if (cache->num_shares == 0) {
125 * not in any share yet,
126 * initialize the "empty" section (NULL):
127 * parameters without a previous [section] are stored here.
129 if (!smbconf_txt_do_section(NULL, private_data)) {
130 return false;
134 param_names = cache->param_names[cache->current_share];
135 param_values = cache->param_values[cache->current_share];
136 num_params = cache->num_params[cache->current_share];
138 if (!(tpd->verbatim) &&
139 smbconf_find_in_array(param_name, param_names, num_params, &idx))
141 TALLOC_FREE(param_values[idx]);
142 param_values[idx] = talloc_strdup(cache, param_value);
143 if (param_values[idx] == NULL) {
144 return false;
146 return true;
148 werr = smbconf_add_string_to_array(cache,
149 &(cache->param_names[cache->current_share]),
150 num_params, param_name);
151 if (!W_ERROR_IS_OK(werr)) {
152 return false;
154 werr = smbconf_add_string_to_array(cache,
155 &(cache->param_values[cache->current_share]),
156 num_params, param_value);
157 cache->num_params[cache->current_share]++;
158 return W_ERROR_IS_OK(werr);
161 static void smbconf_txt_flush_cache(struct smbconf_ctx *ctx)
163 TALLOC_FREE(pd(ctx)->cache);
166 static WERROR smbconf_txt_init_cache(struct smbconf_ctx *ctx)
168 if (pd(ctx)->cache != NULL) {
169 smbconf_txt_flush_cache(ctx);
172 pd(ctx)->cache = TALLOC_ZERO_P(pd(ctx), struct txt_cache);
174 if (pd(ctx)->cache == NULL) {
175 return WERR_NOMEM;
178 return WERR_OK;
181 static WERROR smbconf_txt_load_file(struct smbconf_ctx *ctx)
183 WERROR werr;
184 uint64_t new_csn;
186 if (!file_exist(ctx->path, NULL)) {
187 return WERR_BADFILE;
190 new_csn = (uint64_t)file_modtime(ctx->path);
191 if (new_csn == pd(ctx)->csn) {
192 return WERR_OK;
195 werr = smbconf_txt_init_cache(ctx);
196 if (!W_ERROR_IS_OK(werr)) {
197 return werr;
200 if (!pm_process(ctx->path, smbconf_txt_do_section,
201 smbconf_txt_do_parameter, pd(ctx)))
203 return WERR_CAN_NOT_COMPLETE;
206 pd(ctx)->csn = new_csn;
208 return WERR_OK;
212 /**********************************************************************
214 * smbconf operations: text backend implementations
216 **********************************************************************/
219 * initialize the text based smbconf backend
221 static WERROR smbconf_txt_init(struct smbconf_ctx *ctx, const char *path)
223 if (path == NULL) {
224 path = get_dyn_CONFIGFILE();
226 ctx->path = talloc_strdup(ctx, path);
227 if (ctx->path == NULL) {
228 return WERR_NOMEM;
231 ctx->data = TALLOC_ZERO_P(ctx, struct txt_private_data);
232 if (ctx->data == NULL) {
233 return WERR_NOMEM;
236 pd(ctx)->verbatim = true;
238 return WERR_OK;
241 static int smbconf_txt_shutdown(struct smbconf_ctx *ctx)
243 return ctx->ops->close_conf(ctx);
246 static WERROR smbconf_txt_open(struct smbconf_ctx *ctx)
248 return smbconf_txt_load_file(ctx);
251 static int smbconf_txt_close(struct smbconf_ctx *ctx)
253 smbconf_txt_flush_cache(ctx);
254 return 0;
258 * Get the change sequence number of the given service/parameter.
259 * service and parameter strings may be NULL.
261 static void smbconf_txt_get_csn(struct smbconf_ctx *ctx,
262 struct smbconf_csn *csn,
263 const char *service, const char *param)
265 if (csn == NULL) {
266 return;
269 csn->csn = (uint64_t)file_modtime(ctx->path);
273 * Drop the whole configuration (restarting empty)
275 static WERROR smbconf_txt_drop(struct smbconf_ctx *ctx)
277 return WERR_NOT_SUPPORTED;
281 * get the list of share names defined in the configuration.
283 static WERROR smbconf_txt_get_share_names(struct smbconf_ctx *ctx,
284 TALLOC_CTX *mem_ctx,
285 uint32_t *num_shares,
286 char ***share_names)
288 uint32_t count;
289 uint32_t added_count = 0;
290 TALLOC_CTX *tmp_ctx = NULL;
291 WERROR werr = WERR_OK;
292 char **tmp_share_names = NULL;
294 if ((num_shares == NULL) || (share_names == NULL)) {
295 werr = WERR_INVALID_PARAM;
296 goto done;
299 werr = smbconf_txt_load_file(ctx);
300 if (!W_ERROR_IS_OK(werr)) {
301 return werr;
304 tmp_ctx = talloc_stackframe();
305 if (tmp_ctx == NULL) {
306 werr = WERR_NOMEM;
307 goto done;
310 /* make sure "global" is always listed first,
311 * possibly after NULL section */
313 if (smbconf_share_exists(ctx, NULL)) {
314 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
315 0, NULL);
316 if (!W_ERROR_IS_OK(werr)) {
317 goto done;
319 added_count++;
322 if (smbconf_share_exists(ctx, GLOBAL_NAME)) {
323 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
324 added_count, GLOBAL_NAME);
325 if (!W_ERROR_IS_OK(werr)) {
326 goto done;
328 added_count++;
331 for (count = 0; count < pd(ctx)->cache->num_shares; count++) {
332 if (strequal(pd(ctx)->cache->share_names[count], GLOBAL_NAME) ||
333 (pd(ctx)->cache->share_names[count] == NULL))
335 continue;
338 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
339 added_count,
340 pd(ctx)->cache->share_names[count]);
341 if (!W_ERROR_IS_OK(werr)) {
342 goto done;
344 added_count++;
347 *num_shares = added_count;
348 if (added_count > 0) {
349 *share_names = talloc_move(mem_ctx, &tmp_share_names);
350 } else {
351 *share_names = NULL;
354 done:
355 TALLOC_FREE(tmp_ctx);
356 return werr;
360 * check if a share/service of a given name exists
362 static bool smbconf_txt_share_exists(struct smbconf_ctx *ctx,
363 const char *servicename)
365 WERROR werr;
367 werr = smbconf_txt_load_file(ctx);
368 if (!W_ERROR_IS_OK(werr)) {
369 return false;
372 return smbconf_find_in_array(servicename,
373 pd(ctx)->cache->share_names,
374 pd(ctx)->cache->num_shares, NULL);
378 * Add a service if it does not already exist
380 static WERROR smbconf_txt_create_share(struct smbconf_ctx *ctx,
381 const char *servicename)
383 return WERR_NOT_SUPPORTED;
387 * get a definition of a share (service) from configuration.
389 static WERROR smbconf_txt_get_share(struct smbconf_ctx *ctx,
390 TALLOC_CTX *mem_ctx,
391 const char *servicename,
392 struct smbconf_service **service)
394 WERROR werr;
395 uint32_t sidx, count;
396 bool found;
397 TALLOC_CTX *tmp_ctx = NULL;
398 struct smbconf_service *tmp_service = NULL;
400 werr = smbconf_txt_load_file(ctx);
401 if (!W_ERROR_IS_OK(werr)) {
402 return werr;
405 found = smbconf_find_in_array(servicename,
406 pd(ctx)->cache->share_names,
407 pd(ctx)->cache->num_shares,
408 &sidx);
409 if (!found) {
410 return WERR_NO_SUCH_SERVICE;
413 tmp_ctx = talloc_stackframe();
414 if (tmp_ctx == NULL) {
415 werr = WERR_NOMEM;
416 goto done;
419 tmp_service = TALLOC_ZERO_P(tmp_ctx, struct smbconf_service);
420 if (tmp_service == NULL) {
421 werr = WERR_NOMEM;
422 goto done;
425 if (servicename != NULL) {
426 tmp_service->name = talloc_strdup(tmp_service, servicename);
427 if (tmp_service->name == NULL) {
428 werr = WERR_NOMEM;
429 goto done;
433 for (count = 0; count < pd(ctx)->cache->num_params[sidx]; count++) {
434 werr = smbconf_add_string_to_array(tmp_service,
435 &(tmp_service->param_names),
436 count,
437 pd(ctx)->cache->param_names[sidx][count]);
438 if (!W_ERROR_IS_OK(werr)) {
439 goto done;
441 werr = smbconf_add_string_to_array(tmp_service,
442 &(tmp_service->param_values),
443 count,
444 pd(ctx)->cache->param_values[sidx][count]);
445 if (!W_ERROR_IS_OK(werr)) {
446 goto done;
450 tmp_service->num_params = count;
451 if (count > 0) {
452 *service = talloc_move(mem_ctx, &tmp_service);
453 } else {
454 *service = NULL;
457 done:
458 TALLOC_FREE(tmp_ctx);
459 return werr;
463 * delete a service from configuration
465 static WERROR smbconf_txt_delete_share(struct smbconf_ctx *ctx,
466 const char *servicename)
468 return WERR_NOT_SUPPORTED;
472 * set a configuration parameter to the value provided.
474 static WERROR smbconf_txt_set_parameter(struct smbconf_ctx *ctx,
475 const char *service,
476 const char *param,
477 const char *valstr)
479 return WERR_NOT_SUPPORTED;
483 * get the value of a configuration parameter as a string
485 static WERROR smbconf_txt_get_parameter(struct smbconf_ctx *ctx,
486 TALLOC_CTX *mem_ctx,
487 const char *service,
488 const char *param,
489 char **valstr)
491 WERROR werr;
492 bool found;
493 uint32_t share_index, param_index;
495 werr = smbconf_txt_load_file(ctx);
496 if (!W_ERROR_IS_OK(werr)) {
497 return werr;
500 found = smbconf_find_in_array(service,
501 pd(ctx)->cache->share_names,
502 pd(ctx)->cache->num_shares,
503 &share_index);
504 if (!found) {
505 return WERR_NO_SUCH_SERVICE;
508 found = smbconf_reverse_find_in_array(param,
509 pd(ctx)->cache->param_names[share_index],
510 pd(ctx)->cache->num_params[share_index],
511 &param_index);
512 if (!found) {
513 return WERR_INVALID_PARAM;
516 *valstr = talloc_strdup(mem_ctx,
517 pd(ctx)->cache->param_values[share_index][param_index]);
519 if (*valstr == NULL) {
520 return WERR_NOMEM;
523 return WERR_OK;
527 * delete a parameter from configuration
529 static WERROR smbconf_txt_delete_parameter(struct smbconf_ctx *ctx,
530 const char *service,
531 const char *param)
533 return WERR_NOT_SUPPORTED;
536 static WERROR smbconf_txt_get_includes(struct smbconf_ctx *ctx,
537 TALLOC_CTX *mem_ctx,
538 const char *service,
539 uint32_t *num_includes,
540 char ***includes)
542 WERROR werr;
543 bool found;
544 uint32_t sidx, count;
545 TALLOC_CTX *tmp_ctx = NULL;
546 uint32_t tmp_num_includes = 0;
547 char **tmp_includes = NULL;
549 werr = smbconf_txt_load_file(ctx);
550 if (!W_ERROR_IS_OK(werr)) {
551 return werr;
554 found = smbconf_find_in_array(service,
555 pd(ctx)->cache->share_names,
556 pd(ctx)->cache->num_shares,
557 &sidx);
558 if (!found) {
559 return WERR_NO_SUCH_SERVICE;
562 tmp_ctx = talloc_stackframe();
564 for (count = 0; count < pd(ctx)->cache->num_params[sidx]; count++) {
565 if (strequal(pd(ctx)->cache->param_names[sidx][count],
566 "include"))
568 werr = smbconf_add_string_to_array(tmp_ctx,
569 &tmp_includes,
570 tmp_num_includes,
571 pd(ctx)->cache->param_values[sidx][count]);
572 if (!W_ERROR_IS_OK(werr)) {
573 goto done;
575 tmp_num_includes++;
579 *num_includes = tmp_num_includes;
580 if (*num_includes > 0) {
581 *includes = talloc_move(mem_ctx, &tmp_includes);
582 if (*includes == NULL) {
583 werr = WERR_NOMEM;
584 goto done;
586 } else {
587 *includes = NULL;
590 werr = WERR_OK;
592 done:
593 TALLOC_FREE(tmp_ctx);
594 return werr;
597 static WERROR smbconf_txt_set_includes(struct smbconf_ctx *ctx,
598 const char *service,
599 uint32_t num_includes,
600 const char **includes)
602 return WERR_NOT_SUPPORTED;
605 static WERROR smbconf_txt_delete_includes(struct smbconf_ctx *ctx,
606 const char *service)
608 return WERR_NOT_SUPPORTED;
612 static struct smbconf_ops smbconf_ops_txt = {
613 .init = smbconf_txt_init,
614 .shutdown = smbconf_txt_shutdown,
615 .open_conf = smbconf_txt_open,
616 .close_conf = smbconf_txt_close,
617 .get_csn = smbconf_txt_get_csn,
618 .drop = smbconf_txt_drop,
619 .get_share_names = smbconf_txt_get_share_names,
620 .share_exists = smbconf_txt_share_exists,
621 .create_share = smbconf_txt_create_share,
622 .get_share = smbconf_txt_get_share,
623 .delete_share = smbconf_txt_delete_share,
624 .set_parameter = smbconf_txt_set_parameter,
625 .get_parameter = smbconf_txt_get_parameter,
626 .delete_parameter = smbconf_txt_delete_parameter,
627 .get_includes = smbconf_txt_get_includes,
628 .set_includes = smbconf_txt_set_includes,
629 .delete_includes = smbconf_txt_delete_includes,
634 * initialize the smbconf text backend
635 * the only function that is exported from this module
637 WERROR smbconf_init_txt(TALLOC_CTX *mem_ctx,
638 struct smbconf_ctx **conf_ctx,
639 const char *path)
641 WERROR werr;
643 werr = smbconf_init_internal(mem_ctx, conf_ctx, path, &smbconf_ops_txt);
644 if (!W_ERROR_IS_OK(werr)) {
645 return werr;
648 return smbconf_txt_load_file(*conf_ctx);