22c81018c741f1daf883612e64d1189b7572b68b
[abartlet/samba.git/.git] / source4 / 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 "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(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(ret, "key=%s,", keyname);
42                         
43                 if(begin) {
44                         *begin = '\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         if(strlen(ret) == 0) return NULL;
56         
57         return ret;
58 }
59
60
61 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx, struct registry_key *k, int idx, struct registry_key **subkey)
62 {
63         struct ldb_context *c = k->hive->backend_data;
64         int ret;
65         struct ldb_message **msg;
66         struct ldb_message_element *el;
67
68         ret = ldb_search(c, (char *)k->backend_data, LDB_SCOPE_ONELEVEL, "(key=*)", NULL,&msg);
69
70         if(ret < 0) {
71                 DEBUG(0, ("Error getting subkeys for '%s': %s\n", (char *)k->backend_data, ldb_errstring(c)));
72                 return WERR_FOOBAR;
73         }
74
75         if(idx >= ret) return WERR_NO_MORE_ITEMS;
76         
77         el = ldb_msg_find_element(msg[idx], "key");
78         
79         *subkey = talloc_p(mem_ctx, struct registry_key);
80         (*subkey)->name = talloc_strdup(mem_ctx, el->values[0].data);
81         (*subkey)->backend_data = talloc_strdup(mem_ctx, msg[idx]->dn);
82
83         ldb_search_free(c, msg);
84         return WERR_OK;
85 }
86
87 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct registry_key *k, int idx, struct registry_value **value)
88 {
89         struct ldb_context *c = k->hive->backend_data;
90         int ret;
91         struct ldb_message **msg;
92         struct ldb_message_element *el;
93
94         ret = ldb_search(c, (char *)k->backend_data, LDB_SCOPE_ONELEVEL, "(value=*)", NULL,&msg);
95
96         if(ret < 0) {
97                 DEBUG(0, ("Error getting values for '%s': %s\n", (char *)k->backend_data, ldb_errstring(c)));
98                 return WERR_FOOBAR;
99         }
100
101         if(idx >= ret) return WERR_NO_MORE_ITEMS;
102         
103         el = ldb_msg_find_element(msg[idx], "value");
104         
105         *value = talloc_p(mem_ctx, struct registry_value);
106         (*value)->name = talloc_strdup(mem_ctx, el->values[0].data);
107         (*value)->backend_data = talloc_strdup(mem_ctx, msg[idx]->dn);
108
109         ldb_search_free(c, msg);
110         return WERR_OK;
111 }
112
113 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, struct registry_hive *h, const char *name, struct registry_key **key)
114 {
115         struct ldb_context *c = h->backend_data;
116         struct ldb_message **msg;
117         char *ldap_path;
118         int ret;
119         ldap_path = reg_path_to_ldb(mem_ctx, name, NULL);
120         
121         ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL,&msg);
122
123         if(ret == 0) {
124                 return WERR_NO_MORE_ITEMS;
125         } else if(ret < 0) {
126                 DEBUG(0, ("Error opening key '%s': %s\n", ldap_path, ldb_errstring(c)));
127                 return WERR_FOOBAR;
128         }
129
130         *key = talloc_p(mem_ctx, struct registry_key);
131         (*key)->name = talloc_strdup(mem_ctx, strrchr(name, '\\'));
132         (*key)->backend_data = talloc_strdup(mem_ctx, msg[0]->dn);
133
134         ldb_search_free(c, msg);
135
136         return WERR_OK;
137 }
138
139 static WERROR ldb_open_hive(TALLOC_CTX *mem_ctx, struct registry_hive *hive, struct registry_key **k)
140 {
141         struct ldb_context *c;
142
143         if (!hive->location) return WERR_INVALID_PARAM;
144         c = ldb_connect(hive->location, 0, NULL);
145
146         if(!c) {
147                 DEBUG(1, ("ldb_open_hive: %s\n", ldb_errstring(hive->backend_data)));
148                 return WERR_FOOBAR;
149         }
150         ldb_set_debug_stderr(c);
151         hive->backend_data = c;
152
153         hive->root = talloc_zero_p(mem_ctx, struct registry_key);
154         hive->root->name = talloc_strdup(mem_ctx, "");
155
156         return WERR_OK;
157 }
158
159 static WERROR ldb_add_key (TALLOC_CTX *mem_ctx, struct registry_key *parent, const char *name, uint32_t access_mask, SEC_DESC *sd, struct registry_key **newkey)
160 {
161         struct ldb_context *ctx = parent->hive->backend_data;
162         struct ldb_message msg;
163         int ret;
164
165         ZERO_STRUCT(msg);
166
167         msg.dn = reg_path_to_ldb(mem_ctx, parent->path, talloc_asprintf(mem_ctx, "key=%s,", name));
168
169         ldb_msg_add_string(ctx, &msg, "key", talloc_strdup(mem_ctx, name));
170
171         ret = ldb_add(ctx, &msg);
172         if (ret < 0) {
173                 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(parent->hive->backend_data)));
174                 return WERR_FOOBAR;
175         }
176
177         *newkey = talloc_zero_p(mem_ctx, struct registry_key);
178         (*newkey)->backend_data = msg.dn;
179         (*newkey)->name = talloc_strdup(mem_ctx, name);
180
181         return WERR_OK;
182 }
183
184 static WERROR ldb_del_key (struct registry_key *key)
185 {
186         int ret;
187
188         ret = ldb_delete(key->hive->backend_data, key->backend_data);
189
190         if (ret < 0) {
191                 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(key->hive->backend_data)));
192                 return WERR_FOOBAR;
193         }
194
195         return WERR_OK;
196 }
197
198 static WERROR ldb_close_hive (struct registry_hive *hive)
199 {
200         ldb_close (hive->backend_data);
201         return WERR_OK;
202 }
203
204 static struct registry_operations reg_backend_ldb = {
205         .name = "ldb",
206         .add_key = ldb_add_key,
207         .del_key = ldb_del_key,
208         .open_hive = ldb_open_hive,
209         .close_hive = ldb_close_hive,
210         .open_key = ldb_open_key,
211         .get_value_by_index = ldb_get_value_by_id,
212         .get_subkey_by_index = ldb_get_subkey_by_id,
213 };
214
215 NTSTATUS registry_ldb_init(void)
216 {
217         return register_backend("registry", &reg_backend_ldb);
218 }