r11567: Ldb API change patch.
[abartlet/samba.git/.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, struct ldb_result **res)
152 {
153         struct ildb_private *ildb = module->private_data;
154         int count, i;
155         struct ldap_message **ldapres, *msg;
156         char *search_base;
157         NTSTATUS status;
158
159         if (scope == LDB_SCOPE_DEFAULT) {
160                 scope = LDB_SCOPE_SUBTREE;
161         }
162         
163         if (base == NULL) {
164                 if (ildb->rootDSE == NULL) {
165                         ildb_rootdse(module);
166                 }
167                 if (ildb->rootDSE != NULL) {
168                         search_base = talloc_strdup(ildb,
169                                                 ldb_msg_find_string(ildb->rootDSE, 
170                                                                 "defaultNamingContext", ""));
171                 } else {
172                         search_base = talloc_strdup(ildb, "");
173                 }
174         } else {
175                 search_base = ldb_dn_linearize(ildb, base);
176         }
177         if (search_base == NULL) {
178                 ldb_set_errstring(module, talloc_asprintf(module, "Unable to determine baseDN"));
179                 return LDB_ERR_OTHER;
180         }
181         if (tree == NULL) {
182                 ldb_set_errstring(module, talloc_asprintf(module, "Invalid expression parse tree"));
183                 return LDB_ERR_OTHER;
184         }
185
186         (*res) = talloc(ildb, struct ldb_result);
187         if (! *res) {
188                 return LDB_ERR_OTHER;
189         }
190         (*res)->count = 0;
191         (*res)->msgs = NULL;
192
193         status = ildap_search_bytree(ildb->ldap, search_base, scope, tree, attrs, 
194                                             0, &ldapres);
195         talloc_free(search_base);
196         if (!NT_STATUS_IS_OK(status)) {
197                 ildb_map_error(ildb, status);
198                 return LDB_ERR_OTHER;
199         }
200
201         count = ildap_count_entries(ildb->ldap, ldapres);
202         if (count == -1) {
203                 talloc_free(ldapres);
204                 return LDB_ERR_OTHER;
205         }
206
207         if (count == 0) {
208                 talloc_free(ldapres);
209                 return LDB_SUCCESS;
210         }
211
212         (*res)->msgs = talloc_array(*res, struct ldb_message *, count + 1);
213         if (! (*res)->msgs) {
214                 talloc_free(ldapres);
215                 return LDB_ERR_OTHER;
216         }
217
218         (*res)->msgs[0] = NULL;
219
220         /* loop over all messages */
221         for (i=0;i<count;i++) {
222                 struct ldap_SearchResEntry *search;
223
224                 msg = ldapres[i];
225                 search = &msg->r.SearchResultEntry;
226
227                 (*res)->msgs[i] = talloc(*res, struct ldb_message);
228                 if (!(*res)->msgs[i]) {
229                         goto failed;
230                 }
231                 (*res)->msgs[i+1] = NULL;
232
233                 (*res)->msgs[i]->dn = ldb_dn_explode((*res)->msgs[i], search->dn);
234                 if ((*res)->msgs[i]->dn == NULL) {
235                         goto failed;
236                 }
237                 (*res)->msgs[i]->num_elements = search->num_attributes;
238                 (*res)->msgs[i]->elements = talloc_steal((*res)->msgs[i], search->attributes);
239                 (*res)->msgs[i]->private_data = NULL;
240         }
241
242         talloc_free(ldapres);
243
244         (*res)->count = count;
245         return LDB_SUCCESS;
246
247 failed:
248         if (*res) talloc_free(*res);
249         return LDB_ERR_OTHER;
250 }
251
252
253 /*
254   convert a ldb_message structure to a list of ldap_mod structures
255   ready for ildap_add() or ildap_modify()
256 */
257 static struct ldap_mod **ildb_msg_to_mods(struct ldb_context *ldb,
258                                           const struct ldb_message *msg, int use_flags)
259 {
260         struct ldap_mod **mods;
261         unsigned int i;
262         int num_mods = 0;
263
264         /* allocate maximum number of elements needed */
265         mods = talloc_array(ldb, struct ldap_mod *, msg->num_elements+1);
266         if (!mods) {
267                 errno = ENOMEM;
268                 return NULL;
269         }
270         mods[0] = NULL;
271
272         for (i=0;i<msg->num_elements;i++) {
273                 const struct ldb_message_element *el = &msg->elements[i];
274
275                 mods[num_mods] = talloc(ldb, struct ldap_mod);
276                 if (!mods[num_mods]) {
277                         goto failed;
278                 }
279                 mods[num_mods+1] = NULL;
280                 mods[num_mods]->type = 0;
281                 mods[num_mods]->attrib = *el;
282                 if (use_flags) {
283                         switch (el->flags & LDB_FLAG_MOD_MASK) {
284                         case LDB_FLAG_MOD_ADD:
285                                 mods[num_mods]->type = LDAP_MODIFY_ADD;
286                                 break;
287                         case LDB_FLAG_MOD_DELETE:
288                                 mods[num_mods]->type = LDAP_MODIFY_DELETE;
289                                 break;
290                         case LDB_FLAG_MOD_REPLACE:
291                                 mods[num_mods]->type = LDAP_MODIFY_REPLACE;
292                                 break;
293                         }
294                 }
295                 num_mods++;
296         }
297
298         return mods;
299
300 failed:
301         talloc_free(mods);
302         return NULL;
303 }
304
305
306 /*
307   add a record
308 */
309 static int ildb_add(struct ldb_module *module, const struct ldb_message *msg)
310 {
311         struct ldb_context *ldb = module->ldb;
312         struct ildb_private *ildb = module->private_data;
313         struct ldap_mod **mods;
314         char *dn;
315         int ret = 0;
316         NTSTATUS status;
317
318         /* ignore ltdb specials */
319         if (ldb_dn_is_special(msg->dn)) {
320                 return LDB_SUCCESS;
321         }
322
323         mods = ildb_msg_to_mods(ldb, msg, 0);
324         if (mods == NULL) {
325                 return LDB_ERR_OPERATIONS_ERROR;
326         }
327
328         dn = ldb_dn_linearize(mods, msg->dn);
329         if (dn == NULL) {
330                 talloc_free(mods);
331                 return LDB_ERR_INVALID_DN_SYNTAX;
332         }
333
334         status = ildap_add(ildb->ldap, dn, mods);
335         ret = ildb_map_error(ildb, status);
336
337         talloc_free(mods);
338
339         return ret;
340 }
341
342
343 /*
344   modify a record
345 */
346 static int ildb_modify(struct ldb_module *module, const struct ldb_message *msg)
347 {
348         struct ldb_context *ldb = module->ldb;
349         struct ildb_private *ildb = module->private_data;
350         struct ldap_mod **mods;
351         char *dn;
352         int ret = 0;
353         NTSTATUS status;
354
355         /* ignore ltdb specials */
356         if (ldb_dn_is_special(msg->dn)) {
357                 return LDB_SUCCESS;
358         }
359
360         mods = ildb_msg_to_mods(ldb, msg, 1);
361         if (mods == NULL) {
362                 return LDB_ERR_OPERATIONS_ERROR;
363         }
364
365         dn = ldb_dn_linearize(mods, msg->dn);
366         if (dn == NULL) {
367                 talloc_free(mods);
368                 return LDB_ERR_INVALID_DN_SYNTAX;
369         }
370
371         status = ildap_modify(ildb->ldap, dn, mods);
372         ret = ildb_map_error(ildb, status);
373
374         talloc_free(mods);
375
376         return ret;
377 }
378
379 static int ildb_start_trans(struct ldb_module *module)
380 {
381         /* TODO implement a local locking mechanism here */
382
383         return 0;
384 }
385
386 static int ildb_end_trans(struct ldb_module *module)
387 {
388         /* TODO implement a local transaction mechanism here */
389
390         return 0;
391 }
392
393 static int ildb_del_trans(struct ldb_module *module)
394 {
395         /* TODO implement a local locking mechanism here */
396
397         return 0;
398 }
399
400 static int ildb_request(struct ldb_module *module, struct ldb_request *req)
401 {
402         switch (req->operation) {
403
404         case LDB_REQ_SEARCH:
405                 return ildb_search_bytree(module,
406                                           req->op.search.base,
407                                           req->op.search.scope, 
408                                           req->op.search.tree, 
409                                           req->op.search.attrs, 
410                                           req->op.search.res);
411
412         case LDB_REQ_ADD:
413                 return ildb_add(module, req->op.add.message);
414
415         case LDB_REQ_MODIFY:
416                 return ildb_modify(module, req->op.mod.message);
417
418         case LDB_REQ_DELETE:
419                 return ildb_delete(module, req->op.del.dn);
420
421         case LDB_REQ_RENAME:
422                 return ildb_rename(module,
423                                         req->op.rename.olddn,
424                                         req->op.rename.newdn);
425
426         default:
427                 return -1;
428
429         }
430 }
431
432 static const struct ldb_module_ops ildb_ops = {
433         .name              = "ldap",
434         .request           = ildb_request,
435         .start_transaction = ildb_start_trans,
436         .end_transaction   = ildb_end_trans,
437         .del_transaction   = ildb_del_trans
438 };
439
440
441 /*
442   fetch the rootDSE
443 */
444 static void ildb_rootdse(struct ldb_module *module)
445 {
446         struct ildb_private *ildb = module->private_data;
447         struct ldb_result *res = NULL;
448         struct ldb_dn *empty_dn = ldb_dn_new(ildb);
449         int ret;
450         ret = ildb_search_bytree(module, empty_dn, LDB_SCOPE_BASE, 
451                                  ldb_parse_tree(empty_dn, "dn=dc=rootDSE"), 
452                                  NULL, &res);
453         if (ret == LDB_SUCCESS && res->count == 1) {
454                 ildb->rootDSE = talloc_steal(ildb, res->msgs[0]);
455         }
456         if (ret == LDB_SUCCESS) talloc_free(res);
457         talloc_free(empty_dn);
458 }
459
460
461 /*
462   connect to the database
463 */
464 int ildb_connect(struct ldb_context *ldb, const char *url, 
465                  unsigned int flags, const char *options[])
466 {
467         struct ildb_private *ildb = NULL;
468         NTSTATUS status;
469         struct cli_credentials *creds;
470
471         ildb = talloc(ldb, struct ildb_private);
472         if (!ildb) {
473                 ldb_oom(ldb);
474                 goto failed;
475         }
476
477         ildb->rootDSE = NULL;
478         ildb->ldb     = ldb;
479
480         ildb->ldap = ldap_new_connection(ildb, ldb_get_opaque(ldb, "EventContext"));
481         if (!ildb->ldap) {
482                 ldb_oom(ldb);
483                 goto failed;
484         }
485
486         status = ldap_connect(ildb->ldap, url);
487         if (!NT_STATUS_IS_OK(status)) {
488                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to ldap URL '%s' - %s\n",
489                           url, ldap_errstr(ildb->ldap, status));
490                 goto failed;
491         }
492
493         ldb->modules = talloc(ldb, struct ldb_module);
494         if (!ldb->modules) {
495                 ldb_oom(ldb);
496                 goto failed;
497         }
498         ldb->modules->ldb = ldb;
499         ldb->modules->prev = ldb->modules->next = NULL;
500         ldb->modules->private_data = ildb;
501         ldb->modules->ops = &ildb_ops;
502
503         /* caller can optionally setup credentials using the opaque token 'credentials' */
504         creds = talloc_get_type(ldb_get_opaque(ldb, "credentials"), struct cli_credentials);
505         if (creds == NULL) {
506                 struct auth_session_info *session_info = talloc_get_type(ldb_get_opaque(ldb, "sessionInfo"), struct auth_session_info);
507                 if (session_info && session_info->credentials) {
508                         creds = session_info->credentials;
509                 } else {
510                         creds = cmdline_credentials;
511                 }
512         }
513
514         if (creds != NULL && cli_credentials_authentication_requested(creds)) {
515                 status = ldap_bind_sasl(ildb->ldap, creds);
516                 if (!NT_STATUS_IS_OK(status)) {
517                         ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to bind - %s\n",
518                                   ldap_errstr(ildb->ldap, status));
519                         goto failed;
520                 }
521         }
522
523         return 0;
524
525 failed:
526         if (ldb->modules) {
527                 ldb->modules->private_data = NULL;
528         }
529         talloc_free(ildb);
530         return -1;
531 }
532