s4-ldb: simplify ldif handlers using new GUID functions
[kamenim/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 static int ldif_write_NDR(struct ldb_context *ldb, void *mem_ctx,
42                           const struct ldb_val *in, struct ldb_val *out,
43                           size_t struct_size,
44                           ndr_pull_flags_fn_t pull_fn,
45                           ndr_print_fn_t print_fn)
46 {
47         uint8_t *p;
48         enum ndr_err_code err;
49         if (!(ldb_get_flags(ldb) & LDB_FLG_SHOW_BINARY)) {
50                 return ldb_handler_copy(ldb, mem_ctx, in, out);
51         }
52         p = talloc_size(mem_ctx, struct_size);
53         err = ndr_pull_struct_blob(in, mem_ctx, 
54                                    lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), 
55                                    p, pull_fn);
56         if (err != NDR_ERR_SUCCESS) {
57                 talloc_free(p);
58                 out->data = (uint8_t *)talloc_strdup(mem_ctx, "<Unable to decode binary data>");
59                 out->length = strlen((const char *)out->data);
60                 return 0;
61         }
62         out->data = (uint8_t *)ndr_print_struct_string(mem_ctx, print_fn, "NDR", p);
63         talloc_free(p);
64         if (out->data == NULL) {
65                 return ldb_handler_copy(ldb, mem_ctx, in, out);         
66         }
67         out->length = strlen((char *)out->data);
68         return 0;
69 }
70
71 /*
72   convert a ldif formatted objectSid to a NDR formatted blob
73 */
74 static int ldif_read_objectSid(struct ldb_context *ldb, void *mem_ctx,
75                                const struct ldb_val *in, struct ldb_val *out)
76 {
77         enum ndr_err_code ndr_err;
78         struct dom_sid *sid;
79         sid = dom_sid_parse_length(mem_ctx, in);
80         if (sid == NULL) {
81                 return -1;
82         }
83         ndr_err = ndr_push_struct_blob(out, mem_ctx, NULL, sid,
84                                        (ndr_push_flags_fn_t)ndr_push_dom_sid);
85         talloc_free(sid);
86         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
87                 return -1;
88         }
89         return 0;
90 }
91
92 /*
93   convert a NDR formatted blob to a ldif formatted objectSid
94 */
95 int ldif_write_objectSid(struct ldb_context *ldb, void *mem_ctx,
96                                 const struct ldb_val *in, struct ldb_val *out)
97 {
98         struct dom_sid *sid;
99         enum ndr_err_code ndr_err;
100
101         sid = talloc(mem_ctx, struct dom_sid);
102         if (sid == NULL) {
103                 return -1;
104         }
105         ndr_err = ndr_pull_struct_blob_all(in, sid, NULL, sid,
106                                            (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
107         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
108                 talloc_free(sid);
109                 return -1;
110         }
111         *out = data_blob_string_const(dom_sid_string(mem_ctx, sid));
112         talloc_free(sid);
113         if (out->data == NULL) {
114                 return -1;
115         }
116         return 0;
117 }
118
119 bool ldif_comparision_objectSid_isString(const struct ldb_val *v)
120 {
121         if (v->length < 3) {
122                 return false;
123         }
124
125         if (strncmp("S-", (const char *)v->data, 2) != 0) return false;
126         
127         return true;
128 }
129
130 /*
131   compare two objectSids
132 */
133 static int ldif_comparison_objectSid(struct ldb_context *ldb, void *mem_ctx,
134                                     const struct ldb_val *v1, const struct ldb_val *v2)
135 {
136         if (ldif_comparision_objectSid_isString(v1) && ldif_comparision_objectSid_isString(v2)) {
137                 return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
138         } else if (ldif_comparision_objectSid_isString(v1)
139                    && !ldif_comparision_objectSid_isString(v2)) {
140                 struct ldb_val v;
141                 int ret;
142                 if (ldif_read_objectSid(ldb, mem_ctx, v1, &v) != 0) {
143                         /* Perhaps not a string after all */
144                         return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
145                 }
146                 ret = ldb_comparison_binary(ldb, mem_ctx, &v, v2);
147                 talloc_free(v.data);
148                 return ret;
149         } else if (!ldif_comparision_objectSid_isString(v1)
150                    && ldif_comparision_objectSid_isString(v2)) {
151                 struct ldb_val v;
152                 int ret;
153                 if (ldif_read_objectSid(ldb, mem_ctx, v2, &v) != 0) {
154                         /* Perhaps not a string after all */
155                         return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
156                 }
157                 ret = ldb_comparison_binary(ldb, mem_ctx, v1, &v);
158                 talloc_free(v.data);
159                 return ret;
160         }
161         return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
162 }
163
164 /*
165   canonicalise a objectSid
166 */
167 static int ldif_canonicalise_objectSid(struct ldb_context *ldb, void *mem_ctx,
168                                       const struct ldb_val *in, struct ldb_val *out)
169 {
170         if (ldif_comparision_objectSid_isString(in)) {
171                 if (ldif_read_objectSid(ldb, mem_ctx, in, out) != 0) {
172                         /* Perhaps not a string after all */
173                         return ldb_handler_copy(ldb, mem_ctx, in, out);
174                 }
175                 return 0;
176         }
177         return ldb_handler_copy(ldb, mem_ctx, in, out);
178 }
179
180 static int extended_dn_read_SID(struct ldb_context *ldb, void *mem_ctx,
181                               const struct ldb_val *in, struct ldb_val *out)
182 {
183         struct dom_sid sid;
184         enum ndr_err_code ndr_err;
185         if (ldif_comparision_objectSid_isString(in)) {
186                 if (ldif_read_objectSid(ldb, mem_ctx, in, out) == 0) {
187                         return 0;
188                 }
189         }
190         
191         /* Perhaps not a string after all */
192         *out = data_blob_talloc(mem_ctx, NULL, in->length/2+1);
193
194         if (!out->data) {
195                 return -1;
196         }
197
198         (*out).length = strhex_to_str((char *)out->data, out->length,
199                                      (const char *)in->data, in->length);
200
201         /* Check it looks like a SID */
202         ndr_err = ndr_pull_struct_blob_all(out, mem_ctx, NULL, &sid,
203                                            (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
204         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
205                 return -1;
206         }
207         return 0;
208 }
209
210 /*
211   convert a ldif formatted objectGUID to a NDR formatted blob
212 */
213 static int ldif_read_objectGUID(struct ldb_context *ldb, void *mem_ctx,
214                                 const struct ldb_val *in, struct ldb_val *out)
215 {
216         struct GUID guid;
217         NTSTATUS status;
218         enum ndr_err_code ndr_err;
219
220         status = GUID_from_data_blob(in, &guid);
221         if (!NT_STATUS_IS_OK(status)) {
222                 return -1;
223         }
224
225         ndr_err = ndr_push_struct_blob(out, mem_ctx, NULL, &guid,
226                                        (ndr_push_flags_fn_t)ndr_push_GUID);
227         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
228                 return -1;
229         }
230         return 0;
231 }
232
233 /*
234   convert a NDR formatted blob to a ldif formatted objectGUID
235 */
236 static int ldif_write_objectGUID(struct ldb_context *ldb, void *mem_ctx,
237                                  const struct ldb_val *in, struct ldb_val *out)
238 {
239         struct GUID guid;
240         NTSTATUS status;
241
242         status = GUID_from_ndr_blob(in, &guid);
243         if (!NT_STATUS_IS_OK(status)) {
244                 return -1;
245         }
246         out->data = (uint8_t *)GUID_string(mem_ctx, &guid);
247         if (out->data == NULL) {
248                 return -1;
249         }
250         out->length = strlen((const char *)out->data);
251         return 0;
252 }
253
254 static bool ldif_comparision_objectGUID_isString(const struct ldb_val *v)
255 {
256         if (v->length != 36 && v->length != 38) return false;
257
258         /* Might be a GUID string, can't be a binary GUID (fixed 16 bytes) */
259         return true;
260 }
261
262 static int extended_dn_read_GUID(struct ldb_context *ldb, void *mem_ctx,
263                               const struct ldb_val *in, struct ldb_val *out)
264 {
265         struct GUID guid;
266         NTSTATUS status;
267
268         if (in->length == 36 && ldif_read_objectGUID(ldb, mem_ctx, in, out) == 0) {
269                 return 0;
270         }
271
272         /* Try as 'hex' form */
273         if (in->length != 32) {
274                 return -1;
275         }
276                 
277         *out = data_blob_talloc(mem_ctx, NULL, in->length/2+1);
278         
279         if (!out->data) {
280                 return -1;
281         }
282         
283         (*out).length = strhex_to_str((char *)out->data, out->length,
284                                       (const char *)in->data, in->length);
285         
286         /* Check it looks like a GUID */
287         status = GUID_from_ndr_blob(out, &guid);
288         if (!NT_STATUS_IS_OK(status)) {
289                 data_blob_free(out);
290                 return -1;
291         }
292         return 0;
293 }
294
295 /*
296   compare two objectGUIDs
297 */
298 static int ldif_comparison_objectGUID(struct ldb_context *ldb, void *mem_ctx,
299                                      const struct ldb_val *v1, const struct ldb_val *v2)
300 {
301         if (ldif_comparision_objectGUID_isString(v1) && ldif_comparision_objectGUID_isString(v2)) {
302                 return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
303         } else if (ldif_comparision_objectGUID_isString(v1)
304                    && !ldif_comparision_objectGUID_isString(v2)) {
305                 struct ldb_val v;
306                 int ret;
307                 if (ldif_read_objectGUID(ldb, mem_ctx, v1, &v) != 0) {
308                         /* Perhaps it wasn't a valid string after all */
309                         return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
310                 }
311                 ret = ldb_comparison_binary(ldb, mem_ctx, &v, v2);
312                 talloc_free(v.data);
313                 return ret;
314         } else if (!ldif_comparision_objectGUID_isString(v1)
315                    && ldif_comparision_objectGUID_isString(v2)) {
316                 struct ldb_val v;
317                 int ret;
318                 if (ldif_read_objectGUID(ldb, mem_ctx, v2, &v) != 0) {
319                         /* Perhaps it wasn't a valid string after all */
320                         return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
321                 }
322                 ret = ldb_comparison_binary(ldb, mem_ctx, v1, &v);
323                 talloc_free(v.data);
324                 return ret;
325         }
326         return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
327 }
328
329 /*
330   canonicalise a objectGUID
331 */
332 static int ldif_canonicalise_objectGUID(struct ldb_context *ldb, void *mem_ctx,
333                                        const struct ldb_val *in, struct ldb_val *out)
334 {
335         if (ldif_comparision_objectGUID_isString(in)) {
336                 if (ldif_read_objectGUID(ldb, mem_ctx, in, out) != 0) {
337                         /* Perhaps it wasn't a valid string after all */
338                         return ldb_handler_copy(ldb, mem_ctx, in, out);
339                 }
340                 return 0;
341         }
342         return ldb_handler_copy(ldb, mem_ctx, in, out);
343 }
344
345
346 /*
347   convert a ldif (SDDL) formatted ntSecurityDescriptor to a NDR formatted blob
348 */
349 static int ldif_read_ntSecurityDescriptor(struct ldb_context *ldb, void *mem_ctx,
350                                           const struct ldb_val *in, struct ldb_val *out)
351 {
352         struct security_descriptor *sd;
353         enum ndr_err_code ndr_err;
354
355         sd = talloc(mem_ctx, struct security_descriptor);
356         if (sd == NULL) {
357                 return -1;
358         }
359
360         ndr_err = ndr_pull_struct_blob(in, sd, NULL, sd,
361                                        (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
362         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
363                 /* If this does not parse, then it is probably SDDL, and we should try it that way */
364                 
365                 const struct dom_sid *sid = samdb_domain_sid(ldb);
366                 talloc_free(sd);
367                 sd = sddl_decode(mem_ctx, (const char *)in->data, sid);
368                 if (sd == NULL) {
369                         return -1;
370                 }
371         }
372
373         ndr_err = ndr_push_struct_blob(out, mem_ctx, NULL, sd,
374                                        (ndr_push_flags_fn_t)ndr_push_security_descriptor);
375         talloc_free(sd);
376         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
377                 return -1;
378         }
379
380         return 0;
381 }
382
383 /*
384   convert a NDR formatted blob to a ldif formatted ntSecurityDescriptor (SDDL format)
385 */
386 static int ldif_write_ntSecurityDescriptor(struct ldb_context *ldb, void *mem_ctx,
387                                            const struct ldb_val *in, struct ldb_val *out)
388 {
389         struct security_descriptor *sd;
390         enum ndr_err_code ndr_err;
391
392         if (ldb_get_flags(ldb) & LDB_FLG_SHOW_BINARY) {
393                 return ldif_write_NDR(ldb, mem_ctx, in, out, 
394                                       sizeof(struct security_descriptor),
395                                       (ndr_pull_flags_fn_t)ndr_pull_security_descriptor,
396                                       (ndr_print_fn_t)ndr_print_security_descriptor);
397                                       
398         }
399
400         sd = talloc(mem_ctx, struct security_descriptor);
401         if (sd == NULL) {
402                 return -1;
403         }
404         /* We can't use ndr_pull_struct_blob_all because this contains relative pointers */
405         ndr_err = ndr_pull_struct_blob(in, sd, NULL, sd,
406                                            (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
407         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
408                 talloc_free(sd);
409                 return -1;
410         }
411         out->data = (uint8_t *)sddl_encode(mem_ctx, sd, NULL);
412         talloc_free(sd);
413         if (out->data == NULL) {
414                 return -1;
415         }
416         out->length = strlen((const char *)out->data);
417         return 0;
418 }
419
420 /* 
421    canonicalise an objectCategory.  We use the short form as the cannoical form:
422    cn=Person,cn=Schema,cn=Configuration,<basedn> becomes 'person'
423 */
424
425 static int ldif_canonicalise_objectCategory(struct ldb_context *ldb, void *mem_ctx,
426                                             const struct ldb_val *in, struct ldb_val *out)
427 {
428         struct ldb_dn *dn1 = NULL;
429         const struct dsdb_schema *schema = dsdb_get_schema(ldb);
430         const struct dsdb_class *sclass;
431         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
432         if (!tmp_ctx) {
433                 return LDB_ERR_OPERATIONS_ERROR;
434         }
435
436         if (!schema) {
437                 talloc_free(tmp_ctx);
438                 *out = data_blob_talloc(mem_ctx, in->data, in->length);
439                 if (in->data && !out->data) {
440                         return LDB_ERR_OPERATIONS_ERROR;
441                 }
442                 return LDB_SUCCESS;
443         }
444         dn1 = ldb_dn_from_ldb_val(tmp_ctx, ldb, in);
445         if ( ! ldb_dn_validate(dn1)) {
446                 const char *lDAPDisplayName = talloc_strndup(tmp_ctx, (char *)in->data, in->length);
447                 sclass = dsdb_class_by_lDAPDisplayName(schema, lDAPDisplayName);
448                 if (sclass) {
449                         struct ldb_dn *dn = ldb_dn_new(mem_ctx, ldb,  
450                                                        sclass->defaultObjectCategory);
451                         *out = data_blob_string_const(ldb_dn_alloc_casefold(mem_ctx, dn));
452                         talloc_free(tmp_ctx);
453
454                         if (!out->data) {
455                                 return LDB_ERR_OPERATIONS_ERROR;
456                         }
457                         return LDB_SUCCESS;
458                 } else {
459                         *out = data_blob_talloc(mem_ctx, in->data, in->length);
460                         talloc_free(tmp_ctx);
461
462                         if (in->data && !out->data) {
463                                 return LDB_ERR_OPERATIONS_ERROR;
464                         }
465                         return LDB_SUCCESS;
466                 }
467         }
468         *out = data_blob_string_const(ldb_dn_alloc_casefold(mem_ctx, dn1));
469         talloc_free(tmp_ctx);
470
471         if (!out->data) {
472                 return LDB_ERR_OPERATIONS_ERROR;
473         }
474         return LDB_SUCCESS;
475 }
476
477 static int ldif_comparison_objectCategory(struct ldb_context *ldb, void *mem_ctx,
478                                           const struct ldb_val *v1,
479                                           const struct ldb_val *v2)
480 {
481         return ldb_any_comparison(ldb, mem_ctx, ldif_canonicalise_objectCategory,
482                                   v1, v2);
483 }
484
485 /*
486   convert a ldif formatted prefixMap to a NDR formatted blob
487 */
488 static int ldif_read_prefixMap(struct ldb_context *ldb, void *mem_ctx,
489                                const struct ldb_val *in, struct ldb_val *out)
490 {
491         struct prefixMapBlob *blob;
492         enum ndr_err_code ndr_err;
493         char *string, *line, *p, *oid;
494         DATA_BLOB oid_blob;
495
496         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
497
498         if (tmp_ctx == NULL) {
499                 return -1;
500         }
501
502         blob = talloc_zero(tmp_ctx, struct prefixMapBlob);
503         if (blob == NULL) {
504                 talloc_free(blob);
505                 return -1;
506         }
507
508         blob->version = PREFIX_MAP_VERSION_DSDB;
509         
510         string = talloc_strndup(mem_ctx, (const char *)in->data, in->length);
511         if (string == NULL) {
512                 talloc_free(blob);
513                 return -1;
514         }
515
516         line = string;
517         while (line && line[0]) {
518                 p=strchr(line, ';');
519                 if (p) {
520                         p[0] = '\0';
521                 } else {
522                         p=strchr(line, '\n');
523                         if (p) {
524                                 p[0] = '\0';
525                         }
526                 }
527                 /* allow a traling seperator */
528                 if (line == p) {
529                         break;
530                 }
531                 
532                 blob->ctr.dsdb.mappings = talloc_realloc(blob, 
533                                                          blob->ctr.dsdb.mappings, 
534                                                          struct drsuapi_DsReplicaOIDMapping,
535                                                          blob->ctr.dsdb.num_mappings+1);
536                 if (!blob->ctr.dsdb.mappings) {
537                         talloc_free(tmp_ctx);
538                         return -1;
539                 }
540
541                 blob->ctr.dsdb.mappings[blob->ctr.dsdb.num_mappings].id_prefix = strtoul(line, &oid, 10);
542
543                 if (oid[0] != ':') {
544                         talloc_free(tmp_ctx);
545                         return -1;
546                 }
547
548                 /* we know there must be at least ":" */
549                 oid++;
550
551                 if (!ber_write_partial_OID_String(blob->ctr.dsdb.mappings, &oid_blob, oid)) {
552                         talloc_free(tmp_ctx);
553                         return -1;
554                 }
555                 blob->ctr.dsdb.mappings[blob->ctr.dsdb.num_mappings].oid.length = oid_blob.length;
556                 blob->ctr.dsdb.mappings[blob->ctr.dsdb.num_mappings].oid.binary_oid = oid_blob.data;
557
558                 blob->ctr.dsdb.num_mappings++;
559
560                 /* Now look past the terminator we added above */
561                 if (p) {
562                         line = p + 1;
563                 } else {
564                         line = NULL;
565                 }
566         }
567
568         ndr_err = ndr_push_struct_blob(out, mem_ctx, 
569                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), 
570                                        blob,
571                                        (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
572         talloc_free(tmp_ctx);
573         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
574                 return -1;
575         }
576         return 0;
577 }
578
579 /*
580   convert a NDR formatted blob to a ldif formatted prefixMap
581 */
582 static int ldif_write_prefixMap(struct ldb_context *ldb, void *mem_ctx,
583                                 const struct ldb_val *in, struct ldb_val *out)
584 {
585         struct prefixMapBlob *blob;
586         enum ndr_err_code ndr_err;
587         char *string;
588         uint32_t i;
589
590         if (ldb_get_flags(ldb) & LDB_FLG_SHOW_BINARY) {
591                 return ldif_write_NDR(ldb, mem_ctx, in, out, 
592                                       sizeof(struct prefixMapBlob),
593                                       (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob,
594                                       (ndr_print_fn_t)ndr_print_prefixMapBlob);
595                                       
596         }
597
598         blob = talloc(mem_ctx, struct prefixMapBlob);
599         if (blob == NULL) {
600                 return -1;
601         }
602         ndr_err = ndr_pull_struct_blob_all(in, blob, 
603                                            lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), 
604                                            blob,
605                                            (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
606         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
607                 goto failed;
608         }
609         if (blob->version != PREFIX_MAP_VERSION_DSDB) {
610                 goto failed;
611         }
612         string = talloc_strdup(mem_ctx, "");
613         if (string == NULL) {
614                 goto failed;
615         }
616
617         for (i=0; i < blob->ctr.dsdb.num_mappings; i++) {
618                 DATA_BLOB oid_blob;
619                 const char *partial_oid = NULL;
620
621                 if (i > 0) {
622                         string = talloc_asprintf_append(string, ";"); 
623                 }
624
625                 oid_blob = data_blob_const(blob->ctr.dsdb.mappings[i].oid.binary_oid,
626                                            blob->ctr.dsdb.mappings[i].oid.length);
627                 if (!ber_read_partial_OID_String(blob, oid_blob, &partial_oid)) {
628                         DEBUG(0, ("ber_read_partial_OID failed on prefixMap item with id: 0x%X",
629                                   blob->ctr.dsdb.mappings[i].id_prefix));
630                         goto failed;
631                 }
632                 string = talloc_asprintf_append(string, "%u:%s", 
633                                                    blob->ctr.dsdb.mappings[i].id_prefix,
634                                                    partial_oid);
635                 talloc_free(discard_const(partial_oid));
636                 if (string == NULL) {
637                         goto failed;
638                 }
639         }
640
641         talloc_free(blob);
642         *out = data_blob_string_const(string);
643         return 0;
644
645 failed:
646         talloc_free(blob);
647         return -1;
648 }
649
650 static bool ldif_comparision_prefixMap_isString(const struct ldb_val *v)
651 {
652         if (v->length < 4) {
653                 return true;
654         }
655
656         if (IVAL(v->data, 0) == PREFIX_MAP_VERSION_DSDB) {
657                 return false;
658         }
659         
660         return true;
661 }
662
663 /*
664   canonicalise a prefixMap
665 */
666 static int ldif_canonicalise_prefixMap(struct ldb_context *ldb, void *mem_ctx,
667                                        const struct ldb_val *in, struct ldb_val *out)
668 {
669         if (ldif_comparision_prefixMap_isString(in)) {
670                 return ldif_read_prefixMap(ldb, mem_ctx, in, out);
671         }
672         return ldb_handler_copy(ldb, mem_ctx, in, out);
673 }
674
675 static int ldif_comparison_prefixMap(struct ldb_context *ldb, void *mem_ctx,
676                                      const struct ldb_val *v1,
677                                      const struct ldb_val *v2)
678 {
679         return ldb_any_comparison(ldb, mem_ctx, ldif_canonicalise_prefixMap,
680                                   v1, v2);
681 }
682
683 /* Canonicalisation of two 32-bit integers */
684 static int ldif_canonicalise_int32(struct ldb_context *ldb, void *mem_ctx,
685                         const struct ldb_val *in, struct ldb_val *out)
686 {
687         char *end;
688         /* We've to use "strtoll" here to have the intended overflows.
689          * Otherwise we may get "LONG_MAX" and the conversion is wrong. */
690         int32_t i = (int32_t) strtoll((char *)in->data, &end, 0);
691         if (*end != 0) {
692                 return -1;
693         }
694         out->data = (uint8_t *) talloc_asprintf(mem_ctx, "%d", i);
695         if (out->data == NULL) {
696                 return -1;
697         }
698         out->length = strlen((char *)out->data);
699         return 0;
700 }
701
702 /* Comparison of two 32-bit integers */
703 static int ldif_comparison_int32(struct ldb_context *ldb, void *mem_ctx,
704                         const struct ldb_val *v1, const struct ldb_val *v2)
705 {
706         /* We've to use "strtoll" here to have the intended overflows.
707          * Otherwise we may get "LONG_MAX" and the conversion is wrong. */
708         return (int32_t) strtoll((char *)v1->data, NULL, 0)
709          - (int32_t) strtoll((char *)v2->data, NULL, 0);
710 }
711
712 /*
713   convert a NDR formatted blob to a ldif formatted repsFromTo
714 */
715 static int ldif_write_repsFromTo(struct ldb_context *ldb, void *mem_ctx,
716                                  const struct ldb_val *in, struct ldb_val *out)
717 {
718         return ldif_write_NDR(ldb, mem_ctx, in, out, 
719                               sizeof(struct repsFromToBlob),
720                               (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob,
721                               (ndr_print_fn_t)ndr_print_repsFromToBlob);
722 }
723
724 /*
725   convert a NDR formatted blob to a ldif formatted replPropertyMetaData
726 */
727 static int ldif_write_replPropertyMetaData(struct ldb_context *ldb, void *mem_ctx,
728                                            const struct ldb_val *in, struct ldb_val *out)
729 {
730         return ldif_write_NDR(ldb, mem_ctx, in, out, 
731                               sizeof(struct replPropertyMetaDataBlob),
732                               (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob,
733                               (ndr_print_fn_t)ndr_print_replPropertyMetaDataBlob);
734 }
735
736 /*
737   convert a NDR formatted blob to a ldif formatted replUpToDateVector
738 */
739 static int ldif_write_replUpToDateVector(struct ldb_context *ldb, void *mem_ctx,
740                                          const struct ldb_val *in, struct ldb_val *out)
741 {
742         return ldif_write_NDR(ldb, mem_ctx, in, out, 
743                               sizeof(struct replUpToDateVectorBlob),
744                               (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob,
745                               (ndr_print_fn_t)ndr_print_replUpToDateVectorBlob);
746 }
747
748
749 static int extended_dn_write_hex(struct ldb_context *ldb, void *mem_ctx,
750                                  const struct ldb_val *in, struct ldb_val *out)
751 {
752         *out = data_blob_string_const(data_blob_hex_string_lower(mem_ctx, in));
753         if (!out->data) {
754                 return -1;
755         }
756         return 0;
757 }
758
759 static const struct ldb_schema_syntax samba_syntaxes[] = {
760         {
761                 .name             = LDB_SYNTAX_SAMBA_SID,
762                 .ldif_read_fn     = ldif_read_objectSid,
763                 .ldif_write_fn    = ldif_write_objectSid,
764                 .canonicalise_fn  = ldif_canonicalise_objectSid,
765                 .comparison_fn    = ldif_comparison_objectSid
766         },{
767                 .name             = LDB_SYNTAX_SAMBA_SECURITY_DESCRIPTOR,
768                 .ldif_read_fn     = ldif_read_ntSecurityDescriptor,
769                 .ldif_write_fn    = ldif_write_ntSecurityDescriptor,
770                 .canonicalise_fn  = ldb_handler_copy,
771                 .comparison_fn    = ldb_comparison_binary
772         },{
773                 .name             = LDB_SYNTAX_SAMBA_GUID,
774                 .ldif_read_fn     = ldif_read_objectGUID,
775                 .ldif_write_fn    = ldif_write_objectGUID,
776                 .canonicalise_fn  = ldif_canonicalise_objectGUID,
777                 .comparison_fn    = ldif_comparison_objectGUID
778         },{
779                 .name             = LDB_SYNTAX_SAMBA_OBJECT_CATEGORY,
780                 .ldif_read_fn     = ldb_handler_copy,
781                 .ldif_write_fn    = ldb_handler_copy,
782                 .canonicalise_fn  = ldif_canonicalise_objectCategory,
783                 .comparison_fn    = ldif_comparison_objectCategory
784         },{
785                 .name             = LDB_SYNTAX_SAMBA_PREFIX_MAP,
786                 .ldif_read_fn     = ldif_read_prefixMap,
787                 .ldif_write_fn    = ldif_write_prefixMap,
788                 .canonicalise_fn  = ldif_canonicalise_prefixMap,
789                 .comparison_fn    = ldif_comparison_prefixMap
790         },{
791                 .name             = LDB_SYNTAX_SAMBA_INT32,
792                 .ldif_read_fn     = ldb_handler_copy,
793                 .ldif_write_fn    = ldb_handler_copy,
794                 .canonicalise_fn  = ldif_canonicalise_int32,
795                 .comparison_fn    = ldif_comparison_int32
796         },{
797                 .name             = LDB_SYNTAX_SAMBA_REPSFROMTO,
798                 .ldif_read_fn     = ldb_handler_copy,
799                 .ldif_write_fn    = ldif_write_repsFromTo,
800                 .canonicalise_fn  = ldb_handler_copy,
801                 .comparison_fn    = ldb_comparison_binary
802         },{
803                 .name             = LDB_SYNTAX_SAMBA_REPLPROPERTYMETADATA,
804                 .ldif_read_fn     = ldb_handler_copy,
805                 .ldif_write_fn    = ldif_write_replPropertyMetaData,
806                 .canonicalise_fn  = ldb_handler_copy,
807                 .comparison_fn    = ldb_comparison_binary
808         },{
809                 .name             = LDB_SYNTAX_SAMBA_REPLUPTODATEVECTOR,
810                 .ldif_read_fn     = ldb_handler_copy,
811                 .ldif_write_fn    = ldif_write_replUpToDateVector,
812                 .canonicalise_fn  = ldb_handler_copy,
813                 .comparison_fn    = ldb_comparison_binary
814         },{
815                 .name             = DSDB_SYNTAX_BINARY_DN,
816                 .ldif_read_fn     = ldb_handler_copy,
817                 .ldif_write_fn    = ldb_handler_copy,
818                 .canonicalise_fn  = dsdb_dn_binary_canonicalise,
819                 .comparison_fn    = dsdb_dn_binary_comparison
820         },{
821                 .name             = DSDB_SYNTAX_STRING_DN,
822                 .ldif_read_fn     = ldb_handler_copy,
823                 .ldif_write_fn    = ldb_handler_copy,
824                 .canonicalise_fn  = dsdb_dn_string_canonicalise,
825                 .comparison_fn    = dsdb_dn_string_comparison
826         },
827 };
828
829 static const struct ldb_dn_extended_syntax samba_dn_syntax[] = {
830         {
831                 .name             = "SID",
832                 .read_fn          = extended_dn_read_SID,
833                 .write_clear_fn   = ldif_write_objectSid,
834                 .write_hex_fn     = extended_dn_write_hex
835         },{
836                 .name             = "GUID",
837                 .read_fn          = extended_dn_read_GUID,
838                 .write_clear_fn   = ldif_write_objectGUID,
839                 .write_hex_fn     = extended_dn_write_hex
840         },{
841                 .name             = "WKGUID",
842                 .read_fn          = ldb_handler_copy,
843                 .write_clear_fn   = ldb_handler_copy,
844                 .write_hex_fn     = ldb_handler_copy
845         }
846 };
847
848 /* TODO: Should be dynamic at some point */
849 static const struct {
850         const char *name;
851         const char *syntax;
852 } samba_attributes[] = {
853         { "objectSid",                  LDB_SYNTAX_SAMBA_SID },
854         { "securityIdentifier",         LDB_SYNTAX_SAMBA_SID },
855         { "ntSecurityDescriptor",       LDB_SYNTAX_SAMBA_SECURITY_DESCRIPTOR },
856         { "objectGUID",                 LDB_SYNTAX_SAMBA_GUID },
857         { "invocationId",               LDB_SYNTAX_SAMBA_GUID },
858         { "schemaIDGUID",               LDB_SYNTAX_SAMBA_GUID },
859         { "oMSyntax",                   LDB_SYNTAX_SAMBA_INT32 },
860         { "attributeSecurityGUID",      LDB_SYNTAX_SAMBA_GUID },
861         { "parentGUID",                 LDB_SYNTAX_SAMBA_GUID },
862         { "siteGUID",                   LDB_SYNTAX_SAMBA_GUID },
863         { "pKTGUID",                    LDB_SYNTAX_SAMBA_GUID },
864         { "fRSVersionGUID",             LDB_SYNTAX_SAMBA_GUID },
865         { "fRSReplicaSetGUID",          LDB_SYNTAX_SAMBA_GUID },
866         { "netbootGUID",                LDB_SYNTAX_SAMBA_GUID },
867         { "objectCategory",             LDB_SYNTAX_SAMBA_OBJECT_CATEGORY },
868         { "prefixMap",                  LDB_SYNTAX_SAMBA_PREFIX_MAP },
869         { "repsFrom",                   LDB_SYNTAX_SAMBA_REPSFROMTO },
870         { "repsTo",                     LDB_SYNTAX_SAMBA_REPSFROMTO },
871         { "replPropertyMetaData",       LDB_SYNTAX_SAMBA_REPLPROPERTYMETADATA },
872         { "replUpToDateVector",         LDB_SYNTAX_SAMBA_REPLUPTODATEVECTOR },
873 };
874
875 const struct ldb_schema_syntax *ldb_samba_syntax_by_name(struct ldb_context *ldb, const char *name)
876 {
877         uint32_t j;
878         const struct ldb_schema_syntax *s = NULL;
879         
880         for (j=0; j < ARRAY_SIZE(samba_syntaxes); j++) {
881                 if (strcmp(name, samba_syntaxes[j].name) == 0) {
882                         s = &samba_syntaxes[j];
883                         break;
884                 }
885         }
886         return s;
887 }
888
889 const struct ldb_schema_syntax *ldb_samba_syntax_by_lDAPDisplayName(struct ldb_context *ldb, const char *name)
890 {
891         uint32_t j;
892         const struct ldb_schema_syntax *s = NULL;
893
894         for (j=0; j < ARRAY_SIZE(samba_attributes); j++) {
895                 if (strcmp(samba_attributes[j].name, name) == 0) {
896                         s = ldb_samba_syntax_by_name(ldb, samba_attributes[j].syntax);
897                         break;
898                 }
899         }
900         
901         return s;
902 }
903
904 /*
905   register the samba ldif handlers
906 */
907 int ldb_register_samba_handlers(struct ldb_context *ldb)
908 {
909         uint32_t i;
910
911         for (i=0; i < ARRAY_SIZE(samba_attributes); i++) {
912                 int ret;
913                 const struct ldb_schema_syntax *s = NULL;
914
915                 s = ldb_samba_syntax_by_name(ldb, samba_attributes[i].syntax);
916
917                 if (!s) {
918                         s = ldb_standard_syntax_by_name(ldb, samba_attributes[i].syntax);
919                 }
920
921                 if (!s) {
922                         return -1;
923                 }
924
925                 ret = ldb_schema_attribute_add_with_syntax(ldb, samba_attributes[i].name, LDB_ATTR_FLAG_FIXED, s);
926                 if (ret != LDB_SUCCESS) {
927                         return ret;
928                 }
929         }
930
931         for (i=0; i < ARRAY_SIZE(samba_dn_syntax); i++) {
932                 int ret;
933                 ret = ldb_dn_extended_add_syntax(ldb, LDB_ATTR_FLAG_FIXED, &samba_dn_syntax[i]);
934                 if (ret != LDB_SUCCESS) {
935                         return ret;
936                 }
937
938                 
939         }
940
941         return LDB_SUCCESS;
942 }