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/>.
24 #include <ldb_errors.h>
25 #include "auth/auth.h"
27 #include "param/share.h"
28 #include "param/param.h"
30 NTSTATUS
share_ldb_init(void);
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
);
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
),
52 return NT_STATUS_UNSUCCESSFUL
;
56 (*ctx
)->priv_data
= (void *)sdb
;
61 static const char *sldb_string_option(struct share_config
*scfg
, const char *opt_name
, const char *defval
)
63 struct ldb_message
*msg
;
64 struct ldb_message_element
*el
;
67 if (scfg
== NULL
) return defval
;
69 msg
= talloc_get_type(scfg
->opaque
, struct ldb_message
);
71 colon
= strchr(opt_name
, ':');
75 name
= talloc_strdup(scfg
, opt_name
);
79 name
[colon
-opt_name
] = '-';
81 el
= ldb_msg_find_element(msg
, name
);
84 el
= ldb_msg_find_element(msg
, opt_name
);
91 return (const char *)(el
->values
[0].data
);
94 static int sldb_int_option(struct share_config
*scfg
, const char *opt_name
, int defval
)
99 val
= sldb_string_option(scfg
, opt_name
, NULL
);
100 if (val
== NULL
) return defval
;
103 ret
= (int)strtol(val
, NULL
, 10);
104 if (errno
) return -1;
109 static bool sldb_bool_option(struct share_config
*scfg
, const char *opt_name
, bool defval
)
113 val
= sldb_string_option(scfg
, opt_name
, NULL
);
114 if (val
== NULL
) return defval
;
116 if (strcasecmp(val
, "true") == 0) return true;
121 static const char **sldb_string_list_option(TALLOC_CTX
*mem_ctx
, struct share_config
*scfg
, const char *opt_name
)
123 struct ldb_message
*msg
;
124 struct ldb_message_element
*el
;
129 if (scfg
== NULL
) return NULL
;
131 msg
= talloc_get_type(scfg
->opaque
, struct ldb_message
);
133 colon
= strchr(opt_name
, ':');
137 name
= talloc_strdup(scfg
, opt_name
);
141 name
[colon
-opt_name
] = '-';
143 el
= ldb_msg_find_element(msg
, name
);
146 el
= ldb_msg_find_element(msg
, opt_name
);
153 list
= talloc_array(mem_ctx
, const char *, el
->num_values
+ 1);
154 if (!list
) return NULL
;
156 for (i
= 0; i
< el
->num_values
; i
++) {
157 list
[i
] = (const char *)(el
->values
[i
].data
);
164 static NTSTATUS
sldb_list_all(TALLOC_CTX
*mem_ctx
,
165 struct share_context
*ctx
,
171 struct ldb_context
*ldb
;
172 struct ldb_result
*res
;
175 tmp_ctx
= talloc_new(mem_ctx
);
177 DEBUG(0,("ERROR: Out of memory!\n"));
178 return NT_STATUS_NO_MEMORY
;
181 ldb
= talloc_get_type(ctx
->priv_data
, struct ldb_context
);
183 ret
= ldb_search(ldb
, tmp_ctx
, &res
, ldb_dn_new(tmp_ctx
, ldb
, "CN=SHARES"),
184 LDB_SCOPE_SUBTREE
, NULL
, "(name=*)");
185 if (ret
!= LDB_SUCCESS
) {
186 talloc_free(tmp_ctx
);
187 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
190 n
= talloc_array(mem_ctx
, const char *, res
->count
);
192 DEBUG(0,("ERROR: Out of memory!\n"));
193 talloc_free(tmp_ctx
);
194 return NT_STATUS_NO_MEMORY
;
197 for (i
= 0, j
= 0; i
< res
->count
; i
++) {
198 n
[j
] = talloc_strdup(n
, ldb_msg_find_attr_as_string(res
->msgs
[i
], "name", NULL
));
200 DEBUG(0,("WARNING: Malformed share object in share database\n!"));
208 talloc_free(tmp_ctx
);
213 static NTSTATUS
sldb_get_config(TALLOC_CTX
*mem_ctx
,
214 struct share_context
*ctx
,
216 struct share_config
**scfg
)
219 struct share_config
*s
;
220 struct ldb_context
*ldb
;
221 struct ldb_result
*res
;
224 tmp_ctx
= talloc_new(mem_ctx
);
226 DEBUG(0,("ERROR: Out of memory!\n"));
227 return NT_STATUS_NO_MEMORY
;
230 ldb
= talloc_get_type(ctx
->priv_data
, struct ldb_context
);
232 ret
= ldb_search(ldb
, tmp_ctx
, &res
,
233 ldb_dn_new(tmp_ctx
, ldb
, "CN=SHARES"), LDB_SCOPE_SUBTREE
, NULL
,
235 if (ret
!= LDB_SUCCESS
|| res
->count
> 1) {
236 talloc_free(tmp_ctx
);
237 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
238 } else if (res
->count
!= 1) {
239 talloc_free(tmp_ctx
);
240 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
243 s
= talloc(tmp_ctx
, struct share_config
);
245 DEBUG(0,("ERROR: Out of memory!\n"));
246 talloc_free(tmp_ctx
);
247 return NT_STATUS_NO_MEMORY
;
250 s
->name
= talloc_strdup(s
, ldb_msg_find_attr_as_string(res
->msgs
[0], "name", NULL
));
252 DEBUG(0,("ERROR: Invalid share object!\n"));
253 talloc_free(tmp_ctx
);
254 return NT_STATUS_UNSUCCESSFUL
;
257 s
->opaque
= talloc_steal(s
, res
->msgs
[0]);
259 DEBUG(0,("ERROR: Invalid share object!\n"));
260 talloc_free(tmp_ctx
);
261 return NT_STATUS_UNSUCCESSFUL
;
266 *scfg
= talloc_steal(mem_ctx
, s
);
268 talloc_free(tmp_ctx
);
272 #define SHARE_ADD_STRING(name, value) do { \
273 err = ldb_msg_add_string(msg, name, value); \
274 if (err != LDB_SUCCESS) { \
275 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
276 ret = NT_STATUS_UNSUCCESSFUL; \
280 #define SHARE_ADD_INT(name, value) do { \
281 err = ldb_msg_add_fmt(msg, name, "%d", value); \
282 if (err != LDB_SUCCESS) { \
283 DEBUG(2,("ERROR: unable to add integer share option %s to ldb msg\n", name)); \
284 ret = NT_STATUS_UNSUCCESSFUL; \
288 #define SHARE_ADD_BLOB(name, value) do { \
289 err = ldb_msg_add_value(msg, name, value, NULL); \
290 if (err != LDB_SUCCESS) { \
291 DEBUG(2,("ERROR: unable to add blob share option %s to ldb msg\n", name)); \
292 ret = NT_STATUS_UNSUCCESSFUL; \
296 static NTSTATUS
sldb_create(struct share_context
*ctx
, const char *name
, struct share_info
*info
, int count
)
298 struct ldb_context
*ldb
;
299 struct ldb_message
*msg
;
304 for (i
= 0, j
= 0; i
< count
&& j
!= 0x03; i
++) {
305 if (strcasecmp(info
[i
].name
, SHARE_TYPE
) == 0) j
|= 0x02;
306 if (strcasecmp(info
[i
].name
, SHARE_PATH
) == 0) j
|= 0x01;
307 if (strcasecmp(info
[i
].name
, SHARE_NAME
) == 0) {
308 if (strcasecmp(name
, (char *)info
[i
].value
) != 0) {
309 return NT_STATUS_INVALID_PARAMETER
;
313 if (!name
|| j
!= 0x03) {
314 return NT_STATUS_INVALID_PARAMETER
;
317 tmp_ctx
= talloc_new(NULL
);
319 DEBUG(0,("ERROR: Out of memory!\n"));
320 return NT_STATUS_NO_MEMORY
;
323 ldb
= talloc_get_type(ctx
->priv_data
, struct ldb_context
);
325 msg
= ldb_msg_new(tmp_ctx
);
327 DEBUG(0,("ERROR: Out of memory!\n"));
328 ret
= NT_STATUS_NO_MEMORY
;
332 /* TODO: escape info->name */
333 msg
->dn
= ldb_dn_new_fmt(tmp_ctx
, ldb
, "CN=%s,CN=SHARES", name
);
335 DEBUG(0,("ERROR: Out of memory!\n"));
336 ret
= NT_STATUS_NO_MEMORY
;
340 SHARE_ADD_STRING("objectClass", "top");
341 SHARE_ADD_STRING("objectClass", "share");
342 SHARE_ADD_STRING("cn", name
);
343 SHARE_ADD_STRING(SHARE_NAME
, name
);
345 for (i
= 0; i
< count
; i
++) {
346 if (strcasecmp(info
[i
].name
, SHARE_NAME
) == 0) continue;
348 switch (info
[i
].type
) {
349 case SHARE_INFO_STRING
:
350 SHARE_ADD_STRING(info
[i
].name
, (char *)info
[i
].value
);
353 SHARE_ADD_INT(info
[i
].name
, *((int *)info
[i
].value
));
355 case SHARE_INFO_BLOB
:
356 SHARE_ADD_BLOB(info
[i
].name
, (DATA_BLOB
*)info
[i
].value
);
359 DEBUG(2,("ERROR: Invalid share info type for %s\n", info
[i
].name
));
360 ret
= NT_STATUS_INVALID_PARAMETER
;
365 /* TODO: Security Descriptor */
367 SHARE_ADD_STRING(SHARE_AVAILABLE
, "true");
368 SHARE_ADD_STRING(SHARE_BROWSEABLE
, "true");
369 SHARE_ADD_STRING(SHARE_READONLY
, "false");
370 SHARE_ADD_STRING(SHARE_NTVFS_HANDLER
, "unixuid");
371 SHARE_ADD_STRING(SHARE_NTVFS_HANDLER
, "posix");
373 err
= ldb_add(ldb
, msg
);
374 if (err
!= LDB_SUCCESS
) {
375 DEBUG(2,("ERROR: unable to add share %s to share.ldb\n"
376 " err=%d [%s]\n", name
, err
, ldb_errstring(ldb
)));
377 if (err
== LDB_ERR_NO_SUCH_OBJECT
) {
378 ret
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
379 } else if (err
== LDB_ERR_ENTRY_ALREADY_EXISTS
) {
380 ret
= NT_STATUS_OBJECT_NAME_COLLISION
;
382 ret
= NT_STATUS_UNSUCCESSFUL
;
389 talloc_free(tmp_ctx
);
393 #define SHARE_MOD_STRING(name, value) do { \
394 err = ldb_msg_add_empty(msg, name, LDB_FLAG_MOD_REPLACE, NULL); \
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; \
400 err = ldb_msg_add_string(msg, name, value); \
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; \
407 #define SHARE_MOD_INT(name, value) do { \
408 err = ldb_msg_add_empty(msg, name, LDB_FLAG_MOD_REPLACE, NULL); \
409 if (err != LDB_SUCCESS) { \
410 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
411 ret = NT_STATUS_UNSUCCESSFUL; \
414 err = ldb_msg_add_fmt(msg, name, "%d", value); \
415 if (err != LDB_SUCCESS) { \
416 DEBUG(2,("ERROR: unable to add integer share option %s to ldb msg\n", name)); \
417 ret = NT_STATUS_UNSUCCESSFUL; \
421 #define SHARE_MOD_BLOB(name, value) do { \
422 err = ldb_msg_add_empty(msg, name, LDB_FLAG_MOD_REPLACE, NULL); \
423 if (err != LDB_SUCCESS) { \
424 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
425 ret = NT_STATUS_UNSUCCESSFUL; \
428 err = ldb_msg_add_value(msg, name, value, NULL); \
429 if (err != LDB_SUCCESS) { \
430 DEBUG(2,("ERROR: unable to add blob share option %s to ldb msg\n", name)); \
431 ret = NT_STATUS_UNSUCCESSFUL; \
435 static NTSTATUS
sldb_set(struct share_context
*ctx
, const char *name
, struct share_info
*info
, int count
)
437 struct ldb_context
*ldb
;
438 struct ldb_message
*msg
;
441 bool do_rename
= false;
446 return NT_STATUS_INVALID_PARAMETER
;
449 tmp_ctx
= talloc_new(NULL
);
451 DEBUG(0,("ERROR: Out of memory!\n"));
452 return NT_STATUS_NO_MEMORY
;
455 ldb
= talloc_get_type(ctx
->priv_data
, struct ldb_context
);
457 msg
= ldb_msg_new(tmp_ctx
);
459 DEBUG(0,("ERROR: Out of memory!\n"));
460 ret
= NT_STATUS_NO_MEMORY
;
464 /* TODO: escape name */
465 msg
->dn
= ldb_dn_new_fmt(tmp_ctx
, ldb
, "CN=%s,CN=SHARES", name
);
467 DEBUG(0,("ERROR: Out of memory!\n"));
468 ret
= NT_STATUS_NO_MEMORY
;
472 for (i
= 0; i
< count
; i
++) {
473 if (strcasecmp(info
[i
].name
, SHARE_NAME
) == 0) {
474 if (strcasecmp(name
, (char *)info
[i
].value
) != 0) {
476 newname
= (char *)info
[i
].value
;
477 SHARE_MOD_STRING("cn", (char *)info
[i
].value
);
481 switch (info
[i
].type
) {
482 case SHARE_INFO_STRING
:
483 SHARE_MOD_STRING(info
[i
].name
, (char *)info
[i
].value
);
486 SHARE_MOD_INT(info
[i
].name
, *((int *)info
[i
].value
));
488 case SHARE_INFO_BLOB
:
489 SHARE_MOD_BLOB(info
[i
].name
, (DATA_BLOB
*)info
[i
].value
);
492 DEBUG(2,("ERROR: Invalid share info type for %s\n", info
[i
].name
));
493 ret
= NT_STATUS_INVALID_PARAMETER
;
499 struct ldb_dn
*olddn
, *newdn
;
503 /* TODO: escape newname */
504 newdn
= ldb_dn_new_fmt(tmp_ctx
, ldb
, "CN=%s,CN=SHARES", newname
);
506 DEBUG(0,("ERROR: Out of memory!\n"));
507 ret
= NT_STATUS_NO_MEMORY
;
511 err
= ldb_rename(ldb
, olddn
, newdn
);
512 if (err
!= LDB_SUCCESS
) {
513 DEBUG(2,("ERROR: unable to rename share %s (to %s)\n"
514 " err=%d [%s]\n", name
, newname
, err
, ldb_errstring(ldb
)));
515 if (err
== LDB_ERR_NO_SUCH_OBJECT
) {
516 ret
= NT_STATUS_OBJECT_NAME_COLLISION
;
518 ret
= NT_STATUS_UNSUCCESSFUL
;
526 err
= ldb_modify(ldb
, msg
);
527 if (err
!= LDB_SUCCESS
) {
528 DEBUG(2,("ERROR: unable to add share %s to share.ldb\n"
529 " err=%d [%s]\n", name
, err
, ldb_errstring(ldb
)));
530 if (err
== LDB_ERR_NO_SUCH_OBJECT
) {
531 ret
= NT_STATUS_OBJECT_NAME_COLLISION
;
533 ret
= NT_STATUS_UNSUCCESSFUL
;
540 talloc_free(tmp_ctx
);
544 static NTSTATUS
sldb_remove(struct share_context
*ctx
, const char *name
)
546 struct ldb_context
*ldb
;
552 tmp_ctx
= talloc_new(NULL
);
554 DEBUG(0,("ERROR: Out of memory!\n"));
555 return NT_STATUS_NO_MEMORY
;
558 ldb
= talloc_get_type(ctx
->priv_data
, struct ldb_context
);
560 dn
= ldb_dn_new_fmt(tmp_ctx
, ldb
, "CN=%s,CN=SHARES", name
);
562 DEBUG(0,("ERROR: Out of memory!\n"));
563 ret
= NT_STATUS_NO_MEMORY
;
567 err
= ldb_delete(ldb
, dn
);
568 if (err
!= LDB_SUCCESS
) {
569 DEBUG(2,("ERROR: unable to remove share %s from share.ldb\n"
570 " err=%d [%s]\n", name
, err
, ldb_errstring(ldb
)));
571 ret
= NT_STATUS_UNSUCCESSFUL
;
577 talloc_free(tmp_ctx
);
581 static const struct share_ops ops
= {
584 .string_option
= sldb_string_option
,
585 .int_option
= sldb_int_option
,
586 .bool_option
= sldb_bool_option
,
587 .string_list_option
= sldb_string_list_option
,
588 .list_all
= sldb_list_all
,
589 .get_config
= sldb_get_config
,
590 .create
= sldb_create
,
592 .remove
= sldb_remove
595 NTSTATUS
share_ldb_init(void)
597 return share_register(&ops
);