Avoid the use of extensibleObject in ldap mapping backend.
[kai/samba.git] / source4 / 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, NULL, &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, NULL, 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, NULL, &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, NULL, 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_dn *dn;
157         struct ldb_val out = data_blob(NULL, 0);
158         const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(module->ldb, "objectCategory");
159
160         dn = ldb_dn_new(ctx, module->ldb, val->data);
161         if (dn && ldb_dn_validate(dn)) {
162                 talloc_free(dn);
163                 return val_copy(module, ctx, val);
164         }
165         talloc_free(dn);
166
167         if (a->syntax->canonicalise_fn(module->ldb, ctx, val, &out) != LDB_SUCCESS) {
168                 return data_blob(NULL, 0);
169         }
170
171         return out;
172 }
173
174 static struct ldb_val normalise_to_signed32(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
175 {
176         long long int signed_ll = strtoll((const char *)val->data, NULL, 10);
177         if (signed_ll >= 0x80000000LL) {
178                 union {
179                         int32_t signed_int;
180                         uint32_t unsigned_int;
181                 } u = {
182                         .unsigned_int = strtoul((const char *)val->data, NULL, 10)
183                 };
184
185                 struct ldb_val out = data_blob_string_const(talloc_asprintf(ctx, "%d", u.signed_int));
186                 return out;
187         }
188         return val_copy(module, ctx, val);
189 }
190
191 static struct ldb_val usn_to_entryCSN(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
192 {
193         struct ldb_val out;
194         unsigned long long usn = strtoull((const char *)val->data, NULL, 10);
195         time_t t = (usn >> 24);
196         out = data_blob_string_const(talloc_asprintf(ctx, "%s#%06x#00#000000", ldb_timestring(ctx, t), (unsigned int)(usn & 0xFFFFFF)));
197         return out;
198 }
199
200 static unsigned long long entryCSN_to_usn_int(TALLOC_CTX *ctx, const struct ldb_val *val) 
201 {
202         char *entryCSN = talloc_strdup(ctx, (const char *)val->data);
203         char *mod_per_sec;
204         time_t t;
205         unsigned long long usn;
206         char *p;
207         if (!entryCSN) {
208                 return 0;
209         }
210         p = strchr(entryCSN, '#');
211         if (!p) {
212                 return 0;
213         }
214         p[0] = '\0';
215         p++;
216         mod_per_sec = p;
217
218         p = strchr(p, '#');
219         if (!p) {
220                 return 0;
221         }
222         p[0] = '\0';
223         p++;
224
225         usn = strtol(mod_per_sec, NULL, 16);
226
227         t = ldb_string_to_time(entryCSN);
228         
229         usn = usn | ((unsigned long long)t <<24);
230         return usn;
231 }
232
233 static struct ldb_val entryCSN_to_usn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
234 {
235         struct ldb_val out;
236         unsigned long long usn = entryCSN_to_usn_int(ctx, val);
237         out = data_blob_string_const(talloc_asprintf(ctx, "%lld", usn));
238         return out;
239 }
240
241 static struct ldb_val usn_to_timestamp(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
242 {
243         struct ldb_val out;
244         unsigned long long usn = strtoull((const char *)val->data, NULL, 10);
245         time_t t = (usn >> 24);
246         out = data_blob_string_const(ldb_timestring(ctx, t));
247         return out;
248 }
249
250 static struct ldb_val timestamp_to_usn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
251 {
252         struct ldb_val out;
253         time_t t;
254         unsigned long long usn;
255
256         t = ldb_string_to_time((const char *)val->data);
257         
258         usn = ((unsigned long long)t <<24);
259
260         out = data_blob_string_const(talloc_asprintf(ctx, "%lld", usn));
261         return out;
262 }
263
264
265 static const struct ldb_map_attribute entryuuid_attributes[] = 
266 {
267         /* objectGUID */
268         {
269                 .local_name = "objectGUID",
270                 .type = MAP_CONVERT,
271                 .u = {
272                         .convert = {
273                                 .remote_name = "entryUUID", 
274                                 .convert_local = guid_always_string,
275                                 .convert_remote = encode_guid,
276                         },
277                 },
278         },
279         /* invocationId */
280         {
281                 .local_name = "invocationId",
282                 .type = MAP_CONVERT,
283                 .u = {
284                         .convert = {
285                                 .remote_name = "invocationId", 
286                                 .convert_local = guid_always_string,
287                                 .convert_remote = encode_guid,
288                         },
289                 },
290         },
291         /* objectSid */
292         {
293                 .local_name = "objectSid",
294                 .type = MAP_CONVERT,
295                 .u = {
296                         .convert = {
297                                 .remote_name = "objectSid", 
298                                 .convert_local = sid_always_binary,
299                                 .convert_remote = val_copy,
300                         },
301                 },
302         },
303         {
304                 .local_name = "name",
305                 .type = MAP_RENAME,
306                 .u = {
307                         .rename = {
308                                  .remote_name = "samba4RDN"
309                          }
310                 }
311         },
312         {
313                 .local_name = "whenCreated",
314                 .type = MAP_RENAME,
315                 .u = {
316                         .rename = {
317                                  .remote_name = "createTimestamp"
318                          }
319                 }
320         },
321         {
322                 .local_name = "whenChanged",
323                 .type = MAP_RENAME,
324                 .u = {
325                         .rename = {
326                                  .remote_name = "modifyTimestamp"
327                          }
328                 }
329         },
330         {
331                 .local_name = "objectClasses",
332                 .type = MAP_RENAME,
333                 .u = {
334                         .rename = {
335                                  .remote_name = "samba4ObjectClasses"
336                          }
337                 }
338         },
339         {
340                 .local_name = "dITContentRules",
341                 .type = MAP_RENAME,
342                 .u = {
343                         .rename = {
344                                  .remote_name = "samba4DITContentRules"
345                          }
346                 }
347         },
348         {
349                 .local_name = "attributeTypes",
350                 .type = MAP_RENAME,
351                 .u = {
352                         .rename = {
353                                  .remote_name = "samba4AttributeTypes"
354                          }
355                 }
356         },
357         {
358                 .local_name = "sambaPassword",
359                 .type = MAP_RENAME,
360                 .u = {
361                         .rename = {
362                                  .remote_name = "userPassword"
363                          }
364                 }
365         },
366         {
367                 .local_name = "objectCategory",
368                 .type = MAP_CONVERT,
369                 .u = {
370                         .convert = {
371                                 .remote_name = "objectCategory", 
372                                 .convert_local = objectCategory_always_dn,
373                                 .convert_remote = val_copy,
374                         },
375                 },
376         },
377         {
378                 .local_name = "distinguishedName",
379                 .type = MAP_RENAME,
380                 .u = {
381                         .rename = {
382                                  .remote_name = "entryDN"
383                          }
384                 }
385         },
386         {
387                 .local_name = "groupType",
388                 .type = MAP_CONVERT,
389                 .u = {
390                         .convert = {
391                                  .remote_name = "groupType",
392                                  .convert_local = normalise_to_signed32,
393                                  .convert_remote = val_copy,
394                          },
395                 }
396         },
397         {
398                 .local_name = "sAMAccountType",
399                 .type = MAP_CONVERT,
400                 .u = {
401                         .convert = {
402                                  .remote_name = "sAMAccountType",
403                                  .convert_local = normalise_to_signed32,
404                                  .convert_remote = val_copy,
405                          },
406                 }
407         },
408         {
409                 .local_name = "usnChanged",
410                 .type = MAP_CONVERT,
411                 .u = {
412                         .convert = {
413                                  .remote_name = "entryCSN",
414                                  .convert_local = usn_to_entryCSN,
415                                  .convert_remote = entryCSN_to_usn
416                          },
417                 },
418         },
419         {
420                 .local_name = "usnCreated",
421                 .type = MAP_CONVERT,
422                 .u = {
423                         .convert = {
424                                  .remote_name = "createTimestamp",
425                                  .convert_local = usn_to_timestamp,
426                                  .convert_remote = timestamp_to_usn,
427                          },
428                 },
429         },
430         {
431                 .local_name = "*",
432                 .type = MAP_KEEP,
433         },
434         {
435                 .local_name = NULL,
436         }
437 };
438
439 /* This objectClass conflicts with builtin classes on OpenLDAP */
440 const struct ldb_map_objectclass entryuuid_objectclasses[] =
441 {
442         {
443                 .local_name = "subSchema",
444                 .remote_name = "samba4SubSchema"
445         },
446         {
447                 .local_name = NULL
448         }
449 };
450
451 /* These things do not show up in wildcard searches in OpenLDAP, but
452  * we need them to show up in the AD-like view */
453 static const char * const entryuuid_wildcard_attributes[] = {
454         "objectGUID", 
455         "whenCreated", 
456         "whenChanged",
457         "usnCreated",
458         "usnChanged",
459         "memberOf",
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 = "groupType",
538                 .type = MAP_CONVERT,
539                 .u = {
540                         .convert = {
541                                  .remote_name = "groupType",
542                                  .convert_local = normalise_to_signed32,
543                                  .convert_remote = val_copy,
544                          },
545                 }
546         },
547         {
548                 .local_name = "sAMAccountType",
549                 .type = MAP_CONVERT,
550                 .u = {
551                         .convert = {
552                                  .remote_name = "sAMAccountType",
553                                  .convert_local = normalise_to_signed32,
554                                  .convert_remote = val_copy,
555                          },
556                 }
557         },
558         {
559                 .local_name = "usnChanged",
560                 .type = MAP_CONVERT,
561                 .u = {
562                         .convert = {
563                                  .remote_name = "modifyTimestamp",
564                                  .convert_local = usn_to_timestamp,
565                                  .convert_remote = timestamp_to_usn,
566                          },
567                 },
568         },
569         {
570                 .local_name = "usnCreated",
571                 .type = MAP_CONVERT,
572                 .u = {
573                         .convert = {
574                                  .remote_name = "createTimestamp",
575                                  .convert_local = usn_to_timestamp,
576                                  .convert_remote = timestamp_to_usn,
577                          },
578                 },
579         },
580         {
581                 .local_name = "*",
582                 .type = MAP_KEEP,
583         },
584         {
585                 .local_name = NULL,
586         }
587 };
588
589 /* These things do not show up in wildcard searches in OpenLDAP, but
590  * we need them to show up in the AD-like view */
591 static const char * const nsuniqueid_wildcard_attributes[] = {
592         "objectGUID", 
593         "whenCreated", 
594         "whenChanged",
595         "usnCreated",
596         "usnChanged",
597         NULL
598 };
599
600 static int get_remote_rootdse(struct ldb_context *ldb, void *context, 
601                        struct ldb_reply *ares) 
602 {
603         struct entryuuid_private *entryuuid_private;
604         entryuuid_private = talloc_get_type(context,
605                                             struct entryuuid_private);
606         if (ares->type == LDB_REPLY_ENTRY) {
607                 int i;
608                 struct ldb_message_element *el = ldb_msg_find_element(ares->message, "namingContexts");
609                 entryuuid_private->base_dns = talloc_realloc(entryuuid_private, entryuuid_private->base_dns, struct ldb_dn *, 
610                                                              el->num_values + 1);
611                 for (i=0; i < el->num_values; i++) {
612                         if (!entryuuid_private->base_dns) {
613                                 return LDB_ERR_OPERATIONS_ERROR;
614                         }
615                         entryuuid_private->base_dns[i] = ldb_dn_new(entryuuid_private->base_dns, ldb, (const char *)el->values[i].data);
616                         if ( ! ldb_dn_validate(entryuuid_private->base_dns[i])) {
617                                 return LDB_ERR_OPERATIONS_ERROR;
618                         }
619                 }
620                 entryuuid_private->base_dns[i] = NULL;
621         }
622
623         return LDB_SUCCESS;
624 }
625
626 static int find_base_dns(struct ldb_module *module, 
627                           struct entryuuid_private *entryuuid_private) 
628 {
629         int ret;
630         struct ldb_request *req;
631         const char *naming_context_attr[] = {
632                 "namingContexts",
633                 NULL
634         };
635         req = talloc(entryuuid_private, struct ldb_request);
636         if (req == NULL) {
637                 ldb_set_errstring(module->ldb, "Out of Memory");
638                 return LDB_ERR_OPERATIONS_ERROR;
639         }
640
641         req->operation = LDB_SEARCH;
642         req->op.search.base = ldb_dn_new(req, module->ldb, NULL);
643         req->op.search.scope = LDB_SCOPE_BASE;
644
645         req->op.search.tree = ldb_parse_tree(req, "objectClass=*");
646         if (req->op.search.tree == NULL) {
647                 ldb_set_errstring(module->ldb, "Unable to parse search expression");
648                 talloc_free(req);
649                 return LDB_ERR_OPERATIONS_ERROR;
650         }
651
652         req->op.search.attrs = naming_context_attr;
653         req->controls = NULL;
654         req->context = entryuuid_private;
655         req->callback = get_remote_rootdse;
656         ldb_set_timeout(module->ldb, req, 0); /* use default timeout */
657
658         ret = ldb_next_request(module, req);
659         
660         if (ret == LDB_SUCCESS) {
661                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
662         }
663         
664         talloc_free(req);
665         if (ret != LDB_SUCCESS) {
666                 return ret;
667         }
668
669         return LDB_SUCCESS;
670 }
671
672 /* the context init function */
673 static int entryuuid_init(struct ldb_module *module)
674 {
675         int ret;
676         struct map_private *map_private;
677         struct entryuuid_private *entryuuid_private;
678
679         ret = ldb_map_init(module, entryuuid_attributes, entryuuid_objectclasses, entryuuid_wildcard_attributes, "samba4Top", NULL);
680         if (ret != LDB_SUCCESS)
681                 return ret;
682
683         map_private = talloc_get_type(module->private_data, struct map_private);
684
685         entryuuid_private = talloc_zero(map_private, struct entryuuid_private);
686         map_private->caller_private = entryuuid_private;
687
688         ret = find_base_dns(module, entryuuid_private);
689
690         return ldb_next_init(module);
691 }
692
693 /* the context init function */
694 static int nsuniqueid_init(struct ldb_module *module)
695 {
696         int ret;
697         struct map_private *map_private;
698         struct entryuuid_private *entryuuid_private;
699
700         ret = ldb_map_init(module, nsuniqueid_attributes, NULL, nsuniqueid_wildcard_attributes, "samba4Top", NULL);
701         if (ret != LDB_SUCCESS)
702                 return ret;
703
704         map_private = talloc_get_type(module->private_data, struct map_private);
705
706         entryuuid_private = talloc_zero(map_private, struct entryuuid_private);
707         map_private->caller_private = entryuuid_private;
708
709         ret = find_base_dns(module, entryuuid_private);
710
711         return ldb_next_init(module);
712 }
713
714 static int get_seq(struct ldb_context *ldb, void *context, 
715                    struct ldb_reply *ares) 
716 {
717         unsigned long long *max_seq = (unsigned long long *)context;
718         unsigned long long seq;
719         if (ares->type == LDB_REPLY_ENTRY) {
720                 struct ldb_message_element *el = ldb_msg_find_element(ares->message, "contextCSN");
721                 if (el) {
722                         seq = entryCSN_to_usn_int(ares, &el->values[0]);
723                         *max_seq = MAX(seq, *max_seq);
724                 }
725         }
726
727         return LDB_SUCCESS;
728 }
729
730 static int entryuuid_sequence_number(struct ldb_module *module, struct ldb_request *req)
731 {
732         int i, ret;
733         struct map_private *map_private;
734         struct entryuuid_private *entryuuid_private;
735         unsigned long long max_seq = 0;
736         struct ldb_request *search_req;
737         map_private = talloc_get_type(module->private_data, struct map_private);
738
739         entryuuid_private = talloc_get_type(map_private->caller_private, struct entryuuid_private);
740
741         /* Search the baseDNs for a sequence number */
742         for (i=0; entryuuid_private && 
743                      entryuuid_private->base_dns && 
744                      entryuuid_private->base_dns[i];
745                 i++) {
746                 static const char *contextCSN_attr[] = {
747                         "contextCSN", NULL
748                 };
749                 search_req = talloc(req, struct ldb_request);
750                 if (search_req == NULL) {
751                         ldb_set_errstring(module->ldb, "Out of Memory");
752                         return LDB_ERR_OPERATIONS_ERROR;
753                 }
754                 
755                 search_req->operation = LDB_SEARCH;
756                 search_req->op.search.base = entryuuid_private->base_dns[i];
757                 search_req->op.search.scope = LDB_SCOPE_BASE;
758                 
759                 search_req->op.search.tree = ldb_parse_tree(search_req, "objectClass=*");
760                 if (search_req->op.search.tree == NULL) {
761                         ldb_set_errstring(module->ldb, "Unable to parse search expression");
762                         talloc_free(search_req);
763                         return LDB_ERR_OPERATIONS_ERROR;
764                 }
765                 
766                 search_req->op.search.attrs = contextCSN_attr;
767                 search_req->controls = NULL;
768                 search_req->context = &max_seq;
769                 search_req->callback = get_seq;
770                 ldb_set_timeout(module->ldb, search_req, 0); /* use default timeout */
771                 
772                 ret = ldb_next_request(module, search_req);
773                 
774                 if (ret == LDB_SUCCESS) {
775                         ret = ldb_wait(search_req->handle, LDB_WAIT_ALL);
776                 }
777                 
778                 talloc_free(search_req);
779                 if (ret != LDB_SUCCESS) {
780                         return ret;
781                 }
782         }
783
784         switch (req->op.seq_num.type) {
785         case LDB_SEQ_HIGHEST_SEQ:
786                 req->op.seq_num.seq_num = max_seq;
787                 break;
788         case LDB_SEQ_NEXT:
789                 req->op.seq_num.seq_num = max_seq;
790                 req->op.seq_num.seq_num++;
791                 break;
792         case LDB_SEQ_HIGHEST_TIMESTAMP:
793         {
794                 req->op.seq_num.seq_num = (max_seq >> 24);
795                 break;
796         }
797         }
798         req->op.seq_num.flags = 0;
799         req->op.seq_num.flags |= LDB_SEQ_TIMESTAMP_SEQUENCE;
800         req->op.seq_num.flags |= LDB_SEQ_GLOBAL_SEQUENCE;
801         return LDB_SUCCESS;
802 }
803
804 _PUBLIC_ const struct ldb_module_ops ldb_entryuuid_module_ops = {
805         .name              = "entryuuid",
806         .init_context      = entryuuid_init,
807         .sequence_number   = entryuuid_sequence_number,
808         LDB_MAP_OPS
809 };
810
811 _PUBLIC_ const struct ldb_module_ops ldb_nsuniqueid_module_ops = {
812         .name              = "nsuniqueid",
813         .init_context      = nsuniqueid_init,
814         .sequence_number   = entryuuid_sequence_number,
815         LDB_MAP_OPS
816 };