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 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
;
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
, ':');
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
);
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
)
99 val
= sldb_string_option(scfg
, scfg
, opt_name
, NULL
);
100 if (val
== NULL
) return defval
;
103 ret
= (int)strtol(val
, NULL
, 10);
106 if (errno
) return -1;
111 static bool sldb_bool_option(struct share_config
*scfg
, const char *opt_name
, bool defval
)
115 val
= sldb_string_option(scfg
, scfg
, opt_name
, NULL
);
116 if (val
== NULL
) return defval
;
118 if (strcasecmp(val
, "true") == 0) {
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
;
135 if (scfg
== NULL
) return NULL
;
137 msg
= talloc_get_type(scfg
->opaque
, struct ldb_message
);
139 colon
= strchr(opt_name
, ':');
143 name
= talloc_strdup(scfg
, opt_name
);
147 name
[colon
-opt_name
] = '-';
149 el
= ldb_msg_find_element(msg
, name
);
152 el
= ldb_msg_find_element(msg
, opt_name
);
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
);
170 static NTSTATUS
sldb_list_all(TALLOC_CTX
*mem_ctx
,
171 struct share_context
*ctx
,
177 struct ldb_context
*ldb
;
178 struct ldb_result
*res
;
181 tmp_ctx
= talloc_new(mem_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
);
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
));
206 DEBUG(0,("WARNING: Malformed share object in share database\n!"));
214 talloc_free(tmp_ctx
);
219 static NTSTATUS
sldb_get_config(TALLOC_CTX
*mem_ctx
,
220 struct share_context
*ctx
,
222 struct share_config
**scfg
)
225 struct share_config
*s
;
226 struct ldb_context
*ldb
;
227 struct ldb_result
*res
;
230 tmp_ctx
= talloc_new(mem_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
,
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
);
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
));
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]);
265 DEBUG(0,("ERROR: Invalid share object!\n"));
266 talloc_free(tmp_ctx
);
267 return NT_STATUS_UNSUCCESSFUL
;
272 *scfg
= talloc_steal(mem_ctx
, s
);
274 talloc_free(tmp_ctx
);
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; \
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; \
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; \
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
;
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
);
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
);
333 DEBUG(0,("ERROR: Out of memory!\n"));
334 ret
= NT_STATUS_NO_MEMORY
;
338 /* TODO: escape info->name */
339 msg
->dn
= ldb_dn_new_fmt(tmp_ctx
, ldb
, "CN=%s,CN=SHARES", name
);
341 DEBUG(0,("ERROR: Out of memory!\n"));
342 ret
= NT_STATUS_NO_MEMORY
;
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
);
359 SHARE_ADD_INT(info
[i
].name
, *((int *)info
[i
].value
));
361 case SHARE_INFO_BLOB
:
362 SHARE_ADD_BLOB(info
[i
].name
, (DATA_BLOB
*)info
[i
].value
);
365 DEBUG(2,("ERROR: Invalid share info type for %s\n", info
[i
].name
));
366 ret
= NT_STATUS_INVALID_PARAMETER
;
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
;
388 ret
= NT_STATUS_UNSUCCESSFUL
;
395 talloc_free(tmp_ctx
);
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; \
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; \
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; \
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; \
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; \
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; \
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
;
447 bool do_rename
= false;
452 return NT_STATUS_INVALID_PARAMETER
;
455 tmp_ctx
= talloc_new(NULL
);
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
);
465 DEBUG(0,("ERROR: Out of memory!\n"));
466 ret
= NT_STATUS_NO_MEMORY
;
470 /* TODO: escape name */
471 msg
->dn
= ldb_dn_new_fmt(tmp_ctx
, ldb
, "CN=%s,CN=SHARES", name
);
473 DEBUG(0,("ERROR: Out of memory!\n"));
474 ret
= NT_STATUS_NO_MEMORY
;
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) {
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
);
492 SHARE_MOD_INT(info
[i
].name
, *((int *)info
[i
].value
));
494 case SHARE_INFO_BLOB
:
495 SHARE_MOD_BLOB(info
[i
].name
, (DATA_BLOB
*)info
[i
].value
);
498 DEBUG(2,("ERROR: Invalid share info type for %s\n", info
[i
].name
));
499 ret
= NT_STATUS_INVALID_PARAMETER
;
505 struct ldb_dn
*olddn
, *newdn
;
509 /* TODO: escape newname */
510 newdn
= ldb_dn_new_fmt(tmp_ctx
, ldb
, "CN=%s,CN=SHARES", newname
);
512 DEBUG(0,("ERROR: Out of memory!\n"));
513 ret
= NT_STATUS_NO_MEMORY
;
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
;
524 ret
= NT_STATUS_UNSUCCESSFUL
;
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
;
539 ret
= NT_STATUS_UNSUCCESSFUL
;
546 talloc_free(tmp_ctx
);
550 static NTSTATUS
sldb_remove(struct share_context
*ctx
, const char *name
)
552 struct ldb_context
*ldb
;
558 tmp_ctx
= talloc_new(NULL
);
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
);
568 DEBUG(0,("ERROR: Out of memory!\n"));
569 ret
= NT_STATUS_NO_MEMORY
;
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
;
583 talloc_free(tmp_ctx
);
587 static const struct share_ops ops
= {
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
,
598 .remove
= sldb_remove
601 NTSTATUS
share_ldb_init(void)
603 return share_register(&ops
);