r26140: Add a new test for searches by distinguieshedName and dn, and
[kai/samba.git] / source / dsdb / samdb / ldb_modules / simple_ldap_map.c
1 /* 
2    ldb database module
3
4    LDAP semantics mapping module
5
6    Copyright (C) Jelmer Vernooij 2005
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 /* 
24    This module relies on ldb_map to do all the real work, but performs
25    some of the trivial mappings between AD semantics and that provided
26    by OpenLDAP and similar servers.
27 */
28
29 #include "includes.h"
30 #include "ldb/include/ldb.h"
31 #include "ldb/include/ldb_private.h"
32 #include "ldb/include/ldb_errors.h"
33 #include "ldb/ldb_map/ldb_map.h"
34
35 #include "librpc/gen_ndr/ndr_misc.h"
36 #include "librpc/ndr/libndr.h"
37
38 struct entryuuid_private {
39         struct ldb_dn **base_dns;
40 };
41
42 static struct ldb_val encode_guid(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
43 {
44         struct GUID guid;
45         NTSTATUS status = GUID_from_string((char *)val->data, &guid);
46         enum ndr_err_code ndr_err;
47         struct ldb_val out = data_blob(NULL, 0);
48
49         if (!NT_STATUS_IS_OK(status)) {
50                 return out;
51         }
52         ndr_err = ndr_push_struct_blob(&out, ctx, &guid,
53                                        (ndr_push_flags_fn_t)ndr_push_GUID);
54         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
55                 return out;
56         }
57
58         return out;
59 }
60
61 static struct ldb_val guid_always_string(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
62 {
63         struct GUID *guid;
64         struct ldb_val out = data_blob(NULL, 0);
65         if (val->length >= 32 && val->data[val->length] == '\0') {
66                 ldb_handler_copy(module->ldb, ctx, val, &out);
67         } else {
68                 enum ndr_err_code ndr_err;
69
70                 guid = talloc(ctx, struct GUID);
71                 if (guid == NULL) {
72                         return out;
73                 }
74                 ndr_err = ndr_pull_struct_blob(val, guid, guid,
75                                                (ndr_pull_flags_fn_t)ndr_pull_GUID);
76                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
77                         talloc_free(guid);
78                         return out;
79                 }
80                 out = data_blob_string_const(GUID_string(ctx, guid));
81                 talloc_free(guid);
82         }
83         return out;
84 }
85
86 static struct ldb_val encode_ns_guid(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
87 {
88         struct GUID guid;
89         NTSTATUS status = NS_GUID_from_string((char *)val->data, &guid);
90         enum ndr_err_code ndr_err;
91         struct ldb_val out = data_blob(NULL, 0);
92
93         if (!NT_STATUS_IS_OK(status)) {
94                 return out;
95         }
96         ndr_err = ndr_push_struct_blob(&out, ctx, &guid,
97                                        (ndr_push_flags_fn_t)ndr_push_GUID);
98         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
99                 return out;
100         }
101
102         return out;
103 }
104
105 static struct ldb_val guid_ns_string(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
106 {
107         struct ldb_val out = data_blob(NULL, 0);
108         if (val->length >= 32 && val->data[val->length] == '\0') {
109                 struct GUID guid;
110                 GUID_from_string((char *)val->data, &guid);
111                 out = data_blob_string_const(NS_GUID_string(ctx, &guid));
112         } else {
113                 enum ndr_err_code ndr_err;
114                 struct GUID *guid_p;
115                 guid_p = talloc(ctx, struct GUID);
116                 if (guid_p == NULL) {
117                         return out;
118                 }
119                 ndr_err = ndr_pull_struct_blob(val, guid_p, guid_p,
120                                                (ndr_pull_flags_fn_t)ndr_pull_GUID);
121                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
122                         talloc_free(guid_p);
123                         return out;
124                 }
125                 out = data_blob_string_const(NS_GUID_string(ctx, guid_p));
126                 talloc_free(guid_p);
127         }
128         return out;
129 }
130
131 /* The backend holds binary sids, so just copy them back */
132 static struct ldb_val val_copy(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
133 {
134         struct ldb_val out = data_blob(NULL, 0);
135         ldb_handler_copy(module->ldb, ctx, val, &out);
136
137         return out;
138 }
139
140 /* Ensure we always convert sids into binary, so the backend doesn't have to know about both forms */
141 static struct ldb_val sid_always_binary(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
142 {
143         struct ldb_val out = data_blob(NULL, 0);
144         const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(module->ldb, "objectSid");
145
146         if (a->syntax->canonicalise_fn(module->ldb, ctx, val, &out) != LDB_SUCCESS) {
147                 return data_blob(NULL, 0);
148         }
149
150         return out;
151 }
152
153 /* Ensure we always convert objectCategory into a DN */
154 static struct ldb_val objectCategory_always_dn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
155 {
156         struct ldb_val out = data_blob(NULL, 0);
157         const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(module->ldb, "objectCategory");
158
159         if (a->syntax->canonicalise_fn(module->ldb, ctx, val, &out) != LDB_SUCCESS) {
160                 return data_blob(NULL, 0);
161         }
162
163         return out;
164 }
165
166 static struct ldb_val normalise_to_signed32(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
167 {
168         long long int signed_ll = strtoll((const char *)val->data, NULL, 10);
169         if (signed_ll >= 0x80000000LL) {
170                 union {
171                         int32_t signed_int;
172                         uint32_t unsigned_int;
173                 } u = {
174                         .unsigned_int = strtoul((const char *)val->data, NULL, 10)
175                 };
176
177                 struct ldb_val out = data_blob_string_const(talloc_asprintf(ctx, "%d", u.signed_int));
178                 return out;
179         }
180         return val_copy(module, ctx, val);
181 }
182
183 static struct ldb_val usn_to_entryCSN(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
184 {
185         struct ldb_val out;
186         unsigned long long usn = strtoull((const char *)val->data, NULL, 10);
187         time_t t = (usn >> 24);
188         out = data_blob_string_const(talloc_asprintf(ctx, "%s#%06x#00#000000", ldb_timestring(ctx, t), (unsigned int)(usn & 0xFFFFFF)));
189         return out;
190 }
191
192 static unsigned long long entryCSN_to_usn_int(TALLOC_CTX *ctx, const struct ldb_val *val) 
193 {
194         char *entryCSN = talloc_strdup(ctx, (const char *)val->data);
195         char *mod_per_sec;
196         time_t t;
197         unsigned long long usn;
198         char *p;
199         if (!entryCSN) {
200                 return 0;
201         }
202         p = strchr(entryCSN, '#');
203         if (!p) {
204                 return 0;
205         }
206         p[0] = '\0';
207         p++;
208         mod_per_sec = p;
209
210         p = strchr(p, '#');
211         if (!p) {
212                 return 0;
213         }
214         p[0] = '\0';
215         p++;
216
217         usn = strtol(mod_per_sec, NULL, 16);
218
219         t = ldb_string_to_time(entryCSN);
220         
221         usn = usn | ((unsigned long long)t <<24);
222         return usn;
223 }
224
225 static struct ldb_val entryCSN_to_usn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
226 {
227         struct ldb_val out;
228         unsigned long long usn = entryCSN_to_usn_int(ctx, val);
229         out = data_blob_string_const(talloc_asprintf(ctx, "%lld", usn));
230         return out;
231 }
232
233 static struct ldb_val usn_to_timestamp(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
234 {
235         struct ldb_val out;
236         unsigned long long usn = strtoull((const char *)val->data, NULL, 10);
237         time_t t = (usn >> 24);
238         out = data_blob_string_const(ldb_timestring(ctx, t));
239         return out;
240 }
241
242 static struct ldb_val timestamp_to_usn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
243 {
244         struct ldb_val out;
245         time_t t;
246         unsigned long long usn;
247
248         t = ldb_string_to_time((const char *)val->data);
249         
250         usn = ((unsigned long long)t <<24);
251
252         out = data_blob_string_const(talloc_asprintf(ctx, "%lld", usn));
253         return out;
254 }
255
256
257 static const struct ldb_map_attribute entryuuid_attributes[] = 
258 {
259         /* objectGUID */
260         {
261                 .local_name = "objectGUID",
262                 .type = MAP_CONVERT,
263                 .u = {
264                         .convert = {
265                                 .remote_name = "entryUUID", 
266                                 .convert_local = guid_always_string,
267                                 .convert_remote = encode_guid,
268                         },
269                 },
270         },
271         /* invocationId */
272         {
273                 .local_name = "invocationId",
274                 .type = MAP_CONVERT,
275                 .u = {
276                         .convert = {
277                                 .remote_name = "invocationId", 
278                                 .convert_local = guid_always_string,
279                                 .convert_remote = encode_guid,
280                         },
281                 },
282         },
283         /* objectSid */
284         {
285                 .local_name = "objectSid",
286                 .type = MAP_CONVERT,
287                 .u = {
288                         .convert = {
289                                 .remote_name = "objectSid", 
290                                 .convert_local = sid_always_binary,
291                                 .convert_remote = val_copy,
292                         },
293                 },
294         },
295         {
296                 .local_name = "name",
297                 .type = MAP_RENAME,
298                 .u = {
299                         .rename = {
300                                  .remote_name = "samba4RDN"
301                          }
302                 }
303         },
304         {
305                 .local_name = "whenCreated",
306                 .type = MAP_RENAME,
307                 .u = {
308                         .rename = {
309                                  .remote_name = "createTimestamp"
310                          }
311                 }
312         },
313         {
314                 .local_name = "whenChanged",
315                 .type = MAP_RENAME,
316                 .u = {
317                         .rename = {
318                                  .remote_name = "modifyTimestamp"
319                          }
320                 }
321         },
322         {
323                 .local_name = "objectClasses",
324                 .type = MAP_RENAME,
325                 .u = {
326                         .rename = {
327                                  .remote_name = "samba4ObjectClasses"
328                          }
329                 }
330         },
331         {
332                 .local_name = "dITContentRules",
333                 .type = MAP_RENAME,
334                 .u = {
335                         .rename = {
336                                  .remote_name = "samba4DITContentRules"
337                          }
338                 }
339         },
340         {
341                 .local_name = "attributeTypes",
342                 .type = MAP_RENAME,
343                 .u = {
344                         .rename = {
345                                  .remote_name = "samba4AttributeTypes"
346                          }
347                 }
348         },
349         {
350                 .local_name = "sambaPassword",
351                 .type = MAP_RENAME,
352                 .u = {
353                         .rename = {
354                                  .remote_name = "userPassword"
355                          }
356                 }
357         },
358         {
359                 .local_name = "objectCategory",
360                 .type = MAP_CONVERT,
361                 .u = {
362                         .convert = {
363                                 .remote_name = "objectCategory", 
364                                 .convert_local = objectCategory_always_dn,
365                                 .convert_remote = val_copy,
366                         },
367                 },
368         },
369         {
370                 .local_name = "distinguishedName",
371                 .type = MAP_RENAME,
372                 .u = {
373                         .rename = {
374                                  .remote_name = "entryDN"
375                          }
376                 }
377         },
378         {
379                 .local_name = "dn",
380                 .type = MAP_RENAME,
381                 .u = {
382                         .rename = {
383                                  .remote_name = "entryDN"
384                          }
385                 }
386         },
387         {
388                 .local_name = "groupType",
389                 .type = MAP_CONVERT,
390                 .u = {
391                         .convert = {
392                                  .remote_name = "groupType",
393                                  .convert_local = normalise_to_signed32,
394                                  .convert_remote = val_copy,
395                          },
396                 }
397         },
398         {
399                 .local_name = "sAMAccountType",
400                 .type = MAP_CONVERT,
401                 .u = {
402                         .convert = {
403                                  .remote_name = "sAMAccountType",
404                                  .convert_local = normalise_to_signed32,
405                                  .convert_remote = val_copy,
406                          },
407                 }
408         },
409         {
410                 .local_name = "usnChanged",
411                 .type = MAP_CONVERT,
412                 .u = {
413                         .convert = {
414                                  .remote_name = "entryCSN",
415                                  .convert_local = usn_to_entryCSN,
416                                  .convert_remote = entryCSN_to_usn
417                          },
418                 },
419         },
420         {
421                 .local_name = "usnCreated",
422                 .type = MAP_CONVERT,
423                 .u = {
424                         .convert = {
425                                  .remote_name = "createTimestamp",
426                                  .convert_local = usn_to_timestamp,
427                                  .convert_remote = timestamp_to_usn,
428                          },
429                 },
430         },
431         {
432                 .local_name = "*",
433                 .type = MAP_KEEP,
434         },
435         {
436                 .local_name = NULL,
437         }
438 };
439
440 /* This objectClass conflicts with builtin classes on OpenLDAP */
441 const struct ldb_map_objectclass entryuuid_objectclasses[] =
442 {
443         {
444                 .local_name = "subSchema",
445                 .remote_name = "samba4SubSchema"
446         },
447         {
448                 .local_name = NULL
449         }
450 };
451
452 /* These things do not show up in wildcard searches in OpenLDAP, but
453  * we need them to show up in the AD-like view */
454 static const char * const entryuuid_wildcard_attributes[] = {
455         "objectGUID", 
456         "whenCreated", 
457         "whenChanged",
458         "usnCreated",
459         "usnChanged",
460         NULL
461 };
462
463 static const struct ldb_map_attribute nsuniqueid_attributes[] = 
464 {
465         /* objectGUID */
466         {
467                 .local_name = "objectGUID",
468                 .type = MAP_CONVERT,
469                 .u = {
470                         .convert = {
471                                 .remote_name = "nsuniqueid", 
472                                 .convert_local = guid_ns_string,
473                                 .convert_remote = encode_ns_guid,
474                         },
475                 },
476         },
477         /* objectSid */ 
478         {
479                 .local_name = "objectSid",
480                 .type = MAP_CONVERT,
481                 .u = {
482                         .convert = {
483                                 .remote_name = "objectSid", 
484                                 .convert_local = sid_always_binary,
485                                 .convert_remote = val_copy,
486                         },
487                 },
488         },
489         {
490                 .local_name = "whenCreated",
491                 .type = MAP_RENAME,
492                 .u = {
493                         .rename = {
494                                  .remote_name = "createTimestamp"
495                          }
496                 }
497         },
498         {
499                 .local_name = "whenChanged",
500                 .type = MAP_RENAME,
501                 .u = {
502                         .rename = {
503                                  .remote_name = "modifyTimestamp"
504                          }
505                 }
506         },
507         {
508                 .local_name = "sambaPassword",
509                 .type = MAP_RENAME,
510                 .u = {
511                         .rename = {
512                                  .remote_name = "userPassword"
513                          }
514                 }
515         },
516         {
517                 .local_name = "objectCategory",
518                 .type = MAP_CONVERT,
519                 .u = {
520                         .convert = {
521                                 .remote_name = "objectCategory", 
522                                 .convert_local = objectCategory_always_dn,
523                                 .convert_remote = val_copy,
524                         },
525                 },
526         },
527         {
528                 .local_name = "distinguishedName",
529                 .type = MAP_RENAME,
530                 .u = {
531                         .rename = {
532                                  .remote_name = "entryDN"
533                          }
534                 }
535         },
536         {
537                 .local_name = "dn",
538                 .type = MAP_RENAME,
539                 .u = {
540                         .rename = {
541                                  .remote_name = "entryDN"
542                          }
543                 }
544         },
545         {
546                 .local_name = "groupType",
547                 .type = MAP_CONVERT,
548                 .u = {
549                         .convert = {
550                                  .remote_name = "groupType",
551                                  .convert_local = normalise_to_signed32,
552                                  .convert_remote = val_copy,
553                          },
554                 }
555         },
556         {
557                 .local_name = "sAMAccountType",
558                 .type = MAP_CONVERT,
559                 .u = {
560                         .convert = {
561                                  .remote_name = "sAMAccountType",
562                                  .convert_local = normalise_to_signed32,
563                                  .convert_remote = val_copy,
564                          },
565                 }
566         },
567         {
568                 .local_name = "usnChanged",
569                 .type = MAP_CONVERT,
570                 .u = {
571                         .convert = {
572                                  .remote_name = "modifyTimestamp",
573                                  .convert_local = usn_to_timestamp,
574                                  .convert_remote = timestamp_to_usn,
575                          },
576                 },
577         },
578         {
579                 .local_name = "usnCreated",
580                 .type = MAP_CONVERT,
581                 .u = {
582                         .convert = {
583                                  .remote_name = "createTimestamp",
584                                  .convert_local = usn_to_timestamp,
585                                  .convert_remote = timestamp_to_usn,
586                          },
587                 },
588         },
589         {
590                 .local_name = "*",
591                 .type = MAP_KEEP,
592         },
593         {
594                 .local_name = NULL,
595         }
596 };
597
598 /* These things do not show up in wildcard searches in OpenLDAP, but
599  * we need them to show up in the AD-like view */
600 static const char * const nsuniqueid_wildcard_attributes[] = {
601         "objectGUID", 
602         "whenCreated", 
603         "whenChanged",
604         "usnCreated",
605         "usnChanged",
606         NULL
607 };
608
609 static int get_remote_rootdse(struct ldb_context *ldb, void *context, 
610                        struct ldb_reply *ares) 
611 {
612         struct entryuuid_private *entryuuid_private;
613         entryuuid_private = talloc_get_type(context,
614                                             struct entryuuid_private);
615         if (ares->type == LDB_REPLY_ENTRY) {
616                 int i;
617                 struct ldb_message_element *el = ldb_msg_find_element(ares->message, "namingContexts");
618                 entryuuid_private->base_dns = talloc_realloc(entryuuid_private, entryuuid_private->base_dns, struct ldb_dn *, 
619                                                              el->num_values + 1);
620                 for (i=0; i < el->num_values; i++) {
621                         if (!entryuuid_private->base_dns) {
622                                 return LDB_ERR_OPERATIONS_ERROR;
623                         }
624                         entryuuid_private->base_dns[i] = ldb_dn_new(entryuuid_private->base_dns, ldb, (const char *)el->values[i].data);
625                         if ( ! ldb_dn_validate(entryuuid_private->base_dns[i])) {
626                                 return LDB_ERR_OPERATIONS_ERROR;
627                         }
628                 }
629                 entryuuid_private->base_dns[i] = NULL;
630         }
631
632         return LDB_SUCCESS;
633 }
634
635 static int find_base_dns(struct ldb_module *module, 
636                           struct entryuuid_private *entryuuid_private) 
637 {
638         int ret;
639         struct ldb_request *req;
640         const char *naming_context_attr[] = {
641                 "namingContexts",
642                 NULL
643         };
644         req = talloc(entryuuid_private, struct ldb_request);
645         if (req == NULL) {
646                 ldb_set_errstring(module->ldb, "Out of Memory");
647                 return LDB_ERR_OPERATIONS_ERROR;
648         }
649
650         req->operation = LDB_SEARCH;
651         req->op.search.base = ldb_dn_new(req, module->ldb, NULL);
652         req->op.search.scope = LDB_SCOPE_BASE;
653
654         req->op.search.tree = ldb_parse_tree(req, "objectClass=*");
655         if (req->op.search.tree == NULL) {
656                 ldb_set_errstring(module->ldb, "Unable to parse search expression");
657                 talloc_free(req);
658                 return LDB_ERR_OPERATIONS_ERROR;
659         }
660
661         req->op.search.attrs = naming_context_attr;
662         req->controls = NULL;
663         req->context = entryuuid_private;
664         req->callback = get_remote_rootdse;
665         ldb_set_timeout(module->ldb, req, 0); /* use default timeout */
666
667         ret = ldb_next_request(module, req);
668         
669         if (ret == LDB_SUCCESS) {
670                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
671         }
672         
673         talloc_free(req);
674         if (ret != LDB_SUCCESS) {
675                 return ret;
676         }
677
678         return LDB_SUCCESS;
679 }
680
681 /* the context init function */
682 static int entryuuid_init(struct ldb_module *module)
683 {
684         int ret;
685         struct map_private *map_private;
686         struct entryuuid_private *entryuuid_private;
687
688         ret = ldb_map_init(module, entryuuid_attributes, entryuuid_objectclasses, entryuuid_wildcard_attributes, NULL);
689         if (ret != LDB_SUCCESS)
690                 return ret;
691
692         map_private = talloc_get_type(module->private_data, struct map_private);
693
694         entryuuid_private = talloc_zero(map_private, struct entryuuid_private);
695         map_private->caller_private = entryuuid_private;
696
697         ret = find_base_dns(module, entryuuid_private);
698
699         return ldb_next_init(module);
700 }
701
702 /* the context init function */
703 static int nsuniqueid_init(struct ldb_module *module)
704 {
705         int ret;
706         struct map_private *map_private;
707         struct entryuuid_private *entryuuid_private;
708
709         ret = ldb_map_init(module, nsuniqueid_attributes, NULL, nsuniqueid_wildcard_attributes, NULL);
710         if (ret != LDB_SUCCESS)
711                 return ret;
712
713         map_private = talloc_get_type(module->private_data, struct map_private);
714
715         entryuuid_private = talloc_zero(map_private, struct entryuuid_private);
716         map_private->caller_private = entryuuid_private;
717
718         ret = find_base_dns(module, entryuuid_private);
719
720         return ldb_next_init(module);
721 }
722
723 static int get_seq(struct ldb_context *ldb, void *context, 
724                    struct ldb_reply *ares) 
725 {
726         unsigned long long *max_seq = (unsigned long long *)context;
727         unsigned long long seq;
728         if (ares->type == LDB_REPLY_ENTRY) {
729                 struct ldb_message_element *el = ldb_msg_find_element(ares->message, "contextCSN");
730                 if (el) {
731                         seq = entryCSN_to_usn_int(ares, &el->values[0]);
732                         *max_seq = MAX(seq, *max_seq);
733                 }
734         }
735
736         return LDB_SUCCESS;
737 }
738
739 static int entryuuid_sequence_number(struct ldb_module *module, struct ldb_request *req)
740 {
741         int i, ret;
742         struct map_private *map_private;
743         struct entryuuid_private *entryuuid_private;
744         unsigned long long max_seq = 0;
745         struct ldb_request *search_req;
746         map_private = talloc_get_type(module->private_data, struct map_private);
747
748         entryuuid_private = talloc_get_type(map_private->caller_private, struct entryuuid_private);
749
750         /* Search the baseDNs for a sequence number */
751         for (i=0; entryuuid_private && 
752                      entryuuid_private->base_dns && 
753                      entryuuid_private->base_dns[i];
754                 i++) {
755                 static const char *contextCSN_attr[] = {
756                         "contextCSN", NULL
757                 };
758                 search_req = talloc(req, struct ldb_request);
759                 if (search_req == NULL) {
760                         ldb_set_errstring(module->ldb, "Out of Memory");
761                         return LDB_ERR_OPERATIONS_ERROR;
762                 }
763                 
764                 search_req->operation = LDB_SEARCH;
765                 search_req->op.search.base = entryuuid_private->base_dns[i];
766                 search_req->op.search.scope = LDB_SCOPE_BASE;
767                 
768                 search_req->op.search.tree = ldb_parse_tree(search_req, "objectClass=*");
769                 if (search_req->op.search.tree == NULL) {
770                         ldb_set_errstring(module->ldb, "Unable to parse search expression");
771                         talloc_free(search_req);
772                         return LDB_ERR_OPERATIONS_ERROR;
773                 }
774                 
775                 search_req->op.search.attrs = contextCSN_attr;
776                 search_req->controls = NULL;
777                 search_req->context = &max_seq;
778                 search_req->callback = get_seq;
779                 ldb_set_timeout(module->ldb, search_req, 0); /* use default timeout */
780                 
781                 ret = ldb_next_request(module, search_req);
782                 
783                 if (ret == LDB_SUCCESS) {
784                         ret = ldb_wait(search_req->handle, LDB_WAIT_ALL);
785                 }
786                 
787                 talloc_free(search_req);
788                 if (ret != LDB_SUCCESS) {
789                         return ret;
790                 }
791         }
792
793         switch (req->op.seq_num.type) {
794         case LDB_SEQ_HIGHEST_SEQ:
795                 req->op.seq_num.seq_num = max_seq;
796                 break;
797         case LDB_SEQ_NEXT:
798                 req->op.seq_num.seq_num = max_seq;
799                 req->op.seq_num.seq_num++;
800                 break;
801         case LDB_SEQ_HIGHEST_TIMESTAMP:
802         {
803                 req->op.seq_num.seq_num = (max_seq >> 24);
804                 break;
805         }
806         }
807         req->op.seq_num.flags = 0;
808         req->op.seq_num.flags |= LDB_SEQ_TIMESTAMP_SEQUENCE;
809         req->op.seq_num.flags |= LDB_SEQ_GLOBAL_SEQUENCE;
810         return LDB_SUCCESS;
811 }
812
813 static struct ldb_module_ops entryuuid_ops = {
814         .name              = "entryuuid",
815         .init_context      = entryuuid_init,
816         .sequence_number   = entryuuid_sequence_number
817 };
818
819 static struct ldb_module_ops nsuniqueid_ops = {
820         .name              = "nsuniqueid",
821         .init_context      = nsuniqueid_init,
822         .sequence_number   = entryuuid_sequence_number
823 };
824
825 /* the init function */
826 int ldb_simple_ldap_map_module_init(void)
827 {
828         int ret;
829         struct ldb_module_ops ops = ldb_map_get_ops();
830         entryuuid_ops.add       = ops.add;
831         entryuuid_ops.modify    = ops.modify;
832         entryuuid_ops.del       = ops.del;
833         entryuuid_ops.rename    = ops.rename;
834         entryuuid_ops.search    = ops.search;
835         entryuuid_ops.wait      = ops.wait;
836         ret = ldb_register_module(&entryuuid_ops);
837
838         if (ret) {
839                 return ret;
840         }
841
842         nsuniqueid_ops.add      = ops.add;
843         nsuniqueid_ops.modify   = ops.modify;
844         nsuniqueid_ops.del      = ops.del;
845         nsuniqueid_ops.rename   = ops.rename;
846         nsuniqueid_ops.search   = ops.search;
847         nsuniqueid_ops.wait     = ops.wait;
848         ret = ldb_register_module(&nsuniqueid_ops);
849
850         return ret;
851 }