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