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 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
);
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
),
50 return NT_STATUS_UNSUCCESSFUL
;
54 (*ctx
)->priv_data
= (void *)sdb
;
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
, ':')) {
71 name
= talloc_strdup(scfg
, opt_name
);
75 p
= strchr(name
, ':');
78 el
= ldb_msg_find_element(msg
, name
);
80 el
= ldb_msg_find_element(msg
, opt_name
);
87 return (const char *)(el
->values
[0].data
);
90 static int sldb_int_option(struct share_config
*scfg
, const char *opt_name
, int defval
)
95 val
= sldb_string_option(scfg
, opt_name
, NULL
);
96 if (val
== NULL
) return defval
;
99 ret
= (int)strtol(val
, NULL
, 10);
100 if (errno
) return -1;
105 static bool sldb_bool_option(struct share_config
*scfg
, const char *opt_name
, bool defval
)
109 val
= sldb_string_option(scfg
, opt_name
, NULL
);
110 if (val
== NULL
) return defval
;
112 if (strcasecmp(val
, "true") == 0) return true;
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
;
124 if (scfg
== NULL
) return NULL
;
126 msg
= talloc_get_type(scfg
->opaque
, struct ldb_message
);
128 if (strchr(opt_name
, ':')) {
131 name
= talloc_strdup(scfg
, opt_name
);
135 p
= strchr(name
, ':');
138 el
= ldb_msg_find_element(msg
, name
);
140 el
= ldb_msg_find_element(msg
, opt_name
);
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
);
158 static NTSTATUS
sldb_list_all(TALLOC_CTX
*mem_ctx
,
159 struct share_context
*ctx
,
165 struct ldb_context
*ldb
;
166 struct ldb_result
*res
;
169 tmp_ctx
= talloc_new(mem_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
);
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
));
194 DEBUG(0,("WARNING: Malformed share object in share database\n!"));
202 talloc_free(tmp_ctx
);
207 static NTSTATUS
sldb_get_config(TALLOC_CTX
*mem_ctx
,
208 struct share_context
*ctx
,
210 struct share_config
**scfg
)
213 struct share_config
*s
;
214 struct ldb_context
*ldb
;
215 struct ldb_result
*res
;
218 tmp_ctx
= talloc_new(mem_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
,
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
);
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
));
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]);
253 DEBUG(0,("ERROR: Invalid share object!\n"));
254 talloc_free(tmp_ctx
);
255 return NT_STATUS_UNSUCCESSFUL
;
260 *scfg
= talloc_steal(mem_ctx
, s
);
262 talloc_free(tmp_ctx
);
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; \
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; \
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; \
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
;
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
);
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
);
321 DEBUG(0,("ERROR: Out of memory!\n"));
322 ret
= NT_STATUS_NO_MEMORY
;
326 /* TODO: escape info->name */
327 msg
->dn
= ldb_dn_new_fmt(tmp_ctx
, ldb
, "CN=%s,CN=SHARES", name
);
329 DEBUG(0,("ERROR: Out of memory!\n"));
330 ret
= NT_STATUS_NO_MEMORY
;
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
);
347 SHARE_ADD_INT(info
[i
].name
, *((int *)info
[i
].value
));
349 case SHARE_INFO_BLOB
:
350 SHARE_ADD_BLOB(info
[i
].name
, (DATA_BLOB
*)info
[i
].value
);
353 DEBUG(2,("ERROR: Invalid share info type for %s\n", info
[i
].name
));
354 ret
= NT_STATUS_INVALID_PARAMETER
;
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
;
376 ret
= NT_STATUS_UNSUCCESSFUL
;
383 talloc_free(tmp_ctx
);
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; \
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; \
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; \
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; \
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; \
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; \
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
;
435 bool do_rename
= false;
440 return NT_STATUS_INVALID_PARAMETER
;
443 tmp_ctx
= talloc_new(NULL
);
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
);
453 DEBUG(0,("ERROR: Out of memory!\n"));
454 ret
= NT_STATUS_NO_MEMORY
;
458 /* TODO: escape name */
459 msg
->dn
= ldb_dn_new_fmt(tmp_ctx
, ldb
, "CN=%s,CN=SHARES", name
);
461 DEBUG(0,("ERROR: Out of memory!\n"));
462 ret
= NT_STATUS_NO_MEMORY
;
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) {
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
);
480 SHARE_MOD_INT(info
[i
].name
, *((int *)info
[i
].value
));
482 case SHARE_INFO_BLOB
:
483 SHARE_MOD_BLOB(info
[i
].name
, (DATA_BLOB
*)info
[i
].value
);
486 DEBUG(2,("ERROR: Invalid share info type for %s\n", info
[i
].name
));
487 ret
= NT_STATUS_INVALID_PARAMETER
;
493 struct ldb_dn
*olddn
, *newdn
;
497 /* TODO: escape newname */
498 newdn
= ldb_dn_new_fmt(tmp_ctx
, ldb
, "CN=%s,CN=SHARES", newname
);
500 DEBUG(0,("ERROR: Out of memory!\n"));
501 ret
= NT_STATUS_NO_MEMORY
;
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
;
512 ret
= NT_STATUS_UNSUCCESSFUL
;
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
;
527 ret
= NT_STATUS_UNSUCCESSFUL
;
534 talloc_free(tmp_ctx
);
538 static NTSTATUS
sldb_remove(struct share_context
*ctx
, const char *name
)
540 struct ldb_context
*ldb
;
546 tmp_ctx
= talloc_new(NULL
);
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
);
556 DEBUG(0,("ERROR: Out of memory!\n"));
557 ret
= NT_STATUS_NO_MEMORY
;
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
;
571 talloc_free(tmp_ctx
);
575 static const struct share_ops ops
= {
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
,
586 .remove
= sldb_remove
589 NTSTATUS
share_ldb_init(void)
591 return share_register(&ops
);