r12743: Remove the ugly way we had to make a second stage init and introduce
[bbaumbach/samba-autobuild/.git] / source4 / lib / ldb / ldb_ildap / ldb_ildap.c
1 /* 
2    ldb database library - ildap backend
3
4    Copyright (C) Andrew Tridgell  2005
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
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 2 of the License, or (at your option) any later version.
14
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.
19
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
23 */
24
25 /*
26   This is a ldb backend for the internal ldap client library in
27   Samba4. By using this backend we are independent of a system ldap
28   library
29 */
30
31
32 #include "includes.h"
33 #include "ldb/include/ldb.h"
34 #include "ldb/include/ldb_private.h"
35 #include "ldb/include/ldb_errors.h"
36 #include "libcli/ldap/ldap.h"
37 #include "libcli/ldap/ldap_client.h"
38 #include "lib/cmdline/popt_common.h"
39 #include "auth/auth.h"
40
41 struct ildb_private {
42         struct ldap_connection *ldap;
43         struct ldb_message *rootDSE;
44         struct ldb_context *ldb;
45 };
46
47
48 /*
49   map an ildap NTSTATUS to a ldb error code
50 */
51 static int ildb_map_error(struct ildb_private *ildb, NTSTATUS status)
52 {
53         if (NT_STATUS_IS_OK(status)) {
54                 return LDB_SUCCESS;
55         }
56         talloc_free(ildb->ldb->err_string);
57         ildb->ldb->err_string = talloc_strdup(ildb, ldap_errstr(ildb->ldap, status));
58         if (NT_STATUS_IS_LDAP(status)) {
59                 return NT_STATUS_LDAP_CODE(status);
60         }
61         return LDB_ERR_OPERATIONS_ERROR;
62 }
63
64 /*
65   rename a record
66 */
67 static int ildb_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
68 {
69         TALLOC_CTX *local_ctx;
70         struct ildb_private *ildb = module->private_data;
71         int ret = 0;
72         char *old_dn;
73         char *newrdn, *parentdn;
74         NTSTATUS status;
75
76         /* ignore ltdb specials */
77         if (ldb_dn_is_special(olddn) || ldb_dn_is_special(newdn)) {
78                 return LDB_SUCCESS;
79         }
80
81         local_ctx = talloc_named(ildb, 0, "ildb_rename local context");
82         if (local_ctx == NULL) {
83                 ret = LDB_ERR_OPERATIONS_ERROR;
84                 goto failed;
85         }
86
87         old_dn = ldb_dn_linearize(local_ctx, olddn);
88         if (old_dn == NULL) {
89                 ret = LDB_ERR_INVALID_DN_SYNTAX;
90                 goto failed;
91         }
92
93         newrdn = talloc_asprintf(local_ctx, "%s=%s",
94                                             newdn->components[0].name,
95                                             ldb_dn_escape_value(ildb, newdn->components[0].value));
96         if (newrdn == NULL) {
97                 ret = LDB_ERR_OPERATIONS_ERROR;
98                 goto failed;
99         }
100
101         parentdn = ldb_dn_linearize(local_ctx, ldb_dn_get_parent(ildb, newdn));
102         if (parentdn == NULL) {
103                 ret = LDB_ERR_INVALID_DN_SYNTAX;
104                 goto failed;
105         }
106
107         status = ildap_rename(ildb->ldap, old_dn, newrdn, parentdn, True);
108         ret = ildb_map_error(ildb, status);
109
110 failed:
111         talloc_free(local_ctx);
112         return ret;
113 }
114
115 /*
116   delete a record
117 */
118 static int ildb_delete(struct ldb_module *module, const struct ldb_dn *dn)
119 {
120         struct ildb_private *ildb = module->private_data;
121         char *del_dn;
122         int ret = 0;
123         NTSTATUS status;
124
125         /* ignore ltdb specials */
126         if (ldb_dn_is_special(dn)) {
127                 return LDB_SUCCESS;
128         }
129         
130         del_dn = ldb_dn_linearize(ildb, dn);
131         if (del_dn == NULL) {
132                 return LDB_ERR_INVALID_DN_SYNTAX;
133         }
134
135         status = ildap_delete(ildb->ldap, del_dn);
136         ret = ildb_map_error(ildb, status);
137
138         talloc_free(del_dn);
139
140         return ret;
141 }
142
143
144 static void ildb_rootdse(struct ldb_module *module);
145
146 /*
147   search for matching records using a ldb_parse_tree
148 */
149 static int ildb_search_bytree(struct ldb_module *module, const struct ldb_dn *base,
150                               enum ldb_scope scope, struct ldb_parse_tree *tree,
151                               const char * const *attrs,
152                               struct ldb_control **control_req,
153                               struct ldb_result **res)
154 {
155         struct ildb_private *ildb = module->private_data;
156         int count, i;
157         struct ldap_message **ldapres, *msg;
158         struct ldap_Control **controls = NULL;
159         char *search_base;
160         NTSTATUS status;
161
162         if (scope == LDB_SCOPE_DEFAULT) {
163                 scope = LDB_SCOPE_SUBTREE;
164         }
165         
166         if (base == NULL) {
167                 if (ildb->rootDSE == NULL) {
168                         ildb_rootdse(module);
169                 }
170                 if (ildb->rootDSE != NULL) {
171                         search_base = talloc_strdup(ildb,
172                                                 ldb_msg_find_string(ildb->rootDSE, 
173                                                                 "defaultNamingContext", ""));
174                 } else {
175                         search_base = talloc_strdup(ildb, "");
176                 }
177         } else {
178                 search_base = ldb_dn_linearize(ildb, base);
179         }
180         if (search_base == NULL) {
181                 ldb_set_errstring(module, talloc_asprintf(module, "Unable to determine baseDN"));
182                 return LDB_ERR_OTHER;
183         }
184         if (tree == NULL) {
185                 ldb_set_errstring(module, talloc_asprintf(module, "Invalid expression parse tree"));
186                 return LDB_ERR_OTHER;
187         }
188
189         (*res) = talloc(ildb, struct ldb_result);
190         if (! *res) {
191                 return LDB_ERR_OTHER;
192         }
193         (*res)->count = 0;
194         (*res)->msgs = NULL;
195         (*res)->controls = NULL;
196
197         status = ildap_search_bytree(ildb->ldap, search_base, scope, tree, attrs, 0,
198                                      (struct ldap_Control **)control_req,
199                                      &controls,
200                                      &ldapres);
201         talloc_free(search_base);
202         if (!NT_STATUS_IS_OK(status)) {
203                 ildb_map_error(ildb, status);
204                 return LDB_ERR_OTHER;
205         }
206
207         count = ildap_count_entries(ildb->ldap, ldapres);
208         if (count == -1) {
209                 talloc_free(ldapres);
210                 return LDB_ERR_OTHER;
211         }
212
213         if (count == 0) {
214                 talloc_free(ldapres);
215                 return LDB_SUCCESS;
216         }
217
218         (*res)->msgs = talloc_array(*res, struct ldb_message *, count + 1);
219         if (! (*res)->msgs) {
220                 talloc_free(ldapres);
221                 return LDB_ERR_OTHER;
222         }
223
224         (*res)->msgs[0] = NULL;
225
226         /* loop over all messages */
227         for (i=0;i<count;i++) {
228                 struct ldap_SearchResEntry *search;
229
230                 msg = ldapres[i];
231                 search = &msg->r.SearchResultEntry;
232
233                 (*res)->msgs[i] = talloc((*res)->msgs, struct ldb_message);
234                 if (!(*res)->msgs[i]) {
235                         goto failed;
236                 }
237                 (*res)->msgs[i+1] = NULL;
238
239                 (*res)->msgs[i]->dn = ldb_dn_explode_or_special((*res)->msgs[i], search->dn);
240                 if ((*res)->msgs[i]->dn == NULL) {
241                         goto failed;
242                 }
243                 (*res)->msgs[i]->num_elements = search->num_attributes;
244                 (*res)->msgs[i]->elements = talloc_steal((*res)->msgs[i], search->attributes);
245                 (*res)->msgs[i]->private_data = NULL;
246         }
247
248         talloc_free(ldapres);
249
250         (*res)->count = count;
251
252         if (controls) {
253                 (*res)->controls = (struct ldb_control **)talloc_steal(*res, controls);
254         }
255
256         return LDB_SUCCESS;
257
258 failed:
259         if (*res) talloc_free(*res);
260         return LDB_ERR_OTHER;
261 }
262
263
264 /*
265   convert a ldb_message structure to a list of ldap_mod structures
266   ready for ildap_add() or ildap_modify()
267 */
268 static struct ldap_mod **ildb_msg_to_mods(struct ldb_context *ldb,
269                                           const struct ldb_message *msg, int use_flags)
270 {
271         struct ldap_mod **mods;
272         unsigned int i;
273         int num_mods = 0;
274
275         /* allocate maximum number of elements needed */
276         mods = talloc_array(ldb, struct ldap_mod *, msg->num_elements+1);
277         if (!mods) {
278                 errno = ENOMEM;
279                 return NULL;
280         }
281         mods[0] = NULL;
282
283         for (i=0;i<msg->num_elements;i++) {
284                 const struct ldb_message_element *el = &msg->elements[i];
285
286                 mods[num_mods] = talloc(ldb, struct ldap_mod);
287                 if (!mods[num_mods]) {
288                         goto failed;
289                 }
290                 mods[num_mods+1] = NULL;
291                 mods[num_mods]->type = 0;
292                 mods[num_mods]->attrib = *el;
293                 if (use_flags) {
294                         switch (el->flags & LDB_FLAG_MOD_MASK) {
295                         case LDB_FLAG_MOD_ADD:
296                                 mods[num_mods]->type = LDAP_MODIFY_ADD;
297                                 break;
298                         case LDB_FLAG_MOD_DELETE:
299                                 mods[num_mods]->type = LDAP_MODIFY_DELETE;
300                                 break;
301                         case LDB_FLAG_MOD_REPLACE:
302                                 mods[num_mods]->type = LDAP_MODIFY_REPLACE;
303                                 break;
304                         }
305                 }
306                 num_mods++;
307         }
308
309         return mods;
310
311 failed:
312         talloc_free(mods);
313         return NULL;
314 }
315
316
317 /*
318   add a record
319 */
320 static int ildb_add(struct ldb_module *module, const struct ldb_message *msg)
321 {
322         struct ldb_context *ldb = module->ldb;
323         struct ildb_private *ildb = module->private_data;
324         struct ldap_mod **mods;
325         char *dn;
326         int ret = 0;
327         NTSTATUS status;
328
329         /* ignore ltdb specials */
330         if (ldb_dn_is_special(msg->dn)) {
331                 return LDB_SUCCESS;
332         }
333
334         mods = ildb_msg_to_mods(ldb, msg, 0);
335         if (mods == NULL) {
336                 return LDB_ERR_OPERATIONS_ERROR;
337         }
338
339         dn = ldb_dn_linearize(mods, msg->dn);
340         if (dn == NULL) {
341                 talloc_free(mods);
342                 return LDB_ERR_INVALID_DN_SYNTAX;
343         }
344
345         status = ildap_add(ildb->ldap, dn, mods);
346         ret = ildb_map_error(ildb, status);
347
348         talloc_free(mods);
349
350         return ret;
351 }
352
353
354 /*
355   modify a record
356 */
357 static int ildb_modify(struct ldb_module *module, const struct ldb_message *msg)
358 {
359         struct ldb_context *ldb = module->ldb;
360         struct ildb_private *ildb = module->private_data;
361         struct ldap_mod **mods;
362         char *dn;
363         int ret = 0;
364         NTSTATUS status;
365
366         /* ignore ltdb specials */
367         if (ldb_dn_is_special(msg->dn)) {
368                 return LDB_SUCCESS;
369         }
370
371         mods = ildb_msg_to_mods(ldb, msg, 1);
372         if (mods == NULL) {
373                 return LDB_ERR_OPERATIONS_ERROR;
374         }
375
376         dn = ldb_dn_linearize(mods, msg->dn);
377         if (dn == NULL) {
378                 talloc_free(mods);
379                 return LDB_ERR_INVALID_DN_SYNTAX;
380         }
381
382         status = ildap_modify(ildb->ldap, dn, mods);
383         ret = ildb_map_error(ildb, status);
384
385         talloc_free(mods);
386
387         return ret;
388 }
389
390 static int ildb_start_trans(struct ldb_module *module)
391 {
392         /* TODO implement a local locking mechanism here */
393
394         return 0;
395 }
396
397 static int ildb_end_trans(struct ldb_module *module)
398 {
399         /* TODO implement a local transaction mechanism here */
400
401         return 0;
402 }
403
404 static int ildb_del_trans(struct ldb_module *module)
405 {
406         /* TODO implement a local locking mechanism here */
407
408         return 0;
409 }
410
411 static int ildb_request(struct ldb_module *module, struct ldb_request *req)
412 {
413         switch (req->operation) {
414
415         case LDB_REQ_SEARCH:
416                 return ildb_search_bytree(module,
417                                           req->op.search.base,
418                                           req->op.search.scope, 
419                                           req->op.search.tree, 
420                                           req->op.search.attrs, 
421                                           req->controls,
422                                           &req->op.search.res);
423
424         case LDB_REQ_ADD:
425                 return ildb_add(module, req->op.add.message);
426
427         case LDB_REQ_MODIFY:
428                 return ildb_modify(module, req->op.mod.message);
429
430         case LDB_REQ_DELETE:
431                 return ildb_delete(module, req->op.del.dn);
432
433         case LDB_REQ_RENAME:
434                 return ildb_rename(module,
435                                         req->op.rename.olddn,
436                                         req->op.rename.newdn);
437
438         default:
439                 return -1;
440
441         }
442 }
443
444 static int ildb_init_2(struct ldb_module *module)
445 {
446         return LDB_SUCCESS;
447 }
448
449 static const struct ldb_module_ops ildb_ops = {
450         .name              = "ldap",
451         .request           = ildb_request,
452         .start_transaction = ildb_start_trans,
453         .end_transaction   = ildb_end_trans,
454         .del_transaction   = ildb_del_trans,
455         .second_stage_init = ildb_init_2
456 };
457
458
459 /*
460   fetch the rootDSE
461 */
462 static void ildb_rootdse(struct ldb_module *module)
463 {
464         struct ildb_private *ildb = module->private_data;
465         struct ldb_result *res = NULL;
466         struct ldb_dn *empty_dn = ldb_dn_new(ildb);
467         int ret;
468         ret = ildb_search_bytree(module, empty_dn, LDB_SCOPE_BASE, 
469                                  ldb_parse_tree(empty_dn, "dn=dc=rootDSE"), 
470                                  NULL, NULL, &res);
471         if (ret == LDB_SUCCESS && res->count == 1) {
472                 ildb->rootDSE = talloc_steal(ildb, res->msgs[0]);
473         }
474         if (ret == LDB_SUCCESS) talloc_free(res);
475         talloc_free(empty_dn);
476 }
477
478
479 /*
480   connect to the database
481 */
482 int ildb_connect(struct ldb_context *ldb, const char *url, 
483                  unsigned int flags, const char *options[])
484 {
485         struct ildb_private *ildb = NULL;
486         NTSTATUS status;
487         struct cli_credentials *creds;
488
489         ildb = talloc(ldb, struct ildb_private);
490         if (!ildb) {
491                 ldb_oom(ldb);
492                 goto failed;
493         }
494
495         ildb->rootDSE = NULL;
496         ildb->ldb     = ldb;
497
498         ildb->ldap = ldap_new_connection(ildb, ldb_get_opaque(ldb, "EventContext"));
499         if (!ildb->ldap) {
500                 ldb_oom(ldb);
501                 goto failed;
502         }
503
504         status = ldap_connect(ildb->ldap, url);
505         if (!NT_STATUS_IS_OK(status)) {
506                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s\n",
507                           url, ldap_errstr(ildb->ldap, status));
508                 goto failed;
509         }
510
511         ldb->modules = talloc(ldb, struct ldb_module);
512         if (!ldb->modules) {
513                 ldb_oom(ldb);
514                 goto failed;
515         }
516         ldb->modules->ldb = ldb;
517         ldb->modules->prev = ldb->modules->next = NULL;
518         ldb->modules->private_data = ildb;
519         ldb->modules->ops = &ildb_ops;
520
521         /* caller can optionally setup credentials using the opaque token 'credentials' */
522         creds = talloc_get_type(ldb_get_opaque(ldb, "credentials"), struct cli_credentials);
523         if (creds == NULL) {
524                 struct auth_session_info *session_info = talloc_get_type(ldb_get_opaque(ldb, "sessionInfo"), struct auth_session_info);
525                 if (session_info) {
526                         creds = session_info->credentials;
527                 }
528         }
529
530         if (creds != NULL && cli_credentials_authentication_requested(creds)) {
531                 const char *bind_dn = cli_credentials_get_bind_dn(creds);
532                 if (bind_dn) {
533                         const char *password = cli_credentials_get_password(creds);
534                         status = ldap_bind_simple(ildb->ldap, bind_dn, password);
535                         if (!NT_STATUS_IS_OK(status)) {
536                                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
537                                           ldap_errstr(ildb->ldap, status));
538                                 goto failed;
539                         }
540                 } else {
541                         status = ldap_bind_sasl(ildb->ldap, creds);
542                         if (!NT_STATUS_IS_OK(status)) {
543                                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
544                                           ldap_errstr(ildb->ldap, status));
545                                 goto failed;
546                         }
547                 }
548         }
549
550         return 0;
551
552 failed:
553         if (ldb->modules) {
554                 ldb->modules->private_data = NULL;
555         }
556         talloc_free(ildb);
557         return -1;
558 }
559