Finish removal of iconv_convenience in public API's.
[sfrench/samba-autobuild/.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         unsigned int 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_FOOBAR_validate_ldb(struct ldb_context *ldb,
79                                               const struct dsdb_schema *schema,
80                                               const struct dsdb_attribute *attr,
81                                               const struct ldb_message_element *in)
82 {
83         return WERR_FOOBAR;
84 }
85
86 static WERROR dsdb_syntax_BOOL_drsuapi_to_ldb(struct ldb_context *ldb, 
87                                               const struct dsdb_schema *schema,
88                                               const struct dsdb_attribute *attr,
89                                               const struct drsuapi_DsReplicaAttribute *in,
90                                               TALLOC_CTX *mem_ctx,
91                                               struct ldb_message_element *out)
92 {
93         unsigned int i;
94
95         out->flags      = 0;
96         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
97         W_ERROR_HAVE_NO_MEMORY(out->name);
98
99         out->num_values = in->value_ctr.num_values;
100         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
101         W_ERROR_HAVE_NO_MEMORY(out->values);
102
103         for (i=0; i < out->num_values; i++) {
104                 uint32_t v;
105                 char *str;
106
107                 if (in->value_ctr.values[i].blob == NULL) {
108                         return WERR_FOOBAR;
109                 }
110
111                 if (in->value_ctr.values[i].blob->length != 4) {
112                         return WERR_FOOBAR;
113                 }
114
115                 v = IVAL(in->value_ctr.values[i].blob->data, 0);
116
117                 if (v != 0) {
118                         str = talloc_strdup(out->values, "TRUE");
119                         W_ERROR_HAVE_NO_MEMORY(str);
120                 } else {
121                         str = talloc_strdup(out->values, "FALSE");
122                         W_ERROR_HAVE_NO_MEMORY(str);
123                 }
124
125                 out->values[i] = data_blob_string_const(str);
126         }
127
128         return WERR_OK;
129 }
130
131 static WERROR dsdb_syntax_BOOL_ldb_to_drsuapi(struct ldb_context *ldb, 
132                                               const struct dsdb_schema *schema,
133                                               const struct dsdb_attribute *attr,
134                                               const struct ldb_message_element *in,
135                                               TALLOC_CTX *mem_ctx,
136                                               struct drsuapi_DsReplicaAttribute *out)
137 {
138         unsigned int i;
139         DATA_BLOB *blobs;
140
141         if (attr->attributeID_id == 0xFFFFFFFF) {
142                 return WERR_FOOBAR;
143         }
144
145         out->attid                      = attr->attributeID_id;
146         out->value_ctr.num_values       = in->num_values;
147         out->value_ctr.values           = talloc_array(mem_ctx,
148                                                        struct drsuapi_DsAttributeValue,
149                                                        in->num_values);
150         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
151
152         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
153         W_ERROR_HAVE_NO_MEMORY(blobs);
154
155         for (i=0; i < in->num_values; i++) {
156                 out->value_ctr.values[i].blob   = &blobs[i];
157
158                 blobs[i] = data_blob_talloc(blobs, NULL, 4);
159                 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
160
161                 if (strcmp("TRUE", (const char *)in->values[i].data) == 0) {
162                         SIVAL(blobs[i].data, 0, 0x00000001);
163                 } else if (strcmp("FALSE", (const char *)in->values[i].data) == 0) {
164                         SIVAL(blobs[i].data, 0, 0x00000000);
165                 } else {
166                         return WERR_FOOBAR;
167                 }
168         }
169
170         return WERR_OK;
171 }
172
173 static WERROR dsdb_syntax_BOOL_validate_ldb(struct ldb_context *ldb,
174                                             const struct dsdb_schema *schema,
175                                             const struct dsdb_attribute *attr,
176                                             const struct ldb_message_element *in)
177 {
178         unsigned int i;
179
180         if (attr->attributeID_id == 0xFFFFFFFF) {
181                 return WERR_FOOBAR;
182         }
183
184         for (i=0; i < in->num_values; i++) {
185                 int t, f;
186
187                 t = strncmp("TRUE",
188                             (const char *)in->values[i].data,
189                             in->values[i].length);
190                 f = strncmp("FALSE",
191                             (const char *)in->values[i].data,
192                             in->values[i].length);
193
194                 if (t != 0 && f != 0) {
195                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
196                 }
197         }
198
199         return WERR_OK;
200 }
201
202 static WERROR dsdb_syntax_INT32_drsuapi_to_ldb(struct ldb_context *ldb, 
203                                                const struct dsdb_schema *schema,
204                                                const struct dsdb_attribute *attr,
205                                                const struct drsuapi_DsReplicaAttribute *in,
206                                                TALLOC_CTX *mem_ctx,
207                                                struct ldb_message_element *out)
208 {
209         unsigned int i;
210
211         out->flags      = 0;
212         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
213         W_ERROR_HAVE_NO_MEMORY(out->name);
214
215         out->num_values = in->value_ctr.num_values;
216         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
217         W_ERROR_HAVE_NO_MEMORY(out->values);
218
219         for (i=0; i < out->num_values; i++) {
220                 int32_t v;
221                 char *str;
222
223                 if (in->value_ctr.values[i].blob == NULL) {
224                         return WERR_FOOBAR;
225                 }
226
227                 if (in->value_ctr.values[i].blob->length != 4) {
228                         return WERR_FOOBAR;
229                 }
230
231                 v = IVALS(in->value_ctr.values[i].blob->data, 0);
232
233                 str = talloc_asprintf(out->values, "%d", v);
234                 W_ERROR_HAVE_NO_MEMORY(str);
235
236                 out->values[i] = data_blob_string_const(str);
237         }
238
239         return WERR_OK;
240 }
241
242 static WERROR dsdb_syntax_INT32_ldb_to_drsuapi(struct ldb_context *ldb, 
243                                                const struct dsdb_schema *schema,
244                                                const struct dsdb_attribute *attr,
245                                                const struct ldb_message_element *in,
246                                                TALLOC_CTX *mem_ctx,
247                                                struct drsuapi_DsReplicaAttribute *out)
248 {
249         unsigned int i;
250         DATA_BLOB *blobs;
251
252         if (attr->attributeID_id == 0xFFFFFFFF) {
253                 return WERR_FOOBAR;
254         }
255
256         out->attid                      = attr->attributeID_id;
257         out->value_ctr.num_values       = in->num_values;
258         out->value_ctr.values           = talloc_array(mem_ctx,
259                                                        struct drsuapi_DsAttributeValue,
260                                                        in->num_values);
261         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
262
263         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
264         W_ERROR_HAVE_NO_MEMORY(blobs);
265
266         for (i=0; i < in->num_values; i++) {
267                 int32_t v;
268
269                 out->value_ctr.values[i].blob   = &blobs[i];
270
271                 blobs[i] = data_blob_talloc(blobs, NULL, 4);
272                 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
273
274                 /* We've to use "strtoll" here to have the intended overflows.
275                  * Otherwise we may get "LONG_MAX" and the conversion is wrong. */
276                 v = (int32_t) strtoll((char *)in->values[i].data, NULL, 0);
277
278                 SIVALS(blobs[i].data, 0, v);
279         }
280
281         return WERR_OK;
282 }
283
284 static WERROR dsdb_syntax_INT32_validate_ldb(struct ldb_context *ldb,
285                                              const struct dsdb_schema *schema,
286                                              const struct dsdb_attribute *attr,
287                                              const struct ldb_message_element *in)
288 {
289         unsigned int i;
290
291         if (attr->attributeID_id == 0xFFFFFFFF) {
292                 return WERR_FOOBAR;
293         }
294
295         for (i=0; i < in->num_values; i++) {
296                 long v;
297                 char buf[sizeof("-2147483648")];
298                 char *end = NULL;
299
300                 ZERO_STRUCT(buf);
301                 if (in->values[i].length >= sizeof(buf)) {
302                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
303                 }
304
305                 memcpy(buf, in->values[i].data, in->values[i].length);
306                 errno = 0;
307                 v = strtol(buf, &end, 10);
308                 if (errno != 0) {
309                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
310                 }
311                 if (end && end[0] != '\0') {
312                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
313                 }
314
315                 if (attr->rangeLower) {
316                         if ((int32_t)v < (int32_t)*attr->rangeLower) {
317                                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
318                         }
319                 }
320
321                 if (attr->rangeUpper) {
322                         if ((int32_t)v > (int32_t)*attr->rangeUpper) {
323                                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
324                         }
325                 }
326         }
327
328         return WERR_OK;
329 }
330
331 static WERROR dsdb_syntax_INT64_drsuapi_to_ldb(struct ldb_context *ldb, 
332                                                const struct dsdb_schema *schema,
333                                                const struct dsdb_attribute *attr,
334                                                const struct drsuapi_DsReplicaAttribute *in,
335                                                TALLOC_CTX *mem_ctx,
336                                                struct ldb_message_element *out)
337 {
338         unsigned int i;
339
340         out->flags      = 0;
341         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
342         W_ERROR_HAVE_NO_MEMORY(out->name);
343
344         out->num_values = in->value_ctr.num_values;
345         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
346         W_ERROR_HAVE_NO_MEMORY(out->values);
347
348         for (i=0; i < out->num_values; i++) {
349                 int64_t v;
350                 char *str;
351
352                 if (in->value_ctr.values[i].blob == NULL) {
353                         return WERR_FOOBAR;
354                 }
355
356                 if (in->value_ctr.values[i].blob->length != 8) {
357                         return WERR_FOOBAR;
358                 }
359
360                 v = BVALS(in->value_ctr.values[i].blob->data, 0);
361
362                 str = talloc_asprintf(out->values, "%lld", (long long int)v);
363                 W_ERROR_HAVE_NO_MEMORY(str);
364
365                 out->values[i] = data_blob_string_const(str);
366         }
367
368         return WERR_OK;
369 }
370
371 static WERROR dsdb_syntax_INT64_ldb_to_drsuapi(struct ldb_context *ldb, 
372                                                const struct dsdb_schema *schema,
373                                                const struct dsdb_attribute *attr,
374                                                const struct ldb_message_element *in,
375                                                TALLOC_CTX *mem_ctx,
376                                                struct drsuapi_DsReplicaAttribute *out)
377 {
378         unsigned int i;
379         DATA_BLOB *blobs;
380
381         if (attr->attributeID_id == 0xFFFFFFFF) {
382                 return WERR_FOOBAR;
383         }
384
385         out->attid                      = attr->attributeID_id;
386         out->value_ctr.num_values       = in->num_values;
387         out->value_ctr.values           = talloc_array(mem_ctx,
388                                                        struct drsuapi_DsAttributeValue,
389                                                        in->num_values);
390         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
391
392         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
393         W_ERROR_HAVE_NO_MEMORY(blobs);
394
395         for (i=0; i < in->num_values; i++) {
396                 int64_t v;
397
398                 out->value_ctr.values[i].blob   = &blobs[i];
399
400                 blobs[i] = data_blob_talloc(blobs, NULL, 8);
401                 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
402
403                 v = strtoll((const char *)in->values[i].data, NULL, 10);
404
405                 SBVALS(blobs[i].data, 0, v);
406         }
407
408         return WERR_OK;
409 }
410
411 static WERROR dsdb_syntax_INT64_validate_ldb(struct ldb_context *ldb,
412                                              const struct dsdb_schema *schema,
413                                              const struct dsdb_attribute *attr,
414                                              const struct ldb_message_element *in)
415 {
416         unsigned int i;
417
418         if (attr->attributeID_id == 0xFFFFFFFF) {
419                 return WERR_FOOBAR;
420         }
421
422         for (i=0; i < in->num_values; i++) {
423                 long long v;
424                 char buf[sizeof("-9223372036854775808")];
425                 char *end = NULL;
426
427                 ZERO_STRUCT(buf);
428                 if (in->values[i].length >= sizeof(buf)) {
429                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
430                 }
431                 memcpy(buf, in->values[i].data, in->values[i].length);
432
433                 errno = 0;
434                 v = strtoll(buf, &end, 10);
435                 if (errno != 0) {
436                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
437                 }
438                 if (end && end[0] != '\0') {
439                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
440                 }
441
442                 if (attr->rangeLower) {
443                         if ((int64_t)v < (int64_t)*attr->rangeLower) {
444                                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
445                         }
446                 }
447
448                 if (attr->rangeUpper) {
449                         if ((int64_t)v > (int64_t)*attr->rangeUpper) {
450                                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
451                         }
452                 }
453         }
454
455         return WERR_OK;
456 }
457 static WERROR dsdb_syntax_NTTIME_UTC_drsuapi_to_ldb(struct ldb_context *ldb, 
458                                                     const struct dsdb_schema *schema,
459                                                     const struct dsdb_attribute *attr,
460                                                     const struct drsuapi_DsReplicaAttribute *in,
461                                                     TALLOC_CTX *mem_ctx,
462                                                     struct ldb_message_element *out)
463 {
464         unsigned int i;
465
466         out->flags      = 0;
467         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
468         W_ERROR_HAVE_NO_MEMORY(out->name);
469
470         out->num_values = in->value_ctr.num_values;
471         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
472         W_ERROR_HAVE_NO_MEMORY(out->values);
473
474         for (i=0; i < out->num_values; i++) {
475                 NTTIME v;
476                 time_t t;
477                 char *str;
478
479                 if (in->value_ctr.values[i].blob == NULL) {
480                         return WERR_FOOBAR;
481                 }
482
483                 if (in->value_ctr.values[i].blob->length != 8) {
484                         return WERR_FOOBAR;
485                 }
486
487                 v = BVAL(in->value_ctr.values[i].blob->data, 0);
488                 v *= 10000000;
489                 t = nt_time_to_unix(v);
490
491                 /* 
492                  * NOTE: On a w2k3 server you can set a GeneralizedTime string
493                  *       via LDAP, but you get back an UTCTime string,
494                  *       but via DRSUAPI you get back the NTTIME_1sec value
495                  *       that represents the GeneralizedTime value!
496                  *
497                  *       So if we store the UTCTime string in our ldb
498                  *       we'll loose information!
499                  */
500                 str = ldb_timestring_utc(out->values, t); 
501                 W_ERROR_HAVE_NO_MEMORY(str);
502                 out->values[i] = data_blob_string_const(str);
503         }
504
505         return WERR_OK;
506 }
507
508 static WERROR dsdb_syntax_NTTIME_UTC_ldb_to_drsuapi(struct ldb_context *ldb, 
509                                                     const struct dsdb_schema *schema,
510                                                     const struct dsdb_attribute *attr,
511                                                     const struct ldb_message_element *in,
512                                                     TALLOC_CTX *mem_ctx,
513                                                     struct drsuapi_DsReplicaAttribute *out)
514 {
515         unsigned int i;
516         DATA_BLOB *blobs;
517
518         if (attr->attributeID_id == 0xFFFFFFFF) {
519                 return WERR_FOOBAR;
520         }
521
522         out->attid                      = attr->attributeID_id;
523         out->value_ctr.num_values       = in->num_values;
524         out->value_ctr.values           = talloc_array(mem_ctx,
525                                                        struct drsuapi_DsAttributeValue,
526                                                        in->num_values);
527         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
528
529         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
530         W_ERROR_HAVE_NO_MEMORY(blobs);
531
532         for (i=0; i < in->num_values; i++) {
533                 NTTIME v;
534                 time_t t;
535
536                 out->value_ctr.values[i].blob   = &blobs[i];
537
538                 blobs[i] = data_blob_talloc(blobs, NULL, 8);
539                 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
540
541                 t = ldb_string_utc_to_time((const char *)in->values[i].data);
542                 unix_to_nt_time(&v, t);
543                 v /= 10000000;
544
545                 SBVAL(blobs[i].data, 0, v);
546         }
547
548         return WERR_OK;
549 }
550
551 static WERROR dsdb_syntax_NTTIME_UTC_validate_ldb(struct ldb_context *ldb,
552                                                   const struct dsdb_schema *schema,
553                                                   const struct dsdb_attribute *attr,
554                                                   const struct ldb_message_element *in)
555 {
556         unsigned int i;
557
558         if (attr->attributeID_id == 0xFFFFFFFF) {
559                 return WERR_FOOBAR;
560         }
561
562         for (i=0; i < in->num_values; i++) {
563                 time_t t;
564                 char buf[sizeof("090826075717Z")];
565
566                 ZERO_STRUCT(buf);
567                 if (in->values[i].length >= sizeof(buf)) {
568                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
569                 }
570                 memcpy(buf, in->values[i].data, in->values[i].length);
571
572                 errno = 0;
573                 t = ldb_string_utc_to_time(buf);
574                 if (errno != 0) {
575                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
576                 }
577
578                 if (attr->rangeLower) {
579                         if ((int32_t)t < (int32_t)*attr->rangeLower) {
580                                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
581                         }
582                 }
583
584                 if (attr->rangeUpper) {
585                         if ((int32_t)t > (int32_t)*attr->rangeLower) {
586                                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
587                         }
588                 }
589
590                 /*
591                  * TODO: verify the comment in the
592                  * dsdb_syntax_NTTIME_UTC_drsuapi_to_ldb() function!
593                  */
594         }
595
596         return WERR_OK;
597 }
598
599 static WERROR dsdb_syntax_NTTIME_drsuapi_to_ldb(struct ldb_context *ldb, 
600                                                 const struct dsdb_schema *schema,
601                                                 const struct dsdb_attribute *attr,
602                                                 const struct drsuapi_DsReplicaAttribute *in,
603                                                 TALLOC_CTX *mem_ctx,
604                                                 struct ldb_message_element *out)
605 {
606         unsigned int i;
607
608         out->flags      = 0;
609         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
610         W_ERROR_HAVE_NO_MEMORY(out->name);
611
612         out->num_values = in->value_ctr.num_values;
613         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
614         W_ERROR_HAVE_NO_MEMORY(out->values);
615
616         for (i=0; i < out->num_values; i++) {
617                 NTTIME v;
618                 time_t t;
619                 char *str;
620
621                 if (in->value_ctr.values[i].blob == NULL) {
622                         return WERR_FOOBAR;
623                 }
624
625                 if (in->value_ctr.values[i].blob->length != 8) {
626                         return WERR_FOOBAR;
627                 }
628
629                 v = BVAL(in->value_ctr.values[i].blob->data, 0);
630                 v *= 10000000;
631                 t = nt_time_to_unix(v);
632
633                 str = ldb_timestring(out->values, t); 
634                 W_ERROR_HAVE_NO_MEMORY(str);
635
636                 out->values[i] = data_blob_string_const(str);
637         }
638
639         return WERR_OK;
640 }
641
642 static WERROR dsdb_syntax_NTTIME_ldb_to_drsuapi(struct ldb_context *ldb, 
643                                                 const struct dsdb_schema *schema,
644                                                 const struct dsdb_attribute *attr,
645                                                 const struct ldb_message_element *in,
646                                                 TALLOC_CTX *mem_ctx,
647                                                 struct drsuapi_DsReplicaAttribute *out)
648 {
649         unsigned int i;
650         DATA_BLOB *blobs;
651
652         if (attr->attributeID_id == 0xFFFFFFFF) {
653                 return WERR_FOOBAR;
654         }
655
656         out->attid                      = attr->attributeID_id;
657         out->value_ctr.num_values       = in->num_values;
658         out->value_ctr.values           = talloc_array(mem_ctx,
659                                                        struct drsuapi_DsAttributeValue,
660                                                        in->num_values);
661         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
662
663         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
664         W_ERROR_HAVE_NO_MEMORY(blobs);
665
666         for (i=0; i < in->num_values; i++) {
667                 NTTIME v;
668                 time_t t;
669                 int ret;
670
671                 out->value_ctr.values[i].blob   = &blobs[i];
672
673                 blobs[i] = data_blob_talloc(blobs, NULL, 8);
674                 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
675
676                 ret = ldb_val_to_time(&in->values[i], &t);
677                 if (ret != LDB_SUCCESS) {
678                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
679                 }
680                 unix_to_nt_time(&v, t);
681                 v /= 10000000;
682
683                 SBVAL(blobs[i].data, 0, v);
684         }
685
686         return WERR_OK;
687 }
688
689 static WERROR dsdb_syntax_NTTIME_validate_ldb(struct ldb_context *ldb,
690                                               const struct dsdb_schema *schema,
691                                               const struct dsdb_attribute *attr,
692                                               const struct ldb_message_element *in)
693 {
694         unsigned int i;
695
696         if (attr->attributeID_id == 0xFFFFFFFF) {
697                 return WERR_FOOBAR;
698         }
699
700         for (i=0; i < in->num_values; i++) {
701                 time_t t;
702                 int ret;
703
704                 ret = ldb_val_to_time(&in->values[i], &t);
705                 if (ret != LDB_SUCCESS) {
706                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
707                 }
708
709                 if (attr->rangeLower) {
710                         if ((int32_t)t < (int32_t)*attr->rangeLower) {
711                                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
712                         }
713                 }
714
715                 if (attr->rangeUpper) {
716                         if ((int32_t)t > (int32_t)*attr->rangeLower) {
717                                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
718                         }
719                 }
720         }
721
722         return WERR_OK;
723 }
724
725 static WERROR dsdb_syntax_DATA_BLOB_drsuapi_to_ldb(struct ldb_context *ldb, 
726                                                    const struct dsdb_schema *schema,
727                                                    const struct dsdb_attribute *attr,
728                                                    const struct drsuapi_DsReplicaAttribute *in,
729                                                    TALLOC_CTX *mem_ctx,
730                                                    struct ldb_message_element *out)
731 {
732         unsigned int i;
733
734         out->flags      = 0;
735         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
736         W_ERROR_HAVE_NO_MEMORY(out->name);
737
738         out->num_values = in->value_ctr.num_values;
739         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
740         W_ERROR_HAVE_NO_MEMORY(out->values);
741
742         for (i=0; i < out->num_values; i++) {
743                 if (in->value_ctr.values[i].blob == NULL) {
744                         return WERR_FOOBAR;
745                 }
746
747                 if (in->value_ctr.values[i].blob->length == 0) {
748                         return WERR_FOOBAR;
749                 }
750
751                 out->values[i] = data_blob_dup_talloc(out->values,
752                                                       in->value_ctr.values[i].blob);
753                 W_ERROR_HAVE_NO_MEMORY(out->values[i].data);
754         }
755
756         return WERR_OK;
757 }
758
759 static WERROR dsdb_syntax_DATA_BLOB_ldb_to_drsuapi(struct ldb_context *ldb, 
760                                                    const struct dsdb_schema *schema,
761                                                    const struct dsdb_attribute *attr,
762                                                    const struct ldb_message_element *in,
763                                                    TALLOC_CTX *mem_ctx,
764                                                    struct drsuapi_DsReplicaAttribute *out)
765 {
766         unsigned int i;
767         DATA_BLOB *blobs;
768
769         if (attr->attributeID_id == 0xFFFFFFFF) {
770                 return WERR_FOOBAR;
771         }
772
773         out->attid                      = attr->attributeID_id;
774         out->value_ctr.num_values       = in->num_values;
775         out->value_ctr.values           = talloc_array(mem_ctx,
776                                                        struct drsuapi_DsAttributeValue,
777                                                        in->num_values);
778         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
779
780         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
781         W_ERROR_HAVE_NO_MEMORY(blobs);
782
783         for (i=0; i < in->num_values; i++) {
784                 out->value_ctr.values[i].blob   = &blobs[i];
785
786                 blobs[i] = data_blob_dup_talloc(blobs, &in->values[i]);
787                 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
788         }
789
790         return WERR_OK;
791 }
792
793 static WERROR dsdb_syntax_DATA_BLOB_validate_one_val(struct ldb_context *ldb,
794                                                      const struct dsdb_schema *schema,
795                                                      const struct dsdb_attribute *attr,
796                                                      const struct ldb_val *val)
797 {
798         if (attr->attributeID_id == 0xFFFFFFFF) {
799                 return WERR_FOOBAR;
800         }
801
802         if (attr->rangeLower) {
803                 if ((uint32_t)val->length < (uint32_t)*attr->rangeLower) {
804                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
805                 }
806         }
807
808         if (attr->rangeUpper) {
809                 if ((uint32_t)val->length > (uint32_t)*attr->rangeUpper) {
810                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
811                 }
812         }
813
814         return WERR_OK;
815 }
816
817 static WERROR dsdb_syntax_DATA_BLOB_validate_ldb(struct ldb_context *ldb,
818                                                  const struct dsdb_schema *schema,
819                                                  const struct dsdb_attribute *attr,
820                                                  const struct ldb_message_element *in)
821 {
822         unsigned int i;
823         WERROR status;
824
825         if (attr->attributeID_id == 0xFFFFFFFF) {
826                 return WERR_FOOBAR;
827         }
828
829         for (i=0; i < in->num_values; i++) {
830                 if (in->values[i].length == 0) {
831                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
832                 }
833
834                 status = dsdb_syntax_DATA_BLOB_validate_one_val(ldb,
835                                                                 schema,
836                                                                 attr,
837                                                                 &in->values[i]);
838                 if (!W_ERROR_IS_OK(status)) {
839                         return status;
840                 }
841         }
842
843         return WERR_OK;
844 }
845
846 static WERROR _dsdb_syntax_auto_OID_drsuapi_to_ldb(struct ldb_context *ldb,
847                                                    const struct dsdb_schema *schema,
848                                                    const struct dsdb_attribute *attr,
849                                                    const struct drsuapi_DsReplicaAttribute *in,
850                                                    TALLOC_CTX *mem_ctx,
851                                                    struct ldb_message_element *out)
852 {
853         unsigned int i;
854
855         out->flags      = 0;
856         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
857         W_ERROR_HAVE_NO_MEMORY(out->name);
858
859         out->num_values = in->value_ctr.num_values;
860         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
861         W_ERROR_HAVE_NO_MEMORY(out->values);
862
863         for (i=0; i < out->num_values; i++) {
864                 uint32_t v;
865                 const struct dsdb_class *c;
866                 const struct dsdb_attribute *a;
867                 const char *str = NULL;
868
869                 if (in->value_ctr.values[i].blob == NULL) {
870                         return WERR_FOOBAR;
871                 }
872
873                 if (in->value_ctr.values[i].blob->length != 4) {
874                         return WERR_FOOBAR;
875                 }
876
877                 v = IVAL(in->value_ctr.values[i].blob->data, 0);
878
879                 if ((c = dsdb_class_by_governsID_id(schema, v))) {
880                         str = talloc_strdup(out->values, c->lDAPDisplayName);
881                 } else if ((a = dsdb_attribute_by_attributeID_id(schema, v))) {
882                         str = talloc_strdup(out->values, a->lDAPDisplayName);
883                 } else {
884                         WERROR werr;
885                         werr = dsdb_schema_pfm_oid_from_attid(schema->prefixmap, v, out->values, &str);
886                         W_ERROR_NOT_OK_RETURN(werr);
887                 }
888                 W_ERROR_HAVE_NO_MEMORY(str);
889
890                 /* the values need to be reversed */
891                 out->values[out->num_values - (i + 1)] = data_blob_string_const(str);
892         }
893
894         return WERR_OK;
895 }
896
897 static WERROR _dsdb_syntax_OID_obj_drsuapi_to_ldb(struct ldb_context *ldb, 
898                                                   const struct dsdb_schema *schema,
899                                                   const struct dsdb_attribute *attr,
900                                                   const struct drsuapi_DsReplicaAttribute *in,
901                                                   TALLOC_CTX *mem_ctx,
902                                                   struct ldb_message_element *out)
903 {
904         unsigned int i;
905
906         out->flags      = 0;
907         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
908         W_ERROR_HAVE_NO_MEMORY(out->name);
909
910         out->num_values = in->value_ctr.num_values;
911         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
912         W_ERROR_HAVE_NO_MEMORY(out->values);
913
914         for (i=0; i < out->num_values; i++) {
915                 uint32_t v;
916                 const struct dsdb_class *c;
917                 const char *str;
918
919                 if (in->value_ctr.values[i].blob == NULL) {
920                         return WERR_FOOBAR;
921                 }
922
923                 if (in->value_ctr.values[i].blob->length != 4) {
924                         return WERR_FOOBAR;
925                 }
926
927                 v = IVAL(in->value_ctr.values[i].blob->data, 0);
928
929                 c = dsdb_class_by_governsID_id(schema, v);
930                 if (!c) {
931                         return WERR_FOOBAR;
932                 }
933
934                 str = talloc_strdup(out->values, c->lDAPDisplayName);
935                 W_ERROR_HAVE_NO_MEMORY(str);
936
937                 /* the values need to be reversed */
938                 out->values[out->num_values - (i + 1)] = data_blob_string_const(str);
939         }
940
941         return WERR_OK;
942 }
943
944 static WERROR _dsdb_syntax_OID_attr_drsuapi_to_ldb(struct ldb_context *ldb, 
945                                                    const struct dsdb_schema *schema,
946                                                    const struct dsdb_attribute *attr,
947                                                    const struct drsuapi_DsReplicaAttribute *in,
948                                                    TALLOC_CTX *mem_ctx,
949                                                    struct ldb_message_element *out)
950 {
951         unsigned int i;
952
953         out->flags      = 0;
954         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
955         W_ERROR_HAVE_NO_MEMORY(out->name);
956
957         out->num_values = in->value_ctr.num_values;
958         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
959         W_ERROR_HAVE_NO_MEMORY(out->values);
960
961         for (i=0; i < out->num_values; i++) {
962                 uint32_t v;
963                 const struct dsdb_attribute *a;
964                 const char *str;
965
966                 if (in->value_ctr.values[i].blob == NULL) {
967                         return WERR_FOOBAR;
968                 }
969
970                 if (in->value_ctr.values[i].blob->length != 4) {
971                         return WERR_FOOBAR;
972                 }
973
974                 v = IVAL(in->value_ctr.values[i].blob->data, 0);
975
976                 a = dsdb_attribute_by_attributeID_id(schema, v);
977                 if (!a) {
978                         return WERR_FOOBAR;
979                 }
980
981                 str = talloc_strdup(out->values, a->lDAPDisplayName);
982                 W_ERROR_HAVE_NO_MEMORY(str);
983
984                 /* the values need to be reversed */
985                 out->values[out->num_values - (i + 1)] = data_blob_string_const(str);
986         }
987
988         return WERR_OK;
989 }
990
991 static WERROR _dsdb_syntax_OID_oid_drsuapi_to_ldb(struct ldb_context *ldb, 
992                                                   const struct dsdb_schema *schema,
993                                                   const struct dsdb_attribute *attr,
994                                                   const struct drsuapi_DsReplicaAttribute *in,
995                                                   TALLOC_CTX *mem_ctx,
996                                                   struct ldb_message_element *out)
997 {
998         unsigned int i;
999
1000         out->flags      = 0;
1001         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
1002         W_ERROR_HAVE_NO_MEMORY(out->name);
1003
1004         out->num_values = in->value_ctr.num_values;
1005         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
1006         W_ERROR_HAVE_NO_MEMORY(out->values);
1007
1008         for (i=0; i < out->num_values; i++) {
1009                 uint32_t attid;
1010                 WERROR status;
1011                 const char *oid;
1012
1013                 if (in->value_ctr.values[i].blob == NULL) {
1014                         return WERR_FOOBAR;
1015                 }
1016
1017                 if (in->value_ctr.values[i].blob->length != 4) {
1018                         return WERR_FOOBAR;
1019                 }
1020
1021                 attid = IVAL(in->value_ctr.values[i].blob->data, 0);
1022
1023                 status = dsdb_schema_pfm_oid_from_attid(schema->prefixmap, attid, out->values, &oid);
1024                 W_ERROR_NOT_OK_RETURN(status);
1025
1026                 out->values[i] = data_blob_string_const(oid);
1027         }
1028
1029         return WERR_OK;
1030 }
1031
1032 static WERROR _dsdb_syntax_auto_OID_ldb_to_drsuapi(struct ldb_context *ldb,
1033                                                    const struct dsdb_schema *schema,
1034                                                    const struct dsdb_attribute *attr,
1035                                                    const struct ldb_message_element *in,
1036                                                    TALLOC_CTX *mem_ctx,
1037                                                   struct drsuapi_DsReplicaAttribute *out)
1038 {
1039         unsigned int i;
1040         DATA_BLOB *blobs;
1041
1042         out->attid= attr->attributeID_id;
1043         out->value_ctr.num_values= in->num_values;
1044         out->value_ctr.values= talloc_array(mem_ctx,
1045                                             struct drsuapi_DsAttributeValue,
1046                                             in->num_values);
1047         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1048
1049         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1050         W_ERROR_HAVE_NO_MEMORY(blobs);
1051
1052         for (i=0; i < in->num_values; i++) {
1053                 const struct dsdb_class *obj_class;
1054                 const struct dsdb_attribute *obj_attr;
1055                 struct ldb_val *v;
1056
1057                 out->value_ctr.values[i].blob= &blobs[i];
1058
1059                 blobs[i] = data_blob_talloc(blobs, NULL, 4);
1060                 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
1061
1062                 /* in DRS windows puts the classes in the opposite
1063                    order to the order used in ldap */
1064                 v = &in->values[(in->num_values-1)-i];
1065
1066                 if ((obj_class = dsdb_class_by_lDAPDisplayName_ldb_val(schema, v))) {
1067                         SIVAL(blobs[i].data, 0, obj_class->governsID_id);
1068                 } else if ((obj_attr = dsdb_attribute_by_lDAPDisplayName_ldb_val(schema, v))) {
1069                         SIVAL(blobs[i].data, 0, obj_attr->attributeID_id);
1070                 } else {
1071                         uint32_t attid;
1072                         WERROR werr;
1073                         werr = dsdb_schema_pfm_make_attid(schema->prefixmap,
1074                                                           (const char *)v->data,
1075                                                           &attid);
1076                         W_ERROR_NOT_OK_RETURN(werr);
1077                         SIVAL(blobs[i].data, 0, attid);
1078                 }
1079
1080         }
1081
1082
1083         return WERR_OK;
1084 }
1085
1086 static WERROR _dsdb_syntax_OID_obj_ldb_to_drsuapi(struct ldb_context *ldb,
1087                                                   const struct dsdb_schema *schema,
1088                                                   const struct dsdb_attribute *attr,
1089                                                   const struct ldb_message_element *in,
1090                                                   TALLOC_CTX *mem_ctx,
1091                                                   struct drsuapi_DsReplicaAttribute *out)
1092 {
1093         unsigned int i;
1094         DATA_BLOB *blobs;
1095
1096         out->attid= attr->attributeID_id;
1097         out->value_ctr.num_values= in->num_values;
1098         out->value_ctr.values= talloc_array(mem_ctx,
1099                                             struct drsuapi_DsAttributeValue,
1100                                             in->num_values);
1101         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1102
1103         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1104         W_ERROR_HAVE_NO_MEMORY(blobs);
1105
1106         for (i=0; i < in->num_values; i++) {
1107                 const struct dsdb_class *obj_class;
1108
1109                 out->value_ctr.values[i].blob= &blobs[i];
1110
1111                 blobs[i] = data_blob_talloc(blobs, NULL, 4);
1112                 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
1113
1114                 /* in DRS windows puts the classes in the opposite
1115                    order to the order used in ldap */
1116                 obj_class = dsdb_class_by_lDAPDisplayName(schema,
1117                                                           (const char *)in->values[(in->num_values-1)-i].data);
1118                 if (!obj_class) {
1119                         return WERR_FOOBAR;
1120                 }
1121                 SIVAL(blobs[i].data, 0, obj_class->governsID_id);
1122         }
1123
1124
1125         return WERR_OK;
1126 }
1127
1128 static WERROR _dsdb_syntax_OID_attr_ldb_to_drsuapi(struct ldb_context *ldb,
1129                                                    const struct dsdb_schema *schema,
1130                                                    const struct dsdb_attribute *attr,
1131                                                    const struct ldb_message_element *in,
1132                                                    TALLOC_CTX *mem_ctx,
1133                                                    struct drsuapi_DsReplicaAttribute *out)
1134 {
1135         unsigned int i;
1136         DATA_BLOB *blobs;
1137
1138         out->attid= attr->attributeID_id;
1139         out->value_ctr.num_values= in->num_values;
1140         out->value_ctr.values= talloc_array(mem_ctx,
1141                                             struct drsuapi_DsAttributeValue,
1142                                             in->num_values);
1143         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1144
1145         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1146         W_ERROR_HAVE_NO_MEMORY(blobs);
1147
1148         for (i=0; i < in->num_values; i++) {
1149                 const struct dsdb_attribute *obj_attr;
1150
1151                 out->value_ctr.values[i].blob= &blobs[i];
1152
1153                 blobs[i] = data_blob_talloc(blobs, NULL, 4);
1154                 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
1155
1156                 obj_attr = dsdb_attribute_by_lDAPDisplayName(schema, (const char *)in->values[i].data);
1157                 if (!obj_attr) {
1158                         return WERR_FOOBAR;
1159                 }
1160                 SIVAL(blobs[i].data, 0, obj_attr->attributeID_id);
1161         }
1162
1163
1164         return WERR_OK;
1165 }
1166
1167 static WERROR _dsdb_syntax_OID_oid_ldb_to_drsuapi(struct ldb_context *ldb,
1168                                                   const struct dsdb_schema *schema,
1169                                                   const struct dsdb_attribute *attr,
1170                                                   const struct ldb_message_element *in,
1171                                                   TALLOC_CTX *mem_ctx,
1172                                                   struct drsuapi_DsReplicaAttribute *out)
1173 {
1174         unsigned int i;
1175         DATA_BLOB *blobs;
1176
1177         out->attid= attr->attributeID_id;
1178         out->value_ctr.num_values= in->num_values;
1179         out->value_ctr.values= talloc_array(mem_ctx,
1180                                             struct drsuapi_DsAttributeValue,
1181                                             in->num_values);
1182         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1183
1184         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1185         W_ERROR_HAVE_NO_MEMORY(blobs);
1186
1187         for (i=0; i < in->num_values; i++) {
1188                 uint32_t attid;
1189                 WERROR status;
1190
1191                 out->value_ctr.values[i].blob= &blobs[i];
1192
1193                 blobs[i] = data_blob_talloc(blobs, NULL, 4);
1194                 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
1195
1196                 status = dsdb_schema_pfm_make_attid(schema->prefixmap,
1197                                                     (const char *)in->values[i].data,
1198                                                     &attid);
1199                 W_ERROR_NOT_OK_RETURN(status);
1200
1201                 SIVAL(blobs[i].data, 0, attid);
1202         }
1203
1204         return WERR_OK;
1205 }
1206
1207 static WERROR dsdb_syntax_OID_drsuapi_to_ldb(struct ldb_context *ldb, 
1208                                              const struct dsdb_schema *schema,
1209                                              const struct dsdb_attribute *attr,
1210                                              const struct drsuapi_DsReplicaAttribute *in,
1211                                              TALLOC_CTX *mem_ctx,
1212                                              struct ldb_message_element *out)
1213 {
1214         switch (attr->attributeID_id) {
1215         case DRSUAPI_ATTRIBUTE_objectClass:
1216         case DRSUAPI_ATTRIBUTE_subClassOf:
1217         case DRSUAPI_ATTRIBUTE_auxiliaryClass:
1218         case DRSUAPI_ATTRIBUTE_systemAuxiliaryClass:
1219         case DRSUAPI_ATTRIBUTE_systemPossSuperiors:
1220         case DRSUAPI_ATTRIBUTE_possSuperiors:
1221                 return _dsdb_syntax_OID_obj_drsuapi_to_ldb(ldb, schema, attr, in, mem_ctx, out);
1222         case DRSUAPI_ATTRIBUTE_systemMustContain:
1223         case DRSUAPI_ATTRIBUTE_systemMayContain:        
1224         case DRSUAPI_ATTRIBUTE_mustContain:
1225         case DRSUAPI_ATTRIBUTE_rDNAttId:
1226         case DRSUAPI_ATTRIBUTE_transportAddressAttribute:
1227         case DRSUAPI_ATTRIBUTE_mayContain:
1228                 return _dsdb_syntax_OID_attr_drsuapi_to_ldb(ldb, schema, attr, in, mem_ctx, out);
1229         case DRSUAPI_ATTRIBUTE_governsID:
1230         case DRSUAPI_ATTRIBUTE_attributeID:
1231         case DRSUAPI_ATTRIBUTE_attributeSyntax:
1232                 return _dsdb_syntax_OID_oid_drsuapi_to_ldb(ldb, schema, attr, in, mem_ctx, out);
1233         }
1234
1235         DEBUG(0,(__location__ ": Unknown handling for attributeID_id for %s\n",
1236                  attr->lDAPDisplayName));
1237         return _dsdb_syntax_auto_OID_drsuapi_to_ldb(ldb, schema, attr, in, mem_ctx, out);
1238 }
1239
1240 static WERROR dsdb_syntax_OID_ldb_to_drsuapi(struct ldb_context *ldb, 
1241                                              const struct dsdb_schema *schema,
1242                                              const struct dsdb_attribute *attr,
1243                                              const struct ldb_message_element *in,
1244                                              TALLOC_CTX *mem_ctx,
1245                                              struct drsuapi_DsReplicaAttribute *out)
1246 {
1247         if (attr->attributeID_id == 0xFFFFFFFF) {
1248                 return WERR_FOOBAR;
1249         }
1250
1251         switch (attr->attributeID_id) {
1252         case DRSUAPI_ATTRIBUTE_objectClass:
1253         case DRSUAPI_ATTRIBUTE_subClassOf:
1254         case DRSUAPI_ATTRIBUTE_auxiliaryClass:
1255         case DRSUAPI_ATTRIBUTE_systemAuxiliaryClass:
1256         case DRSUAPI_ATTRIBUTE_systemPossSuperiors:
1257         case DRSUAPI_ATTRIBUTE_possSuperiors:
1258                 return _dsdb_syntax_OID_obj_ldb_to_drsuapi(ldb, schema, attr, in, mem_ctx, out);
1259         case DRSUAPI_ATTRIBUTE_systemMustContain:
1260         case DRSUAPI_ATTRIBUTE_systemMayContain:        
1261         case DRSUAPI_ATTRIBUTE_mustContain:
1262         case DRSUAPI_ATTRIBUTE_rDNAttId:
1263         case DRSUAPI_ATTRIBUTE_transportAddressAttribute:
1264         case DRSUAPI_ATTRIBUTE_mayContain:
1265                 return _dsdb_syntax_OID_attr_ldb_to_drsuapi(ldb, schema, attr, in, mem_ctx, out);
1266         case DRSUAPI_ATTRIBUTE_governsID:
1267         case DRSUAPI_ATTRIBUTE_attributeID:
1268         case DRSUAPI_ATTRIBUTE_attributeSyntax:
1269                 return _dsdb_syntax_OID_oid_ldb_to_drsuapi(ldb, schema, attr, in, mem_ctx, out);
1270         }
1271
1272         DEBUG(0,(__location__ ": Unknown handling for attributeID_id for %s\n",
1273                  attr->lDAPDisplayName));
1274
1275         return _dsdb_syntax_auto_OID_ldb_to_drsuapi(ldb, schema, attr, in, mem_ctx, out);
1276 }
1277
1278 static WERROR dsdb_syntax_OID_validate_ldb(struct ldb_context *ldb,
1279                                            const struct dsdb_schema *schema,
1280                                            const struct dsdb_attribute *attr,
1281                                            const struct ldb_message_element *in)
1282 {
1283         WERROR status;
1284         struct drsuapi_DsReplicaAttribute drs_tmp;
1285         struct ldb_message_element ldb_tmp;
1286         TALLOC_CTX *tmp_ctx;
1287
1288         if (attr->attributeID_id == 0xFFFFFFFF) {
1289                 return WERR_FOOBAR;
1290         }
1291
1292         /*
1293          * TODO: optimize and verify this code
1294          */
1295
1296         tmp_ctx = talloc_new(ldb);
1297         if (tmp_ctx == NULL) {
1298                 return WERR_NOMEM;
1299         }
1300
1301         status = dsdb_syntax_OID_ldb_to_drsuapi(ldb,
1302                                                 schema,
1303                                                 attr,
1304                                                 in,
1305                                                 tmp_ctx,
1306                                                 &drs_tmp);
1307         if (!W_ERROR_IS_OK(status)) {
1308                 talloc_free(tmp_ctx);
1309                 return status;
1310         }
1311
1312         status = dsdb_syntax_OID_drsuapi_to_ldb(ldb,
1313                                                 schema,
1314                                                 attr,
1315                                                 &drs_tmp,
1316                                                 tmp_ctx,
1317                                                 &ldb_tmp);
1318         if (!W_ERROR_IS_OK(status)) {
1319                 talloc_free(tmp_ctx);
1320                 return status;
1321         }
1322
1323         talloc_free(tmp_ctx);
1324         return WERR_OK;
1325 }
1326
1327 static WERROR dsdb_syntax_UNICODE_drsuapi_to_ldb(struct ldb_context *ldb, 
1328                                                  const struct dsdb_schema *schema,
1329                                                  const struct dsdb_attribute *attr,
1330                                                  const struct drsuapi_DsReplicaAttribute *in,
1331                                                  TALLOC_CTX *mem_ctx,
1332                                                  struct ldb_message_element *out)
1333 {
1334         unsigned int i;
1335
1336         out->flags      = 0;
1337         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
1338         W_ERROR_HAVE_NO_MEMORY(out->name);
1339
1340         out->num_values = in->value_ctr.num_values;
1341         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
1342         W_ERROR_HAVE_NO_MEMORY(out->values);
1343
1344         for (i=0; i < out->num_values; i++) {
1345                 char *str;
1346
1347                 if (in->value_ctr.values[i].blob == NULL) {
1348                         return WERR_FOOBAR;
1349                 }
1350
1351                 if (in->value_ctr.values[i].blob->length == 0) {
1352                         return WERR_FOOBAR;
1353                 }
1354
1355                 if (!convert_string_talloc(out->values, 
1356                                                                         CH_UTF16, CH_UNIX,
1357                                             in->value_ctr.values[i].blob->data,
1358                                             in->value_ctr.values[i].blob->length,
1359                                             (void **)&str, NULL, false)) {
1360                         return WERR_FOOBAR;
1361                 }
1362
1363                 out->values[i] = data_blob_string_const(str);
1364         }
1365
1366         return WERR_OK;
1367 }
1368
1369 static WERROR dsdb_syntax_UNICODE_ldb_to_drsuapi(struct ldb_context *ldb, 
1370                                                  const struct dsdb_schema *schema,
1371                                                  const struct dsdb_attribute *attr,
1372                                                  const struct ldb_message_element *in,
1373                                                  TALLOC_CTX *mem_ctx,
1374                                                  struct drsuapi_DsReplicaAttribute *out)
1375 {
1376         unsigned int i;
1377         DATA_BLOB *blobs;
1378
1379         if (attr->attributeID_id == 0xFFFFFFFF) {
1380                 return WERR_FOOBAR;
1381         }
1382
1383         out->attid                      = attr->attributeID_id;
1384         out->value_ctr.num_values       = in->num_values;
1385         out->value_ctr.values           = talloc_array(mem_ctx,
1386                                                        struct drsuapi_DsAttributeValue,
1387                                                        in->num_values);
1388         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1389
1390         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1391         W_ERROR_HAVE_NO_MEMORY(blobs);
1392
1393         for (i=0; i < in->num_values; i++) {
1394                 out->value_ctr.values[i].blob   = &blobs[i];
1395
1396                 if (!convert_string_talloc(blobs,
1397                         CH_UNIX, CH_UTF16,
1398                         in->values[i].data, in->values[i].length,
1399                         (void **)&blobs[i].data, &blobs[i].length, false)) {
1400                                 return WERR_FOOBAR;
1401                 }
1402         }
1403
1404         return WERR_OK;
1405 }
1406
1407 static WERROR dsdb_syntax_UNICODE_validate_one_val(struct ldb_context *ldb,
1408                                                    const struct dsdb_schema *schema,
1409                                                    const struct dsdb_attribute *attr,
1410                                                    const struct ldb_val *val)
1411 {
1412         void *dst = NULL;
1413         size_t size;
1414         bool ok;
1415
1416         if (attr->attributeID_id == 0xFFFFFFFF) {
1417                 return WERR_FOOBAR;
1418         }
1419
1420         ok = convert_string_talloc_convenience(ldb,
1421                                                schema->iconv_convenience,
1422                                                CH_UNIX, CH_UTF16,
1423                                                val->data,
1424                                                val->length,
1425                                                (void **)&dst,
1426                                                &size, false);
1427         TALLOC_FREE(dst);
1428         if (!ok) {
1429                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1430         }
1431
1432         if (attr->rangeLower) {
1433                 if ((size/2) < *attr->rangeLower) {
1434                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1435                 }
1436         }
1437
1438         if (attr->rangeUpper) {
1439                 if ((size/2) > *attr->rangeUpper) {
1440                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1441                 }
1442         }
1443
1444         return WERR_OK;
1445 }
1446
1447 static WERROR dsdb_syntax_UNICODE_validate_ldb(struct ldb_context *ldb,
1448                                                const struct dsdb_schema *schema,
1449                                                const struct dsdb_attribute *attr,
1450                                                const struct ldb_message_element *in)
1451 {
1452         WERROR status;
1453         unsigned int i;
1454
1455         if (attr->attributeID_id == 0xFFFFFFFF) {
1456                 return WERR_FOOBAR;
1457         }
1458
1459         for (i=0; i < in->num_values; i++) {
1460                 if (in->values[i].length == 0) {
1461                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1462                 }
1463
1464                 status = dsdb_syntax_UNICODE_validate_one_val(ldb,
1465                                                               schema,
1466                                                               attr,
1467                                                               &in->values[i]);
1468                 if (!W_ERROR_IS_OK(status)) {
1469                         return status;
1470                 }
1471         }
1472
1473         return WERR_OK;
1474 }
1475
1476 WERROR dsdb_syntax_one_DN_drsuapi_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, 
1477                                          const struct dsdb_syntax *syntax, 
1478                                          const DATA_BLOB *in, DATA_BLOB *out)
1479 {
1480         struct drsuapi_DsReplicaObjectIdentifier3 id3;
1481         enum ndr_err_code ndr_err;
1482         DATA_BLOB guid_blob;
1483         struct ldb_dn *dn;
1484         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1485         int ret;
1486         NTSTATUS status;
1487
1488         if (!tmp_ctx) {
1489                 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1490         }
1491         
1492         if (in == NULL) {
1493                 talloc_free(tmp_ctx);
1494                 return WERR_FOOBAR;
1495         }
1496         
1497         if (in->length == 0) {
1498                 talloc_free(tmp_ctx);
1499                 return WERR_FOOBAR;
1500         }
1501         
1502         
1503         /* windows sometimes sends an extra two pad bytes here */
1504         ndr_err = ndr_pull_struct_blob(in,
1505                                        tmp_ctx, &id3,
1506                                        (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3);
1507         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1508                 status = ndr_map_error2ntstatus(ndr_err);
1509                 talloc_free(tmp_ctx);
1510                 return ntstatus_to_werror(status);
1511         }
1512         
1513         dn = ldb_dn_new(tmp_ctx, ldb, id3.dn);
1514         if (!dn) {
1515                 talloc_free(tmp_ctx);
1516                 /* If this fails, it must be out of memory, as it does not do much parsing */
1517                 W_ERROR_HAVE_NO_MEMORY(dn);
1518         }
1519
1520         if (!GUID_all_zero(&id3.guid)) {
1521                 status = GUID_to_ndr_blob(&id3.guid, tmp_ctx, &guid_blob);
1522                 if (!NT_STATUS_IS_OK(status)) {
1523                         talloc_free(tmp_ctx);
1524                         return ntstatus_to_werror(status);
1525                 }
1526         
1527                 ret = ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
1528                 if (ret != LDB_SUCCESS) {
1529                         talloc_free(tmp_ctx);
1530                         return WERR_FOOBAR;
1531                 }
1532                 talloc_free(guid_blob.data);
1533         }
1534         
1535         if (id3.__ndr_size_sid) {
1536                 DATA_BLOB sid_blob;
1537                 ndr_err = ndr_push_struct_blob(&sid_blob, tmp_ctx, &id3.sid,
1538                                                (ndr_push_flags_fn_t)ndr_push_dom_sid);
1539                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1540                         status = ndr_map_error2ntstatus(ndr_err);
1541                         talloc_free(tmp_ctx);
1542                         return ntstatus_to_werror(status);
1543                 }
1544                 
1545                 ret = ldb_dn_set_extended_component(dn, "SID", &sid_blob);
1546                 if (ret != LDB_SUCCESS) {
1547                         talloc_free(tmp_ctx);
1548                         return WERR_FOOBAR;
1549                 }
1550         }
1551         
1552         *out = data_blob_string_const(ldb_dn_get_extended_linearized(mem_ctx, dn, 1));
1553         talloc_free(tmp_ctx);
1554         return WERR_OK;
1555 }
1556
1557 static WERROR dsdb_syntax_DN_drsuapi_to_ldb(struct ldb_context *ldb, 
1558                                             const struct dsdb_schema *schema,
1559                                             const struct dsdb_attribute *attr,
1560                                             const struct drsuapi_DsReplicaAttribute *in,
1561                                             TALLOC_CTX *mem_ctx,
1562                                             struct ldb_message_element *out)
1563 {
1564         unsigned int i;
1565
1566         out->flags      = 0;
1567         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
1568         W_ERROR_HAVE_NO_MEMORY(out->name);
1569
1570         out->num_values = in->value_ctr.num_values;
1571         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
1572         W_ERROR_HAVE_NO_MEMORY(out->values);
1573
1574         for (i=0; i < out->num_values; i++) {
1575                 WERROR status = dsdb_syntax_one_DN_drsuapi_to_ldb(out->values, ldb, attr->syntax, 
1576                                                                   in->value_ctr.values[i].blob, 
1577                                                                   &out->values[i]);
1578                 if (!W_ERROR_IS_OK(status)) {
1579                         return status;
1580                 }
1581                                                   
1582         }
1583
1584         return WERR_OK;
1585 }
1586
1587 static WERROR dsdb_syntax_DN_ldb_to_drsuapi(struct ldb_context *ldb, 
1588                                             const struct dsdb_schema *schema,
1589                                             const struct dsdb_attribute *attr,
1590                                             const struct ldb_message_element *in,
1591                                             TALLOC_CTX *mem_ctx,
1592                                             struct drsuapi_DsReplicaAttribute *out)
1593 {
1594         unsigned int i;
1595         DATA_BLOB *blobs;
1596
1597         if (attr->attributeID_id == 0xFFFFFFFF) {
1598                 return WERR_FOOBAR;
1599         }
1600
1601         out->attid                      = attr->attributeID_id;
1602         out->value_ctr.num_values       = in->num_values;
1603         out->value_ctr.values           = talloc_array(mem_ctx,
1604                                                        struct drsuapi_DsAttributeValue,
1605                                                        in->num_values);
1606         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1607
1608         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1609         W_ERROR_HAVE_NO_MEMORY(blobs);
1610
1611         for (i=0; i < in->num_values; i++) {
1612                 struct drsuapi_DsReplicaObjectIdentifier3 id3;
1613                 enum ndr_err_code ndr_err;
1614                 struct ldb_dn *dn;
1615                 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1616                 NTSTATUS status;
1617
1618                 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1619
1620                 out->value_ctr.values[i].blob   = &blobs[i];
1621
1622                 dn = ldb_dn_from_ldb_val(tmp_ctx, ldb, &in->values[i]);
1623
1624                 W_ERROR_HAVE_NO_MEMORY(dn);
1625
1626                 ZERO_STRUCT(id3);
1627
1628                 status = dsdb_get_extended_dn_guid(dn, &id3.guid, "GUID");
1629                 if (!NT_STATUS_IS_OK(status) &&
1630                     !NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1631                         talloc_free(tmp_ctx);
1632                         return ntstatus_to_werror(status);
1633                 }
1634
1635                 status = dsdb_get_extended_dn_sid(dn, &id3.sid, "SID");
1636                 if (!NT_STATUS_IS_OK(status) &&
1637                     !NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1638                         talloc_free(tmp_ctx);
1639                         return ntstatus_to_werror(status);
1640                 }
1641
1642                 id3.dn = ldb_dn_get_linearized(dn);
1643
1644                 ndr_err = ndr_push_struct_blob(&blobs[i], blobs, &id3, (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
1645                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1646                         status = ndr_map_error2ntstatus(ndr_err);
1647                         talloc_free(tmp_ctx);
1648                         return ntstatus_to_werror(status);
1649                 }
1650                 talloc_free(tmp_ctx);
1651         }
1652
1653         return WERR_OK;
1654 }
1655
1656 static WERROR dsdb_syntax_DN_validate_one_val(struct ldb_context *ldb,
1657                                               const struct dsdb_schema *schema,
1658                                               const struct dsdb_attribute *attr,
1659                                               const struct ldb_val *val,
1660                                               TALLOC_CTX *mem_ctx,
1661                                               struct dsdb_dn **_dsdb_dn)
1662 {
1663         static const char * const extended_list[] = { "GUID", "SID", NULL };
1664         enum ndr_err_code ndr_err;
1665         struct GUID guid;
1666         struct dom_sid sid;
1667         const DATA_BLOB *sid_blob;
1668         struct dsdb_dn *dsdb_dn;
1669         struct ldb_dn *dn;
1670         char *dn_str;
1671         struct ldb_dn *dn2;
1672         char *dn2_str;
1673         int num_components;
1674         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1675         NTSTATUS status;
1676
1677         W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1678
1679         if (attr->attributeID_id == 0xFFFFFFFF) {
1680                 return WERR_FOOBAR;
1681         }
1682
1683         dsdb_dn = dsdb_dn_parse(tmp_ctx, ldb, val,
1684                                 attr->syntax->ldap_oid);
1685         if (!dsdb_dn) {
1686                 talloc_free(tmp_ctx);
1687                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1688         }
1689         dn = dsdb_dn->dn;
1690
1691         dn2 = ldb_dn_copy(tmp_ctx, dn);
1692         if (dn == NULL) {
1693                 talloc_free(tmp_ctx);
1694                 return WERR_NOMEM;
1695         }
1696
1697         num_components = ldb_dn_get_comp_num(dn);
1698
1699         status = dsdb_get_extended_dn_guid(dn, &guid, "GUID");
1700         if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1701                 num_components++;
1702         } else if (!NT_STATUS_IS_OK(status)) {
1703                 talloc_free(tmp_ctx);
1704                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1705         }
1706
1707         sid_blob = ldb_dn_get_extended_component(dn, "SID");
1708         if (sid_blob) {
1709                 num_components++;
1710                 ndr_err = ndr_pull_struct_blob_all(sid_blob,
1711                                                    tmp_ctx,
1712                                                    &sid,
1713                                                    (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
1714                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1715                         talloc_free(tmp_ctx);
1716                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1717                 }
1718         }
1719
1720         /* Do not allow links to the RootDSE */
1721         if (num_components == 0) {
1722                 talloc_free(tmp_ctx);
1723                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1724         }
1725
1726         /*
1727          * We need to check that only "GUID" and "SID" are
1728          * specified as extended components, we do that
1729          * by comparing the dn's after removing all components
1730          * from one dn and only the allowed subset from the other
1731          * one.
1732          */
1733         ldb_dn_extended_filter(dn, extended_list);
1734
1735         dn_str = ldb_dn_get_extended_linearized(tmp_ctx, dn, 0);
1736         if (dn_str == NULL) {
1737                 talloc_free(tmp_ctx);
1738                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1739         }
1740         dn2_str = ldb_dn_get_extended_linearized(tmp_ctx, dn2, 0);
1741         if (dn2_str == NULL) {
1742                 talloc_free(tmp_ctx);
1743                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1744         }
1745
1746         if (strcmp(dn_str, dn2_str) != 0) {
1747                 talloc_free(tmp_ctx);
1748                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1749         }
1750
1751         *_dsdb_dn = talloc_move(mem_ctx, &dsdb_dn);
1752         talloc_free(tmp_ctx);
1753         return WERR_OK;
1754 }
1755
1756 static WERROR dsdb_syntax_DN_validate_ldb(struct ldb_context *ldb,
1757                                           const struct dsdb_schema *schema,
1758                                           const struct dsdb_attribute *attr,
1759                                           const struct ldb_message_element *in)
1760 {
1761         unsigned int i;
1762
1763         if (attr->attributeID_id == 0xFFFFFFFF) {
1764                 return WERR_FOOBAR;
1765         }
1766
1767         for (i=0; i < in->num_values; i++) {
1768                 WERROR status;
1769                 struct dsdb_dn *dsdb_dn;
1770                 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
1771                 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1772
1773                 status = dsdb_syntax_DN_validate_one_val(ldb,
1774                                                          schema,
1775                                                          attr,
1776                                                          &in->values[i],
1777                                                          tmp_ctx, &dsdb_dn);
1778                 if (!W_ERROR_IS_OK(status)) {
1779                         talloc_free(tmp_ctx);
1780                         return status;
1781                 }
1782
1783                 if (dsdb_dn->dn_format != DSDB_NORMAL_DN) {
1784                         talloc_free(tmp_ctx);
1785                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1786                 }
1787
1788                 talloc_free(tmp_ctx);
1789         }
1790
1791         return WERR_OK;
1792 }
1793
1794 static WERROR dsdb_syntax_DN_BINARY_drsuapi_to_ldb(struct ldb_context *ldb, 
1795                                                    const struct dsdb_schema *schema,
1796                                                    const struct dsdb_attribute *attr,
1797                                                    const struct drsuapi_DsReplicaAttribute *in,
1798                                                    TALLOC_CTX *mem_ctx,
1799                                                    struct ldb_message_element *out)
1800 {
1801         unsigned int i;
1802         int ret;
1803
1804         out->flags      = 0;
1805         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
1806         W_ERROR_HAVE_NO_MEMORY(out->name);
1807
1808         out->num_values = in->value_ctr.num_values;
1809         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
1810         W_ERROR_HAVE_NO_MEMORY(out->values);
1811
1812         for (i=0; i < out->num_values; i++) {
1813                 struct drsuapi_DsReplicaObjectIdentifier3Binary id3;
1814                 enum ndr_err_code ndr_err;
1815                 DATA_BLOB guid_blob;
1816                 struct ldb_dn *dn;
1817                 struct dsdb_dn *dsdb_dn;
1818                 NTSTATUS status;
1819                 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1820                 if (!tmp_ctx) {
1821                         W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1822                 }
1823
1824                 if (in->value_ctr.values[i].blob == NULL) {
1825                         talloc_free(tmp_ctx);
1826                         return WERR_FOOBAR;
1827                 }
1828
1829                 if (in->value_ctr.values[i].blob->length == 0) {
1830                         talloc_free(tmp_ctx);
1831                         return WERR_FOOBAR;
1832                 }
1833
1834                 
1835                 /* windows sometimes sends an extra two pad bytes here */
1836                 ndr_err = ndr_pull_struct_blob(in->value_ctr.values[i].blob,
1837                                                tmp_ctx, &id3,
1838                                                (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3Binary);
1839                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1840                         status = ndr_map_error2ntstatus(ndr_err);
1841                         talloc_free(tmp_ctx);
1842                         return ntstatus_to_werror(status);
1843                 }
1844
1845                 dn = ldb_dn_new(tmp_ctx, ldb, id3.dn);
1846                 if (!dn) {
1847                         talloc_free(tmp_ctx);
1848                         /* If this fails, it must be out of memory, as it does not do much parsing */
1849                         W_ERROR_HAVE_NO_MEMORY(dn);
1850                 }
1851
1852                 status = GUID_to_ndr_blob(&id3.guid, tmp_ctx, &guid_blob);
1853                 if (!NT_STATUS_IS_OK(status)) {
1854                         talloc_free(tmp_ctx);
1855                         return ntstatus_to_werror(status);
1856                 }
1857
1858                 ret = ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
1859                 if (ret != LDB_SUCCESS) {
1860                         talloc_free(tmp_ctx);
1861                         return WERR_FOOBAR;
1862                 }
1863
1864                 talloc_free(guid_blob.data);
1865
1866                 if (id3.__ndr_size_sid) {
1867                         DATA_BLOB sid_blob;
1868                         ndr_err = ndr_push_struct_blob(&sid_blob, tmp_ctx, &id3.sid,
1869                                                        (ndr_push_flags_fn_t)ndr_push_dom_sid);
1870                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1871                                 status = ndr_map_error2ntstatus(ndr_err);
1872                                 talloc_free(tmp_ctx);
1873                                 return ntstatus_to_werror(status);
1874                         }
1875
1876                         ret = ldb_dn_set_extended_component(dn, "SID", &sid_blob);
1877                         if (ret != LDB_SUCCESS) {
1878                                 talloc_free(tmp_ctx);
1879                                 return WERR_FOOBAR;
1880                         }
1881                 }
1882
1883                 /* set binary stuff */
1884                 dsdb_dn = dsdb_dn_construct(tmp_ctx, dn, id3.binary, attr->syntax->ldap_oid);
1885                 if (!dsdb_dn) {
1886                         /* If this fails, it must be out of memory, we know the ldap_oid is valid */
1887                         talloc_free(tmp_ctx);
1888                         W_ERROR_HAVE_NO_MEMORY(dsdb_dn);
1889                 }
1890                 out->values[i] = data_blob_string_const(dsdb_dn_get_extended_linearized(out->values, dsdb_dn, 1));
1891                 talloc_free(tmp_ctx);
1892         }
1893
1894         return WERR_OK;
1895 }
1896
1897 static WERROR dsdb_syntax_DN_BINARY_ldb_to_drsuapi(struct ldb_context *ldb, 
1898                                                    const struct dsdb_schema *schema,
1899                                                    const struct dsdb_attribute *attr,
1900                                                    const struct ldb_message_element *in,
1901                                                    TALLOC_CTX *mem_ctx,
1902                                                    struct drsuapi_DsReplicaAttribute *out)
1903 {
1904         unsigned int i;
1905         DATA_BLOB *blobs;
1906
1907         if (attr->attributeID_id == 0xFFFFFFFF) {
1908                 return WERR_FOOBAR;
1909         }
1910
1911         out->attid                      = attr->attributeID_id;
1912         out->value_ctr.num_values       = in->num_values;
1913         out->value_ctr.values           = talloc_array(mem_ctx,
1914                                                        struct drsuapi_DsAttributeValue,
1915                                                        in->num_values);
1916         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1917
1918         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1919         W_ERROR_HAVE_NO_MEMORY(blobs);
1920
1921         for (i=0; i < in->num_values; i++) {
1922                 struct drsuapi_DsReplicaObjectIdentifier3Binary id3;
1923                 enum ndr_err_code ndr_err;
1924                 const DATA_BLOB *sid_blob;
1925                 struct dsdb_dn *dsdb_dn;
1926                 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1927                 NTSTATUS status;
1928
1929                 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1930
1931                 out->value_ctr.values[i].blob   = &blobs[i];
1932
1933                 dsdb_dn = dsdb_dn_parse(tmp_ctx, ldb, &in->values[i], attr->syntax->ldap_oid);
1934
1935                 if (!dsdb_dn) {
1936                         talloc_free(tmp_ctx);
1937                         return ntstatus_to_werror(NT_STATUS_INVALID_PARAMETER);
1938                 }
1939
1940                 ZERO_STRUCT(id3);
1941
1942                 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &id3.guid, "GUID");
1943                 if (!NT_STATUS_IS_OK(status) &&
1944                     !NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1945                         talloc_free(tmp_ctx);
1946                         return ntstatus_to_werror(status);
1947                 }
1948
1949                 sid_blob = ldb_dn_get_extended_component(dsdb_dn->dn, "SID");
1950                 if (sid_blob) {
1951                         
1952                         ndr_err = ndr_pull_struct_blob_all(sid_blob, 
1953                                                            tmp_ctx, &id3.sid,
1954                                                            (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
1955                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1956                                 status = ndr_map_error2ntstatus(ndr_err);
1957                                 talloc_free(tmp_ctx);
1958                                 return ntstatus_to_werror(status);
1959                         }
1960                 }
1961
1962                 id3.dn = ldb_dn_get_linearized(dsdb_dn->dn);
1963
1964                 /* get binary stuff */
1965                 id3.binary = dsdb_dn->extra_part;
1966
1967                 ndr_err = ndr_push_struct_blob(&blobs[i], blobs, &id3, (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3Binary);
1968                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1969                         status = ndr_map_error2ntstatus(ndr_err);
1970                         talloc_free(tmp_ctx);
1971                         return ntstatus_to_werror(status);
1972                 }
1973                 talloc_free(tmp_ctx);
1974         }
1975
1976         return WERR_OK;
1977 }
1978
1979 static WERROR dsdb_syntax_DN_BINARY_validate_ldb(struct ldb_context *ldb,
1980                                                  const struct dsdb_schema *schema,
1981                                                  const struct dsdb_attribute *attr,
1982                                                  const struct ldb_message_element *in)
1983 {
1984         unsigned int i;
1985
1986         if (attr->attributeID_id == 0xFFFFFFFF) {
1987                 return WERR_FOOBAR;
1988         }
1989
1990         for (i=0; i < in->num_values; i++) {
1991                 WERROR status;
1992                 struct dsdb_dn *dsdb_dn;
1993                 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
1994                 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1995
1996                 status = dsdb_syntax_DN_validate_one_val(ldb,
1997                                                          schema,
1998                                                          attr,
1999                                                          &in->values[i],
2000                                                          tmp_ctx, &dsdb_dn);
2001                 if (!W_ERROR_IS_OK(status)) {
2002                         talloc_free(tmp_ctx);
2003                         return status;
2004                 }
2005
2006                 if (dsdb_dn->dn_format != DSDB_BINARY_DN) {
2007                         talloc_free(tmp_ctx);
2008                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
2009                 }
2010
2011                 status = dsdb_syntax_DATA_BLOB_validate_one_val(ldb,
2012                                                                 schema,
2013                                                                 attr,
2014                                                                 &dsdb_dn->extra_part);
2015                 if (!W_ERROR_IS_OK(status)) {
2016                         talloc_free(tmp_ctx);
2017                         return status;
2018                 }
2019
2020                 talloc_free(tmp_ctx);
2021         }
2022
2023         return WERR_OK;
2024 }
2025
2026 static WERROR dsdb_syntax_DN_STRING_drsuapi_to_ldb(struct ldb_context *ldb,
2027                                                    const struct dsdb_schema *schema,
2028                                                    const struct dsdb_attribute *attr,
2029                                                    const struct drsuapi_DsReplicaAttribute *in,
2030                                                    TALLOC_CTX *mem_ctx,
2031                                                    struct ldb_message_element *out)
2032 {
2033         return dsdb_syntax_DN_BINARY_drsuapi_to_ldb(ldb,
2034                                                     schema,
2035                                                     attr,
2036                                                     in,
2037                                                     mem_ctx,
2038                                                     out);
2039 }
2040
2041 static WERROR dsdb_syntax_DN_STRING_ldb_to_drsuapi(struct ldb_context *ldb,
2042                                                    const struct dsdb_schema *schema,
2043                                                    const struct dsdb_attribute *attr,
2044                                                    const struct ldb_message_element *in,
2045                                                    TALLOC_CTX *mem_ctx,
2046                                                    struct drsuapi_DsReplicaAttribute *out)
2047 {
2048         return dsdb_syntax_DN_BINARY_ldb_to_drsuapi(ldb,
2049                                                     schema,
2050                                                     attr,
2051                                                     in,
2052                                                     mem_ctx,
2053                                                     out);
2054 }
2055
2056 static WERROR dsdb_syntax_DN_STRING_validate_ldb(struct ldb_context *ldb,
2057                                                  const struct dsdb_schema *schema,
2058                                                  const struct dsdb_attribute *attr,
2059                                                  const struct ldb_message_element *in)
2060 {
2061         unsigned int i;
2062
2063         if (attr->attributeID_id == 0xFFFFFFFF) {
2064                 return WERR_FOOBAR;
2065         }
2066
2067         for (i=0; i < in->num_values; i++) {
2068                 WERROR status;
2069                 struct dsdb_dn *dsdb_dn;
2070                 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2071                 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
2072
2073                 status = dsdb_syntax_DN_validate_one_val(ldb,
2074                                                          schema,
2075                                                          attr,
2076                                                          &in->values[i],
2077                                                          tmp_ctx, &dsdb_dn);
2078                 if (!W_ERROR_IS_OK(status)) {
2079                         talloc_free(tmp_ctx);
2080                         return status;
2081                 }
2082
2083                 if (dsdb_dn->dn_format != DSDB_STRING_DN) {
2084                         talloc_free(tmp_ctx);
2085                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
2086                 }
2087
2088                 status = dsdb_syntax_UNICODE_validate_one_val(ldb,
2089                                                               schema,
2090                                                               attr,
2091                                                               &dsdb_dn->extra_part);
2092                 if (!W_ERROR_IS_OK(status)) {
2093                         talloc_free(tmp_ctx);
2094                         return status;
2095                 }
2096
2097                 talloc_free(tmp_ctx);
2098         }
2099
2100         return WERR_OK;
2101 }
2102
2103 static WERROR dsdb_syntax_PRESENTATION_ADDRESS_drsuapi_to_ldb(struct ldb_context *ldb, 
2104                                                               const struct dsdb_schema *schema,
2105                                                               const struct dsdb_attribute *attr,
2106                                                               const struct drsuapi_DsReplicaAttribute *in,
2107                                                               TALLOC_CTX *mem_ctx,
2108                                                               struct ldb_message_element *out)
2109 {
2110         unsigned int i;
2111
2112         out->flags      = 0;
2113         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
2114         W_ERROR_HAVE_NO_MEMORY(out->name);
2115
2116         out->num_values = in->value_ctr.num_values;
2117         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
2118         W_ERROR_HAVE_NO_MEMORY(out->values);
2119
2120         for (i=0; i < out->num_values; i++) {
2121                 size_t len;
2122                 char *str;
2123
2124                 if (in->value_ctr.values[i].blob == NULL) {
2125                         return WERR_FOOBAR;
2126                 }
2127
2128                 if (in->value_ctr.values[i].blob->length < 4) {
2129                         return WERR_FOOBAR;
2130                 }
2131
2132                 len = IVAL(in->value_ctr.values[i].blob->data, 0);
2133
2134                 if (len != in->value_ctr.values[i].blob->length) {
2135                         return WERR_FOOBAR;
2136                 }
2137
2138                 if (!convert_string_talloc(out->values, CH_UTF16, CH_UNIX,
2139                                             in->value_ctr.values[i].blob->data+4,
2140                                             in->value_ctr.values[i].blob->length-4,
2141                                             (void **)&str, NULL, false)) {
2142                         return WERR_FOOBAR;
2143                 }
2144
2145                 out->values[i] = data_blob_string_const(str);
2146         }
2147
2148         return WERR_OK;
2149 }
2150
2151 static WERROR dsdb_syntax_PRESENTATION_ADDRESS_ldb_to_drsuapi(struct ldb_context *ldb, 
2152                                                               const struct dsdb_schema *schema,
2153                                                               const struct dsdb_attribute *attr,
2154                                                               const struct ldb_message_element *in,
2155                                                               TALLOC_CTX *mem_ctx,
2156                                                               struct drsuapi_DsReplicaAttribute *out)
2157 {
2158         unsigned int i;
2159         DATA_BLOB *blobs;
2160
2161         if (attr->attributeID_id == 0xFFFFFFFF) {
2162                 return WERR_FOOBAR;
2163         }
2164
2165         out->attid                      = attr->attributeID_id;
2166         out->value_ctr.num_values       = in->num_values;
2167         out->value_ctr.values           = talloc_array(mem_ctx,
2168                                                        struct drsuapi_DsAttributeValue,
2169                                                        in->num_values);
2170         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
2171
2172         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
2173         W_ERROR_HAVE_NO_MEMORY(blobs);
2174
2175         for (i=0; i < in->num_values; i++) {
2176                 uint8_t *data;
2177                 size_t ret;
2178
2179                 out->value_ctr.values[i].blob   = &blobs[i];
2180
2181                 if (!convert_string_talloc(blobs, CH_UNIX, CH_UTF16,
2182                                             in->values[i].data,
2183                                             in->values[i].length,
2184                                             (void **)&data, &ret, false)) {
2185                         return WERR_FOOBAR;
2186                 }
2187
2188                 blobs[i] = data_blob_talloc(blobs, NULL, 4 + ret);
2189                 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
2190
2191                 SIVAL(blobs[i].data, 0, 4 + ret);
2192
2193                 if (ret > 0) {
2194                         memcpy(blobs[i].data + 4, data, ret);
2195                         talloc_free(data);
2196                 }
2197         }
2198
2199         return WERR_OK;
2200 }
2201
2202 static WERROR dsdb_syntax_PRESENTATION_ADDRESS_validate_ldb(struct ldb_context *ldb,
2203                                                             const struct dsdb_schema *schema,
2204                                                             const struct dsdb_attribute *attr,
2205                                                             const struct ldb_message_element *in)
2206 {
2207         return dsdb_syntax_UNICODE_validate_ldb(ldb,
2208                                                 schema,
2209                                                 attr,
2210                                                 in);
2211 }
2212
2213 #define OMOBJECTCLASS(val) { .length = sizeof(val) - 1, .data = discard_const_p(uint8_t, val) }
2214
2215 static const struct dsdb_syntax dsdb_syntaxes[] = {
2216         {
2217                 .name                   = "Boolean",
2218                 .ldap_oid               = LDB_SYNTAX_BOOLEAN,
2219                 .oMSyntax               = 1,
2220                 .attributeSyntax_oid    = "2.5.5.8",
2221                 .drsuapi_to_ldb         = dsdb_syntax_BOOL_drsuapi_to_ldb,
2222                 .ldb_to_drsuapi         = dsdb_syntax_BOOL_ldb_to_drsuapi,
2223                 .validate_ldb           = dsdb_syntax_BOOL_validate_ldb,
2224                 .equality               = "booleanMatch",
2225                 .comment                = "Boolean" 
2226         },{
2227                 .name                   = "Integer",
2228                 .ldap_oid               = LDB_SYNTAX_INTEGER,
2229                 .oMSyntax               = 2,
2230                 .attributeSyntax_oid    = "2.5.5.9",
2231                 .drsuapi_to_ldb         = dsdb_syntax_INT32_drsuapi_to_ldb,
2232                 .ldb_to_drsuapi         = dsdb_syntax_INT32_ldb_to_drsuapi,
2233                 .validate_ldb           = dsdb_syntax_INT32_validate_ldb,
2234                 .equality               = "integerMatch",
2235                 .comment                = "Integer",
2236                 .ldb_syntax             = LDB_SYNTAX_SAMBA_INT32
2237         },{
2238                 .name                   = "String(Octet)",
2239                 .ldap_oid               = LDB_SYNTAX_OCTET_STRING,
2240                 .oMSyntax               = 4,
2241                 .attributeSyntax_oid    = "2.5.5.10",
2242                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2243                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2244                 .validate_ldb           = dsdb_syntax_DATA_BLOB_validate_ldb,
2245                 .equality               = "octetStringMatch",
2246                 .comment                = "Octet String",
2247         },{
2248                 .name                   = "String(Sid)",
2249                 .ldap_oid               = LDB_SYNTAX_OCTET_STRING,
2250                 .oMSyntax               = 4,
2251                 .attributeSyntax_oid    = "2.5.5.17",
2252                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2253                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2254                 .validate_ldb           = dsdb_syntax_DATA_BLOB_validate_ldb,
2255                 .equality               = "octetStringMatch",
2256                 .comment                = "Octet String - Security Identifier (SID)",
2257                 .ldb_syntax             = LDB_SYNTAX_SAMBA_SID
2258         },{
2259                 .name                   = "String(Object-Identifier)",
2260                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.38",
2261                 .oMSyntax               = 6,
2262                 .attributeSyntax_oid    = "2.5.5.2",
2263                 .drsuapi_to_ldb         = dsdb_syntax_OID_drsuapi_to_ldb,
2264                 .ldb_to_drsuapi         = dsdb_syntax_OID_ldb_to_drsuapi,
2265                 .validate_ldb           = dsdb_syntax_OID_validate_ldb,
2266                 .equality               = "caseIgnoreMatch", /* Would use "objectIdentifierMatch" but most are ldap attribute/class names */
2267                 .comment                = "OID String",
2268                 .ldb_syntax             = LDB_SYNTAX_DIRECTORY_STRING
2269         },{
2270                 .name                   = "Enumeration",
2271                 .ldap_oid               = LDB_SYNTAX_INTEGER,
2272                 .oMSyntax               = 10,
2273                 .attributeSyntax_oid    = "2.5.5.9",
2274                 .drsuapi_to_ldb         = dsdb_syntax_INT32_drsuapi_to_ldb,
2275                 .ldb_to_drsuapi         = dsdb_syntax_INT32_ldb_to_drsuapi,
2276                 .validate_ldb           = dsdb_syntax_INT32_validate_ldb,
2277                 .ldb_syntax             = LDB_SYNTAX_SAMBA_INT32
2278         },{
2279         /* not used in w2k3 forest */
2280                 .name                   = "String(Numeric)",
2281                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.36",
2282                 .oMSyntax               = 18,
2283                 .attributeSyntax_oid    = "2.5.5.6",
2284                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2285                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2286                 .validate_ldb           = dsdb_syntax_DATA_BLOB_validate_ldb,
2287                 .equality               = "numericStringMatch",
2288                 .substring              = "numericStringSubstringsMatch",
2289                 .comment                = "Numeric String",
2290                 .ldb_syntax             = LDB_SYNTAX_DIRECTORY_STRING,
2291         },{
2292                 .name                   = "String(Printable)",
2293                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.44",
2294                 .oMSyntax               = 19,
2295                 .attributeSyntax_oid    = "2.5.5.5",
2296                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2297                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2298                 .validate_ldb           = dsdb_syntax_DATA_BLOB_validate_ldb,
2299                 .ldb_syntax             = LDB_SYNTAX_OCTET_STRING,
2300         },{
2301                 .name                   = "String(Teletex)",
2302                 .ldap_oid               = "1.2.840.113556.1.4.905",
2303                 .oMSyntax               = 20,
2304                 .attributeSyntax_oid    = "2.5.5.4",
2305                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2306                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2307                 .validate_ldb           = dsdb_syntax_DATA_BLOB_validate_ldb,
2308                 .equality               = "caseIgnoreMatch",
2309                 .substring              = "caseIgnoreSubstringsMatch",
2310                 .comment                = "Case Insensitive String",
2311                 .ldb_syntax             = LDB_SYNTAX_DIRECTORY_STRING,
2312         },{
2313                 .name                   = "String(IA5)",
2314                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.26",
2315                 .oMSyntax               = 22,
2316                 .attributeSyntax_oid    = "2.5.5.5",
2317                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2318                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2319                 .validate_ldb           = dsdb_syntax_DATA_BLOB_validate_ldb,
2320                 .equality               = "caseExactIA5Match",
2321                 .comment                = "Printable String",
2322                 .ldb_syntax             = LDB_SYNTAX_OCTET_STRING,
2323         },{
2324                 .name                   = "String(UTC-Time)",
2325                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.53",
2326                 .oMSyntax               = 23,
2327                 .attributeSyntax_oid    = "2.5.5.11",
2328                 .drsuapi_to_ldb         = dsdb_syntax_NTTIME_UTC_drsuapi_to_ldb,
2329                 .ldb_to_drsuapi         = dsdb_syntax_NTTIME_UTC_ldb_to_drsuapi,
2330                 .validate_ldb           = dsdb_syntax_NTTIME_UTC_validate_ldb,
2331                 .equality               = "generalizedTimeMatch",
2332                 .comment                = "UTC Time",
2333         },{
2334                 .name                   = "String(Generalized-Time)",
2335                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.24",
2336                 .oMSyntax               = 24,
2337                 .attributeSyntax_oid    = "2.5.5.11",
2338                 .drsuapi_to_ldb         = dsdb_syntax_NTTIME_drsuapi_to_ldb,
2339                 .ldb_to_drsuapi         = dsdb_syntax_NTTIME_ldb_to_drsuapi,
2340                 .validate_ldb           = dsdb_syntax_NTTIME_validate_ldb,
2341                 .equality               = "generalizedTimeMatch",
2342                 .comment                = "Generalized Time",
2343                 .ldb_syntax             = LDB_SYNTAX_UTC_TIME,
2344         },{
2345         /* not used in w2k3 schema */
2346                 .name                   = "String(Case Sensitive)",
2347                 .ldap_oid               = "1.2.840.113556.1.4.1362",
2348                 .oMSyntax               = 27,
2349                 .attributeSyntax_oid    = "2.5.5.3",
2350                 .drsuapi_to_ldb         = dsdb_syntax_FOOBAR_drsuapi_to_ldb,
2351                 .ldb_to_drsuapi         = dsdb_syntax_FOOBAR_ldb_to_drsuapi,
2352                 .validate_ldb           = dsdb_syntax_FOOBAR_validate_ldb,
2353         },{
2354                 .name                   = "String(Unicode)",
2355                 .ldap_oid               = LDB_SYNTAX_DIRECTORY_STRING,
2356                 .oMSyntax               = 64,
2357                 .attributeSyntax_oid    = "2.5.5.12",
2358                 .drsuapi_to_ldb         = dsdb_syntax_UNICODE_drsuapi_to_ldb,
2359                 .ldb_to_drsuapi         = dsdb_syntax_UNICODE_ldb_to_drsuapi,
2360                 .validate_ldb           = dsdb_syntax_UNICODE_validate_ldb,
2361                 .equality               = "caseIgnoreMatch",
2362                 .substring              = "caseIgnoreSubstringsMatch",
2363                 .comment                = "Directory String",
2364         },{
2365                 .name                   = "Interval/LargeInteger",
2366                 .ldap_oid               = "1.2.840.113556.1.4.906",
2367                 .oMSyntax               = 65,
2368                 .attributeSyntax_oid    = "2.5.5.16",
2369                 .drsuapi_to_ldb         = dsdb_syntax_INT64_drsuapi_to_ldb,
2370                 .ldb_to_drsuapi         = dsdb_syntax_INT64_ldb_to_drsuapi,
2371                 .validate_ldb           = dsdb_syntax_INT64_validate_ldb,
2372                 .equality               = "integerMatch",
2373                 .comment                = "Large Integer",
2374                 .ldb_syntax             = LDB_SYNTAX_INTEGER,
2375         },{
2376                 .name                   = "String(NT-Sec-Desc)",
2377                 .ldap_oid               = LDB_SYNTAX_SAMBA_SECURITY_DESCRIPTOR,
2378                 .oMSyntax               = 66,
2379                 .attributeSyntax_oid    = "2.5.5.15",
2380                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2381                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2382                 .validate_ldb           = dsdb_syntax_DATA_BLOB_validate_ldb,
2383         },{
2384                 .name                   = "Object(DS-DN)",
2385                 .ldap_oid               = LDB_SYNTAX_DN,
2386                 .oMSyntax               = 127,
2387                 .oMObjectClass          = OMOBJECTCLASS("\x2b\x0c\x02\x87\x73\x1c\x00\x85\x4a"),
2388                 .attributeSyntax_oid    = "2.5.5.1",
2389                 .drsuapi_to_ldb         = dsdb_syntax_DN_drsuapi_to_ldb,
2390                 .ldb_to_drsuapi         = dsdb_syntax_DN_ldb_to_drsuapi,
2391                 .validate_ldb           = dsdb_syntax_DN_validate_ldb,
2392                 .equality               = "distinguishedNameMatch",
2393                 .comment                = "Object(DS-DN) == a DN",
2394         },{
2395                 .name                   = "Object(DN-Binary)",
2396                 .ldap_oid               = DSDB_SYNTAX_BINARY_DN,
2397                 .oMSyntax               = 127,
2398                 .oMObjectClass          = OMOBJECTCLASS("\x2a\x86\x48\x86\xf7\x14\x01\x01\x01\x0b"),
2399                 .attributeSyntax_oid    = "2.5.5.7",
2400                 .drsuapi_to_ldb         = dsdb_syntax_DN_BINARY_drsuapi_to_ldb,
2401                 .ldb_to_drsuapi         = dsdb_syntax_DN_BINARY_ldb_to_drsuapi,
2402                 .validate_ldb           = dsdb_syntax_DN_BINARY_validate_ldb,
2403                 .equality               = "octetStringMatch",
2404                 .comment                = "OctetString: Binary+DN",
2405         },{
2406         /* not used in w2k3 schema, but used in Exchange schema*/
2407                 .name                   = "Object(OR-Name)",
2408                 .ldap_oid               = DSDB_SYNTAX_OR_NAME,
2409                 .oMSyntax               = 127,
2410                 .oMObjectClass          = OMOBJECTCLASS("\x56\x06\x01\x02\x05\x0b\x1D"),
2411                 .attributeSyntax_oid    = "2.5.5.7",
2412                 .drsuapi_to_ldb         = dsdb_syntax_DN_BINARY_drsuapi_to_ldb,
2413                 .ldb_to_drsuapi         = dsdb_syntax_DN_BINARY_ldb_to_drsuapi,
2414                 .validate_ldb           = dsdb_syntax_DN_BINARY_validate_ldb,
2415                 .equality               = "caseIgnoreMatch",
2416                 .ldb_syntax             = LDB_SYNTAX_DN,
2417         },{
2418         /* 
2419          * TODO: verify if DATA_BLOB is correct here...!
2420          *
2421          *       repsFrom and repsTo are the only attributes using
2422          *       this attribute syntax, but they're not replicated... 
2423          */
2424                 .name                   = "Object(Replica-Link)",
2425                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.40",
2426                 .oMSyntax               = 127,
2427                 .oMObjectClass          = OMOBJECTCLASS("\x2a\x86\x48\x86\xf7\x14\x01\x01\x01\x06"),
2428                 .attributeSyntax_oid    = "2.5.5.10",
2429                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2430                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2431                 .validate_ldb           = dsdb_syntax_DATA_BLOB_validate_ldb,
2432         },{
2433                 .name                   = "Object(Presentation-Address)",
2434                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.43",
2435                 .oMSyntax               = 127,
2436                 .oMObjectClass          = OMOBJECTCLASS("\x2b\x0c\x02\x87\x73\x1c\x00\x85\x5c"),
2437                 .attributeSyntax_oid    = "2.5.5.13",
2438                 .drsuapi_to_ldb         = dsdb_syntax_PRESENTATION_ADDRESS_drsuapi_to_ldb,
2439                 .ldb_to_drsuapi         = dsdb_syntax_PRESENTATION_ADDRESS_ldb_to_drsuapi,
2440                 .validate_ldb           = dsdb_syntax_PRESENTATION_ADDRESS_validate_ldb,
2441                 .comment                = "Presentation Address",
2442                 .ldb_syntax             = LDB_SYNTAX_DIRECTORY_STRING,
2443         },{
2444         /* not used in w2k3 schema */
2445                 .name                   = "Object(Access-Point)",
2446                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.2",
2447                 .oMSyntax               = 127,
2448                 .oMObjectClass          = OMOBJECTCLASS("\x2b\x0c\x02\x87\x73\x1c\x00\x85\x3e"),
2449                 .attributeSyntax_oid    = "2.5.5.14",
2450                 .drsuapi_to_ldb         = dsdb_syntax_FOOBAR_drsuapi_to_ldb,
2451                 .ldb_to_drsuapi         = dsdb_syntax_FOOBAR_ldb_to_drsuapi,
2452                 .validate_ldb           = dsdb_syntax_FOOBAR_validate_ldb,
2453                 .ldb_syntax             = LDB_SYNTAX_DIRECTORY_STRING,
2454         },{
2455         /* not used in w2k3 schema */
2456                 .name                   = "Object(DN-String)",
2457                 .ldap_oid               = DSDB_SYNTAX_STRING_DN,
2458                 .oMSyntax               = 127,
2459                 .oMObjectClass          = OMOBJECTCLASS("\x2a\x86\x48\x86\xf7\x14\x01\x01\x01\x0c"),
2460                 .attributeSyntax_oid    = "2.5.5.14",
2461                 .drsuapi_to_ldb         = dsdb_syntax_DN_STRING_drsuapi_to_ldb,
2462                 .ldb_to_drsuapi         = dsdb_syntax_DN_STRING_ldb_to_drsuapi,
2463                 .validate_ldb           = dsdb_syntax_DN_STRING_validate_ldb,
2464                 .equality               = "octetStringMatch",
2465                 .comment                = "OctetString: String+DN",
2466         }
2467 };
2468
2469 const struct dsdb_syntax *find_syntax_map_by_ad_oid(const char *ad_oid) 
2470 {
2471         unsigned int i;
2472         for (i=0; dsdb_syntaxes[i].ldap_oid; i++) {
2473                 if (strcasecmp(ad_oid, dsdb_syntaxes[i].attributeSyntax_oid) == 0) {
2474                         return &dsdb_syntaxes[i];
2475                 }
2476         }
2477         return NULL;
2478 }
2479
2480 const struct dsdb_syntax *find_syntax_map_by_ad_syntax(int oMSyntax) 
2481 {
2482         unsigned int i;
2483         for (i=0; dsdb_syntaxes[i].ldap_oid; i++) {
2484                 if (oMSyntax == dsdb_syntaxes[i].oMSyntax) {
2485                         return &dsdb_syntaxes[i];
2486                 }
2487         }
2488         return NULL;
2489 }
2490
2491 const struct dsdb_syntax *find_syntax_map_by_standard_oid(const char *standard_oid) 
2492 {
2493         unsigned int i;
2494         for (i=0; dsdb_syntaxes[i].ldap_oid; i++) {
2495                 if (strcasecmp(standard_oid, dsdb_syntaxes[i].ldap_oid) == 0) {
2496                         return &dsdb_syntaxes[i];
2497                 }
2498         }
2499         return NULL;
2500 }
2501 const struct dsdb_syntax *dsdb_syntax_for_attribute(const struct dsdb_attribute *attr)
2502 {
2503         unsigned int i;
2504
2505         for (i=0; i < ARRAY_SIZE(dsdb_syntaxes); i++) {
2506                 if (attr->oMSyntax != dsdb_syntaxes[i].oMSyntax) continue;
2507
2508                 if (attr->oMObjectClass.length != dsdb_syntaxes[i].oMObjectClass.length) continue;
2509
2510                 if (attr->oMObjectClass.length) {
2511                         int ret;
2512                         ret = memcmp(attr->oMObjectClass.data,
2513                                      dsdb_syntaxes[i].oMObjectClass.data,
2514                                      attr->oMObjectClass.length);
2515                         if (ret != 0) continue;
2516                 }
2517
2518                 if (strcmp(attr->attributeSyntax_oid, dsdb_syntaxes[i].attributeSyntax_oid) != 0) continue;
2519
2520                 return &dsdb_syntaxes[i];
2521         }
2522
2523         return NULL;
2524 }
2525
2526 WERROR dsdb_attribute_drsuapi_to_ldb(struct ldb_context *ldb, 
2527                                      const struct dsdb_schema *schema,
2528                                      const struct drsuapi_DsReplicaAttribute *in,
2529                                      TALLOC_CTX *mem_ctx,
2530                                      struct ldb_message_element *out)
2531 {
2532         const struct dsdb_attribute *sa;
2533
2534         sa = dsdb_attribute_by_attributeID_id(schema, in->attid);
2535         if (!sa) {
2536                 return WERR_FOOBAR;
2537         }
2538
2539         return sa->syntax->drsuapi_to_ldb(ldb, schema, sa, in, mem_ctx, out);
2540 }
2541
2542 WERROR dsdb_attribute_ldb_to_drsuapi(struct ldb_context *ldb, 
2543                                      const struct dsdb_schema *schema,
2544                                      const struct ldb_message_element *in,
2545                                      TALLOC_CTX *mem_ctx,
2546                                      struct drsuapi_DsReplicaAttribute *out)
2547 {
2548         const struct dsdb_attribute *sa;
2549
2550         sa = dsdb_attribute_by_lDAPDisplayName(schema, in->name);
2551         if (!sa) {
2552                 return WERR_FOOBAR;
2553         }
2554
2555         return sa->syntax->ldb_to_drsuapi(ldb, schema, sa, in, mem_ctx, out);
2556 }
2557
2558 WERROR dsdb_attribute_validate_ldb(struct ldb_context *ldb,
2559                                    const struct dsdb_schema *schema,
2560                                    const struct ldb_message_element *in)
2561 {
2562         const struct dsdb_attribute *sa;
2563
2564         sa = dsdb_attribute_by_lDAPDisplayName(schema, in->name);
2565         if (!sa) {
2566                 return WERR_DS_ATTRIBUTE_TYPE_UNDEFINED;
2567         }
2568
2569         return sa->syntax->validate_ldb(ldb, schema, sa, in);
2570 }