r19489: Change ldb_msg_add_value and ldb_msg_add_empty to take a foruth argument.
[kai/samba-autobuild/.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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
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"
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,
41                                 private_path(*ctx, "share.ldb"),
42                                 system_session(*ctx),
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_explode(tmp_ctx, "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_BAD_NETWORK_NAME;
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         char *filter;
215
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;
220         }
221
222         ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
223
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;
229         }
230         ret = ldb_search(ldb, ldb_dn_explode(tmp_ctx, "CN=SHARES"), LDB_SCOPE_SUBTREE, filter, NULL, &res);
231         talloc_steal(tmp_ctx, res);
232         if (ret != LDB_SUCCESS || res->count != 1) {
233                 talloc_free(tmp_ctx);
234                 return NT_STATUS_BAD_NETWORK_NAME;
235         }
236
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;
242         }
243
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;
249         }
250
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;
256         }
257
258         s->ctx = ctx;
259
260         *scfg = talloc_steal(mem_ctx, s);
261
262         talloc_free(tmp_ctx);
263         return NT_STATUS_OK;
264 }
265
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; \
271                 goto done; \
272         } } while(0)
273
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; \
279                 goto done; \
280         } } while(0)
281
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; \
287                 goto done; \
288         } } while(0)
289
290 NTSTATUS sldb_create(struct share_context *ctx, const char *name, struct share_info *info, int count)
291 {
292         struct ldb_context *ldb;
293         struct ldb_message *msg;
294         TALLOC_CTX *tmp_ctx;
295         NTSTATUS ret;
296         int err, i, j;
297
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;
304                         }
305                 }
306         }
307         if (!name || j != 0x03) {
308                 return NT_STATUS_INVALID_PARAMETER;
309         }
310         
311         tmp_ctx = talloc_new(NULL);
312         if (!tmp_ctx) {
313                 DEBUG(0,("ERROR: Out of memory!\n"));
314                 return NT_STATUS_NO_MEMORY;
315         }
316
317         ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
318
319         msg = ldb_msg_new(tmp_ctx);
320         if (!msg) {
321                 DEBUG(0,("ERROR: Out of memory!\n"));
322                 ret = NT_STATUS_NO_MEMORY;
323                 goto done;
324         }
325
326         /* TODO: escape info->name */
327         msg->dn = ldb_dn_string_compose(tmp_ctx, ldb_dn_new(tmp_ctx), "CN=%s,CN=SHARES", name);
328         if (!msg->dn) {
329                 DEBUG(0,("ERROR: Out of memory!\n"));
330                 ret = NT_STATUS_NO_MEMORY;
331                 goto done;
332         }
333
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);
338
339         for (i = 0; i < count; i++) {
340                 if (strcasecmp(info[i].name, SHARE_NAME) == 0) continue;
341
342                 switch (info[i].type) {
343                 case SHARE_INFO_STRING:
344                         SHARE_ADD_STRING(info[i].name, (char *)info[i].value);
345                         break;
346                 case SHARE_INFO_INT:
347                         SHARE_ADD_INT(info[i].name, *((int *)info[i].value));
348                         break;
349                 case SHARE_INFO_BLOB:
350                         SHARE_ADD_BLOB(info[i].name, (DATA_BLOB *)info[i].value);
351                         break;
352                 default:
353                         DEBUG(2,("ERROR: Invalid share info type for %s\n", info[i].name));
354                         ret = NT_STATUS_INVALID_PARAMETER;
355                         goto done;
356                 }
357         }
358
359         /* TODO: Security Descriptor */
360
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");
366
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_BAD_NETWORK_NAME;
373                 } else {
374                         ret = NT_STATUS_UNSUCCESSFUL;
375                 }
376                 goto done;
377         }
378
379         ret = NT_STATUS_OK;
380 done:
381         talloc_free(tmp_ctx);
382         return ret;
383 }
384
385 #define SHARE_MOD_STRING(name, value) do { \
386         err = ldb_msg_add_empty(msg, name, LDB_FLAG_MOD_REPLACE, NULL); \
387         if (err != LDB_SUCCESS) { \
388                 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
389                 ret = NT_STATUS_UNSUCCESSFUL; \
390                 goto done; \
391         } \
392         err = ldb_msg_add_string(msg, name, value); \
393         if (err != LDB_SUCCESS) { \
394                 DEBUG(2,("ERROR: unable to add string share option %s to ldb msg\n", name)); \
395                 ret = NT_STATUS_UNSUCCESSFUL; \
396                 goto done; \
397         } } while(0)
398
399 #define SHARE_MOD_INT(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; \
404                 goto done; \
405         } \
406         err = ldb_msg_add_fmt(msg, name, "%d", value); \
407         if (err != LDB_SUCCESS) { \
408                 DEBUG(2,("ERROR: unable to add integer share option %s to ldb msg\n", name)); \
409                 ret = NT_STATUS_UNSUCCESSFUL; \
410                 goto done; \
411         } } while(0)
412
413 #define SHARE_MOD_BLOB(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; \
418                 goto done; \
419         } \
420         err = ldb_msg_add_value(msg, name, value, NULL); \
421         if (err != LDB_SUCCESS) { \
422                 DEBUG(2,("ERROR: unable to add blob share option %s to ldb msg\n", name)); \
423                 ret = NT_STATUS_UNSUCCESSFUL; \
424                 goto done; \
425         } } while(0)
426
427 NTSTATUS sldb_set(struct share_context *ctx, const char *name, struct share_info *info, int count)
428 {
429         struct ldb_context *ldb;
430         struct ldb_message *msg;
431         TALLOC_CTX *tmp_ctx;
432         NTSTATUS ret;
433         bool do_rename = False;
434         char *newname;
435         int err, i;
436
437         if (!name) {
438                 return NT_STATUS_INVALID_PARAMETER;
439         }
440         
441         tmp_ctx = talloc_new(NULL);
442         if (!tmp_ctx) {
443                 DEBUG(0,("ERROR: Out of memory!\n"));
444                 return NT_STATUS_NO_MEMORY;
445         }
446
447         ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
448
449         msg = ldb_msg_new(tmp_ctx);
450         if (!msg) {
451                 DEBUG(0,("ERROR: Out of memory!\n"));
452                 ret = NT_STATUS_NO_MEMORY;
453                 goto done;
454         }
455
456         /* TODO: escape name */
457         msg->dn = ldb_dn_string_compose(tmp_ctx, ldb_dn_new(tmp_ctx), "CN=%s,CN=SHARES", name);
458         if (!msg->dn) {
459                 DEBUG(0,("ERROR: Out of memory!\n"));
460                 ret = NT_STATUS_NO_MEMORY;
461                 goto done;
462         }
463
464         for (i = 0; i < count; i++) {
465                 if (strcasecmp(info[i].name, SHARE_NAME) == 0) {
466                         if (strcasecmp(name, (char *)info[i].value) != 0) {
467                                 do_rename = True;
468                                 newname = (char *)info[i].value;
469                                 SHARE_MOD_STRING("cn", (char *)info[i].value);
470                         }
471                 }
472
473                 switch (info[i].type) {
474                 case SHARE_INFO_STRING:
475                         SHARE_MOD_STRING(info[i].name, (char *)info[i].value);
476                         break;
477                 case SHARE_INFO_INT:
478                         SHARE_MOD_INT(info[i].name, *((int *)info[i].value));
479                         break;
480                 case SHARE_INFO_BLOB:
481                         SHARE_MOD_BLOB(info[i].name, (DATA_BLOB *)info[i].value);
482                         break;
483                 default:
484                         DEBUG(2,("ERROR: Invalid share info type for %s\n", info[i].name));
485                         ret = NT_STATUS_INVALID_PARAMETER;
486                         goto done;
487                 }
488         }
489
490         if (do_rename) {
491                 struct ldb_dn *olddn, *newdn;
492
493                 olddn = msg->dn;
494
495                 /* TODO: escape newname */
496                 newdn = ldb_dn_string_compose(tmp_ctx, ldb_dn_new(tmp_ctx), "CN=%s,CN=SHARES", newname);
497                 if (!newdn) {
498                         DEBUG(0,("ERROR: Out of memory!\n"));
499                         ret = NT_STATUS_NO_MEMORY;
500                         goto done;
501                 }
502
503                 err = ldb_rename(ldb, olddn, newdn);
504                 if (err != LDB_SUCCESS) {
505                         DEBUG(2,("ERROR: unable to rename share %s (to %s)\n"
506                                  "       err=%d [%s]\n", name, newname, err, ldb_errstring(ldb)));
507                         if (err == LDB_ERR_NO_SUCH_OBJECT) {
508                                 ret = NT_STATUS_BAD_NETWORK_NAME;
509                         } else {
510                                 ret = NT_STATUS_UNSUCCESSFUL;
511                         }
512                         goto done;
513                 }
514
515                 msg->dn = newdn;
516         }
517
518         err = ldb_modify(ldb, msg);
519         if (err != LDB_SUCCESS) {
520                 DEBUG(2,("ERROR: unable to add share %s to share.ldb\n"
521                          "       err=%d [%s]\n", name, err, ldb_errstring(ldb)));
522                 if (err == LDB_ERR_NO_SUCH_OBJECT) {
523                         ret = NT_STATUS_BAD_NETWORK_NAME;
524                 } else {
525                         ret = NT_STATUS_UNSUCCESSFUL;
526                 }
527                 goto done;
528         }
529
530         ret = NT_STATUS_OK;
531 done:
532         talloc_free(tmp_ctx);
533         return ret;
534 }
535
536 NTSTATUS sldb_remove(struct share_context *ctx, const char *name)
537 {
538         struct ldb_context *ldb;
539         struct ldb_dn *dn;
540         TALLOC_CTX *tmp_ctx;
541         NTSTATUS ret;
542         int err;
543
544         tmp_ctx = talloc_new(NULL);
545         if (!tmp_ctx) {
546                 DEBUG(0,("ERROR: Out of memory!\n"));
547                 return NT_STATUS_NO_MEMORY;
548         }
549
550         ldb = talloc_get_type(ctx->priv_data, struct ldb_context);
551
552         dn = ldb_dn_string_compose(tmp_ctx, ldb_dn_new(tmp_ctx), "CN=%s,CN=SHARES", name);
553         if (!dn) {
554                 DEBUG(0,("ERROR: Out of memory!\n"));
555                 ret = NT_STATUS_NO_MEMORY;
556                 goto done;
557         }
558
559         err = ldb_delete(ldb, dn);
560         if (err != LDB_SUCCESS) {
561                 DEBUG(2,("ERROR: unable to remove share %s from share.ldb\n"
562                          "       err=%d [%s]\n", name, err, ldb_errstring(ldb)));
563                 ret = NT_STATUS_UNSUCCESSFUL;
564                 goto done;
565         }
566
567         ret = NT_STATUS_OK;
568 done:
569         talloc_free(tmp_ctx);
570         return ret;
571 }
572
573 NTSTATUS share_ldb_init(void)
574 {
575         static struct share_ops ops = {
576                 .name = "ldb",
577                 .init = sldb_init,
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,
585                 .set = sldb_set,
586                 .remove = sldb_remove
587         };
588
589         return share_register(&ops);
590 }
591