s4-ldb: display an error if we can't decode a NDR blob
[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         if (ldif_comparision_objectSid_isString(in)) {
185                 if (ldif_read_objectSid(ldb, mem_ctx, in, out) == 0) {
186                         return 0;
187                 }
188         }
189         
190         /* Perhaps not a string after all */
191         *out = data_blob_talloc(mem_ctx, NULL, in->length/2+1);
192
193         if (!out->data) {
194                 return -1;
195         }
196
197         (*out).length = strhex_to_str((char *)out->data, out->length,
198                                      (const char *)in->data, in->length);
199
200         /* Check it looks like a SID */
201         ndr_err = ndr_pull_struct_blob_all(out, mem_ctx, NULL, &sid,
202                                            (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
203         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
204                 return -1;
205         }
206         return 0;
207 }
208
209 /*
210   convert a ldif formatted objectGUID to a NDR formatted blob
211 */
212 static int ldif_read_objectGUID(struct ldb_context *ldb, void *mem_ctx,
213                                 const struct ldb_val *in, struct ldb_val *out)
214 {
215         struct GUID guid;
216         NTSTATUS status;
217         enum ndr_err_code ndr_err;
218
219         status = GUID_from_data_blob(in, &guid);
220         if (!NT_STATUS_IS_OK(status)) {
221                 return -1;
222         }
223
224         ndr_err = ndr_push_struct_blob(out, mem_ctx, NULL, &guid,
225                                        (ndr_push_flags_fn_t)ndr_push_GUID);
226         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
227                 return -1;
228         }
229         return 0;
230 }
231
232 /*
233   convert a NDR formatted blob to a ldif formatted objectGUID
234 */
235 static int ldif_write_objectGUID(struct ldb_context *ldb, void *mem_ctx,
236                                  const struct ldb_val *in, struct ldb_val *out)
237 {
238         struct GUID guid;
239         enum ndr_err_code ndr_err;
240         ndr_err = ndr_pull_struct_blob_all(in, mem_ctx, NULL, &guid,
241                                            (ndr_pull_flags_fn_t)ndr_pull_GUID);
242         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
243                 return -1;
244         }
245         out->data = (uint8_t *)GUID_string(mem_ctx, &guid);
246         if (out->data == NULL) {
247                 return -1;
248         }
249         out->length = strlen((const char *)out->data);
250         return 0;
251 }
252
253 static bool ldif_comparision_objectGUID_isString(const struct ldb_val *v)
254 {
255         if (v->length != 36 && v->length != 38) return false;
256
257         /* Might be a GUID string, can't be a binary GUID (fixed 16 bytes) */
258         return true;
259 }
260
261 static int extended_dn_read_GUID(struct ldb_context *ldb, void *mem_ctx,
262                               const struct ldb_val *in, struct ldb_val *out)
263 {
264         struct GUID guid;
265         enum ndr_err_code ndr_err;
266         if (in->length == 36 && ldif_read_objectGUID(ldb, mem_ctx, in, out) == 0) {
267                 return 0;
268         }
269
270         /* Try as 'hex' form */
271         if (in->length != 32) {
272                 return -1;
273         }
274                 
275         *out = data_blob_talloc(mem_ctx, NULL, in->length/2+1);
276         
277         if (!out->data) {
278                 return -1;
279         }
280         
281         (*out).length = strhex_to_str((char *)out->data, out->length,
282                                       (const char *)in->data, in->length);
283         
284         /* Check it looks like a GUID */
285         ndr_err = ndr_pull_struct_blob_all(out, mem_ctx, NULL, &guid,
286                                            (ndr_pull_flags_fn_t)ndr_pull_GUID);
287         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
288                 return -1;
289         }
290         return 0;
291 }
292
293 /*
294   compare two objectGUIDs
295 */
296 static int ldif_comparison_objectGUID(struct ldb_context *ldb, void *mem_ctx,
297                                      const struct ldb_val *v1, const struct ldb_val *v2)
298 {
299         if (ldif_comparision_objectGUID_isString(v1) && ldif_comparision_objectGUID_isString(v2)) {
300                 return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
301         } else if (ldif_comparision_objectGUID_isString(v1)
302                    && !ldif_comparision_objectGUID_isString(v2)) {
303                 struct ldb_val v;
304                 int ret;
305                 if (ldif_read_objectGUID(ldb, mem_ctx, v1, &v) != 0) {
306                         /* Perhaps it wasn't a valid string after all */
307                         return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
308                 }
309                 ret = ldb_comparison_binary(ldb, mem_ctx, &v, v2);
310                 talloc_free(v.data);
311                 return ret;
312         } else if (!ldif_comparision_objectGUID_isString(v1)
313                    && ldif_comparision_objectGUID_isString(v2)) {
314                 struct ldb_val v;
315                 int ret;
316                 if (ldif_read_objectGUID(ldb, mem_ctx, v2, &v) != 0) {
317                         /* Perhaps it wasn't a valid string after all */
318                         return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
319                 }
320                 ret = ldb_comparison_binary(ldb, mem_ctx, v1, &v);
321                 talloc_free(v.data);
322                 return ret;
323         }
324         return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
325 }
326
327 /*
328   canonicalise a objectGUID
329 */
330 static int ldif_canonicalise_objectGUID(struct ldb_context *ldb, void *mem_ctx,
331                                        const struct ldb_val *in, struct ldb_val *out)
332 {
333         if (ldif_comparision_objectGUID_isString(in)) {
334                 if (ldif_read_objectGUID(ldb, mem_ctx, in, out) != 0) {
335                         /* Perhaps it wasn't a valid string after all */
336                         return ldb_handler_copy(ldb, mem_ctx, in, out);
337                 }
338                 return 0;
339         }
340         return ldb_handler_copy(ldb, mem_ctx, in, out);
341 }
342
343
344 /*
345   convert a ldif (SDDL) formatted ntSecurityDescriptor to a NDR formatted blob
346 */
347 static int ldif_read_ntSecurityDescriptor(struct ldb_context *ldb, void *mem_ctx,
348                                           const struct ldb_val *in, struct ldb_val *out)
349 {
350         struct security_descriptor *sd;
351         enum ndr_err_code ndr_err;
352
353         sd = talloc(mem_ctx, struct security_descriptor);
354         if (sd == NULL) {
355                 return -1;
356         }
357
358         ndr_err = ndr_pull_struct_blob(in, sd, NULL, sd,
359                                        (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
360         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
361                 /* If this does not parse, then it is probably SDDL, and we should try it that way */
362                 
363                 const struct dom_sid *sid = samdb_domain_sid(ldb);
364                 talloc_free(sd);
365                 sd = sddl_decode(mem_ctx, (const char *)in->data, sid);
366                 if (sd == NULL) {
367                         return -1;
368                 }
369         }
370
371         ndr_err = ndr_push_struct_blob(out, mem_ctx, NULL, sd,
372                                        (ndr_push_flags_fn_t)ndr_push_security_descriptor);
373         talloc_free(sd);
374         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
375                 return -1;
376         }
377
378         return 0;
379 }
380
381 /*
382   convert a NDR formatted blob to a ldif formatted ntSecurityDescriptor (SDDL format)
383 */
384 static int ldif_write_ntSecurityDescriptor(struct ldb_context *ldb, void *mem_ctx,
385                                            const struct ldb_val *in, struct ldb_val *out)
386 {
387         struct security_descriptor *sd;
388         enum ndr_err_code ndr_err;
389
390         if (ldb_get_flags(ldb) & LDB_FLG_SHOW_BINARY) {
391                 return ldif_write_NDR(ldb, mem_ctx, in, out, 
392                                       sizeof(struct security_descriptor),
393                                       (ndr_pull_flags_fn_t)ndr_pull_security_descriptor,
394                                       (ndr_print_fn_t)ndr_print_security_descriptor);
395                                       
396         }
397
398         sd = talloc(mem_ctx, struct security_descriptor);
399         if (sd == NULL) {
400                 return -1;
401         }
402         /* We can't use ndr_pull_struct_blob_all because this contains relative pointers */
403         ndr_err = ndr_pull_struct_blob(in, sd, NULL, sd,
404                                            (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
405         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
406                 talloc_free(sd);
407                 return -1;
408         }
409         out->data = (uint8_t *)sddl_encode(mem_ctx, sd, NULL);
410         talloc_free(sd);
411         if (out->data == NULL) {
412                 return -1;
413         }
414         out->length = strlen((const char *)out->data);
415         return 0;
416 }
417
418 /* 
419    canonicalise an objectCategory.  We use the short form as the cannoical form:
420    cn=Person,cn=Schema,cn=Configuration,<basedn> becomes 'person'
421 */
422
423 static int ldif_canonicalise_objectCategory(struct ldb_context *ldb, void *mem_ctx,
424                                             const struct ldb_val *in, struct ldb_val *out)
425 {
426         struct ldb_dn *dn1 = NULL;
427         const struct dsdb_schema *schema = dsdb_get_schema(ldb);
428         const struct dsdb_class *sclass;
429         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
430         if (!tmp_ctx) {
431                 return LDB_ERR_OPERATIONS_ERROR;
432         }
433
434         if (!schema) {
435                 *out = data_blob_talloc(mem_ctx, in->data, in->length);
436                 if (in->data && !out->data) {
437                         return LDB_ERR_OPERATIONS_ERROR;
438                 }
439                 return LDB_SUCCESS;
440         }
441         dn1 = ldb_dn_from_ldb_val(tmp_ctx, ldb, in);
442         if ( ! ldb_dn_validate(dn1)) {
443                 const char *lDAPDisplayName = talloc_strndup(tmp_ctx, (char *)in->data, in->length);
444                 sclass = dsdb_class_by_lDAPDisplayName(schema, lDAPDisplayName);
445                 if (sclass) {
446                         struct ldb_dn *dn = ldb_dn_new(mem_ctx, ldb,  
447                                                        sclass->defaultObjectCategory);
448                         *out = data_blob_string_const(ldb_dn_alloc_casefold(mem_ctx, dn));
449                         talloc_free(tmp_ctx);
450
451                         if (!out->data) {
452                                 return LDB_ERR_OPERATIONS_ERROR;
453                         }
454                         return LDB_SUCCESS;
455                 } else {
456                         *out = data_blob_talloc(mem_ctx, in->data, in->length);
457                         talloc_free(tmp_ctx);
458
459                         if (in->data && !out->data) {
460                                 return LDB_ERR_OPERATIONS_ERROR;
461                         }
462                         return LDB_SUCCESS;
463                 }
464         }
465         *out = data_blob_string_const(ldb_dn_alloc_casefold(mem_ctx, dn1));
466         talloc_free(tmp_ctx);
467
468         if (!out->data) {
469                 return LDB_ERR_OPERATIONS_ERROR;
470         }
471         return LDB_SUCCESS;
472 }
473
474 static int ldif_comparison_objectCategory(struct ldb_context *ldb, void *mem_ctx,
475                                           const struct ldb_val *v1,
476                                           const struct ldb_val *v2)
477 {
478
479         int ret, ret1, ret2;
480         struct ldb_val v1_canon, v2_canon;
481         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
482
483         /* I could try and bail if tmp_ctx was NULL, but what return
484          * value would I use?
485          *
486          * It seems easier to continue on the NULL context 
487          */
488         ret1 = ldif_canonicalise_objectCategory(ldb, tmp_ctx, v1, &v1_canon);
489         ret2 = ldif_canonicalise_objectCategory(ldb, tmp_ctx, v2, &v2_canon);
490
491         if (ret1 == LDB_SUCCESS && ret2 == LDB_SUCCESS) {
492                 ret = data_blob_cmp(&v1_canon, &v2_canon);
493         } else {
494                 ret = data_blob_cmp(v1, v2);
495         }
496         talloc_free(tmp_ctx);
497         return ret;
498 }
499
500 /*
501   convert a ldif formatted prefixMap to a NDR formatted blob
502 */
503 static int ldif_read_prefixMap(struct ldb_context *ldb, void *mem_ctx,
504                                const struct ldb_val *in, struct ldb_val *out)
505 {
506         struct prefixMapBlob *blob;
507         enum ndr_err_code ndr_err;
508         char *string, *line, *p, *oid;
509
510         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
511
512         if (tmp_ctx == NULL) {
513                 return -1;
514         }
515
516         blob = talloc_zero(tmp_ctx, struct prefixMapBlob);
517         if (blob == NULL) {
518                 talloc_free(blob);
519                 return -1;
520         }
521
522         blob->version = PREFIX_MAP_VERSION_DSDB;
523         
524         string = talloc_strndup(mem_ctx, (const char *)in->data, in->length);
525         if (string == NULL) {
526                 talloc_free(blob);
527                 return -1;
528         }
529
530         line = string;
531         while (line && line[0]) {
532                 p=strchr(line, ';');
533                 if (p) {
534                         p[0] = '\0';
535                 } else {
536                         p=strchr(line, '\n');
537                         if (p) {
538                                 p[0] = '\0';
539                         }
540                 }
541                 /* allow a traling seperator */
542                 if (line == p) {
543                         break;
544                 }
545                 
546                 blob->ctr.dsdb.mappings = talloc_realloc(blob, 
547                                                          blob->ctr.dsdb.mappings, 
548                                                          struct drsuapi_DsReplicaOIDMapping,
549                                                          blob->ctr.dsdb.num_mappings+1);
550                 if (!blob->ctr.dsdb.mappings) {
551                         talloc_free(tmp_ctx);
552                         return -1;
553                 }
554
555                 blob->ctr.dsdb.mappings[blob->ctr.dsdb.num_mappings].id_prefix = strtoul(line, &oid, 10);
556
557                 if (oid[0] != ':') {
558                         talloc_free(tmp_ctx);
559                         return -1;
560                 }
561
562                 /* we know there must be at least ":" */
563                 oid++;
564
565                 blob->ctr.dsdb.mappings[blob->ctr.dsdb.num_mappings].oid.oid
566                         = talloc_strdup(blob->ctr.dsdb.mappings, oid);
567
568                 blob->ctr.dsdb.num_mappings++;
569
570                 /* Now look past the terminator we added above */
571                 if (p) {
572                         line = p + 1;
573                 } else {
574                         line = NULL;
575                 }
576         }
577
578         ndr_err = ndr_push_struct_blob(out, mem_ctx, 
579                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), 
580                                        blob,
581                                        (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
582         talloc_free(tmp_ctx);
583         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
584                 return -1;
585         }
586         return 0;
587 }
588
589 /*
590   convert a NDR formatted blob to a ldif formatted prefixMap
591 */
592 static int ldif_write_prefixMap(struct ldb_context *ldb, void *mem_ctx,
593                                 const struct ldb_val *in, struct ldb_val *out)
594 {
595         struct prefixMapBlob *blob;
596         enum ndr_err_code ndr_err;
597         char *string;
598         uint32_t i;
599
600         if (ldb_get_flags(ldb) & LDB_FLG_SHOW_BINARY) {
601                 return ldif_write_NDR(ldb, mem_ctx, in, out, 
602                                       sizeof(struct prefixMapBlob),
603                                       (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob,
604                                       (ndr_print_fn_t)ndr_print_prefixMapBlob);
605                                       
606         }
607
608         blob = talloc(mem_ctx, struct prefixMapBlob);
609         if (blob == NULL) {
610                 return -1;
611         }
612         ndr_err = ndr_pull_struct_blob_all(in, blob, 
613                                            lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), 
614                                            blob,
615                                            (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
616         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
617                 talloc_free(blob);
618                 return -1;
619         }
620         if (blob->version != PREFIX_MAP_VERSION_DSDB) {
621                 return -1;
622         }
623         string = talloc_strdup(mem_ctx, "");
624         if (string == NULL) {
625                 return -1;
626         }
627
628         for (i=0; i < blob->ctr.dsdb.num_mappings; i++) {
629                 if (i > 0) {
630                         string = talloc_asprintf_append(string, ";"); 
631                 }
632                 string = talloc_asprintf_append(string, "%u:%s", 
633                                                    blob->ctr.dsdb.mappings[i].id_prefix,
634                                                    blob->ctr.dsdb.mappings[i].oid.oid);
635                 if (string == NULL) {
636                         return -1;
637                 }
638         }
639
640         talloc_free(blob);
641         *out = data_blob_string_const(string);
642         return 0;
643 }
644
645 static bool ldif_comparision_prefixMap_isString(const struct ldb_val *v)
646 {
647         if (v->length < 4) {
648                 return true;
649         }
650
651         if (IVAL(v->data, 0) == PREFIX_MAP_VERSION_DSDB) {
652                 return false;
653         }
654         
655         return true;
656 }
657
658 /*
659   canonicalise a prefixMap
660 */
661 static int ldif_canonicalise_prefixMap(struct ldb_context *ldb, void *mem_ctx,
662                                        const struct ldb_val *in, struct ldb_val *out)
663 {
664         if (ldif_comparision_prefixMap_isString(in)) {
665                 return ldif_read_prefixMap(ldb, mem_ctx, in, out);
666         }
667         return ldb_handler_copy(ldb, mem_ctx, in, out);
668 }
669
670 static int ldif_comparison_prefixMap(struct ldb_context *ldb, void *mem_ctx,
671                                      const struct ldb_val *v1,
672                                      const struct ldb_val *v2)
673 {
674
675         int ret, ret1, ret2;
676         struct ldb_val v1_canon, v2_canon;
677         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
678
679         /* I could try and bail if tmp_ctx was NULL, but what return
680          * value would I use?
681          *
682          * It seems easier to continue on the NULL context 
683          */
684         ret1 = ldif_canonicalise_prefixMap(ldb, tmp_ctx, v1, &v1_canon);
685         ret2 = ldif_canonicalise_prefixMap(ldb, tmp_ctx, v2, &v2_canon);
686
687         if (ret1 == LDB_SUCCESS && ret2 == LDB_SUCCESS) {
688                 ret = data_blob_cmp(&v1_canon, &v2_canon);
689         } else {
690                 ret = data_blob_cmp(v1, v2);
691         }
692         talloc_free(tmp_ctx);
693         return ret;
694 }
695
696 /* Canonicalisation of two 32-bit integers */
697 static int ldif_canonicalise_int32(struct ldb_context *ldb, void *mem_ctx,
698                         const struct ldb_val *in, struct ldb_val *out)
699 {
700         char *end;
701         /* We've to use "strtoll" here to have the intended overflows.
702          * Otherwise we may get "LONG_MAX" and the conversion is wrong. */
703         int32_t i = (int32_t) strtoll((char *)in->data, &end, 0);
704         if (*end != 0) {
705                 return -1;
706         }
707         out->data = (uint8_t *) talloc_asprintf(mem_ctx, "%d", i);
708         if (out->data == NULL) {
709                 return -1;
710         }
711         out->length = strlen((char *)out->data);
712         return 0;
713 }
714
715 /* Comparison of two 32-bit integers */
716 static int ldif_comparison_int32(struct ldb_context *ldb, void *mem_ctx,
717                         const struct ldb_val *v1, const struct ldb_val *v2)
718 {
719         /* We've to use "strtoll" here to have the intended overflows.
720          * Otherwise we may get "LONG_MAX" and the conversion is wrong. */
721         return (int32_t) strtoll((char *)v1->data, NULL, 0)
722          - (int32_t) strtoll((char *)v2->data, NULL, 0);
723 }
724
725 /*
726   convert a NDR formatted blob to a ldif formatted repsFromTo
727 */
728 static int ldif_write_repsFromTo(struct ldb_context *ldb, void *mem_ctx,
729                                  const struct ldb_val *in, struct ldb_val *out)
730 {
731         return ldif_write_NDR(ldb, mem_ctx, in, out, 
732                               sizeof(struct repsFromToBlob),
733                               (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob,
734                               (ndr_print_fn_t)ndr_print_repsFromToBlob);
735 }
736
737 /*
738   convert a NDR formatted blob to a ldif formatted replPropertyMetaData
739 */
740 static int ldif_write_replPropertyMetaData(struct ldb_context *ldb, void *mem_ctx,
741                                            const struct ldb_val *in, struct ldb_val *out)
742 {
743         return ldif_write_NDR(ldb, mem_ctx, in, out, 
744                               sizeof(struct replPropertyMetaDataBlob),
745                               (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob,
746                               (ndr_print_fn_t)ndr_print_replPropertyMetaDataBlob);
747 }
748
749 /*
750   convert a NDR formatted blob to a ldif formatted replUpToDateVector
751 */
752 static int ldif_write_replUpToDateVector(struct ldb_context *ldb, void *mem_ctx,
753                                          const struct ldb_val *in, struct ldb_val *out)
754 {
755         return ldif_write_NDR(ldb, mem_ctx, in, out, 
756                               sizeof(struct replUpToDateVectorBlob),
757                               (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob,
758                               (ndr_print_fn_t)ndr_print_replPropertyMetaDataBlob);
759 }
760
761
762 static int extended_dn_write_hex(struct ldb_context *ldb, void *mem_ctx,
763                                  const struct ldb_val *in, struct ldb_val *out)
764 {
765         *out = data_blob_string_const(data_blob_hex_string(mem_ctx, in));
766         if (!out->data) {
767                 return -1;
768         }
769         return 0;
770 }
771
772 static const struct ldb_schema_syntax samba_syntaxes[] = {
773         {
774                 .name             = LDB_SYNTAX_SAMBA_SID,
775                 .ldif_read_fn     = ldif_read_objectSid,
776                 .ldif_write_fn    = ldif_write_objectSid,
777                 .canonicalise_fn  = ldif_canonicalise_objectSid,
778                 .comparison_fn    = ldif_comparison_objectSid
779         },{
780                 .name             = LDB_SYNTAX_SAMBA_SECURITY_DESCRIPTOR,
781                 .ldif_read_fn     = ldif_read_ntSecurityDescriptor,
782                 .ldif_write_fn    = ldif_write_ntSecurityDescriptor,
783                 .canonicalise_fn  = ldb_handler_copy,
784                 .comparison_fn    = ldb_comparison_binary
785         },{
786                 .name             = LDB_SYNTAX_SAMBA_GUID,
787                 .ldif_read_fn     = ldif_read_objectGUID,
788                 .ldif_write_fn    = ldif_write_objectGUID,
789                 .canonicalise_fn  = ldif_canonicalise_objectGUID,
790                 .comparison_fn    = ldif_comparison_objectGUID
791         },{
792                 .name             = LDB_SYNTAX_SAMBA_OBJECT_CATEGORY,
793                 .ldif_read_fn     = ldb_handler_copy,
794                 .ldif_write_fn    = ldb_handler_copy,
795                 .canonicalise_fn  = ldif_canonicalise_objectCategory,
796                 .comparison_fn    = ldif_comparison_objectCategory
797         },{
798                 .name             = LDB_SYNTAX_SAMBA_PREFIX_MAP,
799                 .ldif_read_fn     = ldif_read_prefixMap,
800                 .ldif_write_fn    = ldif_write_prefixMap,
801                 .canonicalise_fn  = ldif_canonicalise_prefixMap,
802                 .comparison_fn    = ldif_comparison_prefixMap
803         },{
804                 .name             = LDB_SYNTAX_SAMBA_INT32,
805                 .ldif_read_fn     = ldb_handler_copy,
806                 .ldif_write_fn    = ldb_handler_copy,
807                 .canonicalise_fn  = ldif_canonicalise_int32,
808                 .comparison_fn    = ldif_comparison_int32
809         },{
810                 .name             = LDB_SYNTAX_SAMBA_REPSFROMTO,
811                 .ldif_read_fn     = ldb_handler_copy,
812                 .ldif_write_fn    = ldif_write_repsFromTo,
813                 .canonicalise_fn  = ldb_handler_copy,
814                 .comparison_fn    = ldb_comparison_binary
815         },{
816                 .name             = LDB_SYNTAX_SAMBA_REPLPROPERTYMETADATA,
817                 .ldif_read_fn     = ldb_handler_copy,
818                 .ldif_write_fn    = ldif_write_replPropertyMetaData,
819                 .canonicalise_fn  = ldb_handler_copy,
820                 .comparison_fn    = ldb_comparison_binary
821         },{
822                 .name             = LDB_SYNTAX_SAMBA_REPLUPTODATEVECTOR,
823                 .ldif_read_fn     = ldb_handler_copy,
824                 .ldif_write_fn    = ldif_write_replUpToDateVector,
825                 .canonicalise_fn  = ldb_handler_copy,
826                 .comparison_fn    = ldb_comparison_binary
827         },
828 };
829
830 static const struct ldb_dn_extended_syntax samba_dn_syntax[] = {
831         {
832                 .name             = "SID",
833                 .read_fn          = extended_dn_read_SID,
834                 .write_clear_fn   = ldif_write_objectSid,
835                 .write_hex_fn     = extended_dn_write_hex
836         },{
837                 .name             = "GUID",
838                 .read_fn          = extended_dn_read_GUID,
839                 .write_clear_fn   = ldif_write_objectGUID,
840                 .write_hex_fn     = extended_dn_write_hex
841         },{
842                 .name             = "WKGUID",
843                 .read_fn          = ldb_handler_copy,
844                 .write_clear_fn   = ldb_handler_copy,
845                 .write_hex_fn     = ldb_handler_copy
846         }
847 };
848
849 /* TODO: Should be dynamic at some point */
850 static const struct {
851         const char *name;
852         const char *syntax;
853 } samba_attributes[] = {
854         { "objectSid",                  LDB_SYNTAX_SAMBA_SID },
855         { "securityIdentifier",         LDB_SYNTAX_SAMBA_SID },
856         { "ntSecurityDescriptor",       LDB_SYNTAX_SAMBA_SECURITY_DESCRIPTOR },
857         { "objectGUID",                 LDB_SYNTAX_SAMBA_GUID },
858         { "invocationId",               LDB_SYNTAX_SAMBA_GUID },
859         { "schemaIDGUID",               LDB_SYNTAX_SAMBA_GUID },
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 }