r17860: Let's commit the work down up to now on the new schema module.
[Samba.git] / source4 / param / share_ldb.c
blob8349ad7fe55f176fc7ecf6f454ae90d29c85a394
1 /*
2 Unix SMB/CIFS implementation.
4 LDB based services 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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
24 #include "ldb/include/ldb.h"
25 #include "ldb/include/ldb_errors.h"
26 #include "auth/auth.h"
27 #include "db_wrap.h"
28 #include "param/share.h"
30 static NTSTATUS sldb_init(TALLOC_CTX *mem_ctx, const struct share_ops *ops, struct share_context **ctx)
32 struct ldb_context *sdb;
34 *ctx = talloc(mem_ctx, struct share_context);
35 if (!*ctx) {
36 DEBUG(0, ("ERROR: Out of memory!\n"));
37 return NT_STATUS_NO_MEMORY;
40 sdb = ldb_wrap_connect( *ctx,
41 private_path(*ctx, "share.ldb"),
42 system_session(*ctx),
43 NULL, 0, NULL);
45 if (!sdb) {
46 talloc_free(*ctx);
47 return NT_STATUS_UNSUCCESSFUL;
50 (*ctx)->ops = ops;
51 (*ctx)->priv_data = (void *)sdb;
53 return NT_STATUS_OK;
56 static const char *sldb_string_option(struct share_config *scfg, const char *opt_name, const char *defval)
58 struct ldb_message *msg;
59 struct ldb_message_element *el;
61 if (scfg == NULL) return defval;
63 msg = talloc_get_type(scfg->opaque, struct ldb_message);
65 if (strchr(opt_name, ':')) {
66 char *name, *p;
68 name = talloc_strdup(scfg, opt_name);
69 if (!name) {
70 return NULL;
72 p = strchr(name, ':');
73 *p = '-';
75 el = ldb_msg_find_element(msg, name);
76 } else {
77 el = ldb_msg_find_element(msg, opt_name);
80 if (el == NULL) {
81 return defval;
84 return (const char *)(el->values[0].data);
87 static int sldb_int_option(struct share_config *scfg, const char *opt_name, int defval)
89 const char *val;
90 int ret;
92 val = sldb_string_option(scfg, opt_name, NULL);
93 if (val == NULL) return defval;
95 errno = 0;
96 ret = (int)strtol(val, NULL, 10);
97 if (errno) return -1;
99 return ret;
102 static BOOL sldb_bool_option(struct share_config *scfg, const char *opt_name, BOOL defval)
104 const char *val;
106 val = sldb_string_option(scfg, opt_name, NULL);
107 if (val == NULL) return defval;
109 if (strcasecmp(val, "true") == 0) return True;
111 return False;
114 static const char **sldb_string_list_option(TALLOC_CTX *mem_ctx, struct share_config *scfg, const char *opt_name)
116 struct ldb_message *msg;
117 struct ldb_message_element *el;
118 const char **list;
119 int i;
121 if (scfg == NULL) return NULL;
123 msg = talloc_get_type(scfg->opaque, struct ldb_message);
125 if (strchr(opt_name, ':')) {
126 char *name, *p;
128 name = talloc_strdup(scfg, opt_name);
129 if (!name) {
130 return NULL;
132 p = strchr(name, ':');
133 *p = '-';
135 el = ldb_msg_find_element(msg, name);
136 } else {
137 el = ldb_msg_find_element(msg, opt_name);
140 if (el == NULL) {
141 return NULL;
144 list = talloc_array(mem_ctx, const char *, el->num_values + 1);
145 if (!list) return NULL;
147 for (i = 0; i < el->num_values; i++) {
148 list[i] = (const char *)(el->values[i].data);
150 list[i] = NULL;
152 return list;
155 static NTSTATUS sldb_list_all(TALLOC_CTX *mem_ctx,
156 struct share_context *ctx,
157 int *count,
158 const char ***names)
160 int ret, i, j;
161 const char **n;
162 struct ldb_context *ldb;
163 struct ldb_result *res;
164 TALLOC_CTX *tmp_ctx;
166 tmp_ctx = talloc_new(mem_ctx);
167 if (!tmp_ctx) {
168 DEBUG(0,("ERROR: Out of memory!\n"));
169 return NT_STATUS_NO_MEMORY;
172 ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
174 ret = ldb_search(ldb, ldb_dn_explode(tmp_ctx, "CN=SHARES"), LDB_SCOPE_SUBTREE, "(name=*)", NULL, &res);
175 if (ret != LDB_SUCCESS) {
176 talloc_free(tmp_ctx);
177 return NT_STATUS_UNSUCCESSFUL;
179 talloc_steal(tmp_ctx, res);
181 n = talloc_array(mem_ctx, const char *, res->count);
182 if (!n) {
183 DEBUG(0,("ERROR: Out of memory!\n"));
184 talloc_free(tmp_ctx);
185 return NT_STATUS_NO_MEMORY;
188 for (i = 0, j = 0; i < res->count; i++) {
189 n[j] = talloc_strdup(n, ldb_msg_find_attr_as_string(res->msgs[i], "name", NULL));
190 if (!n[j]) {
191 DEBUG(0,("WARNING: Malformed share object in share database\n!"));
192 continue;
194 j++;
197 *names = n;
198 *count = j;
199 talloc_free(tmp_ctx);
201 return NT_STATUS_OK;
204 static NTSTATUS sldb_get_config(TALLOC_CTX *mem_ctx,
205 struct share_context *ctx,
206 const char *name,
207 struct share_config **scfg)
209 int ret;
210 struct share_config *s;
211 struct ldb_context *ldb;
212 struct ldb_result *res;
213 TALLOC_CTX *tmp_ctx;
214 char *filter;
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 filter = talloc_asprintf(tmp_ctx,"(name=%s)", name);
225 if (!filter) {
226 DEBUG(0,("ERROR: Out of memory!\n"));
227 talloc_free(tmp_ctx);
228 return NT_STATUS_NO_MEMORY;
230 ret = ldb_search(ldb, ldb_dn_explode(tmp_ctx, "CN=SHARES"), LDB_SCOPE_SUBTREE, filter, NULL, &res);
231 if (ret != LDB_SUCCESS || res->count != 1) {
232 talloc_free(tmp_ctx);
233 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
235 talloc_steal(tmp_ctx, res);
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 NTSTATUS share_ldb_init(void)
268 struct share_ops ops;
270 ops.name = "ldb";
271 ops.init = sldb_init;
272 ops.string_option = sldb_string_option;
273 ops.int_option = sldb_int_option;
274 ops.bool_option = sldb_bool_option;
275 ops.string_list_option = sldb_string_list_option;
276 ops.list_all = sldb_list_all;
277 ops.get_config = sldb_get_config;
279 return share_register(&ops);