s4: Make the int32 problem more clear - and fix another error
[ira/wip.git] / source4 / dsdb / schema / schema_syntax.c
1 /* 
2    Unix SMB/CIFS mplementation.
3    DSDB schema syntaxes
4    
5    Copyright (C) Stefan Metzmacher <metze@samba.org> 2006
6    Copyright (C) Simo Sorce 2005
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2008
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21    
22 */
23 #include "includes.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "librpc/gen_ndr/ndr_drsuapi.h"
26 #include "librpc/gen_ndr/ndr_security.h"
27 #include "librpc/gen_ndr/ndr_misc.h"
28 #include "lib/ldb/include/ldb.h"
29 #include "lib/ldb/include/ldb_errors.h"
30 #include "system/time.h"
31 #include "../lib/util/charset/charset.h"
32 #include "librpc/ndr/libndr.h"
33
34 static WERROR dsdb_syntax_FOOBAR_drsuapi_to_ldb(struct ldb_context *ldb, 
35                                                 const struct dsdb_schema *schema,
36                                                 const struct dsdb_attribute *attr,
37                                                 const struct drsuapi_DsReplicaAttribute *in,
38                                                 TALLOC_CTX *mem_ctx,
39                                                 struct ldb_message_element *out)
40 {
41         uint32_t i;
42
43         out->flags      = 0;
44         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
45         W_ERROR_HAVE_NO_MEMORY(out->name);
46
47         out->num_values = in->value_ctr.num_values;
48         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
49         W_ERROR_HAVE_NO_MEMORY(out->values);
50
51         for (i=0; i < out->num_values; i++) {
52                 char *str;
53
54                 if (in->value_ctr.values[i].blob == NULL) {
55                         return WERR_FOOBAR;
56                 }
57
58                 str = talloc_asprintf(out->values, "%s: not implemented",
59                                       attr->syntax->name);
60                 W_ERROR_HAVE_NO_MEMORY(str);
61
62                 out->values[i] = data_blob_string_const(str);
63         }
64
65         return WERR_OK;
66 }
67
68 static WERROR dsdb_syntax_FOOBAR_ldb_to_drsuapi(struct ldb_context *ldb, 
69                                                 const struct dsdb_schema *schema,
70                                                 const struct dsdb_attribute *attr,
71                                                 const struct ldb_message_element *in,
72                                                 TALLOC_CTX *mem_ctx,
73                                                 struct drsuapi_DsReplicaAttribute *out)
74 {
75         return WERR_FOOBAR;
76 }
77
78 static WERROR dsdb_syntax_BOOL_drsuapi_to_ldb(struct ldb_context *ldb, 
79                                               const struct dsdb_schema *schema,
80                                               const struct dsdb_attribute *attr,
81                                               const struct drsuapi_DsReplicaAttribute *in,
82                                               TALLOC_CTX *mem_ctx,
83                                               struct ldb_message_element *out)
84 {
85         uint32_t i;
86
87         out->flags      = 0;
88         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
89         W_ERROR_HAVE_NO_MEMORY(out->name);
90
91         out->num_values = in->value_ctr.num_values;
92         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
93         W_ERROR_HAVE_NO_MEMORY(out->values);
94
95         for (i=0; i < out->num_values; i++) {
96                 uint32_t v;
97                 char *str;
98
99                 if (in->value_ctr.values[i].blob == NULL) {
100                         return WERR_FOOBAR;
101                 }
102
103                 if (in->value_ctr.values[i].blob->length != 4) {
104                         return WERR_FOOBAR;
105                 }
106
107                 v = IVAL(in->value_ctr.values[i].blob->data, 0);
108
109                 if (v != 0) {
110                         str = talloc_strdup(out->values, "TRUE");
111                         W_ERROR_HAVE_NO_MEMORY(str);
112                 } else {
113                         str = talloc_strdup(out->values, "FALSE");
114                         W_ERROR_HAVE_NO_MEMORY(str);
115                 }
116
117                 out->values[i] = data_blob_string_const(str);
118         }
119
120         return WERR_OK;
121 }
122
123 static WERROR dsdb_syntax_BOOL_ldb_to_drsuapi(struct ldb_context *ldb, 
124                                               const struct dsdb_schema *schema,
125                                               const struct dsdb_attribute *attr,
126                                               const struct ldb_message_element *in,
127                                               TALLOC_CTX *mem_ctx,
128                                               struct drsuapi_DsReplicaAttribute *out)
129 {
130         uint32_t i;
131         DATA_BLOB *blobs;
132
133         if (attr->attributeID_id == 0xFFFFFFFF) {
134                 return WERR_FOOBAR;
135         }
136
137         out->attid                      = attr->attributeID_id;
138         out->value_ctr.num_values       = in->num_values;
139         out->value_ctr.values           = talloc_array(mem_ctx,
140                                                        struct drsuapi_DsAttributeValue,
141                                                        in->num_values);
142         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
143
144         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
145         W_ERROR_HAVE_NO_MEMORY(blobs);
146
147         for (i=0; i < in->num_values; i++) {
148                 out->value_ctr.values[i].blob   = &blobs[i];
149
150                 blobs[i] = data_blob_talloc(blobs, NULL, 4);
151                 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
152
153                 if (strcmp("TRUE", (const char *)in->values[i].data) == 0) {
154                         SIVAL(blobs[i].data, 0, 0x00000001);
155                 } else if (strcmp("FALSE", (const char *)in->values[i].data) == 0) {
156                         SIVAL(blobs[i].data, 0, 0x00000000);
157                 } else {
158                         return WERR_FOOBAR;
159                 }
160         }
161
162         return WERR_OK;
163 }
164
165 static WERROR dsdb_syntax_INT32_drsuapi_to_ldb(struct ldb_context *ldb, 
166                                                const struct dsdb_schema *schema,
167                                                const struct dsdb_attribute *attr,
168                                                const struct drsuapi_DsReplicaAttribute *in,
169                                                TALLOC_CTX *mem_ctx,
170                                                struct ldb_message_element *out)
171 {
172         uint32_t i;
173
174         out->flags      = 0;
175         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
176         W_ERROR_HAVE_NO_MEMORY(out->name);
177
178         out->num_values = in->value_ctr.num_values;
179         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
180         W_ERROR_HAVE_NO_MEMORY(out->values);
181
182         for (i=0; i < out->num_values; i++) {
183                 int32_t v;
184                 char *str;
185
186                 if (in->value_ctr.values[i].blob == NULL) {
187                         return WERR_FOOBAR;
188                 }
189
190                 if (in->value_ctr.values[i].blob->length != 4) {
191                         return WERR_FOOBAR;
192                 }
193
194                 v = IVALS(in->value_ctr.values[i].blob->data, 0);
195
196                 str = talloc_asprintf(out->values, "%d", v);
197                 W_ERROR_HAVE_NO_MEMORY(str);
198
199                 out->values[i] = data_blob_string_const(str);
200         }
201
202         return WERR_OK;
203 }
204
205 static WERROR dsdb_syntax_INT32_ldb_to_drsuapi(struct ldb_context *ldb, 
206                                                const struct dsdb_schema *schema,
207                                                const struct dsdb_attribute *attr,
208                                                const struct ldb_message_element *in,
209                                                TALLOC_CTX *mem_ctx,
210                                                struct drsuapi_DsReplicaAttribute *out)
211 {
212         uint32_t i;
213         DATA_BLOB *blobs;
214
215         if (attr->attributeID_id == 0xFFFFFFFF) {
216                 return WERR_FOOBAR;
217         }
218
219         out->attid                      = attr->attributeID_id;
220         out->value_ctr.num_values       = in->num_values;
221         out->value_ctr.values           = talloc_array(mem_ctx,
222                                                        struct drsuapi_DsAttributeValue,
223                                                        in->num_values);
224         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
225
226         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
227         W_ERROR_HAVE_NO_MEMORY(blobs);
228
229         for (i=0; i < in->num_values; i++) {
230                 int32_t v;
231
232                 out->value_ctr.values[i].blob   = &blobs[i];
233
234                 blobs[i] = data_blob_talloc(blobs, NULL, 4);
235                 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
236
237                 /* We've to use "strtoll" here to have the intended overflows.
238                  * Otherwise we may get "LONG_MAX" and the conversion is wrong. */
239                 v = strtoll((const char *)in->values[i].data, NULL, 0);
240
241                 SIVALS(blobs[i].data, 0, v);
242         }
243
244         return WERR_OK;
245 }
246
247 static WERROR dsdb_syntax_INT64_drsuapi_to_ldb(struct ldb_context *ldb, 
248                                                const struct dsdb_schema *schema,
249                                                const struct dsdb_attribute *attr,
250                                                const struct drsuapi_DsReplicaAttribute *in,
251                                                TALLOC_CTX *mem_ctx,
252                                                struct ldb_message_element *out)
253 {
254         uint32_t i;
255
256         out->flags      = 0;
257         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
258         W_ERROR_HAVE_NO_MEMORY(out->name);
259
260         out->num_values = in->value_ctr.num_values;
261         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
262         W_ERROR_HAVE_NO_MEMORY(out->values);
263
264         for (i=0; i < out->num_values; i++) {
265                 int64_t v;
266                 char *str;
267
268                 if (in->value_ctr.values[i].blob == NULL) {
269                         return WERR_FOOBAR;
270                 }
271
272                 if (in->value_ctr.values[i].blob->length != 8) {
273                         return WERR_FOOBAR;
274                 }
275
276                 v = BVALS(in->value_ctr.values[i].blob->data, 0);
277
278                 str = talloc_asprintf(out->values, "%lld", (long long int)v);
279                 W_ERROR_HAVE_NO_MEMORY(str);
280
281                 out->values[i] = data_blob_string_const(str);
282         }
283
284         return WERR_OK;
285 }
286
287 static WERROR dsdb_syntax_INT64_ldb_to_drsuapi(struct ldb_context *ldb, 
288                                                const struct dsdb_schema *schema,
289                                                const struct dsdb_attribute *attr,
290                                                const struct ldb_message_element *in,
291                                                TALLOC_CTX *mem_ctx,
292                                                struct drsuapi_DsReplicaAttribute *out)
293 {
294         uint32_t i;
295         DATA_BLOB *blobs;
296
297         if (attr->attributeID_id == 0xFFFFFFFF) {
298                 return WERR_FOOBAR;
299         }
300
301         out->attid                      = attr->attributeID_id;
302         out->value_ctr.num_values       = in->num_values;
303         out->value_ctr.values           = talloc_array(mem_ctx,
304                                                        struct drsuapi_DsAttributeValue,
305                                                        in->num_values);
306         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
307
308         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
309         W_ERROR_HAVE_NO_MEMORY(blobs);
310
311         for (i=0; i < in->num_values; i++) {
312                 int64_t v;
313
314                 out->value_ctr.values[i].blob   = &blobs[i];
315
316                 blobs[i] = data_blob_talloc(blobs, NULL, 8);
317                 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
318
319                 v = strtoll((const char *)in->values[i].data, NULL, 10);
320
321                 SBVALS(blobs[i].data, 0, v);
322         }
323
324         return WERR_OK;
325 }
326
327 static WERROR dsdb_syntax_NTTIME_UTC_drsuapi_to_ldb(struct ldb_context *ldb, 
328                                                     const struct dsdb_schema *schema,
329                                                     const struct dsdb_attribute *attr,
330                                                     const struct drsuapi_DsReplicaAttribute *in,
331                                                     TALLOC_CTX *mem_ctx,
332                                                     struct ldb_message_element *out)
333 {
334         uint32_t i;
335
336         out->flags      = 0;
337         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
338         W_ERROR_HAVE_NO_MEMORY(out->name);
339
340         out->num_values = in->value_ctr.num_values;
341         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
342         W_ERROR_HAVE_NO_MEMORY(out->values);
343
344         for (i=0; i < out->num_values; i++) {
345                 NTTIME v;
346                 time_t t;
347                 char *str;
348
349                 if (in->value_ctr.values[i].blob == NULL) {
350                         return WERR_FOOBAR;
351                 }
352
353                 if (in->value_ctr.values[i].blob->length != 8) {
354                         return WERR_FOOBAR;
355                 }
356
357                 v = BVAL(in->value_ctr.values[i].blob->data, 0);
358                 v *= 10000000;
359                 t = nt_time_to_unix(v);
360
361                 /* 
362                  * NOTE: On a w2k3 server you can set a GeneralizedTime string
363                  *       via LDAP, but you get back an UTCTime string,
364                  *       but via DRSUAPI you get back the NTTIME_1sec value
365                  *       that represents the GeneralizedTime value!
366                  *
367                  *       So if we store the UTCTime string in our ldb
368                  *       we'll loose information!
369                  */
370                 str = ldb_timestring_utc(out->values, t); 
371                 W_ERROR_HAVE_NO_MEMORY(str);
372                 out->values[i] = data_blob_string_const(str);
373         }
374
375         return WERR_OK;
376 }
377
378 static WERROR dsdb_syntax_NTTIME_UTC_ldb_to_drsuapi(struct ldb_context *ldb, 
379                                                     const struct dsdb_schema *schema,
380                                                     const struct dsdb_attribute *attr,
381                                                     const struct ldb_message_element *in,
382                                                     TALLOC_CTX *mem_ctx,
383                                                     struct drsuapi_DsReplicaAttribute *out)
384 {
385         uint32_t i;
386         DATA_BLOB *blobs;
387
388         if (attr->attributeID_id == 0xFFFFFFFF) {
389                 return WERR_FOOBAR;
390         }
391
392         out->attid                      = attr->attributeID_id;
393         out->value_ctr.num_values       = in->num_values;
394         out->value_ctr.values           = talloc_array(mem_ctx,
395                                                        struct drsuapi_DsAttributeValue,
396                                                        in->num_values);
397         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
398
399         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
400         W_ERROR_HAVE_NO_MEMORY(blobs);
401
402         for (i=0; i < in->num_values; i++) {
403                 NTTIME v;
404                 time_t t;
405
406                 out->value_ctr.values[i].blob   = &blobs[i];
407
408                 blobs[i] = data_blob_talloc(blobs, NULL, 8);
409                 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
410
411                 t = ldb_string_utc_to_time((const char *)in->values[i].data);
412                 unix_to_nt_time(&v, t);
413                 v /= 10000000;
414
415                 SBVAL(blobs[i].data, 0, v);
416         }
417
418         return WERR_OK;
419 }
420
421 static WERROR dsdb_syntax_NTTIME_drsuapi_to_ldb(struct ldb_context *ldb, 
422                                                 const struct dsdb_schema *schema,
423                                                 const struct dsdb_attribute *attr,
424                                                 const struct drsuapi_DsReplicaAttribute *in,
425                                                 TALLOC_CTX *mem_ctx,
426                                                 struct ldb_message_element *out)
427 {
428         uint32_t i;
429
430         out->flags      = 0;
431         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
432         W_ERROR_HAVE_NO_MEMORY(out->name);
433
434         out->num_values = in->value_ctr.num_values;
435         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
436         W_ERROR_HAVE_NO_MEMORY(out->values);
437
438         for (i=0; i < out->num_values; i++) {
439                 NTTIME v;
440                 time_t t;
441                 char *str;
442
443                 if (in->value_ctr.values[i].blob == NULL) {
444                         return WERR_FOOBAR;
445                 }
446
447                 if (in->value_ctr.values[i].blob->length != 8) {
448                         return WERR_FOOBAR;
449                 }
450
451                 v = BVAL(in->value_ctr.values[i].blob->data, 0);
452                 v *= 10000000;
453                 t = nt_time_to_unix(v);
454
455                 str = ldb_timestring(out->values, t); 
456                 W_ERROR_HAVE_NO_MEMORY(str);
457
458                 out->values[i] = data_blob_string_const(str);
459         }
460
461         return WERR_OK;
462 }
463
464 static WERROR dsdb_syntax_NTTIME_ldb_to_drsuapi(struct ldb_context *ldb, 
465                                                 const struct dsdb_schema *schema,
466                                                 const struct dsdb_attribute *attr,
467                                                 const struct ldb_message_element *in,
468                                                 TALLOC_CTX *mem_ctx,
469                                                 struct drsuapi_DsReplicaAttribute *out)
470 {
471         uint32_t i;
472         DATA_BLOB *blobs;
473
474         if (attr->attributeID_id == 0xFFFFFFFF) {
475                 return WERR_FOOBAR;
476         }
477
478         out->attid                      = attr->attributeID_id;
479         out->value_ctr.num_values       = in->num_values;
480         out->value_ctr.values           = talloc_array(mem_ctx,
481                                                        struct drsuapi_DsAttributeValue,
482                                                        in->num_values);
483         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
484
485         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
486         W_ERROR_HAVE_NO_MEMORY(blobs);
487
488         for (i=0; i < in->num_values; i++) {
489                 NTTIME v;
490                 time_t t;
491
492                 out->value_ctr.values[i].blob   = &blobs[i];
493
494                 blobs[i] = data_blob_talloc(blobs, NULL, 8);
495                 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
496
497                 t = ldb_string_to_time((const char *)in->values[i].data);
498                 unix_to_nt_time(&v, t);
499                 v /= 10000000;
500
501                 SBVAL(blobs[i].data, 0, v);
502         }
503
504         return WERR_OK;
505 }
506
507 static WERROR dsdb_syntax_DATA_BLOB_drsuapi_to_ldb(struct ldb_context *ldb, 
508                                                    const struct dsdb_schema *schema,
509                                                    const struct dsdb_attribute *attr,
510                                                    const struct drsuapi_DsReplicaAttribute *in,
511                                                    TALLOC_CTX *mem_ctx,
512                                                    struct ldb_message_element *out)
513 {
514         uint32_t i;
515
516         out->flags      = 0;
517         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
518         W_ERROR_HAVE_NO_MEMORY(out->name);
519
520         out->num_values = in->value_ctr.num_values;
521         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
522         W_ERROR_HAVE_NO_MEMORY(out->values);
523
524         for (i=0; i < out->num_values; i++) {
525                 if (in->value_ctr.values[i].blob == NULL) {
526                         return WERR_FOOBAR;
527                 }
528
529                 if (in->value_ctr.values[i].blob->length == 0) {
530                         return WERR_FOOBAR;
531                 }
532
533                 out->values[i] = data_blob_dup_talloc(out->values,
534                                                       in->value_ctr.values[i].blob);
535                 W_ERROR_HAVE_NO_MEMORY(out->values[i].data);
536         }
537
538         return WERR_OK;
539 }
540
541 static WERROR dsdb_syntax_DATA_BLOB_ldb_to_drsuapi(struct ldb_context *ldb, 
542                                                    const struct dsdb_schema *schema,
543                                                    const struct dsdb_attribute *attr,
544                                                    const struct ldb_message_element *in,
545                                                    TALLOC_CTX *mem_ctx,
546                                                    struct drsuapi_DsReplicaAttribute *out)
547 {
548         uint32_t i;
549         DATA_BLOB *blobs;
550
551         if (attr->attributeID_id == 0xFFFFFFFF) {
552                 return WERR_FOOBAR;
553         }
554
555         out->attid                      = attr->attributeID_id;
556         out->value_ctr.num_values       = in->num_values;
557         out->value_ctr.values           = talloc_array(mem_ctx,
558                                                        struct drsuapi_DsAttributeValue,
559                                                        in->num_values);
560         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
561
562         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
563         W_ERROR_HAVE_NO_MEMORY(blobs);
564
565         for (i=0; i < in->num_values; i++) {
566                 out->value_ctr.values[i].blob   = &blobs[i];
567
568                 blobs[i] = data_blob_dup_talloc(blobs, &in->values[i]);
569                 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
570         }
571
572         return WERR_OK;
573 }
574
575 static WERROR _dsdb_syntax_OID_obj_drsuapi_to_ldb(struct ldb_context *ldb, 
576                                                   const struct dsdb_schema *schema,
577                                                   const struct dsdb_attribute *attr,
578                                                   const struct drsuapi_DsReplicaAttribute *in,
579                                                   TALLOC_CTX *mem_ctx,
580                                                   struct ldb_message_element *out)
581 {
582         uint32_t i;
583
584         out->flags      = 0;
585         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
586         W_ERROR_HAVE_NO_MEMORY(out->name);
587
588         out->num_values = in->value_ctr.num_values;
589         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
590         W_ERROR_HAVE_NO_MEMORY(out->values);
591
592         for (i=0; i < out->num_values; i++) {
593                 uint32_t v;
594                 const struct dsdb_class *c;
595                 const char *str;
596
597                 if (in->value_ctr.values[i].blob == NULL) {
598                         return WERR_FOOBAR;
599                 }
600
601                 if (in->value_ctr.values[i].blob->length != 4) {
602                         return WERR_FOOBAR;
603                 }
604
605                 v = IVAL(in->value_ctr.values[i].blob->data, 0);
606
607                 c = dsdb_class_by_governsID_id(schema, v);
608                 if (!c) {
609                         return WERR_FOOBAR;
610                 }
611
612                 str = talloc_strdup(out->values, c->lDAPDisplayName);
613                 W_ERROR_HAVE_NO_MEMORY(str);
614
615                 /* the values need to be reversed */
616                 out->values[out->num_values - (i + 1)] = data_blob_string_const(str);
617         }
618
619         return WERR_OK;
620 }
621
622 static WERROR _dsdb_syntax_OID_oid_drsuapi_to_ldb(struct ldb_context *ldb, 
623                                                   const struct dsdb_schema *schema,
624                                                   const struct dsdb_attribute *attr,
625                                                   const struct drsuapi_DsReplicaAttribute *in,
626                                                   TALLOC_CTX *mem_ctx,
627                                                   struct ldb_message_element *out)
628 {
629         uint32_t i;
630
631         out->flags      = 0;
632         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
633         W_ERROR_HAVE_NO_MEMORY(out->name);
634
635         out->num_values = in->value_ctr.num_values;
636         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
637         W_ERROR_HAVE_NO_MEMORY(out->values);
638
639         for (i=0; i < out->num_values; i++) {
640                 uint32_t v;
641                 WERROR status;
642                 const char *str;
643
644                 if (in->value_ctr.values[i].blob == NULL) {
645                         return WERR_FOOBAR;
646                 }
647
648                 if (in->value_ctr.values[i].blob->length != 4) {
649                         return WERR_FOOBAR;
650                 }
651
652                 v = IVAL(in->value_ctr.values[i].blob->data, 0);
653
654                 status = dsdb_map_int2oid(schema, v, out->values, &str);
655                 W_ERROR_NOT_OK_RETURN(status);
656
657                 out->values[i] = data_blob_string_const(str);
658         }
659
660         return WERR_OK;
661 }
662
663 static WERROR dsdb_syntax_OID_drsuapi_to_ldb(struct ldb_context *ldb, 
664                                              const struct dsdb_schema *schema,
665                                              const struct dsdb_attribute *attr,
666                                              const struct drsuapi_DsReplicaAttribute *in,
667                                              TALLOC_CTX *mem_ctx,
668                                              struct ldb_message_element *out)
669 {
670         uint32_t i;
671
672         switch (attr->attributeID_id) {
673         case DRSUAPI_ATTRIBUTE_objectClass:
674                 return _dsdb_syntax_OID_obj_drsuapi_to_ldb(ldb, schema, attr, in, mem_ctx, out);
675         case DRSUAPI_ATTRIBUTE_governsID:
676         case DRSUAPI_ATTRIBUTE_attributeID:
677         case DRSUAPI_ATTRIBUTE_attributeSyntax:
678                 return _dsdb_syntax_OID_oid_drsuapi_to_ldb(ldb, schema, attr, in, mem_ctx, out);
679         }
680
681         out->flags      = 0;
682         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
683         W_ERROR_HAVE_NO_MEMORY(out->name);
684
685         out->num_values = in->value_ctr.num_values;
686         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
687         W_ERROR_HAVE_NO_MEMORY(out->values);
688
689         for (i=0; i < out->num_values; i++) {
690                 uint32_t v;
691                 const char *name;
692                 char *str;
693
694                 if (in->value_ctr.values[i].blob == NULL) {
695                         return WERR_FOOBAR;
696                 }
697
698                 if (in->value_ctr.values[i].blob->length != 4) {
699                         return WERR_FOOBAR;
700                 }
701
702                 v = IVAL(in->value_ctr.values[i].blob->data, 0);
703
704                 name = dsdb_lDAPDisplayName_by_id(schema, v);
705                 if (!name) {
706                         return WERR_FOOBAR;
707                 }
708
709                 str = talloc_strdup(out->values, name);
710                 W_ERROR_HAVE_NO_MEMORY(str);
711
712                 out->values[i] = data_blob_string_const(str);
713         }
714
715         return WERR_OK;
716 }
717
718 static WERROR dsdb_syntax_OID_ldb_to_drsuapi(struct ldb_context *ldb, 
719                                              const struct dsdb_schema *schema,
720                                              const struct dsdb_attribute *attr,
721                                              const struct ldb_message_element *in,
722                                              TALLOC_CTX *mem_ctx,
723                                              struct drsuapi_DsReplicaAttribute *out)
724 {
725         uint32_t i;
726         DATA_BLOB *blobs;
727
728         if (attr->attributeID_id == 0xFFFFFFFF) {
729                 return WERR_FOOBAR;
730         }
731
732         switch (attr->attributeID_id) {
733         case DRSUAPI_ATTRIBUTE_objectClass:
734         case DRSUAPI_ATTRIBUTE_governsID:
735         case DRSUAPI_ATTRIBUTE_attributeID:
736         case DRSUAPI_ATTRIBUTE_attributeSyntax:
737                 return dsdb_syntax_FOOBAR_ldb_to_drsuapi(ldb, schema, attr, in, mem_ctx, out);
738         }
739
740         out->attid                      = attr->attributeID_id;
741         out->value_ctr.num_values       = in->num_values;
742         out->value_ctr.values           = talloc_array(mem_ctx,
743                                                        struct drsuapi_DsAttributeValue,
744                                                        in->num_values);
745         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
746
747         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
748         W_ERROR_HAVE_NO_MEMORY(blobs);
749
750         for (i=0; i < in->num_values; i++) {
751                 uint32_t v;
752
753                 out->value_ctr.values[i].blob   = &blobs[i];
754
755                 blobs[i] = data_blob_talloc(blobs, NULL, 4);
756                 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
757
758                 v = strtol((const char *)in->values[i].data, NULL, 10);
759
760                 SIVAL(blobs[i].data, 0, v);
761         }
762
763         return WERR_OK;
764 }
765
766 static WERROR dsdb_syntax_UNICODE_drsuapi_to_ldb(struct ldb_context *ldb, 
767                                                  const struct dsdb_schema *schema,
768                                                  const struct dsdb_attribute *attr,
769                                                  const struct drsuapi_DsReplicaAttribute *in,
770                                                  TALLOC_CTX *mem_ctx,
771                                                  struct ldb_message_element *out)
772 {
773         uint32_t i;
774
775         out->flags      = 0;
776         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
777         W_ERROR_HAVE_NO_MEMORY(out->name);
778
779         out->num_values = in->value_ctr.num_values;
780         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
781         W_ERROR_HAVE_NO_MEMORY(out->values);
782
783         for (i=0; i < out->num_values; i++) {
784                 char *str;
785
786                 if (in->value_ctr.values[i].blob == NULL) {
787                         return WERR_FOOBAR;
788                 }
789
790                 if (in->value_ctr.values[i].blob->length == 0) {
791                         return WERR_FOOBAR;
792                 }
793
794                 if (!convert_string_talloc_convenience(out->values, 
795                                                 schema->iconv_convenience, 
796                                                                         CH_UTF16, CH_UNIX,
797                                             in->value_ctr.values[i].blob->data,
798                                             in->value_ctr.values[i].blob->length,
799                                             (void **)&str, NULL, false)) {
800                         return WERR_FOOBAR;
801                 }
802
803                 out->values[i] = data_blob_string_const(str);
804         }
805
806         return WERR_OK;
807 }
808
809 static WERROR dsdb_syntax_UNICODE_ldb_to_drsuapi(struct ldb_context *ldb, 
810                                                  const struct dsdb_schema *schema,
811                                                  const struct dsdb_attribute *attr,
812                                                  const struct ldb_message_element *in,
813                                                  TALLOC_CTX *mem_ctx,
814                                                  struct drsuapi_DsReplicaAttribute *out)
815 {
816         uint32_t i;
817         DATA_BLOB *blobs;
818
819         if (attr->attributeID_id == 0xFFFFFFFF) {
820                 return WERR_FOOBAR;
821         }
822
823         out->attid                      = attr->attributeID_id;
824         out->value_ctr.num_values       = in->num_values;
825         out->value_ctr.values           = talloc_array(mem_ctx,
826                                                        struct drsuapi_DsAttributeValue,
827                                                        in->num_values);
828         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
829
830         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
831         W_ERROR_HAVE_NO_MEMORY(blobs);
832
833         for (i=0; i < in->num_values; i++) {
834                 out->value_ctr.values[i].blob   = &blobs[i];
835
836                 if (!convert_string_talloc_convenience(blobs,
837                         schema->iconv_convenience, CH_UNIX, CH_UTF16,
838                         in->values[i].data, in->values[i].length,
839                         (void **)&blobs[i].data, &blobs[i].length, false)) {
840                                 return WERR_FOOBAR;
841                 }
842         }
843
844         return WERR_OK;
845 }
846
847 static WERROR dsdb_syntax_DN_drsuapi_to_ldb(struct ldb_context *ldb, 
848                                             const struct dsdb_schema *schema,
849                                             const struct dsdb_attribute *attr,
850                                             const struct drsuapi_DsReplicaAttribute *in,
851                                             TALLOC_CTX *mem_ctx,
852                                             struct ldb_message_element *out)
853 {
854         uint32_t i;
855         int ret;
856
857         out->flags      = 0;
858         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
859         W_ERROR_HAVE_NO_MEMORY(out->name);
860
861         out->num_values = in->value_ctr.num_values;
862         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
863         W_ERROR_HAVE_NO_MEMORY(out->values);
864
865         for (i=0; i < out->num_values; i++) {
866                 struct drsuapi_DsReplicaObjectIdentifier3 id3;
867                 enum ndr_err_code ndr_err;
868                 DATA_BLOB guid_blob;
869                 struct ldb_dn *dn;
870                 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
871                 if (!tmp_ctx) {
872                         W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
873                 }
874
875                 if (in->value_ctr.values[i].blob == NULL) {
876                         talloc_free(tmp_ctx);
877                         return WERR_FOOBAR;
878                 }
879
880                 if (in->value_ctr.values[i].blob->length == 0) {
881                         talloc_free(tmp_ctx);
882                         return WERR_FOOBAR;
883                 }
884
885                 
886
887                 ndr_err = ndr_pull_struct_blob_all(in->value_ctr.values[i].blob,
888                                                    tmp_ctx, schema->iconv_convenience, &id3,
889                                                    (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3);
890                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
891                         NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
892                         talloc_free(tmp_ctx);
893                         return ntstatus_to_werror(status);
894                 }
895
896                 dn = ldb_dn_new(tmp_ctx, ldb, id3.dn);
897                 if (!dn) {
898                         talloc_free(tmp_ctx);
899                         /* If this fails, it must be out of memory, as it does not do much parsing */
900                         W_ERROR_HAVE_NO_MEMORY(dn);
901                 }
902
903                 ndr_err = ndr_push_struct_blob(&guid_blob, tmp_ctx, schema->iconv_convenience, &id3.guid,
904                                                (ndr_push_flags_fn_t)ndr_push_GUID);
905                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
906                         NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
907                         talloc_free(tmp_ctx);
908                         return ntstatus_to_werror(status);
909                 }
910
911                 ret = ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
912                 if (ret != LDB_SUCCESS) {
913                         talloc_free(tmp_ctx);
914                         return WERR_FOOBAR;
915                 }
916
917                 talloc_free(guid_blob.data);
918
919                 if (id3.__ndr_size_sid) {
920                         DATA_BLOB sid_blob;
921                         ndr_err = ndr_push_struct_blob(&sid_blob, tmp_ctx, schema->iconv_convenience, &id3.sid,
922                                                        (ndr_push_flags_fn_t)ndr_push_dom_sid);
923                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
924                                 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
925                                 talloc_free(tmp_ctx);
926                                 return ntstatus_to_werror(status);
927                         }
928
929                         ret = ldb_dn_set_extended_component(dn, "SID", &sid_blob);
930                         if (ret != LDB_SUCCESS) {
931                                 talloc_free(tmp_ctx);
932                                 return WERR_FOOBAR;
933                         }
934                 }
935
936                 out->values[i] = data_blob_string_const(ldb_dn_get_extended_linearized(out->values, dn, 1));
937                 talloc_free(tmp_ctx);
938         }
939
940         return WERR_OK;
941 }
942
943 static WERROR dsdb_syntax_DN_ldb_to_drsuapi(struct ldb_context *ldb, 
944                                             const struct dsdb_schema *schema,
945                                             const struct dsdb_attribute *attr,
946                                             const struct ldb_message_element *in,
947                                             TALLOC_CTX *mem_ctx,
948                                             struct drsuapi_DsReplicaAttribute *out)
949 {
950         uint32_t i;
951         DATA_BLOB *blobs;
952
953         if (attr->attributeID_id == 0xFFFFFFFF) {
954                 return WERR_FOOBAR;
955         }
956
957         out->attid                      = attr->attributeID_id;
958         out->value_ctr.num_values       = in->num_values;
959         out->value_ctr.values           = talloc_array(mem_ctx,
960                                                        struct drsuapi_DsAttributeValue,
961                                                        in->num_values);
962         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
963
964         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
965         W_ERROR_HAVE_NO_MEMORY(blobs);
966
967         for (i=0; i < in->num_values; i++) {
968                 struct drsuapi_DsReplicaObjectIdentifier3 id3;
969                 enum ndr_err_code ndr_err;
970                 const DATA_BLOB *guid_blob, *sid_blob;
971                 struct ldb_dn *dn;
972                 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
973                 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
974
975                 out->value_ctr.values[i].blob   = &blobs[i];
976
977                 dn = ldb_dn_from_ldb_val(tmp_ctx, ldb, &in->values[i]);
978
979                 W_ERROR_HAVE_NO_MEMORY(dn);
980
981                 guid_blob = ldb_dn_get_extended_component(dn, "GUID");
982
983                 ZERO_STRUCT(id3);
984
985                 if (guid_blob) {
986                         ndr_err = ndr_pull_struct_blob_all(guid_blob, 
987                                                            tmp_ctx, schema->iconv_convenience, &id3.guid,
988                                                            (ndr_pull_flags_fn_t)ndr_pull_GUID);
989                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
990                                 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
991                                 talloc_free(tmp_ctx);
992                                 return ntstatus_to_werror(status);
993                         }
994                 }
995
996                 sid_blob = ldb_dn_get_extended_component(dn, "SID");
997                 if (sid_blob) {
998                         
999                         ndr_err = ndr_pull_struct_blob_all(sid_blob, 
1000                                                            tmp_ctx, schema->iconv_convenience, &id3.sid,
1001                                                            (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
1002                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1003                                 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
1004                                 talloc_free(tmp_ctx);
1005                                 return ntstatus_to_werror(status);
1006                         }
1007                 }
1008
1009                 id3.dn = ldb_dn_get_linearized(dn);
1010
1011                 ndr_err = ndr_push_struct_blob(&blobs[i], blobs, schema->iconv_convenience, &id3, (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
1012                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1013                         NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
1014                         talloc_free(tmp_ctx);
1015                         return ntstatus_to_werror(status);
1016                 }
1017                 talloc_free(tmp_ctx);
1018         }
1019
1020         return WERR_OK;
1021 }
1022
1023 static WERROR dsdb_syntax_DN_BINARY_drsuapi_to_ldb(struct ldb_context *ldb, 
1024                                                    const struct dsdb_schema *schema,
1025                                                    const struct dsdb_attribute *attr,
1026                                                    const struct drsuapi_DsReplicaAttribute *in,
1027                                                    TALLOC_CTX *mem_ctx,
1028                                                    struct ldb_message_element *out)
1029 {
1030         uint32_t i;
1031
1032         out->flags      = 0;
1033         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
1034         W_ERROR_HAVE_NO_MEMORY(out->name);
1035
1036         out->num_values = in->value_ctr.num_values;
1037         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
1038         W_ERROR_HAVE_NO_MEMORY(out->values);
1039
1040         for (i=0; i < out->num_values; i++) {
1041                 struct drsuapi_DsReplicaObjectIdentifier3Binary id3b;
1042                 char *binary;
1043                 char *str;
1044                 enum ndr_err_code ndr_err;
1045
1046                 if (in->value_ctr.values[i].blob == NULL) {
1047                         return WERR_FOOBAR;
1048                 }
1049
1050                 if (in->value_ctr.values[i].blob->length == 0) {
1051                         return WERR_FOOBAR;
1052                 }
1053
1054                 ndr_err = ndr_pull_struct_blob_all(in->value_ctr.values[i].blob,
1055                                                    out->values, schema->iconv_convenience, &id3b,
1056                                                    (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3Binary);
1057                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1058                         NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
1059                         return ntstatus_to_werror(status);
1060                 }
1061
1062                 /* TODO: handle id3.guid and id3.sid */
1063                 binary = data_blob_hex_string(out->values, &id3b.binary);
1064                 W_ERROR_HAVE_NO_MEMORY(binary);
1065
1066                 str = talloc_asprintf(out->values, "B:%u:%s:%s",
1067                                       (unsigned int)(id3b.binary.length * 2), /* because of 2 hex chars per byte */
1068                                       binary,
1069                                       id3b.dn);
1070                 W_ERROR_HAVE_NO_MEMORY(str);
1071
1072                 /* TODO: handle id3.guid and id3.sid */
1073                 out->values[i] = data_blob_string_const(str);
1074         }
1075
1076         return WERR_OK;
1077 }
1078
1079 static WERROR dsdb_syntax_DN_BINARY_ldb_to_drsuapi(struct ldb_context *ldb, 
1080                                                    const struct dsdb_schema *schema,
1081                                                    const struct dsdb_attribute *attr,
1082                                                    const struct ldb_message_element *in,
1083                                                    TALLOC_CTX *mem_ctx,
1084                                                    struct drsuapi_DsReplicaAttribute *out)
1085 {
1086         uint32_t i;
1087         DATA_BLOB *blobs;
1088
1089         if (attr->attributeID_id == 0xFFFFFFFF) {
1090                 return WERR_FOOBAR;
1091         }
1092
1093         out->attid                      = attr->attributeID_id;
1094         out->value_ctr.num_values       = in->num_values;
1095         out->value_ctr.values           = talloc_array(mem_ctx,
1096                                                        struct drsuapi_DsAttributeValue,
1097                                                        in->num_values);
1098         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1099
1100         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1101         W_ERROR_HAVE_NO_MEMORY(blobs);
1102
1103         for (i=0; i < in->num_values; i++) {
1104                 struct drsuapi_DsReplicaObjectIdentifier3Binary id3b;
1105                 enum ndr_err_code ndr_err;
1106
1107                 out->value_ctr.values[i].blob   = &blobs[i];
1108
1109                 /* TODO: handle id3b.guid and id3b.sid, id3.binary */
1110                 ZERO_STRUCT(id3b);
1111                 id3b.dn         = (const char *)in->values[i].data;
1112                 id3b.binary     = data_blob(NULL, 0);
1113
1114                 ndr_err = ndr_push_struct_blob(&blobs[i], blobs, schema->iconv_convenience, &id3b,
1115                                                (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3Binary);
1116                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1117                         NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
1118                         return ntstatus_to_werror(status);
1119                 }
1120         }
1121
1122         return WERR_OK;
1123 }
1124
1125 static WERROR dsdb_syntax_PRESENTATION_ADDRESS_drsuapi_to_ldb(struct ldb_context *ldb, 
1126                                                               const struct dsdb_schema *schema,
1127                                                               const struct dsdb_attribute *attr,
1128                                                               const struct drsuapi_DsReplicaAttribute *in,
1129                                                               TALLOC_CTX *mem_ctx,
1130                                                               struct ldb_message_element *out)
1131 {
1132         uint32_t i;
1133
1134         out->flags      = 0;
1135         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
1136         W_ERROR_HAVE_NO_MEMORY(out->name);
1137
1138         out->num_values = in->value_ctr.num_values;
1139         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
1140         W_ERROR_HAVE_NO_MEMORY(out->values);
1141
1142         for (i=0; i < out->num_values; i++) {
1143                 uint32_t len;
1144                 char *str;
1145
1146                 if (in->value_ctr.values[i].blob == NULL) {
1147                         return WERR_FOOBAR;
1148                 }
1149
1150                 if (in->value_ctr.values[i].blob->length < 4) {
1151                         return WERR_FOOBAR;
1152                 }
1153
1154                 len = IVAL(in->value_ctr.values[i].blob->data, 0);
1155
1156                 if (len != in->value_ctr.values[i].blob->length) {
1157                         return WERR_FOOBAR;
1158                 }
1159
1160                 if (!convert_string_talloc_convenience(out->values, schema->iconv_convenience, CH_UTF16, CH_UNIX,
1161                                             in->value_ctr.values[i].blob->data+4,
1162                                             in->value_ctr.values[i].blob->length-4,
1163                                             (void **)&str, NULL, false)) {
1164                         return WERR_FOOBAR;
1165                 }
1166
1167                 out->values[i] = data_blob_string_const(str);
1168         }
1169
1170         return WERR_OK;
1171 }
1172
1173 static WERROR dsdb_syntax_PRESENTATION_ADDRESS_ldb_to_drsuapi(struct ldb_context *ldb, 
1174                                                               const struct dsdb_schema *schema,
1175                                                               const struct dsdb_attribute *attr,
1176                                                               const struct ldb_message_element *in,
1177                                                               TALLOC_CTX *mem_ctx,
1178                                                               struct drsuapi_DsReplicaAttribute *out)
1179 {
1180         uint32_t i;
1181         DATA_BLOB *blobs;
1182
1183         if (attr->attributeID_id == 0xFFFFFFFF) {
1184                 return WERR_FOOBAR;
1185         }
1186
1187         out->attid                      = attr->attributeID_id;
1188         out->value_ctr.num_values       = in->num_values;
1189         out->value_ctr.values           = talloc_array(mem_ctx,
1190                                                        struct drsuapi_DsAttributeValue,
1191                                                        in->num_values);
1192         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1193
1194         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1195         W_ERROR_HAVE_NO_MEMORY(blobs);
1196
1197         for (i=0; i < in->num_values; i++) {
1198                 uint8_t *data;
1199                 size_t ret;
1200
1201                 out->value_ctr.values[i].blob   = &blobs[i];
1202
1203                 if (!convert_string_talloc_convenience(blobs, schema->iconv_convenience, CH_UNIX, CH_UTF16,
1204                                             in->values[i].data,
1205                                             in->values[i].length,
1206                                             (void **)&data, &ret, false)) {
1207                         return WERR_FOOBAR;
1208                 }
1209
1210                 blobs[i] = data_blob_talloc(blobs, NULL, 4 + ret);
1211                 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
1212
1213                 SIVAL(blobs[i].data, 0, 4 + ret);
1214
1215                 if (ret > 0) {
1216                         memcpy(blobs[i].data + 4, data, ret);
1217                         talloc_free(data);
1218                 }
1219         }
1220
1221         return WERR_OK;
1222 }
1223
1224 #define OMOBJECTCLASS(val) { .length = sizeof(val) - 1, .data = discard_const_p(uint8_t, val) }
1225
1226 static const struct dsdb_syntax dsdb_syntaxes[] = {
1227         {
1228                 .name                   = "Boolean",
1229                 .ldap_oid               = LDB_SYNTAX_BOOLEAN,
1230                 .oMSyntax               = 1,
1231                 .attributeSyntax_oid    = "2.5.5.8",
1232                 .drsuapi_to_ldb         = dsdb_syntax_BOOL_drsuapi_to_ldb,
1233                 .ldb_to_drsuapi         = dsdb_syntax_BOOL_ldb_to_drsuapi,
1234                 .equality               = "booleanMatch",
1235                 .comment                = "Boolean" 
1236         },{
1237                 .name                   = "Integer",
1238                 .ldap_oid               = LDB_SYNTAX_INTEGER,
1239                 .oMSyntax               = 2,
1240                 .attributeSyntax_oid    = "2.5.5.9",
1241                 .drsuapi_to_ldb         = dsdb_syntax_INT32_drsuapi_to_ldb,
1242                 .ldb_to_drsuapi         = dsdb_syntax_INT32_ldb_to_drsuapi,
1243                 .equality               = "integerMatch",
1244                 .comment                = "Integer",
1245                 .ldb_syntax             = LDB_SYNTAX_SAMBA_INT32
1246         },{
1247                 .name                   = "String(Octet)",
1248                 .ldap_oid               = LDB_SYNTAX_OCTET_STRING,
1249                 .oMSyntax               = 4,
1250                 .attributeSyntax_oid    = "2.5.5.10",
1251                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
1252                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
1253                 .equality               = "octetStringMatch",
1254                 .comment                = "Octet String",
1255         },{
1256                 .name                   = "String(Sid)",
1257                 .ldap_oid               = LDB_SYNTAX_OCTET_STRING,
1258                 .oMSyntax               = 4,
1259                 .attributeSyntax_oid    = "2.5.5.17",
1260                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
1261                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
1262                 .equality               = "octetStringMatch",
1263                 .comment                = "Octet String - Security Identifier (SID)",
1264                 .ldb_syntax             = LDB_SYNTAX_SAMBA_SID
1265         },{
1266                 .name                   = "String(Object-Identifier)",
1267                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.38",
1268                 .oMSyntax               = 6,
1269                 .attributeSyntax_oid    = "2.5.5.2",
1270                 .drsuapi_to_ldb         = dsdb_syntax_OID_drsuapi_to_ldb,
1271                 .ldb_to_drsuapi         = dsdb_syntax_OID_ldb_to_drsuapi,
1272                 .equality               = "caseIgnoreMatch", /* Would use "objectIdentifierMatch" but most are ldap attribute/class names */
1273                 .comment                = "OID String",
1274                 .ldb_syntax             = LDB_SYNTAX_DIRECTORY_STRING
1275         },{
1276                 .name                   = "Enumeration",
1277                 .ldap_oid               = LDB_SYNTAX_INTEGER,
1278                 .oMSyntax               = 10,
1279                 .attributeSyntax_oid    = "2.5.5.9",
1280                 .drsuapi_to_ldb         = dsdb_syntax_INT32_drsuapi_to_ldb,
1281                 .ldb_to_drsuapi         = dsdb_syntax_INT32_ldb_to_drsuapi,
1282                 .ldb_syntax             = LDB_SYNTAX_SAMBA_INT32
1283         },{
1284         /* not used in w2k3 forest */
1285                 .name                   = "String(Numeric)",
1286                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.36",
1287                 .oMSyntax               = 18,
1288                 .attributeSyntax_oid    = "2.5.5.6",
1289                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
1290                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
1291                 .equality               = "numericStringMatch",
1292                 .substring              = "numericStringSubstringsMatch",
1293                 .comment                = "Numeric String",
1294                 .ldb_syntax             = LDB_SYNTAX_DIRECTORY_STRING,
1295         },{
1296                 .name                   = "String(Printable)",
1297                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.44",
1298                 .oMSyntax               = 19,
1299                 .attributeSyntax_oid    = "2.5.5.5",
1300                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
1301                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
1302                 .ldb_syntax             = LDB_SYNTAX_OCTET_STRING,
1303         },{
1304                 .name                   = "String(Teletex)",
1305                 .ldap_oid               = "1.2.840.113556.1.4.905",
1306                 .oMSyntax               = 20,
1307                 .attributeSyntax_oid    = "2.5.5.4",
1308                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
1309                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
1310                 .equality               = "caseIgnoreMatch",
1311                 .substring              = "caseIgnoreSubstringsMatch",
1312                 .comment                = "Case Insensitive String",
1313                 .ldb_syntax             = LDB_SYNTAX_DIRECTORY_STRING,
1314         },{
1315                 .name                   = "String(IA5)",
1316                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.26",
1317                 .oMSyntax               = 22,
1318                 .attributeSyntax_oid    = "2.5.5.5",
1319                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
1320                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
1321                 .equality               = "caseExactIA5Match",
1322                 .comment                = "Printable String",
1323                 .ldb_syntax             = LDB_SYNTAX_OCTET_STRING,
1324         },{
1325                 .name                   = "String(UTC-Time)",
1326                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.53",
1327                 .oMSyntax               = 23,
1328                 .attributeSyntax_oid    = "2.5.5.11",
1329                 .drsuapi_to_ldb         = dsdb_syntax_NTTIME_UTC_drsuapi_to_ldb,
1330                 .ldb_to_drsuapi         = dsdb_syntax_NTTIME_UTC_ldb_to_drsuapi,
1331                 .equality               = "generalizedTimeMatch",
1332                 .comment                = "UTC Time",
1333         },{
1334                 .name                   = "String(Generalized-Time)",
1335                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.24",
1336                 .oMSyntax               = 24,
1337                 .attributeSyntax_oid    = "2.5.5.11",
1338                 .drsuapi_to_ldb         = dsdb_syntax_NTTIME_drsuapi_to_ldb,
1339                 .ldb_to_drsuapi         = dsdb_syntax_NTTIME_ldb_to_drsuapi,
1340                 .equality               = "generalizedTimeMatch",
1341                 .comment                = "Generalized Time",
1342                 .ldb_syntax             = LDB_SYNTAX_UTC_TIME,
1343         },{
1344         /* not used in w2k3 schema */
1345                 .name                   = "String(Case Sensitive)",
1346                 .ldap_oid               = "1.2.840.113556.1.4.1362",
1347                 .oMSyntax               = 27,
1348                 .attributeSyntax_oid    = "2.5.5.3",
1349                 .drsuapi_to_ldb         = dsdb_syntax_FOOBAR_drsuapi_to_ldb,
1350                 .ldb_to_drsuapi         = dsdb_syntax_FOOBAR_ldb_to_drsuapi,
1351         },{
1352                 .name                   = "String(Unicode)",
1353                 .ldap_oid               = LDB_SYNTAX_DIRECTORY_STRING,
1354                 .oMSyntax               = 64,
1355                 .attributeSyntax_oid    = "2.5.5.12",
1356                 .drsuapi_to_ldb         = dsdb_syntax_UNICODE_drsuapi_to_ldb,
1357                 .ldb_to_drsuapi         = dsdb_syntax_UNICODE_ldb_to_drsuapi,
1358                 .equality               = "caseIgnoreMatch",
1359                 .substring              = "caseIgnoreSubstringsMatch",
1360                 .comment                = "Directory String",
1361         },{
1362                 .name                   = "Interval/LargeInteger",
1363                 .ldap_oid               = "1.2.840.113556.1.4.906",
1364                 .oMSyntax               = 65,
1365                 .attributeSyntax_oid    = "2.5.5.16",
1366                 .drsuapi_to_ldb         = dsdb_syntax_INT64_drsuapi_to_ldb,
1367                 .ldb_to_drsuapi         = dsdb_syntax_INT64_ldb_to_drsuapi,
1368                 .equality               = "integerMatch",
1369                 .comment                = "Large Integer",
1370                 .ldb_syntax             = LDB_SYNTAX_INTEGER,
1371         },{
1372                 .name                   = "String(NT-Sec-Desc)",
1373                 .ldap_oid               = LDB_SYNTAX_SAMBA_SECURITY_DESCRIPTOR,
1374                 .oMSyntax               = 66,
1375                 .attributeSyntax_oid    = "2.5.5.15",
1376                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
1377                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
1378         },{
1379                 .name                   = "Object(DS-DN)",
1380                 .ldap_oid               = LDB_SYNTAX_DN,
1381                 .oMSyntax               = 127,
1382                 .oMObjectClass          = OMOBJECTCLASS("\x2b\x0c\x02\x87\x73\x1c\x00\x85\x4a"),
1383                 .attributeSyntax_oid    = "2.5.5.1",
1384                 .drsuapi_to_ldb         = dsdb_syntax_DN_drsuapi_to_ldb,
1385                 .ldb_to_drsuapi         = dsdb_syntax_DN_ldb_to_drsuapi,
1386                 .equality               = "distinguishedNameMatch",
1387                 .comment                = "Object(DS-DN) == a DN",
1388         },{
1389                 .name                   = "Object(DN-Binary)",
1390                 .ldap_oid               = "1.2.840.113556.1.4.903",
1391                 .oMSyntax               = 127,
1392                 .oMObjectClass          = OMOBJECTCLASS("\x2a\x86\x48\x86\xf7\x14\x01\x01\x01\x0b"),
1393                 .attributeSyntax_oid    = "2.5.5.7",
1394                 .drsuapi_to_ldb         = dsdb_syntax_DN_BINARY_drsuapi_to_ldb,
1395                 .ldb_to_drsuapi         = dsdb_syntax_DN_BINARY_ldb_to_drsuapi,
1396                 .equality               = "octetStringMatch",
1397                 .comment                = "OctetString: Binary+DN",
1398                 .ldb_syntax             = LDB_SYNTAX_OCTET_STRING,
1399         },{
1400         /* not used in w2k3 schema */
1401                 .name                   = "Object(OR-Name)",
1402                 .ldap_oid               = "1.2.840.113556.1.4.1221",
1403                 .oMSyntax               = 127,
1404                 .oMObjectClass          = OMOBJECTCLASS("\x56\x06\x01\x02\x05\x0b\x1D"),
1405                 .attributeSyntax_oid    = "2.5.5.7",
1406                 .drsuapi_to_ldb         = dsdb_syntax_FOOBAR_drsuapi_to_ldb,
1407                 .ldb_to_drsuapi         = dsdb_syntax_FOOBAR_ldb_to_drsuapi,
1408         },{
1409         /* 
1410          * TODO: verify if DATA_BLOB is correct here...!
1411          *
1412          *       repsFrom and repsTo are the only attributes using
1413          *       this attribute syntax, but they're not replicated... 
1414          */
1415                 .name                   = "Object(Replica-Link)",
1416                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.40",
1417                 .oMSyntax               = 127,
1418                 .oMObjectClass          = OMOBJECTCLASS("\x2a\x86\x48\x86\xf7\x14\x01\x01\x01\x06"),
1419                 .attributeSyntax_oid    = "2.5.5.10",
1420                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
1421                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
1422         },{
1423                 .name                   = "Object(Presentation-Address)",
1424                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.43",
1425                 .oMSyntax               = 127,
1426                 .oMObjectClass          = OMOBJECTCLASS("\x2b\x0c\x02\x87\x73\x1c\x00\x85\x5c"),
1427                 .attributeSyntax_oid    = "2.5.5.13",
1428                 .drsuapi_to_ldb         = dsdb_syntax_PRESENTATION_ADDRESS_drsuapi_to_ldb,
1429                 .ldb_to_drsuapi         = dsdb_syntax_PRESENTATION_ADDRESS_ldb_to_drsuapi,
1430                 .comment                = "Presentation Address",
1431                 .ldb_syntax             = LDB_SYNTAX_DIRECTORY_STRING,
1432         },{
1433         /* not used in w2k3 schema */
1434                 .name                   = "Object(Access-Point)",
1435                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.2",
1436                 .oMSyntax               = 127,
1437                 .oMObjectClass          = OMOBJECTCLASS("\x2b\x0c\x02\x87\x73\x1c\x00\x85\x3e"),
1438                 .attributeSyntax_oid    = "2.5.5.14",
1439                 .drsuapi_to_ldb         = dsdb_syntax_FOOBAR_drsuapi_to_ldb,
1440                 .ldb_to_drsuapi         = dsdb_syntax_FOOBAR_ldb_to_drsuapi,
1441                 .ldb_syntax             = LDB_SYNTAX_DIRECTORY_STRING,
1442         },{
1443         /* not used in w2k3 schema */
1444                 .name                   = "Object(DN-String)",
1445                 .ldap_oid               = "1.2.840.113556.1.4.904",
1446                 .oMSyntax               = 127,
1447                 .oMObjectClass          = OMOBJECTCLASS("\x2a\x86\x48\x86\xf7\x14\x01\x01\x01\x0c"),
1448                 .attributeSyntax_oid    = "2.5.5.14",
1449                 .drsuapi_to_ldb         = dsdb_syntax_DN_BINARY_drsuapi_to_ldb,
1450                 .ldb_to_drsuapi         = dsdb_syntax_DN_BINARY_ldb_to_drsuapi,
1451                 .equality               = "octetStringMatch",
1452                 .comment                = "OctetString: String+DN",
1453                 .ldb_syntax             = LDB_SYNTAX_OCTET_STRING,
1454         }
1455 };
1456
1457 const struct dsdb_syntax *find_syntax_map_by_ad_oid(const char *ad_oid) 
1458 {
1459         int i;
1460         for (i=0; dsdb_syntaxes[i].ldap_oid; i++) {
1461                 if (strcasecmp(ad_oid, dsdb_syntaxes[i].attributeSyntax_oid) == 0) {
1462                         return &dsdb_syntaxes[i];
1463                 }
1464         }
1465         return NULL;
1466 }
1467
1468 const struct dsdb_syntax *find_syntax_map_by_ad_syntax(int oMSyntax) 
1469 {
1470         int i;
1471         for (i=0; dsdb_syntaxes[i].ldap_oid; i++) {
1472                 if (oMSyntax == dsdb_syntaxes[i].oMSyntax) {
1473                         return &dsdb_syntaxes[i];
1474                 }
1475         }
1476         return NULL;
1477 }
1478
1479 const struct dsdb_syntax *find_syntax_map_by_standard_oid(const char *standard_oid) 
1480 {
1481         int i;
1482         for (i=0; dsdb_syntaxes[i].ldap_oid; i++) {
1483                 if (strcasecmp(standard_oid, dsdb_syntaxes[i].ldap_oid) == 0) {
1484                         return &dsdb_syntaxes[i];
1485                 }
1486         }
1487         return NULL;
1488 }
1489 const struct dsdb_syntax *dsdb_syntax_for_attribute(const struct dsdb_attribute *attr)
1490 {
1491         uint32_t i;
1492
1493         for (i=0; i < ARRAY_SIZE(dsdb_syntaxes); i++) {
1494                 if (attr->oMSyntax != dsdb_syntaxes[i].oMSyntax) continue;
1495
1496                 if (attr->oMObjectClass.length != dsdb_syntaxes[i].oMObjectClass.length) continue;
1497
1498                 if (attr->oMObjectClass.length) {
1499                         int ret;
1500                         ret = memcmp(attr->oMObjectClass.data,
1501                                      dsdb_syntaxes[i].oMObjectClass.data,
1502                                      attr->oMObjectClass.length);
1503                         if (ret != 0) continue;
1504                 }
1505
1506                 if (strcmp(attr->attributeSyntax_oid, dsdb_syntaxes[i].attributeSyntax_oid) != 0) continue;
1507
1508                 return &dsdb_syntaxes[i];
1509         }
1510
1511         return NULL;
1512 }
1513
1514 WERROR dsdb_attribute_drsuapi_to_ldb(struct ldb_context *ldb, 
1515                                      const struct dsdb_schema *schema,
1516                                      const struct drsuapi_DsReplicaAttribute *in,
1517                                      TALLOC_CTX *mem_ctx,
1518                                      struct ldb_message_element *out)
1519 {
1520         const struct dsdb_attribute *sa;
1521
1522         sa = dsdb_attribute_by_attributeID_id(schema, in->attid);
1523         if (!sa) {
1524                 return WERR_FOOBAR;
1525         }
1526
1527         return sa->syntax->drsuapi_to_ldb(ldb, schema, sa, in, mem_ctx, out);
1528 }
1529
1530 WERROR dsdb_attribute_ldb_to_drsuapi(struct ldb_context *ldb, 
1531                                      const struct dsdb_schema *schema,
1532                                      const struct ldb_message_element *in,
1533                                      TALLOC_CTX *mem_ctx,
1534                                      struct drsuapi_DsReplicaAttribute *out)
1535 {
1536         const struct dsdb_attribute *sa;
1537
1538         sa = dsdb_attribute_by_lDAPDisplayName(schema, in->name);
1539         if (!sa) {
1540                 return WERR_FOOBAR;
1541         }
1542
1543         return sa->syntax->ldb_to_drsuapi(ldb, schema, sa, in, mem_ctx, out);
1544 }