s3-libsmb: put namequery headers to nmblib.h
[Samba.git] / source4 / param / share_ldb.c
blob28e145d7db429bee8f15ea5457bbf242bb6114a3
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 static NTSTATUS sldb_init(TALLOC_CTX *mem_ctx, const struct share_ops *ops,
31 struct tevent_context *ev_ctx,
32 struct loadparm_context *lp_ctx,
33 struct share_context **ctx)
35 struct ldb_context *sdb;
37 *ctx = talloc(mem_ctx, struct share_context);
38 if (!*ctx) {
39 DEBUG(0, ("ERROR: Out of memory!\n"));
40 return NT_STATUS_NO_MEMORY;
43 sdb = ldb_wrap_connect(*ctx, ev_ctx, lp_ctx,
44 private_path(*ctx, lp_ctx, "share.ldb"),
45 system_session(lp_ctx),
46 NULL, 0);
48 if (!sdb) {
49 talloc_free(*ctx);
50 return NT_STATUS_UNSUCCESSFUL;
53 (*ctx)->ops = ops;
54 (*ctx)->priv_data = (void *)sdb;
56 return NT_STATUS_OK;
59 static const char *sldb_string_option(struct share_config *scfg, const char *opt_name, const char *defval)
61 struct ldb_message *msg;
62 struct ldb_message_element *el;
64 if (scfg == NULL) return defval;
66 msg = talloc_get_type(scfg->opaque, struct ldb_message);
68 if (strchr(opt_name, ':')) {
69 char *name, *p;
71 name = talloc_strdup(scfg, opt_name);
72 if (!name) {
73 return NULL;
75 p = strchr(name, ':');
76 *p = '-';
78 el = ldb_msg_find_element(msg, name);
79 } else {
80 el = ldb_msg_find_element(msg, opt_name);
83 if (el == NULL) {
84 return defval;
87 return (const char *)(el->values[0].data);
90 static int sldb_int_option(struct share_config *scfg, const char *opt_name, int defval)
92 const char *val;
93 int ret;
95 val = sldb_string_option(scfg, opt_name, NULL);
96 if (val == NULL) return defval;
98 errno = 0;
99 ret = (int)strtol(val, NULL, 10);
100 if (errno) return -1;
102 return ret;
105 static bool sldb_bool_option(struct share_config *scfg, const char *opt_name, bool defval)
107 const char *val;
109 val = sldb_string_option(scfg, opt_name, NULL);
110 if (val == NULL) return defval;
112 if (strcasecmp(val, "true") == 0) return true;
114 return false;
117 static const char **sldb_string_list_option(TALLOC_CTX *mem_ctx, struct share_config *scfg, const char *opt_name)
119 struct ldb_message *msg;
120 struct ldb_message_element *el;
121 const char **list;
122 int i;
124 if (scfg == NULL) return NULL;
126 msg = talloc_get_type(scfg->opaque, struct ldb_message);
128 if (strchr(opt_name, ':')) {
129 char *name, *p;
131 name = talloc_strdup(scfg, opt_name);
132 if (!name) {
133 return NULL;
135 p = strchr(name, ':');
136 *p = '-';
138 el = ldb_msg_find_element(msg, name);
139 } else {
140 el = ldb_msg_find_element(msg, opt_name);
143 if (el == NULL) {
144 return NULL;
147 list = talloc_array(mem_ctx, const char *, el->num_values + 1);
148 if (!list) return NULL;
150 for (i = 0; i < el->num_values; i++) {
151 list[i] = (const char *)(el->values[i].data);
153 list[i] = NULL;
155 return list;
158 static NTSTATUS sldb_list_all(TALLOC_CTX *mem_ctx,
159 struct share_context *ctx,
160 int *count,
161 const char ***names)
163 int ret, i, j;
164 const char **n;
165 struct ldb_context *ldb;
166 struct ldb_result *res;
167 TALLOC_CTX *tmp_ctx;
169 tmp_ctx = talloc_new(mem_ctx);
170 if (!tmp_ctx) {
171 DEBUG(0,("ERROR: Out of memory!\n"));
172 return NT_STATUS_NO_MEMORY;
175 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
177 ret = ldb_search(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, "CN=SHARES"),
178 LDB_SCOPE_SUBTREE, NULL, "(name=*)");
179 if (ret != LDB_SUCCESS) {
180 talloc_free(tmp_ctx);
181 return NT_STATUS_INTERNAL_DB_CORRUPTION;
184 n = talloc_array(mem_ctx, const char *, res->count);
185 if (!n) {
186 DEBUG(0,("ERROR: Out of memory!\n"));
187 talloc_free(tmp_ctx);
188 return NT_STATUS_NO_MEMORY;
191 for (i = 0, j = 0; i < res->count; i++) {
192 n[j] = talloc_strdup(n, ldb_msg_find_attr_as_string(res->msgs[i], "name", NULL));
193 if (!n[j]) {
194 DEBUG(0,("WARNING: Malformed share object in share database\n!"));
195 continue;
197 j++;
200 *names = n;
201 *count = j;
202 talloc_free(tmp_ctx);
204 return NT_STATUS_OK;
207 static NTSTATUS sldb_get_config(TALLOC_CTX *mem_ctx,
208 struct share_context *ctx,
209 const char *name,
210 struct share_config **scfg)
212 int ret;
213 struct share_config *s;
214 struct ldb_context *ldb;
215 struct ldb_result *res;
216 TALLOC_CTX *tmp_ctx;
218 tmp_ctx = talloc_new(mem_ctx);
219 if (!tmp_ctx) {
220 DEBUG(0,("ERROR: Out of memory!\n"));
221 return NT_STATUS_NO_MEMORY;
224 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
226 ret = ldb_search(ldb, tmp_ctx, &res,
227 ldb_dn_new(tmp_ctx, ldb, "CN=SHARES"), LDB_SCOPE_SUBTREE, NULL,
228 "(name=%s)", name);
229 if (ret != LDB_SUCCESS || res->count > 1) {
230 talloc_free(tmp_ctx);
231 return NT_STATUS_INTERNAL_DB_CORRUPTION;
232 } else if (res->count != 1) {
233 talloc_free(tmp_ctx);
234 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
237 s = talloc(tmp_ctx, struct share_config);
238 if (!s) {
239 DEBUG(0,("ERROR: Out of memory!\n"));
240 talloc_free(tmp_ctx);
241 return NT_STATUS_NO_MEMORY;
244 s->name = talloc_strdup(s, ldb_msg_find_attr_as_string(res->msgs[0], "name", NULL));
245 if (!s->name) {
246 DEBUG(0,("ERROR: Invalid share object!\n"));
247 talloc_free(tmp_ctx);
248 return NT_STATUS_UNSUCCESSFUL;
251 s->opaque = talloc_steal(s, res->msgs[0]);
252 if (!s->opaque) {
253 DEBUG(0,("ERROR: Invalid share object!\n"));
254 talloc_free(tmp_ctx);
255 return NT_STATUS_UNSUCCESSFUL;
258 s->ctx = ctx;
260 *scfg = talloc_steal(mem_ctx, s);
262 talloc_free(tmp_ctx);
263 return NT_STATUS_OK;
266 #define SHARE_ADD_STRING(name, value) do { \
267 err = ldb_msg_add_string(msg, name, value); \
268 if (err != LDB_SUCCESS) { \
269 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
270 ret = NT_STATUS_UNSUCCESSFUL; \
271 goto done; \
272 } } while(0)
274 #define SHARE_ADD_INT(name, value) do { \
275 err = ldb_msg_add_fmt(msg, name, "%d", value); \
276 if (err != LDB_SUCCESS) { \
277 DEBUG(2,("ERROR: unable to add integer share option %s to ldb msg\n", name)); \
278 ret = NT_STATUS_UNSUCCESSFUL; \
279 goto done; \
280 } } while(0)
282 #define SHARE_ADD_BLOB(name, value) do { \
283 err = ldb_msg_add_value(msg, name, value, NULL); \
284 if (err != LDB_SUCCESS) { \
285 DEBUG(2,("ERROR: unable to add blob share option %s to ldb msg\n", name)); \
286 ret = NT_STATUS_UNSUCCESSFUL; \
287 goto done; \
288 } } while(0)
290 static NTSTATUS sldb_create(struct share_context *ctx, const char *name, struct share_info *info, int count)
292 struct ldb_context *ldb;
293 struct ldb_message *msg;
294 TALLOC_CTX *tmp_ctx;
295 NTSTATUS ret;
296 int err, i, j;
298 for (i = 0, j = 0; i < count && j != 0x03; i++) {
299 if (strcasecmp(info[i].name, SHARE_TYPE) == 0) j |= 0x02;
300 if (strcasecmp(info[i].name, SHARE_PATH) == 0) j |= 0x01;
301 if (strcasecmp(info[i].name, SHARE_NAME) == 0) {
302 if (strcasecmp(name, (char *)info[i].value) != 0) {
303 return NT_STATUS_INVALID_PARAMETER;
307 if (!name || j != 0x03) {
308 return NT_STATUS_INVALID_PARAMETER;
311 tmp_ctx = talloc_new(NULL);
312 if (!tmp_ctx) {
313 DEBUG(0,("ERROR: Out of memory!\n"));
314 return NT_STATUS_NO_MEMORY;
317 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
319 msg = ldb_msg_new(tmp_ctx);
320 if (!msg) {
321 DEBUG(0,("ERROR: Out of memory!\n"));
322 ret = NT_STATUS_NO_MEMORY;
323 goto done;
326 /* TODO: escape info->name */
327 msg->dn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", name);
328 if (!msg->dn) {
329 DEBUG(0,("ERROR: Out of memory!\n"));
330 ret = NT_STATUS_NO_MEMORY;
331 goto done;
334 SHARE_ADD_STRING("objectClass", "top");
335 SHARE_ADD_STRING("objectClass", "share");
336 SHARE_ADD_STRING("cn", name);
337 SHARE_ADD_STRING(SHARE_NAME, name);
339 for (i = 0; i < count; i++) {
340 if (strcasecmp(info[i].name, SHARE_NAME) == 0) continue;
342 switch (info[i].type) {
343 case SHARE_INFO_STRING:
344 SHARE_ADD_STRING(info[i].name, (char *)info[i].value);
345 break;
346 case SHARE_INFO_INT:
347 SHARE_ADD_INT(info[i].name, *((int *)info[i].value));
348 break;
349 case SHARE_INFO_BLOB:
350 SHARE_ADD_BLOB(info[i].name, (DATA_BLOB *)info[i].value);
351 break;
352 default:
353 DEBUG(2,("ERROR: Invalid share info type for %s\n", info[i].name));
354 ret = NT_STATUS_INVALID_PARAMETER;
355 goto done;
359 /* TODO: Security Descriptor */
361 SHARE_ADD_STRING(SHARE_AVAILABLE, "true");
362 SHARE_ADD_STRING(SHARE_BROWSEABLE, "true");
363 SHARE_ADD_STRING(SHARE_READONLY, "false");
364 SHARE_ADD_STRING(SHARE_NTVFS_HANDLER, "unixuid");
365 SHARE_ADD_STRING(SHARE_NTVFS_HANDLER, "posix");
367 err = ldb_add(ldb, msg);
368 if (err != LDB_SUCCESS) {
369 DEBUG(2,("ERROR: unable to add share %s to share.ldb\n"
370 " err=%d [%s]\n", name, err, ldb_errstring(ldb)));
371 if (err == LDB_ERR_NO_SUCH_OBJECT) {
372 ret = NT_STATUS_OBJECT_NAME_NOT_FOUND;
373 } else if (err == LDB_ERR_ENTRY_ALREADY_EXISTS) {
374 ret = NT_STATUS_OBJECT_NAME_COLLISION;
375 } else {
376 ret = NT_STATUS_UNSUCCESSFUL;
378 goto done;
381 ret = NT_STATUS_OK;
382 done:
383 talloc_free(tmp_ctx);
384 return ret;
387 #define SHARE_MOD_STRING(name, value) do { \
388 err = ldb_msg_add_empty(msg, name, LDB_FLAG_MOD_REPLACE, NULL); \
389 if (err != LDB_SUCCESS) { \
390 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
391 ret = NT_STATUS_UNSUCCESSFUL; \
392 goto done; \
394 err = ldb_msg_add_string(msg, name, value); \
395 if (err != LDB_SUCCESS) { \
396 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
397 ret = NT_STATUS_UNSUCCESSFUL; \
398 goto done; \
399 } } while(0)
401 #define SHARE_MOD_INT(name, value) do { \
402 err = ldb_msg_add_empty(msg, name, LDB_FLAG_MOD_REPLACE, NULL); \
403 if (err != LDB_SUCCESS) { \
404 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
405 ret = NT_STATUS_UNSUCCESSFUL; \
406 goto done; \
408 err = ldb_msg_add_fmt(msg, name, "%d", value); \
409 if (err != LDB_SUCCESS) { \
410 DEBUG(2,("ERROR: unable to add integer share option %s to ldb msg\n", name)); \
411 ret = NT_STATUS_UNSUCCESSFUL; \
412 goto done; \
413 } } while(0)
415 #define SHARE_MOD_BLOB(name, value) do { \
416 err = ldb_msg_add_empty(msg, name, LDB_FLAG_MOD_REPLACE, NULL); \
417 if (err != LDB_SUCCESS) { \
418 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
419 ret = NT_STATUS_UNSUCCESSFUL; \
420 goto done; \
422 err = ldb_msg_add_value(msg, name, value, NULL); \
423 if (err != LDB_SUCCESS) { \
424 DEBUG(2,("ERROR: unable to add blob share option %s to ldb msg\n", name)); \
425 ret = NT_STATUS_UNSUCCESSFUL; \
426 goto done; \
427 } } while(0)
429 static NTSTATUS sldb_set(struct share_context *ctx, const char *name, struct share_info *info, int count)
431 struct ldb_context *ldb;
432 struct ldb_message *msg;
433 TALLOC_CTX *tmp_ctx;
434 NTSTATUS ret;
435 bool do_rename = false;
436 char *newname;
437 int err, i;
439 if (!name) {
440 return NT_STATUS_INVALID_PARAMETER;
443 tmp_ctx = talloc_new(NULL);
444 if (!tmp_ctx) {
445 DEBUG(0,("ERROR: Out of memory!\n"));
446 return NT_STATUS_NO_MEMORY;
449 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
451 msg = ldb_msg_new(tmp_ctx);
452 if (!msg) {
453 DEBUG(0,("ERROR: Out of memory!\n"));
454 ret = NT_STATUS_NO_MEMORY;
455 goto done;
458 /* TODO: escape name */
459 msg->dn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", name);
460 if (!msg->dn) {
461 DEBUG(0,("ERROR: Out of memory!\n"));
462 ret = NT_STATUS_NO_MEMORY;
463 goto done;
466 for (i = 0; i < count; i++) {
467 if (strcasecmp(info[i].name, SHARE_NAME) == 0) {
468 if (strcasecmp(name, (char *)info[i].value) != 0) {
469 do_rename = true;
470 newname = (char *)info[i].value;
471 SHARE_MOD_STRING("cn", (char *)info[i].value);
475 switch (info[i].type) {
476 case SHARE_INFO_STRING:
477 SHARE_MOD_STRING(info[i].name, (char *)info[i].value);
478 break;
479 case SHARE_INFO_INT:
480 SHARE_MOD_INT(info[i].name, *((int *)info[i].value));
481 break;
482 case SHARE_INFO_BLOB:
483 SHARE_MOD_BLOB(info[i].name, (DATA_BLOB *)info[i].value);
484 break;
485 default:
486 DEBUG(2,("ERROR: Invalid share info type for %s\n", info[i].name));
487 ret = NT_STATUS_INVALID_PARAMETER;
488 goto done;
492 if (do_rename) {
493 struct ldb_dn *olddn, *newdn;
495 olddn = msg->dn;
497 /* TODO: escape newname */
498 newdn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", newname);
499 if (!newdn) {
500 DEBUG(0,("ERROR: Out of memory!\n"));
501 ret = NT_STATUS_NO_MEMORY;
502 goto done;
505 err = ldb_rename(ldb, olddn, newdn);
506 if (err != LDB_SUCCESS) {
507 DEBUG(2,("ERROR: unable to rename share %s (to %s)\n"
508 " err=%d [%s]\n", name, newname, err, ldb_errstring(ldb)));
509 if (err == LDB_ERR_NO_SUCH_OBJECT) {
510 ret = NT_STATUS_OBJECT_NAME_COLLISION;
511 } else {
512 ret = NT_STATUS_UNSUCCESSFUL;
514 goto done;
517 msg->dn = newdn;
520 err = ldb_modify(ldb, msg);
521 if (err != LDB_SUCCESS) {
522 DEBUG(2,("ERROR: unable to add share %s to share.ldb\n"
523 " err=%d [%s]\n", name, err, ldb_errstring(ldb)));
524 if (err == LDB_ERR_NO_SUCH_OBJECT) {
525 ret = NT_STATUS_OBJECT_NAME_COLLISION;
526 } else {
527 ret = NT_STATUS_UNSUCCESSFUL;
529 goto done;
532 ret = NT_STATUS_OK;
533 done:
534 talloc_free(tmp_ctx);
535 return ret;
538 static NTSTATUS sldb_remove(struct share_context *ctx, const char *name)
540 struct ldb_context *ldb;
541 struct ldb_dn *dn;
542 TALLOC_CTX *tmp_ctx;
543 NTSTATUS ret;
544 int err;
546 tmp_ctx = talloc_new(NULL);
547 if (!tmp_ctx) {
548 DEBUG(0,("ERROR: Out of memory!\n"));
549 return NT_STATUS_NO_MEMORY;
552 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
554 dn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", name);
555 if (!dn) {
556 DEBUG(0,("ERROR: Out of memory!\n"));
557 ret = NT_STATUS_NO_MEMORY;
558 goto done;
561 err = ldb_delete(ldb, dn);
562 if (err != LDB_SUCCESS) {
563 DEBUG(2,("ERROR: unable to remove share %s from share.ldb\n"
564 " err=%d [%s]\n", name, err, ldb_errstring(ldb)));
565 ret = NT_STATUS_UNSUCCESSFUL;
566 goto done;
569 ret = NT_STATUS_OK;
570 done:
571 talloc_free(tmp_ctx);
572 return ret;
575 static const struct share_ops ops = {
576 .name = "ldb",
577 .init = sldb_init,
578 .string_option = sldb_string_option,
579 .int_option = sldb_int_option,
580 .bool_option = sldb_bool_option,
581 .string_list_option = sldb_string_list_option,
582 .list_all = sldb_list_all,
583 .get_config = sldb_get_config,
584 .create = sldb_create,
585 .set = sldb_set,
586 .remove = sldb_remove
589 NTSTATUS share_ldb_init(void)
591 return share_register(&ops);