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.
25 * Don't expect brilliant performance, since it is not hashing the lists.
29 #include "smbconf_private.h"
32 uint32_t current_share
;
40 struct txt_private_data
{
41 struct txt_cache
*cache
;
46 /**********************************************************************
50 **********************************************************************/
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
)
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
;
74 werr
= smbconf_add_string_to_array(cache
, &(cache
->share_names
),
75 cache
->num_shares
, section
);
76 if (!W_ERROR_IS_OK(werr
)) {
79 cache
->current_share
= cache
->num_shares
;
82 cache
->param_names
= TALLOC_REALLOC_ARRAY(cache
,
86 if (cache
->param_names
== NULL
) {
89 cache
->param_names
[cache
->current_share
] = NULL
;
91 cache
->param_values
= TALLOC_REALLOC_ARRAY(cache
,
95 if (cache
->param_values
== NULL
) {
98 cache
->param_values
[cache
->current_share
] = NULL
;
100 cache
->num_params
= TALLOC_REALLOC_ARRAY(cache
,
104 if (cache
->num_params
== NULL
) {
107 cache
->num_params
[cache
->current_share
] = 0;
112 static bool smbconf_txt_do_parameter(const char *param_name
,
113 const char *param_value
,
117 char **param_names
, **param_values
;
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
)) {
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
) {
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
)) {
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
) {
181 static WERROR
smbconf_txt_load_file(struct smbconf_ctx
*ctx
)
186 if (!file_exist(ctx
->path
, NULL
)) {
190 new_csn
= (uint64_t)file_modtime(ctx
->path
);
191 if (new_csn
== pd(ctx
)->csn
) {
195 werr
= smbconf_txt_init_cache(ctx
);
196 if (!W_ERROR_IS_OK(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
;
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
)
224 path
= get_dyn_CONFIGFILE();
226 ctx
->path
= talloc_strdup(ctx
, path
);
227 if (ctx
->path
== NULL
) {
231 ctx
->data
= TALLOC_ZERO_P(ctx
, struct txt_private_data
);
232 if (ctx
->data
== NULL
) {
236 pd(ctx
)->verbatim
= true;
241 static int smbconf_txt_shutdown(struct smbconf_ctx
*ctx
)
243 return ctx
->ops
->close_conf(ctx
);
246 static bool smbconf_txt_requires_messaging(struct smbconf_ctx
*ctx
)
251 static WERROR
smbconf_txt_open(struct smbconf_ctx
*ctx
)
253 return smbconf_txt_load_file(ctx
);
256 static int smbconf_txt_close(struct smbconf_ctx
*ctx
)
258 smbconf_txt_flush_cache(ctx
);
263 * Get the change sequence number of the given service/parameter.
264 * service and parameter strings may be NULL.
266 static void smbconf_txt_get_csn(struct smbconf_ctx
*ctx
,
267 struct smbconf_csn
*csn
,
268 const char *service
, const char *param
)
274 csn
->csn
= (uint64_t)file_modtime(ctx
->path
);
278 * Drop the whole configuration (restarting empty)
280 static WERROR
smbconf_txt_drop(struct smbconf_ctx
*ctx
)
282 return WERR_NOT_SUPPORTED
;
286 * get the list of share names defined in the configuration.
288 static WERROR
smbconf_txt_get_share_names(struct smbconf_ctx
*ctx
,
290 uint32_t *num_shares
,
294 uint32_t added_count
= 0;
295 TALLOC_CTX
*tmp_ctx
= NULL
;
296 WERROR werr
= WERR_OK
;
297 char **tmp_share_names
= NULL
;
299 if ((num_shares
== NULL
) || (share_names
== NULL
)) {
300 werr
= WERR_INVALID_PARAM
;
304 werr
= smbconf_txt_load_file(ctx
);
305 if (!W_ERROR_IS_OK(werr
)) {
309 tmp_ctx
= talloc_stackframe();
311 /* make sure "global" is always listed first,
312 * possibly after NULL section */
314 if (smbconf_share_exists(ctx
, NULL
)) {
315 werr
= smbconf_add_string_to_array(tmp_ctx
, &tmp_share_names
,
317 if (!W_ERROR_IS_OK(werr
)) {
323 if (smbconf_share_exists(ctx
, GLOBAL_NAME
)) {
324 werr
= smbconf_add_string_to_array(tmp_ctx
, &tmp_share_names
,
325 added_count
, GLOBAL_NAME
);
326 if (!W_ERROR_IS_OK(werr
)) {
332 for (count
= 0; count
< pd(ctx
)->cache
->num_shares
; count
++) {
333 if (strequal(pd(ctx
)->cache
->share_names
[count
], GLOBAL_NAME
) ||
334 (pd(ctx
)->cache
->share_names
[count
] == NULL
))
339 werr
= smbconf_add_string_to_array(tmp_ctx
, &tmp_share_names
,
341 pd(ctx
)->cache
->share_names
[count
]);
342 if (!W_ERROR_IS_OK(werr
)) {
348 *num_shares
= added_count
;
349 if (added_count
> 0) {
350 *share_names
= talloc_move(mem_ctx
, &tmp_share_names
);
356 TALLOC_FREE(tmp_ctx
);
361 * check if a share/service of a given name exists
363 static bool smbconf_txt_share_exists(struct smbconf_ctx
*ctx
,
364 const char *servicename
)
368 werr
= smbconf_txt_load_file(ctx
);
369 if (!W_ERROR_IS_OK(werr
)) {
373 return smbconf_find_in_array(servicename
,
374 pd(ctx
)->cache
->share_names
,
375 pd(ctx
)->cache
->num_shares
, NULL
);
379 * Add a service if it does not already exist
381 static WERROR
smbconf_txt_create_share(struct smbconf_ctx
*ctx
,
382 const char *servicename
)
384 return WERR_NOT_SUPPORTED
;
388 * get a definition of a share (service) from configuration.
390 static WERROR
smbconf_txt_get_share(struct smbconf_ctx
*ctx
,
392 const char *servicename
,
393 struct smbconf_service
**service
)
396 uint32_t sidx
, count
;
398 TALLOC_CTX
*tmp_ctx
= NULL
;
399 struct smbconf_service
*tmp_service
= NULL
;
401 werr
= smbconf_txt_load_file(ctx
);
402 if (!W_ERROR_IS_OK(werr
)) {
406 found
= smbconf_find_in_array(servicename
,
407 pd(ctx
)->cache
->share_names
,
408 pd(ctx
)->cache
->num_shares
,
411 return WERR_NO_SUCH_SERVICE
;
414 tmp_ctx
= talloc_stackframe();
416 tmp_service
= TALLOC_ZERO_P(tmp_ctx
, struct smbconf_service
);
417 if (tmp_service
== NULL
) {
422 if (servicename
!= NULL
) {
423 tmp_service
->name
= talloc_strdup(tmp_service
, servicename
);
424 if (tmp_service
->name
== NULL
) {
430 for (count
= 0; count
< pd(ctx
)->cache
->num_params
[sidx
]; count
++) {
431 werr
= smbconf_add_string_to_array(tmp_service
,
432 &(tmp_service
->param_names
),
434 pd(ctx
)->cache
->param_names
[sidx
][count
]);
435 if (!W_ERROR_IS_OK(werr
)) {
438 werr
= smbconf_add_string_to_array(tmp_service
,
439 &(tmp_service
->param_values
),
441 pd(ctx
)->cache
->param_values
[sidx
][count
]);
442 if (!W_ERROR_IS_OK(werr
)) {
447 tmp_service
->num_params
= count
;
449 *service
= talloc_move(mem_ctx
, &tmp_service
);
455 TALLOC_FREE(tmp_ctx
);
460 * delete a service from configuration
462 static WERROR
smbconf_txt_delete_share(struct smbconf_ctx
*ctx
,
463 const char *servicename
)
465 return WERR_NOT_SUPPORTED
;
469 * set a configuration parameter to the value provided.
471 static WERROR
smbconf_txt_set_parameter(struct smbconf_ctx
*ctx
,
476 return WERR_NOT_SUPPORTED
;
480 * get the value of a configuration parameter as a string
482 static WERROR
smbconf_txt_get_parameter(struct smbconf_ctx
*ctx
,
490 uint32_t share_index
, param_index
;
492 werr
= smbconf_txt_load_file(ctx
);
493 if (!W_ERROR_IS_OK(werr
)) {
497 found
= smbconf_find_in_array(service
,
498 pd(ctx
)->cache
->share_names
,
499 pd(ctx
)->cache
->num_shares
,
502 return WERR_NO_SUCH_SERVICE
;
505 found
= smbconf_reverse_find_in_array(param
,
506 pd(ctx
)->cache
->param_names
[share_index
],
507 pd(ctx
)->cache
->num_params
[share_index
],
510 return WERR_INVALID_PARAM
;
513 *valstr
= talloc_strdup(mem_ctx
,
514 pd(ctx
)->cache
->param_values
[share_index
][param_index
]);
516 if (*valstr
== NULL
) {
524 * delete a parameter from configuration
526 static WERROR
smbconf_txt_delete_parameter(struct smbconf_ctx
*ctx
,
530 return WERR_NOT_SUPPORTED
;
533 static WERROR
smbconf_txt_get_includes(struct smbconf_ctx
*ctx
,
536 uint32_t *num_includes
,
541 uint32_t sidx
, count
;
542 TALLOC_CTX
*tmp_ctx
= NULL
;
543 uint32_t tmp_num_includes
= 0;
544 char **tmp_includes
= NULL
;
546 werr
= smbconf_txt_load_file(ctx
);
547 if (!W_ERROR_IS_OK(werr
)) {
551 found
= smbconf_find_in_array(service
,
552 pd(ctx
)->cache
->share_names
,
553 pd(ctx
)->cache
->num_shares
,
556 return WERR_NO_SUCH_SERVICE
;
559 tmp_ctx
= talloc_stackframe();
561 for (count
= 0; count
< pd(ctx
)->cache
->num_params
[sidx
]; count
++) {
562 if (strequal(pd(ctx
)->cache
->param_names
[sidx
][count
],
565 werr
= smbconf_add_string_to_array(tmp_ctx
,
568 pd(ctx
)->cache
->param_values
[sidx
][count
]);
569 if (!W_ERROR_IS_OK(werr
)) {
576 *num_includes
= tmp_num_includes
;
577 if (*num_includes
> 0) {
578 *includes
= talloc_move(mem_ctx
, &tmp_includes
);
579 if (*includes
== NULL
) {
590 TALLOC_FREE(tmp_ctx
);
594 static WERROR
smbconf_txt_set_includes(struct smbconf_ctx
*ctx
,
596 uint32_t num_includes
,
597 const char **includes
)
599 return WERR_NOT_SUPPORTED
;
602 static WERROR
smbconf_txt_delete_includes(struct smbconf_ctx
*ctx
,
605 return WERR_NOT_SUPPORTED
;
609 static struct smbconf_ops smbconf_ops_txt
= {
610 .init
= smbconf_txt_init
,
611 .shutdown
= smbconf_txt_shutdown
,
612 .requires_messaging
= smbconf_txt_requires_messaging
,
613 .open_conf
= smbconf_txt_open
,
614 .close_conf
= smbconf_txt_close
,
615 .get_csn
= smbconf_txt_get_csn
,
616 .drop
= smbconf_txt_drop
,
617 .get_share_names
= smbconf_txt_get_share_names
,
618 .share_exists
= smbconf_txt_share_exists
,
619 .create_share
= smbconf_txt_create_share
,
620 .get_share
= smbconf_txt_get_share
,
621 .delete_share
= smbconf_txt_delete_share
,
622 .set_parameter
= smbconf_txt_set_parameter
,
623 .get_parameter
= smbconf_txt_get_parameter
,
624 .delete_parameter
= smbconf_txt_delete_parameter
,
625 .get_includes
= smbconf_txt_get_includes
,
626 .set_includes
= smbconf_txt_set_includes
,
627 .delete_includes
= smbconf_txt_delete_includes
,
632 * initialize the smbconf text backend
633 * the only function that is exported from this module
635 WERROR
smbconf_init_txt(TALLOC_CTX
*mem_ctx
,
636 struct smbconf_ctx
**conf_ctx
,
641 werr
= smbconf_init_internal(mem_ctx
, conf_ctx
, path
, &smbconf_ops_txt
);
642 if (!W_ERROR_IS_OK(werr
)) {
646 return smbconf_txt_load_file(*conf_ctx
);