libsmbclient: Fix typos
[Samba/id10ts.git] / lib / smbconf / smbconf.c
blob27d36cac0e543ae13830033118bcab703e6d2b53
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 sbcErr smbconf_global_check(struct smbconf_ctx *ctx)
32 if (!smbconf_share_exists(ctx, GLOBAL_NAME)) {
33 return smbconf_create_share(ctx, GLOBAL_NAME);
36 return SBC_ERR_OK;
40 /**********************************************************************
42 * The actual libsmbconf API functions that are exported.
44 **********************************************************************/
46 const char *sbcErrorString(sbcErr error)
48 switch (error) {
49 case SBC_ERR_OK:
50 return "SBC_ERR_OK";
51 case SBC_ERR_NOT_IMPLEMENTED:
52 return "SBC_ERR_NOT_IMPLEMENTED";
53 case SBC_ERR_NOT_SUPPORTED:
54 return "SBC_ERR_NOT_SUPPORTED";
55 case SBC_ERR_UNKNOWN_FAILURE:
56 return "SBC_ERR_UNKNOWN_FAILURE";
57 case SBC_ERR_NOMEM:
58 return "SBC_ERR_NOMEM";
59 case SBC_ERR_INVALID_PARAM:
60 return "SBC_ERR_INVALID_PARAM";
61 case SBC_ERR_BADFILE:
62 return "SBC_ERR_BADFILE";
63 case SBC_ERR_NO_SUCH_SERVICE:
64 return "SBC_ERR_NO_SUCH_SERVICE";
65 case SBC_ERR_IO_FAILURE:
66 return "SBC_ERR_IO_FAILURE";
67 case SBC_ERR_CAN_NOT_COMPLETE:
68 return "SBC_ERR_CAN_NOT_COMPLETE";
69 case SBC_ERR_NO_MORE_ITEMS:
70 return "SBC_ERR_NO_MORE_ITEMS";
71 case SBC_ERR_FILE_EXISTS:
72 return "SBC_ERR_FILE_EXISTS";
73 case SBC_ERR_ACCESS_DENIED:
74 return "SBC_ERR_ACCESS_DENIED";
77 return "unknown sbcErr value";
81 /**
82 * Tell whether the backend requires messaging to be set up
83 * for the backend to work correctly.
85 bool smbconf_backend_requires_messaging(struct smbconf_ctx *ctx)
87 return ctx->ops->requires_messaging(ctx);
90 /**
91 * Tell whether the source is writeable.
93 bool smbconf_is_writeable(struct smbconf_ctx *ctx)
95 return ctx->ops->is_writeable(ctx);
98 /**
99 * Close the configuration.
101 void smbconf_shutdown(struct smbconf_ctx *ctx)
103 talloc_free(ctx);
107 * Detect changes in the configuration.
108 * The given csn struct is filled with the current csn.
109 * smbconf_changed() can also be used for initial retrieval
110 * of the csn.
112 bool smbconf_changed(struct smbconf_ctx *ctx, struct smbconf_csn *csn,
113 const char *service, const char *param)
115 struct smbconf_csn old_csn;
117 if (csn == NULL) {
118 return false;
121 old_csn = *csn;
123 ctx->ops->get_csn(ctx, csn, service, param);
124 return (csn->csn != old_csn.csn);
128 * Drop the whole configuration (restarting empty).
130 sbcErr smbconf_drop(struct smbconf_ctx *ctx)
132 return ctx->ops->drop(ctx);
136 * Get the whole configuration as lists of strings with counts:
138 * num_shares : number of shares
139 * share_names : list of length num_shares of share names
140 * num_params : list of length num_shares of parameter counts for each share
141 * param_names : list of lists of parameter names for each share
142 * param_values : list of lists of parameter values for each share
144 sbcErr smbconf_get_config(struct smbconf_ctx *ctx,
145 TALLOC_CTX *mem_ctx,
146 uint32_t *num_shares,
147 struct smbconf_service ***services)
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 err = SBC_ERR_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 goto done;
169 tmp_services = talloc_array(tmp_ctx, struct smbconf_service *,
170 tmp_num_shares);
171 if (tmp_services == NULL) {
172 err = SBC_ERR_NOMEM;
173 goto done;
176 for (count = 0; count < tmp_num_shares; count++) {
177 err = smbconf_get_share(ctx, tmp_services,
178 tmp_share_names[count],
179 &tmp_services[count]);
180 if (!SBC_ERROR_IS_OK(err)) {
181 goto done;
185 err = SBC_ERR_OK;
187 *num_shares = tmp_num_shares;
188 if (tmp_num_shares > 0) {
189 *services = talloc_move(mem_ctx, &tmp_services);
190 } else {
191 *services = NULL;
194 done:
195 talloc_free(tmp_ctx);
196 return err;
200 * get the list of share names defined in the configuration.
202 sbcErr smbconf_get_share_names(struct smbconf_ctx *ctx,
203 TALLOC_CTX *mem_ctx,
204 uint32_t *num_shares,
205 char ***share_names)
207 return ctx->ops->get_share_names(ctx, mem_ctx, num_shares,
208 share_names);
212 * check if a share/service of a given name exists
214 bool smbconf_share_exists(struct smbconf_ctx *ctx,
215 const char *servicename)
217 return ctx->ops->share_exists(ctx, servicename);
221 * Add a service if it does not already exist.
223 sbcErr smbconf_create_share(struct smbconf_ctx *ctx,
224 const char *servicename)
226 if ((servicename != NULL) && smbconf_share_exists(ctx, servicename)) {
227 return SBC_ERR_FILE_EXISTS;
230 return ctx->ops->create_share(ctx, servicename);
234 * create and set the definition for a new share (service).
236 sbcErr smbconf_create_set_share(struct smbconf_ctx *ctx,
237 struct smbconf_service *service)
239 sbcErr err, err2;
240 int i;
241 uint32_t num_includes = 0;
242 char **includes = NULL;
243 TALLOC_CTX *tmp_ctx = NULL;
245 if ((service->name != NULL) && smbconf_share_exists(ctx, service->name))
247 return SBC_ERR_FILE_EXISTS;
250 err = smbconf_transaction_start(ctx);
251 if (!SBC_ERROR_IS_OK(err)) {
252 return err;
255 tmp_ctx = talloc_stackframe();
257 err = smbconf_create_share(ctx, service->name);
258 if (!SBC_ERROR_IS_OK(err)) {
259 goto cancel;
262 for (i = 0; i < service->num_params; i++) {
263 if (strequal(service->param_names[i], "include")) {
264 includes = talloc_realloc(tmp_ctx, includes, char *,
265 num_includes+1);
266 if (includes == NULL) {
267 err = SBC_ERR_NOMEM;
268 goto cancel;
270 includes[num_includes] = talloc_strdup(includes,
271 service->param_values[i]);
272 if (includes[num_includes] == NULL) {
273 err = SBC_ERR_NOMEM;
274 goto cancel;
276 num_includes++;
277 } else {
278 err = smbconf_set_parameter(ctx,
279 service->name,
280 service->param_names[i],
281 service->param_values[i]);
282 if (!SBC_ERROR_IS_OK(err)) {
283 goto cancel;
288 err = smbconf_set_includes(ctx, service->name, num_includes,
289 (const char **)includes);
290 if (!SBC_ERROR_IS_OK(err)) {
291 goto cancel;
294 err = smbconf_transaction_commit(ctx);
296 goto done;
298 cancel:
299 err2 = smbconf_transaction_cancel(ctx);
300 if (!SBC_ERROR_IS_OK(err2)) {
301 DEBUG(5, (__location__ ": Error cancelling transaction: %s\n",
302 sbcErrorString(err2)));
305 done:
306 talloc_free(tmp_ctx);
307 return err;
311 * get a definition of a share (service) from configuration.
313 sbcErr smbconf_get_share(struct smbconf_ctx *ctx,
314 TALLOC_CTX *mem_ctx,
315 const char *servicename,
316 struct smbconf_service **service)
318 return ctx->ops->get_share(ctx, mem_ctx, servicename, service);
322 * delete a service from configuration
324 sbcErr smbconf_delete_share(struct smbconf_ctx *ctx, const char *servicename)
326 if (!smbconf_share_exists(ctx, servicename)) {
327 return SBC_ERR_NO_SUCH_SERVICE;
330 return ctx->ops->delete_share(ctx, servicename);
334 * set a configuration parameter to the value provided.
336 sbcErr smbconf_set_parameter(struct smbconf_ctx *ctx,
337 const char *service,
338 const char *param,
339 const char *valstr)
341 return ctx->ops->set_parameter(ctx, service, param, valstr);
345 * Set a global parameter
346 * (i.e. a parameter in the [global] service).
348 * This also creates [global] when it does not exist.
350 sbcErr smbconf_set_global_parameter(struct smbconf_ctx *ctx,
351 const char *param, const char *val)
353 sbcErr err;
355 err = smbconf_global_check(ctx);
356 if (!SBC_ERROR_IS_OK(err)) {
357 return err;
359 err = smbconf_set_parameter(ctx, GLOBAL_NAME, param, val);
361 return err;
365 * get the value of a configuration parameter as a string
367 sbcErr smbconf_get_parameter(struct smbconf_ctx *ctx,
368 TALLOC_CTX *mem_ctx,
369 const char *service,
370 const char *param,
371 char **valstr)
373 if (valstr == NULL) {
374 return SBC_ERR_INVALID_PARAM;
377 return ctx->ops->get_parameter(ctx, mem_ctx, service, param, valstr);
381 * Get the value of a global parameter.
383 * Create [global] if it does not exist.
385 sbcErr smbconf_get_global_parameter(struct smbconf_ctx *ctx,
386 TALLOC_CTX *mem_ctx,
387 const char *param,
388 char **valstr)
390 sbcErr err;
392 err = smbconf_global_check(ctx);
393 if (!SBC_ERROR_IS_OK(err)) {
394 return err;
397 err = smbconf_get_parameter(ctx, mem_ctx, GLOBAL_NAME, param,
398 valstr);
400 return err;
404 * delete a parameter from configuration
406 sbcErr smbconf_delete_parameter(struct smbconf_ctx *ctx,
407 const char *service, const char *param)
409 return ctx->ops->delete_parameter(ctx, service, param);
413 * Delete a global parameter.
415 * Create [global] if it does not exist.
417 sbcErr smbconf_delete_global_parameter(struct smbconf_ctx *ctx,
418 const char *param)
420 sbcErr err;
422 err = smbconf_global_check(ctx);
423 if (!SBC_ERROR_IS_OK(err)) {
424 return err;
426 err = smbconf_delete_parameter(ctx, GLOBAL_NAME, param);
428 return err;
431 sbcErr smbconf_get_includes(struct smbconf_ctx *ctx,
432 TALLOC_CTX *mem_ctx,
433 const char *service,
434 uint32_t *num_includes, char ***includes)
436 return ctx->ops->get_includes(ctx, mem_ctx, service, num_includes,
437 includes);
440 sbcErr smbconf_get_global_includes(struct smbconf_ctx *ctx,
441 TALLOC_CTX *mem_ctx,
442 uint32_t *num_includes, char ***includes)
444 sbcErr err;
446 err = smbconf_global_check(ctx);
447 if (!SBC_ERROR_IS_OK(err)) {
448 return err;
450 err = smbconf_get_includes(ctx, mem_ctx, GLOBAL_NAME,
451 num_includes, includes);
453 return err;
456 sbcErr smbconf_set_includes(struct smbconf_ctx *ctx,
457 const char *service,
458 uint32_t num_includes, const char **includes)
460 return ctx->ops->set_includes(ctx, service, num_includes, includes);
463 sbcErr smbconf_set_global_includes(struct smbconf_ctx *ctx,
464 uint32_t num_includes,
465 const char **includes)
467 sbcErr err;
469 err = smbconf_global_check(ctx);
470 if (!SBC_ERROR_IS_OK(err)) {
471 return err;
473 err = smbconf_set_includes(ctx, GLOBAL_NAME,
474 num_includes, includes);
476 return err;
480 sbcErr smbconf_delete_includes(struct smbconf_ctx *ctx, const char *service)
482 return ctx->ops->delete_includes(ctx, service);
485 sbcErr smbconf_delete_global_includes(struct smbconf_ctx *ctx)
487 sbcErr err;
489 err = smbconf_global_check(ctx);
490 if (!SBC_ERROR_IS_OK(err)) {
491 return err;
493 err = smbconf_delete_includes(ctx, GLOBAL_NAME);
495 return err;
498 sbcErr smbconf_transaction_start(struct smbconf_ctx *ctx)
500 return ctx->ops->transaction_start(ctx);
503 sbcErr smbconf_transaction_commit(struct smbconf_ctx *ctx)
505 return ctx->ops->transaction_commit(ctx);
508 sbcErr smbconf_transaction_cancel(struct smbconf_ctx *ctx)
510 return ctx->ops->transaction_cancel(ctx);