r23792: convert Samba4 to GPLv3
[mdw/samba.git] / source4 / dsdb / samdb / ldb_modules / entryUUID.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_result *objectclass_res;     
40         struct ldb_dn **base_dns;
41 };
42
43 static struct ldb_val encode_guid(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
44 {
45         struct GUID guid;
46         NTSTATUS status = GUID_from_string((char *)val->data, &guid);
47         struct ldb_val out = data_blob(NULL, 0);
48
49         if (!NT_STATUS_IS_OK(status)) {
50                 return out;
51         }
52         status = ndr_push_struct_blob(&out, ctx, &guid, 
53                                       (ndr_push_flags_fn_t)ndr_push_GUID);
54         if (!NT_STATUS_IS_OK(status)) {
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         NTSTATUS status;
65         struct ldb_val out = data_blob(NULL, 0);
66         if (val->length >= 32 && val->data[val->length] == '\0') {
67                 ldb_handler_copy(module->ldb, ctx, val, &out);
68         } else {
69                 guid = talloc(ctx, struct GUID);
70                 if (guid == NULL) {
71                         return out;
72                 }
73                 status = ndr_pull_struct_blob(val, guid, guid, 
74                                               (ndr_pull_flags_fn_t)ndr_pull_GUID);
75                 if (!NT_STATUS_IS_OK(status)) {
76                         talloc_free(guid);
77                         return out;
78                 }
79                 out = data_blob_string_const(GUID_string(ctx, guid));
80                 talloc_free(guid);
81         }
82         return out;
83 }
84
85 static struct ldb_val encode_ns_guid(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
86 {
87         struct GUID guid;
88         NTSTATUS status = NS_GUID_from_string((char *)val->data, &guid);
89         struct ldb_val out = data_blob(NULL, 0);
90
91         if (!NT_STATUS_IS_OK(status)) {
92                 return out;
93         }
94         status = ndr_push_struct_blob(&out, ctx, &guid, 
95                                       (ndr_push_flags_fn_t)ndr_push_GUID);
96         if (!NT_STATUS_IS_OK(status)) {
97                 return out;
98         }
99
100         return out;
101 }
102
103 static struct ldb_val guid_ns_string(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
104 {
105         NTSTATUS status;
106         struct ldb_val out = data_blob(NULL, 0);
107         if (val->length >= 32 && val->data[val->length] == '\0') {
108                 struct GUID guid;
109                 GUID_from_string((char *)val->data, &guid);
110                 out = data_blob_string_const(NS_GUID_string(ctx, &guid));
111         } else {
112                 struct GUID *guid_p;
113                 guid_p = talloc(ctx, struct GUID);
114                 if (guid_p == NULL) {
115                         return out;
116                 }
117                 status = ndr_pull_struct_blob(val, guid_p, guid_p, 
118                                               (ndr_pull_flags_fn_t)ndr_pull_GUID);
119                 if (!NT_STATUS_IS_OK(status)) {
120                         talloc_free(guid_p);
121                         return out;
122                 }
123                 out = data_blob_string_const(NS_GUID_string(ctx, guid_p));
124                 talloc_free(guid_p);
125         }
126         return out;
127 }
128
129 /* The backend holds binary sids, so just copy them back */
130 static struct ldb_val val_copy(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
131 {
132         struct ldb_val out = data_blob(NULL, 0);
133         ldb_handler_copy(module->ldb, ctx, val, &out);
134
135         return out;
136 }
137
138 /* Ensure we always convert sids into binary, so the backend doesn't have to know about both forms */
139 static struct ldb_val sid_always_binary(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
140 {
141         struct ldb_val out = data_blob(NULL, 0);
142         const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(module->ldb, "objectSid");
143
144         if (a->syntax->canonicalise_fn(module->ldb, ctx, val, &out) != LDB_SUCCESS) {
145                 return data_blob(NULL, 0);
146         }
147
148         return out;
149 }
150
151 static struct ldb_val objectCategory_always_dn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
152 {
153         int i;
154         struct map_private *map_private;
155         struct entryUUID_private *entryUUID_private;
156         struct ldb_result *list;
157
158         if (ldb_dn_validate(ldb_dn_new(ctx, module->ldb, (const char *)val->data))) {
159                 return *val;
160         }
161         map_private = talloc_get_type(module->private_data, struct map_private);
162
163         entryUUID_private = talloc_get_type(map_private->caller_private, struct entryUUID_private);
164         list = entryUUID_private->objectclass_res;
165
166         for (i=0; list && (i < list->count); i++) {
167                 if (ldb_attr_cmp((const char *)val->data, ldb_msg_find_attr_as_string(list->msgs[i], "lDAPDisplayName", NULL)) == 0) {
168                         char *dn = ldb_dn_alloc_linearized(ctx, list->msgs[i]->dn);
169                         return data_blob_string_const(dn);
170                 }
171         }
172         return *val;
173 }
174
175 static struct ldb_val class_to_oid(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
176 {
177         int i;
178         struct map_private *map_private;
179         struct entryUUID_private *entryUUID_private;
180         struct ldb_result *list;
181
182         map_private = talloc_get_type(module->private_data, struct map_private);
183
184         entryUUID_private = talloc_get_type(map_private->caller_private, struct entryUUID_private);
185         list = entryUUID_private->objectclass_res;
186
187         for (i=0; list && (i < list->count); i++) {
188                 if (ldb_attr_cmp((const char *)val->data, ldb_msg_find_attr_as_string(list->msgs[i], "lDAPDisplayName", NULL)) == 0) {
189                         const char *oid = ldb_msg_find_attr_as_string(list->msgs[i], "governsID", NULL);
190                         return data_blob_string_const(oid);
191                 }
192         }
193         return *val;
194 }
195
196 static struct ldb_val class_from_oid(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
197 {
198         int i;
199         struct map_private *map_private;
200         struct entryUUID_private *entryUUID_private;
201         struct ldb_result *list;
202
203         map_private = talloc_get_type(module->private_data, struct map_private);
204
205         entryUUID_private = talloc_get_type(map_private->caller_private, struct entryUUID_private);
206         list = entryUUID_private->objectclass_res;
207
208         for (i=0; list && (i < list->count); i++) {
209                 if (ldb_attr_cmp((const char *)val->data, ldb_msg_find_attr_as_string(list->msgs[i], "governsID", NULL)) == 0) {
210                         const char *oc = ldb_msg_find_attr_as_string(list->msgs[i], "lDAPDisplayName", NULL);
211                         return data_blob_string_const(oc);
212                 }
213         }
214         return *val;
215 }
216
217
218 static struct ldb_val normalise_to_signed32(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
219 {
220         long long int signed_ll = strtoll((const char *)val->data, NULL, 10);
221         if (signed_ll >= 0x80000000LL) {
222                 union {
223                         int32_t signed_int;
224                         uint32_t unsigned_int;
225                 } u = {
226                         .unsigned_int = strtoul((const char *)val->data, NULL, 10)
227                 };
228
229                 struct ldb_val out = data_blob_string_const(talloc_asprintf(ctx, "%d", u.signed_int));
230                 return out;
231         }
232         return val_copy(module, ctx, val);
233 }
234
235 static struct ldb_val usn_to_entryCSN(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
236 {
237         struct ldb_val out;
238         unsigned long long usn = strtoull((const char *)val->data, NULL, 10);
239         time_t t = (usn >> 24);
240         out = data_blob_string_const(talloc_asprintf(ctx, "%s#%06x#00#000000", ldb_timestring(ctx, t), (unsigned int)(usn & 0xFFFFFF)));
241         return out;
242 }
243
244 static unsigned long long entryCSN_to_usn_int(TALLOC_CTX *ctx, const struct ldb_val *val) 
245 {
246         char *entryCSN = talloc_strdup(ctx, (const char *)val->data);
247         char *mod_per_sec;
248         time_t t;
249         unsigned long long usn;
250         char *p;
251         if (!entryCSN) {
252                 return 0;
253         }
254         p = strchr(entryCSN, '#');
255         if (!p) {
256                 return 0;
257         }
258         p[0] = '\0';
259         p++;
260         mod_per_sec = p;
261
262         p = strchr(p, '#');
263         if (!p) {
264                 return 0;
265         }
266         p[0] = '\0';
267         p++;
268
269         usn = strtol(mod_per_sec, NULL, 16);
270
271         t = ldb_string_to_time(entryCSN);
272         
273         usn = usn | ((unsigned long long)t <<24);
274         return usn;
275 }
276
277 static struct ldb_val entryCSN_to_usn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
278 {
279         struct ldb_val out;
280         unsigned long long usn = entryCSN_to_usn_int(ctx, val);
281         out = data_blob_string_const(talloc_asprintf(ctx, "%lld", usn));
282         return out;
283 }
284
285 static struct ldb_val usn_to_timestamp(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
286 {
287         struct ldb_val out;
288         unsigned long long usn = strtoull((const char *)val->data, NULL, 10);
289         time_t t = (usn >> 24);
290         out = data_blob_string_const(ldb_timestring(ctx, t));
291         return out;
292 }
293
294 static struct ldb_val timestamp_to_usn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
295 {
296         struct ldb_val out;
297         time_t t;
298         unsigned long long usn;
299
300         t = ldb_string_to_time((const char *)val->data);
301         
302         usn = ((unsigned long long)t <<24);
303
304         out = data_blob_string_const(talloc_asprintf(ctx, "%lld", usn));
305         return out;
306 }
307
308
309 const struct ldb_map_attribute entryUUID_attributes[] = 
310 {
311         /* objectGUID */
312         {
313                 .local_name = "objectGUID",
314                 .type = MAP_CONVERT,
315                 .u = {
316                         .convert = {
317                                 .remote_name = "entryUUID", 
318                                 .convert_local = guid_always_string,
319                                 .convert_remote = encode_guid,
320                         },
321                 },
322         },
323         /* invocationId */
324         {
325                 .local_name = "invocationId",
326                 .type = MAP_CONVERT,
327                 .u = {
328                         .convert = {
329                                 .remote_name = "invocationId", 
330                                 .convert_local = guid_always_string,
331                                 .convert_remote = encode_guid,
332                         },
333                 },
334         },
335         /* objectSid */
336         {
337                 .local_name = "objectSid",
338                 .type = MAP_CONVERT,
339                 .u = {
340                         .convert = {
341                                 .remote_name = "objectSid", 
342                                 .convert_local = sid_always_binary,
343                                 .convert_remote = val_copy,
344                         },
345                 },
346         },
347         {
348                 .local_name = "whenCreated",
349                 .type = MAP_RENAME,
350                 .u = {
351                         .rename = {
352                                  .remote_name = "createTimestamp"
353                          }
354                 }
355         },
356         {
357                 .local_name = "whenChanged",
358                 .type = MAP_RENAME,
359                 .u = {
360                         .rename = {
361                                  .remote_name = "modifyTimestamp"
362                          }
363                 }
364         },
365         {
366                 .local_name = "objectClasses",
367                 .type = MAP_RENAME,
368                 .u = {
369                         .rename = {
370                                  .remote_name = "samba4ObjectClasses"
371                          }
372                 }
373         },
374         {
375                 .local_name = "dITContentRules",
376                 .type = MAP_RENAME,
377                 .u = {
378                         .rename = {
379                                  .remote_name = "samba4DITContentRules"
380                          }
381                 }
382         },
383         {
384                 .local_name = "attributeTypes",
385                 .type = MAP_RENAME,
386                 .u = {
387                         .rename = {
388                                  .remote_name = "samba4AttributeTypes"
389                          }
390                 }
391         },
392         {
393                 .local_name = "sambaPassword",
394                 .type = MAP_RENAME,
395                 .u = {
396                         .rename = {
397                                  .remote_name = "userPassword"
398                          }
399                 }
400         },
401 #if 0
402         {
403                 .local_name = "allowedChildClassesEffective",
404                 .type = MAP_CONVERT,
405                 .u = {
406                         .convert = {
407                                 .remote_name = "allowedChildClassesEffective", 
408                                 .convert_local = class_to_oid,
409                                 .convert_remote = class_from_oid,
410                         },
411                 },
412         },
413 #endif
414         {
415                 .local_name = "objectCategory",
416                 .type = MAP_CONVERT,
417                 .u = {
418                         .convert = {
419                                 .remote_name = "objectCategory", 
420                                 .convert_local = objectCategory_always_dn,
421                                 .convert_remote = val_copy,
422                         },
423                 },
424         },
425         {
426                 .local_name = "distinguishedName",
427                 .type = MAP_RENAME,
428                 .u = {
429                         .rename = {
430                                  .remote_name = "entryDN"
431                          }
432                 }
433         },
434         {
435                 .local_name = "groupType",
436                 .type = MAP_CONVERT,
437                 .u = {
438                         .convert = {
439                                  .remote_name = "groupType",
440                                  .convert_local = normalise_to_signed32,
441                                  .convert_remote = val_copy,
442                          },
443                 }
444         },
445         {
446                 .local_name = "sAMAccountType",
447                 .type = MAP_CONVERT,
448                 .u = {
449                         .convert = {
450                                  .remote_name = "sAMAccountType",
451                                  .convert_local = normalise_to_signed32,
452                                  .convert_remote = val_copy,
453                          },
454                 }
455         },
456         {
457                 .local_name = "usnChanged",
458                 .type = MAP_CONVERT,
459                 .u = {
460                         .convert = {
461                                  .remote_name = "entryCSN",
462                                  .convert_local = usn_to_entryCSN,
463                                  .convert_remote = entryCSN_to_usn
464                          },
465                 },
466         },
467         {
468                 .local_name = "usnCreated",
469                 .type = MAP_CONVERT,
470                 .u = {
471                         .convert = {
472                                  .remote_name = "createTimestamp",
473                                  .convert_local = usn_to_timestamp,
474                                  .convert_remote = timestamp_to_usn,
475                          },
476                 },
477         },
478         {
479                 .local_name = "*",
480                 .type = MAP_KEEP,
481         },
482         {
483                 .local_name = NULL,
484         }
485 };
486
487 /* This objectClass conflicts with builtin classes on OpenLDAP */
488 const struct ldb_map_objectclass entryUUID_objectclasses[] =
489 {
490         {
491                 .local_name = "subSchema",
492                 .remote_name = "samba4SubSchema"
493         },
494         {
495                 .local_name = NULL
496         }
497 };
498
499 /* These things do not show up in wildcard searches in OpenLDAP, but
500  * we need them to show up in the AD-like view */
501 const char * const entryUUID_wildcard_attributes[] = {
502         "objectGUID", 
503         "whenCreated", 
504         "whenChanged",
505         "usnCreated",
506         "usnChanged",
507         NULL
508 };
509
510 const struct ldb_map_attribute nsuniqueid_attributes[] = 
511 {
512         /* objectGUID */
513         {
514                 .local_name = "objectGUID",
515                 .type = MAP_CONVERT,
516                 .u = {
517                         .convert = {
518                                 .remote_name = "nsuniqueid", 
519                                 .convert_local = guid_ns_string,
520                                 .convert_remote = encode_ns_guid,
521                         },
522                 },
523         },
524         /* objectSid */ 
525         {
526                 .local_name = "objectSid",
527                 .type = MAP_CONVERT,
528                 .u = {
529                         .convert = {
530                                 .remote_name = "objectSid", 
531                                 .convert_local = sid_always_binary,
532                                 .convert_remote = val_copy,
533                         },
534                 },
535         },
536         {
537                 .local_name = "whenCreated",
538                 .type = MAP_RENAME,
539                 .u = {
540                         .rename = {
541                                  .remote_name = "createTimestamp"
542                          }
543                 }
544         },
545         {
546                 .local_name = "whenChanged",
547                 .type = MAP_RENAME,
548                 .u = {
549                         .rename = {
550                                  .remote_name = "modifyTimestamp"
551                          }
552                 }
553         },
554         {
555                 .local_name = "sambaPassword",
556                 .type = MAP_RENAME,
557                 .u = {
558                         .rename = {
559                                  .remote_name = "userPassword"
560                          }
561                 }
562         },
563 #if 0
564         {
565                 .local_name = "allowedChildClassesEffective",
566                 .type = MAP_CONVERT,
567                 .u = {
568                         .convert = {
569                                 .remote_name = "allowedChildClassesEffective", 
570                                 .convert_local = class_to_oid,
571                                 .convert_remote = class_from_oid,
572                         },
573                 },
574         },
575 #endif
576         {
577                 .local_name = "objectCategory",
578                 .type = MAP_CONVERT,
579                 .u = {
580                         .convert = {
581                                 .remote_name = "objectCategory", 
582                                 .convert_local = objectCategory_always_dn,
583                                 .convert_remote = val_copy,
584                         },
585                 },
586         },
587         {
588                 .local_name = "distinguishedName",
589                 .type = MAP_RENAME,
590                 .u = {
591                         .rename = {
592                                  .remote_name = "entryDN"
593                          }
594                 }
595         },
596         {
597                 .local_name = "groupType",
598                 .type = MAP_CONVERT,
599                 .u = {
600                         .convert = {
601                                  .remote_name = "groupType",
602                                  .convert_local = normalise_to_signed32,
603                                  .convert_remote = val_copy,
604                          },
605                 }
606         },
607         {
608                 .local_name = "sAMAccountType",
609                 .type = MAP_CONVERT,
610                 .u = {
611                         .convert = {
612                                  .remote_name = "sAMAccountType",
613                                  .convert_local = normalise_to_signed32,
614                                  .convert_remote = val_copy,
615                          },
616                 }
617         },
618         {
619                 .local_name = "usnChanged",
620                 .type = MAP_CONVERT,
621                 .u = {
622                         .convert = {
623                                  .remote_name = "modifyTimestamp",
624                                  .convert_local = usn_to_timestamp,
625                                  .convert_remote = timestamp_to_usn,
626                          },
627                 },
628         },
629         {
630                 .local_name = "usnCreated",
631                 .type = MAP_CONVERT,
632                 .u = {
633                         .convert = {
634                                  .remote_name = "createTimestamp",
635                                  .convert_local = usn_to_timestamp,
636                                  .convert_remote = timestamp_to_usn,
637                          },
638                 },
639         },
640         {
641                 .local_name = "*",
642                 .type = MAP_KEEP,
643         },
644         {
645                 .local_name = NULL,
646         }
647 };
648
649 /* These things do not show up in wildcard searches in OpenLDAP, but
650  * we need them to show up in the AD-like view */
651 const char * const nsuniqueid_wildcard_attributes[] = {
652         "objectGUID", 
653         "whenCreated", 
654         "whenChanged",
655         "usnCreated",
656         "usnChanged",
657         NULL
658 };
659
660 static struct ldb_dn *find_schema_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx) 
661 {
662         const char *rootdse_attrs[] = {"schemaNamingContext", NULL};
663         struct ldb_dn *schema_dn;
664         struct ldb_dn *basedn = ldb_dn_new(mem_ctx, ldb, NULL);
665         struct ldb_result *rootdse_res;
666         int ldb_ret;
667         if (!basedn) {
668                 return NULL;
669         }
670         
671         /* Search for rootdse */
672         ldb_ret = ldb_search(ldb, basedn, LDB_SCOPE_BASE, NULL, rootdse_attrs, &rootdse_res);
673         if (ldb_ret != LDB_SUCCESS) {
674                 return NULL;
675         }
676         
677         talloc_steal(mem_ctx, rootdse_res);
678
679         if (rootdse_res->count != 1) {
680                 ldb_asprintf_errstring(ldb, "Failed to find rootDSE: count %d", rootdse_res->count);
681                 return NULL;
682         }
683         
684         /* Locate schema */
685         schema_dn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, rootdse_res->msgs[0], "schemaNamingContext");
686         if (!schema_dn) {
687                 return NULL;
688         }
689
690         talloc_free(rootdse_res);
691         return schema_dn;
692 }
693
694 static int fetch_objectclass_schema(struct ldb_context *ldb, struct ldb_dn *schemadn,
695                               TALLOC_CTX *mem_ctx, 
696                               struct ldb_result **objectclass_res)
697 {
698         TALLOC_CTX *local_ctx = talloc_new(mem_ctx);
699         int ret;
700         const char *attrs[] = {
701                 "lDAPDisplayName",
702                 "governsID",
703                 NULL
704         };
705
706         if (!local_ctx) {
707                 return LDB_ERR_OPERATIONS_ERROR;
708         }
709         
710         /* Downlaod schema */
711         ret = ldb_search(ldb, schemadn, LDB_SCOPE_SUBTREE, 
712                          "objectClass=classSchema", 
713                          attrs, objectclass_res);
714         if (ret != LDB_SUCCESS) {
715                 return ret;
716         }
717
718         talloc_steal(mem_ctx, objectclass_res);
719
720         return ret;
721 }
722
723
724 static int get_remote_rootdse(struct ldb_context *ldb, void *context, 
725                        struct ldb_reply *ares) 
726 {
727         struct entryUUID_private *entryUUID_private;
728         entryUUID_private = talloc_get_type(context,
729                                             struct entryUUID_private);
730         if (ares->type == LDB_REPLY_ENTRY) {
731                 int i;
732                 struct ldb_message_element *el = ldb_msg_find_element(ares->message, "namingContexts");
733                 entryUUID_private->base_dns = talloc_realloc(entryUUID_private, entryUUID_private->base_dns, struct ldb_dn *, 
734                                                              el->num_values + 1);
735                 for (i=0; i < el->num_values; i++) {
736                         if (!entryUUID_private->base_dns) {
737                                 return LDB_ERR_OPERATIONS_ERROR;
738                         }
739                         entryUUID_private->base_dns[i] = ldb_dn_new(entryUUID_private->base_dns, ldb, (const char *)el->values[i].data);
740                         if ( ! ldb_dn_validate(entryUUID_private->base_dns[i])) {
741                                 return LDB_ERR_OPERATIONS_ERROR;
742                         }
743                 }
744                 entryUUID_private->base_dns[i] = NULL;
745         }
746
747         return LDB_SUCCESS;
748 }
749
750 static int find_base_dns(struct ldb_module *module, 
751                           struct entryUUID_private *entryUUID_private) 
752 {
753         int ret;
754         struct ldb_request *req;
755         const char *naming_context_attr[] = {
756                 "namingContexts",
757                 NULL
758         };
759         req = talloc(entryUUID_private, struct ldb_request);
760         if (req == NULL) {
761                 ldb_set_errstring(module->ldb, "Out of Memory");
762                 return LDB_ERR_OPERATIONS_ERROR;
763         }
764
765         req->operation = LDB_SEARCH;
766         req->op.search.base = ldb_dn_new(req, module->ldb, NULL);
767         req->op.search.scope = LDB_SCOPE_BASE;
768
769         req->op.search.tree = ldb_parse_tree(req, "objectClass=*");
770         if (req->op.search.tree == NULL) {
771                 ldb_set_errstring(module->ldb, "Unable to parse search expression");
772                 talloc_free(req);
773                 return LDB_ERR_OPERATIONS_ERROR;
774         }
775
776         req->op.search.attrs = naming_context_attr;
777         req->controls = NULL;
778         req->context = entryUUID_private;
779         req->callback = get_remote_rootdse;
780         ldb_set_timeout(module->ldb, req, 0); /* use default timeout */
781
782         ret = ldb_next_request(module, req);
783         
784         if (ret == LDB_SUCCESS) {
785                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
786         }
787         
788         talloc_free(req);
789         if (ret != LDB_SUCCESS) {
790                 return ret;
791         }
792
793         return LDB_SUCCESS;
794 }
795
796 /* the context init function */
797 static int entryUUID_init(struct ldb_module *module)
798 {
799         int ret;
800         struct map_private *map_private;
801         struct entryUUID_private *entryUUID_private;
802         struct ldb_dn *schema_dn;
803
804         ret = ldb_map_init(module, entryUUID_attributes, entryUUID_objectclasses, entryUUID_wildcard_attributes, NULL);
805         if (ret != LDB_SUCCESS)
806                 return ret;
807
808         map_private = talloc_get_type(module->private_data, struct map_private);
809
810         entryUUID_private = talloc_zero(map_private, struct entryUUID_private);
811         map_private->caller_private = entryUUID_private;
812
813         schema_dn = find_schema_dn(module->ldb, map_private);
814         if (!schema_dn) {
815                 /* Perhaps no schema yet */
816                 return LDB_SUCCESS;
817         }
818         
819         ret = fetch_objectclass_schema(module->ldb, schema_dn, entryUUID_private, 
820                                        &entryUUID_private->objectclass_res);
821         if (ret != LDB_SUCCESS) {
822                 /* Perhaps no schema yet */
823                 return LDB_SUCCESS;
824         }       
825
826         ret = find_base_dns(module, entryUUID_private);
827
828         return ldb_next_init(module);
829 }
830
831 /* the context init function */
832 static int nsuniqueid_init(struct ldb_module *module)
833 {
834         int ret;
835         struct map_private *map_private;
836         struct entryUUID_private *entryUUID_private;
837         struct ldb_dn *schema_dn;
838
839         ret = ldb_map_init(module, nsuniqueid_attributes, NULL, nsuniqueid_wildcard_attributes, NULL);
840         if (ret != LDB_SUCCESS)
841                 return ret;
842
843         map_private = talloc_get_type(module->private_data, struct map_private);
844
845         entryUUID_private = talloc_zero(map_private, struct entryUUID_private);
846         map_private->caller_private = entryUUID_private;
847
848         schema_dn = find_schema_dn(module->ldb, map_private);
849         if (!schema_dn) {
850                 /* Perhaps no schema yet */
851                 return LDB_SUCCESS;
852         }
853         
854         ret = fetch_objectclass_schema(module->ldb, schema_dn, entryUUID_private, 
855                                        &entryUUID_private->objectclass_res);
856         if (ret != LDB_SUCCESS) {
857                 /* Perhaps no schema yet */
858                 return LDB_SUCCESS;
859         }       
860
861         ret = find_base_dns(module, entryUUID_private);
862
863         return ldb_next_init(module);
864 }
865
866 static int get_seq(struct ldb_context *ldb, void *context, 
867                    struct ldb_reply *ares) 
868 {
869         unsigned long long *max_seq = context;
870         unsigned long long seq;
871         if (ares->type == LDB_REPLY_ENTRY) {
872                 struct ldb_message_element *el = ldb_msg_find_element(ares->message, "contextCSN");
873                 if (el) {
874                         seq = entryCSN_to_usn_int(ares, &el->values[0]);
875                         *max_seq = MAX(seq, *max_seq);
876                 }
877         }
878
879         return LDB_SUCCESS;
880 }
881
882 static int entryUUID_sequence_number(struct ldb_module *module, struct ldb_request *req)
883 {
884         int i, ret;
885         struct map_private *map_private;
886         struct entryUUID_private *entryUUID_private;
887         unsigned long long max_seq = 0;
888         struct ldb_request *search_req;
889         map_private = talloc_get_type(module->private_data, struct map_private);
890
891         entryUUID_private = talloc_get_type(map_private->caller_private, struct entryUUID_private);
892
893         /* Search the baseDNs for a sequence number */
894         for (i=0; entryUUID_private && 
895                      entryUUID_private->base_dns && 
896                      entryUUID_private->base_dns[i];
897                 i++) {
898                 static const char *contextCSN_attr[] = {
899                         "contextCSN", NULL
900                 };
901                 search_req = talloc(req, struct ldb_request);
902                 if (search_req == NULL) {
903                         ldb_set_errstring(module->ldb, "Out of Memory");
904                         return LDB_ERR_OPERATIONS_ERROR;
905                 }
906                 
907                 search_req->operation = LDB_SEARCH;
908                 search_req->op.search.base = entryUUID_private->base_dns[i];
909                 search_req->op.search.scope = LDB_SCOPE_BASE;
910                 
911                 search_req->op.search.tree = ldb_parse_tree(search_req, "objectClass=*");
912                 if (search_req->op.search.tree == NULL) {
913                         ldb_set_errstring(module->ldb, "Unable to parse search expression");
914                         talloc_free(search_req);
915                         return LDB_ERR_OPERATIONS_ERROR;
916                 }
917                 
918                 search_req->op.search.attrs = contextCSN_attr;
919                 search_req->controls = NULL;
920                 search_req->context = &max_seq;
921                 search_req->callback = get_seq;
922                 ldb_set_timeout(module->ldb, search_req, 0); /* use default timeout */
923                 
924                 ret = ldb_next_request(module, search_req);
925                 
926                 if (ret == LDB_SUCCESS) {
927                         ret = ldb_wait(search_req->handle, LDB_WAIT_ALL);
928                 }
929                 
930                 talloc_free(search_req);
931                 if (ret != LDB_SUCCESS) {
932                         return ret;
933                 }
934         }
935
936         switch (req->op.seq_num.type) {
937         case LDB_SEQ_HIGHEST_SEQ:
938                 req->op.seq_num.seq_num = max_seq;
939                 break;
940         case LDB_SEQ_NEXT:
941                 req->op.seq_num.seq_num = max_seq;
942                 req->op.seq_num.seq_num++;
943                 break;
944         case LDB_SEQ_HIGHEST_TIMESTAMP:
945         {
946                 req->op.seq_num.seq_num = (max_seq >> 24);
947                 break;
948         }
949         }
950         req->op.seq_num.flags = 0;
951         req->op.seq_num.flags |= LDB_SEQ_TIMESTAMP_SEQUENCE;
952         req->op.seq_num.flags |= LDB_SEQ_GLOBAL_SEQUENCE;
953         return LDB_SUCCESS;
954 }
955
956 static struct ldb_module_ops entryUUID_ops = {
957         .name              = "entryUUID",
958         .init_context      = entryUUID_init,
959         .sequence_number   = entryUUID_sequence_number
960 };
961
962 static struct ldb_module_ops nsuniqueid_ops = {
963         .name              = "nsuniqueid",
964         .init_context      = nsuniqueid_init,
965         .sequence_number   = entryUUID_sequence_number
966 };
967
968 /* the init function */
969 int ldb_entryUUID_module_init(void)
970 {
971         int ret;
972         struct ldb_module_ops ops = ldb_map_get_ops();
973         entryUUID_ops.add       = ops.add;
974         entryUUID_ops.modify    = ops.modify;
975         entryUUID_ops.del       = ops.del;
976         entryUUID_ops.rename    = ops.rename;
977         entryUUID_ops.search    = ops.search;
978         entryUUID_ops.wait      = ops.wait;
979         ret = ldb_register_module(&entryUUID_ops);
980
981         if (ret) {
982                 return ret;
983         }
984
985         nsuniqueid_ops.add      = ops.add;
986         nsuniqueid_ops.modify   = ops.modify;
987         nsuniqueid_ops.del      = ops.del;
988         nsuniqueid_ops.rename   = ops.rename;
989         nsuniqueid_ops.search   = ops.search;
990         nsuniqueid_ops.wait     = ops.wait;
991         ret = ldb_register_module(&nsuniqueid_ops);
992
993         return ret;
994 }