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