2 Unix SMB/CIFS implementation.
4 LDB based services configuration
6 Copyright (C) Simo Sorce 2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "ldb/include/ldb.h"
24 #include "ldb/include/ldb_errors.h"
25 #include "auth/auth.h"
27 #include "param/share.h"
29 static NTSTATUS sldb_init(TALLOC_CTX *mem_ctx, const struct share_ops *ops, struct share_context **ctx)
31 struct ldb_context *sdb;
33 *ctx = talloc(mem_ctx, struct share_context);
35 DEBUG(0, ("ERROR: Out of memory!\n"));
36 return NT_STATUS_NO_MEMORY;
39 sdb = ldb_wrap_connect( *ctx,
40 private_path(*ctx, "share.ldb"),
46 return NT_STATUS_UNSUCCESSFUL;
50 (*ctx)->priv_data = (void *)sdb;
55 static const char *sldb_string_option(struct share_config *scfg, const char *opt_name, const char *defval)
57 struct ldb_message *msg;
58 struct ldb_message_element *el;
60 if (scfg == NULL) return defval;
62 msg = talloc_get_type(scfg->opaque, struct ldb_message);
64 if (strchr(opt_name, ':')) {
67 name = talloc_strdup(scfg, opt_name);
71 p = strchr(name, ':');
74 el = ldb_msg_find_element(msg, name);
76 el = ldb_msg_find_element(msg, opt_name);
83 return (const char *)(el->values[0].data);
86 static int sldb_int_option(struct share_config *scfg, const char *opt_name, int defval)
91 val = sldb_string_option(scfg, opt_name, NULL);
92 if (val == NULL) return defval;
95 ret = (int)strtol(val, NULL, 10);
101 static BOOL sldb_bool_option(struct share_config *scfg, const char *opt_name, BOOL defval)
105 val = sldb_string_option(scfg, opt_name, NULL);
106 if (val == NULL) return defval;
108 if (strcasecmp(val, "true") == 0) return True;
113 static const char **sldb_string_list_option(TALLOC_CTX *mem_ctx, struct share_config *scfg, const char *opt_name)
115 struct ldb_message *msg;
116 struct ldb_message_element *el;
120 if (scfg == NULL) return NULL;
122 msg = talloc_get_type(scfg->opaque, struct ldb_message);
124 if (strchr(opt_name, ':')) {
127 name = talloc_strdup(scfg, opt_name);
131 p = strchr(name, ':');
134 el = ldb_msg_find_element(msg, name);
136 el = ldb_msg_find_element(msg, opt_name);
143 list = talloc_array(mem_ctx, const char *, el->num_values + 1);
144 if (!list) return NULL;
146 for (i = 0; i < el->num_values; i++) {
147 list[i] = (const char *)(el->values[i].data);
154 static NTSTATUS sldb_list_all(TALLOC_CTX *mem_ctx,
155 struct share_context *ctx,
161 struct ldb_context *ldb;
162 struct ldb_result *res;
165 tmp_ctx = talloc_new(mem_ctx);
167 DEBUG(0,("ERROR: Out of memory!\n"));
168 return NT_STATUS_NO_MEMORY;
171 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
173 ret = ldb_search(ldb, ldb_dn_new(tmp_ctx, ldb, "CN=SHARES"), LDB_SCOPE_SUBTREE, "(name=*)", NULL, &res);
174 talloc_steal(tmp_ctx, res);
175 if (ret != LDB_SUCCESS) {
176 talloc_free(tmp_ctx);
177 return NT_STATUS_INTERNAL_DB_CORRUPTION;
180 n = talloc_array(mem_ctx, const char *, res->count);
182 DEBUG(0,("ERROR: Out of memory!\n"));
183 talloc_free(tmp_ctx);
184 return NT_STATUS_NO_MEMORY;
187 for (i = 0, j = 0; i < res->count; i++) {
188 n[j] = talloc_strdup(n, ldb_msg_find_attr_as_string(res->msgs[i], "name", NULL));
190 DEBUG(0,("WARNING: Malformed share object in share database\n!"));
198 talloc_free(tmp_ctx);
203 static NTSTATUS sldb_get_config(TALLOC_CTX *mem_ctx,
204 struct share_context *ctx,
206 struct share_config **scfg)
209 struct share_config *s;
210 struct ldb_context *ldb;
211 struct ldb_result *res;
214 tmp_ctx = talloc_new(mem_ctx);
216 DEBUG(0,("ERROR: Out of memory!\n"));
217 return NT_STATUS_NO_MEMORY;
220 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
222 ret = ldb_search_exp_fmt(ldb, tmp_ctx, &res,
223 ldb_dn_new(tmp_ctx, ldb, "CN=SHARES"), LDB_SCOPE_SUBTREE, NULL,
225 if (ret != LDB_SUCCESS || res->count > 1) {
226 talloc_free(tmp_ctx);
227 return NT_STATUS_INTERNAL_DB_CORRUPTION;
228 } else if (res->count != 1) {
229 talloc_free(tmp_ctx);
230 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
233 s = talloc(tmp_ctx, struct share_config);
235 DEBUG(0,("ERROR: Out of memory!\n"));
236 talloc_free(tmp_ctx);
237 return NT_STATUS_NO_MEMORY;
240 s->name = talloc_strdup(s, ldb_msg_find_attr_as_string(res->msgs[0], "name", NULL));
242 DEBUG(0,("ERROR: Invalid share object!\n"));
243 talloc_free(tmp_ctx);
244 return NT_STATUS_UNSUCCESSFUL;
247 s->opaque = talloc_steal(s, res->msgs[0]);
249 DEBUG(0,("ERROR: Invalid share object!\n"));
250 talloc_free(tmp_ctx);
251 return NT_STATUS_UNSUCCESSFUL;
256 *scfg = talloc_steal(mem_ctx, s);
258 talloc_free(tmp_ctx);
262 #define SHARE_ADD_STRING(name, value) do { \
263 err = ldb_msg_add_string(msg, name, value); \
264 if (err != LDB_SUCCESS) { \
265 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
266 ret = NT_STATUS_UNSUCCESSFUL; \
270 #define SHARE_ADD_INT(name, value) do { \
271 err = ldb_msg_add_fmt(msg, name, "%d", value); \
272 if (err != LDB_SUCCESS) { \
273 DEBUG(2,("ERROR: unable to add integer share option %s to ldb msg\n", name)); \
274 ret = NT_STATUS_UNSUCCESSFUL; \
278 #define SHARE_ADD_BLOB(name, value) do { \
279 err = ldb_msg_add_value(msg, name, value, NULL); \
280 if (err != LDB_SUCCESS) { \
281 DEBUG(2,("ERROR: unable to add blob share option %s to ldb msg\n", name)); \
282 ret = NT_STATUS_UNSUCCESSFUL; \
286 NTSTATUS sldb_create(struct share_context *ctx, const char *name, struct share_info *info, int count)
288 struct ldb_context *ldb;
289 struct ldb_message *msg;
294 for (i = 0, j = 0; i < count && j != 0x03; i++) {
295 if (strcasecmp(info[i].name, SHARE_TYPE) == 0) j |= 0x02;
296 if (strcasecmp(info[i].name, SHARE_PATH) == 0) j |= 0x01;
297 if (strcasecmp(info[i].name, SHARE_NAME) == 0) {
298 if (strcasecmp(name, (char *)info[i].value) != 0) {
299 return NT_STATUS_INVALID_PARAMETER;
303 if (!name || j != 0x03) {
304 return NT_STATUS_INVALID_PARAMETER;
307 tmp_ctx = talloc_new(NULL);
309 DEBUG(0,("ERROR: Out of memory!\n"));
310 return NT_STATUS_NO_MEMORY;
313 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
315 msg = ldb_msg_new(tmp_ctx);
317 DEBUG(0,("ERROR: Out of memory!\n"));
318 ret = NT_STATUS_NO_MEMORY;
322 /* TODO: escape info->name */
323 msg->dn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", name);
325 DEBUG(0,("ERROR: Out of memory!\n"));
326 ret = NT_STATUS_NO_MEMORY;
330 SHARE_ADD_STRING("objectClass", "top");
331 SHARE_ADD_STRING("objectClass", "share");
332 SHARE_ADD_STRING("cn", name);
333 SHARE_ADD_STRING(SHARE_NAME, name);
335 for (i = 0; i < count; i++) {
336 if (strcasecmp(info[i].name, SHARE_NAME) == 0) continue;
338 switch (info[i].type) {
339 case SHARE_INFO_STRING:
340 SHARE_ADD_STRING(info[i].name, (char *)info[i].value);
343 SHARE_ADD_INT(info[i].name, *((int *)info[i].value));
345 case SHARE_INFO_BLOB:
346 SHARE_ADD_BLOB(info[i].name, (DATA_BLOB *)info[i].value);
349 DEBUG(2,("ERROR: Invalid share info type for %s\n", info[i].name));
350 ret = NT_STATUS_INVALID_PARAMETER;
355 /* TODO: Security Descriptor */
357 SHARE_ADD_STRING(SHARE_AVAILABLE, "True");
358 SHARE_ADD_STRING(SHARE_BROWSEABLE, "True");
359 SHARE_ADD_STRING(SHARE_READONLY, "False");
360 SHARE_ADD_STRING(SHARE_NTVFS_HANDLER, "unixuid");
361 SHARE_ADD_STRING(SHARE_NTVFS_HANDLER, "posix");
363 err = ldb_add(ldb, msg);
364 if (err != LDB_SUCCESS) {
365 DEBUG(2,("ERROR: unable to add share %s to share.ldb\n"
366 " err=%d [%s]\n", name, err, ldb_errstring(ldb)));
367 if (err == LDB_ERR_NO_SUCH_OBJECT) {
368 ret = NT_STATUS_OBJECT_NAME_NOT_FOUND;
369 } else if (err == LDB_ERR_ENTRY_ALREADY_EXISTS) {
370 ret = NT_STATUS_OBJECT_NAME_COLLISION;
372 ret = NT_STATUS_UNSUCCESSFUL;
379 talloc_free(tmp_ctx);
383 #define SHARE_MOD_STRING(name, value) do { \
384 err = ldb_msg_add_empty(msg, name, LDB_FLAG_MOD_REPLACE, NULL); \
385 if (err != LDB_SUCCESS) { \
386 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
387 ret = NT_STATUS_UNSUCCESSFUL; \
390 err = ldb_msg_add_string(msg, name, value); \
391 if (err != LDB_SUCCESS) { \
392 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
393 ret = NT_STATUS_UNSUCCESSFUL; \
397 #define SHARE_MOD_INT(name, value) do { \
398 err = ldb_msg_add_empty(msg, name, LDB_FLAG_MOD_REPLACE, NULL); \
399 if (err != LDB_SUCCESS) { \
400 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
401 ret = NT_STATUS_UNSUCCESSFUL; \
404 err = ldb_msg_add_fmt(msg, name, "%d", value); \
405 if (err != LDB_SUCCESS) { \
406 DEBUG(2,("ERROR: unable to add integer share option %s to ldb msg\n", name)); \
407 ret = NT_STATUS_UNSUCCESSFUL; \
411 #define SHARE_MOD_BLOB(name, value) do { \
412 err = ldb_msg_add_empty(msg, name, LDB_FLAG_MOD_REPLACE, NULL); \
413 if (err != LDB_SUCCESS) { \
414 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
415 ret = NT_STATUS_UNSUCCESSFUL; \
418 err = ldb_msg_add_value(msg, name, value, NULL); \
419 if (err != LDB_SUCCESS) { \
420 DEBUG(2,("ERROR: unable to add blob share option %s to ldb msg\n", name)); \
421 ret = NT_STATUS_UNSUCCESSFUL; \
425 NTSTATUS sldb_set(struct share_context *ctx, const char *name, struct share_info *info, int count)
427 struct ldb_context *ldb;
428 struct ldb_message *msg;
431 bool do_rename = False;
436 return NT_STATUS_INVALID_PARAMETER;
439 tmp_ctx = talloc_new(NULL);
441 DEBUG(0,("ERROR: Out of memory!\n"));
442 return NT_STATUS_NO_MEMORY;
445 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
447 msg = ldb_msg_new(tmp_ctx);
449 DEBUG(0,("ERROR: Out of memory!\n"));
450 ret = NT_STATUS_NO_MEMORY;
454 /* TODO: escape name */
455 msg->dn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", name);
457 DEBUG(0,("ERROR: Out of memory!\n"));
458 ret = NT_STATUS_NO_MEMORY;
462 for (i = 0; i < count; i++) {
463 if (strcasecmp(info[i].name, SHARE_NAME) == 0) {
464 if (strcasecmp(name, (char *)info[i].value) != 0) {
466 newname = (char *)info[i].value;
467 SHARE_MOD_STRING("cn", (char *)info[i].value);
471 switch (info[i].type) {
472 case SHARE_INFO_STRING:
473 SHARE_MOD_STRING(info[i].name, (char *)info[i].value);
476 SHARE_MOD_INT(info[i].name, *((int *)info[i].value));
478 case SHARE_INFO_BLOB:
479 SHARE_MOD_BLOB(info[i].name, (DATA_BLOB *)info[i].value);
482 DEBUG(2,("ERROR: Invalid share info type for %s\n", info[i].name));
483 ret = NT_STATUS_INVALID_PARAMETER;
489 struct ldb_dn *olddn, *newdn;
493 /* TODO: escape newname */
494 newdn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", newname);
496 DEBUG(0,("ERROR: Out of memory!\n"));
497 ret = NT_STATUS_NO_MEMORY;
501 err = ldb_rename(ldb, olddn, newdn);
502 if (err != LDB_SUCCESS) {
503 DEBUG(2,("ERROR: unable to rename share %s (to %s)\n"
504 " err=%d [%s]\n", name, newname, err, ldb_errstring(ldb)));
505 if (err == LDB_ERR_NO_SUCH_OBJECT) {
506 ret = NT_STATUS_OBJECT_NAME_COLLISION;
508 ret = NT_STATUS_UNSUCCESSFUL;
516 err = ldb_modify(ldb, msg);
517 if (err != LDB_SUCCESS) {
518 DEBUG(2,("ERROR: unable to add share %s to share.ldb\n"
519 " err=%d [%s]\n", name, err, ldb_errstring(ldb)));
520 if (err == LDB_ERR_NO_SUCH_OBJECT) {
521 ret = NT_STATUS_OBJECT_NAME_COLLISION;
523 ret = NT_STATUS_UNSUCCESSFUL;
530 talloc_free(tmp_ctx);
534 NTSTATUS sldb_remove(struct share_context *ctx, const char *name)
536 struct ldb_context *ldb;
542 tmp_ctx = talloc_new(NULL);
544 DEBUG(0,("ERROR: Out of memory!\n"));
545 return NT_STATUS_NO_MEMORY;
548 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
550 dn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", name);
552 DEBUG(0,("ERROR: Out of memory!\n"));
553 ret = NT_STATUS_NO_MEMORY;
557 err = ldb_delete(ldb, dn);
558 if (err != LDB_SUCCESS) {
559 DEBUG(2,("ERROR: unable to remove share %s from share.ldb\n"
560 " err=%d [%s]\n", name, err, ldb_errstring(ldb)));
561 ret = NT_STATUS_UNSUCCESSFUL;
567 talloc_free(tmp_ctx);
571 NTSTATUS share_ldb_init(void)
573 static struct share_ops ops = {
576 .string_option = sldb_string_option,
577 .int_option = sldb_int_option,
578 .bool_option = sldb_bool_option,
579 .string_list_option = sldb_string_list_option,
580 .list_all = sldb_list_all,
581 .get_config = sldb_get_config,
582 .create = sldb_create,
584 .remove = sldb_remove
587 return share_register(&ops);