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