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