4 Copyright (C) Andrew Tridgell 2005
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 ldb proxy module. At startup this looks for a record like this:
30 olddn = basedn to proxy in upstream server
31 newdn = basedn in local server
32 username = username to connect to upstream
33 password = password for upstream
35 NOTE: this module is a complete hack at this stage. I am committing it just
36 so others can know how I am investigating mmc support
41 #include "ldb/include/ldb.h"
42 #include "ldb/include/ldb_errors.h"
43 #include "ldb/include/ldb_private.h"
44 #include "auth/credentials/credentials.h"
47 struct ldb_context *upstream;
56 load the @PROXYINFO record
58 static int load_proxy_info(struct ldb_module *module)
60 struct proxy_data *proxy = talloc_get_type(module->private_data, struct proxy_data);
62 struct ldb_result *res = NULL;
64 const char *olddn, *newdn, *url, *username, *password, *oldstr, *newstr;
65 struct cli_credentials *creds;
68 /* see if we have already loaded it */
69 if (proxy->upstream != NULL) {
73 dn = ldb_dn_new(proxy, module->ldb, "@PROXYINFO");
77 ret = ldb_search(module->ldb, dn, LDB_SCOPE_BASE, NULL, NULL, &res);
79 if (ret != LDB_SUCCESS || res->count != 1) {
80 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "Can't find @PROXYINFO\n");
84 url = ldb_msg_find_attr_as_string(res->msgs[0], "url", NULL);
85 olddn = ldb_msg_find_attr_as_string(res->msgs[0], "olddn", NULL);
86 newdn = ldb_msg_find_attr_as_string(res->msgs[0], "newdn", NULL);
87 username = ldb_msg_find_attr_as_string(res->msgs[0], "username", NULL);
88 password = ldb_msg_find_attr_as_string(res->msgs[0], "password", NULL);
89 oldstr = ldb_msg_find_attr_as_string(res->msgs[0], "oldstr", NULL);
90 newstr = ldb_msg_find_attr_as_string(res->msgs[0], "newstr", NULL);
92 if (url == NULL || olddn == NULL || newdn == NULL || username == NULL || password == NULL) {
93 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "Need url, olddn, newdn, oldstr, newstr, username and password in @PROXYINFO\n");
97 proxy->olddn = ldb_dn_new(proxy, module->ldb, olddn);
98 if (proxy->olddn == NULL) {
99 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "Failed to explode olddn '%s'\n", olddn);
103 proxy->newdn = ldb_dn_new(proxy, module->ldb, newdn);
104 if (proxy->newdn == NULL) {
105 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "Failed to explode newdn '%s'\n", newdn);
109 proxy->upstream = ldb_init(proxy);
110 if (proxy->upstream == NULL) {
111 ldb_oom(module->ldb);
115 proxy->oldstr = str_list_make(proxy, oldstr, ", ");
116 if (proxy->oldstr == NULL) {
117 ldb_oom(module->ldb);
121 proxy->newstr = str_list_make(proxy, newstr, ", ");
122 if (proxy->newstr == NULL) {
123 ldb_oom(module->ldb);
127 /* setup credentials for connection */
128 creds = cli_credentials_init(proxy->upstream);
130 ldb_oom(module->ldb);
133 cli_credentials_guess(creds);
134 cli_credentials_set_username(creds, username, CRED_SPECIFIED);
135 cli_credentials_set_password(creds, password, CRED_SPECIFIED);
137 ldb_set_opaque(proxy->upstream, "credentials", creds);
139 ret = ldb_connect(proxy->upstream, url, 0, NULL);
141 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "proxy failed to connect to %s\n", url);
145 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "proxy connected to %s\n", url);
153 talloc_free(proxy->olddn);
154 talloc_free(proxy->newdn);
155 talloc_free(proxy->upstream);
156 proxy->upstream = NULL;
162 convert a binary blob
164 static void proxy_convert_blob(TALLOC_CTX *mem_ctx, struct ldb_val *v,
165 const char *oldstr, const char *newstr)
167 int len1, len2, len3;
168 uint8_t *olddata = v->data;
169 char *p = strcasestr((char *)v->data, oldstr);
171 len1 = (p - (char *)v->data);
172 len2 = strlen(newstr);
173 len3 = v->length - (p+strlen(oldstr) - (char *)v->data);
174 v->length = len1+len2+len3;
175 v->data = talloc_size(mem_ctx, v->length);
176 memcpy(v->data, olddata, len1);
177 memcpy(v->data+len1, newstr, len2);
178 memcpy(v->data+len1+len2, olddata + len1 + strlen(oldstr), len3);
182 convert a returned value
184 static void proxy_convert_value(struct ldb_module *module, struct ldb_message *msg, struct ldb_val *v)
186 struct proxy_data *proxy = talloc_get_type(module->private_data, struct proxy_data);
188 for (i=0;proxy->oldstr[i];i++) {
189 char *p = strcasestr((char *)v->data, proxy->oldstr[i]);
190 if (p == NULL) continue;
191 proxy_convert_blob(msg, v, proxy->oldstr[i], proxy->newstr[i]);
197 convert a returned value
199 static struct ldb_parse_tree *proxy_convert_tree(struct ldb_module *module,
200 struct ldb_parse_tree *tree)
202 struct proxy_data *proxy = talloc_get_type(module->private_data, struct proxy_data);
204 char *expression = ldb_filter_from_tree(module, tree);
205 for (i=0;proxy->newstr[i];i++) {
207 char *p = strcasestr(expression, proxy->newstr[i]);
208 if (p == NULL) continue;
209 v.data = (uint8_t *)expression;
210 v.length = strlen(expression)+1;
211 proxy_convert_blob(module, &v, proxy->newstr[i], proxy->oldstr[i]);
212 return ldb_parse_tree(module, (const char *)v.data);
220 convert a returned record
222 static void proxy_convert_record(struct ldb_module *module, struct ldb_message *msg)
224 struct proxy_data *proxy = talloc_get_type(module->private_data, struct proxy_data);
227 /* fix the message DN */
228 if (ldb_dn_compare_base(module->ldb, proxy->olddn, msg->dn) == 0) {
229 ldb_dn_remove_base_components(msg->dn, ldb_dn_get_comp_num(proxy->olddn));
230 ldb_dn_add_base(msg->dn, proxy->newdn);
233 /* fix any attributes */
234 for (attr=0;attr<msg->num_elements;attr++) {
235 for (v=0;v<msg->elements[attr].num_values;v++) {
236 proxy_convert_value(module, msg, &msg->elements[attr].values[v]);
240 /* fix any DN components */
241 for (attr=0;attr<msg->num_elements;attr++) {
242 for (v=0;v<msg->elements[attr].num_values;v++) {
243 proxy_convert_value(module, msg, &msg->elements[attr].values[v]);
249 static int proxy_search_bytree(struct ldb_module *module, struct ldb_request *req)
251 struct proxy_data *proxy = talloc_get_type(module->private_data, struct proxy_data);
252 struct ldb_request *newreq;
256 if (req->op.search.base == NULL ||
257 (req->op.search.base->comp_num == 1 &&
258 req->op.search.base->components[0].name[0] == '@')) {
262 if (load_proxy_info(module) != 0) {
266 /* see if the dn is within olddn */
267 if (ldb_dn_compare_base(module->ldb, proxy->newdn, req->op.search.base) != 0) {
271 newreq = talloc(module, struct ldb_request);
272 if (newreq == NULL) {
276 newreq->op.search.tree = proxy_convert_tree(module, req->op.search.tree);
278 /* convert the basedn of this search */
279 base = ldb_dn_copy(proxy, req->op.search.base);
284 ldb_dn_remove_base_components(base, ldb_dn_get_comp_num(proxy->newdn));
285 ldb_dn_add_base(base, proxy->olddn);
287 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "proxying: '%s' with dn '%s' \n",
288 ldb_filter_from_tree(proxy, newreq->op.search.tree), ldb_dn_get_linearized(newreq->op.search.base));
289 for (i = 0; req->op.search.attrs && req->op.search.attrs[i]; i++) {
290 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "attr: '%s'\n", req->op.search.attrs[i]);
293 newreq->op.search.base = base;
294 newreq->op.search.scope = req->op.search.scope;
295 newreq->op.search.attrs = req->op.search.attrs;
296 newreq->op.search.res = req->op.search.res;
297 newreq->controls = req->controls;
298 ret = ldb_request(proxy->upstream, newreq);
299 if (ret != LDB_SUCCESS) {
300 ldb_set_errstring(module->ldb, ldb_errstring(proxy->upstream));
305 for (i = 0; i < newreq->op.search.res->count; i++) {
306 printf("# record %d\n", i+1);
308 proxy_convert_record(module, newreq->op.search.res->msgs[i]);
315 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "proxy failed for %s\n",
316 ldb_dn_get_linearized(req->op.search.base));
319 return ldb_next_request(module, req);
322 static int proxy_request(struct ldb_module *module, struct ldb_request *req)
324 switch (req->operation) {
327 return proxy_search_bytree(module, req);
330 return ldb_next_request(module, req);
335 static const struct ldb_module_ops proxy_ops = {
337 .request = proxy_request
340 int proxy_module_init(void)
342 return ldb_register_module(&proxy_ops);