s4-dsdb: use safe length limiting in string->integer conversion
[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
219         status = GUID_from_data_blob(in, &guid);
220         if (!NT_STATUS_IS_OK(status)) {
221                 return -1;
222         }
223
224         status = GUID_to_ndr_blob(&guid, mem_ctx, out);
225         if (!NT_STATUS_IS_OK(status)) {
226                 return -1;
227         }
228         return 0;
229 }
230
231 /*
232   convert a NDR formatted blob to a ldif formatted objectGUID
233 */
234 static int ldif_write_objectGUID(struct ldb_context *ldb, void *mem_ctx,
235                                  const struct ldb_val *in, struct ldb_val *out)
236 {
237         struct GUID guid;
238         NTSTATUS status;
239
240         status = GUID_from_ndr_blob(in, &guid);
241         if (!NT_STATUS_IS_OK(status)) {
242                 return -1;
243         }
244         out->data = (uint8_t *)GUID_string(mem_ctx, &guid);
245         if (out->data == NULL) {
246                 return -1;
247         }
248         out->length = strlen((const char *)out->data);
249         return 0;
250 }
251
252 static bool ldif_comparision_objectGUID_isString(const struct ldb_val *v)
253 {
254         if (v->length != 36 && v->length != 38) return false;
255
256         /* Might be a GUID string, can't be a binary GUID (fixed 16 bytes) */
257         return true;
258 }
259
260 static int extended_dn_read_GUID(struct ldb_context *ldb, void *mem_ctx,
261                               const struct ldb_val *in, struct ldb_val *out)
262 {
263         struct GUID guid;
264         NTSTATUS status;
265
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         status = GUID_from_ndr_blob(out, &guid);
286         if (!NT_STATUS_IS_OK(status)) {
287                 data_blob_free(out);
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                 talloc_free(tmp_ctx);
436                 *out = data_blob_talloc(mem_ctx, in->data, in->length);
437                 if (in->data && !out->data) {
438                         return LDB_ERR_OPERATIONS_ERROR;
439                 }
440                 return LDB_SUCCESS;
441         }
442         dn1 = ldb_dn_from_ldb_val(tmp_ctx, ldb, in);
443         if ( ! ldb_dn_validate(dn1)) {
444                 const char *lDAPDisplayName = talloc_strndup(tmp_ctx, (char *)in->data, in->length);
445                 sclass = dsdb_class_by_lDAPDisplayName(schema, lDAPDisplayName);
446                 if (sclass) {
447                         struct ldb_dn *dn = ldb_dn_new(mem_ctx, ldb,  
448                                                        sclass->defaultObjectCategory);
449                         *out = data_blob_string_const(ldb_dn_alloc_casefold(mem_ctx, dn));
450                         talloc_free(tmp_ctx);
451
452                         if (!out->data) {
453                                 return LDB_ERR_OPERATIONS_ERROR;
454                         }
455                         return LDB_SUCCESS;
456                 } else {
457                         *out = data_blob_talloc(mem_ctx, in->data, in->length);
458                         talloc_free(tmp_ctx);
459
460                         if (in->data && !out->data) {
461                                 return LDB_ERR_OPERATIONS_ERROR;
462                         }
463                         return LDB_SUCCESS;
464                 }
465         }
466         *out = data_blob_string_const(ldb_dn_alloc_casefold(mem_ctx, dn1));
467         talloc_free(tmp_ctx);
468
469         if (!out->data) {
470                 return LDB_ERR_OPERATIONS_ERROR;
471         }
472         return LDB_SUCCESS;
473 }
474
475 static int ldif_comparison_objectCategory(struct ldb_context *ldb, void *mem_ctx,
476                                           const struct ldb_val *v1,
477                                           const struct ldb_val *v2)
478 {
479         return ldb_any_comparison(ldb, mem_ctx, ldif_canonicalise_objectCategory,
480                                   v1, v2);
481 }
482
483 /*
484   convert a ldif formatted prefixMap to a NDR formatted blob
485 */
486 static int ldif_read_prefixMap(struct ldb_context *ldb, void *mem_ctx,
487                                const struct ldb_val *in, struct ldb_val *out)
488 {
489         struct prefixMapBlob *blob;
490         enum ndr_err_code ndr_err;
491         char *string, *line, *p, *oid;
492         DATA_BLOB oid_blob;
493
494         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
495
496         if (tmp_ctx == NULL) {
497                 return -1;
498         }
499
500         blob = talloc_zero(tmp_ctx, struct prefixMapBlob);
501         if (blob == NULL) {
502                 talloc_free(blob);
503                 return -1;
504         }
505
506         blob->version = PREFIX_MAP_VERSION_DSDB;
507         
508         string = talloc_strndup(mem_ctx, (const char *)in->data, in->length);
509         if (string == NULL) {
510                 talloc_free(blob);
511                 return -1;
512         }
513
514         line = string;
515         while (line && line[0]) {
516                 p=strchr(line, ';');
517                 if (p) {
518                         p[0] = '\0';
519                 } else {
520                         p=strchr(line, '\n');
521                         if (p) {
522                                 p[0] = '\0';
523                         }
524                 }
525                 /* allow a traling seperator */
526                 if (line == p) {
527                         break;
528                 }
529                 
530                 blob->ctr.dsdb.mappings = talloc_realloc(blob, 
531                                                          blob->ctr.dsdb.mappings, 
532                                                          struct drsuapi_DsReplicaOIDMapping,
533                                                          blob->ctr.dsdb.num_mappings+1);
534                 if (!blob->ctr.dsdb.mappings) {
535                         talloc_free(tmp_ctx);
536                         return -1;
537                 }
538
539                 blob->ctr.dsdb.mappings[blob->ctr.dsdb.num_mappings].id_prefix = strtoul(line, &oid, 10);
540
541                 if (oid[0] != ':') {
542                         talloc_free(tmp_ctx);
543                         return -1;
544                 }
545
546                 /* we know there must be at least ":" */
547                 oid++;
548
549                 if (!ber_write_partial_OID_String(blob->ctr.dsdb.mappings, &oid_blob, oid)) {
550                         talloc_free(tmp_ctx);
551                         return -1;
552                 }
553                 blob->ctr.dsdb.mappings[blob->ctr.dsdb.num_mappings].oid.length = oid_blob.length;
554                 blob->ctr.dsdb.mappings[blob->ctr.dsdb.num_mappings].oid.binary_oid = oid_blob.data;
555
556                 blob->ctr.dsdb.num_mappings++;
557
558                 /* Now look past the terminator we added above */
559                 if (p) {
560                         line = p + 1;
561                 } else {
562                         line = NULL;
563                 }
564         }
565
566         ndr_err = ndr_push_struct_blob(out, mem_ctx, 
567                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), 
568                                        blob,
569                                        (ndr_push_flags_fn_t)ndr_push_prefixMapBlob);
570         talloc_free(tmp_ctx);
571         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
572                 return -1;
573         }
574         return 0;
575 }
576
577 /*
578   convert a NDR formatted blob to a ldif formatted prefixMap
579 */
580 static int ldif_write_prefixMap(struct ldb_context *ldb, void *mem_ctx,
581                                 const struct ldb_val *in, struct ldb_val *out)
582 {
583         struct prefixMapBlob *blob;
584         enum ndr_err_code ndr_err;
585         char *string;
586         uint32_t i;
587
588         if (ldb_get_flags(ldb) & LDB_FLG_SHOW_BINARY) {
589                 return ldif_write_NDR(ldb, mem_ctx, in, out, 
590                                       sizeof(struct prefixMapBlob),
591                                       (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob,
592                                       (ndr_print_fn_t)ndr_print_prefixMapBlob);
593                                       
594         }
595
596         blob = talloc(mem_ctx, struct prefixMapBlob);
597         if (blob == NULL) {
598                 return -1;
599         }
600         ndr_err = ndr_pull_struct_blob_all(in, blob, 
601                                            lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")), 
602                                            blob,
603                                            (ndr_pull_flags_fn_t)ndr_pull_prefixMapBlob);
604         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
605                 goto failed;
606         }
607         if (blob->version != PREFIX_MAP_VERSION_DSDB) {
608                 goto failed;
609         }
610         string = talloc_strdup(mem_ctx, "");
611         if (string == NULL) {
612                 goto failed;
613         }
614
615         for (i=0; i < blob->ctr.dsdb.num_mappings; i++) {
616                 DATA_BLOB oid_blob;
617                 const char *partial_oid = NULL;
618
619                 if (i > 0) {
620                         string = talloc_asprintf_append(string, ";"); 
621                 }
622
623                 oid_blob = data_blob_const(blob->ctr.dsdb.mappings[i].oid.binary_oid,
624                                            blob->ctr.dsdb.mappings[i].oid.length);
625                 if (!ber_read_partial_OID_String(blob, oid_blob, &partial_oid)) {
626                         DEBUG(0, ("ber_read_partial_OID failed on prefixMap item with id: 0x%X",
627                                   blob->ctr.dsdb.mappings[i].id_prefix));
628                         goto failed;
629                 }
630                 string = talloc_asprintf_append(string, "%u:%s", 
631                                                    blob->ctr.dsdb.mappings[i].id_prefix,
632                                                    partial_oid);
633                 talloc_free(discard_const(partial_oid));
634                 if (string == NULL) {
635                         goto failed;
636                 }
637         }
638
639         talloc_free(blob);
640         *out = data_blob_string_const(string);
641         return 0;
642
643 failed:
644         talloc_free(blob);
645         return -1;
646 }
647
648 static bool ldif_comparision_prefixMap_isString(const struct ldb_val *v)
649 {
650         if (v->length < 4) {
651                 return true;
652         }
653
654         if (IVAL(v->data, 0) == PREFIX_MAP_VERSION_DSDB) {
655                 return false;
656         }
657         
658         return true;
659 }
660
661 /*
662   canonicalise a prefixMap
663 */
664 static int ldif_canonicalise_prefixMap(struct ldb_context *ldb, void *mem_ctx,
665                                        const struct ldb_val *in, struct ldb_val *out)
666 {
667         if (ldif_comparision_prefixMap_isString(in)) {
668                 return ldif_read_prefixMap(ldb, mem_ctx, in, out);
669         }
670         return ldb_handler_copy(ldb, mem_ctx, in, out);
671 }
672
673 static int ldif_comparison_prefixMap(struct ldb_context *ldb, void *mem_ctx,
674                                      const struct ldb_val *v1,
675                                      const struct ldb_val *v2)
676 {
677         return ldb_any_comparison(ldb, mem_ctx, ldif_canonicalise_prefixMap,
678                                   v1, v2);
679 }
680
681 /* length limited conversion of a ldb_val to a int32_t */
682 static int val_to_int32(const struct ldb_val *in, int32_t *v)
683 {
684         char *end;
685         char buf[64];
686
687         /* make sure we don't read past the end of the data */
688         if (in->length > sizeof(buf)-1) {
689                 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
690         }
691         strncpy(buf, (char *)in->data, in->length);
692         buf[in->length] = 0;
693
694         /* We've to use "strtoll" here to have the intended overflows.
695          * Otherwise we may get "LONG_MAX" and the conversion is wrong. */
696         *v = (int32_t) strtoll(buf, &end, 0);
697         if (*end != 0) {
698                 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
699         }
700         return LDB_SUCCESS;
701 }
702
703 /* Canonicalisation of two 32-bit integers */
704 static int ldif_canonicalise_int32(struct ldb_context *ldb, void *mem_ctx,
705                         const struct ldb_val *in, struct ldb_val *out)
706 {
707         int32_t i;
708         int ret;
709
710         ret = val_to_int32(in, &i);
711         if (ret != LDB_SUCCESS) {
712                 return ret;
713         }
714         out->data = (uint8_t *) talloc_asprintf(mem_ctx, "%d", i);
715         if (out->data == NULL) {
716                 ldb_oom(ldb);
717                 return LDB_ERR_OPERATIONS_ERROR;
718         }
719         out->length = strlen((char *)out->data);
720         return 0;
721 }
722
723 /* Comparison of two 32-bit integers */
724 static int ldif_comparison_int32(struct ldb_context *ldb, void *mem_ctx,
725                                  const struct ldb_val *v1, const struct ldb_val *v2)
726 {
727         int32_t i1=0, i2=0;
728         val_to_int32(v1, &i1);
729         val_to_int32(v2, &i2);
730         if (i1 == i2) return 0;
731         return i1 > i2? 1 : -1;
732 }
733
734 /*
735   convert a NDR formatted blob to a ldif formatted repsFromTo
736 */
737 static int ldif_write_repsFromTo(struct ldb_context *ldb, void *mem_ctx,
738                                  const struct ldb_val *in, struct ldb_val *out)
739 {
740         return ldif_write_NDR(ldb, mem_ctx, in, out, 
741                               sizeof(struct repsFromToBlob),
742                               (ndr_pull_flags_fn_t)ndr_pull_repsFromToBlob,
743                               (ndr_print_fn_t)ndr_print_repsFromToBlob);
744 }
745
746 /*
747   convert a NDR formatted blob to a ldif formatted replPropertyMetaData
748 */
749 static int ldif_write_replPropertyMetaData(struct ldb_context *ldb, void *mem_ctx,
750                                            const struct ldb_val *in, struct ldb_val *out)
751 {
752         return ldif_write_NDR(ldb, mem_ctx, in, out, 
753                               sizeof(struct replPropertyMetaDataBlob),
754                               (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob,
755                               (ndr_print_fn_t)ndr_print_replPropertyMetaDataBlob);
756 }
757
758 /*
759   convert a NDR formatted blob to a ldif formatted replUpToDateVector
760 */
761 static int ldif_write_replUpToDateVector(struct ldb_context *ldb, void *mem_ctx,
762                                          const struct ldb_val *in, struct ldb_val *out)
763 {
764         return ldif_write_NDR(ldb, mem_ctx, in, out, 
765                               sizeof(struct replUpToDateVectorBlob),
766                               (ndr_pull_flags_fn_t)ndr_pull_replUpToDateVectorBlob,
767                               (ndr_print_fn_t)ndr_print_replUpToDateVectorBlob);
768 }
769
770
771 static int extended_dn_write_hex(struct ldb_context *ldb, void *mem_ctx,
772                                  const struct ldb_val *in, struct ldb_val *out)
773 {
774         *out = data_blob_string_const(data_blob_hex_string_lower(mem_ctx, in));
775         if (!out->data) {
776                 return -1;
777         }
778         return 0;
779 }
780
781 static const struct ldb_schema_syntax samba_syntaxes[] = {
782         {
783                 .name             = LDB_SYNTAX_SAMBA_SID,
784                 .ldif_read_fn     = ldif_read_objectSid,
785                 .ldif_write_fn    = ldif_write_objectSid,
786                 .canonicalise_fn  = ldif_canonicalise_objectSid,
787                 .comparison_fn    = ldif_comparison_objectSid
788         },{
789                 .name             = LDB_SYNTAX_SAMBA_SECURITY_DESCRIPTOR,
790                 .ldif_read_fn     = ldif_read_ntSecurityDescriptor,
791                 .ldif_write_fn    = ldif_write_ntSecurityDescriptor,
792                 .canonicalise_fn  = ldb_handler_copy,
793                 .comparison_fn    = ldb_comparison_binary
794         },{
795                 .name             = LDB_SYNTAX_SAMBA_GUID,
796                 .ldif_read_fn     = ldif_read_objectGUID,
797                 .ldif_write_fn    = ldif_write_objectGUID,
798                 .canonicalise_fn  = ldif_canonicalise_objectGUID,
799                 .comparison_fn    = ldif_comparison_objectGUID
800         },{
801                 .name             = LDB_SYNTAX_SAMBA_OBJECT_CATEGORY,
802                 .ldif_read_fn     = ldb_handler_copy,
803                 .ldif_write_fn    = ldb_handler_copy,
804                 .canonicalise_fn  = ldif_canonicalise_objectCategory,
805                 .comparison_fn    = ldif_comparison_objectCategory
806         },{
807                 .name             = LDB_SYNTAX_SAMBA_PREFIX_MAP,
808                 .ldif_read_fn     = ldif_read_prefixMap,
809                 .ldif_write_fn    = ldif_write_prefixMap,
810                 .canonicalise_fn  = ldif_canonicalise_prefixMap,
811                 .comparison_fn    = ldif_comparison_prefixMap
812         },{
813                 .name             = LDB_SYNTAX_SAMBA_INT32,
814                 .ldif_read_fn     = ldb_handler_copy,
815                 .ldif_write_fn    = ldb_handler_copy,
816                 .canonicalise_fn  = ldif_canonicalise_int32,
817                 .comparison_fn    = ldif_comparison_int32
818         },{
819                 .name             = LDB_SYNTAX_SAMBA_REPSFROMTO,
820                 .ldif_read_fn     = ldb_handler_copy,
821                 .ldif_write_fn    = ldif_write_repsFromTo,
822                 .canonicalise_fn  = ldb_handler_copy,
823                 .comparison_fn    = ldb_comparison_binary
824         },{
825                 .name             = LDB_SYNTAX_SAMBA_REPLPROPERTYMETADATA,
826                 .ldif_read_fn     = ldb_handler_copy,
827                 .ldif_write_fn    = ldif_write_replPropertyMetaData,
828                 .canonicalise_fn  = ldb_handler_copy,
829                 .comparison_fn    = ldb_comparison_binary
830         },{
831                 .name             = LDB_SYNTAX_SAMBA_REPLUPTODATEVECTOR,
832                 .ldif_read_fn     = ldb_handler_copy,
833                 .ldif_write_fn    = ldif_write_replUpToDateVector,
834                 .canonicalise_fn  = ldb_handler_copy,
835                 .comparison_fn    = ldb_comparison_binary
836         },{
837                 .name             = DSDB_SYNTAX_BINARY_DN,
838                 .ldif_read_fn     = ldb_handler_copy,
839                 .ldif_write_fn    = ldb_handler_copy,
840                 .canonicalise_fn  = dsdb_dn_binary_canonicalise,
841                 .comparison_fn    = dsdb_dn_binary_comparison
842         },{
843                 .name             = DSDB_SYNTAX_STRING_DN,
844                 .ldif_read_fn     = ldb_handler_copy,
845                 .ldif_write_fn    = ldb_handler_copy,
846                 .canonicalise_fn  = dsdb_dn_string_canonicalise,
847                 .comparison_fn    = dsdb_dn_string_comparison
848         },
849 };
850
851 static const struct ldb_dn_extended_syntax samba_dn_syntax[] = {
852         {
853                 .name             = "SID",
854                 .read_fn          = extended_dn_read_SID,
855                 .write_clear_fn   = ldif_write_objectSid,
856                 .write_hex_fn     = extended_dn_write_hex
857         },{
858                 .name             = "GUID",
859                 .read_fn          = extended_dn_read_GUID,
860                 .write_clear_fn   = ldif_write_objectGUID,
861                 .write_hex_fn     = extended_dn_write_hex
862         },{
863                 .name             = "WKGUID",
864                 .read_fn          = ldb_handler_copy,
865                 .write_clear_fn   = ldb_handler_copy,
866                 .write_hex_fn     = ldb_handler_copy
867         },{
868                 .name             = "RMD_INVOCID",
869                 .read_fn          = extended_dn_read_GUID,
870                 .write_clear_fn   = ldif_write_objectGUID,
871                 .write_hex_fn     = extended_dn_write_hex
872         },{
873                 .name             = "DELETED",
874                 .read_fn          = ldb_handler_copy,
875                 .write_clear_fn   = ldb_handler_copy,
876                 .write_hex_fn     = ldb_handler_copy
877         },{
878                 .name             = "RMD_ADDTIME",
879                 .read_fn          = ldb_handler_copy,
880                 .write_clear_fn   = ldb_handler_copy,
881                 .write_hex_fn     = ldb_handler_copy
882         },{
883                 .name             = "RMD_CHANGETIME",
884                 .read_fn          = ldb_handler_copy,
885                 .write_clear_fn   = ldb_handler_copy,
886                 .write_hex_fn     = ldb_handler_copy
887         },{
888                 .name             = "RMD_LOCAL_USN",
889                 .read_fn          = ldb_handler_copy,
890                 .write_clear_fn   = ldb_handler_copy,
891                 .write_hex_fn     = ldb_handler_copy
892         },{
893                 .name             = "RMD_ORIGINATING_USN",
894                 .read_fn          = ldb_handler_copy,
895                 .write_clear_fn   = ldb_handler_copy,
896                 .write_hex_fn     = ldb_handler_copy
897         },{
898                 .name             = "RMD_VERSION",
899                 .read_fn          = ldb_handler_copy,
900                 .write_clear_fn   = ldb_handler_copy,
901                 .write_hex_fn     = ldb_handler_copy
902         }
903 };
904
905 /* TODO: Should be dynamic at some point */
906 static const struct {
907         const char *name;
908         const char *syntax;
909 } samba_attributes[] = {
910         { "objectSid",                  LDB_SYNTAX_SAMBA_SID },
911         { "securityIdentifier",         LDB_SYNTAX_SAMBA_SID },
912         { "ntSecurityDescriptor",       LDB_SYNTAX_SAMBA_SECURITY_DESCRIPTOR },
913         { "objectGUID",                 LDB_SYNTAX_SAMBA_GUID },
914         { "invocationId",               LDB_SYNTAX_SAMBA_GUID },
915         { "schemaIDGUID",               LDB_SYNTAX_SAMBA_GUID },
916         { "oMSyntax",                   LDB_SYNTAX_SAMBA_INT32 },
917         { "attributeSecurityGUID",      LDB_SYNTAX_SAMBA_GUID },
918         { "parentGUID",                 LDB_SYNTAX_SAMBA_GUID },
919         { "siteGUID",                   LDB_SYNTAX_SAMBA_GUID },
920         { "pKTGUID",                    LDB_SYNTAX_SAMBA_GUID },
921         { "fRSVersionGUID",             LDB_SYNTAX_SAMBA_GUID },
922         { "fRSReplicaSetGUID",          LDB_SYNTAX_SAMBA_GUID },
923         { "netbootGUID",                LDB_SYNTAX_SAMBA_GUID },
924         { "msDS-OptionalFeatureGUID",   LDB_SYNTAX_SAMBA_GUID },
925         { "objectCategory",             LDB_SYNTAX_SAMBA_OBJECT_CATEGORY },
926         { "prefixMap",                  LDB_SYNTAX_SAMBA_PREFIX_MAP },
927         { "repsFrom",                   LDB_SYNTAX_SAMBA_REPSFROMTO },
928         { "repsTo",                     LDB_SYNTAX_SAMBA_REPSFROMTO },
929         { "replPropertyMetaData",       LDB_SYNTAX_SAMBA_REPLPROPERTYMETADATA },
930         { "replUpToDateVector",         LDB_SYNTAX_SAMBA_REPLUPTODATEVECTOR },
931 };
932
933 const struct ldb_schema_syntax *ldb_samba_syntax_by_name(struct ldb_context *ldb, const char *name)
934 {
935         uint32_t j;
936         const struct ldb_schema_syntax *s = NULL;
937         
938         for (j=0; j < ARRAY_SIZE(samba_syntaxes); j++) {
939                 if (strcmp(name, samba_syntaxes[j].name) == 0) {
940                         s = &samba_syntaxes[j];
941                         break;
942                 }
943         }
944         return s;
945 }
946
947 const struct ldb_schema_syntax *ldb_samba_syntax_by_lDAPDisplayName(struct ldb_context *ldb, const char *name)
948 {
949         uint32_t j;
950         const struct ldb_schema_syntax *s = NULL;
951
952         for (j=0; j < ARRAY_SIZE(samba_attributes); j++) {
953                 if (strcmp(samba_attributes[j].name, name) == 0) {
954                         s = ldb_samba_syntax_by_name(ldb, samba_attributes[j].syntax);
955                         break;
956                 }
957         }
958         
959         return s;
960 }
961
962 /*
963   register the samba ldif handlers
964 */
965 int ldb_register_samba_handlers(struct ldb_context *ldb)
966 {
967         uint32_t i;
968
969         for (i=0; i < ARRAY_SIZE(samba_attributes); i++) {
970                 int ret;
971                 const struct ldb_schema_syntax *s = NULL;
972
973                 s = ldb_samba_syntax_by_name(ldb, samba_attributes[i].syntax);
974
975                 if (!s) {
976                         s = ldb_standard_syntax_by_name(ldb, samba_attributes[i].syntax);
977                 }
978
979                 if (!s) {
980                         return -1;
981                 }
982
983                 ret = ldb_schema_attribute_add_with_syntax(ldb, samba_attributes[i].name, LDB_ATTR_FLAG_FIXED, s);
984                 if (ret != LDB_SUCCESS) {
985                         return ret;
986                 }
987         }
988
989         for (i=0; i < ARRAY_SIZE(samba_dn_syntax); i++) {
990                 int ret;
991                 ret = ldb_dn_extended_add_syntax(ldb, LDB_ATTR_FLAG_FIXED, &samba_dn_syntax[i]);
992                 if (ret != LDB_SUCCESS) {
993                         return ret;
994                 }
995
996                 
997         }
998
999         return LDB_SUCCESS;
1000 }