Fixed the patchfile tests and tidy up the patchfile backends.
[Samba.git] / source4 / param / share_ldb.c
blobfb40f1e9bfe8be040e3480ea91dd26eccebc1787
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/include/ldb.h"
24 #include "ldb/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 static NTSTATUS sldb_init(TALLOC_CTX *mem_ctx, const struct share_ops *ops, struct loadparm_context *lp_ctx,
31 struct share_context **ctx)
33 struct ldb_context *sdb;
35 *ctx = talloc(mem_ctx, struct share_context);
36 if (!*ctx) {
37 DEBUG(0, ("ERROR: Out of memory!\n"));
38 return NT_STATUS_NO_MEMORY;
41 sdb = ldb_wrap_connect(*ctx, lp_ctx,
42 private_path(*ctx, lp_ctx, "share.ldb"),
43 system_session(*ctx, lp_ctx),
44 NULL, 0, NULL);
46 if (!sdb) {
47 talloc_free(*ctx);
48 return NT_STATUS_UNSUCCESSFUL;
51 (*ctx)->ops = ops;
52 (*ctx)->priv_data = (void *)sdb;
54 return NT_STATUS_OK;
57 static const char *sldb_string_option(struct share_config *scfg, const char *opt_name, const char *defval)
59 struct ldb_message *msg;
60 struct ldb_message_element *el;
62 if (scfg == NULL) return defval;
64 msg = talloc_get_type(scfg->opaque, struct ldb_message);
66 if (strchr(opt_name, ':')) {
67 char *name, *p;
69 name = talloc_strdup(scfg, opt_name);
70 if (!name) {
71 return NULL;
73 p = strchr(name, ':');
74 *p = '-';
76 el = ldb_msg_find_element(msg, name);
77 } else {
78 el = ldb_msg_find_element(msg, opt_name);
81 if (el == NULL) {
82 return defval;
85 return (const char *)(el->values[0].data);
88 static int sldb_int_option(struct share_config *scfg, const char *opt_name, int defval)
90 const char *val;
91 int ret;
93 val = sldb_string_option(scfg, opt_name, NULL);
94 if (val == NULL) return defval;
96 errno = 0;
97 ret = (int)strtol(val, NULL, 10);
98 if (errno) return -1;
100 return ret;
103 static bool sldb_bool_option(struct share_config *scfg, const char *opt_name, bool defval)
105 const char *val;
107 val = sldb_string_option(scfg, opt_name, NULL);
108 if (val == NULL) return defval;
110 if (strcasecmp(val, "true") == 0) return true;
112 return false;
115 static const char **sldb_string_list_option(TALLOC_CTX *mem_ctx, struct share_config *scfg, const char *opt_name)
117 struct ldb_message *msg;
118 struct ldb_message_element *el;
119 const char **list;
120 int i;
122 if (scfg == NULL) return NULL;
124 msg = talloc_get_type(scfg->opaque, struct ldb_message);
126 if (strchr(opt_name, ':')) {
127 char *name, *p;
129 name = talloc_strdup(scfg, opt_name);
130 if (!name) {
131 return NULL;
133 p = strchr(name, ':');
134 *p = '-';
136 el = ldb_msg_find_element(msg, name);
137 } else {
138 el = ldb_msg_find_element(msg, opt_name);
141 if (el == NULL) {
142 return NULL;
145 list = talloc_array(mem_ctx, const char *, el->num_values + 1);
146 if (!list) return NULL;
148 for (i = 0; i < el->num_values; i++) {
149 list[i] = (const char *)(el->values[i].data);
151 list[i] = NULL;
153 return list;
156 static NTSTATUS sldb_list_all(TALLOC_CTX *mem_ctx,
157 struct share_context *ctx,
158 int *count,
159 const char ***names)
161 int ret, i, j;
162 const char **n;
163 struct ldb_context *ldb;
164 struct ldb_result *res;
165 TALLOC_CTX *tmp_ctx;
167 tmp_ctx = talloc_new(mem_ctx);
168 if (!tmp_ctx) {
169 DEBUG(0,("ERROR: Out of memory!\n"));
170 return NT_STATUS_NO_MEMORY;
173 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
175 ret = ldb_search(ldb, ldb_dn_new(tmp_ctx, ldb, "CN=SHARES"), LDB_SCOPE_SUBTREE, "(name=*)", NULL, &res);
176 talloc_steal(tmp_ctx, res);
177 if (ret != LDB_SUCCESS) {
178 talloc_free(tmp_ctx);
179 return NT_STATUS_INTERNAL_DB_CORRUPTION;
182 n = talloc_array(mem_ctx, const char *, res->count);
183 if (!n) {
184 DEBUG(0,("ERROR: Out of memory!\n"));
185 talloc_free(tmp_ctx);
186 return NT_STATUS_NO_MEMORY;
189 for (i = 0, j = 0; i < res->count; i++) {
190 n[j] = talloc_strdup(n, ldb_msg_find_attr_as_string(res->msgs[i], "name", NULL));
191 if (!n[j]) {
192 DEBUG(0,("WARNING: Malformed share object in share database\n!"));
193 continue;
195 j++;
198 *names = n;
199 *count = j;
200 talloc_free(tmp_ctx);
202 return NT_STATUS_OK;
205 static NTSTATUS sldb_get_config(TALLOC_CTX *mem_ctx,
206 struct share_context *ctx,
207 const char *name,
208 struct share_config **scfg)
210 int ret;
211 struct share_config *s;
212 struct ldb_context *ldb;
213 struct ldb_result *res;
214 TALLOC_CTX *tmp_ctx;
216 tmp_ctx = talloc_new(mem_ctx);
217 if (!tmp_ctx) {
218 DEBUG(0,("ERROR: Out of memory!\n"));
219 return NT_STATUS_NO_MEMORY;
222 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
224 ret = ldb_search_exp_fmt(ldb, tmp_ctx, &res,
225 ldb_dn_new(tmp_ctx, ldb, "CN=SHARES"), LDB_SCOPE_SUBTREE, NULL,
226 "(name=%s)", name);
227 if (ret != LDB_SUCCESS || res->count > 1) {
228 talloc_free(tmp_ctx);
229 return NT_STATUS_INTERNAL_DB_CORRUPTION;
230 } else if (res->count != 1) {
231 talloc_free(tmp_ctx);
232 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
235 s = talloc(tmp_ctx, struct share_config);
236 if (!s) {
237 DEBUG(0,("ERROR: Out of memory!\n"));
238 talloc_free(tmp_ctx);
239 return NT_STATUS_NO_MEMORY;
242 s->name = talloc_strdup(s, ldb_msg_find_attr_as_string(res->msgs[0], "name", NULL));
243 if (!s->name) {
244 DEBUG(0,("ERROR: Invalid share object!\n"));
245 talloc_free(tmp_ctx);
246 return NT_STATUS_UNSUCCESSFUL;
249 s->opaque = talloc_steal(s, res->msgs[0]);
250 if (!s->opaque) {
251 DEBUG(0,("ERROR: Invalid share object!\n"));
252 talloc_free(tmp_ctx);
253 return NT_STATUS_UNSUCCESSFUL;
256 s->ctx = ctx;
258 *scfg = talloc_steal(mem_ctx, s);
260 talloc_free(tmp_ctx);
261 return NT_STATUS_OK;
264 #define SHARE_ADD_STRING(name, value) do { \
265 err = ldb_msg_add_string(msg, name, value); \
266 if (err != LDB_SUCCESS) { \
267 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
268 ret = NT_STATUS_UNSUCCESSFUL; \
269 goto done; \
270 } } while(0)
272 #define SHARE_ADD_INT(name, value) do { \
273 err = ldb_msg_add_fmt(msg, name, "%d", value); \
274 if (err != LDB_SUCCESS) { \
275 DEBUG(2,("ERROR: unable to add integer share option %s to ldb msg\n", name)); \
276 ret = NT_STATUS_UNSUCCESSFUL; \
277 goto done; \
278 } } while(0)
280 #define SHARE_ADD_BLOB(name, value) do { \
281 err = ldb_msg_add_value(msg, name, value, NULL); \
282 if (err != LDB_SUCCESS) { \
283 DEBUG(2,("ERROR: unable to add blob share option %s to ldb msg\n", name)); \
284 ret = NT_STATUS_UNSUCCESSFUL; \
285 goto done; \
286 } } while(0)
288 NTSTATUS sldb_create(struct share_context *ctx, const char *name, struct share_info *info, int count)
290 struct ldb_context *ldb;
291 struct ldb_message *msg;
292 TALLOC_CTX *tmp_ctx;
293 NTSTATUS ret;
294 int err, i, j;
296 for (i = 0, j = 0; i < count && j != 0x03; i++) {
297 if (strcasecmp(info[i].name, SHARE_TYPE) == 0) j |= 0x02;
298 if (strcasecmp(info[i].name, SHARE_PATH) == 0) j |= 0x01;
299 if (strcasecmp(info[i].name, SHARE_NAME) == 0) {
300 if (strcasecmp(name, (char *)info[i].value) != 0) {
301 return NT_STATUS_INVALID_PARAMETER;
305 if (!name || j != 0x03) {
306 return NT_STATUS_INVALID_PARAMETER;
309 tmp_ctx = talloc_new(NULL);
310 if (!tmp_ctx) {
311 DEBUG(0,("ERROR: Out of memory!\n"));
312 return NT_STATUS_NO_MEMORY;
315 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
317 msg = ldb_msg_new(tmp_ctx);
318 if (!msg) {
319 DEBUG(0,("ERROR: Out of memory!\n"));
320 ret = NT_STATUS_NO_MEMORY;
321 goto done;
324 /* TODO: escape info->name */
325 msg->dn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", name);
326 if (!msg->dn) {
327 DEBUG(0,("ERROR: Out of memory!\n"));
328 ret = NT_STATUS_NO_MEMORY;
329 goto done;
332 SHARE_ADD_STRING("objectClass", "top");
333 SHARE_ADD_STRING("objectClass", "share");
334 SHARE_ADD_STRING("cn", name);
335 SHARE_ADD_STRING(SHARE_NAME, name);
337 for (i = 0; i < count; i++) {
338 if (strcasecmp(info[i].name, SHARE_NAME) == 0) continue;
340 switch (info[i].type) {
341 case SHARE_INFO_STRING:
342 SHARE_ADD_STRING(info[i].name, (char *)info[i].value);
343 break;
344 case SHARE_INFO_INT:
345 SHARE_ADD_INT(info[i].name, *((int *)info[i].value));
346 break;
347 case SHARE_INFO_BLOB:
348 SHARE_ADD_BLOB(info[i].name, (DATA_BLOB *)info[i].value);
349 break;
350 default:
351 DEBUG(2,("ERROR: Invalid share info type for %s\n", info[i].name));
352 ret = NT_STATUS_INVALID_PARAMETER;
353 goto done;
357 /* TODO: Security Descriptor */
359 SHARE_ADD_STRING(SHARE_AVAILABLE, "true");
360 SHARE_ADD_STRING(SHARE_BROWSEABLE, "true");
361 SHARE_ADD_STRING(SHARE_READONLY, "false");
362 SHARE_ADD_STRING(SHARE_NTVFS_HANDLER, "unixuid");
363 SHARE_ADD_STRING(SHARE_NTVFS_HANDLER, "posix");
365 err = ldb_add(ldb, msg);
366 if (err != LDB_SUCCESS) {
367 DEBUG(2,("ERROR: unable to add share %s to share.ldb\n"
368 " err=%d [%s]\n", name, err, ldb_errstring(ldb)));
369 if (err == LDB_ERR_NO_SUCH_OBJECT) {
370 ret = NT_STATUS_OBJECT_NAME_NOT_FOUND;
371 } else if (err == LDB_ERR_ENTRY_ALREADY_EXISTS) {
372 ret = NT_STATUS_OBJECT_NAME_COLLISION;
373 } else {
374 ret = NT_STATUS_UNSUCCESSFUL;
376 goto done;
379 ret = NT_STATUS_OK;
380 done:
381 talloc_free(tmp_ctx);
382 return ret;
385 #define SHARE_MOD_STRING(name, value) do { \
386 err = ldb_msg_add_empty(msg, name, LDB_FLAG_MOD_REPLACE, NULL); \
387 if (err != LDB_SUCCESS) { \
388 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
389 ret = NT_STATUS_UNSUCCESSFUL; \
390 goto done; \
392 err = ldb_msg_add_string(msg, name, value); \
393 if (err != LDB_SUCCESS) { \
394 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
395 ret = NT_STATUS_UNSUCCESSFUL; \
396 goto done; \
397 } } while(0)
399 #define SHARE_MOD_INT(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_fmt(msg, name, "%d", value); \
407 if (err != LDB_SUCCESS) { \
408 DEBUG(2,("ERROR: unable to add integer share option %s to ldb msg\n", name)); \
409 ret = NT_STATUS_UNSUCCESSFUL; \
410 goto done; \
411 } } while(0)
413 #define SHARE_MOD_BLOB(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_value(msg, name, value, NULL); \
421 if (err != LDB_SUCCESS) { \
422 DEBUG(2,("ERROR: unable to add blob share option %s to ldb msg\n", name)); \
423 ret = NT_STATUS_UNSUCCESSFUL; \
424 goto done; \
425 } } while(0)
427 NTSTATUS sldb_set(struct share_context *ctx, const char *name, struct share_info *info, int count)
429 struct ldb_context *ldb;
430 struct ldb_message *msg;
431 TALLOC_CTX *tmp_ctx;
432 NTSTATUS ret;
433 bool do_rename = false;
434 char *newname;
435 int err, i;
437 if (!name) {
438 return NT_STATUS_INVALID_PARAMETER;
441 tmp_ctx = talloc_new(NULL);
442 if (!tmp_ctx) {
443 DEBUG(0,("ERROR: Out of memory!\n"));
444 return NT_STATUS_NO_MEMORY;
447 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
449 msg = ldb_msg_new(tmp_ctx);
450 if (!msg) {
451 DEBUG(0,("ERROR: Out of memory!\n"));
452 ret = NT_STATUS_NO_MEMORY;
453 goto done;
456 /* TODO: escape name */
457 msg->dn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", name);
458 if (!msg->dn) {
459 DEBUG(0,("ERROR: Out of memory!\n"));
460 ret = NT_STATUS_NO_MEMORY;
461 goto done;
464 for (i = 0; i < count; i++) {
465 if (strcasecmp(info[i].name, SHARE_NAME) == 0) {
466 if (strcasecmp(name, (char *)info[i].value) != 0) {
467 do_rename = true;
468 newname = (char *)info[i].value;
469 SHARE_MOD_STRING("cn", (char *)info[i].value);
473 switch (info[i].type) {
474 case SHARE_INFO_STRING:
475 SHARE_MOD_STRING(info[i].name, (char *)info[i].value);
476 break;
477 case SHARE_INFO_INT:
478 SHARE_MOD_INT(info[i].name, *((int *)info[i].value));
479 break;
480 case SHARE_INFO_BLOB:
481 SHARE_MOD_BLOB(info[i].name, (DATA_BLOB *)info[i].value);
482 break;
483 default:
484 DEBUG(2,("ERROR: Invalid share info type for %s\n", info[i].name));
485 ret = NT_STATUS_INVALID_PARAMETER;
486 goto done;
490 if (do_rename) {
491 struct ldb_dn *olddn, *newdn;
493 olddn = msg->dn;
495 /* TODO: escape newname */
496 newdn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", newname);
497 if (!newdn) {
498 DEBUG(0,("ERROR: Out of memory!\n"));
499 ret = NT_STATUS_NO_MEMORY;
500 goto done;
503 err = ldb_rename(ldb, olddn, newdn);
504 if (err != LDB_SUCCESS) {
505 DEBUG(2,("ERROR: unable to rename share %s (to %s)\n"
506 " err=%d [%s]\n", name, newname, err, ldb_errstring(ldb)));
507 if (err == LDB_ERR_NO_SUCH_OBJECT) {
508 ret = NT_STATUS_OBJECT_NAME_COLLISION;
509 } else {
510 ret = NT_STATUS_UNSUCCESSFUL;
512 goto done;
515 msg->dn = newdn;
518 err = ldb_modify(ldb, msg);
519 if (err != LDB_SUCCESS) {
520 DEBUG(2,("ERROR: unable to add share %s to share.ldb\n"
521 " err=%d [%s]\n", name, err, ldb_errstring(ldb)));
522 if (err == LDB_ERR_NO_SUCH_OBJECT) {
523 ret = NT_STATUS_OBJECT_NAME_COLLISION;
524 } else {
525 ret = NT_STATUS_UNSUCCESSFUL;
527 goto done;
530 ret = NT_STATUS_OK;
531 done:
532 talloc_free(tmp_ctx);
533 return ret;
536 NTSTATUS sldb_remove(struct share_context *ctx, const char *name)
538 struct ldb_context *ldb;
539 struct ldb_dn *dn;
540 TALLOC_CTX *tmp_ctx;
541 NTSTATUS ret;
542 int err;
544 tmp_ctx = talloc_new(NULL);
545 if (!tmp_ctx) {
546 DEBUG(0,("ERROR: Out of memory!\n"));
547 return NT_STATUS_NO_MEMORY;
550 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
552 dn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", name);
553 if (!dn) {
554 DEBUG(0,("ERROR: Out of memory!\n"));
555 ret = NT_STATUS_NO_MEMORY;
556 goto done;
559 err = ldb_delete(ldb, dn);
560 if (err != LDB_SUCCESS) {
561 DEBUG(2,("ERROR: unable to remove share %s from share.ldb\n"
562 " err=%d [%s]\n", name, err, ldb_errstring(ldb)));
563 ret = NT_STATUS_UNSUCCESSFUL;
564 goto done;
567 ret = NT_STATUS_OK;
568 done:
569 talloc_free(tmp_ctx);
570 return ret;
573 static const struct share_ops ops = {
574 .name = "ldb",
575 .init = sldb_init,
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,
583 .set = sldb_set,
584 .remove = sldb_remove
587 NTSTATUS share_ldb_init(void)
589 return share_register(&ops);