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