Search for memberOf when clients ask for a wildcard against OpenLDAP
[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_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 = "groupType",
380                 .type = MAP_CONVERT,
381                 .u = {
382                         .convert = {
383                                  .remote_name = "groupType",
384                                  .convert_local = normalise_to_signed32,
385                                  .convert_remote = val_copy,
386                          },
387                 }
388         },
389         {
390                 .local_name = "sAMAccountType",
391                 .type = MAP_CONVERT,
392                 .u = {
393                         .convert = {
394                                  .remote_name = "sAMAccountType",
395                                  .convert_local = normalise_to_signed32,
396                                  .convert_remote = val_copy,
397                          },
398                 }
399         },
400         {
401                 .local_name = "usnChanged",
402                 .type = MAP_CONVERT,
403                 .u = {
404                         .convert = {
405                                  .remote_name = "entryCSN",
406                                  .convert_local = usn_to_entryCSN,
407                                  .convert_remote = entryCSN_to_usn
408                          },
409                 },
410         },
411         {
412                 .local_name = "usnCreated",
413                 .type = MAP_CONVERT,
414                 .u = {
415                         .convert = {
416                                  .remote_name = "createTimestamp",
417                                  .convert_local = usn_to_timestamp,
418                                  .convert_remote = timestamp_to_usn,
419                          },
420                 },
421         },
422         {
423                 .local_name = "*",
424                 .type = MAP_KEEP,
425         },
426         {
427                 .local_name = NULL,
428         }
429 };
430
431 /* This objectClass conflicts with builtin classes on OpenLDAP */
432 const struct ldb_map_objectclass entryuuid_objectclasses[] =
433 {
434         {
435                 .local_name = "subSchema",
436                 .remote_name = "samba4SubSchema"
437         },
438         {
439                 .local_name = NULL
440         }
441 };
442
443 /* These things do not show up in wildcard searches in OpenLDAP, but
444  * we need them to show up in the AD-like view */
445 static const char * const entryuuid_wildcard_attributes[] = {
446         "objectGUID", 
447         "whenCreated", 
448         "whenChanged",
449         "usnCreated",
450         "usnChanged",
451         "memberOf",
452         NULL
453 };
454
455 static const struct ldb_map_attribute nsuniqueid_attributes[] = 
456 {
457         /* objectGUID */
458         {
459                 .local_name = "objectGUID",
460                 .type = MAP_CONVERT,
461                 .u = {
462                         .convert = {
463                                 .remote_name = "nsuniqueid", 
464                                 .convert_local = guid_ns_string,
465                                 .convert_remote = encode_ns_guid,
466                         },
467                 },
468         },
469         /* objectSid */ 
470         {
471                 .local_name = "objectSid",
472                 .type = MAP_CONVERT,
473                 .u = {
474                         .convert = {
475                                 .remote_name = "objectSid", 
476                                 .convert_local = sid_always_binary,
477                                 .convert_remote = val_copy,
478                         },
479                 },
480         },
481         {
482                 .local_name = "whenCreated",
483                 .type = MAP_RENAME,
484                 .u = {
485                         .rename = {
486                                  .remote_name = "createTimestamp"
487                          }
488                 }
489         },
490         {
491                 .local_name = "whenChanged",
492                 .type = MAP_RENAME,
493                 .u = {
494                         .rename = {
495                                  .remote_name = "modifyTimestamp"
496                          }
497                 }
498         },
499         {
500                 .local_name = "sambaPassword",
501                 .type = MAP_RENAME,
502                 .u = {
503                         .rename = {
504                                  .remote_name = "userPassword"
505                          }
506                 }
507         },
508         {
509                 .local_name = "objectCategory",
510                 .type = MAP_CONVERT,
511                 .u = {
512                         .convert = {
513                                 .remote_name = "objectCategory", 
514                                 .convert_local = objectCategory_always_dn,
515                                 .convert_remote = val_copy,
516                         },
517                 },
518         },
519         {
520                 .local_name = "distinguishedName",
521                 .type = MAP_RENAME,
522                 .u = {
523                         .rename = {
524                                  .remote_name = "entryDN"
525                          }
526                 }
527         },
528         {
529                 .local_name = "groupType",
530                 .type = MAP_CONVERT,
531                 .u = {
532                         .convert = {
533                                  .remote_name = "groupType",
534                                  .convert_local = normalise_to_signed32,
535                                  .convert_remote = val_copy,
536                          },
537                 }
538         },
539         {
540                 .local_name = "sAMAccountType",
541                 .type = MAP_CONVERT,
542                 .u = {
543                         .convert = {
544                                  .remote_name = "sAMAccountType",
545                                  .convert_local = normalise_to_signed32,
546                                  .convert_remote = val_copy,
547                          },
548                 }
549         },
550         {
551                 .local_name = "usnChanged",
552                 .type = MAP_CONVERT,
553                 .u = {
554                         .convert = {
555                                  .remote_name = "modifyTimestamp",
556                                  .convert_local = usn_to_timestamp,
557                                  .convert_remote = timestamp_to_usn,
558                          },
559                 },
560         },
561         {
562                 .local_name = "usnCreated",
563                 .type = MAP_CONVERT,
564                 .u = {
565                         .convert = {
566                                  .remote_name = "createTimestamp",
567                                  .convert_local = usn_to_timestamp,
568                                  .convert_remote = timestamp_to_usn,
569                          },
570                 },
571         },
572         {
573                 .local_name = "*",
574                 .type = MAP_KEEP,
575         },
576         {
577                 .local_name = NULL,
578         }
579 };
580
581 /* These things do not show up in wildcard searches in OpenLDAP, but
582  * we need them to show up in the AD-like view */
583 static const char * const nsuniqueid_wildcard_attributes[] = {
584         "objectGUID", 
585         "whenCreated", 
586         "whenChanged",
587         "usnCreated",
588         "usnChanged",
589         NULL
590 };
591
592 static int get_remote_rootdse(struct ldb_context *ldb, void *context, 
593                        struct ldb_reply *ares) 
594 {
595         struct entryuuid_private *entryuuid_private;
596         entryuuid_private = talloc_get_type(context,
597                                             struct entryuuid_private);
598         if (ares->type == LDB_REPLY_ENTRY) {
599                 int i;
600                 struct ldb_message_element *el = ldb_msg_find_element(ares->message, "namingContexts");
601                 entryuuid_private->base_dns = talloc_realloc(entryuuid_private, entryuuid_private->base_dns, struct ldb_dn *, 
602                                                              el->num_values + 1);
603                 for (i=0; i < el->num_values; i++) {
604                         if (!entryuuid_private->base_dns) {
605                                 return LDB_ERR_OPERATIONS_ERROR;
606                         }
607                         entryuuid_private->base_dns[i] = ldb_dn_new(entryuuid_private->base_dns, ldb, (const char *)el->values[i].data);
608                         if ( ! ldb_dn_validate(entryuuid_private->base_dns[i])) {
609                                 return LDB_ERR_OPERATIONS_ERROR;
610                         }
611                 }
612                 entryuuid_private->base_dns[i] = NULL;
613         }
614
615         return LDB_SUCCESS;
616 }
617
618 static int find_base_dns(struct ldb_module *module, 
619                           struct entryuuid_private *entryuuid_private) 
620 {
621         int ret;
622         struct ldb_request *req;
623         const char *naming_context_attr[] = {
624                 "namingContexts",
625                 NULL
626         };
627         req = talloc(entryuuid_private, struct ldb_request);
628         if (req == NULL) {
629                 ldb_set_errstring(module->ldb, "Out of Memory");
630                 return LDB_ERR_OPERATIONS_ERROR;
631         }
632
633         req->operation = LDB_SEARCH;
634         req->op.search.base = ldb_dn_new(req, module->ldb, NULL);
635         req->op.search.scope = LDB_SCOPE_BASE;
636
637         req->op.search.tree = ldb_parse_tree(req, "objectClass=*");
638         if (req->op.search.tree == NULL) {
639                 ldb_set_errstring(module->ldb, "Unable to parse search expression");
640                 talloc_free(req);
641                 return LDB_ERR_OPERATIONS_ERROR;
642         }
643
644         req->op.search.attrs = naming_context_attr;
645         req->controls = NULL;
646         req->context = entryuuid_private;
647         req->callback = get_remote_rootdse;
648         ldb_set_timeout(module->ldb, req, 0); /* use default timeout */
649
650         ret = ldb_next_request(module, req);
651         
652         if (ret == LDB_SUCCESS) {
653                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
654         }
655         
656         talloc_free(req);
657         if (ret != LDB_SUCCESS) {
658                 return ret;
659         }
660
661         return LDB_SUCCESS;
662 }
663
664 /* the context init function */
665 static int entryuuid_init(struct ldb_module *module)
666 {
667         int ret;
668         struct map_private *map_private;
669         struct entryuuid_private *entryuuid_private;
670
671         ret = ldb_map_init(module, entryuuid_attributes, entryuuid_objectclasses, entryuuid_wildcard_attributes, "extensibleObject", NULL);
672         if (ret != LDB_SUCCESS)
673                 return ret;
674
675         map_private = talloc_get_type(module->private_data, struct map_private);
676
677         entryuuid_private = talloc_zero(map_private, struct entryuuid_private);
678         map_private->caller_private = entryuuid_private;
679
680         ret = find_base_dns(module, entryuuid_private);
681
682         return ldb_next_init(module);
683 }
684
685 /* the context init function */
686 static int nsuniqueid_init(struct ldb_module *module)
687 {
688         int ret;
689         struct map_private *map_private;
690         struct entryuuid_private *entryuuid_private;
691
692         ret = ldb_map_init(module, nsuniqueid_attributes, NULL, nsuniqueid_wildcard_attributes, "extensibleObject", NULL);
693         if (ret != LDB_SUCCESS)
694                 return ret;
695
696         map_private = talloc_get_type(module->private_data, struct map_private);
697
698         entryuuid_private = talloc_zero(map_private, struct entryuuid_private);
699         map_private->caller_private = entryuuid_private;
700
701         ret = find_base_dns(module, entryuuid_private);
702
703         return ldb_next_init(module);
704 }
705
706 static int get_seq(struct ldb_context *ldb, void *context, 
707                    struct ldb_reply *ares) 
708 {
709         unsigned long long *max_seq = (unsigned long long *)context;
710         unsigned long long seq;
711         if (ares->type == LDB_REPLY_ENTRY) {
712                 struct ldb_message_element *el = ldb_msg_find_element(ares->message, "contextCSN");
713                 if (el) {
714                         seq = entryCSN_to_usn_int(ares, &el->values[0]);
715                         *max_seq = MAX(seq, *max_seq);
716                 }
717         }
718
719         return LDB_SUCCESS;
720 }
721
722 static int entryuuid_sequence_number(struct ldb_module *module, struct ldb_request *req)
723 {
724         int i, ret;
725         struct map_private *map_private;
726         struct entryuuid_private *entryuuid_private;
727         unsigned long long max_seq = 0;
728         struct ldb_request *search_req;
729         map_private = talloc_get_type(module->private_data, struct map_private);
730
731         entryuuid_private = talloc_get_type(map_private->caller_private, struct entryuuid_private);
732
733         /* Search the baseDNs for a sequence number */
734         for (i=0; entryuuid_private && 
735                      entryuuid_private->base_dns && 
736                      entryuuid_private->base_dns[i];
737                 i++) {
738                 static const char *contextCSN_attr[] = {
739                         "contextCSN", NULL
740                 };
741                 search_req = talloc(req, struct ldb_request);
742                 if (search_req == NULL) {
743                         ldb_set_errstring(module->ldb, "Out of Memory");
744                         return LDB_ERR_OPERATIONS_ERROR;
745                 }
746                 
747                 search_req->operation = LDB_SEARCH;
748                 search_req->op.search.base = entryuuid_private->base_dns[i];
749                 search_req->op.search.scope = LDB_SCOPE_BASE;
750                 
751                 search_req->op.search.tree = ldb_parse_tree(search_req, "objectClass=*");
752                 if (search_req->op.search.tree == NULL) {
753                         ldb_set_errstring(module->ldb, "Unable to parse search expression");
754                         talloc_free(search_req);
755                         return LDB_ERR_OPERATIONS_ERROR;
756                 }
757                 
758                 search_req->op.search.attrs = contextCSN_attr;
759                 search_req->controls = NULL;
760                 search_req->context = &max_seq;
761                 search_req->callback = get_seq;
762                 ldb_set_timeout(module->ldb, search_req, 0); /* use default timeout */
763                 
764                 ret = ldb_next_request(module, search_req);
765                 
766                 if (ret == LDB_SUCCESS) {
767                         ret = ldb_wait(search_req->handle, LDB_WAIT_ALL);
768                 }
769                 
770                 talloc_free(search_req);
771                 if (ret != LDB_SUCCESS) {
772                         return ret;
773                 }
774         }
775
776         switch (req->op.seq_num.type) {
777         case LDB_SEQ_HIGHEST_SEQ:
778                 req->op.seq_num.seq_num = max_seq;
779                 break;
780         case LDB_SEQ_NEXT:
781                 req->op.seq_num.seq_num = max_seq;
782                 req->op.seq_num.seq_num++;
783                 break;
784         case LDB_SEQ_HIGHEST_TIMESTAMP:
785         {
786                 req->op.seq_num.seq_num = (max_seq >> 24);
787                 break;
788         }
789         }
790         req->op.seq_num.flags = 0;
791         req->op.seq_num.flags |= LDB_SEQ_TIMESTAMP_SEQUENCE;
792         req->op.seq_num.flags |= LDB_SEQ_GLOBAL_SEQUENCE;
793         return LDB_SUCCESS;
794 }
795
796 static struct ldb_module_ops entryuuid_ops = {
797         .name              = "entryuuid",
798         .init_context      = entryuuid_init,
799         .sequence_number   = entryuuid_sequence_number
800 };
801
802 static struct ldb_module_ops nsuniqueid_ops = {
803         .name              = "nsuniqueid",
804         .init_context      = nsuniqueid_init,
805         .sequence_number   = entryuuid_sequence_number
806 };
807
808 /* the init function */
809 int ldb_simple_ldap_map_module_init(void)
810 {
811         int ret;
812         struct ldb_module_ops ops = ldb_map_get_ops();
813         entryuuid_ops.add       = ops.add;
814         entryuuid_ops.modify    = ops.modify;
815         entryuuid_ops.del       = ops.del;
816         entryuuid_ops.rename    = ops.rename;
817         entryuuid_ops.search    = ops.search;
818         entryuuid_ops.wait      = ops.wait;
819         ret = ldb_register_module(&entryuuid_ops);
820
821         if (ret) {
822                 return ret;
823         }
824
825         nsuniqueid_ops.add      = ops.add;
826         nsuniqueid_ops.modify   = ops.modify;
827         nsuniqueid_ops.del      = ops.del;
828         nsuniqueid_ops.rename   = ops.rename;
829         nsuniqueid_ops.search   = ops.search;
830         nsuniqueid_ops.wait     = ops.wait;
831         ret = ldb_register_module(&nsuniqueid_ops);
832
833         return ret;
834 }