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, see <http://www.gnu.org/licenses/>.
25 ldb proxy module. At startup this looks for a record like this:
29 olddn = basedn to proxy in upstream server
30 newdn = basedn in local server
31 username = username to connect to upstream
32 password = password for upstream
34 NOTE: this module is a complete hack at this stage. I am committing it just
35 so others can know how I am investigating mmc support
40 #include "ldb_module.h"
41 #include "auth/credentials/credentials.h"
44 struct ldb_context *upstream;
52 struct ldb_module *module;
53 struct ldb_request *req;
61 load the @PROXYINFO record
63 static int load_proxy_info(struct ldb_module *module)
65 struct ldb_context *ldb = ldb_module_get_ctx(module);
66 struct proxy_data *proxy = talloc_get_type(ldb_module_get_private(module), struct proxy_data);
68 struct ldb_result *res = NULL;
70 const char *olddn, *newdn, *url, *username, *password, *oldstr, *newstr;
71 struct cli_credentials *creds;
73 /* see if we have already loaded it */
74 if (proxy->upstream != NULL) {
78 dn = ldb_dn_new(proxy, ldb, "@PROXYINFO");
82 ret = ldb_search(ldb, proxy, &res, dn, LDB_SCOPE_BASE, NULL, NULL);
84 if (ret != LDB_SUCCESS || res->count != 1) {
85 ldb_debug(ldb, LDB_DEBUG_FATAL, "Can't find @PROXYINFO\n");
89 url = ldb_msg_find_attr_as_string(res->msgs[0], "url", NULL);
90 olddn = ldb_msg_find_attr_as_string(res->msgs[0], "olddn", NULL);
91 newdn = ldb_msg_find_attr_as_string(res->msgs[0], "newdn", NULL);
92 username = ldb_msg_find_attr_as_string(res->msgs[0], "username", NULL);
93 password = ldb_msg_find_attr_as_string(res->msgs[0], "password", NULL);
94 oldstr = ldb_msg_find_attr_as_string(res->msgs[0], "oldstr", NULL);
95 newstr = ldb_msg_find_attr_as_string(res->msgs[0], "newstr", NULL);
97 if (url == NULL || olddn == NULL || newdn == NULL || username == NULL || password == NULL) {
98 ldb_debug(ldb, LDB_DEBUG_FATAL, "Need url, olddn, newdn, oldstr, newstr, username and password in @PROXYINFO\n");
102 proxy->olddn = ldb_dn_new(proxy, ldb, olddn);
103 if (proxy->olddn == NULL) {
104 ldb_debug(ldb, LDB_DEBUG_FATAL, "Failed to explode olddn '%s'\n", olddn);
108 proxy->newdn = ldb_dn_new(proxy, ldb, newdn);
109 if (proxy->newdn == NULL) {
110 ldb_debug(ldb, LDB_DEBUG_FATAL, "Failed to explode newdn '%s'\n", newdn);
114 proxy->upstream = ldb_init(proxy, ldb_get_event_context(ldb));
115 if (proxy->upstream == NULL) {
120 proxy->oldstr = str_list_make(proxy, oldstr, ", ");
121 if (proxy->oldstr == NULL) {
126 proxy->newstr = str_list_make(proxy, newstr, ", ");
127 if (proxy->newstr == NULL) {
132 /* setup credentials for connection */
133 creds = cli_credentials_init(proxy->upstream);
138 cli_credentials_guess(creds, ldb_get_opaque(ldb, "loadparm"));
139 cli_credentials_set_username(creds, username, CRED_SPECIFIED);
140 cli_credentials_set_password(creds, password, CRED_SPECIFIED);
142 ldb_set_opaque(proxy->upstream, "credentials", creds);
144 ret = ldb_connect(proxy->upstream, url, 0, NULL);
146 ldb_debug(ldb, LDB_DEBUG_FATAL, "proxy failed to connect to %s\n", url);
150 ldb_debug(ldb, LDB_DEBUG_TRACE, "proxy connected to %s\n", url);
158 talloc_free(proxy->olddn);
159 talloc_free(proxy->newdn);
160 talloc_free(proxy->upstream);
161 proxy->upstream = NULL;
162 return LDB_ERR_OPERATIONS_ERROR;
167 convert a binary blob
169 static void proxy_convert_blob(TALLOC_CTX *mem_ctx, struct ldb_val *v,
170 const char *oldstr, const char *newstr)
172 int len1, len2, len3;
173 uint8_t *olddata = v->data;
174 char *p = strcasestr((char *)v->data, oldstr);
176 len1 = (p - (char *)v->data);
177 len2 = strlen(newstr);
178 len3 = v->length - (p+strlen(oldstr) - (char *)v->data);
179 v->length = len1+len2+len3;
180 v->data = talloc_size(mem_ctx, v->length);
181 memcpy(v->data, olddata, len1);
182 memcpy(v->data+len1, newstr, len2);
183 memcpy(v->data+len1+len2, olddata + len1 + strlen(oldstr), len3);
187 convert a returned value
189 static void proxy_convert_value(struct proxy_data *proxy, struct ldb_message *msg, struct ldb_val *v)
193 for (i=0;proxy->oldstr[i];i++) {
194 char *p = strcasestr((char *)v->data, proxy->oldstr[i]);
195 if (p == NULL) continue;
196 proxy_convert_blob(msg, v, proxy->oldstr[i], proxy->newstr[i]);
202 convert a returned value
204 static struct ldb_parse_tree *proxy_convert_tree(TALLOC_CTX *mem_ctx,
205 struct proxy_data *proxy,
206 struct ldb_parse_tree *tree)
209 char *expression = ldb_filter_from_tree(mem_ctx, tree);
211 for (i=0;proxy->newstr[i];i++) {
213 char *p = strcasestr(expression, proxy->newstr[i]);
214 if (p == NULL) continue;
215 v.data = (uint8_t *)expression;
216 v.length = strlen(expression)+1;
217 proxy_convert_blob(mem_ctx, &v, proxy->newstr[i], proxy->oldstr[i]);
218 return ldb_parse_tree(mem_ctx, (const char *)v.data);
226 convert a returned record
228 static void proxy_convert_record(struct ldb_context *ldb,
229 struct proxy_data *proxy,
230 struct ldb_message *msg)
234 /* fix the message DN */
235 if (ldb_dn_compare_base(proxy->olddn, msg->dn) == 0) {
236 ldb_dn_remove_base_components(msg->dn, ldb_dn_get_comp_num(proxy->olddn));
237 ldb_dn_add_base(msg->dn, proxy->newdn);
240 /* fix any attributes */
241 for (attr=0;attr<msg->num_elements;attr++) {
242 for (v=0;v<msg->elements[attr].num_values;v++) {
243 proxy_convert_value(proxy, msg, &msg->elements[attr].values[v]);
247 /* fix any DN components */
248 for (attr=0;attr<msg->num_elements;attr++) {
249 for (v=0;v<msg->elements[attr].num_values;v++) {
250 proxy_convert_value(proxy, msg, &msg->elements[attr].values[v]);
255 static int proxy_search_callback(struct ldb_request *req,
256 struct ldb_reply *ares)
258 struct ldb_context *ldb;
259 struct proxy_data *proxy;
260 struct proxy_ctx *ac;
263 ac = talloc_get_type(req->context, struct proxy_ctx);
264 ldb = ldb_module_get_ctx(ac->module);
265 proxy = talloc_get_type(ldb_module_get_private(module), struct proxy_data);
268 return ldb_module_done(ac->req, NULL, NULL,
269 LDB_ERR_OPERATIONS_ERROR);
271 if (ares->error != LDB_SUCCESS) {
272 return ldb_module_done(ac->req, ares->controls,
273 ares->response, ares->error);
276 /* Only entries are interesting, and we only want the olddn */
277 switch (ares->type) {
278 case LDB_REPLY_ENTRY:
283 proxy_convert_record(ldb, proxy, ares->message);
284 ret = ldb_module_send_entry(ac->req, ares->message, ares->controls);
287 case LDB_REPLY_REFERRAL:
289 /* ignore remote referrals */
295 printf("# record %d\n", ac->count+1);
298 return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
306 static int proxy_search_bytree(struct ldb_module *module, struct ldb_request *req)
308 struct ldb_context *ldb;
309 struct proxy_ctx *ac;
310 struct ldb_parse_tree *newtree;
311 struct proxy_data *proxy = talloc_get_type(ldb_module_get_private(module), struct proxy_data);
312 struct ldb_request *newreq;
316 ldb = ldb_module_get_ctx(module);
318 if (req->op.search.base == NULL ||
319 (req->op.search.base->comp_num == 1 &&
320 req->op.search.base->components[0].name[0] == '@')) {
324 if (load_proxy_info(module) != LDB_SUCCESS) {
325 return LDB_ERR_OPERATIONS_ERROR;
328 /* see if the dn is within olddn */
329 if (ldb_dn_compare_base(proxy->newdn, req->op.search.base) != 0) {
333 ac = talloc(req, struct proxy_ctx);
335 return LDB_ERR_OPERATIONS_ERROR;
344 newtree = proxy_convert_tree(ac, proxy, req->op.search.tree);
346 /* convert the basedn of this search */
347 base = ldb_dn_copy(ac, req->op.search.base);
351 ldb_dn_remove_base_components(base, ldb_dn_get_comp_num(proxy->newdn));
352 ldb_dn_add_base(base, proxy->olddn);
354 ldb_debug(ldb, LDB_DEBUG_FATAL, "proxying: '%s' with dn '%s' \n",
355 ldb_filter_from_tree(ac, newreq->op.search.tree), ldb_dn_get_linearized(newreq->op.search.base));
356 for (i = 0; req->op.search.attrs && req->op.search.attrs[i]; i++) {
357 ldb_debug(ldb, LDB_DEBUG_FATAL, "attr: '%s'\n", req->op.search.attrs[i]);
360 ret = ldb_build_search_req_ex(&newreq, ldb, ac,
361 base, req->op.search.scope,
362 newtree, req->op.search.attrs,
364 ac, proxy_search_callback,
367 /* FIXME: warning, need a real event system hooked up for this to work properly,
368 * for now this makes the module *not* ASYNC */
369 ret = ldb_request(proxy->upstream, newreq);
370 if (ret != LDB_SUCCESS) {
371 ldb_set_errstring(ldb, ldb_errstring(proxy->upstream));
373 ret = ldb_wait(newreq->handle, LDB_WAIT_ALL);
374 if (ret != LDB_SUCCESS) {
375 ldb_set_errstring(ldb, ldb_errstring(proxy->upstream));
380 ldb_debug(ldb, LDB_DEBUG_TRACE, "proxy failed for %s\n",
381 ldb_dn_get_linearized(req->op.search.base));
384 return ldb_next_request(module, req);
387 static int proxy_request(struct ldb_module *module, struct ldb_request *req)
389 switch (req->operation) {
392 return proxy_search_bytree(module, req);
395 return ldb_next_request(module, req);
400 _PUBLIC_ const struct ldb_module_ops ldb_proxy_module_ops = {
402 .request = proxy_request