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 **********************************************************************/
45 const char *sbcErrorString(sbcErr error
)
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";
57 return "SBC_ERR_NOMEM";
58 case SBC_ERR_INVALID_PARAM
:
59 return "SBC_ERR_INVALID_PARAM";
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";
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
);
90 * Tell whether the source is writeable.
92 bool smbconf_is_writeable(struct smbconf_ctx
*ctx
)
94 return ctx
->ops
->is_writeable(ctx
);
98 * Close the configuration.
100 void smbconf_shutdown(struct smbconf_ctx
*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
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
;
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
,
145 uint32_t *num_shares
,
146 struct smbconf_service
***services
)
148 WERROR werr
= WERR_OK
;
150 TALLOC_CTX
*tmp_ctx
= NULL
;
151 uint32_t tmp_num_shares
;
152 char **tmp_share_names
;
153 struct smbconf_service
**tmp_services
;
156 if ((num_shares
== NULL
) || (services
== NULL
)) {
157 werr
= WERR_INVALID_PARAM
;
161 tmp_ctx
= talloc_stackframe();
163 err
= smbconf_get_share_names(ctx
, tmp_ctx
, &tmp_num_shares
,
165 if (!SBC_ERROR_IS_OK(err
)) {
166 werr
= WERR_GENERAL_FAILURE
;
170 tmp_services
= talloc_array(tmp_ctx
, struct smbconf_service
*,
173 if (tmp_services
== NULL
) {
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
)) {
189 *num_shares
= tmp_num_shares
;
190 if (tmp_num_shares
> 0) {
191 *services
= talloc_move(mem_ctx
, &tmp_services
);
197 talloc_free(tmp_ctx
);
202 * get the list of share names defined in the configuration.
204 sbcErr
smbconf_get_share_names(struct smbconf_ctx
*ctx
,
206 uint32_t *num_shares
,
209 return ctx
->ops
->get_share_names(ctx
, mem_ctx
, num_shares
,
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
,
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
,
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
)
280 werr
= smbconf_global_check(ctx
);
281 if (W_ERROR_IS_OK(werr
)) {
282 werr
= smbconf_set_parameter(ctx
, GLOBAL_NAME
, param
, val
);
289 * get the value of a configuration parameter as a string
291 WERROR
smbconf_get_parameter(struct smbconf_ctx
*ctx
,
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
,
316 werr
= smbconf_global_check(ctx
);
317 if (W_ERROR_IS_OK(werr
)) {
318 werr
= smbconf_get_parameter(ctx
, mem_ctx
, GLOBAL_NAME
, param
,
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
,
344 werr
= smbconf_global_check(ctx
);
345 if (W_ERROR_IS_OK(werr
)) {
346 werr
= smbconf_delete_parameter(ctx
, GLOBAL_NAME
, param
);
352 WERROR
smbconf_get_includes(struct smbconf_ctx
*ctx
,
355 uint32_t *num_includes
, char ***includes
)
357 return ctx
->ops
->get_includes(ctx
, mem_ctx
, service
, num_includes
,
361 WERROR
smbconf_get_global_includes(struct smbconf_ctx
*ctx
,
363 uint32_t *num_includes
, char ***includes
)
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
);
376 WERROR
smbconf_set_includes(struct smbconf_ctx
*ctx
,
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
)
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
);
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
)
408 werr
= smbconf_global_check(ctx
);
409 if (W_ERROR_IS_OK(werr
)) {
410 werr
= smbconf_delete_includes(ctx
, GLOBAL_NAME
);
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
);