a0eed3d7c0d26295a8e35d8b3e25062ac9fd3efe
[bbaumbach/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_convenience(out->values, 
1356                                                 schema->iconv_convenience, 
1357                                                                         CH_UTF16, CH_UNIX,
1358                                             in->value_ctr.values[i].blob->data,
1359                                             in->value_ctr.values[i].blob->length,
1360                                             (void **)&str, NULL, false)) {
1361                         return WERR_FOOBAR;
1362                 }
1363
1364                 out->values[i] = data_blob_string_const(str);
1365         }
1366
1367         return WERR_OK;
1368 }
1369
1370 static WERROR dsdb_syntax_UNICODE_ldb_to_drsuapi(struct ldb_context *ldb, 
1371                                                  const struct dsdb_schema *schema,
1372                                                  const struct dsdb_attribute *attr,
1373                                                  const struct ldb_message_element *in,
1374                                                  TALLOC_CTX *mem_ctx,
1375                                                  struct drsuapi_DsReplicaAttribute *out)
1376 {
1377         unsigned int i;
1378         DATA_BLOB *blobs;
1379
1380         if (attr->attributeID_id == 0xFFFFFFFF) {
1381                 return WERR_FOOBAR;
1382         }
1383
1384         out->attid                      = attr->attributeID_id;
1385         out->value_ctr.num_values       = in->num_values;
1386         out->value_ctr.values           = talloc_array(mem_ctx,
1387                                                        struct drsuapi_DsAttributeValue,
1388                                                        in->num_values);
1389         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1390
1391         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1392         W_ERROR_HAVE_NO_MEMORY(blobs);
1393
1394         for (i=0; i < in->num_values; i++) {
1395                 out->value_ctr.values[i].blob   = &blobs[i];
1396
1397                 if (!convert_string_talloc_convenience(blobs,
1398                         schema->iconv_convenience, CH_UNIX, CH_UTF16,
1399                         in->values[i].data, in->values[i].length,
1400                         (void **)&blobs[i].data, &blobs[i].length, false)) {
1401                                 return WERR_FOOBAR;
1402                 }
1403         }
1404
1405         return WERR_OK;
1406 }
1407
1408 static WERROR dsdb_syntax_UNICODE_validate_one_val(struct ldb_context *ldb,
1409                                                    const struct dsdb_schema *schema,
1410                                                    const struct dsdb_attribute *attr,
1411                                                    const struct ldb_val *val)
1412 {
1413         void *dst = NULL;
1414         size_t size;
1415         bool ok;
1416
1417         if (attr->attributeID_id == 0xFFFFFFFF) {
1418                 return WERR_FOOBAR;
1419         }
1420
1421         ok = convert_string_talloc_convenience(ldb,
1422                                                schema->iconv_convenience,
1423                                                CH_UNIX, CH_UTF16,
1424                                                val->data,
1425                                                val->length,
1426                                                (void **)&dst,
1427                                                &size, false);
1428         TALLOC_FREE(dst);
1429         if (!ok) {
1430                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1431         }
1432
1433         if (attr->rangeLower) {
1434                 if ((size/2) < *attr->rangeLower) {
1435                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1436                 }
1437         }
1438
1439         if (attr->rangeUpper) {
1440                 if ((size/2) > *attr->rangeUpper) {
1441                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1442                 }
1443         }
1444
1445         return WERR_OK;
1446 }
1447
1448 static WERROR dsdb_syntax_UNICODE_validate_ldb(struct ldb_context *ldb,
1449                                                const struct dsdb_schema *schema,
1450                                                const struct dsdb_attribute *attr,
1451                                                const struct ldb_message_element *in)
1452 {
1453         WERROR status;
1454         unsigned int i;
1455
1456         if (attr->attributeID_id == 0xFFFFFFFF) {
1457                 return WERR_FOOBAR;
1458         }
1459
1460         for (i=0; i < in->num_values; i++) {
1461                 if (in->values[i].length == 0) {
1462                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1463                 }
1464
1465                 status = dsdb_syntax_UNICODE_validate_one_val(ldb,
1466                                                               schema,
1467                                                               attr,
1468                                                               &in->values[i]);
1469                 if (!W_ERROR_IS_OK(status)) {
1470                         return status;
1471                 }
1472         }
1473
1474         return WERR_OK;
1475 }
1476
1477 WERROR dsdb_syntax_one_DN_drsuapi_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, 
1478                                          const struct dsdb_syntax *syntax, 
1479                                          struct smb_iconv_convenience *iconv_convenience,
1480                                          const DATA_BLOB *in, DATA_BLOB *out)
1481 {
1482         struct drsuapi_DsReplicaObjectIdentifier3 id3;
1483         enum ndr_err_code ndr_err;
1484         DATA_BLOB guid_blob;
1485         struct ldb_dn *dn;
1486         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1487         int ret;
1488         NTSTATUS status;
1489
1490         if (!tmp_ctx) {
1491                 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1492         }
1493         
1494         if (in == NULL) {
1495                 talloc_free(tmp_ctx);
1496                 return WERR_FOOBAR;
1497         }
1498         
1499         if (in->length == 0) {
1500                 talloc_free(tmp_ctx);
1501                 return WERR_FOOBAR;
1502         }
1503         
1504         
1505         /* windows sometimes sends an extra two pad bytes here */
1506         ndr_err = ndr_pull_struct_blob(in,
1507                                        tmp_ctx, iconv_convenience, &id3,
1508                                        (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3);
1509         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1510                 status = ndr_map_error2ntstatus(ndr_err);
1511                 talloc_free(tmp_ctx);
1512                 return ntstatus_to_werror(status);
1513         }
1514         
1515         dn = ldb_dn_new(tmp_ctx, ldb, id3.dn);
1516         if (!dn) {
1517                 talloc_free(tmp_ctx);
1518                 /* If this fails, it must be out of memory, as it does not do much parsing */
1519                 W_ERROR_HAVE_NO_MEMORY(dn);
1520         }
1521
1522         if (!GUID_all_zero(&id3.guid)) {
1523                 status = GUID_to_ndr_blob(&id3.guid, tmp_ctx, &guid_blob);
1524                 if (!NT_STATUS_IS_OK(status)) {
1525                         talloc_free(tmp_ctx);
1526                         return ntstatus_to_werror(status);
1527                 }
1528         
1529                 ret = ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
1530                 if (ret != LDB_SUCCESS) {
1531                         talloc_free(tmp_ctx);
1532                         return WERR_FOOBAR;
1533                 }
1534                 talloc_free(guid_blob.data);
1535         }
1536         
1537         if (id3.__ndr_size_sid) {
1538                 DATA_BLOB sid_blob;
1539                 ndr_err = ndr_push_struct_blob(&sid_blob, tmp_ctx, iconv_convenience, &id3.sid,
1540                                                (ndr_push_flags_fn_t)ndr_push_dom_sid);
1541                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1542                         status = ndr_map_error2ntstatus(ndr_err);
1543                         talloc_free(tmp_ctx);
1544                         return ntstatus_to_werror(status);
1545                 }
1546                 
1547                 ret = ldb_dn_set_extended_component(dn, "SID", &sid_blob);
1548                 if (ret != LDB_SUCCESS) {
1549                         talloc_free(tmp_ctx);
1550                         return WERR_FOOBAR;
1551                 }
1552         }
1553         
1554         *out = data_blob_string_const(ldb_dn_get_extended_linearized(mem_ctx, dn, 1));
1555         talloc_free(tmp_ctx);
1556         return WERR_OK;
1557 }
1558
1559 static WERROR dsdb_syntax_DN_drsuapi_to_ldb(struct ldb_context *ldb, 
1560                                             const struct dsdb_schema *schema,
1561                                             const struct dsdb_attribute *attr,
1562                                             const struct drsuapi_DsReplicaAttribute *in,
1563                                             TALLOC_CTX *mem_ctx,
1564                                             struct ldb_message_element *out)
1565 {
1566         unsigned int i;
1567
1568         out->flags      = 0;
1569         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
1570         W_ERROR_HAVE_NO_MEMORY(out->name);
1571
1572         out->num_values = in->value_ctr.num_values;
1573         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
1574         W_ERROR_HAVE_NO_MEMORY(out->values);
1575
1576         for (i=0; i < out->num_values; i++) {
1577                 WERROR status = dsdb_syntax_one_DN_drsuapi_to_ldb(out->values, ldb, attr->syntax, 
1578                                                                   schema->iconv_convenience, 
1579                                                                   in->value_ctr.values[i].blob, 
1580                                                                   &out->values[i]);
1581                 if (!W_ERROR_IS_OK(status)) {
1582                         return status;
1583                 }
1584                                                   
1585         }
1586
1587         return WERR_OK;
1588 }
1589
1590 static WERROR dsdb_syntax_DN_ldb_to_drsuapi(struct ldb_context *ldb, 
1591                                             const struct dsdb_schema *schema,
1592                                             const struct dsdb_attribute *attr,
1593                                             const struct ldb_message_element *in,
1594                                             TALLOC_CTX *mem_ctx,
1595                                             struct drsuapi_DsReplicaAttribute *out)
1596 {
1597         unsigned int i;
1598         DATA_BLOB *blobs;
1599
1600         if (attr->attributeID_id == 0xFFFFFFFF) {
1601                 return WERR_FOOBAR;
1602         }
1603
1604         out->attid                      = attr->attributeID_id;
1605         out->value_ctr.num_values       = in->num_values;
1606         out->value_ctr.values           = talloc_array(mem_ctx,
1607                                                        struct drsuapi_DsAttributeValue,
1608                                                        in->num_values);
1609         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1610
1611         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1612         W_ERROR_HAVE_NO_MEMORY(blobs);
1613
1614         for (i=0; i < in->num_values; i++) {
1615                 struct drsuapi_DsReplicaObjectIdentifier3 id3;
1616                 enum ndr_err_code ndr_err;
1617                 struct ldb_dn *dn;
1618                 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1619                 NTSTATUS status;
1620
1621                 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1622
1623                 out->value_ctr.values[i].blob   = &blobs[i];
1624
1625                 dn = ldb_dn_from_ldb_val(tmp_ctx, ldb, &in->values[i]);
1626
1627                 W_ERROR_HAVE_NO_MEMORY(dn);
1628
1629                 ZERO_STRUCT(id3);
1630
1631                 status = dsdb_get_extended_dn_guid(dn, &id3.guid, "GUID");
1632                 if (!NT_STATUS_IS_OK(status) &&
1633                     !NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1634                         talloc_free(tmp_ctx);
1635                         return ntstatus_to_werror(status);
1636                 }
1637
1638                 status = dsdb_get_extended_dn_sid(dn, &id3.sid, "SID");
1639                 if (!NT_STATUS_IS_OK(status) &&
1640                     !NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1641                         talloc_free(tmp_ctx);
1642                         return ntstatus_to_werror(status);
1643                 }
1644
1645                 id3.dn = ldb_dn_get_linearized(dn);
1646
1647                 ndr_err = ndr_push_struct_blob(&blobs[i], blobs, schema->iconv_convenience, &id3, (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
1648                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1649                         status = ndr_map_error2ntstatus(ndr_err);
1650                         talloc_free(tmp_ctx);
1651                         return ntstatus_to_werror(status);
1652                 }
1653                 talloc_free(tmp_ctx);
1654         }
1655
1656         return WERR_OK;
1657 }
1658
1659 static WERROR dsdb_syntax_DN_validate_one_val(struct ldb_context *ldb,
1660                                               const struct dsdb_schema *schema,
1661                                               const struct dsdb_attribute *attr,
1662                                               const struct ldb_val *val,
1663                                               TALLOC_CTX *mem_ctx,
1664                                               struct dsdb_dn **_dsdb_dn)
1665 {
1666         static const char * const extended_list[] = { "GUID", "SID", NULL };
1667         enum ndr_err_code ndr_err;
1668         struct GUID guid;
1669         struct dom_sid sid;
1670         const DATA_BLOB *sid_blob;
1671         struct dsdb_dn *dsdb_dn;
1672         struct ldb_dn *dn;
1673         char *dn_str;
1674         struct ldb_dn *dn2;
1675         char *dn2_str;
1676         int num_components;
1677         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1678         NTSTATUS status;
1679
1680         W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1681
1682         if (attr->attributeID_id == 0xFFFFFFFF) {
1683                 return WERR_FOOBAR;
1684         }
1685
1686         dsdb_dn = dsdb_dn_parse(tmp_ctx, ldb, val,
1687                                 attr->syntax->ldap_oid);
1688         if (!dsdb_dn) {
1689                 talloc_free(tmp_ctx);
1690                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1691         }
1692         dn = dsdb_dn->dn;
1693
1694         dn2 = ldb_dn_copy(tmp_ctx, dn);
1695         if (dn == NULL) {
1696                 talloc_free(tmp_ctx);
1697                 return WERR_NOMEM;
1698         }
1699
1700         num_components = ldb_dn_get_comp_num(dn);
1701
1702         status = dsdb_get_extended_dn_guid(dn, &guid, "GUID");
1703         if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1704                 num_components++;
1705         } else if (!NT_STATUS_IS_OK(status)) {
1706                 talloc_free(tmp_ctx);
1707                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1708         }
1709
1710         sid_blob = ldb_dn_get_extended_component(dn, "SID");
1711         if (sid_blob) {
1712                 num_components++;
1713                 ndr_err = ndr_pull_struct_blob_all(sid_blob,
1714                                                    tmp_ctx,
1715                                                    schema->iconv_convenience,
1716                                                    &sid,
1717                                                    (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
1718                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1719                         talloc_free(tmp_ctx);
1720                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1721                 }
1722         }
1723
1724         /* Do not allow links to the RootDSE */
1725         if (num_components == 0) {
1726                 talloc_free(tmp_ctx);
1727                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1728         }
1729
1730         /*
1731          * We need to check that only "GUID" and "SID" are
1732          * specified as extended components, we do that
1733          * by comparing the dn's after removing all components
1734          * from one dn and only the allowed subset from the other
1735          * one.
1736          */
1737         ldb_dn_extended_filter(dn, extended_list);
1738
1739         dn_str = ldb_dn_get_extended_linearized(tmp_ctx, dn, 0);
1740         if (dn_str == NULL) {
1741                 talloc_free(tmp_ctx);
1742                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1743         }
1744         dn2_str = ldb_dn_get_extended_linearized(tmp_ctx, dn2, 0);
1745         if (dn2_str == NULL) {
1746                 talloc_free(tmp_ctx);
1747                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1748         }
1749
1750         if (strcmp(dn_str, dn2_str) != 0) {
1751                 talloc_free(tmp_ctx);
1752                 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1753         }
1754
1755         *_dsdb_dn = talloc_move(mem_ctx, &dsdb_dn);
1756         talloc_free(tmp_ctx);
1757         return WERR_OK;
1758 }
1759
1760 static WERROR dsdb_syntax_DN_validate_ldb(struct ldb_context *ldb,
1761                                           const struct dsdb_schema *schema,
1762                                           const struct dsdb_attribute *attr,
1763                                           const struct ldb_message_element *in)
1764 {
1765         unsigned int i;
1766
1767         if (attr->attributeID_id == 0xFFFFFFFF) {
1768                 return WERR_FOOBAR;
1769         }
1770
1771         for (i=0; i < in->num_values; i++) {
1772                 WERROR status;
1773                 struct dsdb_dn *dsdb_dn;
1774                 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
1775                 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1776
1777                 status = dsdb_syntax_DN_validate_one_val(ldb,
1778                                                          schema,
1779                                                          attr,
1780                                                          &in->values[i],
1781                                                          tmp_ctx, &dsdb_dn);
1782                 if (!W_ERROR_IS_OK(status)) {
1783                         talloc_free(tmp_ctx);
1784                         return status;
1785                 }
1786
1787                 if (dsdb_dn->dn_format != DSDB_NORMAL_DN) {
1788                         talloc_free(tmp_ctx);
1789                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1790                 }
1791
1792                 talloc_free(tmp_ctx);
1793         }
1794
1795         return WERR_OK;
1796 }
1797
1798 static WERROR dsdb_syntax_DN_BINARY_drsuapi_to_ldb(struct ldb_context *ldb, 
1799                                                    const struct dsdb_schema *schema,
1800                                                    const struct dsdb_attribute *attr,
1801                                                    const struct drsuapi_DsReplicaAttribute *in,
1802                                                    TALLOC_CTX *mem_ctx,
1803                                                    struct ldb_message_element *out)
1804 {
1805         unsigned int i;
1806         int ret;
1807
1808         out->flags      = 0;
1809         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
1810         W_ERROR_HAVE_NO_MEMORY(out->name);
1811
1812         out->num_values = in->value_ctr.num_values;
1813         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
1814         W_ERROR_HAVE_NO_MEMORY(out->values);
1815
1816         for (i=0; i < out->num_values; i++) {
1817                 struct drsuapi_DsReplicaObjectIdentifier3Binary id3;
1818                 enum ndr_err_code ndr_err;
1819                 DATA_BLOB guid_blob;
1820                 struct ldb_dn *dn;
1821                 struct dsdb_dn *dsdb_dn;
1822                 NTSTATUS status;
1823                 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1824                 if (!tmp_ctx) {
1825                         W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1826                 }
1827
1828                 if (in->value_ctr.values[i].blob == NULL) {
1829                         talloc_free(tmp_ctx);
1830                         return WERR_FOOBAR;
1831                 }
1832
1833                 if (in->value_ctr.values[i].blob->length == 0) {
1834                         talloc_free(tmp_ctx);
1835                         return WERR_FOOBAR;
1836                 }
1837
1838                 
1839                 /* windows sometimes sends an extra two pad bytes here */
1840                 ndr_err = ndr_pull_struct_blob(in->value_ctr.values[i].blob,
1841                                                tmp_ctx, schema->iconv_convenience, &id3,
1842                                                (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3Binary);
1843                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1844                         status = ndr_map_error2ntstatus(ndr_err);
1845                         talloc_free(tmp_ctx);
1846                         return ntstatus_to_werror(status);
1847                 }
1848
1849                 dn = ldb_dn_new(tmp_ctx, ldb, id3.dn);
1850                 if (!dn) {
1851                         talloc_free(tmp_ctx);
1852                         /* If this fails, it must be out of memory, as it does not do much parsing */
1853                         W_ERROR_HAVE_NO_MEMORY(dn);
1854                 }
1855
1856                 status = GUID_to_ndr_blob(&id3.guid, tmp_ctx, &guid_blob);
1857                 if (!NT_STATUS_IS_OK(status)) {
1858                         talloc_free(tmp_ctx);
1859                         return ntstatus_to_werror(status);
1860                 }
1861
1862                 ret = ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
1863                 if (ret != LDB_SUCCESS) {
1864                         talloc_free(tmp_ctx);
1865                         return WERR_FOOBAR;
1866                 }
1867
1868                 talloc_free(guid_blob.data);
1869
1870                 if (id3.__ndr_size_sid) {
1871                         DATA_BLOB sid_blob;
1872                         ndr_err = ndr_push_struct_blob(&sid_blob, tmp_ctx, schema->iconv_convenience, &id3.sid,
1873                                                        (ndr_push_flags_fn_t)ndr_push_dom_sid);
1874                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1875                                 status = ndr_map_error2ntstatus(ndr_err);
1876                                 talloc_free(tmp_ctx);
1877                                 return ntstatus_to_werror(status);
1878                         }
1879
1880                         ret = ldb_dn_set_extended_component(dn, "SID", &sid_blob);
1881                         if (ret != LDB_SUCCESS) {
1882                                 talloc_free(tmp_ctx);
1883                                 return WERR_FOOBAR;
1884                         }
1885                 }
1886
1887                 /* set binary stuff */
1888                 dsdb_dn = dsdb_dn_construct(tmp_ctx, dn, id3.binary, attr->syntax->ldap_oid);
1889                 if (!dsdb_dn) {
1890                         /* If this fails, it must be out of memory, we know the ldap_oid is valid */
1891                         talloc_free(tmp_ctx);
1892                         W_ERROR_HAVE_NO_MEMORY(dsdb_dn);
1893                 }
1894                 out->values[i] = data_blob_string_const(dsdb_dn_get_extended_linearized(out->values, dsdb_dn, 1));
1895                 talloc_free(tmp_ctx);
1896         }
1897
1898         return WERR_OK;
1899 }
1900
1901 static WERROR dsdb_syntax_DN_BINARY_ldb_to_drsuapi(struct ldb_context *ldb, 
1902                                                    const struct dsdb_schema *schema,
1903                                                    const struct dsdb_attribute *attr,
1904                                                    const struct ldb_message_element *in,
1905                                                    TALLOC_CTX *mem_ctx,
1906                                                    struct drsuapi_DsReplicaAttribute *out)
1907 {
1908         unsigned int i;
1909         DATA_BLOB *blobs;
1910
1911         if (attr->attributeID_id == 0xFFFFFFFF) {
1912                 return WERR_FOOBAR;
1913         }
1914
1915         out->attid                      = attr->attributeID_id;
1916         out->value_ctr.num_values       = in->num_values;
1917         out->value_ctr.values           = talloc_array(mem_ctx,
1918                                                        struct drsuapi_DsAttributeValue,
1919                                                        in->num_values);
1920         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1921
1922         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1923         W_ERROR_HAVE_NO_MEMORY(blobs);
1924
1925         for (i=0; i < in->num_values; i++) {
1926                 struct drsuapi_DsReplicaObjectIdentifier3Binary id3;
1927                 enum ndr_err_code ndr_err;
1928                 const DATA_BLOB *sid_blob;
1929                 struct dsdb_dn *dsdb_dn;
1930                 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1931                 NTSTATUS status;
1932
1933                 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1934
1935                 out->value_ctr.values[i].blob   = &blobs[i];
1936
1937                 dsdb_dn = dsdb_dn_parse(tmp_ctx, ldb, &in->values[i], attr->syntax->ldap_oid);
1938
1939                 if (!dsdb_dn) {
1940                         talloc_free(tmp_ctx);
1941                         return ntstatus_to_werror(NT_STATUS_INVALID_PARAMETER);
1942                 }
1943
1944                 ZERO_STRUCT(id3);
1945
1946                 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &id3.guid, "GUID");
1947                 if (!NT_STATUS_IS_OK(status) &&
1948                     !NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1949                         talloc_free(tmp_ctx);
1950                         return ntstatus_to_werror(status);
1951                 }
1952
1953                 sid_blob = ldb_dn_get_extended_component(dsdb_dn->dn, "SID");
1954                 if (sid_blob) {
1955                         
1956                         ndr_err = ndr_pull_struct_blob_all(sid_blob, 
1957                                                            tmp_ctx, schema->iconv_convenience, &id3.sid,
1958                                                            (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
1959                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1960                                 status = ndr_map_error2ntstatus(ndr_err);
1961                                 talloc_free(tmp_ctx);
1962                                 return ntstatus_to_werror(status);
1963                         }
1964                 }
1965
1966                 id3.dn = ldb_dn_get_linearized(dsdb_dn->dn);
1967
1968                 /* get binary stuff */
1969                 id3.binary = dsdb_dn->extra_part;
1970
1971                 ndr_err = ndr_push_struct_blob(&blobs[i], blobs, schema->iconv_convenience, &id3, (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3Binary);
1972                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1973                         status = ndr_map_error2ntstatus(ndr_err);
1974                         talloc_free(tmp_ctx);
1975                         return ntstatus_to_werror(status);
1976                 }
1977                 talloc_free(tmp_ctx);
1978         }
1979
1980         return WERR_OK;
1981 }
1982
1983 static WERROR dsdb_syntax_DN_BINARY_validate_ldb(struct ldb_context *ldb,
1984                                                  const struct dsdb_schema *schema,
1985                                                  const struct dsdb_attribute *attr,
1986                                                  const struct ldb_message_element *in)
1987 {
1988         unsigned int i;
1989
1990         if (attr->attributeID_id == 0xFFFFFFFF) {
1991                 return WERR_FOOBAR;
1992         }
1993
1994         for (i=0; i < in->num_values; i++) {
1995                 WERROR status;
1996                 struct dsdb_dn *dsdb_dn;
1997                 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
1998                 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1999
2000                 status = dsdb_syntax_DN_validate_one_val(ldb,
2001                                                          schema,
2002                                                          attr,
2003                                                          &in->values[i],
2004                                                          tmp_ctx, &dsdb_dn);
2005                 if (!W_ERROR_IS_OK(status)) {
2006                         talloc_free(tmp_ctx);
2007                         return status;
2008                 }
2009
2010                 if (dsdb_dn->dn_format != DSDB_BINARY_DN) {
2011                         talloc_free(tmp_ctx);
2012                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
2013                 }
2014
2015                 status = dsdb_syntax_DATA_BLOB_validate_one_val(ldb,
2016                                                                 schema,
2017                                                                 attr,
2018                                                                 &dsdb_dn->extra_part);
2019                 if (!W_ERROR_IS_OK(status)) {
2020                         talloc_free(tmp_ctx);
2021                         return status;
2022                 }
2023
2024                 talloc_free(tmp_ctx);
2025         }
2026
2027         return WERR_OK;
2028 }
2029
2030 static WERROR dsdb_syntax_DN_STRING_drsuapi_to_ldb(struct ldb_context *ldb,
2031                                                    const struct dsdb_schema *schema,
2032                                                    const struct dsdb_attribute *attr,
2033                                                    const struct drsuapi_DsReplicaAttribute *in,
2034                                                    TALLOC_CTX *mem_ctx,
2035                                                    struct ldb_message_element *out)
2036 {
2037         return dsdb_syntax_DN_BINARY_drsuapi_to_ldb(ldb,
2038                                                     schema,
2039                                                     attr,
2040                                                     in,
2041                                                     mem_ctx,
2042                                                     out);
2043 }
2044
2045 static WERROR dsdb_syntax_DN_STRING_ldb_to_drsuapi(struct ldb_context *ldb,
2046                                                    const struct dsdb_schema *schema,
2047                                                    const struct dsdb_attribute *attr,
2048                                                    const struct ldb_message_element *in,
2049                                                    TALLOC_CTX *mem_ctx,
2050                                                    struct drsuapi_DsReplicaAttribute *out)
2051 {
2052         return dsdb_syntax_DN_BINARY_ldb_to_drsuapi(ldb,
2053                                                     schema,
2054                                                     attr,
2055                                                     in,
2056                                                     mem_ctx,
2057                                                     out);
2058 }
2059
2060 static WERROR dsdb_syntax_DN_STRING_validate_ldb(struct ldb_context *ldb,
2061                                                  const struct dsdb_schema *schema,
2062                                                  const struct dsdb_attribute *attr,
2063                                                  const struct ldb_message_element *in)
2064 {
2065         unsigned int i;
2066
2067         if (attr->attributeID_id == 0xFFFFFFFF) {
2068                 return WERR_FOOBAR;
2069         }
2070
2071         for (i=0; i < in->num_values; i++) {
2072                 WERROR status;
2073                 struct dsdb_dn *dsdb_dn;
2074                 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
2075                 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
2076
2077                 status = dsdb_syntax_DN_validate_one_val(ldb,
2078                                                          schema,
2079                                                          attr,
2080                                                          &in->values[i],
2081                                                          tmp_ctx, &dsdb_dn);
2082                 if (!W_ERROR_IS_OK(status)) {
2083                         talloc_free(tmp_ctx);
2084                         return status;
2085                 }
2086
2087                 if (dsdb_dn->dn_format != DSDB_STRING_DN) {
2088                         talloc_free(tmp_ctx);
2089                         return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
2090                 }
2091
2092                 status = dsdb_syntax_UNICODE_validate_one_val(ldb,
2093                                                               schema,
2094                                                               attr,
2095                                                               &dsdb_dn->extra_part);
2096                 if (!W_ERROR_IS_OK(status)) {
2097                         talloc_free(tmp_ctx);
2098                         return status;
2099                 }
2100
2101                 talloc_free(tmp_ctx);
2102         }
2103
2104         return WERR_OK;
2105 }
2106
2107 static WERROR dsdb_syntax_PRESENTATION_ADDRESS_drsuapi_to_ldb(struct ldb_context *ldb, 
2108                                                               const struct dsdb_schema *schema,
2109                                                               const struct dsdb_attribute *attr,
2110                                                               const struct drsuapi_DsReplicaAttribute *in,
2111                                                               TALLOC_CTX *mem_ctx,
2112                                                               struct ldb_message_element *out)
2113 {
2114         unsigned int i;
2115
2116         out->flags      = 0;
2117         out->name       = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
2118         W_ERROR_HAVE_NO_MEMORY(out->name);
2119
2120         out->num_values = in->value_ctr.num_values;
2121         out->values     = talloc_array(mem_ctx, struct ldb_val, out->num_values);
2122         W_ERROR_HAVE_NO_MEMORY(out->values);
2123
2124         for (i=0; i < out->num_values; i++) {
2125                 size_t len;
2126                 char *str;
2127
2128                 if (in->value_ctr.values[i].blob == NULL) {
2129                         return WERR_FOOBAR;
2130                 }
2131
2132                 if (in->value_ctr.values[i].blob->length < 4) {
2133                         return WERR_FOOBAR;
2134                 }
2135
2136                 len = IVAL(in->value_ctr.values[i].blob->data, 0);
2137
2138                 if (len != in->value_ctr.values[i].blob->length) {
2139                         return WERR_FOOBAR;
2140                 }
2141
2142                 if (!convert_string_talloc_convenience(out->values, schema->iconv_convenience, CH_UTF16, CH_UNIX,
2143                                             in->value_ctr.values[i].blob->data+4,
2144                                             in->value_ctr.values[i].blob->length-4,
2145                                             (void **)&str, NULL, false)) {
2146                         return WERR_FOOBAR;
2147                 }
2148
2149                 out->values[i] = data_blob_string_const(str);
2150         }
2151
2152         return WERR_OK;
2153 }
2154
2155 static WERROR dsdb_syntax_PRESENTATION_ADDRESS_ldb_to_drsuapi(struct ldb_context *ldb, 
2156                                                               const struct dsdb_schema *schema,
2157                                                               const struct dsdb_attribute *attr,
2158                                                               const struct ldb_message_element *in,
2159                                                               TALLOC_CTX *mem_ctx,
2160                                                               struct drsuapi_DsReplicaAttribute *out)
2161 {
2162         unsigned int i;
2163         DATA_BLOB *blobs;
2164
2165         if (attr->attributeID_id == 0xFFFFFFFF) {
2166                 return WERR_FOOBAR;
2167         }
2168
2169         out->attid                      = attr->attributeID_id;
2170         out->value_ctr.num_values       = in->num_values;
2171         out->value_ctr.values           = talloc_array(mem_ctx,
2172                                                        struct drsuapi_DsAttributeValue,
2173                                                        in->num_values);
2174         W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
2175
2176         blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
2177         W_ERROR_HAVE_NO_MEMORY(blobs);
2178
2179         for (i=0; i < in->num_values; i++) {
2180                 uint8_t *data;
2181                 size_t ret;
2182
2183                 out->value_ctr.values[i].blob   = &blobs[i];
2184
2185                 if (!convert_string_talloc_convenience(blobs, schema->iconv_convenience, CH_UNIX, CH_UTF16,
2186                                             in->values[i].data,
2187                                             in->values[i].length,
2188                                             (void **)&data, &ret, false)) {
2189                         return WERR_FOOBAR;
2190                 }
2191
2192                 blobs[i] = data_blob_talloc(blobs, NULL, 4 + ret);
2193                 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
2194
2195                 SIVAL(blobs[i].data, 0, 4 + ret);
2196
2197                 if (ret > 0) {
2198                         memcpy(blobs[i].data + 4, data, ret);
2199                         talloc_free(data);
2200                 }
2201         }
2202
2203         return WERR_OK;
2204 }
2205
2206 static WERROR dsdb_syntax_PRESENTATION_ADDRESS_validate_ldb(struct ldb_context *ldb,
2207                                                             const struct dsdb_schema *schema,
2208                                                             const struct dsdb_attribute *attr,
2209                                                             const struct ldb_message_element *in)
2210 {
2211         return dsdb_syntax_UNICODE_validate_ldb(ldb,
2212                                                 schema,
2213                                                 attr,
2214                                                 in);
2215 }
2216
2217 #define OMOBJECTCLASS(val) { .length = sizeof(val) - 1, .data = discard_const_p(uint8_t, val) }
2218
2219 static const struct dsdb_syntax dsdb_syntaxes[] = {
2220         {
2221                 .name                   = "Boolean",
2222                 .ldap_oid               = LDB_SYNTAX_BOOLEAN,
2223                 .oMSyntax               = 1,
2224                 .attributeSyntax_oid    = "2.5.5.8",
2225                 .drsuapi_to_ldb         = dsdb_syntax_BOOL_drsuapi_to_ldb,
2226                 .ldb_to_drsuapi         = dsdb_syntax_BOOL_ldb_to_drsuapi,
2227                 .validate_ldb           = dsdb_syntax_BOOL_validate_ldb,
2228                 .equality               = "booleanMatch",
2229                 .comment                = "Boolean" 
2230         },{
2231                 .name                   = "Integer",
2232                 .ldap_oid               = LDB_SYNTAX_INTEGER,
2233                 .oMSyntax               = 2,
2234                 .attributeSyntax_oid    = "2.5.5.9",
2235                 .drsuapi_to_ldb         = dsdb_syntax_INT32_drsuapi_to_ldb,
2236                 .ldb_to_drsuapi         = dsdb_syntax_INT32_ldb_to_drsuapi,
2237                 .validate_ldb           = dsdb_syntax_INT32_validate_ldb,
2238                 .equality               = "integerMatch",
2239                 .comment                = "Integer",
2240                 .ldb_syntax             = LDB_SYNTAX_SAMBA_INT32
2241         },{
2242                 .name                   = "String(Octet)",
2243                 .ldap_oid               = LDB_SYNTAX_OCTET_STRING,
2244                 .oMSyntax               = 4,
2245                 .attributeSyntax_oid    = "2.5.5.10",
2246                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2247                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2248                 .validate_ldb           = dsdb_syntax_DATA_BLOB_validate_ldb,
2249                 .equality               = "octetStringMatch",
2250                 .comment                = "Octet String",
2251         },{
2252                 .name                   = "String(Sid)",
2253                 .ldap_oid               = LDB_SYNTAX_OCTET_STRING,
2254                 .oMSyntax               = 4,
2255                 .attributeSyntax_oid    = "2.5.5.17",
2256                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2257                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2258                 .validate_ldb           = dsdb_syntax_DATA_BLOB_validate_ldb,
2259                 .equality               = "octetStringMatch",
2260                 .comment                = "Octet String - Security Identifier (SID)",
2261                 .ldb_syntax             = LDB_SYNTAX_SAMBA_SID
2262         },{
2263                 .name                   = "String(Object-Identifier)",
2264                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.38",
2265                 .oMSyntax               = 6,
2266                 .attributeSyntax_oid    = "2.5.5.2",
2267                 .drsuapi_to_ldb         = dsdb_syntax_OID_drsuapi_to_ldb,
2268                 .ldb_to_drsuapi         = dsdb_syntax_OID_ldb_to_drsuapi,
2269                 .validate_ldb           = dsdb_syntax_OID_validate_ldb,
2270                 .equality               = "caseIgnoreMatch", /* Would use "objectIdentifierMatch" but most are ldap attribute/class names */
2271                 .comment                = "OID String",
2272                 .ldb_syntax             = LDB_SYNTAX_DIRECTORY_STRING
2273         },{
2274                 .name                   = "Enumeration",
2275                 .ldap_oid               = LDB_SYNTAX_INTEGER,
2276                 .oMSyntax               = 10,
2277                 .attributeSyntax_oid    = "2.5.5.9",
2278                 .drsuapi_to_ldb         = dsdb_syntax_INT32_drsuapi_to_ldb,
2279                 .ldb_to_drsuapi         = dsdb_syntax_INT32_ldb_to_drsuapi,
2280                 .validate_ldb           = dsdb_syntax_INT32_validate_ldb,
2281                 .ldb_syntax             = LDB_SYNTAX_SAMBA_INT32
2282         },{
2283         /* not used in w2k3 forest */
2284                 .name                   = "String(Numeric)",
2285                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.36",
2286                 .oMSyntax               = 18,
2287                 .attributeSyntax_oid    = "2.5.5.6",
2288                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2289                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2290                 .validate_ldb           = dsdb_syntax_DATA_BLOB_validate_ldb,
2291                 .equality               = "numericStringMatch",
2292                 .substring              = "numericStringSubstringsMatch",
2293                 .comment                = "Numeric String",
2294                 .ldb_syntax             = LDB_SYNTAX_DIRECTORY_STRING,
2295         },{
2296                 .name                   = "String(Printable)",
2297                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.44",
2298                 .oMSyntax               = 19,
2299                 .attributeSyntax_oid    = "2.5.5.5",
2300                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2301                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2302                 .validate_ldb           = dsdb_syntax_DATA_BLOB_validate_ldb,
2303                 .ldb_syntax             = LDB_SYNTAX_OCTET_STRING,
2304         },{
2305                 .name                   = "String(Teletex)",
2306                 .ldap_oid               = "1.2.840.113556.1.4.905",
2307                 .oMSyntax               = 20,
2308                 .attributeSyntax_oid    = "2.5.5.4",
2309                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2310                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2311                 .validate_ldb           = dsdb_syntax_DATA_BLOB_validate_ldb,
2312                 .equality               = "caseIgnoreMatch",
2313                 .substring              = "caseIgnoreSubstringsMatch",
2314                 .comment                = "Case Insensitive String",
2315                 .ldb_syntax             = LDB_SYNTAX_DIRECTORY_STRING,
2316         },{
2317                 .name                   = "String(IA5)",
2318                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.26",
2319                 .oMSyntax               = 22,
2320                 .attributeSyntax_oid    = "2.5.5.5",
2321                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2322                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2323                 .validate_ldb           = dsdb_syntax_DATA_BLOB_validate_ldb,
2324                 .equality               = "caseExactIA5Match",
2325                 .comment                = "Printable String",
2326                 .ldb_syntax             = LDB_SYNTAX_OCTET_STRING,
2327         },{
2328                 .name                   = "String(UTC-Time)",
2329                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.53",
2330                 .oMSyntax               = 23,
2331                 .attributeSyntax_oid    = "2.5.5.11",
2332                 .drsuapi_to_ldb         = dsdb_syntax_NTTIME_UTC_drsuapi_to_ldb,
2333                 .ldb_to_drsuapi         = dsdb_syntax_NTTIME_UTC_ldb_to_drsuapi,
2334                 .validate_ldb           = dsdb_syntax_NTTIME_UTC_validate_ldb,
2335                 .equality               = "generalizedTimeMatch",
2336                 .comment                = "UTC Time",
2337         },{
2338                 .name                   = "String(Generalized-Time)",
2339                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.24",
2340                 .oMSyntax               = 24,
2341                 .attributeSyntax_oid    = "2.5.5.11",
2342                 .drsuapi_to_ldb         = dsdb_syntax_NTTIME_drsuapi_to_ldb,
2343                 .ldb_to_drsuapi         = dsdb_syntax_NTTIME_ldb_to_drsuapi,
2344                 .validate_ldb           = dsdb_syntax_NTTIME_validate_ldb,
2345                 .equality               = "generalizedTimeMatch",
2346                 .comment                = "Generalized Time",
2347                 .ldb_syntax             = LDB_SYNTAX_UTC_TIME,
2348         },{
2349         /* not used in w2k3 schema */
2350                 .name                   = "String(Case Sensitive)",
2351                 .ldap_oid               = "1.2.840.113556.1.4.1362",
2352                 .oMSyntax               = 27,
2353                 .attributeSyntax_oid    = "2.5.5.3",
2354                 .drsuapi_to_ldb         = dsdb_syntax_FOOBAR_drsuapi_to_ldb,
2355                 .ldb_to_drsuapi         = dsdb_syntax_FOOBAR_ldb_to_drsuapi,
2356                 .validate_ldb           = dsdb_syntax_FOOBAR_validate_ldb,
2357         },{
2358                 .name                   = "String(Unicode)",
2359                 .ldap_oid               = LDB_SYNTAX_DIRECTORY_STRING,
2360                 .oMSyntax               = 64,
2361                 .attributeSyntax_oid    = "2.5.5.12",
2362                 .drsuapi_to_ldb         = dsdb_syntax_UNICODE_drsuapi_to_ldb,
2363                 .ldb_to_drsuapi         = dsdb_syntax_UNICODE_ldb_to_drsuapi,
2364                 .validate_ldb           = dsdb_syntax_UNICODE_validate_ldb,
2365                 .equality               = "caseIgnoreMatch",
2366                 .substring              = "caseIgnoreSubstringsMatch",
2367                 .comment                = "Directory String",
2368         },{
2369                 .name                   = "Interval/LargeInteger",
2370                 .ldap_oid               = "1.2.840.113556.1.4.906",
2371                 .oMSyntax               = 65,
2372                 .attributeSyntax_oid    = "2.5.5.16",
2373                 .drsuapi_to_ldb         = dsdb_syntax_INT64_drsuapi_to_ldb,
2374                 .ldb_to_drsuapi         = dsdb_syntax_INT64_ldb_to_drsuapi,
2375                 .validate_ldb           = dsdb_syntax_INT64_validate_ldb,
2376                 .equality               = "integerMatch",
2377                 .comment                = "Large Integer",
2378                 .ldb_syntax             = LDB_SYNTAX_INTEGER,
2379         },{
2380                 .name                   = "String(NT-Sec-Desc)",
2381                 .ldap_oid               = LDB_SYNTAX_SAMBA_SECURITY_DESCRIPTOR,
2382                 .oMSyntax               = 66,
2383                 .attributeSyntax_oid    = "2.5.5.15",
2384                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2385                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2386                 .validate_ldb           = dsdb_syntax_DATA_BLOB_validate_ldb,
2387         },{
2388                 .name                   = "Object(DS-DN)",
2389                 .ldap_oid               = LDB_SYNTAX_DN,
2390                 .oMSyntax               = 127,
2391                 .oMObjectClass          = OMOBJECTCLASS("\x2b\x0c\x02\x87\x73\x1c\x00\x85\x4a"),
2392                 .attributeSyntax_oid    = "2.5.5.1",
2393                 .drsuapi_to_ldb         = dsdb_syntax_DN_drsuapi_to_ldb,
2394                 .ldb_to_drsuapi         = dsdb_syntax_DN_ldb_to_drsuapi,
2395                 .validate_ldb           = dsdb_syntax_DN_validate_ldb,
2396                 .equality               = "distinguishedNameMatch",
2397                 .comment                = "Object(DS-DN) == a DN",
2398         },{
2399                 .name                   = "Object(DN-Binary)",
2400                 .ldap_oid               = DSDB_SYNTAX_BINARY_DN,
2401                 .oMSyntax               = 127,
2402                 .oMObjectClass          = OMOBJECTCLASS("\x2a\x86\x48\x86\xf7\x14\x01\x01\x01\x0b"),
2403                 .attributeSyntax_oid    = "2.5.5.7",
2404                 .drsuapi_to_ldb         = dsdb_syntax_DN_BINARY_drsuapi_to_ldb,
2405                 .ldb_to_drsuapi         = dsdb_syntax_DN_BINARY_ldb_to_drsuapi,
2406                 .validate_ldb           = dsdb_syntax_DN_BINARY_validate_ldb,
2407                 .equality               = "octetStringMatch",
2408                 .comment                = "OctetString: Binary+DN",
2409         },{
2410         /* not used in w2k3 schema, but used in Exchange schema*/
2411                 .name                   = "Object(OR-Name)",
2412                 .ldap_oid               = DSDB_SYNTAX_OR_NAME,
2413                 .oMSyntax               = 127,
2414                 .oMObjectClass          = OMOBJECTCLASS("\x56\x06\x01\x02\x05\x0b\x1D"),
2415                 .attributeSyntax_oid    = "2.5.5.7",
2416                 .drsuapi_to_ldb         = dsdb_syntax_DN_BINARY_drsuapi_to_ldb,
2417                 .ldb_to_drsuapi         = dsdb_syntax_DN_BINARY_ldb_to_drsuapi,
2418                 .validate_ldb           = dsdb_syntax_DN_BINARY_validate_ldb,
2419                 .equality               = "caseIgnoreMatch",
2420                 .ldb_syntax             = LDB_SYNTAX_DN,
2421         },{
2422         /* 
2423          * TODO: verify if DATA_BLOB is correct here...!
2424          *
2425          *       repsFrom and repsTo are the only attributes using
2426          *       this attribute syntax, but they're not replicated... 
2427          */
2428                 .name                   = "Object(Replica-Link)",
2429                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.40",
2430                 .oMSyntax               = 127,
2431                 .oMObjectClass          = OMOBJECTCLASS("\x2a\x86\x48\x86\xf7\x14\x01\x01\x01\x06"),
2432                 .attributeSyntax_oid    = "2.5.5.10",
2433                 .drsuapi_to_ldb         = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2434                 .ldb_to_drsuapi         = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2435                 .validate_ldb           = dsdb_syntax_DATA_BLOB_validate_ldb,
2436         },{
2437                 .name                   = "Object(Presentation-Address)",
2438                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.43",
2439                 .oMSyntax               = 127,
2440                 .oMObjectClass          = OMOBJECTCLASS("\x2b\x0c\x02\x87\x73\x1c\x00\x85\x5c"),
2441                 .attributeSyntax_oid    = "2.5.5.13",
2442                 .drsuapi_to_ldb         = dsdb_syntax_PRESENTATION_ADDRESS_drsuapi_to_ldb,
2443                 .ldb_to_drsuapi         = dsdb_syntax_PRESENTATION_ADDRESS_ldb_to_drsuapi,
2444                 .validate_ldb           = dsdb_syntax_PRESENTATION_ADDRESS_validate_ldb,
2445                 .comment                = "Presentation Address",
2446                 .ldb_syntax             = LDB_SYNTAX_DIRECTORY_STRING,
2447         },{
2448         /* not used in w2k3 schema */
2449                 .name                   = "Object(Access-Point)",
2450                 .ldap_oid               = "1.3.6.1.4.1.1466.115.121.1.2",
2451                 .oMSyntax               = 127,
2452                 .oMObjectClass          = OMOBJECTCLASS("\x2b\x0c\x02\x87\x73\x1c\x00\x85\x3e"),
2453                 .attributeSyntax_oid    = "2.5.5.14",
2454                 .drsuapi_to_ldb         = dsdb_syntax_FOOBAR_drsuapi_to_ldb,
2455                 .ldb_to_drsuapi         = dsdb_syntax_FOOBAR_ldb_to_drsuapi,
2456                 .validate_ldb           = dsdb_syntax_FOOBAR_validate_ldb,
2457                 .ldb_syntax             = LDB_SYNTAX_DIRECTORY_STRING,
2458         },{
2459         /* not used in w2k3 schema */
2460                 .name                   = "Object(DN-String)",
2461                 .ldap_oid               = DSDB_SYNTAX_STRING_DN,
2462                 .oMSyntax               = 127,
2463                 .oMObjectClass          = OMOBJECTCLASS("\x2a\x86\x48\x86\xf7\x14\x01\x01\x01\x0c"),
2464                 .attributeSyntax_oid    = "2.5.5.14",
2465                 .drsuapi_to_ldb         = dsdb_syntax_DN_STRING_drsuapi_to_ldb,
2466                 .ldb_to_drsuapi         = dsdb_syntax_DN_STRING_ldb_to_drsuapi,
2467                 .validate_ldb           = dsdb_syntax_DN_STRING_validate_ldb,
2468                 .equality               = "octetStringMatch",
2469                 .comment                = "OctetString: String+DN",
2470         }
2471 };
2472
2473 const struct dsdb_syntax *find_syntax_map_by_ad_oid(const char *ad_oid) 
2474 {
2475         unsigned int i;
2476         for (i=0; dsdb_syntaxes[i].ldap_oid; i++) {
2477                 if (strcasecmp(ad_oid, dsdb_syntaxes[i].attributeSyntax_oid) == 0) {
2478                         return &dsdb_syntaxes[i];
2479                 }
2480         }
2481         return NULL;
2482 }
2483
2484 const struct dsdb_syntax *find_syntax_map_by_ad_syntax(int oMSyntax) 
2485 {
2486         unsigned int i;
2487         for (i=0; dsdb_syntaxes[i].ldap_oid; i++) {
2488                 if (oMSyntax == dsdb_syntaxes[i].oMSyntax) {
2489                         return &dsdb_syntaxes[i];
2490                 }
2491         }
2492         return NULL;
2493 }
2494
2495 const struct dsdb_syntax *find_syntax_map_by_standard_oid(const char *standard_oid) 
2496 {
2497         unsigned int i;
2498         for (i=0; dsdb_syntaxes[i].ldap_oid; i++) {
2499                 if (strcasecmp(standard_oid, dsdb_syntaxes[i].ldap_oid) == 0) {
2500                         return &dsdb_syntaxes[i];
2501                 }
2502         }
2503         return NULL;
2504 }
2505 const struct dsdb_syntax *dsdb_syntax_for_attribute(const struct dsdb_attribute *attr)
2506 {
2507         unsigned int i;
2508
2509         for (i=0; i < ARRAY_SIZE(dsdb_syntaxes); i++) {
2510                 if (attr->oMSyntax != dsdb_syntaxes[i].oMSyntax) continue;
2511
2512                 if (attr->oMObjectClass.length != dsdb_syntaxes[i].oMObjectClass.length) continue;
2513
2514                 if (attr->oMObjectClass.length) {
2515                         int ret;
2516                         ret = memcmp(attr->oMObjectClass.data,
2517                                      dsdb_syntaxes[i].oMObjectClass.data,
2518                                      attr->oMObjectClass.length);
2519                         if (ret != 0) continue;
2520                 }
2521
2522                 if (strcmp(attr->attributeSyntax_oid, dsdb_syntaxes[i].attributeSyntax_oid) != 0) continue;
2523
2524                 return &dsdb_syntaxes[i];
2525         }
2526
2527         return NULL;
2528 }
2529
2530 WERROR dsdb_attribute_drsuapi_to_ldb(struct ldb_context *ldb, 
2531                                      const struct dsdb_schema *schema,
2532                                      const struct drsuapi_DsReplicaAttribute *in,
2533                                      TALLOC_CTX *mem_ctx,
2534                                      struct ldb_message_element *out)
2535 {
2536         const struct dsdb_attribute *sa;
2537
2538         sa = dsdb_attribute_by_attributeID_id(schema, in->attid);
2539         if (!sa) {
2540                 return WERR_FOOBAR;
2541         }
2542
2543         return sa->syntax->drsuapi_to_ldb(ldb, schema, sa, in, mem_ctx, out);
2544 }
2545
2546 WERROR dsdb_attribute_ldb_to_drsuapi(struct ldb_context *ldb, 
2547                                      const struct dsdb_schema *schema,
2548                                      const struct ldb_message_element *in,
2549                                      TALLOC_CTX *mem_ctx,
2550                                      struct drsuapi_DsReplicaAttribute *out)
2551 {
2552         const struct dsdb_attribute *sa;
2553
2554         sa = dsdb_attribute_by_lDAPDisplayName(schema, in->name);
2555         if (!sa) {
2556                 return WERR_FOOBAR;
2557         }
2558
2559         return sa->syntax->ldb_to_drsuapi(ldb, schema, sa, in, mem_ctx, out);
2560 }
2561
2562 WERROR dsdb_attribute_validate_ldb(struct ldb_context *ldb,
2563                                    const struct dsdb_schema *schema,
2564                                    const struct ldb_message_element *in)
2565 {
2566         const struct dsdb_attribute *sa;
2567
2568         sa = dsdb_attribute_by_lDAPDisplayName(schema, in->name);
2569         if (!sa) {
2570                 return WERR_DS_ATTRIBUTE_TYPE_UNDEFINED;
2571         }
2572
2573         return sa->syntax->validate_ldb(ldb, schema, sa, in);
2574 }