r1983: a completely new implementation of talloc
[samba.git] / source / lib / registry / reg_backend_ldb / reg_backend_ldb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Registry interface
4    Copyright (C) Jelmer Vernooij  2004.
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "lib/registry/common/registry.h"
23
24 static char *reg_path_to_ldb(TALLOC_CTX *mem_ctx, const char *path, const char *add)
25 {
26         char *ret = talloc_strdup(mem_ctx, "");
27         char *mypath = strdup(path);
28         char *end = mypath, *begin;
29
30         if(add) 
31                 ret = talloc_asprintf_append(mem_ctx, ret, "%s", add);
32
33         while(end) {
34                 char *keyname;
35                 begin = strrchr(end, '\\');
36
37                 if(begin) keyname = begin + 1;
38                 else keyname = mypath;
39
40                 if(strlen(keyname))
41                         ret = talloc_asprintf_append(mem_ctx, ret, "key=%s,", keyname);
42                         
43                 if(begin) {
44                         begin[0] = '\0';
45                         end = begin-1;
46                 } else {
47                         end = NULL;
48                 }
49         }
50
51         SAFE_FREE(mypath);
52
53         ret[strlen(ret)-1] = '\0';
54         
55         return ret;
56 }
57
58 /* 
59  * Saves the dn as private_data for every key/val
60  */
61
62 static WERROR ldb_open_registry(REG_HANDLE *handle, const char *location, const char *credentials)
63 {
64         struct ldb_context *c;
65
66         if (!location) return WERR_INVALID_PARAM;
67         c = ldb_connect(location, 0, NULL);
68
69         ldb_set_debug_stderr(c);
70
71         if(!c) return WERR_FOOBAR;
72
73         handle->backend_data = c;
74         
75         return WERR_OK;
76 }
77
78 static WERROR ldb_close_registry(REG_HANDLE *h) 
79 {
80         ldb_close((struct ldb_context *)h->backend_data);
81         return WERR_OK;
82 }
83
84 static WERROR ldb_add_key(REG_KEY *p, const char *name, uint32_t access_mask, SEC_DESC *sec, REG_KEY **new)
85 {
86         return WERR_NOT_SUPPORTED;      
87 }
88
89 static WERROR ldb_fetch_subkeys(REG_KEY *k, int *count, REG_KEY ***subkeys)
90 {
91         struct ldb_context *c = k->handle->backend_data;
92         char *path;
93         int ret, i, j;
94         struct ldb_message **msg;
95         REG_KEY *key = NULL;
96
97         ret = ldb_search(c, (char *)k->backend_data, LDB_SCOPE_ONELEVEL, "(key=*)", NULL,&msg);
98
99         if(ret < 0) {
100                 DEBUG(0, ("Error getting subkeys for '%s': %s\n", k->backend_data, ldb_errstring(c)));
101                 return WERR_FOOBAR;
102         }
103
104         *subkeys = talloc_array_p(k->mem_ctx, REG_KEY *, ret);
105         j = 0;
106         for(i = 0; i < ret; i++) {
107                 struct ldb_message_element *el;
108                 char *name;
109                 el = ldb_msg_find_element(msg[i], "key");
110
111                 name = el->values[0].data;
112
113                 /* Dirty hack to circumvent ldb_tdb bug */
114                 if(k->backend_data && !strcmp(msg[i]->dn, (char *)k->backend_data)) continue;
115                         
116                 (*subkeys)[j] = reg_key_new_rel(name, k, NULL);
117                 (*subkeys)[j]->backend_data = talloc_strdup((*subkeys)[j]->mem_ctx, msg[i]->dn);
118                 j++;
119         }
120         *count = j;
121
122         ldb_search_free(c, msg);
123         return WERR_OK;
124 }
125
126 static WERROR ldb_fetch_values(REG_KEY *k, int *count, REG_VAL ***values)
127 {
128         struct ldb_context *c = k->handle->backend_data;
129         char *path;
130         int ret, i, j;
131         struct ldb_message **msg;
132         REG_KEY *key = NULL;
133
134         ret = ldb_search(c, (char *)k->backend_data, LDB_SCOPE_ONELEVEL, "(value=*)", NULL,&msg);
135
136         if(ret < 0) {
137                 DEBUG(0, ("Error getting values for '%s': %s\n", k->backend_data, ldb_errstring(c)));
138                 return WERR_FOOBAR;
139         }
140
141         *values = talloc_array_p(k->mem_ctx, REG_VAL *, ret);
142         j = 0;
143         for(i = 0; i < ret; i++) {
144                 struct ldb_message_element *el;
145                 char *name;
146                 el = ldb_msg_find_element(msg[i], "key");
147
148                 name = el->values[0].data;
149
150                 /* Dirty hack to circumvent ldb_tdb bug */
151                 if(k->backend_data && !strcmp(msg[i]->dn, (char *)k->backend_data)) continue;
152                         
153                 (*values)[j] = reg_val_new(k, NULL);
154                 (*values)[j]->backend_data = talloc_strdup((*values)[j]->mem_ctx, msg[i]->dn);
155                 j++;
156         }
157         *count = j;
158
159         ldb_search_free(c, msg);
160         return WERR_OK;
161 }
162
163 static WERROR ldb_get_hive(REG_HANDLE *h, int num, REG_KEY **key)
164 {
165         if(num != 0) return WERR_NO_MORE_ITEMS;
166         *key = reg_key_new_abs("", h, NULL);
167         return WERR_OK;
168 }
169
170 static WERROR ldb_open_key(REG_HANDLE *h, int num, const char *name, REG_KEY **key)
171 {
172         struct ldb_context *c = h->backend_data;
173         char *path;
174         struct ldb_message **msg;
175         char *ldap_path, *new_ldap_path;
176         int ret;
177         TALLOC_CTX *mem_ctx = talloc_init("ldb_path");
178         if(num != 0) return WERR_NO_MORE_ITEMS;
179         ldap_path = reg_path_to_ldb(mem_ctx, name, NULL);
180         
181         ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "*", NULL,&msg);
182
183         if(ret == 0) {
184                 return WERR_NO_MORE_ITEMS;
185         } else if(ret < 0) {
186                 DEBUG(0, ("Error opening key '%s': %s\n", ldap_path, ldb_errstring(c)));
187                 return WERR_FOOBAR;
188         }
189
190         *key = reg_key_new_abs(name, h, ldap_path);
191         talloc_steal((*key)->mem_ctx, ldap_path);
192         printf("Got something!\n");
193         /* FIXME */
194
195         ldb_search_free(c, msg);
196         talloc_destroy(mem_ctx);
197
198         return WERR_OK;
199 }
200
201 static struct registry_ops reg_backend_ldb = {
202         .name = "ldb",
203         .open_registry = ldb_open_registry,
204         .get_hive = ldb_get_hive,
205         .close_registry = ldb_close_registry,
206         .open_key = ldb_open_key,
207         .fetch_subkeys = ldb_fetch_subkeys,
208         .fetch_values = ldb_fetch_values,
209         .add_key = ldb_add_key,
210 };
211
212 NTSTATUS registry_ldb_init(void)
213 {
214         return register_backend("registry", &reg_backend_ldb);
215 }