libsmbconf: Convert smbconf_get_share_names() to sbcErr.
[Samba.git] / lib / smbconf / smbconf.c
blobf429295568f3da1e41d7d03cbf6e5524804ad750
1 /*
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/>.
21 #include "includes.h"
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);
35 return WERR_OK;
39 /**********************************************************************
41 * The actual libsmbconf API functions that are exported.
43 **********************************************************************/
45 const char *sbcErrorString(sbcErr error)
47 switch (error) {
48 case SBC_ERR_OK:
49 return "SBC_ERR_OK";
50 case SBC_ERR_NOT_IMPLEMENTED:
51 return "SBC_ERR_NOT_IMPLEMENTED";
52 case SBC_ERR_NOT_SUPPORTED:
53 return "SBC_ERR_NOT_SUPPORTED";
54 case SBC_ERR_UNKNOWN_FAILURE:
55 return "SBC_ERR_UNKNOWN_FAILURE";
56 case SBC_ERR_NOMEM:
57 return "SBC_ERR_NOMEM";
58 case SBC_ERR_INVALID_PARAM:
59 return "SBC_ERR_INVALID_PARAM";
60 case SBC_ERR_BADFILE:
61 return "SBC_ERR_BADFILE";
62 case SBC_ERR_NO_SUCH_SERVICE:
63 return "SBC_ERR_NO_SUCH_SERVICE";
64 case SBC_ERR_IO_FAILURE:
65 return "SBC_ERR_IO_FAILURE";
66 case SBC_ERR_CAN_NOT_COMPLETE:
67 return "SBC_ERR_CAN_NOT_COMPLETE";
68 case SBC_ERR_NO_MORE_ITEMS:
69 return "SBC_ERR_NO_MORE_ITEMS";
70 case SBC_ERR_FILE_EXISTS:
71 return "SBC_ERR_FILE_EXISTS";
72 case SBC_ERR_ACCESS_DENIED:
73 return "SBC_ERR_ACCESS_DENIED";
76 return "unknown sbcErr value";
80 /**
81 * Tell whether the backend requires messaging to be set up
82 * for the backend to work correctly.
84 bool smbconf_backend_requires_messaging(struct smbconf_ctx *ctx)
86 return ctx->ops->requires_messaging(ctx);
89 /**
90 * Tell whether the source is writeable.
92 bool smbconf_is_writeable(struct smbconf_ctx *ctx)
94 return ctx->ops->is_writeable(ctx);
97 /**
98 * Close the configuration.
100 void smbconf_shutdown(struct smbconf_ctx *ctx)
102 talloc_free(ctx);
106 * Detect changes in the configuration.
107 * The given csn struct is filled with the current csn.
108 * smbconf_changed() can also be used for initial retrieval
109 * of the csn.
111 bool smbconf_changed(struct smbconf_ctx *ctx, struct smbconf_csn *csn,
112 const char *service, const char *param)
114 struct smbconf_csn old_csn;
116 if (csn == NULL) {
117 return false;
120 old_csn = *csn;
122 ctx->ops->get_csn(ctx, csn, service, param);
123 return (csn->csn != old_csn.csn);
127 * Drop the whole configuration (restarting empty).
129 sbcErr smbconf_drop(struct smbconf_ctx *ctx)
131 return ctx->ops->drop(ctx);
135 * Get the whole configuration as lists of strings with counts:
137 * num_shares : number of shares
138 * share_names : list of length num_shares of share names
139 * num_params : list of length num_shares of parameter counts for each share
140 * param_names : list of lists of parameter names for each share
141 * param_values : list of lists of parameter values for each share
143 WERROR smbconf_get_config(struct smbconf_ctx *ctx,
144 TALLOC_CTX *mem_ctx,
145 uint32_t *num_shares,
146 struct smbconf_service ***services)
148 WERROR werr = WERR_OK;
149 sbcErr err;
150 TALLOC_CTX *tmp_ctx = NULL;
151 uint32_t tmp_num_shares;
152 char **tmp_share_names;
153 struct smbconf_service **tmp_services;
154 uint32_t count;
156 if ((num_shares == NULL) || (services == NULL)) {
157 werr = WERR_INVALID_PARAM;
158 goto done;
161 tmp_ctx = talloc_stackframe();
163 err = smbconf_get_share_names(ctx, tmp_ctx, &tmp_num_shares,
164 &tmp_share_names);
165 if (!SBC_ERROR_IS_OK(err)) {
166 werr = WERR_GENERAL_FAILURE;
167 goto done;
170 tmp_services = talloc_array(tmp_ctx, struct smbconf_service *,
171 tmp_num_shares);
173 if (tmp_services == NULL) {
174 werr = WERR_NOMEM;
175 goto done;
178 for (count = 0; count < tmp_num_shares; count++) {
179 werr = smbconf_get_share(ctx, tmp_services,
180 tmp_share_names[count],
181 &tmp_services[count]);
182 if (!W_ERROR_IS_OK(werr)) {
183 goto done;
187 werr = WERR_OK;
189 *num_shares = tmp_num_shares;
190 if (tmp_num_shares > 0) {
191 *services = talloc_move(mem_ctx, &tmp_services);
192 } else {
193 *services = NULL;
196 done:
197 talloc_free(tmp_ctx);
198 return werr;
202 * get the list of share names defined in the configuration.
204 sbcErr smbconf_get_share_names(struct smbconf_ctx *ctx,
205 TALLOC_CTX *mem_ctx,
206 uint32_t *num_shares,
207 char ***share_names)
209 return ctx->ops->get_share_names(ctx, mem_ctx, num_shares,
210 share_names);
214 * check if a share/service of a given name exists
216 bool smbconf_share_exists(struct smbconf_ctx *ctx,
217 const char *servicename)
219 return ctx->ops->share_exists(ctx, servicename);
223 * Add a service if it does not already exist.
225 WERROR smbconf_create_share(struct smbconf_ctx *ctx,
226 const char *servicename)
228 if ((servicename != NULL) && smbconf_share_exists(ctx, servicename)) {
229 return WERR_FILE_EXISTS;
232 return ctx->ops->create_share(ctx, servicename);
236 * get a definition of a share (service) from configuration.
238 WERROR smbconf_get_share(struct smbconf_ctx *ctx,
239 TALLOC_CTX *mem_ctx,
240 const char *servicename,
241 struct smbconf_service **service)
243 return ctx->ops->get_share(ctx, mem_ctx, servicename, service);
247 * delete a service from configuration
249 WERROR smbconf_delete_share(struct smbconf_ctx *ctx, const char *servicename)
251 if (!smbconf_share_exists(ctx, servicename)) {
252 return WERR_NO_SUCH_SERVICE;
255 return ctx->ops->delete_share(ctx, servicename);
259 * set a configuration parameter to the value provided.
261 WERROR smbconf_set_parameter(struct smbconf_ctx *ctx,
262 const char *service,
263 const char *param,
264 const char *valstr)
266 return ctx->ops->set_parameter(ctx, service, param, valstr);
270 * Set a global parameter
271 * (i.e. a parameter in the [global] service).
273 * This also creates [global] when it does not exist.
275 WERROR smbconf_set_global_parameter(struct smbconf_ctx *ctx,
276 const char *param, const char *val)
278 WERROR werr;
280 werr = smbconf_global_check(ctx);
281 if (W_ERROR_IS_OK(werr)) {
282 werr = smbconf_set_parameter(ctx, GLOBAL_NAME, param, val);
285 return werr;
289 * get the value of a configuration parameter as a string
291 WERROR smbconf_get_parameter(struct smbconf_ctx *ctx,
292 TALLOC_CTX *mem_ctx,
293 const char *service,
294 const char *param,
295 char **valstr)
297 if (valstr == NULL) {
298 return WERR_INVALID_PARAM;
301 return ctx->ops->get_parameter(ctx, mem_ctx, service, param, valstr);
305 * Get the value of a global parameter.
307 * Create [global] if it does not exist.
309 WERROR smbconf_get_global_parameter(struct smbconf_ctx *ctx,
310 TALLOC_CTX *mem_ctx,
311 const char *param,
312 char **valstr)
314 WERROR werr;
316 werr = smbconf_global_check(ctx);
317 if (W_ERROR_IS_OK(werr)) {
318 werr = smbconf_get_parameter(ctx, mem_ctx, GLOBAL_NAME, param,
319 valstr);
322 return werr;
326 * delete a parameter from configuration
328 WERROR smbconf_delete_parameter(struct smbconf_ctx *ctx,
329 const char *service, const char *param)
331 return ctx->ops->delete_parameter(ctx, service, param);
335 * Delete a global parameter.
337 * Create [global] if it does not exist.
339 WERROR smbconf_delete_global_parameter(struct smbconf_ctx *ctx,
340 const char *param)
342 WERROR werr;
344 werr = smbconf_global_check(ctx);
345 if (W_ERROR_IS_OK(werr)) {
346 werr = smbconf_delete_parameter(ctx, GLOBAL_NAME, param);
349 return werr;
352 WERROR smbconf_get_includes(struct smbconf_ctx *ctx,
353 TALLOC_CTX *mem_ctx,
354 const char *service,
355 uint32_t *num_includes, char ***includes)
357 return ctx->ops->get_includes(ctx, mem_ctx, service, num_includes,
358 includes);
361 WERROR smbconf_get_global_includes(struct smbconf_ctx *ctx,
362 TALLOC_CTX *mem_ctx,
363 uint32_t *num_includes, char ***includes)
365 WERROR werr;
367 werr = smbconf_global_check(ctx);
368 if (W_ERROR_IS_OK(werr)) {
369 werr = smbconf_get_includes(ctx, mem_ctx, GLOBAL_NAME,
370 num_includes, includes);
373 return werr;
376 WERROR smbconf_set_includes(struct smbconf_ctx *ctx,
377 const char *service,
378 uint32_t num_includes, const char **includes)
380 return ctx->ops->set_includes(ctx, service, num_includes, includes);
383 WERROR smbconf_set_global_includes(struct smbconf_ctx *ctx,
384 uint32_t num_includes,
385 const char **includes)
387 WERROR werr;
389 werr = smbconf_global_check(ctx);
390 if (W_ERROR_IS_OK(werr)) {
391 werr = smbconf_set_includes(ctx, GLOBAL_NAME,
392 num_includes, includes);
395 return werr;
399 WERROR smbconf_delete_includes(struct smbconf_ctx *ctx, const char *service)
401 return ctx->ops->delete_includes(ctx, service);
404 WERROR smbconf_delete_global_includes(struct smbconf_ctx *ctx)
406 WERROR werr;
408 werr = smbconf_global_check(ctx);
409 if (W_ERROR_IS_OK(werr)) {
410 werr = smbconf_delete_includes(ctx, GLOBAL_NAME);
413 return werr;
416 WERROR smbconf_transaction_start(struct smbconf_ctx *ctx)
418 return ctx->ops->transaction_start(ctx);
421 WERROR smbconf_transaction_commit(struct smbconf_ctx *ctx)
423 return ctx->ops->transaction_commit(ctx);
426 WERROR smbconf_transaction_cancel(struct smbconf_ctx *ctx)
428 return ctx->ops->transaction_cancel(ctx);