2 * Unix SMB/CIFS implementation.
3 * libsmbconf - Samba configuration library
4 * Copyright (C) Michael Adam 2007-2008
5 * Copyright (C) Guenther Deschner 2007
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "smbconf_private.h"
24 /**********************************************************************
26 * internal helper functions
28 **********************************************************************/
30 static WERROR smbconf_global_check(struct smbconf_ctx *ctx)
32 if (!smbconf_share_exists(ctx, GLOBAL_NAME)) {
33 return smbconf_create_share(ctx, GLOBAL_NAME);
39 /**********************************************************************
41 * The actual libsmbconf API functions that are exported.
43 **********************************************************************/
46 * Tell whether the backend requires messaging to be set up
47 * for the backend to work correctly.
49 bool smbconf_backend_requires_messaging(struct smbconf_ctx *ctx)
51 return ctx->ops->requires_messaging(ctx);
55 * Tell whether the source is writeable.
57 bool smbconf_is_writeable(struct smbconf_ctx *ctx)
59 return ctx->ops->is_writeable(ctx);
63 * Close the configuration.
65 void smbconf_shutdown(struct smbconf_ctx *ctx)
71 * Detect changes in the configuration.
72 * The given csn struct is filled with the current csn.
73 * smbconf_changed() can also be used for initial retrieval
76 bool smbconf_changed(struct smbconf_ctx *ctx, struct smbconf_csn *csn,
77 const char *service, const char *param)
79 struct smbconf_csn old_csn;
87 ctx->ops->get_csn(ctx, csn, service, param);
88 return (csn->csn != old_csn.csn);
92 * Drop the whole configuration (restarting empty).
94 WERROR smbconf_drop(struct smbconf_ctx *ctx)
96 return ctx->ops->drop(ctx);
100 * Get the whole configuration as lists of strings with counts:
102 * num_shares : number of shares
103 * share_names : list of length num_shares of share names
104 * num_params : list of length num_shares of parameter counts for each share
105 * param_names : list of lists of parameter names for each share
106 * param_values : list of lists of parameter values for each share
108 WERROR smbconf_get_config(struct smbconf_ctx *ctx,
110 uint32_t *num_shares,
111 struct smbconf_service ***services)
113 WERROR werr = WERR_OK;
114 TALLOC_CTX *tmp_ctx = NULL;
115 uint32_t tmp_num_shares;
116 char **tmp_share_names;
117 struct smbconf_service **tmp_services;
120 if ((num_shares == NULL) || (services == NULL)) {
121 werr = WERR_INVALID_PARAM;
125 tmp_ctx = talloc_stackframe();
127 werr = smbconf_get_share_names(ctx, tmp_ctx, &tmp_num_shares,
129 if (!W_ERROR_IS_OK(werr)) {
133 tmp_services = talloc_array(tmp_ctx, struct smbconf_service *,
136 if (tmp_services == NULL) {
141 for (count = 0; count < tmp_num_shares; count++) {
142 werr = smbconf_get_share(ctx, tmp_services,
143 tmp_share_names[count],
144 &tmp_services[count]);
145 if (!W_ERROR_IS_OK(werr)) {
152 *num_shares = tmp_num_shares;
153 if (tmp_num_shares > 0) {
154 *services = talloc_move(mem_ctx, &tmp_services);
160 talloc_free(tmp_ctx);
165 * get the list of share names defined in the configuration.
167 WERROR smbconf_get_share_names(struct smbconf_ctx *ctx,
169 uint32_t *num_shares,
172 return ctx->ops->get_share_names(ctx, mem_ctx, num_shares,
177 * check if a share/service of a given name exists
179 bool smbconf_share_exists(struct smbconf_ctx *ctx,
180 const char *servicename)
182 return ctx->ops->share_exists(ctx, servicename);
186 * Add a service if it does not already exist.
188 WERROR smbconf_create_share(struct smbconf_ctx *ctx,
189 const char *servicename)
191 if ((servicename != NULL) && smbconf_share_exists(ctx, servicename)) {
192 return WERR_FILE_EXISTS;
195 return ctx->ops->create_share(ctx, servicename);
199 * get a definition of a share (service) from configuration.
201 WERROR smbconf_get_share(struct smbconf_ctx *ctx,
203 const char *servicename,
204 struct smbconf_service **service)
206 return ctx->ops->get_share(ctx, mem_ctx, servicename, service);
210 * delete a service from configuration
212 WERROR smbconf_delete_share(struct smbconf_ctx *ctx, const char *servicename)
214 if (!smbconf_share_exists(ctx, servicename)) {
215 return WERR_NO_SUCH_SERVICE;
218 return ctx->ops->delete_share(ctx, servicename);
222 * set a configuration parameter to the value provided.
224 WERROR smbconf_set_parameter(struct smbconf_ctx *ctx,
229 if (!smbconf_share_exists(ctx, service)) {
230 return WERR_NO_SUCH_SERVICE;
233 return ctx->ops->set_parameter(ctx, service, param, valstr);
237 * Set a global parameter
238 * (i.e. a parameter in the [global] service).
240 * This also creates [global] when it does not exist.
242 WERROR smbconf_set_global_parameter(struct smbconf_ctx *ctx,
243 const char *param, const char *val)
247 werr = smbconf_global_check(ctx);
248 if (W_ERROR_IS_OK(werr)) {
249 werr = smbconf_set_parameter(ctx, GLOBAL_NAME, param, val);
256 * get the value of a configuration parameter as a string
258 WERROR smbconf_get_parameter(struct smbconf_ctx *ctx,
264 if (valstr == NULL) {
265 return WERR_INVALID_PARAM;
268 if (!smbconf_share_exists(ctx, service)) {
269 return WERR_NO_SUCH_SERVICE;
272 return ctx->ops->get_parameter(ctx, mem_ctx, service, param, valstr);
276 * Get the value of a global parameter.
278 * Create [global] if it does not exist.
280 WERROR smbconf_get_global_parameter(struct smbconf_ctx *ctx,
287 werr = smbconf_global_check(ctx);
288 if (W_ERROR_IS_OK(werr)) {
289 werr = smbconf_get_parameter(ctx, mem_ctx, GLOBAL_NAME, param,
297 * delete a parameter from configuration
299 WERROR smbconf_delete_parameter(struct smbconf_ctx *ctx,
300 const char *service, const char *param)
302 if (!smbconf_share_exists(ctx, service)) {
303 return WERR_NO_SUCH_SERVICE;
306 return ctx->ops->delete_parameter(ctx, service, param);
310 * Delete a global parameter.
312 * Create [global] if it does not exist.
314 WERROR smbconf_delete_global_parameter(struct smbconf_ctx *ctx,
319 werr = smbconf_global_check(ctx);
320 if (W_ERROR_IS_OK(werr)) {
321 werr = smbconf_delete_parameter(ctx, GLOBAL_NAME, param);
327 WERROR smbconf_get_includes(struct smbconf_ctx *ctx,
330 uint32_t *num_includes, char ***includes)
332 if (!smbconf_share_exists(ctx, service)) {
333 return WERR_NO_SUCH_SERVICE;
336 return ctx->ops->get_includes(ctx, mem_ctx, service, num_includes,
340 WERROR smbconf_get_global_includes(struct smbconf_ctx *ctx,
342 uint32_t *num_includes, char ***includes)
346 werr = smbconf_global_check(ctx);
347 if (W_ERROR_IS_OK(werr)) {
348 werr = smbconf_get_includes(ctx, mem_ctx, GLOBAL_NAME,
349 num_includes, includes);
355 WERROR smbconf_set_includes(struct smbconf_ctx *ctx,
357 uint32_t num_includes, const char **includes)
359 if (!smbconf_share_exists(ctx, service)) {
360 return WERR_NO_SUCH_SERVICE;
363 return ctx->ops->set_includes(ctx, service, num_includes, includes);
366 WERROR smbconf_set_global_includes(struct smbconf_ctx *ctx,
367 uint32_t num_includes,
368 const char **includes)
372 werr = smbconf_global_check(ctx);
373 if (W_ERROR_IS_OK(werr)) {
374 werr = smbconf_set_includes(ctx, GLOBAL_NAME,
375 num_includes, includes);
382 WERROR smbconf_delete_includes(struct smbconf_ctx *ctx, const char *service)
384 if (!smbconf_share_exists(ctx, service)) {
385 return WERR_NO_SUCH_SERVICE;
388 return ctx->ops->delete_includes(ctx, service);
391 WERROR smbconf_delete_global_includes(struct smbconf_ctx *ctx)
395 werr = smbconf_global_check(ctx);
396 if (W_ERROR_IS_OK(werr)) {
397 werr = smbconf_delete_includes(ctx, GLOBAL_NAME);
403 WERROR smbconf_transaction_start(struct smbconf_ctx *ctx)
405 return ctx->ops->transaction_start(ctx);
408 WERROR smbconf_transaction_commit(struct smbconf_ctx *ctx)
410 return ctx->ops->transaction_commit(ctx);
413 WERROR smbconf_transaction_cancel(struct smbconf_ctx *ctx)
415 return ctx->ops->transaction_cancel(ctx);