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