487359eff6ba7a8ac09ad9c7cacbdc4f28492f00
[samba.git] / source4 / param / share_ldb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    LDB based services configuration
5    
6    Copyright (C) Simo Sorce     2006
7    
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.
12    
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.
17    
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/>.
20 */
21
22 #include "includes.h"
23 #include "ldb/include/ldb.h"
24 #include "ldb/include/ldb_errors.h"
25 #include "auth/auth.h"
26 #include "ldb_wrap.h"
27 #include "param/share.h"
28 #include "param/param.h"
29
30 static NTSTATUS sldb_init(TALLOC_CTX *mem_ctx, const struct share_ops *ops, struct share_context **ctx)
31 {
32         struct ldb_context *sdb;
33
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;
38         }
39         
40         sdb = ldb_wrap_connect(*ctx, global_loadparm, 
41                                private_path(*ctx, global_loadparm, "share.ldb"),
42                                system_session(*ctx, global_loadparm),
43                                NULL, 0, NULL);
44
45         if (!sdb) {
46                 talloc_free(*ctx);
47                 return NT_STATUS_UNSUCCESSFUL;
48         }
49
50         (*ctx)->ops = ops;
51         (*ctx)->priv_data = (void *)sdb;
52
53         return NT_STATUS_OK;
54 }
55
56 static const char *sldb_string_option(struct share_config *scfg, const char *opt_name, const char *defval)
57 {
58         struct ldb_message *msg;
59         struct ldb_message_element *el;
60
61         if (scfg == NULL) return defval;
62
63         msg = talloc_get_type(scfg->opaque, struct ldb_message);
64
65         if (strchr(opt_name, ':')) {
66                 char *name, *p;
67
68                 name = talloc_strdup(scfg, opt_name);
69                 if (!name) {
70                         return NULL;
71                 }
72                 p = strchr(name, ':');
73                 *p = '-';
74
75                 el = ldb_msg_find_element(msg, name);
76         } else {
77                 el = ldb_msg_find_element(msg, opt_name);
78         }
79
80         if (el == NULL) {
81                 return defval;
82         }
83
84         return (const char *)(el->values[0].data);
85 }
86
87 static int sldb_int_option(struct share_config *scfg, const char *opt_name, int defval)
88 {
89         const char *val;
90         int ret;
91
92         val = sldb_string_option(scfg, opt_name, NULL);
93         if (val == NULL) return defval;
94
95         errno = 0;
96         ret = (int)strtol(val, NULL, 10);
97         if (errno) return -1;
98
99         return ret;
100 }
101
102 static bool sldb_bool_option(struct share_config *scfg, const char *opt_name, bool defval)
103 {
104         const char *val;
105
106         val = sldb_string_option(scfg, opt_name, NULL);
107         if (val == NULL) return defval;
108
109         if (strcasecmp(val, "true") == 0) return true;
110
111         return false;
112 }
113
114 static const char **sldb_string_list_option(TALLOC_CTX *mem_ctx, struct share_config *scfg, const char *opt_name)
115 {
116         struct ldb_message *msg;
117         struct ldb_message_element *el;
118         const char **list;
119         int i;
120
121         if (scfg == NULL) return NULL;
122
123         msg = talloc_get_type(scfg->opaque, struct ldb_message);
124
125         if (strchr(opt_name, ':')) {
126                 char *name, *p;
127
128                 name = talloc_strdup(scfg, opt_name);
129                 if (!name) {
130                         return NULL;
131                 }
132                 p = strchr(name, ':');
133                 *p = '-';
134
135                 el = ldb_msg_find_element(msg, name);
136         } else {
137                 el = ldb_msg_find_element(msg, opt_name);
138         }
139
140         if (el == NULL) {
141                 return NULL;
142         }
143
144         list = talloc_array(mem_ctx, const char *, el->num_values + 1);
145         if (!list) return NULL;
146
147         for (i = 0; i < el->num_values; i++) {
148                 list[i] = (const char *)(el->values[i].data);
149         }
150         list[i] = NULL;
151
152         return list;
153 }
154
155 static NTSTATUS sldb_list_all(TALLOC_CTX *mem_ctx,
156                                  struct share_context *ctx,
157                                  int *count,
158                                  const char ***names)
159 {
160         int ret, i, j;
161         const char **n;
162         struct ldb_context *ldb;
163         struct ldb_result *res;
164         TALLOC_CTX *tmp_ctx;
165
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;
170         }
171
172         ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
173
174         ret = ldb_search(ldb, ldb_dn_new(tmp_ctx, ldb, "CN=SHARES"), LDB_SCOPE_SUBTREE, "(name=*)", NULL, &res);
175         talloc_steal(tmp_ctx, res);
176         if (ret != LDB_SUCCESS) {
177                 talloc_free(tmp_ctx);
178                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
179         }
180
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;
186         }
187
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;
193                 }
194                 j++;
195         }
196
197         *names = n;
198         *count = j;
199         talloc_free(tmp_ctx);
200
201         return NT_STATUS_OK;
202 }
203
204 static NTSTATUS sldb_get_config(TALLOC_CTX *mem_ctx,
205                          struct share_context *ctx,
206                          const char *name,
207                          struct share_config **scfg)
208 {
209         int ret;
210         struct share_config *s;
211         struct ldb_context *ldb;
212         struct ldb_result *res;
213         TALLOC_CTX *tmp_ctx;
214
215         tmp_ctx = talloc_new(mem_ctx);
216         if (!tmp_ctx) {
217                 DEBUG(0,("ERROR: Out of memory!\n"));
218                 return NT_STATUS_NO_MEMORY;
219         }
220
221         ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
222
223         ret = ldb_search_exp_fmt(ldb, tmp_ctx, &res,
224                                  ldb_dn_new(tmp_ctx, ldb, "CN=SHARES"), LDB_SCOPE_SUBTREE, NULL,
225                                  "(name=%s)", name);
226         if (ret != LDB_SUCCESS || res->count > 1) {
227                 talloc_free(tmp_ctx);
228                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
229         } else if (res->count != 1) {
230                 talloc_free(tmp_ctx);
231                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
232         }
233
234         s = talloc(tmp_ctx, struct share_config);
235         if (!s) {
236                 DEBUG(0,("ERROR: Out of memory!\n"));
237                 talloc_free(tmp_ctx);
238                 return NT_STATUS_NO_MEMORY;
239         }
240
241         s->name = talloc_strdup(s, ldb_msg_find_attr_as_string(res->msgs[0], "name", NULL));
242         if (!s->name) {
243                 DEBUG(0,("ERROR: Invalid share object!\n"));
244                 talloc_free(tmp_ctx);
245                 return NT_STATUS_UNSUCCESSFUL;
246         }
247
248         s->opaque = talloc_steal(s, res->msgs[0]);
249         if (!s->opaque) {
250                 DEBUG(0,("ERROR: Invalid share object!\n"));
251                 talloc_free(tmp_ctx);
252                 return NT_STATUS_UNSUCCESSFUL;
253         }
254
255         s->ctx = ctx;
256
257         *scfg = talloc_steal(mem_ctx, s);
258
259         talloc_free(tmp_ctx);
260         return NT_STATUS_OK;
261 }
262
263 #define SHARE_ADD_STRING(name, value) do { \
264         err = ldb_msg_add_string(msg, name, value); \
265         if (err != LDB_SUCCESS) { \
266                 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
267                 ret = NT_STATUS_UNSUCCESSFUL; \
268                 goto done; \
269         } } while(0)
270
271 #define SHARE_ADD_INT(name, value) do { \
272         err = ldb_msg_add_fmt(msg, name, "%d", value); \
273         if (err != LDB_SUCCESS) { \
274                 DEBUG(2,("ERROR: unable to add integer share option %s to ldb msg\n", name)); \
275                 ret = NT_STATUS_UNSUCCESSFUL; \
276                 goto done; \
277         } } while(0)
278
279 #define SHARE_ADD_BLOB(name, value) do { \
280         err = ldb_msg_add_value(msg, name, value, NULL); \
281         if (err != LDB_SUCCESS) { \
282                 DEBUG(2,("ERROR: unable to add blob share option %s to ldb msg\n", name)); \
283                 ret = NT_STATUS_UNSUCCESSFUL; \
284                 goto done; \
285         } } while(0)
286
287 NTSTATUS sldb_create(struct share_context *ctx, const char *name, struct share_info *info, int count)
288 {
289         struct ldb_context *ldb;
290         struct ldb_message *msg;
291         TALLOC_CTX *tmp_ctx;
292         NTSTATUS ret;
293         int err, i, j;
294
295         for (i = 0, j = 0; i < count && j != 0x03; i++) {
296                 if (strcasecmp(info[i].name, SHARE_TYPE) == 0) j |= 0x02;
297                 if (strcasecmp(info[i].name, SHARE_PATH) == 0) j |= 0x01;
298                 if (strcasecmp(info[i].name, SHARE_NAME) == 0) {
299                         if (strcasecmp(name, (char *)info[i].value) != 0) {
300                                 return NT_STATUS_INVALID_PARAMETER;
301                         }
302                 }
303         }
304         if (!name || j != 0x03) {
305                 return NT_STATUS_INVALID_PARAMETER;
306         }
307         
308         tmp_ctx = talloc_new(NULL);
309         if (!tmp_ctx) {
310                 DEBUG(0,("ERROR: Out of memory!\n"));
311                 return NT_STATUS_NO_MEMORY;
312         }
313
314         ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
315
316         msg = ldb_msg_new(tmp_ctx);
317         if (!msg) {
318                 DEBUG(0,("ERROR: Out of memory!\n"));
319                 ret = NT_STATUS_NO_MEMORY;
320                 goto done;
321         }
322
323         /* TODO: escape info->name */
324         msg->dn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", name);
325         if (!msg->dn) {
326                 DEBUG(0,("ERROR: Out of memory!\n"));
327                 ret = NT_STATUS_NO_MEMORY;
328                 goto done;
329         }
330
331         SHARE_ADD_STRING("objectClass", "top");
332         SHARE_ADD_STRING("objectClass", "share");
333         SHARE_ADD_STRING("cn", name);
334         SHARE_ADD_STRING(SHARE_NAME, name);
335
336         for (i = 0; i < count; i++) {
337                 if (strcasecmp(info[i].name, SHARE_NAME) == 0) continue;
338
339                 switch (info[i].type) {
340                 case SHARE_INFO_STRING:
341                         SHARE_ADD_STRING(info[i].name, (char *)info[i].value);
342                         break;
343                 case SHARE_INFO_INT:
344                         SHARE_ADD_INT(info[i].name, *((int *)info[i].value));
345                         break;
346                 case SHARE_INFO_BLOB:
347                         SHARE_ADD_BLOB(info[i].name, (DATA_BLOB *)info[i].value);
348                         break;
349                 default:
350                         DEBUG(2,("ERROR: Invalid share info type for %s\n", info[i].name));
351                         ret = NT_STATUS_INVALID_PARAMETER;
352                         goto done;
353                 }
354         }
355
356         /* TODO: Security Descriptor */
357
358         SHARE_ADD_STRING(SHARE_AVAILABLE, "true");
359         SHARE_ADD_STRING(SHARE_BROWSEABLE, "true");
360         SHARE_ADD_STRING(SHARE_READONLY, "false");
361         SHARE_ADD_STRING(SHARE_NTVFS_HANDLER, "unixuid");
362         SHARE_ADD_STRING(SHARE_NTVFS_HANDLER, "posix");
363
364         err = ldb_add(ldb, msg);
365         if (err != LDB_SUCCESS) {
366                 DEBUG(2,("ERROR: unable to add share %s to share.ldb\n"
367                          "       err=%d [%s]\n", name, err, ldb_errstring(ldb)));
368                 if (err == LDB_ERR_NO_SUCH_OBJECT) {
369                         ret = NT_STATUS_OBJECT_NAME_NOT_FOUND;
370                 } else if (err == LDB_ERR_ENTRY_ALREADY_EXISTS) {
371                         ret = NT_STATUS_OBJECT_NAME_COLLISION;
372                 } else {
373                         ret = NT_STATUS_UNSUCCESSFUL;
374                 }
375                 goto done;
376         }
377
378         ret = NT_STATUS_OK;
379 done:
380         talloc_free(tmp_ctx);
381         return ret;
382 }
383
384 #define SHARE_MOD_STRING(name, value) do { \
385         err = ldb_msg_add_empty(msg, name, LDB_FLAG_MOD_REPLACE, NULL); \
386         if (err != LDB_SUCCESS) { \
387                 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
388                 ret = NT_STATUS_UNSUCCESSFUL; \
389                 goto done; \
390         } \
391         err = ldb_msg_add_string(msg, name, value); \
392         if (err != LDB_SUCCESS) { \
393                 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
394                 ret = NT_STATUS_UNSUCCESSFUL; \
395                 goto done; \
396         } } while(0)
397
398 #define SHARE_MOD_INT(name, value) do { \
399         err = ldb_msg_add_empty(msg, name, LDB_FLAG_MOD_REPLACE, NULL); \
400         if (err != LDB_SUCCESS) { \
401                 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
402                 ret = NT_STATUS_UNSUCCESSFUL; \
403                 goto done; \
404         } \
405         err = ldb_msg_add_fmt(msg, name, "%d", value); \
406         if (err != LDB_SUCCESS) { \
407                 DEBUG(2,("ERROR: unable to add integer share option %s to ldb msg\n", name)); \
408                 ret = NT_STATUS_UNSUCCESSFUL; \
409                 goto done; \
410         } } while(0)
411
412 #define SHARE_MOD_BLOB(name, value) do { \
413         err = ldb_msg_add_empty(msg, name, LDB_FLAG_MOD_REPLACE, NULL); \
414         if (err != LDB_SUCCESS) { \
415                 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
416                 ret = NT_STATUS_UNSUCCESSFUL; \
417                 goto done; \
418         } \
419         err = ldb_msg_add_value(msg, name, value, NULL); \
420         if (err != LDB_SUCCESS) { \
421                 DEBUG(2,("ERROR: unable to add blob share option %s to ldb msg\n", name)); \
422                 ret = NT_STATUS_UNSUCCESSFUL; \
423                 goto done; \
424         } } while(0)
425
426 NTSTATUS sldb_set(struct share_context *ctx, const char *name, struct share_info *info, int count)
427 {
428         struct ldb_context *ldb;
429         struct ldb_message *msg;
430         TALLOC_CTX *tmp_ctx;
431         NTSTATUS ret;
432         bool do_rename = false;
433         char *newname;
434         int err, i;
435
436         if (!name) {
437                 return NT_STATUS_INVALID_PARAMETER;
438         }
439         
440         tmp_ctx = talloc_new(NULL);
441         if (!tmp_ctx) {
442                 DEBUG(0,("ERROR: Out of memory!\n"));
443                 return NT_STATUS_NO_MEMORY;
444         }
445
446         ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
447
448         msg = ldb_msg_new(tmp_ctx);
449         if (!msg) {
450                 DEBUG(0,("ERROR: Out of memory!\n"));
451                 ret = NT_STATUS_NO_MEMORY;
452                 goto done;
453         }
454
455         /* TODO: escape name */
456         msg->dn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", name);
457         if (!msg->dn) {
458                 DEBUG(0,("ERROR: Out of memory!\n"));
459                 ret = NT_STATUS_NO_MEMORY;
460                 goto done;
461         }
462
463         for (i = 0; i < count; i++) {
464                 if (strcasecmp(info[i].name, SHARE_NAME) == 0) {
465                         if (strcasecmp(name, (char *)info[i].value) != 0) {
466                                 do_rename = true;
467                                 newname = (char *)info[i].value;
468                                 SHARE_MOD_STRING("cn", (char *)info[i].value);
469                         }
470                 }
471
472                 switch (info[i].type) {
473                 case SHARE_INFO_STRING:
474                         SHARE_MOD_STRING(info[i].name, (char *)info[i].value);
475                         break;
476                 case SHARE_INFO_INT:
477                         SHARE_MOD_INT(info[i].name, *((int *)info[i].value));
478                         break;
479                 case SHARE_INFO_BLOB:
480                         SHARE_MOD_BLOB(info[i].name, (DATA_BLOB *)info[i].value);
481                         break;
482                 default:
483                         DEBUG(2,("ERROR: Invalid share info type for %s\n", info[i].name));
484                         ret = NT_STATUS_INVALID_PARAMETER;
485                         goto done;
486                 }
487         }
488
489         if (do_rename) {
490                 struct ldb_dn *olddn, *newdn;
491
492                 olddn = msg->dn;
493
494                 /* TODO: escape newname */
495                 newdn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", newname);
496                 if (!newdn) {
497                         DEBUG(0,("ERROR: Out of memory!\n"));
498                         ret = NT_STATUS_NO_MEMORY;
499                         goto done;
500                 }
501
502                 err = ldb_rename(ldb, olddn, newdn);
503                 if (err != LDB_SUCCESS) {
504                         DEBUG(2,("ERROR: unable to rename share %s (to %s)\n"
505                                  "       err=%d [%s]\n", name, newname, err, ldb_errstring(ldb)));
506                         if (err == LDB_ERR_NO_SUCH_OBJECT) {
507                                 ret = NT_STATUS_OBJECT_NAME_COLLISION;
508                         } else {
509                                 ret = NT_STATUS_UNSUCCESSFUL;
510                         }
511                         goto done;
512                 }
513
514                 msg->dn = newdn;
515         }
516
517         err = ldb_modify(ldb, msg);
518         if (err != LDB_SUCCESS) {
519                 DEBUG(2,("ERROR: unable to add share %s to share.ldb\n"
520                          "       err=%d [%s]\n", name, err, ldb_errstring(ldb)));
521                 if (err == LDB_ERR_NO_SUCH_OBJECT) {
522                         ret = NT_STATUS_OBJECT_NAME_COLLISION;
523                 } else {
524                         ret = NT_STATUS_UNSUCCESSFUL;
525                 }
526                 goto done;
527         }
528
529         ret = NT_STATUS_OK;
530 done:
531         talloc_free(tmp_ctx);
532         return ret;
533 }
534
535 NTSTATUS sldb_remove(struct share_context *ctx, const char *name)
536 {
537         struct ldb_context *ldb;
538         struct ldb_dn *dn;
539         TALLOC_CTX *tmp_ctx;
540         NTSTATUS ret;
541         int err;
542
543         tmp_ctx = talloc_new(NULL);
544         if (!tmp_ctx) {
545                 DEBUG(0,("ERROR: Out of memory!\n"));
546                 return NT_STATUS_NO_MEMORY;
547         }
548
549         ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
550
551         dn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", name);
552         if (!dn) {
553                 DEBUG(0,("ERROR: Out of memory!\n"));
554                 ret = NT_STATUS_NO_MEMORY;
555                 goto done;
556         }
557
558         err = ldb_delete(ldb, dn);
559         if (err != LDB_SUCCESS) {
560                 DEBUG(2,("ERROR: unable to remove share %s from share.ldb\n"
561                          "       err=%d [%s]\n", name, err, ldb_errstring(ldb)));
562                 ret = NT_STATUS_UNSUCCESSFUL;
563                 goto done;
564         }
565
566         ret = NT_STATUS_OK;
567 done:
568         talloc_free(tmp_ctx);
569         return ret;
570 }
571
572 static const struct share_ops ops = {
573         .name = "ldb",
574         .init = sldb_init,
575         .string_option = sldb_string_option,
576         .int_option = sldb_int_option,
577         .bool_option = sldb_bool_option,
578         .string_list_option = sldb_string_list_option,
579         .list_all = sldb_list_all,
580         .get_config = sldb_get_config,
581         .create = sldb_create,
582         .set = sldb_set,
583         .remove = sldb_remove
584 };
585
586 NTSTATUS share_ldb_init(void)
587 {
588         return share_register(&ops);
589 }