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"
30 #include "lib/smbconf/smbconf_txt.h"
33 uint32_t current_share
;
41 struct txt_private_data
{
42 struct txt_cache
*cache
;
47 /**********************************************************************
51 **********************************************************************/
54 * a convenience helper to cast the private data structure
56 static struct txt_private_data
*pd(struct smbconf_ctx
*ctx
)
58 return (struct txt_private_data
*)(ctx
->data
);
61 static bool smbconf_txt_do_section(const char *section
, void *private_data
)
65 struct txt_private_data
*tpd
= (struct txt_private_data
*)private_data
;
66 struct txt_cache
*cache
= tpd
->cache
;
68 if (smbconf_find_in_array(section
, cache
->share_names
,
69 cache
->num_shares
, &idx
))
71 cache
->current_share
= idx
;
75 werr
= smbconf_add_string_to_array(cache
, &(cache
->share_names
),
76 cache
->num_shares
, section
);
77 if (!W_ERROR_IS_OK(werr
)) {
80 cache
->current_share
= cache
->num_shares
;
83 cache
->param_names
= talloc_realloc(cache
,
87 if (cache
->param_names
== NULL
) {
90 cache
->param_names
[cache
->current_share
] = NULL
;
92 cache
->param_values
= talloc_realloc(cache
,
96 if (cache
->param_values
== NULL
) {
99 cache
->param_values
[cache
->current_share
] = NULL
;
101 cache
->num_params
= talloc_realloc(cache
,
105 if (cache
->num_params
== NULL
) {
108 cache
->num_params
[cache
->current_share
] = 0;
113 static bool smbconf_txt_do_parameter(const char *param_name
,
114 const char *param_value
,
118 char **param_names
, **param_values
;
121 struct txt_private_data
*tpd
= (struct txt_private_data
*)private_data
;
122 struct txt_cache
*cache
= tpd
->cache
;
124 if (cache
->num_shares
== 0) {
126 * not in any share yet,
127 * initialize the "empty" section (NULL):
128 * parameters without a previous [section] are stored here.
130 if (!smbconf_txt_do_section(NULL
, private_data
)) {
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
];
139 if (!(tpd
->verbatim
) &&
140 smbconf_find_in_array(param_name
, param_names
, num_params
, &idx
))
142 talloc_free(param_values
[idx
]);
143 param_values
[idx
] = talloc_strdup(cache
, param_value
);
144 if (param_values
[idx
] == NULL
) {
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
)) {
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
);
162 static void smbconf_txt_flush_cache(struct smbconf_ctx
*ctx
)
164 talloc_free(pd(ctx
)->cache
);
165 pd(ctx
)->cache
= NULL
;
168 static WERROR
smbconf_txt_init_cache(struct smbconf_ctx
*ctx
)
170 if (pd(ctx
)->cache
!= NULL
) {
171 smbconf_txt_flush_cache(ctx
);
174 pd(ctx
)->cache
= talloc_zero(pd(ctx
), struct txt_cache
);
176 if (pd(ctx
)->cache
== NULL
) {
183 static WERROR
smbconf_txt_load_file(struct smbconf_ctx
*ctx
)
188 if (!file_exist(ctx
->path
)) {
192 new_csn
= (uint64_t)file_modtime(ctx
->path
);
193 if (new_csn
== pd(ctx
)->csn
) {
197 werr
= smbconf_txt_init_cache(ctx
);
198 if (!W_ERROR_IS_OK(werr
)) {
202 if (!pm_process(ctx
->path
, smbconf_txt_do_section
,
203 smbconf_txt_do_parameter
, pd(ctx
)))
205 return WERR_CAN_NOT_COMPLETE
;
208 pd(ctx
)->csn
= new_csn
;
214 /**********************************************************************
216 * smbconf operations: text backend implementations
218 **********************************************************************/
221 * initialize the text based smbconf backend
223 static WERROR
smbconf_txt_init(struct smbconf_ctx
*ctx
, const char *path
)
228 ctx
->path
= talloc_strdup(ctx
, path
);
229 if (ctx
->path
== NULL
) {
233 ctx
->data
= talloc_zero(ctx
, struct txt_private_data
);
234 if (ctx
->data
== NULL
) {
238 pd(ctx
)->verbatim
= true;
243 static int smbconf_txt_shutdown(struct smbconf_ctx
*ctx
)
245 return ctx
->ops
->close_conf(ctx
);
248 static bool smbconf_txt_requires_messaging(struct smbconf_ctx
*ctx
)
253 static bool smbconf_txt_is_writeable(struct smbconf_ctx
*ctx
)
255 /* no write support in this backend yet... */
259 static WERROR
smbconf_txt_open(struct smbconf_ctx
*ctx
)
261 return smbconf_txt_load_file(ctx
);
264 static int smbconf_txt_close(struct smbconf_ctx
*ctx
)
266 smbconf_txt_flush_cache(ctx
);
271 * Get the change sequence number of the given service/parameter.
272 * service and parameter strings may be NULL.
274 static void smbconf_txt_get_csn(struct smbconf_ctx
*ctx
,
275 struct smbconf_csn
*csn
,
276 const char *service
, const char *param
)
282 csn
->csn
= (uint64_t)file_modtime(ctx
->path
);
286 * Drop the whole configuration (restarting empty)
288 static WERROR
smbconf_txt_drop(struct smbconf_ctx
*ctx
)
290 return WERR_NOT_SUPPORTED
;
294 * get the list of share names defined in the configuration.
296 static WERROR
smbconf_txt_get_share_names(struct smbconf_ctx
*ctx
,
298 uint32_t *num_shares
,
302 uint32_t added_count
= 0;
303 TALLOC_CTX
*tmp_ctx
= NULL
;
304 WERROR werr
= WERR_OK
;
305 char **tmp_share_names
= NULL
;
307 if ((num_shares
== NULL
) || (share_names
== NULL
)) {
308 werr
= WERR_INVALID_PARAM
;
312 werr
= smbconf_txt_load_file(ctx
);
313 if (!W_ERROR_IS_OK(werr
)) {
317 tmp_ctx
= talloc_stackframe();
319 /* make sure "global" is always listed first,
320 * possibly after NULL section */
322 if (smbconf_share_exists(ctx
, NULL
)) {
323 werr
= smbconf_add_string_to_array(tmp_ctx
, &tmp_share_names
,
325 if (!W_ERROR_IS_OK(werr
)) {
331 if (smbconf_share_exists(ctx
, GLOBAL_NAME
)) {
332 werr
= smbconf_add_string_to_array(tmp_ctx
, &tmp_share_names
,
333 added_count
, GLOBAL_NAME
);
334 if (!W_ERROR_IS_OK(werr
)) {
340 for (count
= 0; count
< pd(ctx
)->cache
->num_shares
; count
++) {
341 if (strequal(pd(ctx
)->cache
->share_names
[count
], GLOBAL_NAME
) ||
342 (pd(ctx
)->cache
->share_names
[count
] == NULL
))
347 werr
= smbconf_add_string_to_array(tmp_ctx
, &tmp_share_names
,
349 pd(ctx
)->cache
->share_names
[count
]);
350 if (!W_ERROR_IS_OK(werr
)) {
356 *num_shares
= added_count
;
357 if (added_count
> 0) {
358 *share_names
= talloc_move(mem_ctx
, &tmp_share_names
);
364 talloc_free(tmp_ctx
);
369 * check if a share/service of a given name exists
371 static bool smbconf_txt_share_exists(struct smbconf_ctx
*ctx
,
372 const char *servicename
)
376 werr
= smbconf_txt_load_file(ctx
);
377 if (!W_ERROR_IS_OK(werr
)) {
381 return smbconf_find_in_array(servicename
,
382 pd(ctx
)->cache
->share_names
,
383 pd(ctx
)->cache
->num_shares
, NULL
);
387 * Add a service if it does not already exist
389 static WERROR
smbconf_txt_create_share(struct smbconf_ctx
*ctx
,
390 const char *servicename
)
392 return WERR_NOT_SUPPORTED
;
396 * get a definition of a share (service) from configuration.
398 static WERROR
smbconf_txt_get_share(struct smbconf_ctx
*ctx
,
400 const char *servicename
,
401 struct smbconf_service
**service
)
404 uint32_t sidx
, count
;
406 TALLOC_CTX
*tmp_ctx
= NULL
;
407 struct smbconf_service
*tmp_service
= NULL
;
409 werr
= smbconf_txt_load_file(ctx
);
410 if (!W_ERROR_IS_OK(werr
)) {
414 found
= smbconf_find_in_array(servicename
,
415 pd(ctx
)->cache
->share_names
,
416 pd(ctx
)->cache
->num_shares
,
419 return WERR_NO_SUCH_SERVICE
;
422 tmp_ctx
= talloc_stackframe();
424 tmp_service
= talloc_zero(tmp_ctx
, struct smbconf_service
);
425 if (tmp_service
== NULL
) {
430 if (servicename
!= NULL
) {
431 tmp_service
->name
= talloc_strdup(tmp_service
, servicename
);
432 if (tmp_service
->name
== NULL
) {
438 for (count
= 0; count
< pd(ctx
)->cache
->num_params
[sidx
]; count
++) {
439 werr
= smbconf_add_string_to_array(tmp_service
,
440 &(tmp_service
->param_names
),
442 pd(ctx
)->cache
->param_names
[sidx
][count
]);
443 if (!W_ERROR_IS_OK(werr
)) {
446 werr
= smbconf_add_string_to_array(tmp_service
,
447 &(tmp_service
->param_values
),
449 pd(ctx
)->cache
->param_values
[sidx
][count
]);
450 if (!W_ERROR_IS_OK(werr
)) {
455 tmp_service
->num_params
= count
;
456 *service
= talloc_move(mem_ctx
, &tmp_service
);
459 talloc_free(tmp_ctx
);
464 * delete a service from configuration
466 static WERROR
smbconf_txt_delete_share(struct smbconf_ctx
*ctx
,
467 const char *servicename
)
469 return WERR_NOT_SUPPORTED
;
473 * set a configuration parameter to the value provided.
475 static WERROR
smbconf_txt_set_parameter(struct smbconf_ctx
*ctx
,
480 return WERR_NOT_SUPPORTED
;
484 * get the value of a configuration parameter as a string
486 static WERROR
smbconf_txt_get_parameter(struct smbconf_ctx
*ctx
,
494 uint32_t share_index
, param_index
;
496 werr
= smbconf_txt_load_file(ctx
);
497 if (!W_ERROR_IS_OK(werr
)) {
501 found
= smbconf_find_in_array(service
,
502 pd(ctx
)->cache
->share_names
,
503 pd(ctx
)->cache
->num_shares
,
506 return WERR_NO_SUCH_SERVICE
;
509 found
= smbconf_reverse_find_in_array(param
,
510 pd(ctx
)->cache
->param_names
[share_index
],
511 pd(ctx
)->cache
->num_params
[share_index
],
514 return WERR_INVALID_PARAM
;
517 *valstr
= talloc_strdup(mem_ctx
,
518 pd(ctx
)->cache
->param_values
[share_index
][param_index
]);
520 if (*valstr
== NULL
) {
528 * delete a parameter from configuration
530 static WERROR
smbconf_txt_delete_parameter(struct smbconf_ctx
*ctx
,
534 return WERR_NOT_SUPPORTED
;
537 static WERROR
smbconf_txt_get_includes(struct smbconf_ctx
*ctx
,
540 uint32_t *num_includes
,
545 uint32_t sidx
, count
;
546 TALLOC_CTX
*tmp_ctx
= NULL
;
547 uint32_t tmp_num_includes
= 0;
548 char **tmp_includes
= NULL
;
550 werr
= smbconf_txt_load_file(ctx
);
551 if (!W_ERROR_IS_OK(werr
)) {
555 found
= smbconf_find_in_array(service
,
556 pd(ctx
)->cache
->share_names
,
557 pd(ctx
)->cache
->num_shares
,
560 return WERR_NO_SUCH_SERVICE
;
563 tmp_ctx
= talloc_stackframe();
565 for (count
= 0; count
< pd(ctx
)->cache
->num_params
[sidx
]; count
++) {
566 if (strequal(pd(ctx
)->cache
->param_names
[sidx
][count
],
569 werr
= smbconf_add_string_to_array(tmp_ctx
,
572 pd(ctx
)->cache
->param_values
[sidx
][count
]);
573 if (!W_ERROR_IS_OK(werr
)) {
580 *num_includes
= tmp_num_includes
;
581 if (*num_includes
> 0) {
582 *includes
= talloc_move(mem_ctx
, &tmp_includes
);
583 if (*includes
== NULL
) {
594 talloc_free(tmp_ctx
);
598 static WERROR
smbconf_txt_set_includes(struct smbconf_ctx
*ctx
,
600 uint32_t num_includes
,
601 const char **includes
)
603 return WERR_NOT_SUPPORTED
;
606 static WERROR
smbconf_txt_delete_includes(struct smbconf_ctx
*ctx
,
609 return WERR_NOT_SUPPORTED
;
612 static WERROR
smbconf_txt_transaction_start(struct smbconf_ctx
*ctx
)
617 static WERROR
smbconf_txt_transaction_commit(struct smbconf_ctx
*ctx
)
622 static WERROR
smbconf_txt_transaction_cancel(struct smbconf_ctx
*ctx
)
627 static struct smbconf_ops smbconf_ops_txt
= {
628 .init
= smbconf_txt_init
,
629 .shutdown
= smbconf_txt_shutdown
,
630 .requires_messaging
= smbconf_txt_requires_messaging
,
631 .is_writeable
= smbconf_txt_is_writeable
,
632 .open_conf
= smbconf_txt_open
,
633 .close_conf
= smbconf_txt_close
,
634 .get_csn
= smbconf_txt_get_csn
,
635 .drop
= smbconf_txt_drop
,
636 .get_share_names
= smbconf_txt_get_share_names
,
637 .share_exists
= smbconf_txt_share_exists
,
638 .create_share
= smbconf_txt_create_share
,
639 .get_share
= smbconf_txt_get_share
,
640 .delete_share
= smbconf_txt_delete_share
,
641 .set_parameter
= smbconf_txt_set_parameter
,
642 .get_parameter
= smbconf_txt_get_parameter
,
643 .delete_parameter
= smbconf_txt_delete_parameter
,
644 .get_includes
= smbconf_txt_get_includes
,
645 .set_includes
= smbconf_txt_set_includes
,
646 .delete_includes
= smbconf_txt_delete_includes
,
647 .transaction_start
= smbconf_txt_transaction_start
,
648 .transaction_commit
= smbconf_txt_transaction_commit
,
649 .transaction_cancel
= smbconf_txt_transaction_cancel
,
654 * initialize the smbconf text backend
655 * the only function that is exported from this module
657 WERROR
smbconf_init_txt(TALLOC_CTX
*mem_ctx
,
658 struct smbconf_ctx
**conf_ctx
,
663 werr
= smbconf_init_internal(mem_ctx
, conf_ctx
, path
, &smbconf_ops_txt
);
664 if (!W_ERROR_IS_OK(werr
)) {
668 return smbconf_txt_load_file(*conf_ctx
);