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 sbcErr
smbconf_global_check(struct smbconf_ctx
*ctx
)
32 if (!smbconf_share_exists(ctx
, GLOBAL_NAME
)) {
33 return smbconf_create_share(ctx
, GLOBAL_NAME
);
40 /**********************************************************************
42 * The actual libsmbconf API functions that are exported.
44 **********************************************************************/
46 const char *sbcErrorString(sbcErr error
)
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";
58 return "SBC_ERR_NOMEM";
59 case SBC_ERR_INVALID_PARAM
:
60 return "SBC_ERR_INVALID_PARAM";
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";
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
);
91 * Tell whether the source is writeable.
93 bool smbconf_is_writeable(struct smbconf_ctx
*ctx
)
95 return ctx
->ops
->is_writeable(ctx
);
99 * Close the configuration.
101 void smbconf_shutdown(struct smbconf_ctx
*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
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
;
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 WERROR
smbconf_get_config(struct smbconf_ctx
*ctx
,
146 uint32_t *num_shares
,
147 struct smbconf_service
***services
)
149 WERROR werr
= WERR_OK
;
151 TALLOC_CTX
*tmp_ctx
= NULL
;
152 uint32_t tmp_num_shares
;
153 char **tmp_share_names
;
154 struct smbconf_service
**tmp_services
;
157 if ((num_shares
== NULL
) || (services
== NULL
)) {
158 werr
= WERR_INVALID_PARAM
;
162 tmp_ctx
= talloc_stackframe();
164 err
= smbconf_get_share_names(ctx
, tmp_ctx
, &tmp_num_shares
,
166 if (!SBC_ERROR_IS_OK(err
)) {
167 werr
= WERR_GENERAL_FAILURE
;
171 tmp_services
= talloc_array(tmp_ctx
, struct smbconf_service
*,
174 if (tmp_services
== NULL
) {
179 for (count
= 0; count
< tmp_num_shares
; count
++) {
180 err
= smbconf_get_share(ctx
, tmp_services
,
181 tmp_share_names
[count
],
182 &tmp_services
[count
]);
183 if (!SBC_ERROR_IS_OK(err
)) {
184 werr
= WERR_GENERAL_FAILURE
;
191 *num_shares
= tmp_num_shares
;
192 if (tmp_num_shares
> 0) {
193 *services
= talloc_move(mem_ctx
, &tmp_services
);
199 talloc_free(tmp_ctx
);
204 * get the list of share names defined in the configuration.
206 sbcErr
smbconf_get_share_names(struct smbconf_ctx
*ctx
,
208 uint32_t *num_shares
,
211 return ctx
->ops
->get_share_names(ctx
, mem_ctx
, num_shares
,
216 * check if a share/service of a given name exists
218 bool smbconf_share_exists(struct smbconf_ctx
*ctx
,
219 const char *servicename
)
221 return ctx
->ops
->share_exists(ctx
, servicename
);
225 * Add a service if it does not already exist.
227 sbcErr
smbconf_create_share(struct smbconf_ctx
*ctx
,
228 const char *servicename
)
230 if ((servicename
!= NULL
) && smbconf_share_exists(ctx
, servicename
)) {
231 return SBC_ERR_FILE_EXISTS
;
234 return ctx
->ops
->create_share(ctx
, servicename
);
238 * get a definition of a share (service) from configuration.
240 sbcErr
smbconf_get_share(struct smbconf_ctx
*ctx
,
242 const char *servicename
,
243 struct smbconf_service
**service
)
245 return ctx
->ops
->get_share(ctx
, mem_ctx
, servicename
, service
);
249 * delete a service from configuration
251 sbcErr
smbconf_delete_share(struct smbconf_ctx
*ctx
, const char *servicename
)
253 if (!smbconf_share_exists(ctx
, servicename
)) {
254 return SBC_ERR_NO_SUCH_SERVICE
;
257 return ctx
->ops
->delete_share(ctx
, servicename
);
261 * set a configuration parameter to the value provided.
263 sbcErr
smbconf_set_parameter(struct smbconf_ctx
*ctx
,
268 return ctx
->ops
->set_parameter(ctx
, service
, param
, valstr
);
272 * Set a global parameter
273 * (i.e. a parameter in the [global] service).
275 * This also creates [global] when it does not exist.
277 sbcErr
smbconf_set_global_parameter(struct smbconf_ctx
*ctx
,
278 const char *param
, const char *val
)
282 err
= smbconf_global_check(ctx
);
283 if (!SBC_ERROR_IS_OK(err
)) {
286 err
= smbconf_set_parameter(ctx
, GLOBAL_NAME
, param
, val
);
292 * get the value of a configuration parameter as a string
294 sbcErr
smbconf_get_parameter(struct smbconf_ctx
*ctx
,
300 if (valstr
== NULL
) {
301 return SBC_ERR_INVALID_PARAM
;
304 return ctx
->ops
->get_parameter(ctx
, mem_ctx
, service
, param
, valstr
);
308 * Get the value of a global parameter.
310 * Create [global] if it does not exist.
312 sbcErr
smbconf_get_global_parameter(struct smbconf_ctx
*ctx
,
319 err
= smbconf_global_check(ctx
);
320 if (!SBC_ERROR_IS_OK(err
)) {
324 err
= smbconf_get_parameter(ctx
, mem_ctx
, GLOBAL_NAME
, param
,
331 * delete a parameter from configuration
333 sbcErr
smbconf_delete_parameter(struct smbconf_ctx
*ctx
,
334 const char *service
, const char *param
)
336 return ctx
->ops
->delete_parameter(ctx
, service
, param
);
340 * Delete a global parameter.
342 * Create [global] if it does not exist.
344 sbcErr
smbconf_delete_global_parameter(struct smbconf_ctx
*ctx
,
349 err
= smbconf_global_check(ctx
);
350 if (!SBC_ERROR_IS_OK(err
)) {
353 err
= smbconf_delete_parameter(ctx
, GLOBAL_NAME
, param
);
358 sbcErr
smbconf_get_includes(struct smbconf_ctx
*ctx
,
361 uint32_t *num_includes
, char ***includes
)
363 return ctx
->ops
->get_includes(ctx
, mem_ctx
, service
, num_includes
,
367 sbcErr
smbconf_get_global_includes(struct smbconf_ctx
*ctx
,
369 uint32_t *num_includes
, char ***includes
)
373 err
= smbconf_global_check(ctx
);
374 if (!SBC_ERROR_IS_OK(err
)) {
377 err
= smbconf_get_includes(ctx
, mem_ctx
, GLOBAL_NAME
,
378 num_includes
, includes
);
383 sbcErr
smbconf_set_includes(struct smbconf_ctx
*ctx
,
385 uint32_t num_includes
, const char **includes
)
387 return ctx
->ops
->set_includes(ctx
, service
, num_includes
, includes
);
390 sbcErr
smbconf_set_global_includes(struct smbconf_ctx
*ctx
,
391 uint32_t num_includes
,
392 const char **includes
)
396 err
= smbconf_global_check(ctx
);
397 if (!SBC_ERROR_IS_OK(err
)) {
400 err
= smbconf_set_includes(ctx
, GLOBAL_NAME
,
401 num_includes
, includes
);
407 sbcErr
smbconf_delete_includes(struct smbconf_ctx
*ctx
, const char *service
)
409 return ctx
->ops
->delete_includes(ctx
, service
);
412 sbcErr
smbconf_delete_global_includes(struct smbconf_ctx
*ctx
)
416 err
= smbconf_global_check(ctx
);
417 if (!SBC_ERROR_IS_OK(err
)) {
420 err
= smbconf_delete_includes(ctx
, GLOBAL_NAME
);
425 sbcErr
smbconf_transaction_start(struct smbconf_ctx
*ctx
)
427 return ctx
->ops
->transaction_start(ctx
);
430 sbcErr
smbconf_transaction_commit(struct smbconf_ctx
*ctx
)
432 return ctx
->ops
->transaction_commit(ctx
);
435 sbcErr
smbconf_transaction_cancel(struct smbconf_ctx
*ctx
)
437 return ctx
->ops
->transaction_cancel(ctx
);