ctdb-tests: Move die() function to top of script
[Samba.git] / source4 / param / share_ldb.c
blobf8b1a3f5d977eb8b1d1f6bfe3d8ddb9bd5de0192
1 /*
2 Unix SMB/CIFS implementation.
4 LDB based shares 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/>.
22 #include "includes.h"
23 #include <ldb.h>
24 #include <ldb_errors.h>
25 #include "auth/auth.h"
26 #include "ldb_wrap.h"
27 #include "param/share.h"
28 #include "param/param.h"
30 NTSTATUS share_ldb_init(TALLOC_CTX *);
32 static NTSTATUS sldb_init(TALLOC_CTX *mem_ctx, const struct share_ops *ops,
33 struct tevent_context *ev_ctx,
34 struct loadparm_context *lp_ctx,
35 struct share_context **ctx)
37 struct ldb_context *sdb;
39 *ctx = talloc(mem_ctx, struct share_context);
40 if (!*ctx) {
41 DEBUG(0, ("ERROR: Out of memory!\n"));
42 return NT_STATUS_NO_MEMORY;
45 sdb = ldb_wrap_connect(*ctx, ev_ctx, lp_ctx,
46 lpcfg_private_path(*ctx, lp_ctx, "share.ldb"),
47 system_session(lp_ctx),
48 NULL, 0);
50 if (!sdb) {
51 talloc_free(*ctx);
52 return NT_STATUS_UNSUCCESSFUL;
55 (*ctx)->ops = ops;
56 (*ctx)->priv_data = (void *)sdb;
58 return NT_STATUS_OK;
61 static char *sldb_string_option(TALLOC_CTX *mem_ctx, struct share_config *scfg, const char *opt_name, const char *defval)
63 struct ldb_message *msg;
64 struct ldb_message_element *el;
65 const char *colon;
67 if (scfg == NULL) return talloc_strdup(mem_ctx, defval);
69 msg = talloc_get_type(scfg->opaque, struct ldb_message);
71 colon = strchr(opt_name, ':');
72 if (colon != NULL) {
73 char *name;
75 name = talloc_strdup(scfg, opt_name);
76 if (!name) {
77 return NULL;
79 name[colon-opt_name] = '-';
81 el = ldb_msg_find_element(msg, name);
82 TALLOC_FREE(name);
83 } else {
84 el = ldb_msg_find_element(msg, opt_name);
87 if (el == NULL) {
88 return talloc_strdup(mem_ctx, defval);
91 return (char *)(el->values[0].data);
94 static int sldb_int_option(struct share_config *scfg, const char *opt_name, int defval)
96 char *val;
97 int ret;
99 val = sldb_string_option(scfg, scfg, opt_name, NULL);
100 if (val == NULL) return defval;
102 errno = 0;
103 ret = (int)strtol(val, NULL, 10);
104 TALLOC_FREE(val);
106 if (errno) return -1;
108 return ret;
111 static bool sldb_bool_option(struct share_config *scfg, const char *opt_name, bool defval)
113 char *val;
115 val = sldb_string_option(scfg, scfg, opt_name, NULL);
116 if (val == NULL) return defval;
118 if (strcasecmp(val, "true") == 0) {
119 TALLOC_FREE(val);
120 return true;
123 TALLOC_FREE(val);
124 return false;
127 static const char **sldb_string_list_option(TALLOC_CTX *mem_ctx, struct share_config *scfg, const char *opt_name)
129 struct ldb_message *msg;
130 struct ldb_message_element *el;
131 const char **list;
132 const char *colon;
133 int i;
135 if (scfg == NULL) return NULL;
137 msg = talloc_get_type(scfg->opaque, struct ldb_message);
139 colon = strchr(opt_name, ':');
140 if (colon != NULL) {
141 char *name;
143 name = talloc_strdup(scfg, opt_name);
144 if (!name) {
145 return NULL;
147 name[colon-opt_name] = '-';
149 el = ldb_msg_find_element(msg, name);
150 TALLOC_FREE(name);
151 } else {
152 el = ldb_msg_find_element(msg, opt_name);
155 if (el == NULL) {
156 return NULL;
159 list = talloc_array(mem_ctx, const char *, el->num_values + 1);
160 if (!list) return NULL;
162 for (i = 0; i < el->num_values; i++) {
163 list[i] = (const char *)(el->values[i].data);
165 list[i] = NULL;
167 return list;
170 static NTSTATUS sldb_list_all(TALLOC_CTX *mem_ctx,
171 struct share_context *ctx,
172 int *count,
173 const char ***names)
175 int ret, i, j;
176 const char **n;
177 struct ldb_context *ldb;
178 struct ldb_result *res;
179 TALLOC_CTX *tmp_ctx;
181 tmp_ctx = talloc_new(mem_ctx);
182 if (!tmp_ctx) {
183 DEBUG(0,("ERROR: Out of memory!\n"));
184 return NT_STATUS_NO_MEMORY;
187 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
189 ret = ldb_search(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, "CN=SHARES"),
190 LDB_SCOPE_SUBTREE, NULL, "(name=*)");
191 if (ret != LDB_SUCCESS) {
192 talloc_free(tmp_ctx);
193 return NT_STATUS_INTERNAL_DB_CORRUPTION;
196 n = talloc_array(mem_ctx, const char *, res->count);
197 if (!n) {
198 DEBUG(0,("ERROR: Out of memory!\n"));
199 talloc_free(tmp_ctx);
200 return NT_STATUS_NO_MEMORY;
203 for (i = 0, j = 0; i < res->count; i++) {
204 n[j] = talloc_strdup(n, ldb_msg_find_attr_as_string(res->msgs[i], "name", NULL));
205 if (!n[j]) {
206 DEBUG(0,("WARNING: Malformed share object in share database\n!"));
207 continue;
209 j++;
212 *names = n;
213 *count = j;
214 talloc_free(tmp_ctx);
216 return NT_STATUS_OK;
219 static NTSTATUS sldb_get_config(TALLOC_CTX *mem_ctx,
220 struct share_context *ctx,
221 const char *name,
222 struct share_config **scfg)
224 int ret;
225 struct share_config *s;
226 struct ldb_context *ldb;
227 struct ldb_result *res;
228 TALLOC_CTX *tmp_ctx;
230 tmp_ctx = talloc_new(mem_ctx);
231 if (!tmp_ctx) {
232 DEBUG(0,("ERROR: Out of memory!\n"));
233 return NT_STATUS_NO_MEMORY;
236 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
238 ret = ldb_search(ldb, tmp_ctx, &res,
239 ldb_dn_new(tmp_ctx, ldb, "CN=SHARES"), LDB_SCOPE_SUBTREE, NULL,
240 "(name=%s)", name);
241 if (ret != LDB_SUCCESS || res->count > 1) {
242 talloc_free(tmp_ctx);
243 return NT_STATUS_INTERNAL_DB_CORRUPTION;
244 } else if (res->count != 1) {
245 talloc_free(tmp_ctx);
246 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
249 s = talloc(tmp_ctx, struct share_config);
250 if (!s) {
251 DEBUG(0,("ERROR: Out of memory!\n"));
252 talloc_free(tmp_ctx);
253 return NT_STATUS_NO_MEMORY;
256 s->name = talloc_strdup(s, ldb_msg_find_attr_as_string(res->msgs[0], "name", NULL));
257 if (!s->name) {
258 DEBUG(0,("ERROR: Invalid share object!\n"));
259 talloc_free(tmp_ctx);
260 return NT_STATUS_UNSUCCESSFUL;
263 s->opaque = talloc_steal(s, res->msgs[0]);
264 if (!s->opaque) {
265 DEBUG(0,("ERROR: Invalid share object!\n"));
266 talloc_free(tmp_ctx);
267 return NT_STATUS_UNSUCCESSFUL;
270 s->ctx = ctx;
272 *scfg = talloc_steal(mem_ctx, s);
274 talloc_free(tmp_ctx);
275 return NT_STATUS_OK;
278 #define SHARE_ADD_STRING(name, value) do { \
279 err = ldb_msg_add_string(msg, name, value); \
280 if (err != LDB_SUCCESS) { \
281 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
282 ret = NT_STATUS_UNSUCCESSFUL; \
283 goto done; \
284 } } while(0)
286 #define SHARE_ADD_INT(name, value) do { \
287 err = ldb_msg_add_fmt(msg, name, "%d", value); \
288 if (err != LDB_SUCCESS) { \
289 DEBUG(2,("ERROR: unable to add integer share option %s to ldb msg\n", name)); \
290 ret = NT_STATUS_UNSUCCESSFUL; \
291 goto done; \
292 } } while(0)
294 #define SHARE_ADD_BLOB(name, value) do { \
295 err = ldb_msg_add_value(msg, name, value, NULL); \
296 if (err != LDB_SUCCESS) { \
297 DEBUG(2,("ERROR: unable to add blob share option %s to ldb msg\n", name)); \
298 ret = NT_STATUS_UNSUCCESSFUL; \
299 goto done; \
300 } } while(0)
302 static NTSTATUS sldb_create(struct share_context *ctx, const char *name, struct share_info *info, int count)
304 struct ldb_context *ldb;
305 struct ldb_message *msg;
306 TALLOC_CTX *tmp_ctx;
307 NTSTATUS ret;
308 int err, i, j;
310 for (i = 0, j = 0; i < count && j != 0x03; i++) {
311 if (strcasecmp(info[i].name, SHARE_TYPE) == 0) j |= 0x02;
312 if (strcasecmp(info[i].name, SHARE_PATH) == 0) j |= 0x01;
313 if (strcasecmp(info[i].name, SHARE_NAME) == 0) {
314 if (strcasecmp(name, (char *)info[i].value) != 0) {
315 return NT_STATUS_INVALID_PARAMETER;
319 if (!name || j != 0x03) {
320 return NT_STATUS_INVALID_PARAMETER;
323 tmp_ctx = talloc_new(NULL);
324 if (!tmp_ctx) {
325 DEBUG(0,("ERROR: Out of memory!\n"));
326 return NT_STATUS_NO_MEMORY;
329 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
331 msg = ldb_msg_new(tmp_ctx);
332 if (!msg) {
333 DEBUG(0,("ERROR: Out of memory!\n"));
334 ret = NT_STATUS_NO_MEMORY;
335 goto done;
338 /* TODO: escape info->name */
339 msg->dn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", name);
340 if (!msg->dn) {
341 DEBUG(0,("ERROR: Out of memory!\n"));
342 ret = NT_STATUS_NO_MEMORY;
343 goto done;
346 SHARE_ADD_STRING("objectClass", "top");
347 SHARE_ADD_STRING("objectClass", "share");
348 SHARE_ADD_STRING("cn", name);
349 SHARE_ADD_STRING(SHARE_NAME, name);
351 for (i = 0; i < count; i++) {
352 if (strcasecmp(info[i].name, SHARE_NAME) == 0) continue;
354 switch (info[i].type) {
355 case SHARE_INFO_STRING:
356 SHARE_ADD_STRING(info[i].name, (char *)info[i].value);
357 break;
358 case SHARE_INFO_INT:
359 SHARE_ADD_INT(info[i].name, *((int *)info[i].value));
360 break;
361 case SHARE_INFO_BLOB:
362 SHARE_ADD_BLOB(info[i].name, (DATA_BLOB *)info[i].value);
363 break;
364 default:
365 DEBUG(2,("ERROR: Invalid share info type for %s\n", info[i].name));
366 ret = NT_STATUS_INVALID_PARAMETER;
367 goto done;
371 /* TODO: Security Descriptor */
373 SHARE_ADD_STRING(SHARE_AVAILABLE, "true");
374 SHARE_ADD_STRING(SHARE_BROWSEABLE, "true");
375 SHARE_ADD_STRING(SHARE_READONLY, "false");
376 SHARE_ADD_STRING(SHARE_NTVFS_HANDLER, "unixuid");
377 SHARE_ADD_STRING(SHARE_NTVFS_HANDLER, "posix");
379 err = ldb_add(ldb, msg);
380 if (err != LDB_SUCCESS) {
381 DEBUG(2,("ERROR: unable to add share %s to share.ldb\n"
382 " err=%d [%s]\n", name, err, ldb_errstring(ldb)));
383 if (err == LDB_ERR_NO_SUCH_OBJECT) {
384 ret = NT_STATUS_OBJECT_NAME_NOT_FOUND;
385 } else if (err == LDB_ERR_ENTRY_ALREADY_EXISTS) {
386 ret = NT_STATUS_OBJECT_NAME_COLLISION;
387 } else {
388 ret = NT_STATUS_UNSUCCESSFUL;
390 goto done;
393 ret = NT_STATUS_OK;
394 done:
395 talloc_free(tmp_ctx);
396 return ret;
399 #define SHARE_MOD_STRING(name, value) do { \
400 err = ldb_msg_add_empty(msg, name, LDB_FLAG_MOD_REPLACE, NULL); \
401 if (err != LDB_SUCCESS) { \
402 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
403 ret = NT_STATUS_UNSUCCESSFUL; \
404 goto done; \
406 err = ldb_msg_add_string(msg, name, value); \
407 if (err != LDB_SUCCESS) { \
408 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
409 ret = NT_STATUS_UNSUCCESSFUL; \
410 goto done; \
411 } } while(0)
413 #define SHARE_MOD_INT(name, value) do { \
414 err = ldb_msg_add_empty(msg, name, LDB_FLAG_MOD_REPLACE, NULL); \
415 if (err != LDB_SUCCESS) { \
416 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
417 ret = NT_STATUS_UNSUCCESSFUL; \
418 goto done; \
420 err = ldb_msg_add_fmt(msg, name, "%d", value); \
421 if (err != LDB_SUCCESS) { \
422 DEBUG(2,("ERROR: unable to add integer share option %s to ldb msg\n", name)); \
423 ret = NT_STATUS_UNSUCCESSFUL; \
424 goto done; \
425 } } while(0)
427 #define SHARE_MOD_BLOB(name, value) do { \
428 err = ldb_msg_add_empty(msg, name, LDB_FLAG_MOD_REPLACE, NULL); \
429 if (err != LDB_SUCCESS) { \
430 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
431 ret = NT_STATUS_UNSUCCESSFUL; \
432 goto done; \
434 err = ldb_msg_add_value(msg, name, value, NULL); \
435 if (err != LDB_SUCCESS) { \
436 DEBUG(2,("ERROR: unable to add blob share option %s to ldb msg\n", name)); \
437 ret = NT_STATUS_UNSUCCESSFUL; \
438 goto done; \
439 } } while(0)
441 static NTSTATUS sldb_set(struct share_context *ctx, const char *name, struct share_info *info, int count)
443 struct ldb_context *ldb;
444 struct ldb_message *msg;
445 TALLOC_CTX *tmp_ctx;
446 NTSTATUS ret;
447 bool do_rename = false;
448 char *newname = NULL;
449 int err, i;
451 if (!name) {
452 return NT_STATUS_INVALID_PARAMETER;
455 tmp_ctx = talloc_new(NULL);
456 if (!tmp_ctx) {
457 DEBUG(0,("ERROR: Out of memory!\n"));
458 return NT_STATUS_NO_MEMORY;
461 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
463 msg = ldb_msg_new(tmp_ctx);
464 if (!msg) {
465 DEBUG(0,("ERROR: Out of memory!\n"));
466 ret = NT_STATUS_NO_MEMORY;
467 goto done;
470 /* TODO: escape name */
471 msg->dn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", name);
472 if (!msg->dn) {
473 DEBUG(0,("ERROR: Out of memory!\n"));
474 ret = NT_STATUS_NO_MEMORY;
475 goto done;
478 for (i = 0; i < count; i++) {
479 if (strcasecmp(info[i].name, SHARE_NAME) == 0) {
480 if (strcasecmp(name, (char *)info[i].value) != 0) {
481 do_rename = true;
482 newname = (char *)info[i].value;
483 SHARE_MOD_STRING("cn", (char *)info[i].value);
487 switch (info[i].type) {
488 case SHARE_INFO_STRING:
489 SHARE_MOD_STRING(info[i].name, (char *)info[i].value);
490 break;
491 case SHARE_INFO_INT:
492 SHARE_MOD_INT(info[i].name, *((int *)info[i].value));
493 break;
494 case SHARE_INFO_BLOB:
495 SHARE_MOD_BLOB(info[i].name, (DATA_BLOB *)info[i].value);
496 break;
497 default:
498 DEBUG(2,("ERROR: Invalid share info type for %s\n", info[i].name));
499 ret = NT_STATUS_INVALID_PARAMETER;
500 goto done;
504 if (do_rename) {
505 struct ldb_dn *olddn, *newdn;
507 olddn = msg->dn;
509 /* TODO: escape newname */
510 newdn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", newname);
511 if (!newdn) {
512 DEBUG(0,("ERROR: Out of memory!\n"));
513 ret = NT_STATUS_NO_MEMORY;
514 goto done;
517 err = ldb_rename(ldb, olddn, newdn);
518 if (err != LDB_SUCCESS) {
519 DEBUG(2,("ERROR: unable to rename share %s (to %s)\n"
520 " err=%d [%s]\n", name, newname, err, ldb_errstring(ldb)));
521 if (err == LDB_ERR_NO_SUCH_OBJECT) {
522 ret = NT_STATUS_OBJECT_NAME_COLLISION;
523 } else {
524 ret = NT_STATUS_UNSUCCESSFUL;
526 goto done;
529 msg->dn = newdn;
532 err = ldb_modify(ldb, msg);
533 if (err != LDB_SUCCESS) {
534 DEBUG(2,("ERROR: unable to add share %s to share.ldb\n"
535 " err=%d [%s]\n", name, err, ldb_errstring(ldb)));
536 if (err == LDB_ERR_NO_SUCH_OBJECT) {
537 ret = NT_STATUS_OBJECT_NAME_COLLISION;
538 } else {
539 ret = NT_STATUS_UNSUCCESSFUL;
541 goto done;
544 ret = NT_STATUS_OK;
545 done:
546 talloc_free(tmp_ctx);
547 return ret;
550 static NTSTATUS sldb_remove(struct share_context *ctx, const char *name)
552 struct ldb_context *ldb;
553 struct ldb_dn *dn;
554 TALLOC_CTX *tmp_ctx;
555 NTSTATUS ret;
556 int err;
558 tmp_ctx = talloc_new(NULL);
559 if (!tmp_ctx) {
560 DEBUG(0,("ERROR: Out of memory!\n"));
561 return NT_STATUS_NO_MEMORY;
564 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
566 dn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", name);
567 if (!dn) {
568 DEBUG(0,("ERROR: Out of memory!\n"));
569 ret = NT_STATUS_NO_MEMORY;
570 goto done;
573 err = ldb_delete(ldb, dn);
574 if (err != LDB_SUCCESS) {
575 DEBUG(2,("ERROR: unable to remove share %s from share.ldb\n"
576 " err=%d [%s]\n", name, err, ldb_errstring(ldb)));
577 ret = NT_STATUS_UNSUCCESSFUL;
578 goto done;
581 ret = NT_STATUS_OK;
582 done:
583 talloc_free(tmp_ctx);
584 return ret;
587 static const struct share_ops ops = {
588 .name = "ldb",
589 .init = sldb_init,
590 .string_option = sldb_string_option,
591 .int_option = sldb_int_option,
592 .bool_option = sldb_bool_option,
593 .string_list_option = sldb_string_list_option,
594 .list_all = sldb_list_all,
595 .get_config = sldb_get_config,
596 .create = sldb_create,
597 .set = sldb_set,
598 .remove = sldb_remove
601 NTSTATUS share_ldb_init(TALLOC_CTX *ctx)
603 return share_register(&ops);