a7706ac6dcf1e2ead3f669f9b74f07f0f7eeca0b
[samba.git] / source4 / lib / ldb-samba / ldif_handlers.c
1 /* 
2    ldb database library - ldif handlers for Samba
3
4    Copyright (C) Andrew Tridgell 2005
5    Copyright (C) Andrew Bartlett 2006-2009
6    Copyright (C) Matthias Dieter Wallnöfer 2009
7      ** NOTE! The following LGPL license applies to the ldb
8      ** library. This does NOT imply that all of Samba is released
9      ** under the LGPL
10    
11    This library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Lesser General Public
13    License as published by the Free Software Foundation; either
14    version 3 of the License, or (at your option) any later version.
15
16    This library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Lesser General Public License for more details.
20
21    You should have received a copy of the GNU Lesser General Public
22    License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "lib/ldb/include/ldb_module.h"
28 #include "ldb_handlers.h"
29 #include "dsdb/samdb/samdb.h"
30 #include "librpc/gen_ndr/ndr_security.h"
31 #include "librpc/gen_ndr/ndr_misc.h"
32 #include "librpc/gen_ndr/ndr_drsblobs.h"
33 #include "librpc/ndr/libndr.h"
34 #include "libcli/security/security.h"
35 #include "param/param.h"
36 #include "../lib/util/asn1.h"
37
38 /*
39   use ndr_print_* to convert a NDR formatted blob to a ldif formatted blob
40
41   If mask_errors is true, then function succeeds but out data
42   is set to "<Unable to decode binary data>" message
43
44   \return 0 on success; -1 on error
45 */
46 static int ldif_write_NDR(struct ldb_context *ldb, void *mem_ctx,
47                           const struct ldb_val *in, struct ldb_val *out,
48                           size_t struct_size,
49                           ndr_pull_flags_fn_t pull_fn,
50                           ndr_print_fn_t print_fn,
51                           bool mask_errors)
52 {
53         uint8_t *p;
54         enum ndr_err_code err;
55         if (!(ldb_get_flags(ldb) & LDB_FLG_SHOW_BINARY)) {
56                 return ldb_handler_copy(ldb, mem_ctx, in, out);
57         }
58         p = talloc_size(mem_ctx, struct_size);
59         err = ndr_pull_struct_blob(in, mem_ctx, 
60                                    p, pull_fn);
61         if (err != NDR_ERR_SUCCESS) {
62                 /* fail in not in mask_error mode */
63                 if (!mask_errors) {
64                         return -1;
65                 }
66                 talloc_free(p);
67                 out->data = (uint8_t *)talloc_strdup(mem_ctx, "<Unable to decode binary data>");
68                 out->length = strlen((const char *)out->data);
69                 return 0;
70         }
71         out->data = (uint8_t *)ndr_print_struct_string(mem_ctx, print_fn, "NDR", p);
72         talloc_free(p);
73         if (out->data == NULL) {
74                 return ldb_handler_copy(ldb, mem_ctx, in, out);         
75         }
76         out->length = strlen((char *)out->data);
77         return 0;
78 }
79
80 /*
81   convert a ldif formatted objectSid to a NDR formatted blob
82 */
83 static int ldif_read_objectSid(struct ldb_context *ldb, void *mem_ctx,
84                                const struct ldb_val *in, struct ldb_val *out)
85 {
86         enum ndr_err_code ndr_err;
87         struct dom_sid *sid;
88         sid = dom_sid_parse_length(mem_ctx, in);
89         if (sid == NULL) {
90                 return -1;
91         }
92         ndr_err = ndr_push_struct_blob(out, mem_ctx, sid,
93                                        (ndr_push_flags_fn_t)ndr_push_dom_sid);
94         talloc_free(sid);
95         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
96                 return -1;
97         }
98         return 0;
99 }
100
101 /*
102   convert a NDR formatted blob to a ldif formatted objectSid
103 */
104 int ldif_write_objectSid(struct ldb_context *ldb, void *mem_ctx,
105                                 const struct ldb_val *in, struct ldb_val *out)
106 {
107         struct dom_sid *sid;
108         enum ndr_err_code ndr_err;
109
110         sid = talloc(mem_ctx, struct dom_sid);
111         if (sid == NULL) {
112                 return -1;
113         }
114         ndr_err = ndr_pull_struct_blob_all(in, sid, sid,
115                                            (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
116         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
117                 talloc_free(sid);
118                 return -1;
119         }
120         *out = data_blob_string_const(dom_sid_string(mem_ctx, sid));
121         talloc_free(sid);
122         if (out->data == NULL) {
123                 return -1;
124         }
125         return 0;
126 }
127
128 bool ldif_comparision_objectSid_isString(const struct ldb_val *v)
129 {
130         if (v->length < 3) {
131                 return false;
132         }
133
134         if (strncmp("S-", (const char *)v->data, 2) != 0) return false;
135         
136         return true;
137 }
138
139 /*
140   compare two objectSids
141 */
142 static int ldif_comparison_objectSid(struct ldb_context *ldb, void *mem_ctx,
143                                     const struct ldb_val *v1, const struct ldb_val *v2)
144 {
145         if (ldif_comparision_objectSid_isString(v1) && ldif_comparision_objectSid_isString(v2)) {
146                 return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
147         } else if (ldif_comparision_objectSid_isString(v1)
148                    && !ldif_comparision_objectSid_isString(v2)) {
149                 struct ldb_val v;
150                 int ret;
151                 if (ldif_read_objectSid(ldb, mem_ctx, v1, &v) != 0) {
152                         /* Perhaps not a string after all */
153                         return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
154                 }
155                 ret = ldb_comparison_binary(ldb, mem_ctx, &v, v2);
156                 talloc_free(v.data);
157                 return ret;
158         } else if (!ldif_comparision_objectSid_isString(v1)
159                    && ldif_comparision_objectSid_isString(v2)) {
160                 struct ldb_val v;
161                 int ret;
162                 if (ldif_read_objectSid(ldb, mem_ctx, v2, &v) != 0) {
163                         /* Perhaps not a string after all */
164                         return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
165                 }
166                 ret = ldb_comparison_binary(ldb, mem_ctx, v1, &v);
167                 talloc_free(v.data);
168                 return ret;
169         }
170         return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
171 }
172
173 /*
174   canonicalise a objectSid
175 */
176 static int ldif_canonicalise_objectSid(struct ldb_context *ldb, void *mem_ctx,
177                                       const struct ldb_val *in, struct ldb_val *out)
178 {
179         if (ldif_comparision_objectSid_isString(in)) {
180                 if (ldif_read_objectSid(ldb, mem_ctx, in, out) != 0) {
181                         /* Perhaps not a string after all */
182                         return ldb_handler_copy(ldb, mem_ctx, in, out);
183                 }
184                 return 0;
185         }
186         return ldb_handler_copy(ldb, mem_ctx, in, out);
187 }
188
189 static int extended_dn_read_SID(struct ldb_context *ldb, void *mem_ctx,
190                               const struct ldb_val *in, struct ldb_val *out)
191 {
192         struct dom_sid sid;
193         enum ndr_err_code ndr_err;
194         if (ldif_comparision_objectSid_isString(in)) {
195                 if (ldif_read_objectSid(ldb, mem_ctx, in, out) == 0) {
196                         return 0;
197                 }
198         }
199         
200         /* Perhaps not a string after all */
201         *out = data_blob_talloc(mem_ctx, NULL, in->length/2+1);
202
203         if (!out->data) {
204                 return -1;
205         }
206
207         (*out).length = strhex_to_str((char *)out->data, out->length,
208                                      (const char *)in->data, in->length);
209
210         /* Check it looks like a SID */
211         ndr_err = ndr_pull_struct_blob_all(out, mem_ctx, &sid,
212                                            (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
213         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
214                 return -1;
215         }
216         return 0;
217 }
218
219 /*
220   convert a ldif formatted objectGUID to a NDR formatted blob
221 */
222 static int ldif_read_objectGUID(struct ldb_context *ldb, void *mem_ctx,
223                                 const struct ldb_val *in, struct ldb_val *out)
224 {
225         struct GUID guid;
226         NTSTATUS status;
227
228         status = GUID_from_data_blob(in, &guid);
229         if (!NT_STATUS_IS_OK(status)) {
230                 return -1;
231         }
232
233         status = GUID_to_ndr_blob(&guid, mem_ctx, out);
234         if (!NT_STATUS_IS_OK(status)) {
235                 return -1;
236         }
237         return 0;
238 }
239
240 /*
241   convert a NDR formatted blob to a ldif formatted objectGUID
242 */
243 static int ldif_write_objectGUID(struct ldb_context *ldb, void *mem_ctx,
244                                  const struct ldb_val *in, struct ldb_val *out)
245 {
246         struct GUID guid;
247         NTSTATUS status;
248
249         status = GUID_from_ndr_blob(in, &guid);
250         if (!NT_STATUS_IS_OK(status)) {
251                 return -1;
252         }
253         out->data = (uint8_t *)GUID_string(mem_ctx, &guid);
254         if (out->data == NULL) {
255                 return -1;
256         }
257         out->length = strlen((const char *)out->data);
258         return 0;
259 }
260
261 static bool ldif_comparision_objectGUID_isString(const struct ldb_val *v)
262 {
263         if (v->length != 36 && v->length != 38) return false;
264
265         /* Might be a GUID string, can't be a binary GUID (fixed 16 bytes) */
266         return true;
267 }
268
269 static int extended_dn_read_GUID(struct ldb_context *ldb, void *mem_ctx,
270                               const struct ldb_val *in, struct ldb_val *out)
271 {
272         struct GUID guid;
273         NTSTATUS status;
274
275         if (in->length == 36 && ldif_read_objectGUID(ldb, mem_ctx, in, out) == 0) {
276                 return 0;
277         }
278
279         /* Try as 'hex' form */
280         if (in->length != 32) {
281                 return -1;
282         }
283                 
284         *out = data_blob_talloc(mem_ctx, NULL, in->length/2+1);
285         
286         if (!out->data) {
287                 return -1;
288         }
289         
290         (*out).length = strhex_to_str((char *)out->data, out->length,
291                                       (const char *)in->data, in->length);
292         
293         /* Check it looks like a GUID */
294         status = GUID_from_ndr_blob(out, &guid);
295         if (!NT_STATUS_IS_OK(status)) {
296                 data_blob_free(out);
297                 return -1;
298         }
299         return 0;
300 }
301
302 /*
303   compare two objectGUIDs
304 */
305 static int ldif_comparison_objectGUID(struct ldb_context *ldb, void *mem_ctx,
306                                      const struct ldb_val *v1, const struct ldb_val *v2)
307 {
308         if (ldif_comparision_objectGUID_isString(v1) && ldif_comparision_objectGUID_isString(v2)) {
309                 return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
310         } else if (ldif_comparision_objectGUID_isString(v1)
311                    && !ldif_comparision_objectGUID_isString(v2)) {
312                 struct ldb_val v;
313                 int ret;
314                 if (ldif_read_objectGUID(ldb, mem_ctx, v1, &v) != 0) {
315                         /* Perhaps it wasn't a valid string after all */
316                         return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
317                 }
318                 ret = ldb_comparison_binary(ldb, mem_ctx, &v, v2);
319                 talloc_free(v.data);
320                 return ret;
321         } else if (!ldif_comparision_objectGUID_isString(v1)
322                    && ldif_comparision_objectGUID_isString(v2)) {
323                 struct ldb_val v;
324                 int ret;
325                 if (ldif_read_objectGUID(ldb, mem_ctx, v2, &v) != 0) {
326                         /* Perhaps it wasn't a valid string after all */
327                         return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
328                 }
329                 ret = ldb_comparison_binary(ldb, mem_ctx, v1, &v);
330                 talloc_free(v.data);
331                 return ret;
332         }
333         return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
334 }
335
336 /*
337   canonicalise a objectGUID
338 */
339 static int ldif_canonicalise_objectGUID(struct ldb_context *ldb, void *mem_ctx,
340                                        const struct ldb_val *in, struct ldb_val *out)
341 {
342         if (ldif_comparision_objectGUID_isString(in)) {
343                 if (ldif_read_objectGUID(ldb, mem_ctx, in, out) != 0) {
344                         /* Perhaps it wasn't a valid string after all */
345                         return ldb_handler_copy(ldb, mem_ctx, in, out);
346                 }
347                 return 0;
348         }
349         return ldb_handler_copy(ldb, mem_ctx, in, out);
350 }
351
352
353 /*
354   convert a ldif (SDDL) formatted ntSecurityDescriptor to a NDR formatted blob
355 */
356 static int ldif_read_ntSecurityDescriptor(struct ldb_context *ldb, void *mem_ctx,
357                                           const struct ldb_val *in, struct ldb_val *out)
358 {
359         struct security_descriptor *sd;
360         enum ndr_err_code ndr_err;
361
362         sd = talloc(mem_ctx, struct security_descriptor);
363         if (sd == NULL) {
364                 return -1;
365         }
366
367         ndr_err = ndr_pull_struct_blob(in, sd, sd,
368                                        (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
369         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
370                 /* If this does not parse, then it is probably SDDL, and we should try it that way */
371                 
372                 const struct dom_sid *sid = samdb_domain_sid(ldb);
373                 talloc_free(sd);
374                 sd = sddl_decode(mem_ctx, (const char *)in->data, sid);
375                 if (sd == NULL) {
376                         return -1;
377                 }
378         }
379
380         ndr_err = ndr_push_struct_blob(out, mem_ctx, sd,
381                                        (ndr_push_flags_fn_t)ndr_push_security_descriptor);
382         talloc_free(sd);
383         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
384                 return -1;
385         }
386
387         return 0;
388 }
389
390 /*
391   convert a NDR formatted blob to a ldif formatted ntSecurityDescriptor (SDDL format)
392 */
393 static int ldif_write_ntSecurityDescriptor(struct ldb_context *ldb, void *mem_ctx,
394                                            const struct ldb_val *in, struct ldb_val *out)
395 {
396         struct security_descriptor *sd;
397         enum ndr_err_code ndr_err;
398
399         if (ldb_get_flags(ldb) & LDB_FLG_SHOW_BINARY) {
400                 return ldif_write_NDR(ldb, mem_ctx, in, out, 
401                                       sizeof(struct security_descriptor),
402                                       (ndr_pull_flags_fn_t)ndr_pull_security_descriptor,
403                                       (ndr_print_fn_t)ndr_print_security_descriptor,
404                                       true);
405                                       
406         }
407
408         sd = talloc(mem_ctx, struct security_descriptor);
409         if (sd == NULL) {
410                 return -1;
411         }
412         /* We can't use ndr_pull_struct_blob_all because this contains relative pointers */
413         ndr_err = ndr_pull_struct_blob(in, sd, sd,
414                                            (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
415         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
416                 talloc_free(sd);
417                 return -1;
418         }
419         out->data = (uint8_t *)sddl_encode(mem_ctx, sd, samdb_domain_sid_cache_only(ldb));
420         talloc_free(sd);
421         if (out->data == NULL) {
422                 return -1;
423         }
424         out->length = strlen((const char *)out->data);
425         return 0;
426 }
427
428 /* 
429    canonicalise an objectCategory.  We use the short form as the cannoical form:
430    cn=Person,cn=Schema,cn=Configuration,<basedn> becomes 'person'
431 */
432
433 static int ldif_canonicalise_objectCategory(struct ldb_context *ldb, void *mem_ctx,
434                                             const struct ldb_val *in, struct ldb_val *out)
435 {
436         struct ldb_dn *dn1 = NULL;
437         const struct dsdb_schema *schema = dsdb_get_schema(ldb, NULL);
438         const struct dsdb_class *sclass;
439         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
440         if (!tmp_ctx) {
441                 return LDB_ERR_OPERATIONS_ERROR;
442         }
443
444         if (!schema) {
445                 talloc_free(tmp_ctx);
446                 *out = data_blob_talloc(mem_ctx, in->data, in->length);
447                 if (in->data && !out->data) {
448                         return LDB_ERR_OPERATIONS_ERROR;
449                 }
450                 return LDB_SUCCESS;
451         }
452         dn1 = ldb_dn_from_ldb_val(tmp_ctx, ldb, in);
453         if ( ! ldb_dn_validate(dn1)) {
454                 const char *lDAPDisplayName = talloc_strndup(tmp_ctx, (char *)in->data, in->length);
455                 sclass = dsdb_class_by_lDAPDisplayName(schema, lDAPDisplayName);
456                 if (sclass) {
457                         struct ldb_dn *dn = ldb_dn_new(mem_ctx, ldb,  
458                                                        sclass->defaultObjectCategory);
459                         *out = data_blob_string_const(ldb_dn_alloc_casefold(mem_ctx, dn));
460                         talloc_free(tmp_ctx);
461
462                         if (!out->data) {
463                                 return LDB_ERR_OPERATIONS_ERROR;
464                         }
465                         return LDB_SUCCESS;
466                 } else {
467                         *out = data_blob_talloc(mem_ctx, in->data, in->length);
468                         talloc_free(tmp_ctx);
469
470                         if (in->data && !out->data) {
471                                 return LDB_ERR_OPERATIONS_ERROR;
472                         }
473                         return LDB_SUCCESS;
474                 }
475         }
476         *out = data_blob_string_const(ldb_dn_alloc_casefold(mem_ctx, dn1));
477         talloc_free(tmp_ctx);
478
479         if (!out->data) {
480                 return LDB_ERR_OPERATIONS_ERROR;
481         }
482         return LDB_SUCCESS;
483 }
484
485 static int ldif_comparison_objectCategory(struct ldb_context *ldb, void *mem_ctx,
486                                           const struct ldb_val *v1,
487                                           const struct ldb_val *v2)
488 {
489         return ldb_any_comparison(ldb, mem_ctx, ldif_canonicalise_objectCategory,
490                                   v1, v2);
491 }
492
493 /*
494   convert a NDR formatted blob to a ldif formatted schemaInfo
495 */
496 static int ldif_write_schemaInfo(struct ldb_context *ldb, void *mem_ctx,
497                                  const struct ldb_val *in, struct ldb_val *out)
498 {
499         return ldif_write_NDR(ldb, mem_ctx, in, out,
500                               sizeof(struct repsFromToBlob),
501                               (ndr_pull_flags_fn_t)ndr_pull_schemaInfoBlob,
502                               (ndr_print_fn_t)ndr_print_schemaInfoBlob,
503                               true);
504 }
505
506 /*
507   convert a ldif formatted prefixMap to a NDR formatted blob
508 */
509 static int ldif_read_prefixMap(struct ldb_context *ldb, void *mem_ctx,
510                                const struct ldb_val *in, struct ldb_val *out)
511 {
512         struct prefixMapBlob *blob;
513         enum ndr_err_code ndr_err;
514         char *string, *line, *p, *oid;
515         DATA_BLOB oid_blob;
516
517         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
518
519         if (tmp_ctx == NULL) {
520                 return -1;
521         }
522
523         blob = talloc_zero(tmp_ctx, struct prefixMapBlob);
524         if (blob == NULL) {
525                 talloc_free(tmp_ctx);
526                 return -1;
527         }
528
529         ndr_err = ndr_pull_struct_blob(in, tmp_ctx, blob,
530                                        (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
531         if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
532                 ndr_err = ndr_push_struct_blob(out, mem_ctx,
533                                                blob,
534                                                (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
535                 talloc_free(tmp_ctx);
536                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
537                         return -1;
538                 }
539                 return 0;
540         }
541
542         /* If this does not parse, then it is probably the text version, and we should try it that way */
543         blob->version = PREFIX_MAP_VERSION_DSDB;
544         
545         string = talloc_strndup(mem_ctx, (const char *)in->data, in->length);
546         if (string == NULL) {
547                 talloc_free(blob);
548                 return -1;
549         }
550
551         line = string;
552         while (line && line[0]) {
553                 p=strchr(line, ';');
554                 if (p) {
555                         p[0] = '\0';
556                 } else {
557                         p=strchr(line, '\n');
558                         if (p) {
559                                 p[0] = '\0';
560                         }
561                 }
562                 /* allow a trailing separator */
563                 if (line == p) {
564                         break;
565                 }
566                 
567                 blob->ctr.dsdb.mappings = talloc_realloc(blob, 
568                                                          blob->ctr.dsdb.mappings, 
569                                                          struct drsuapi_DsReplicaOIDMapping,
570                                                          blob->ctr.dsdb.num_mappings+1);
571                 if (!blob->ctr.dsdb.mappings) {
572                         talloc_free(tmp_ctx);
573                         return -1;
574                 }
575
576                 blob->ctr.dsdb.mappings[blob->ctr.dsdb.num_mappings].id_prefix = strtoul(line, &oid, 10);
577
578                 if (oid[0] != ':') {
579                         talloc_free(tmp_ctx);
580                         return -1;
581                 }
582
583                 /* we know there must be at least ":" */
584                 oid++;
585
586                 if (!ber_write_partial_OID_String(blob->ctr.dsdb.mappings, &oid_blob, oid)) {
587                         talloc_free(tmp_ctx);
588                         return -1;
589                 }
590                 blob->ctr.dsdb.mappings[blob->ctr.dsdb.num_mappings].oid.length = oid_blob.length;
591                 blob->ctr.dsdb.mappings[blob->ctr.dsdb.num_mappings].oid.binary_oid = oid_blob.data;
592
593                 blob->ctr.dsdb.num_mappings++;
594
595                 /* Now look past the terminator we added above */
596                 if (p) {
597                         line = p + 1;
598                 } else {
599                         line = NULL;
600                 }
601         }
602
603         ndr_err = ndr_push_struct_blob(out, mem_ctx, 
604                                        blob,
605                                        (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
606         talloc_free(tmp_ctx);
607         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
608                 return -1;
609         }
610         return 0;
611 }
612
613 /*
614   convert a NDR formatted blob to a ldif formatted prefixMap
615 */
616 static int ldif_write_prefixMap(struct ldb_context *ldb, void *mem_ctx,
617                                 const struct ldb_val *in, struct ldb_val *out)
618 {
619         struct prefixMapBlob *blob;
620         enum ndr_err_code ndr_err;
621         char *string;
622         uint32_t i;
623
624         if (ldb_get_flags(ldb) & LDB_FLG_SHOW_BINARY) {
625                 int err;
626                 /* try to decode the blob as S4 prefixMap */
627                 err = ldif_write_NDR(ldb, mem_ctx, in, out,
628                                      sizeof(struct prefixMapBlob),
629                                      (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob,
630                                      (ndr_print_fn_t)ndr_print_prefixMapBlob,
631                                      false);
632                 if (0 == err) {
633                         return err;
634                 }
635                 /* try parsing it as Windows PrefixMap value */
636                 return ldif_write_NDR(ldb, mem_ctx, in, out,
637                                       sizeof(struct drsuapi_MSPrefixMap_Ctr),
638                                       (ndr_pull_flags_fn_t)ndr_pull_drsuapi_MSPrefixMap_Ctr,
639                                       (ndr_print_fn_t)ndr_print_drsuapi_MSPrefixMap_Ctr,
640                                       true);
641         }
642
643         blob = talloc(mem_ctx, struct prefixMapBlob);
644         if (blob == NULL) {
645                 return -1;
646         }
647         ndr_err = ndr_pull_struct_blob_all(in, blob, 
648                                            blob,
649                                            (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
650         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
651                 goto failed;
652         }
653         if (blob->version != PREFIX_MAP_VERSION_DSDB) {
654                 goto failed;
655         }
656         string = talloc_strdup(mem_ctx, "");
657         if (string == NULL) {
658                 goto failed;
659         }
660
661         for (i=0; i < blob->ctr.dsdb.num_mappings; i++) {
662                 DATA_BLOB oid_blob;
663                 const char *partial_oid = NULL;
664
665                 if (i > 0) {
666                         string = talloc_asprintf_append(string, ";"); 
667                 }
668
669                 oid_blob = data_blob_const(blob->ctr.dsdb.mappings[i].oid.binary_oid,
670                                            blob->ctr.dsdb.mappings[i].oid.length);
671                 if (!ber_read_partial_OID_String(blob, oid_blob, &partial_oid)) {
672                         DEBUG(0, ("ber_read_partial_OID failed on prefixMap item with id: 0x%X",
673                                   blob->ctr.dsdb.mappings[i].id_prefix));
674                         goto failed;
675                 }
676                 string = talloc_asprintf_append(string, "%u:%s", 
677                                                    blob->ctr.dsdb.mappings[i].id_prefix,
678                                                    partial_oid);
679                 talloc_free(discard_const(partial_oid));
680                 if (string == NULL) {
681                         goto failed;
682                 }
683         }
684
685         talloc_free(blob);
686         *out = data_blob_string_const(string);
687         return 0;
688
689 failed:
690         talloc_free(blob);
691         return -1;
692 }
693
694 static bool ldif_comparision_prefixMap_isString(const struct ldb_val *v)
695 {
696         if (v->length < 4) {
697                 return true;
698         }
699
700         if (IVAL(v->data, 0) == PREFIX_MAP_VERSION_DSDB) {
701                 return false;
702         }
703         
704         return true;
705 }
706
707 /*
708   canonicalise a prefixMap
709 */
710 static int ldif_canonicalise_prefixMap(struct ldb_context *ldb, void *mem_ctx,
711                                        const struct ldb_val *in, struct ldb_val *out)
712 {
713         if (ldif_comparision_prefixMap_isString(in)) {
714                 return ldif_read_prefixMap(ldb, mem_ctx, in, out);
715         }
716         return ldb_handler_copy(ldb, mem_ctx, in, out);
717 }
718
719 static int ldif_comparison_prefixMap(struct ldb_context *ldb, void *mem_ctx,
720                                      const struct ldb_val *v1,
721                                      const struct ldb_val *v2)
722 {
723         return ldb_any_comparison(ldb, mem_ctx, ldif_canonicalise_prefixMap,
724                                   v1, v2);
725 }
726
727 /* length limited conversion of a ldb_val to a int32_t */
728 static int val_to_int32(const struct ldb_val *in, int32_t *v)
729 {
730         char *end;
731         char buf[64];
732
733         /* make sure we don't read past the end of the data */
734         if (in->length > sizeof(buf)-1) {
735                 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
736         }
737         strncpy(buf, (char *)in->data, in->length);
738         buf[in->length] = 0;
739
740         /* We've to use "strtoll" here to have the intended overflows.
741          * Otherwise we may get "LONG_MAX" and the conversion is wrong. */
742         *v = (int32_t) strtoll(buf, &end, 0);
743         if (*end != 0) {
744                 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
745         }
746         return LDB_SUCCESS;
747 }
748
749 /* length limited conversion of a ldb_val to a int64_t */
750 static int val_to_int64(const struct ldb_val *in, int64_t *v)
751 {
752         char *end;
753         char buf[64];
754
755         /* make sure we don't read past the end of the data */
756         if (in->length > sizeof(buf)-1) {
757                 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
758         }
759         strncpy(buf, (char *)in->data, in->length);
760         buf[in->length] = 0;
761
762         *v = (int64_t) strtoll(buf, &end, 0);
763         if (*end != 0) {
764                 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
765         }
766         return LDB_SUCCESS;
767 }
768
769 /* Canonicalisation of two 32-bit integers */
770 static int ldif_canonicalise_int32(struct ldb_context *ldb, void *mem_ctx,
771                         const struct ldb_val *in, struct ldb_val *out)
772 {
773         int32_t i;
774         int ret;
775
776         ret = val_to_int32(in, &i);
777         if (ret != LDB_SUCCESS) {
778                 return ret;
779         }
780         out->data = (uint8_t *) talloc_asprintf(mem_ctx, "%d", i);
781         if (out->data == NULL) {
782                 ldb_oom(ldb);
783                 return LDB_ERR_OPERATIONS_ERROR;
784         }
785         out->length = strlen((char *)out->data);
786         return 0;
787 }
788
789 /* Comparison of two 32-bit integers */
790 static int ldif_comparison_int32(struct ldb_context *ldb, void *mem_ctx,
791                                  const struct ldb_val *v1, const struct ldb_val *v2)
792 {
793         int32_t i1=0, i2=0;
794         val_to_int32(v1, &i1);
795         val_to_int32(v2, &i2);
796         if (i1 == i2) return 0;
797         return i1 > i2? 1 : -1;
798 }
799
800 /* Canonicalisation of two 64-bit integers */
801 static int ldif_canonicalise_int64(struct ldb_context *ldb, void *mem_ctx,
802                                    const struct ldb_val *in, struct ldb_val *out)
803 {
804         int64_t i;
805         int ret;
806
807         ret = val_to_int64(in, &i);
808         if (ret != LDB_SUCCESS) {
809                 return ret;
810         }
811         out->data = (uint8_t *) talloc_asprintf(mem_ctx, "%lld", (long long)i);
812         if (out->data == NULL) {
813                 ldb_oom(ldb);
814                 return LDB_ERR_OPERATIONS_ERROR;
815         }
816         out->length = strlen((char *)out->data);
817         return 0;
818 }
819
820 /* Comparison of two 64-bit integers */
821 static int ldif_comparison_int64(struct ldb_context *ldb, void *mem_ctx,
822                                  const struct ldb_val *v1, const struct ldb_val *v2)
823 {
824         int64_t i1=0, i2=0;
825         val_to_int64(v1, &i1);
826         val_to_int64(v2, &i2);
827         if (i1 == i2) return 0;
828         return i1 > i2? 1 : -1;
829 }
830
831 /*
832   convert a NDR formatted blob to a ldif formatted repsFromTo
833 */
834 static int ldif_write_repsFromTo(struct ldb_context *ldb, void *mem_ctx,
835                                  const struct ldb_val *in, struct ldb_val *out)
836 {
837         return ldif_write_NDR(ldb, mem_ctx, in, out, 
838                               sizeof(struct repsFromToBlob),
839                               (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob,
840                               (ndr_print_fn_t)ndr_print_repsFromToBlob,
841                               true);
842 }
843
844 /*
845   convert a NDR formatted blob to a ldif formatted replPropertyMetaData
846 */
847 static int ldif_write_replPropertyMetaData(struct ldb_context *ldb, void *mem_ctx,
848                                            const struct ldb_val *in, struct ldb_val *out)
849 {
850         return ldif_write_NDR(ldb, mem_ctx, in, out, 
851                               sizeof(struct replPropertyMetaDataBlob),
852                               (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob,
853                               (ndr_print_fn_t)ndr_print_replPropertyMetaDataBlob,
854                               true);
855 }
856
857 /*
858   convert a NDR formatted blob to a ldif formatted replUpToDateVector
859 */
860 static int ldif_write_replUpToDateVector(struct ldb_context *ldb, void *mem_ctx,
861                                          const struct ldb_val *in, struct ldb_val *out)
862 {
863         return ldif_write_NDR(ldb, mem_ctx, in, out, 
864                               sizeof(struct replUpToDateVectorBlob),
865                               (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob,
866                               (ndr_print_fn_t)ndr_print_replUpToDateVectorBlob,
867                               true);
868 }
869
870
871 static int extended_dn_write_hex(struct ldb_context *ldb, void *mem_ctx,
872                                  const struct ldb_val *in, struct ldb_val *out)
873 {
874         *out = data_blob_string_const(data_blob_hex_string_lower(mem_ctx, in));
875         if (!out->data) {
876                 return -1;
877         }
878         return 0;
879 }
880
881 /*
882   compare two dns
883 */
884 static int samba_ldb_dn_link_comparison(struct ldb_context *ldb, void *mem_ctx,
885                                         const struct ldb_val *v1, const struct ldb_val *v2)
886 {
887         struct ldb_dn *dn1 = NULL, *dn2 = NULL;
888         int ret;
889
890         if (dsdb_dn_is_deleted_val(v1)) {
891                 /* If the DN is deleted, then we can't search for it */
892                 return -1;
893         }
894
895         if (dsdb_dn_is_deleted_val(v2)) {
896                 /* If the DN is deleted, then we can't search for it */
897                 return -1;
898         }
899
900         dn1 = ldb_dn_from_ldb_val(mem_ctx, ldb, v1);
901         if ( ! ldb_dn_validate(dn1)) return -1;
902
903         dn2 = ldb_dn_from_ldb_val(mem_ctx, ldb, v2);
904         if ( ! ldb_dn_validate(dn2)) {
905                 talloc_free(dn1);
906                 return -1;
907         }
908
909         ret = ldb_dn_compare(dn1, dn2);
910
911         talloc_free(dn1);
912         talloc_free(dn2);
913         return ret;
914 }
915
916 static int samba_ldb_dn_link_canonicalise(struct ldb_context *ldb, void *mem_ctx,
917                                           const struct ldb_val *in, struct ldb_val *out)
918 {
919         struct ldb_dn *dn;
920         int ret = -1;
921
922         out->length = 0;
923         out->data = NULL;
924
925         dn = ldb_dn_from_ldb_val(mem_ctx, ldb, in);
926         if ( ! ldb_dn_validate(dn)) {
927                 return LDB_ERR_INVALID_DN_SYNTAX;
928         }
929
930         /* By including the RMD_FLAGS of a deleted DN, we ensure it
931          * does not casually match a not deleted DN */
932         if (dsdb_dn_is_deleted_val(in)) {
933                 out->data = (uint8_t *)talloc_asprintf(mem_ctx,
934                                                        "<RMD_FLAGS=%u>%s",
935                                                        dsdb_dn_val_rmd_flags(in),
936                                                        ldb_dn_get_casefold(dn));
937         } else {
938                 out->data = (uint8_t *)ldb_dn_alloc_casefold(mem_ctx, dn);
939         }
940
941         if (out->data == NULL) {
942                 goto done;
943         }
944         out->length = strlen((char *)out->data);
945
946         ret = 0;
947
948 done:
949         talloc_free(dn);
950
951         return ret;
952 }
953
954
955 /*
956   write a 64 bit 2-part range
957 */
958 static int ldif_write_range64(struct ldb_context *ldb, void *mem_ctx,
959                               const struct ldb_val *in, struct ldb_val *out)
960 {
961         int64_t v;
962         int ret;
963         ret = val_to_int64(in, &v);
964         if (ret != LDB_SUCCESS) {
965                 return ret;
966         }
967         out->data = (uint8_t *)talloc_asprintf(mem_ctx, "%lu-%lu",
968                                                (unsigned long)(v&0xFFFFFFFF),
969                                                (unsigned long)(v>>32));
970         if (out->data == NULL) {
971                 ldb_oom(ldb);
972                 return LDB_ERR_OPERATIONS_ERROR;
973         }
974         out->length = strlen((char *)out->data);
975         return LDB_SUCCESS;
976 }
977
978 /*
979   read a 64 bit 2-part range
980 */
981 static int ldif_read_range64(struct ldb_context *ldb, void *mem_ctx,
982                               const struct ldb_val *in, struct ldb_val *out)
983 {
984         unsigned long high, low;
985         char buf[64];
986
987         if (memchr(in->data, '-', in->length) == NULL) {
988                 return ldb_handler_copy(ldb, mem_ctx, in, out);
989         }
990
991         if (in->length > sizeof(buf)-1) {
992                 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
993         }
994         strncpy(buf, (const char *)in->data, in->length);
995         buf[in->length] = 0;
996
997         if (sscanf(buf, "%lu-%lu", &low, &high) != 2) {
998                 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
999         }
1000
1001         out->data = (uint8_t *)talloc_asprintf(mem_ctx, "%llu",
1002                                                (unsigned long long)(((uint64_t)high)<<32) | (low));
1003
1004         if (out->data == NULL) {
1005                 ldb_oom(ldb);
1006                 return LDB_ERR_OPERATIONS_ERROR;
1007         }
1008         out->length = strlen((char *)out->data);
1009         return LDB_SUCCESS;
1010 }
1011
1012 static const struct ldb_schema_syntax samba_syntaxes[] = {
1013         {
1014                 .name             = LDB_SYNTAX_SAMBA_SID,
1015                 .ldif_read_fn     = ldif_read_objectSid,
1016                 .ldif_write_fn    = ldif_write_objectSid,
1017                 .canonicalise_fn  = ldif_canonicalise_objectSid,
1018                 .comparison_fn    = ldif_comparison_objectSid
1019         },{
1020                 .name             = LDB_SYNTAX_SAMBA_SECURITY_DESCRIPTOR,
1021                 .ldif_read_fn     = ldif_read_ntSecurityDescriptor,
1022                 .ldif_write_fn    = ldif_write_ntSecurityDescriptor,
1023                 .canonicalise_fn  = ldb_handler_copy,
1024                 .comparison_fn    = ldb_comparison_binary
1025         },{
1026                 .name             = LDB_SYNTAX_SAMBA_GUID,
1027                 .ldif_read_fn     = ldif_read_objectGUID,
1028                 .ldif_write_fn    = ldif_write_objectGUID,
1029                 .canonicalise_fn  = ldif_canonicalise_objectGUID,
1030                 .comparison_fn    = ldif_comparison_objectGUID
1031         },{
1032                 .name             = LDB_SYNTAX_SAMBA_OBJECT_CATEGORY,
1033                 .ldif_read_fn     = ldb_handler_copy,
1034                 .ldif_write_fn    = ldb_handler_copy,
1035                 .canonicalise_fn  = ldif_canonicalise_objectCategory,
1036                 .comparison_fn    = ldif_comparison_objectCategory
1037         },{
1038                 .name             = LDB_SYNTAX_SAMBA_SCHEMAINFO,
1039                 .ldif_read_fn     = ldb_handler_copy,
1040                 .ldif_write_fn    = ldif_write_schemaInfo,
1041                 .canonicalise_fn  = ldb_handler_copy,
1042                 .comparison_fn    = ldb_comparison_binary
1043         },{
1044                 .name             = LDB_SYNTAX_SAMBA_PREFIX_MAP,
1045                 .ldif_read_fn     = ldif_read_prefixMap,
1046                 .ldif_write_fn    = ldif_write_prefixMap,
1047                 .canonicalise_fn  = ldif_canonicalise_prefixMap,
1048                 .comparison_fn    = ldif_comparison_prefixMap
1049         },{
1050                 .name             = LDB_SYNTAX_SAMBA_INT32,
1051                 .ldif_read_fn     = ldb_handler_copy,
1052                 .ldif_write_fn    = ldb_handler_copy,
1053                 .canonicalise_fn  = ldif_canonicalise_int32,
1054                 .comparison_fn    = ldif_comparison_int32
1055         },{
1056                 .name             = LDB_SYNTAX_SAMBA_REPSFROMTO,
1057                 .ldif_read_fn     = ldb_handler_copy,
1058                 .ldif_write_fn    = ldif_write_repsFromTo,
1059                 .canonicalise_fn  = ldb_handler_copy,
1060                 .comparison_fn    = ldb_comparison_binary
1061         },{
1062                 .name             = LDB_SYNTAX_SAMBA_REPLPROPERTYMETADATA,
1063                 .ldif_read_fn     = ldb_handler_copy,
1064                 .ldif_write_fn    = ldif_write_replPropertyMetaData,
1065                 .canonicalise_fn  = ldb_handler_copy,
1066                 .comparison_fn    = ldb_comparison_binary
1067         },{
1068                 .name             = LDB_SYNTAX_SAMBA_REPLUPTODATEVECTOR,
1069                 .ldif_read_fn     = ldb_handler_copy,
1070                 .ldif_write_fn    = ldif_write_replUpToDateVector,
1071                 .canonicalise_fn  = ldb_handler_copy,
1072                 .comparison_fn    = ldb_comparison_binary
1073         },{
1074                 .name             = DSDB_SYNTAX_BINARY_DN,
1075                 .ldif_read_fn     = ldb_handler_copy,
1076                 .ldif_write_fn    = ldb_handler_copy,
1077                 .canonicalise_fn  = dsdb_dn_binary_canonicalise,
1078                 .comparison_fn    = dsdb_dn_binary_comparison
1079         },{
1080                 .name             = DSDB_SYNTAX_STRING_DN,
1081                 .ldif_read_fn     = ldb_handler_copy,
1082                 .ldif_write_fn    = ldb_handler_copy,
1083                 .canonicalise_fn  = dsdb_dn_string_canonicalise,
1084                 .comparison_fn    = dsdb_dn_string_comparison
1085         },{
1086                 .name             = LDB_SYNTAX_DN,
1087                 .ldif_read_fn     = ldb_handler_copy,
1088                 .ldif_write_fn    = ldb_handler_copy,
1089                 .canonicalise_fn  = samba_ldb_dn_link_canonicalise,
1090                 .comparison_fn    = samba_ldb_dn_link_comparison,
1091         },{
1092                 .name             = LDB_SYNTAX_SAMBA_RANGE64,
1093                 .ldif_read_fn     = ldif_read_range64,
1094                 .ldif_write_fn    = ldif_write_range64,
1095                 .canonicalise_fn  = ldif_canonicalise_int64,
1096                 .comparison_fn    = ldif_comparison_int64
1097         },
1098 };
1099
1100 static const struct ldb_dn_extended_syntax samba_dn_syntax[] = {
1101         {
1102                 .name             = "SID",
1103                 .read_fn          = extended_dn_read_SID,
1104                 .write_clear_fn   = ldif_write_objectSid,
1105                 .write_hex_fn     = extended_dn_write_hex
1106         },{
1107                 .name             = "GUID",
1108                 .read_fn          = extended_dn_read_GUID,
1109                 .write_clear_fn   = ldif_write_objectGUID,
1110                 .write_hex_fn     = extended_dn_write_hex
1111         },{
1112                 .name             = "WKGUID",
1113                 .read_fn          = ldb_handler_copy,
1114                 .write_clear_fn   = ldb_handler_copy,
1115                 .write_hex_fn     = ldb_handler_copy
1116         },{
1117                 .name             = "RMD_INVOCID",
1118                 .read_fn          = extended_dn_read_GUID,
1119                 .write_clear_fn   = ldif_write_objectGUID,
1120                 .write_hex_fn     = extended_dn_write_hex
1121         },{
1122                 .name             = "RMD_FLAGS",
1123                 .read_fn          = ldb_handler_copy,
1124                 .write_clear_fn   = ldb_handler_copy,
1125                 .write_hex_fn     = ldb_handler_copy
1126         },{
1127                 .name             = "RMD_ADDTIME",
1128                 .read_fn          = ldb_handler_copy,
1129                 .write_clear_fn   = ldb_handler_copy,
1130                 .write_hex_fn     = ldb_handler_copy
1131         },{
1132                 .name             = "RMD_CHANGETIME",
1133                 .read_fn          = ldb_handler_copy,
1134                 .write_clear_fn   = ldb_handler_copy,
1135                 .write_hex_fn     = ldb_handler_copy
1136         },{
1137                 .name             = "RMD_LOCAL_USN",
1138                 .read_fn          = ldb_handler_copy,
1139                 .write_clear_fn   = ldb_handler_copy,
1140                 .write_hex_fn     = ldb_handler_copy
1141         },{
1142                 .name             = "RMD_ORIGINATING_USN",
1143                 .read_fn          = ldb_handler_copy,
1144                 .write_clear_fn   = ldb_handler_copy,
1145                 .write_hex_fn     = ldb_handler_copy
1146         },{
1147                 .name             = "RMD_VERSION",
1148                 .read_fn          = ldb_handler_copy,
1149                 .write_clear_fn   = ldb_handler_copy,
1150                 .write_hex_fn     = ldb_handler_copy
1151         }
1152 };
1153
1154 /* TODO: Should be dynamic at some point */
1155 static const struct {
1156         const char *name;
1157         const char *syntax;
1158 } samba_attributes[] = {
1159         { "objectSid",                  LDB_SYNTAX_SAMBA_SID },
1160         { "securityIdentifier",         LDB_SYNTAX_SAMBA_SID },
1161         { "tokenGroups",                LDB_SYNTAX_SAMBA_SID },
1162         { "ntSecurityDescriptor",       LDB_SYNTAX_SAMBA_SECURITY_DESCRIPTOR },
1163         { "oMSyntax",                   LDB_SYNTAX_SAMBA_INT32 },
1164         { "objectCategory",             LDB_SYNTAX_SAMBA_OBJECT_CATEGORY },
1165         { "schemaInfo",                 LDB_SYNTAX_SAMBA_SCHEMAINFO },
1166         { "prefixMap",                  LDB_SYNTAX_SAMBA_PREFIX_MAP },
1167         { "repsFrom",                   LDB_SYNTAX_SAMBA_REPSFROMTO },
1168         { "repsTo",                     LDB_SYNTAX_SAMBA_REPSFROMTO },
1169         { "replPropertyMetaData",       LDB_SYNTAX_SAMBA_REPLPROPERTYMETADATA },
1170         { "replUpToDateVector",         LDB_SYNTAX_SAMBA_REPLUPTODATEVECTOR },
1171         { "rIDAllocationPool",          LDB_SYNTAX_SAMBA_RANGE64 },
1172         { "rIDPreviousAllocationPool",  LDB_SYNTAX_SAMBA_RANGE64 },
1173         { "rIDAvailablePool",           LDB_SYNTAX_SAMBA_RANGE64 },
1174
1175         /*
1176          * these are extracted by searching
1177          * (&(attributeSyntax=2.5.5.10)(rangeLower=16)(rangeUpper=16)(omSyntax=4))
1178          */
1179         { "attributeSecurityGUID",              LDB_SYNTAX_SAMBA_GUID },
1180         { "categoryId",                         LDB_SYNTAX_SAMBA_GUID },
1181         { "controlAccessRights",                LDB_SYNTAX_SAMBA_GUID },
1182         { "currMachineId",                      LDB_SYNTAX_SAMBA_GUID },
1183         { "fRSReplicaSetGUID",                  LDB_SYNTAX_SAMBA_GUID },
1184         { "fRSVersionGUID",                     LDB_SYNTAX_SAMBA_GUID },
1185         { "implementedCategories",              LDB_SYNTAX_SAMBA_GUID },
1186         { "msDS-AzObjectGuid",                  LDB_SYNTAX_SAMBA_GUID },
1187         { "msDFSR-ContentSetGuid",              LDB_SYNTAX_SAMBA_GUID },
1188         { "msDFSR-ReplicationGroupGuid",        LDB_SYNTAX_SAMBA_GUID },
1189         { "mSMQDigests",                        LDB_SYNTAX_SAMBA_GUID },
1190         { "mSMQOwnerID",                        LDB_SYNTAX_SAMBA_GUID },
1191         { "mSMQQMID",                           LDB_SYNTAX_SAMBA_GUID },
1192         { "mSMQQueueType",                      LDB_SYNTAX_SAMBA_GUID },
1193         { "mSMQSites",                          LDB_SYNTAX_SAMBA_GUID },
1194         { "netbootGUID",                        LDB_SYNTAX_SAMBA_GUID },
1195         { "objectGUID",                         LDB_SYNTAX_SAMBA_GUID },
1196         { "pKTGuid",                            LDB_SYNTAX_SAMBA_GUID },
1197         { "requiredCategories",                 LDB_SYNTAX_SAMBA_GUID },
1198         { "schemaIDGUID",                       LDB_SYNTAX_SAMBA_GUID },
1199         { "siteGUID",                           LDB_SYNTAX_SAMBA_GUID },
1200         { "msDFS-GenerationGUIDv2",             LDB_SYNTAX_SAMBA_GUID },
1201         { "msDFS-LinkIdentityGUIDv2",           LDB_SYNTAX_SAMBA_GUID },
1202         { "msDFS-NamespaceIdentityGUIDv2",      LDB_SYNTAX_SAMBA_GUID },
1203
1204         /*
1205          * these are known to be GUIDs
1206          */
1207         { "invocationId",                       LDB_SYNTAX_SAMBA_GUID },
1208         { "parentGUID",                         LDB_SYNTAX_SAMBA_GUID },
1209         { "msDS-OptionalFeatureGUID",           LDB_SYNTAX_SAMBA_GUID },
1210 };
1211
1212 const struct ldb_schema_syntax *ldb_samba_syntax_by_name(struct ldb_context *ldb, const char *name)
1213 {
1214         unsigned int j;
1215         const struct ldb_schema_syntax *s = NULL;
1216         
1217         for (j=0; j < ARRAY_SIZE(samba_syntaxes); j++) {
1218                 if (strcmp(name, samba_syntaxes[j].name) == 0) {
1219                         s = &samba_syntaxes[j];
1220                         break;
1221                 }
1222         }
1223         return s;
1224 }
1225
1226 const struct ldb_schema_syntax *ldb_samba_syntax_by_lDAPDisplayName(struct ldb_context *ldb, const char *name)
1227 {
1228         unsigned int j;
1229         const struct ldb_schema_syntax *s = NULL;
1230
1231         for (j=0; j < ARRAY_SIZE(samba_attributes); j++) {
1232                 if (strcmp(samba_attributes[j].name, name) == 0) {
1233                         s = ldb_samba_syntax_by_name(ldb, samba_attributes[j].syntax);
1234                         break;
1235                 }
1236         }
1237         
1238         return s;
1239 }
1240
1241 /*
1242   register the samba ldif handlers
1243 */
1244 int ldb_register_samba_handlers(struct ldb_context *ldb)
1245 {
1246         unsigned int i;
1247
1248         for (i=0; i < ARRAY_SIZE(samba_attributes); i++) {
1249                 int ret;
1250                 const struct ldb_schema_syntax *s = NULL;
1251
1252                 s = ldb_samba_syntax_by_name(ldb, samba_attributes[i].syntax);
1253
1254                 if (!s) {
1255                         s = ldb_standard_syntax_by_name(ldb, samba_attributes[i].syntax);
1256                 }
1257
1258                 if (!s) {
1259                         return -1;
1260                 }
1261
1262                 ret = ldb_schema_attribute_add_with_syntax(ldb, samba_attributes[i].name, LDB_ATTR_FLAG_FIXED, s);
1263                 if (ret != LDB_SUCCESS) {
1264                         return ret;
1265                 }
1266         }
1267
1268         for (i=0; i < ARRAY_SIZE(samba_dn_syntax); i++) {
1269                 int ret;
1270                 ret = ldb_dn_extended_add_syntax(ldb, LDB_ATTR_FLAG_FIXED, &samba_dn_syntax[i]);
1271                 if (ret != LDB_SUCCESS) {
1272                         return ret;
1273                 }
1274
1275                 
1276         }
1277
1278         return LDB_SUCCESS;
1279 }