libsmbconf: fix crashbug - correctly check for existence of file.
[Samba/ekacnet.git] / source3 / lib / smbconf / smbconf_txt_simple.c
blob9164d1690a9ffe1c050851957726254179085096
1 /*
2 * Unix SMB/CIFS implementation.
3 * libsmbconf - Samba configuration library, simple 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) {
124 /* not in any share ... */
125 return false;
128 param_names = cache->param_names[cache->current_share];
129 param_values = cache->param_values[cache->current_share];
130 num_params = cache->num_params[cache->current_share];
132 if (!(tpd->verbatim) &&
133 smbconf_find_in_array(param_name, param_names, num_params, &idx))
135 TALLOC_FREE(param_values[idx]);
136 param_values[idx] = talloc_strdup(cache, param_value);
137 if (param_values[idx] == NULL) {
138 return false;
140 return true;
142 werr = smbconf_add_string_to_array(cache,
143 &(cache->param_names[cache->current_share]),
144 num_params, param_name);
145 if (!W_ERROR_IS_OK(werr)) {
146 return false;
148 werr = smbconf_add_string_to_array(cache,
149 &(cache->param_values[cache->current_share]),
150 num_params, param_value);
151 cache->num_params[cache->current_share]++;
152 return W_ERROR_IS_OK(werr);
155 static void smbconf_txt_flush_cache(struct smbconf_ctx *ctx)
157 TALLOC_FREE(pd(ctx)->cache);
160 static WERROR smbconf_txt_init_cache(struct smbconf_ctx *ctx)
162 if (pd(ctx)->cache != NULL) {
163 smbconf_txt_flush_cache(ctx);
166 pd(ctx)->cache = TALLOC_ZERO_P(pd(ctx), struct txt_cache);
168 if (pd(ctx)->cache == NULL) {
169 return WERR_NOMEM;
172 return WERR_OK;
175 static WERROR smbconf_txt_load_file(struct smbconf_ctx *ctx)
177 WERROR werr;
178 uint64_t new_csn;
180 if (!file_exist(ctx->path, NULL)) {
181 return WERR_BADFILE;
184 new_csn = (uint64_t)file_modtime(ctx->path);
185 if (new_csn == pd(ctx)->csn) {
186 return WERR_OK;
189 werr = smbconf_txt_init_cache(ctx);
190 if (!W_ERROR_IS_OK(werr)) {
191 return werr;
194 if (!pm_process(ctx->path, smbconf_txt_do_section,
195 smbconf_txt_do_parameter, pd(ctx)))
197 return WERR_CAN_NOT_COMPLETE;
200 pd(ctx)->csn = new_csn;
202 return WERR_OK;
206 /**********************************************************************
208 * smbconf operations: simple text implementations
210 **********************************************************************/
213 * initialize the text based smbconf backend
215 static WERROR smbconf_txt_init(struct smbconf_ctx *ctx, const char *path)
217 if (path == NULL) {
218 path = get_dyn_CONFIGFILE();
220 ctx->path = talloc_strdup(ctx, path);
221 if (ctx->path == NULL) {
222 return WERR_NOMEM;
225 ctx->data = TALLOC_ZERO_P(ctx, struct txt_private_data);
227 return WERR_OK;
230 static int smbconf_txt_shutdown(struct smbconf_ctx *ctx)
232 return ctx->ops->close_conf(ctx);
235 static WERROR smbconf_txt_open(struct smbconf_ctx *ctx)
237 return smbconf_txt_load_file(ctx);
240 static int smbconf_txt_close(struct smbconf_ctx *ctx)
242 smbconf_txt_flush_cache(ctx);
243 return 0;
247 * Get the change sequence number of the given service/parameter.
248 * service and parameter strings may be NULL.
250 static void smbconf_txt_get_csn(struct smbconf_ctx *ctx,
251 struct smbconf_csn *csn,
252 const char *service, const char *param)
254 if (csn == NULL) {
255 return;
258 csn->csn = (uint64_t)file_modtime(ctx->path);
262 * Drop the whole configuration (restarting empty)
264 static WERROR smbconf_txt_drop(struct smbconf_ctx *ctx)
266 return WERR_NOT_SUPPORTED;
270 * get the list of share names defined in the configuration.
272 static WERROR smbconf_txt_get_share_names(struct smbconf_ctx *ctx,
273 TALLOC_CTX *mem_ctx,
274 uint32_t *num_shares,
275 char ***share_names)
277 uint32_t count;
278 uint32_t added_count = 0;
279 TALLOC_CTX *tmp_ctx = NULL;
280 WERROR werr = WERR_OK;
281 char **tmp_share_names = NULL;
283 if ((num_shares == NULL) || (share_names == NULL)) {
284 werr = WERR_INVALID_PARAM;
285 goto done;
288 werr = smbconf_txt_load_file(ctx);
289 if (!W_ERROR_IS_OK(werr)) {
290 return werr;
293 tmp_ctx = talloc_stackframe();
294 if (tmp_ctx == NULL) {
295 werr = WERR_NOMEM;
296 goto done;
299 /* make sure "global" is always listed first */
300 if (smbconf_share_exists(ctx, GLOBAL_NAME)) {
301 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
302 0, GLOBAL_NAME);
303 if (!W_ERROR_IS_OK(werr)) {
304 goto done;
306 added_count++;
309 for (count = 0; count < pd(ctx)->cache->num_shares; count++) {
310 if (strequal(pd(ctx)->cache->share_names[count], GLOBAL_NAME)) {
311 continue;
314 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
315 added_count,
316 pd(ctx)->cache->share_names[count]);
317 if (!W_ERROR_IS_OK(werr)) {
318 goto done;
320 added_count++;
323 *num_shares = added_count;
324 if (added_count > 0) {
325 *share_names = talloc_move(mem_ctx, &tmp_share_names);
326 } else {
327 *share_names = NULL;
330 done:
331 TALLOC_FREE(tmp_ctx);
332 return werr;
336 * check if a share/service of a given name exists
338 static bool smbconf_txt_share_exists(struct smbconf_ctx *ctx,
339 const char *servicename)
341 WERROR werr;
343 werr = smbconf_txt_load_file(ctx);
344 if (!W_ERROR_IS_OK(werr)) {
345 return false;
348 return smbconf_find_in_array(servicename,
349 pd(ctx)->cache->share_names,
350 pd(ctx)->cache->num_shares, NULL);
354 * Add a service if it does not already exist
356 static WERROR smbconf_txt_create_share(struct smbconf_ctx *ctx,
357 const char *servicename)
359 return WERR_NOT_SUPPORTED;
363 * get a definition of a share (service) from configuration.
365 static WERROR smbconf_txt_get_share(struct smbconf_ctx *ctx,
366 TALLOC_CTX *mem_ctx,
367 const char *servicename,
368 uint32_t *num_params,
369 char ***param_names, char ***param_values)
371 WERROR werr;
372 uint32_t sidx, count;
373 bool found;
374 TALLOC_CTX *tmp_ctx = NULL;
375 char **tmp_param_names = NULL;
376 char **tmp_param_values = NULL;
378 werr = smbconf_txt_load_file(ctx);
379 if (!W_ERROR_IS_OK(werr)) {
380 return werr;
383 found = smbconf_find_in_array(servicename,
384 pd(ctx)->cache->share_names,
385 pd(ctx)->cache->num_shares,
386 &sidx);
387 if (!found) {
388 return WERR_NO_SUCH_SERVICE;
391 tmp_ctx = talloc_stackframe();
392 if (tmp_ctx == NULL) {
393 werr = WERR_NOMEM;
394 goto done;
397 for (count = 0; count < pd(ctx)->cache->num_params[sidx]; count++) {
398 werr = smbconf_add_string_to_array(tmp_ctx,
399 &tmp_param_names,
400 count,
401 pd(ctx)->cache->param_names[sidx][count]);
402 if (!W_ERROR_IS_OK(werr)) {
403 goto done;
405 werr = smbconf_add_string_to_array(tmp_ctx,
406 &tmp_param_values,
407 count,
408 pd(ctx)->cache->param_values[sidx][count]);
409 if (!W_ERROR_IS_OK(werr)) {
410 goto done;
414 *num_params = count;
415 if (count > 0) {
416 *param_names = talloc_move(mem_ctx, &tmp_param_names);
417 *param_values = talloc_move(mem_ctx, &tmp_param_values);
418 } else {
419 *param_names = NULL;
420 *param_values = NULL;
423 done:
424 TALLOC_FREE(tmp_ctx);
425 return werr;
429 * delete a service from configuration
431 static WERROR smbconf_txt_delete_share(struct smbconf_ctx *ctx,
432 const char *servicename)
434 return WERR_NOT_SUPPORTED;
438 * set a configuration parameter to the value provided.
440 static WERROR smbconf_txt_set_parameter(struct smbconf_ctx *ctx,
441 const char *service,
442 const char *param,
443 const char *valstr)
445 return WERR_NOT_SUPPORTED;
449 * get the value of a configuration parameter as a string
451 static WERROR smbconf_txt_get_parameter(struct smbconf_ctx *ctx,
452 TALLOC_CTX *mem_ctx,
453 const char *service,
454 const char *param,
455 char **valstr)
457 WERROR werr;
458 bool found;
459 uint32_t share_index, param_index;
461 werr = smbconf_txt_load_file(ctx);
462 if (!W_ERROR_IS_OK(werr)) {
463 return werr;
466 found = smbconf_find_in_array(service,
467 pd(ctx)->cache->share_names,
468 pd(ctx)->cache->num_shares,
469 &share_index);
470 if (!found) {
471 return WERR_NO_SUCH_SERVICE;
474 found = smbconf_reverse_find_in_array(param,
475 pd(ctx)->cache->param_names[share_index],
476 pd(ctx)->cache->num_params[share_index],
477 &param_index);
478 if (!found) {
479 return WERR_INVALID_PARAM;
482 *valstr = talloc_strdup(mem_ctx,
483 pd(ctx)->cache->param_values[share_index][param_index]);
485 if (*valstr == NULL) {
486 return WERR_NOMEM;
489 return WERR_OK;
493 * delete a parameter from configuration
495 static WERROR smbconf_txt_delete_parameter(struct smbconf_ctx *ctx,
496 const char *service,
497 const char *param)
499 return WERR_NOT_SUPPORTED;
502 static WERROR smbconf_txt_get_includes(struct smbconf_ctx *ctx,
503 TALLOC_CTX *mem_ctx,
504 const char *service,
505 uint32_t *num_includes,
506 char ***includes)
508 WERROR werr;
509 bool found;
510 uint32_t sidx, count;
511 TALLOC_CTX *tmp_ctx = NULL;
512 uint32_t tmp_num_includes = 0;
513 char **tmp_includes = NULL;
515 werr = smbconf_txt_load_file(ctx);
516 if (!W_ERROR_IS_OK(werr)) {
517 return werr;
520 found = smbconf_find_in_array(service,
521 pd(ctx)->cache->share_names,
522 pd(ctx)->cache->num_shares,
523 &sidx);
524 if (!found) {
525 return WERR_NO_SUCH_SERVICE;
528 tmp_ctx = talloc_stackframe();
530 for (count = 0; count < pd(ctx)->cache->num_params[sidx]; count++) {
531 if (strequal(pd(ctx)->cache->param_names[sidx][count],
532 "include"))
534 werr = smbconf_add_string_to_array(tmp_ctx,
535 &tmp_includes,
536 tmp_num_includes,
537 pd(ctx)->cache->param_values[sidx][count]);
538 if (!W_ERROR_IS_OK(werr)) {
539 goto done;
541 tmp_num_includes++;
545 *num_includes = tmp_num_includes;
546 if (*num_includes > 0) {
547 *includes = talloc_move(mem_ctx, &tmp_includes);
548 if (*includes == NULL) {
549 werr = WERR_NOMEM;
550 goto done;
552 } else {
553 *includes = NULL;
556 werr = WERR_OK;
558 done:
559 TALLOC_FREE(tmp_ctx);
560 return werr;
563 static WERROR smbconf_txt_set_includes(struct smbconf_ctx *ctx,
564 const char *service,
565 uint32_t num_includes,
566 const char **includes)
568 return WERR_NOT_SUPPORTED;
571 static struct smbconf_ops smbconf_ops_txt = {
572 .init = smbconf_txt_init,
573 .shutdown = smbconf_txt_shutdown,
574 .open_conf = smbconf_txt_open,
575 .close_conf = smbconf_txt_close,
576 .get_csn = smbconf_txt_get_csn,
577 .drop = smbconf_txt_drop,
578 .get_share_names = smbconf_txt_get_share_names,
579 .share_exists = smbconf_txt_share_exists,
580 .create_share = smbconf_txt_create_share,
581 .get_share = smbconf_txt_get_share,
582 .delete_share = smbconf_txt_delete_share,
583 .set_parameter = smbconf_txt_set_parameter,
584 .get_parameter = smbconf_txt_get_parameter,
585 .delete_parameter = smbconf_txt_delete_parameter,
586 .get_includes = smbconf_txt_get_includes,
587 .set_includes = smbconf_txt_set_includes,
592 * initialize the smbconf text backend
593 * the only function that is exported from this module
595 WERROR smbconf_init_txt_simple(TALLOC_CTX *mem_ctx,
596 struct smbconf_ctx **conf_ctx,
597 const char *path,
598 bool verbatim)
600 WERROR werr;
602 werr = smbconf_init(mem_ctx, conf_ctx, path, &smbconf_ops_txt);
603 if (!W_ERROR_IS_OK(werr)) {
604 return werr;
607 pd(*conf_ctx)->verbatim = verbatim;
609 return smbconf_txt_load_file(*conf_ctx);