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 * Close the configuration.
57 void smbconf_shutdown(struct smbconf_ctx *ctx)
63 * Detect changes in the configuration.
64 * The given csn struct is filled with the current csn.
65 * smbconf_changed() can also be used for initial retrieval
68 bool smbconf_changed(struct smbconf_ctx *ctx, struct smbconf_csn *csn,
69 const char *service, const char *param)
71 struct smbconf_csn old_csn;
79 ctx->ops->get_csn(ctx, csn, service, param);
80 return (csn->csn != old_csn.csn);
84 * Drop the whole configuration (restarting empty).
86 WERROR smbconf_drop(struct smbconf_ctx *ctx)
88 return ctx->ops->drop(ctx);
92 * Get the whole configuration as lists of strings with counts:
94 * num_shares : number of shares
95 * share_names : list of length num_shares of share names
96 * num_params : list of length num_shares of parameter counts for each share
97 * param_names : list of lists of parameter names for each share
98 * param_values : list of lists of parameter values for each share
100 WERROR smbconf_get_config(struct smbconf_ctx *ctx,
102 uint32_t *num_shares,
103 struct smbconf_service ***services)
105 WERROR werr = WERR_OK;
106 TALLOC_CTX *tmp_ctx = NULL;
107 uint32_t tmp_num_shares;
108 char **tmp_share_names;
109 struct smbconf_service **tmp_services;
112 if ((num_shares == NULL) || (services == NULL)) {
113 werr = WERR_INVALID_PARAM;
117 tmp_ctx = talloc_stackframe();
119 werr = smbconf_get_share_names(ctx, tmp_ctx, &tmp_num_shares,
121 if (!W_ERROR_IS_OK(werr)) {
125 tmp_services = TALLOC_ARRAY(tmp_ctx, struct smbconf_service *,
128 if (tmp_services == NULL) {
133 for (count = 0; count < tmp_num_shares; count++) {
134 werr = smbconf_get_share(ctx, tmp_services,
135 tmp_share_names[count],
136 &tmp_services[count]);
137 if (!W_ERROR_IS_OK(werr)) {
144 *num_shares = tmp_num_shares;
145 if (tmp_num_shares > 0) {
146 *services = talloc_move(mem_ctx, &tmp_services);
152 TALLOC_FREE(tmp_ctx);
157 * get the list of share names defined in the configuration.
159 WERROR smbconf_get_share_names(struct smbconf_ctx *ctx,
161 uint32_t *num_shares,
164 return ctx->ops->get_share_names(ctx, mem_ctx, num_shares,
169 * check if a share/service of a given name exists
171 bool smbconf_share_exists(struct smbconf_ctx *ctx,
172 const char *servicename)
174 return ctx->ops->share_exists(ctx, servicename);
178 * Add a service if it does not already exist.
180 WERROR smbconf_create_share(struct smbconf_ctx *ctx,
181 const char *servicename)
183 if ((servicename != NULL) && smbconf_share_exists(ctx, servicename)) {
184 return WERR_FILE_EXISTS;
187 return ctx->ops->create_share(ctx, servicename);
191 * get a definition of a share (service) from configuration.
193 WERROR smbconf_get_share(struct smbconf_ctx *ctx,
195 const char *servicename,
196 struct smbconf_service **service)
198 if (!smbconf_share_exists(ctx, servicename)) {
199 return WERR_NO_SUCH_SERVICE;
202 return ctx->ops->get_share(ctx, mem_ctx, servicename, service);
206 * delete a service from configuration
208 WERROR smbconf_delete_share(struct smbconf_ctx *ctx, const char *servicename)
210 if (!smbconf_share_exists(ctx, servicename)) {
211 return WERR_NO_SUCH_SERVICE;
214 return ctx->ops->delete_share(ctx, servicename);
218 * set a configuration parameter to the value provided.
220 WERROR smbconf_set_parameter(struct smbconf_ctx *ctx,
225 if (!smbconf_share_exists(ctx, service)) {
226 return WERR_NO_SUCH_SERVICE;
229 return ctx->ops->set_parameter(ctx, service, param, valstr);
233 * Set a global parameter
234 * (i.e. a parameter in the [global] service).
236 * This also creates [global] when it does not exist.
238 WERROR smbconf_set_global_parameter(struct smbconf_ctx *ctx,
239 const char *param, const char *val)
243 werr = smbconf_global_check(ctx);
244 if (W_ERROR_IS_OK(werr)) {
245 werr = smbconf_set_parameter(ctx, GLOBAL_NAME, param, val);
252 * get the value of a configuration parameter as a string
254 WERROR smbconf_get_parameter(struct smbconf_ctx *ctx,
260 if (valstr == NULL) {
261 return WERR_INVALID_PARAM;
264 if (!smbconf_share_exists(ctx, service)) {
265 return WERR_NO_SUCH_SERVICE;
268 return ctx->ops->get_parameter(ctx, mem_ctx, service, param, valstr);
272 * Get the value of a global parameter.
274 * Create [global] if it does not exist.
276 WERROR smbconf_get_global_parameter(struct smbconf_ctx *ctx,
283 werr = smbconf_global_check(ctx);
284 if (W_ERROR_IS_OK(werr)) {
285 werr = smbconf_get_parameter(ctx, mem_ctx, GLOBAL_NAME, param,
293 * delete a parameter from configuration
295 WERROR smbconf_delete_parameter(struct smbconf_ctx *ctx,
296 const char *service, const char *param)
298 if (!smbconf_share_exists(ctx, service)) {
299 return WERR_NO_SUCH_SERVICE;
302 return ctx->ops->delete_parameter(ctx, service, param);
306 * Delete a global parameter.
308 * Create [global] if it does not exist.
310 WERROR smbconf_delete_global_parameter(struct smbconf_ctx *ctx,
315 werr = smbconf_global_check(ctx);
316 if (W_ERROR_IS_OK(werr)) {
317 werr = smbconf_delete_parameter(ctx, GLOBAL_NAME, param);
323 WERROR smbconf_get_includes(struct smbconf_ctx *ctx,
326 uint32_t *num_includes, char ***includes)
328 if (!smbconf_share_exists(ctx, service)) {
329 return WERR_NO_SUCH_SERVICE;
332 return ctx->ops->get_includes(ctx, mem_ctx, service, num_includes,
336 WERROR smbconf_get_global_includes(struct smbconf_ctx *ctx,
338 uint32_t *num_includes, char ***includes)
342 werr = smbconf_global_check(ctx);
343 if (W_ERROR_IS_OK(werr)) {
344 werr = smbconf_get_includes(ctx, mem_ctx, GLOBAL_NAME,
345 num_includes, includes);
351 WERROR smbconf_set_includes(struct smbconf_ctx *ctx,
353 uint32_t num_includes, const char **includes)
355 if (!smbconf_share_exists(ctx, service)) {
356 return WERR_NO_SUCH_SERVICE;
359 return ctx->ops->set_includes(ctx, service, num_includes, includes);
362 WERROR smbconf_set_global_includes(struct smbconf_ctx *ctx,
363 uint32_t num_includes,
364 const char **includes)
368 werr = smbconf_global_check(ctx);
369 if (W_ERROR_IS_OK(werr)) {
370 werr = smbconf_set_includes(ctx, GLOBAL_NAME,
371 num_includes, includes);
378 WERROR smbconf_delete_includes(struct smbconf_ctx *ctx, const char *service)
380 if (!smbconf_share_exists(ctx, service)) {
381 return WERR_NO_SUCH_SERVICE;
384 return ctx->ops->delete_includes(ctx, service);
387 WERROR smbconf_delete_global_includes(struct smbconf_ctx *ctx)
391 werr = smbconf_global_check(ctx);
392 if (W_ERROR_IS_OK(werr)) {
393 werr = smbconf_delete_includes(ctx, GLOBAL_NAME);