r1118: Read-only enumeration of keys in the LDB backend works now :-)
[kamenim/samba.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 "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         printf("Doing search for : %s\n", ret);
56         return ret;
57 }
58
59 /* 
60  * Saves the dn as private_data for every key/val
61  */
62
63 static WERROR ldb_open_registry(REG_HANDLE *handle, const char *location, const char *credentials)
64 {
65         struct ldb_context *c;
66
67         if (!location) return WERR_INVALID_PARAM;
68         c = ldb_connect(location, 0, NULL);
69
70         ldb_set_debug_stderr(c);
71
72         if(!c) return WERR_FOOBAR;
73
74         handle->backend_data = c;
75         
76         return WERR_OK;
77 }
78
79 static WERROR ldb_close_registry(REG_HANDLE *h) 
80 {
81         ldb_close((struct ldb_context *)h->backend_data);
82         return WERR_OK;
83 }
84
85 static WERROR ldb_fetch_subkeys(REG_KEY *k, int *count, REG_KEY ***subkeys)
86 {
87         struct ldb_context *c = k->handle->backend_data;
88         char *path;
89         int ret, i, j;
90         struct ldb_message **msg;
91         TALLOC_CTX *mem_ctx = talloc_init("ldb_path");
92         REG_KEY *key = NULL;
93
94         ret = ldb_search(c, (char *)k->backend_data, LDB_SCOPE_ONELEVEL, "(key=*)", NULL,&msg);
95
96         if(ret < 0) {
97                 DEBUG(0, ("Error getting subkeys for '%s': %s\n", k->backend_data, ldb_errstring(c)));
98                 return WERR_FOOBAR;
99         }
100
101         *subkeys = talloc_array_p(k->mem_ctx, REG_KEY *, ret);
102         j = 0;
103         for(i = 0; i < ret; i++) {
104                 struct ldb_message_element *el;
105                 char *name;
106                 el = ldb_msg_find_element(msg[i], "key");
107
108                 name = el->values[0].data;
109
110                 /* Dirty hack to circumvent ldb_tdb bug */
111                 if(k->backend_data && !strcmp(msg[i]->dn, (char *)k->backend_data)) continue;
112                         
113                 (*subkeys)[j] = reg_key_new_rel(name, k, NULL);
114                 (*subkeys)[j]->backend_data = talloc_strdup((*subkeys)[j]->mem_ctx, msg[i]->dn);
115                 j++;
116         }
117         *count = j;
118
119         ldb_search_free(c, msg);
120         talloc_destroy(mem_ctx);
121         return WERR_OK;
122 }
123
124 static WERROR ldb_get_hive(REG_HANDLE *h, int num, REG_KEY **key)
125 {
126         if(num != 0) return WERR_NO_MORE_ITEMS;
127         *key = reg_key_new_abs("", h, NULL);
128 //      (*key)->backend_data = talloc_strdup((*key)->mem_ctx, "");
129         return WERR_OK;
130 }
131
132 static WERROR ldb_open_key(REG_HANDLE *h, int num, const char *name, REG_KEY **key)
133 {
134         struct ldb_context *c = h->backend_data;
135         char *path;
136         struct ldb_message **msg;
137         char *ldap_path, *new_ldap_path;
138         int ret;
139         TALLOC_CTX *mem_ctx = talloc_init("ldb_path");
140         if(num != 0) return WERR_NO_MORE_ITEMS;
141         ldap_path = reg_path_to_ldb(mem_ctx, name, NULL);
142         
143         ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "*", NULL,&msg);
144
145         if(ret == 0) {
146                 return WERR_NO_MORE_ITEMS;
147         } else if(ret < 0) {
148                 DEBUG(0, ("Error opening key '%s': %s\n", ldap_path, ldb_errstring(c)));
149                 return WERR_FOOBAR;
150         }
151
152         *key = reg_key_new_abs(name, h, ldap_path);
153         talloc_steal(mem_ctx, (*key)->mem_ctx, ldap_path);
154         printf("Got something!\n");
155         /* FIXME */
156
157         ldb_search_free(c, msg);
158         talloc_destroy(mem_ctx);
159
160         return WERR_OK;
161 }
162
163 static struct registry_ops reg_backend_ldb = {
164         .name = "ldb",
165         .open_registry = ldb_open_registry,
166         .get_hive = ldb_get_hive,
167         .close_registry = ldb_close_registry,
168         .open_key = ldb_open_key,
169         .fetch_subkeys = ldb_fetch_subkeys,
170 };
171
172 NTSTATUS registry_ldb_init(void)
173 {
174         return register_backend("registry", &reg_backend_ldb);
175 }