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 * Close the configuration.
48 void smbconf_shutdown(struct smbconf_ctx *ctx)
54 * Detect changes in the configuration.
55 * The given csn struct is filled with the current csn.
56 * smbconf_changed() can also be used for initial retrieval
59 bool smbconf_changed(struct smbconf_ctx *ctx, struct smbconf_csn *csn,
60 const char *service, const char *param)
62 struct smbconf_csn old_csn;
70 ctx->ops->get_csn(ctx, csn, service, param);
71 return (csn->csn != old_csn.csn);
75 * Drop the whole configuration (restarting empty).
77 WERROR smbconf_drop(struct smbconf_ctx *ctx)
79 return ctx->ops->drop(ctx);
83 * Get the whole configuration as lists of strings with counts:
85 * num_shares : number of shares
86 * share_names : list of length num_shares of share names
87 * num_params : list of length num_shares of parameter counts for each share
88 * param_names : list of lists of parameter names for each share
89 * param_values : list of lists of parameter values for each share
91 WERROR smbconf_get_config(struct smbconf_ctx *ctx,
94 struct smbconf_service ***services)
96 WERROR werr = WERR_OK;
97 TALLOC_CTX *tmp_ctx = NULL;
98 uint32_t tmp_num_shares;
99 char **tmp_share_names;
100 struct smbconf_service **tmp_services;
103 if ((num_shares == NULL) || (services == NULL)) {
104 werr = WERR_INVALID_PARAM;
108 tmp_ctx = talloc_stackframe();
110 werr = smbconf_get_share_names(ctx, tmp_ctx, &tmp_num_shares,
112 if (!W_ERROR_IS_OK(werr)) {
116 tmp_services = TALLOC_ARRAY(tmp_ctx, struct smbconf_service *,
119 if (tmp_services == NULL) {
124 for (count = 0; count < tmp_num_shares; count++) {
125 werr = smbconf_get_share(ctx, tmp_services,
126 tmp_share_names[count],
127 &tmp_services[count]);
128 if (!W_ERROR_IS_OK(werr)) {
135 *num_shares = tmp_num_shares;
136 if (tmp_num_shares > 0) {
137 *services = talloc_move(mem_ctx, &tmp_services);
143 TALLOC_FREE(tmp_ctx);
148 * get the list of share names defined in the configuration.
150 WERROR smbconf_get_share_names(struct smbconf_ctx *ctx,
152 uint32_t *num_shares,
155 return ctx->ops->get_share_names(ctx, mem_ctx, num_shares,
160 * check if a share/service of a given name exists
162 bool smbconf_share_exists(struct smbconf_ctx *ctx,
163 const char *servicename)
165 return ctx->ops->share_exists(ctx, servicename);
169 * Add a service if it does not already exist.
171 WERROR smbconf_create_share(struct smbconf_ctx *ctx,
172 const char *servicename)
174 if ((servicename != NULL) && smbconf_share_exists(ctx, servicename)) {
175 return WERR_ALREADY_EXISTS;
178 return ctx->ops->create_share(ctx, servicename);
182 * get a definition of a share (service) from configuration.
184 WERROR smbconf_get_share(struct smbconf_ctx *ctx,
186 const char *servicename,
187 struct smbconf_service **service)
189 if (!smbconf_share_exists(ctx, servicename)) {
190 return WERR_NO_SUCH_SERVICE;
193 return ctx->ops->get_share(ctx, mem_ctx, servicename, service);
197 * delete a service from configuration
199 WERROR smbconf_delete_share(struct smbconf_ctx *ctx, const char *servicename)
201 if (!smbconf_share_exists(ctx, servicename)) {
202 return WERR_NO_SUCH_SERVICE;
205 return ctx->ops->delete_share(ctx, servicename);
209 * set a configuration parameter to the value provided.
211 WERROR smbconf_set_parameter(struct smbconf_ctx *ctx,
216 if (!smbconf_share_exists(ctx, service)) {
217 return WERR_NO_SUCH_SERVICE;
220 return ctx->ops->set_parameter(ctx, service, param, valstr);
224 * Set a global parameter
225 * (i.e. a parameter in the [global] service).
227 * This also creates [global] when it does not exist.
229 WERROR smbconf_set_global_parameter(struct smbconf_ctx *ctx,
230 const char *param, const char *val)
234 werr = smbconf_global_check(ctx);
235 if (W_ERROR_IS_OK(werr)) {
236 werr = smbconf_set_parameter(ctx, GLOBAL_NAME, param, val);
243 * get the value of a configuration parameter as a string
245 WERROR smbconf_get_parameter(struct smbconf_ctx *ctx,
251 if (valstr == NULL) {
252 return WERR_INVALID_PARAM;
255 if (!smbconf_share_exists(ctx, service)) {
256 return WERR_NO_SUCH_SERVICE;
259 return ctx->ops->get_parameter(ctx, mem_ctx, service, param, valstr);
263 * Get the value of a global parameter.
265 * Create [global] if it does not exist.
267 WERROR smbconf_get_global_parameter(struct smbconf_ctx *ctx,
274 werr = smbconf_global_check(ctx);
275 if (W_ERROR_IS_OK(werr)) {
276 werr = smbconf_get_parameter(ctx, mem_ctx, GLOBAL_NAME, param,
284 * delete a parameter from configuration
286 WERROR smbconf_delete_parameter(struct smbconf_ctx *ctx,
287 const char *service, const char *param)
289 if (!smbconf_share_exists(ctx, service)) {
290 return WERR_NO_SUCH_SERVICE;
293 return ctx->ops->delete_parameter(ctx, service, param);
297 * Delete a global parameter.
299 * Create [global] if it does not exist.
301 WERROR smbconf_delete_global_parameter(struct smbconf_ctx *ctx,
306 werr = smbconf_global_check(ctx);
307 if (W_ERROR_IS_OK(werr)) {
308 werr = smbconf_delete_parameter(ctx, GLOBAL_NAME, param);
314 WERROR smbconf_get_includes(struct smbconf_ctx *ctx,
317 uint32_t *num_includes, char ***includes)
319 if (!smbconf_share_exists(ctx, service)) {
320 return WERR_NO_SUCH_SERVICE;
323 return ctx->ops->get_includes(ctx, mem_ctx, service, num_includes,
327 WERROR smbconf_get_global_includes(struct smbconf_ctx *ctx,
329 uint32_t *num_includes, char ***includes)
333 werr = smbconf_global_check(ctx);
334 if (W_ERROR_IS_OK(werr)) {
335 werr = smbconf_get_includes(ctx, mem_ctx, GLOBAL_NAME,
336 num_includes, includes);
342 WERROR smbconf_set_includes(struct smbconf_ctx *ctx,
344 uint32_t num_includes, const char **includes)
346 if (!smbconf_share_exists(ctx, service)) {
347 return WERR_NO_SUCH_SERVICE;
350 return ctx->ops->set_includes(ctx, service, num_includes, includes);
353 WERROR smbconf_set_global_includes(struct smbconf_ctx *ctx,
354 uint32_t num_includes,
355 const char **includes)
359 werr = smbconf_global_check(ctx);
360 if (W_ERROR_IS_OK(werr)) {
361 werr = smbconf_set_includes(ctx, GLOBAL_NAME,
362 num_includes, includes);
369 WERROR smbconf_delete_includes(struct smbconf_ctx *ctx, const char *service)
371 if (!smbconf_share_exists(ctx, service)) {
372 return WERR_NO_SUCH_SERVICE;
375 return ctx->ops->delete_includes(ctx, service);
378 WERROR smbconf_delete_global_includes(struct smbconf_ctx *ctx)
382 werr = smbconf_global_check(ctx);
383 if (W_ERROR_IS_OK(werr)) {
384 werr = smbconf_delete_includes(ctx, GLOBAL_NAME);