s4-selftest: add unit tests for sites's function in python
[Samba/gebeck_regimport.git] / source4 / dsdb / schema / schema_syntax.c
blob0a5438ba976e024bb37d7091570e8b4026e6b6c0
1 /*
2 Unix SMB/CIFS mplementation.
3 DSDB schema syntaxes
5 Copyright (C) Stefan Metzmacher <metze@samba.org> 2006
6 Copyright (C) Simo Sorce 2005
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2008
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.
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.
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/>.
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"
35 /**
36 * Initialize dsdb_syntax_ctx with default values
37 * for common cases.
39 void dsdb_syntax_ctx_init(struct dsdb_syntax_ctx *ctx,
40 struct ldb_context *ldb,
41 const struct dsdb_schema *schema)
43 ctx->ldb = ldb;
44 ctx->schema = schema;
47 * 'true' will keep current behavior,
48 * i.e. attributeID_id will be returned by default
50 ctx->is_schema_nc = true;
52 ctx->pfm_remote = NULL;
56 /**
57 * Returns ATTID for DRS attribute.
59 * ATTID depends on whether we are replicating
60 * Schema NC or msDs-IntId is set for schemaAttribute
61 * for the attribute.
63 uint32_t dsdb_attribute_get_attid(const struct dsdb_attribute *attr,
64 bool for_schema_nc)
66 if (!for_schema_nc && attr->msDS_IntId) {
67 return attr->msDS_IntId;
70 return attr->attributeID_id;
73 /**
74 * Map an ATTID from remote DC to a local ATTID
75 * using remote prefixMap
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)
82 WERROR werr;
83 const char *oid;
86 * map remote ATTID to local directly in case
87 * of no remote prefixMap (during provision for instance)
89 if (!ctx->pfm_remote) {
90 *id_local = id_remote;
91 return true;
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;
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;
106 return true;
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)
115 unsigned int i;
117 out->flags = 0;
118 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
119 W_ERROR_HAVE_NO_MEMORY(out->name);
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);
125 for (i=0; i < out->num_values; i++) {
126 char *str;
128 if (in->value_ctr.values[i].blob == NULL) {
129 return WERR_FOOBAR;
132 str = talloc_asprintf(out->values, "%s: not implemented",
133 attr->syntax->name);
134 W_ERROR_HAVE_NO_MEMORY(str);
136 out->values[i] = data_blob_string_const(str);
139 return WERR_OK;
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)
148 return WERR_FOOBAR;
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)
155 return WERR_FOOBAR;
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)
164 unsigned int i;
166 out->flags = 0;
167 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
168 W_ERROR_HAVE_NO_MEMORY(out->name);
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);
174 for (i=0; i < out->num_values; i++) {
175 uint32_t v;
176 char *str;
178 if (in->value_ctr.values[i].blob == NULL) {
179 return WERR_FOOBAR;
182 if (in->value_ctr.values[i].blob->length != 4) {
183 return WERR_FOOBAR;
186 v = IVAL(in->value_ctr.values[i].blob->data, 0);
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);
196 out->values[i] = data_blob_string_const(str);
199 return WERR_OK;
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)
208 unsigned int i;
209 DATA_BLOB *blobs;
211 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
212 return WERR_FOOBAR;
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);
223 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
224 W_ERROR_HAVE_NO_MEMORY(blobs);
226 for (i=0; i < in->num_values; i++) {
227 out->value_ctr.values[i].blob = &blobs[i];
229 blobs[i] = data_blob_talloc(blobs, NULL, 4);
230 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
232 if (in->values[i].length >= 4 &&
233 strncmp("TRUE", (const char *)in->values[i].data, in->values[i].length) == 0) {
234 SIVAL(blobs[i].data, 0, 0x00000001);
235 } else if (in->values[i].length >= 5 &&
236 strncmp("FALSE", (const char *)in->values[i].data, in->values[i].length) == 0) {
237 SIVAL(blobs[i].data, 0, 0x00000000);
238 } else {
239 return WERR_FOOBAR;
243 return WERR_OK;
246 static WERROR dsdb_syntax_BOOL_validate_ldb(const struct dsdb_syntax_ctx *ctx,
247 const struct dsdb_attribute *attr,
248 const struct ldb_message_element *in)
250 unsigned int i;
252 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
253 return WERR_FOOBAR;
256 for (i=0; i < in->num_values; i++) {
257 if (in->values[i].length == 0) {
258 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
261 if (in->values[i].length >= 4 &&
262 strncmp("TRUE",
263 (const char *)in->values[i].data,
264 in->values[i].length) == 0) {
265 continue;
267 if (in->values[i].length >= 5 &&
268 strncmp("FALSE",
269 (const char *)in->values[i].data,
270 in->values[i].length) == 0) {
271 continue;
273 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
276 return WERR_OK;
279 static WERROR dsdb_syntax_INT32_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
280 const struct dsdb_attribute *attr,
281 const struct drsuapi_DsReplicaAttribute *in,
282 TALLOC_CTX *mem_ctx,
283 struct ldb_message_element *out)
285 unsigned int i;
287 out->flags = 0;
288 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
289 W_ERROR_HAVE_NO_MEMORY(out->name);
291 out->num_values = in->value_ctr.num_values;
292 out->values = talloc_array(mem_ctx, struct ldb_val, out->num_values);
293 W_ERROR_HAVE_NO_MEMORY(out->values);
295 for (i=0; i < out->num_values; i++) {
296 int32_t v;
297 char *str;
299 if (in->value_ctr.values[i].blob == NULL) {
300 return WERR_FOOBAR;
303 if (in->value_ctr.values[i].blob->length != 4) {
304 return WERR_FOOBAR;
307 v = IVALS(in->value_ctr.values[i].blob->data, 0);
309 str = talloc_asprintf(out->values, "%d", v);
310 W_ERROR_HAVE_NO_MEMORY(str);
312 out->values[i] = data_blob_string_const(str);
315 return WERR_OK;
318 static WERROR dsdb_syntax_INT32_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
319 const struct dsdb_attribute *attr,
320 const struct ldb_message_element *in,
321 TALLOC_CTX *mem_ctx,
322 struct drsuapi_DsReplicaAttribute *out)
324 unsigned int i;
325 DATA_BLOB *blobs;
327 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
328 return WERR_FOOBAR;
331 out->attid = dsdb_attribute_get_attid(attr,
332 ctx->is_schema_nc);
333 out->value_ctr.num_values = in->num_values;
334 out->value_ctr.values = talloc_array(mem_ctx,
335 struct drsuapi_DsAttributeValue,
336 in->num_values);
337 W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
339 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
340 W_ERROR_HAVE_NO_MEMORY(blobs);
342 for (i=0; i < in->num_values; i++) {
343 int32_t v;
345 out->value_ctr.values[i].blob = &blobs[i];
347 blobs[i] = data_blob_talloc(blobs, NULL, 4);
348 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
350 /* We've to use "strtoll" here to have the intended overflows.
351 * Otherwise we may get "LONG_MAX" and the conversion is wrong. */
352 v = (int32_t) strtoll((char *)in->values[i].data, NULL, 0);
354 SIVALS(blobs[i].data, 0, v);
357 return WERR_OK;
360 static WERROR dsdb_syntax_INT32_validate_ldb(const struct dsdb_syntax_ctx *ctx,
361 const struct dsdb_attribute *attr,
362 const struct ldb_message_element *in)
364 unsigned int i;
366 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
367 return WERR_FOOBAR;
370 for (i=0; i < in->num_values; i++) {
371 long v;
372 char buf[sizeof("-2147483648")];
373 char *end = NULL;
375 ZERO_STRUCT(buf);
376 if (in->values[i].length >= sizeof(buf)) {
377 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
380 memcpy(buf, in->values[i].data, in->values[i].length);
381 errno = 0;
382 v = strtol(buf, &end, 10);
383 if (errno != 0) {
384 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
386 if (end && end[0] != '\0') {
387 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
390 if (attr->rangeLower) {
391 if ((int32_t)v < (int32_t)*attr->rangeLower) {
392 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
396 if (attr->rangeUpper) {
397 if ((int32_t)v > (int32_t)*attr->rangeUpper) {
398 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
403 return WERR_OK;
406 static WERROR dsdb_syntax_INT64_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
407 const struct dsdb_attribute *attr,
408 const struct drsuapi_DsReplicaAttribute *in,
409 TALLOC_CTX *mem_ctx,
410 struct ldb_message_element *out)
412 unsigned int i;
414 out->flags = 0;
415 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
416 W_ERROR_HAVE_NO_MEMORY(out->name);
418 out->num_values = in->value_ctr.num_values;
419 out->values = talloc_array(mem_ctx, struct ldb_val, out->num_values);
420 W_ERROR_HAVE_NO_MEMORY(out->values);
422 for (i=0; i < out->num_values; i++) {
423 int64_t v;
424 char *str;
426 if (in->value_ctr.values[i].blob == NULL) {
427 return WERR_FOOBAR;
430 if (in->value_ctr.values[i].blob->length != 8) {
431 return WERR_FOOBAR;
434 v = BVALS(in->value_ctr.values[i].blob->data, 0);
436 str = talloc_asprintf(out->values, "%lld", (long long int)v);
437 W_ERROR_HAVE_NO_MEMORY(str);
439 out->values[i] = data_blob_string_const(str);
442 return WERR_OK;
445 static WERROR dsdb_syntax_INT64_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
446 const struct dsdb_attribute *attr,
447 const struct ldb_message_element *in,
448 TALLOC_CTX *mem_ctx,
449 struct drsuapi_DsReplicaAttribute *out)
451 unsigned int i;
452 DATA_BLOB *blobs;
454 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
455 return WERR_FOOBAR;
458 out->attid = dsdb_attribute_get_attid(attr,
459 ctx->is_schema_nc);
460 out->value_ctr.num_values = in->num_values;
461 out->value_ctr.values = talloc_array(mem_ctx,
462 struct drsuapi_DsAttributeValue,
463 in->num_values);
464 W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
466 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
467 W_ERROR_HAVE_NO_MEMORY(blobs);
469 for (i=0; i < in->num_values; i++) {
470 int64_t v;
472 out->value_ctr.values[i].blob = &blobs[i];
474 blobs[i] = data_blob_talloc(blobs, NULL, 8);
475 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
477 v = strtoll((const char *)in->values[i].data, NULL, 10);
479 SBVALS(blobs[i].data, 0, v);
482 return WERR_OK;
485 static WERROR dsdb_syntax_INT64_validate_ldb(const struct dsdb_syntax_ctx *ctx,
486 const struct dsdb_attribute *attr,
487 const struct ldb_message_element *in)
489 unsigned int i;
491 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
492 return WERR_FOOBAR;
495 for (i=0; i < in->num_values; i++) {
496 long long v;
497 char buf[sizeof("-9223372036854775808")];
498 char *end = NULL;
500 ZERO_STRUCT(buf);
501 if (in->values[i].length >= sizeof(buf)) {
502 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
504 memcpy(buf, in->values[i].data, in->values[i].length);
506 errno = 0;
507 v = strtoll(buf, &end, 10);
508 if (errno != 0) {
509 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
511 if (end && end[0] != '\0') {
512 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
515 if (attr->rangeLower) {
516 if ((int64_t)v < (int64_t)*attr->rangeLower) {
517 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
521 if (attr->rangeUpper) {
522 if ((int64_t)v > (int64_t)*attr->rangeUpper) {
523 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
528 return WERR_OK;
530 static WERROR dsdb_syntax_NTTIME_UTC_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
531 const struct dsdb_attribute *attr,
532 const struct drsuapi_DsReplicaAttribute *in,
533 TALLOC_CTX *mem_ctx,
534 struct ldb_message_element *out)
536 unsigned int i;
538 out->flags = 0;
539 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
540 W_ERROR_HAVE_NO_MEMORY(out->name);
542 out->num_values = in->value_ctr.num_values;
543 out->values = talloc_array(mem_ctx, struct ldb_val, out->num_values);
544 W_ERROR_HAVE_NO_MEMORY(out->values);
546 for (i=0; i < out->num_values; i++) {
547 NTTIME v;
548 time_t t;
549 char *str;
551 if (in->value_ctr.values[i].blob == NULL) {
552 return WERR_FOOBAR;
555 if (in->value_ctr.values[i].blob->length != 8) {
556 return WERR_FOOBAR;
559 v = BVAL(in->value_ctr.values[i].blob->data, 0);
560 if (v == 0) {
561 /* special case for 1601 zero timestamp */
562 out->values[i] = data_blob_string_const("16010101000000.0Z");
563 continue;
565 v *= 10000000;
566 t = nt_time_to_unix(v);
569 * NOTE: On a w2k3 server you can set a GeneralizedTime string
570 * via LDAP, but you get back an UTCTime string,
571 * but via DRSUAPI you get back the NTTIME_1sec value
572 * that represents the GeneralizedTime value!
574 * So if we store the UTCTime string in our ldb
575 * we'll loose information!
577 str = ldb_timestring_utc(out->values, t);
578 W_ERROR_HAVE_NO_MEMORY(str);
579 out->values[i] = data_blob_string_const(str);
582 return WERR_OK;
585 static WERROR dsdb_syntax_NTTIME_UTC_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
586 const struct dsdb_attribute *attr,
587 const struct ldb_message_element *in,
588 TALLOC_CTX *mem_ctx,
589 struct drsuapi_DsReplicaAttribute *out)
591 unsigned int i;
592 DATA_BLOB *blobs;
594 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
595 return WERR_FOOBAR;
598 out->attid = dsdb_attribute_get_attid(attr,
599 ctx->is_schema_nc);
600 out->value_ctr.num_values = in->num_values;
601 out->value_ctr.values = talloc_array(mem_ctx,
602 struct drsuapi_DsAttributeValue,
603 in->num_values);
604 W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
606 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
607 W_ERROR_HAVE_NO_MEMORY(blobs);
609 for (i=0; i < in->num_values; i++) {
610 NTTIME v;
611 time_t t;
613 out->value_ctr.values[i].blob = &blobs[i];
615 blobs[i] = data_blob_talloc(blobs, NULL, 8);
616 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
618 if (ldb_val_string_cmp(&in->values[i], "16010101000000.0Z") == 0) {
619 SBVALS(blobs[i].data, 0, 0);
620 continue;
623 t = ldb_string_utc_to_time((const char *)in->values[i].data);
624 unix_to_nt_time(&v, t);
625 v /= 10000000;
627 SBVAL(blobs[i].data, 0, v);
630 return WERR_OK;
633 static WERROR dsdb_syntax_NTTIME_UTC_validate_ldb(const struct dsdb_syntax_ctx *ctx,
634 const struct dsdb_attribute *attr,
635 const struct ldb_message_element *in)
637 unsigned int i;
639 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
640 return WERR_FOOBAR;
643 for (i=0; i < in->num_values; i++) {
644 time_t t;
645 char buf[sizeof("090826075717Z")];
647 ZERO_STRUCT(buf);
648 if (in->values[i].length >= sizeof(buf)) {
649 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
651 memcpy(buf, in->values[i].data, in->values[i].length);
653 t = ldb_string_utc_to_time(buf);
654 if (t == 0) {
655 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
658 if (attr->rangeLower) {
659 if ((int32_t)t < (int32_t)*attr->rangeLower) {
660 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
664 if (attr->rangeUpper) {
665 if ((int32_t)t > (int32_t)*attr->rangeLower) {
666 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
671 * TODO: verify the comment in the
672 * dsdb_syntax_NTTIME_UTC_drsuapi_to_ldb() function!
676 return WERR_OK;
679 static WERROR dsdb_syntax_NTTIME_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
680 const struct dsdb_attribute *attr,
681 const struct drsuapi_DsReplicaAttribute *in,
682 TALLOC_CTX *mem_ctx,
683 struct ldb_message_element *out)
685 unsigned int i;
687 out->flags = 0;
688 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
689 W_ERROR_HAVE_NO_MEMORY(out->name);
691 out->num_values = in->value_ctr.num_values;
692 out->values = talloc_array(mem_ctx, struct ldb_val, out->num_values);
693 W_ERROR_HAVE_NO_MEMORY(out->values);
695 for (i=0; i < out->num_values; i++) {
696 NTTIME v;
697 time_t t;
698 char *str;
700 if (in->value_ctr.values[i].blob == NULL) {
701 return WERR_FOOBAR;
704 if (in->value_ctr.values[i].blob->length != 8) {
705 return WERR_FOOBAR;
708 v = BVAL(in->value_ctr.values[i].blob->data, 0);
709 if (v == 0) {
710 /* special case for 1601 zero timestamp */
711 out->values[i] = data_blob_string_const("16010101000000.0Z");
712 continue;
714 v *= 10000000;
715 t = nt_time_to_unix(v);
717 str = ldb_timestring(out->values, t);
718 W_ERROR_HAVE_NO_MEMORY(str);
720 out->values[i] = data_blob_string_const(str);
723 return WERR_OK;
726 static WERROR dsdb_syntax_NTTIME_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
727 const struct dsdb_attribute *attr,
728 const struct ldb_message_element *in,
729 TALLOC_CTX *mem_ctx,
730 struct drsuapi_DsReplicaAttribute *out)
732 unsigned int i;
733 DATA_BLOB *blobs;
735 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
736 return WERR_FOOBAR;
739 out->attid = dsdb_attribute_get_attid(attr,
740 ctx->is_schema_nc);
741 out->value_ctr.num_values = in->num_values;
742 out->value_ctr.values = talloc_array(mem_ctx,
743 struct drsuapi_DsAttributeValue,
744 in->num_values);
745 W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
747 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
748 W_ERROR_HAVE_NO_MEMORY(blobs);
750 for (i=0; i < in->num_values; i++) {
751 NTTIME v;
752 time_t t;
753 int ret;
755 out->value_ctr.values[i].blob = &blobs[i];
757 blobs[i] = data_blob_talloc(blobs, NULL, 8);
758 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
760 if (ldb_val_string_cmp(&in->values[i], "16010101000000.0Z") == 0) {
761 SBVALS(blobs[i].data, 0, 0);
762 continue;
765 ret = ldb_val_to_time(&in->values[i], &t);
766 if (ret != LDB_SUCCESS) {
767 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
769 unix_to_nt_time(&v, t);
770 v /= 10000000;
772 SBVAL(blobs[i].data, 0, v);
775 return WERR_OK;
778 static WERROR dsdb_syntax_NTTIME_validate_ldb(const struct dsdb_syntax_ctx *ctx,
779 const struct dsdb_attribute *attr,
780 const struct ldb_message_element *in)
782 unsigned int i;
784 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
785 return WERR_FOOBAR;
788 for (i=0; i < in->num_values; i++) {
789 time_t t;
790 int ret;
792 ret = ldb_val_to_time(&in->values[i], &t);
793 if (ret != LDB_SUCCESS) {
794 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
797 if (attr->rangeLower) {
798 if ((int32_t)t < (int32_t)*attr->rangeLower) {
799 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
803 if (attr->rangeUpper) {
804 if ((int32_t)t > (int32_t)*attr->rangeLower) {
805 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
810 return WERR_OK;
813 static WERROR dsdb_syntax_DATA_BLOB_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
814 const struct dsdb_attribute *attr,
815 const struct drsuapi_DsReplicaAttribute *in,
816 TALLOC_CTX *mem_ctx,
817 struct ldb_message_element *out)
819 unsigned int i;
821 out->flags = 0;
822 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
823 W_ERROR_HAVE_NO_MEMORY(out->name);
825 out->num_values = in->value_ctr.num_values;
826 out->values = talloc_array(mem_ctx, struct ldb_val, out->num_values);
827 W_ERROR_HAVE_NO_MEMORY(out->values);
829 for (i=0; i < out->num_values; i++) {
830 if (in->value_ctr.values[i].blob == NULL) {
831 return WERR_FOOBAR;
834 if (in->value_ctr.values[i].blob->length == 0) {
835 return WERR_FOOBAR;
838 out->values[i] = data_blob_dup_talloc(out->values,
839 *in->value_ctr.values[i].blob);
840 W_ERROR_HAVE_NO_MEMORY(out->values[i].data);
843 return WERR_OK;
846 static WERROR dsdb_syntax_DATA_BLOB_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
847 const struct dsdb_attribute *attr,
848 const struct ldb_message_element *in,
849 TALLOC_CTX *mem_ctx,
850 struct drsuapi_DsReplicaAttribute *out)
852 unsigned int i;
853 DATA_BLOB *blobs;
855 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
856 return WERR_FOOBAR;
859 out->attid = dsdb_attribute_get_attid(attr,
860 ctx->is_schema_nc);
861 out->value_ctr.num_values = in->num_values;
862 out->value_ctr.values = talloc_array(mem_ctx,
863 struct drsuapi_DsAttributeValue,
864 in->num_values);
865 W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
867 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
868 W_ERROR_HAVE_NO_MEMORY(blobs);
870 for (i=0; i < in->num_values; i++) {
871 out->value_ctr.values[i].blob = &blobs[i];
873 blobs[i] = data_blob_dup_talloc(blobs, in->values[i]);
874 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
877 return WERR_OK;
880 static WERROR dsdb_syntax_DATA_BLOB_validate_one_val(const struct dsdb_syntax_ctx *ctx,
881 const struct dsdb_attribute *attr,
882 const struct ldb_val *val)
884 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
885 return WERR_FOOBAR;
888 if (attr->rangeLower) {
889 if ((uint32_t)val->length < (uint32_t)*attr->rangeLower) {
890 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
894 if (attr->rangeUpper) {
895 if ((uint32_t)val->length > (uint32_t)*attr->rangeUpper) {
896 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
900 return WERR_OK;
903 static WERROR dsdb_syntax_DATA_BLOB_validate_ldb(const struct dsdb_syntax_ctx *ctx,
904 const struct dsdb_attribute *attr,
905 const struct ldb_message_element *in)
907 unsigned int i;
908 WERROR status;
910 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
911 return WERR_FOOBAR;
914 for (i=0; i < in->num_values; i++) {
915 if (in->values[i].length == 0) {
916 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
919 status = dsdb_syntax_DATA_BLOB_validate_one_val(ctx,
920 attr,
921 &in->values[i]);
922 if (!W_ERROR_IS_OK(status)) {
923 return status;
927 return WERR_OK;
930 static WERROR _dsdb_syntax_auto_OID_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
931 const struct dsdb_attribute *attr,
932 const struct drsuapi_DsReplicaAttribute *in,
933 TALLOC_CTX *mem_ctx,
934 struct ldb_message_element *out)
936 unsigned int i;
938 out->flags = 0;
939 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
940 W_ERROR_HAVE_NO_MEMORY(out->name);
942 out->num_values = in->value_ctr.num_values;
943 out->values = talloc_array(mem_ctx, struct ldb_val, out->num_values);
944 W_ERROR_HAVE_NO_MEMORY(out->values);
946 for (i=0; i < out->num_values; i++) {
947 uint32_t v;
948 const struct dsdb_class *c;
949 const struct dsdb_attribute *a;
950 const char *str = NULL;
952 if (in->value_ctr.values[i].blob == NULL) {
953 return WERR_FOOBAR;
956 if (in->value_ctr.values[i].blob->length != 4) {
957 return WERR_FOOBAR;
960 v = IVAL(in->value_ctr.values[i].blob->data, 0);
962 if ((c = dsdb_class_by_governsID_id(ctx->schema, v))) {
963 str = talloc_strdup(out->values, c->lDAPDisplayName);
964 } else if ((a = dsdb_attribute_by_attributeID_id(ctx->schema, v))) {
965 str = talloc_strdup(out->values, a->lDAPDisplayName);
966 } else {
967 WERROR werr;
968 SMB_ASSERT(ctx->pfm_remote);
969 werr = dsdb_schema_pfm_oid_from_attid(ctx->pfm_remote, v,
970 out->values, &str);
971 W_ERROR_NOT_OK_RETURN(werr);
973 W_ERROR_HAVE_NO_MEMORY(str);
975 /* the values need to be reversed */
976 out->values[out->num_values - (i + 1)] = data_blob_string_const(str);
979 return WERR_OK;
982 static WERROR _dsdb_syntax_OID_obj_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
983 const struct dsdb_attribute *attr,
984 const struct drsuapi_DsReplicaAttribute *in,
985 TALLOC_CTX *mem_ctx,
986 struct ldb_message_element *out)
988 unsigned int i;
990 out->flags = 0;
991 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
992 W_ERROR_HAVE_NO_MEMORY(out->name);
994 out->num_values = in->value_ctr.num_values;
995 out->values = talloc_array(mem_ctx, struct ldb_val, out->num_values);
996 W_ERROR_HAVE_NO_MEMORY(out->values);
998 for (i=0; i < out->num_values; i++) {
999 uint32_t v;
1000 const struct dsdb_class *c;
1001 const char *str;
1003 if (in->value_ctr.values[i].blob == NULL) {
1004 return WERR_FOOBAR;
1007 if (in->value_ctr.values[i].blob->length != 4) {
1008 return WERR_FOOBAR;
1011 v = IVAL(in->value_ctr.values[i].blob->data, 0);
1013 /* convert remote ATTID to local ATTID */
1014 if (!dsdb_syntax_attid_from_remote_attid(ctx, mem_ctx, v, &v)) {
1015 DEBUG(1,(__location__ ": Failed to map remote ATTID to local ATTID!\n"));
1016 return WERR_FOOBAR;
1019 c = dsdb_class_by_governsID_id(ctx->schema, v);
1020 if (!c) {
1021 DEBUG(1,(__location__ ": Unknown governsID 0x%08X\n", v));
1022 return WERR_FOOBAR;
1025 str = talloc_strdup(out->values, c->lDAPDisplayName);
1026 W_ERROR_HAVE_NO_MEMORY(str);
1028 /* the values need to be reversed */
1029 out->values[out->num_values - (i + 1)] = data_blob_string_const(str);
1032 return WERR_OK;
1035 static WERROR _dsdb_syntax_OID_attr_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
1036 const struct dsdb_attribute *attr,
1037 const struct drsuapi_DsReplicaAttribute *in,
1038 TALLOC_CTX *mem_ctx,
1039 struct ldb_message_element *out)
1041 unsigned int i;
1043 out->flags = 0;
1044 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
1045 W_ERROR_HAVE_NO_MEMORY(out->name);
1047 out->num_values = in->value_ctr.num_values;
1048 out->values = talloc_array(mem_ctx, struct ldb_val, out->num_values);
1049 W_ERROR_HAVE_NO_MEMORY(out->values);
1051 for (i=0; i < out->num_values; i++) {
1052 uint32_t v;
1053 const struct dsdb_attribute *a;
1054 const char *str;
1056 if (in->value_ctr.values[i].blob == NULL) {
1057 return WERR_FOOBAR;
1060 if (in->value_ctr.values[i].blob->length != 4) {
1061 return WERR_FOOBAR;
1064 v = IVAL(in->value_ctr.values[i].blob->data, 0);
1066 /* convert remote ATTID to local ATTID */
1067 if (!dsdb_syntax_attid_from_remote_attid(ctx, mem_ctx, v, &v)) {
1068 DEBUG(1,(__location__ ": Failed to map remote ATTID to local ATTID!\n"));
1069 return WERR_FOOBAR;
1072 a = dsdb_attribute_by_attributeID_id(ctx->schema, v);
1073 if (!a) {
1074 DEBUG(1,(__location__ ": Unknown attributeID_id 0x%08X\n", v));
1075 return WERR_FOOBAR;
1078 str = talloc_strdup(out->values, a->lDAPDisplayName);
1079 W_ERROR_HAVE_NO_MEMORY(str);
1081 /* the values need to be reversed */
1082 out->values[out->num_values - (i + 1)] = data_blob_string_const(str);
1085 return WERR_OK;
1088 static WERROR _dsdb_syntax_OID_oid_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
1089 const struct dsdb_attribute *attr,
1090 const struct drsuapi_DsReplicaAttribute *in,
1091 TALLOC_CTX *mem_ctx,
1092 struct ldb_message_element *out)
1094 unsigned int i;
1095 const struct dsdb_schema_prefixmap *prefixmap;
1097 if (ctx->pfm_remote != NULL) {
1098 prefixmap = ctx->pfm_remote;
1099 } else {
1100 prefixmap = ctx->schema->prefixmap;
1102 SMB_ASSERT(prefixmap);
1104 out->flags = 0;
1105 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
1106 W_ERROR_HAVE_NO_MEMORY(out->name);
1108 out->num_values = in->value_ctr.num_values;
1109 out->values = talloc_array(mem_ctx, struct ldb_val, out->num_values);
1110 W_ERROR_HAVE_NO_MEMORY(out->values);
1112 for (i=0; i < out->num_values; i++) {
1113 uint32_t attid;
1114 WERROR status;
1115 const char *oid;
1117 if (in->value_ctr.values[i].blob == NULL) {
1118 return WERR_FOOBAR;
1121 if (in->value_ctr.values[i].blob->length != 4) {
1122 return WERR_FOOBAR;
1125 attid = IVAL(in->value_ctr.values[i].blob->data, 0);
1127 status = dsdb_schema_pfm_oid_from_attid(prefixmap, attid,
1128 out->values, &oid);
1129 if (!W_ERROR_IS_OK(status)) {
1130 DEBUG(0,(__location__ ": Error: Unknown ATTID 0x%08X\n",
1131 attid));
1132 return status;
1135 out->values[i] = data_blob_string_const(oid);
1138 return WERR_OK;
1141 static WERROR _dsdb_syntax_auto_OID_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
1142 const struct dsdb_attribute *attr,
1143 const struct ldb_message_element *in,
1144 TALLOC_CTX *mem_ctx,
1145 struct drsuapi_DsReplicaAttribute *out)
1147 unsigned int i;
1148 DATA_BLOB *blobs;
1150 out->attid= dsdb_attribute_get_attid(attr,
1151 ctx->is_schema_nc);
1152 out->value_ctr.num_values= in->num_values;
1153 out->value_ctr.values= talloc_array(mem_ctx,
1154 struct drsuapi_DsAttributeValue,
1155 in->num_values);
1156 W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1158 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1159 W_ERROR_HAVE_NO_MEMORY(blobs);
1161 for (i=0; i < in->num_values; i++) {
1162 const struct dsdb_class *obj_class;
1163 const struct dsdb_attribute *obj_attr;
1164 struct ldb_val *v;
1166 out->value_ctr.values[i].blob= &blobs[i];
1168 blobs[i] = data_blob_talloc(blobs, NULL, 4);
1169 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
1171 /* in DRS windows puts the classes in the opposite
1172 order to the order used in ldap */
1173 v = &in->values[(in->num_values-1)-i];
1175 if ((obj_class = dsdb_class_by_lDAPDisplayName_ldb_val(ctx->schema, v))) {
1176 SIVAL(blobs[i].data, 0, obj_class->governsID_id);
1177 } else if ((obj_attr = dsdb_attribute_by_lDAPDisplayName_ldb_val(ctx->schema, v))) {
1178 SIVAL(blobs[i].data, 0, obj_attr->attributeID_id);
1179 } else {
1180 uint32_t attid;
1181 WERROR werr;
1182 werr = dsdb_schema_pfm_attid_from_oid(ctx->schema->prefixmap,
1183 (const char *)v->data,
1184 &attid);
1185 W_ERROR_NOT_OK_RETURN(werr);
1186 SIVAL(blobs[i].data, 0, attid);
1192 return WERR_OK;
1195 static WERROR _dsdb_syntax_OID_obj_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
1196 const struct dsdb_attribute *attr,
1197 const struct ldb_message_element *in,
1198 TALLOC_CTX *mem_ctx,
1199 struct drsuapi_DsReplicaAttribute *out)
1201 unsigned int i;
1202 DATA_BLOB *blobs;
1204 out->attid= dsdb_attribute_get_attid(attr,
1205 ctx->is_schema_nc);
1206 out->value_ctr.num_values= in->num_values;
1207 out->value_ctr.values= talloc_array(mem_ctx,
1208 struct drsuapi_DsAttributeValue,
1209 in->num_values);
1210 W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1212 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1213 W_ERROR_HAVE_NO_MEMORY(blobs);
1215 for (i=0; i < in->num_values; i++) {
1216 const struct dsdb_class *obj_class;
1218 out->value_ctr.values[i].blob= &blobs[i];
1220 blobs[i] = data_blob_talloc(blobs, NULL, 4);
1221 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
1223 /* in DRS windows puts the classes in the opposite
1224 order to the order used in ldap */
1225 obj_class = dsdb_class_by_lDAPDisplayName(ctx->schema,
1226 (const char *)in->values[(in->num_values-1)-i].data);
1227 if (!obj_class) {
1228 return WERR_FOOBAR;
1230 SIVAL(blobs[i].data, 0, obj_class->governsID_id);
1234 return WERR_OK;
1237 static WERROR _dsdb_syntax_OID_attr_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
1238 const struct dsdb_attribute *attr,
1239 const struct ldb_message_element *in,
1240 TALLOC_CTX *mem_ctx,
1241 struct drsuapi_DsReplicaAttribute *out)
1243 unsigned int i;
1244 DATA_BLOB *blobs;
1246 out->attid= dsdb_attribute_get_attid(attr,
1247 ctx->is_schema_nc);
1248 out->value_ctr.num_values= in->num_values;
1249 out->value_ctr.values= talloc_array(mem_ctx,
1250 struct drsuapi_DsAttributeValue,
1251 in->num_values);
1252 W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1254 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1255 W_ERROR_HAVE_NO_MEMORY(blobs);
1257 for (i=0; i < in->num_values; i++) {
1258 const struct dsdb_attribute *obj_attr;
1260 out->value_ctr.values[i].blob= &blobs[i];
1262 blobs[i] = data_blob_talloc(blobs, NULL, 4);
1263 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
1265 obj_attr = dsdb_attribute_by_lDAPDisplayName(ctx->schema, (const char *)in->values[i].data);
1266 if (!obj_attr) {
1267 return WERR_FOOBAR;
1269 SIVAL(blobs[i].data, 0, obj_attr->attributeID_id);
1273 return WERR_OK;
1276 static WERROR _dsdb_syntax_OID_oid_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
1277 const struct dsdb_attribute *attr,
1278 const struct ldb_message_element *in,
1279 TALLOC_CTX *mem_ctx,
1280 struct drsuapi_DsReplicaAttribute *out)
1282 unsigned int i;
1283 DATA_BLOB *blobs;
1285 out->attid= dsdb_attribute_get_attid(attr,
1286 ctx->is_schema_nc);
1287 out->value_ctr.num_values= in->num_values;
1288 out->value_ctr.values= talloc_array(mem_ctx,
1289 struct drsuapi_DsAttributeValue,
1290 in->num_values);
1291 W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1293 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1294 W_ERROR_HAVE_NO_MEMORY(blobs);
1296 for (i=0; i < in->num_values; i++) {
1297 uint32_t attid;
1298 WERROR status;
1300 out->value_ctr.values[i].blob= &blobs[i];
1302 blobs[i] = data_blob_talloc(blobs, NULL, 4);
1303 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
1305 status = dsdb_schema_pfm_attid_from_oid(ctx->schema->prefixmap,
1306 (const char *)in->values[i].data,
1307 &attid);
1308 W_ERROR_NOT_OK_RETURN(status);
1310 SIVAL(blobs[i].data, 0, attid);
1313 return WERR_OK;
1316 static WERROR dsdb_syntax_OID_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
1317 const struct dsdb_attribute *attr,
1318 const struct drsuapi_DsReplicaAttribute *in,
1319 TALLOC_CTX *mem_ctx,
1320 struct ldb_message_element *out)
1322 WERROR werr;
1324 switch (attr->attributeID_id) {
1325 case DRSUAPI_ATTID_objectClass:
1326 case DRSUAPI_ATTID_subClassOf:
1327 case DRSUAPI_ATTID_auxiliaryClass:
1328 case DRSUAPI_ATTID_systemAuxiliaryClass:
1329 case DRSUAPI_ATTID_systemPossSuperiors:
1330 case DRSUAPI_ATTID_possSuperiors:
1331 werr = _dsdb_syntax_OID_obj_drsuapi_to_ldb(ctx, attr, in, mem_ctx, out);
1332 break;
1333 case DRSUAPI_ATTID_systemMustContain:
1334 case DRSUAPI_ATTID_systemMayContain:
1335 case DRSUAPI_ATTID_mustContain:
1336 case DRSUAPI_ATTID_rDNAttId:
1337 case DRSUAPI_ATTID_transportAddressAttribute:
1338 case DRSUAPI_ATTID_mayContain:
1339 werr = _dsdb_syntax_OID_attr_drsuapi_to_ldb(ctx, attr, in, mem_ctx, out);
1340 break;
1341 case DRSUAPI_ATTID_governsID:
1342 case DRSUAPI_ATTID_attributeID:
1343 case DRSUAPI_ATTID_attributeSyntax:
1344 werr = _dsdb_syntax_OID_oid_drsuapi_to_ldb(ctx, attr, in, mem_ctx, out);
1345 break;
1346 default:
1347 DEBUG(0,(__location__ ": Unknown handling for attributeID_id for %s\n",
1348 attr->lDAPDisplayName));
1349 return _dsdb_syntax_auto_OID_drsuapi_to_ldb(ctx, attr, in, mem_ctx, out);
1352 /* When we are doing the vampire of a schema, we don't want
1353 * the inability to reference an OID to get in the way.
1354 * Otherwise, we won't get the new schema with which to
1355 * understand this */
1356 if (!W_ERROR_IS_OK(werr) && ctx->schema->relax_OID_conversions) {
1357 return _dsdb_syntax_OID_oid_drsuapi_to_ldb(ctx, attr, in, mem_ctx, out);
1359 return werr;
1362 static WERROR dsdb_syntax_OID_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
1363 const struct dsdb_attribute *attr,
1364 const struct ldb_message_element *in,
1365 TALLOC_CTX *mem_ctx,
1366 struct drsuapi_DsReplicaAttribute *out)
1368 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
1369 return WERR_FOOBAR;
1372 switch (attr->attributeID_id) {
1373 case DRSUAPI_ATTID_objectClass:
1374 case DRSUAPI_ATTID_subClassOf:
1375 case DRSUAPI_ATTID_auxiliaryClass:
1376 case DRSUAPI_ATTID_systemAuxiliaryClass:
1377 case DRSUAPI_ATTID_systemPossSuperiors:
1378 case DRSUAPI_ATTID_possSuperiors:
1379 return _dsdb_syntax_OID_obj_ldb_to_drsuapi(ctx, attr, in, mem_ctx, out);
1380 case DRSUAPI_ATTID_systemMustContain:
1381 case DRSUAPI_ATTID_systemMayContain:
1382 case DRSUAPI_ATTID_mustContain:
1383 case DRSUAPI_ATTID_rDNAttId:
1384 case DRSUAPI_ATTID_transportAddressAttribute:
1385 case DRSUAPI_ATTID_mayContain:
1386 return _dsdb_syntax_OID_attr_ldb_to_drsuapi(ctx, attr, in, mem_ctx, out);
1387 case DRSUAPI_ATTID_governsID:
1388 case DRSUAPI_ATTID_attributeID:
1389 case DRSUAPI_ATTID_attributeSyntax:
1390 return _dsdb_syntax_OID_oid_ldb_to_drsuapi(ctx, attr, in, mem_ctx, out);
1393 DEBUG(0,(__location__ ": Unknown handling for attributeID_id for %s\n",
1394 attr->lDAPDisplayName));
1396 return _dsdb_syntax_auto_OID_ldb_to_drsuapi(ctx, attr, in, mem_ctx, out);
1399 static WERROR _dsdb_syntax_OID_validate_numericoid(const struct dsdb_syntax_ctx *ctx,
1400 const struct dsdb_attribute *attr,
1401 const struct ldb_message_element *in)
1403 unsigned int i;
1404 TALLOC_CTX *tmp_ctx;
1406 tmp_ctx = talloc_new(ctx->ldb);
1407 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1409 for (i=0; i < in->num_values; i++) {
1410 DATA_BLOB blob;
1411 char *oid_out;
1412 const char *oid = (const char*)in->values[i].data;
1414 if (in->values[i].length == 0) {
1415 talloc_free(tmp_ctx);
1416 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1419 if (!ber_write_OID_String(tmp_ctx, &blob, oid)) {
1420 DEBUG(0,("ber_write_OID_String() failed for %s\n", oid));
1421 talloc_free(tmp_ctx);
1422 return WERR_INVALID_PARAMETER;
1425 if (!ber_read_OID_String(tmp_ctx, blob, &oid_out)) {
1426 DEBUG(0,("ber_read_OID_String() failed for %s\n",
1427 hex_encode_talloc(tmp_ctx, blob.data, blob.length)));
1428 talloc_free(tmp_ctx);
1429 return WERR_INVALID_PARAMETER;
1432 if (strcmp(oid, oid_out) != 0) {
1433 talloc_free(tmp_ctx);
1434 return WERR_INVALID_PARAMETER;
1438 talloc_free(tmp_ctx);
1439 return WERR_OK;
1442 static WERROR dsdb_syntax_OID_validate_ldb(const struct dsdb_syntax_ctx *ctx,
1443 const struct dsdb_attribute *attr,
1444 const struct ldb_message_element *in)
1446 WERROR status;
1447 struct drsuapi_DsReplicaAttribute drs_tmp;
1448 struct ldb_message_element ldb_tmp;
1449 TALLOC_CTX *tmp_ctx;
1451 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
1452 return WERR_FOOBAR;
1455 switch (attr->attributeID_id) {
1456 case DRSUAPI_ATTID_governsID:
1457 case DRSUAPI_ATTID_attributeID:
1458 case DRSUAPI_ATTID_attributeSyntax:
1459 return _dsdb_syntax_OID_validate_numericoid(ctx, attr, in);
1463 * TODO: optimize and verify this code
1466 tmp_ctx = talloc_new(ctx->ldb);
1467 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1469 status = dsdb_syntax_OID_ldb_to_drsuapi(ctx,
1470 attr,
1472 tmp_ctx,
1473 &drs_tmp);
1474 if (!W_ERROR_IS_OK(status)) {
1475 talloc_free(tmp_ctx);
1476 return status;
1479 status = dsdb_syntax_OID_drsuapi_to_ldb(ctx,
1480 attr,
1481 &drs_tmp,
1482 tmp_ctx,
1483 &ldb_tmp);
1484 if (!W_ERROR_IS_OK(status)) {
1485 talloc_free(tmp_ctx);
1486 return status;
1489 talloc_free(tmp_ctx);
1490 return WERR_OK;
1493 static WERROR dsdb_syntax_UNICODE_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
1494 const struct dsdb_attribute *attr,
1495 const struct drsuapi_DsReplicaAttribute *in,
1496 TALLOC_CTX *mem_ctx,
1497 struct ldb_message_element *out)
1499 unsigned int i;
1501 out->flags = 0;
1502 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
1503 W_ERROR_HAVE_NO_MEMORY(out->name);
1505 out->num_values = in->value_ctr.num_values;
1506 out->values = talloc_array(mem_ctx, struct ldb_val, out->num_values);
1507 W_ERROR_HAVE_NO_MEMORY(out->values);
1509 for (i=0; i < out->num_values; i++) {
1510 size_t converted_size = 0;
1511 char *str;
1513 if (in->value_ctr.values[i].blob == NULL) {
1514 return WERR_FOOBAR;
1517 if (in->value_ctr.values[i].blob->length == 0) {
1518 return WERR_FOOBAR;
1521 if (!convert_string_talloc(out->values,
1522 CH_UTF16, CH_UNIX,
1523 in->value_ctr.values[i].blob->data,
1524 in->value_ctr.values[i].blob->length,
1525 (void **)&str, &converted_size)) {
1526 return WERR_FOOBAR;
1529 out->values[i] = data_blob_string_const(str);
1532 return WERR_OK;
1535 static WERROR dsdb_syntax_UNICODE_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
1536 const struct dsdb_attribute *attr,
1537 const struct ldb_message_element *in,
1538 TALLOC_CTX *mem_ctx,
1539 struct drsuapi_DsReplicaAttribute *out)
1541 unsigned int i;
1542 DATA_BLOB *blobs;
1544 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
1545 return WERR_FOOBAR;
1548 out->attid = dsdb_attribute_get_attid(attr,
1549 ctx->is_schema_nc);
1550 out->value_ctr.num_values = in->num_values;
1551 out->value_ctr.values = talloc_array(mem_ctx,
1552 struct drsuapi_DsAttributeValue,
1553 in->num_values);
1554 W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1556 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1557 W_ERROR_HAVE_NO_MEMORY(blobs);
1559 for (i=0; i < in->num_values; i++) {
1560 out->value_ctr.values[i].blob = &blobs[i];
1562 if (!convert_string_talloc(blobs,
1563 CH_UNIX, CH_UTF16,
1564 in->values[i].data, in->values[i].length,
1565 (void **)&blobs[i].data, &blobs[i].length)) {
1566 return WERR_FOOBAR;
1570 return WERR_OK;
1573 static WERROR dsdb_syntax_UNICODE_validate_one_val(const struct dsdb_syntax_ctx *ctx,
1574 const struct dsdb_attribute *attr,
1575 const struct ldb_val *val)
1577 void *dst = NULL;
1578 size_t size;
1579 bool ok;
1581 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
1582 return WERR_FOOBAR;
1585 ok = convert_string_talloc(ctx->ldb,
1586 CH_UNIX, CH_UTF16,
1587 val->data,
1588 val->length,
1589 (void **)&dst,
1590 &size);
1591 TALLOC_FREE(dst);
1592 if (!ok) {
1593 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1596 if (attr->rangeLower) {
1597 if ((size/2) < *attr->rangeLower) {
1598 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1602 if (attr->rangeUpper) {
1603 if ((size/2) > *attr->rangeUpper) {
1604 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1608 return WERR_OK;
1611 static WERROR dsdb_syntax_UNICODE_validate_ldb(const struct dsdb_syntax_ctx *ctx,
1612 const struct dsdb_attribute *attr,
1613 const struct ldb_message_element *in)
1615 WERROR status;
1616 unsigned int i;
1618 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
1619 return WERR_FOOBAR;
1622 for (i=0; i < in->num_values; i++) {
1623 if (in->values[i].length == 0) {
1624 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1627 status = dsdb_syntax_UNICODE_validate_one_val(ctx,
1628 attr,
1629 &in->values[i]);
1630 if (!W_ERROR_IS_OK(status)) {
1631 return status;
1635 return WERR_OK;
1638 static WERROR dsdb_syntax_one_DN_drsuapi_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
1639 const struct dsdb_syntax *syntax,
1640 const DATA_BLOB *in, DATA_BLOB *out)
1642 struct drsuapi_DsReplicaObjectIdentifier3 id3;
1643 enum ndr_err_code ndr_err;
1644 DATA_BLOB guid_blob;
1645 struct ldb_dn *dn;
1646 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1647 int ret;
1648 NTSTATUS status;
1650 if (!tmp_ctx) {
1651 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1654 if (in == NULL) {
1655 talloc_free(tmp_ctx);
1656 return WERR_FOOBAR;
1659 if (in->length == 0) {
1660 talloc_free(tmp_ctx);
1661 return WERR_FOOBAR;
1665 /* windows sometimes sends an extra two pad bytes here */
1666 ndr_err = ndr_pull_struct_blob(in,
1667 tmp_ctx, &id3,
1668 (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3);
1669 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1670 status = ndr_map_error2ntstatus(ndr_err);
1671 talloc_free(tmp_ctx);
1672 return ntstatus_to_werror(status);
1675 dn = ldb_dn_new(tmp_ctx, ldb, id3.dn);
1676 if (!dn) {
1677 talloc_free(tmp_ctx);
1678 /* If this fails, it must be out of memory, as it does not do much parsing */
1679 W_ERROR_HAVE_NO_MEMORY(dn);
1682 if (!GUID_all_zero(&id3.guid)) {
1683 status = GUID_to_ndr_blob(&id3.guid, tmp_ctx, &guid_blob);
1684 if (!NT_STATUS_IS_OK(status)) {
1685 talloc_free(tmp_ctx);
1686 return ntstatus_to_werror(status);
1689 ret = ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
1690 if (ret != LDB_SUCCESS) {
1691 talloc_free(tmp_ctx);
1692 return WERR_FOOBAR;
1694 talloc_free(guid_blob.data);
1697 if (id3.__ndr_size_sid) {
1698 DATA_BLOB sid_blob;
1699 ndr_err = ndr_push_struct_blob(&sid_blob, tmp_ctx, &id3.sid,
1700 (ndr_push_flags_fn_t)ndr_push_dom_sid);
1701 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1702 status = ndr_map_error2ntstatus(ndr_err);
1703 talloc_free(tmp_ctx);
1704 return ntstatus_to_werror(status);
1707 ret = ldb_dn_set_extended_component(dn, "SID", &sid_blob);
1708 if (ret != LDB_SUCCESS) {
1709 talloc_free(tmp_ctx);
1710 return WERR_FOOBAR;
1714 *out = data_blob_string_const(ldb_dn_get_extended_linearized(mem_ctx, dn, 1));
1715 talloc_free(tmp_ctx);
1716 return WERR_OK;
1719 static WERROR dsdb_syntax_DN_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
1720 const struct dsdb_attribute *attr,
1721 const struct drsuapi_DsReplicaAttribute *in,
1722 TALLOC_CTX *mem_ctx,
1723 struct ldb_message_element *out)
1725 unsigned int i;
1727 out->flags = 0;
1728 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
1729 W_ERROR_HAVE_NO_MEMORY(out->name);
1731 out->num_values = in->value_ctr.num_values;
1732 out->values = talloc_array(mem_ctx, struct ldb_val, out->num_values);
1733 W_ERROR_HAVE_NO_MEMORY(out->values);
1735 for (i=0; i < out->num_values; i++) {
1736 WERROR status = dsdb_syntax_one_DN_drsuapi_to_ldb(out->values, ctx->ldb, attr->syntax,
1737 in->value_ctr.values[i].blob,
1738 &out->values[i]);
1739 if (!W_ERROR_IS_OK(status)) {
1740 return status;
1745 return WERR_OK;
1748 static WERROR dsdb_syntax_DN_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
1749 const struct dsdb_attribute *attr,
1750 const struct ldb_message_element *in,
1751 TALLOC_CTX *mem_ctx,
1752 struct drsuapi_DsReplicaAttribute *out)
1754 unsigned int i;
1755 DATA_BLOB *blobs;
1757 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
1758 return WERR_FOOBAR;
1761 out->attid = dsdb_attribute_get_attid(attr,
1762 ctx->is_schema_nc);
1763 out->value_ctr.num_values = in->num_values;
1764 out->value_ctr.values = talloc_array(mem_ctx,
1765 struct drsuapi_DsAttributeValue,
1766 in->num_values);
1767 W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1769 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1770 W_ERROR_HAVE_NO_MEMORY(blobs);
1772 for (i=0; i < in->num_values; i++) {
1773 struct drsuapi_DsReplicaObjectIdentifier3 id3;
1774 enum ndr_err_code ndr_err;
1775 struct ldb_dn *dn;
1776 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1777 NTSTATUS status;
1779 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1781 out->value_ctr.values[i].blob = &blobs[i];
1783 dn = ldb_dn_from_ldb_val(tmp_ctx, ctx->ldb, &in->values[i]);
1785 W_ERROR_HAVE_NO_MEMORY(dn);
1787 ZERO_STRUCT(id3);
1789 status = dsdb_get_extended_dn_guid(dn, &id3.guid, "GUID");
1790 if (!NT_STATUS_IS_OK(status) &&
1791 !NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1792 talloc_free(tmp_ctx);
1793 return ntstatus_to_werror(status);
1796 status = dsdb_get_extended_dn_sid(dn, &id3.sid, "SID");
1797 if (!NT_STATUS_IS_OK(status) &&
1798 !NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1799 talloc_free(tmp_ctx);
1800 return ntstatus_to_werror(status);
1803 id3.dn = ldb_dn_get_linearized(dn);
1805 ndr_err = ndr_push_struct_blob(&blobs[i], blobs, &id3, (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
1806 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1807 status = ndr_map_error2ntstatus(ndr_err);
1808 talloc_free(tmp_ctx);
1809 return ntstatus_to_werror(status);
1811 talloc_free(tmp_ctx);
1814 return WERR_OK;
1817 static WERROR dsdb_syntax_DN_validate_one_val(const struct dsdb_syntax_ctx *ctx,
1818 const struct dsdb_attribute *attr,
1819 const struct ldb_val *val,
1820 TALLOC_CTX *mem_ctx,
1821 struct dsdb_dn **_dsdb_dn)
1823 static const char * const extended_list[] = { "GUID", "SID", NULL };
1824 enum ndr_err_code ndr_err;
1825 struct GUID guid;
1826 struct dom_sid sid;
1827 const DATA_BLOB *sid_blob;
1828 struct dsdb_dn *dsdb_dn;
1829 struct ldb_dn *dn;
1830 char *dn_str;
1831 struct ldb_dn *dn2;
1832 char *dn2_str;
1833 int num_components;
1834 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1835 NTSTATUS status;
1837 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1839 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
1840 return WERR_FOOBAR;
1843 dsdb_dn = dsdb_dn_parse(tmp_ctx, ctx->ldb, val,
1844 attr->syntax->ldap_oid);
1845 if (!dsdb_dn) {
1846 talloc_free(tmp_ctx);
1847 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1849 dn = dsdb_dn->dn;
1851 dn2 = ldb_dn_copy(tmp_ctx, dn);
1852 if (dn == NULL) {
1853 talloc_free(tmp_ctx);
1854 return WERR_NOMEM;
1857 num_components = ldb_dn_get_comp_num(dn);
1859 status = dsdb_get_extended_dn_guid(dn, &guid, "GUID");
1860 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1861 num_components++;
1862 } else if (!NT_STATUS_IS_OK(status)) {
1863 talloc_free(tmp_ctx);
1864 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1867 sid_blob = ldb_dn_get_extended_component(dn, "SID");
1868 if (sid_blob) {
1869 num_components++;
1870 ndr_err = ndr_pull_struct_blob_all(sid_blob,
1871 tmp_ctx,
1872 &sid,
1873 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
1874 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1875 talloc_free(tmp_ctx);
1876 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1880 /* Do not allow links to the RootDSE */
1881 if (num_components == 0) {
1882 talloc_free(tmp_ctx);
1883 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1887 * We need to check that only "GUID" and "SID" are
1888 * specified as extended components, we do that
1889 * by comparing the dn's after removing all components
1890 * from one dn and only the allowed subset from the other
1891 * one.
1893 ldb_dn_extended_filter(dn, extended_list);
1895 dn_str = ldb_dn_get_extended_linearized(tmp_ctx, dn, 0);
1896 if (dn_str == NULL) {
1897 talloc_free(tmp_ctx);
1898 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1900 dn2_str = ldb_dn_get_extended_linearized(tmp_ctx, dn2, 0);
1901 if (dn2_str == NULL) {
1902 talloc_free(tmp_ctx);
1903 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1906 if (strcmp(dn_str, dn2_str) != 0) {
1907 talloc_free(tmp_ctx);
1908 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1911 *_dsdb_dn = talloc_move(mem_ctx, &dsdb_dn);
1912 talloc_free(tmp_ctx);
1913 return WERR_OK;
1916 static WERROR dsdb_syntax_DN_validate_ldb(const struct dsdb_syntax_ctx *ctx,
1917 const struct dsdb_attribute *attr,
1918 const struct ldb_message_element *in)
1920 unsigned int i;
1922 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
1923 return WERR_FOOBAR;
1926 for (i=0; i < in->num_values; i++) {
1927 WERROR status;
1928 struct dsdb_dn *dsdb_dn;
1929 TALLOC_CTX *tmp_ctx = talloc_new(ctx->ldb);
1930 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1932 status = dsdb_syntax_DN_validate_one_val(ctx,
1933 attr,
1934 &in->values[i],
1935 tmp_ctx, &dsdb_dn);
1936 if (!W_ERROR_IS_OK(status)) {
1937 talloc_free(tmp_ctx);
1938 return status;
1941 if (dsdb_dn->dn_format != DSDB_NORMAL_DN) {
1942 talloc_free(tmp_ctx);
1943 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1946 talloc_free(tmp_ctx);
1949 return WERR_OK;
1952 static WERROR dsdb_syntax_DN_BINARY_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
1953 const struct dsdb_attribute *attr,
1954 const struct drsuapi_DsReplicaAttribute *in,
1955 TALLOC_CTX *mem_ctx,
1956 struct ldb_message_element *out)
1958 unsigned int i;
1959 int ret;
1961 out->flags = 0;
1962 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
1963 W_ERROR_HAVE_NO_MEMORY(out->name);
1965 out->num_values = in->value_ctr.num_values;
1966 out->values = talloc_array(mem_ctx, struct ldb_val, out->num_values);
1967 W_ERROR_HAVE_NO_MEMORY(out->values);
1969 for (i=0; i < out->num_values; i++) {
1970 struct drsuapi_DsReplicaObjectIdentifier3Binary id3;
1971 enum ndr_err_code ndr_err;
1972 DATA_BLOB guid_blob;
1973 struct ldb_dn *dn;
1974 struct dsdb_dn *dsdb_dn;
1975 NTSTATUS status;
1976 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1977 if (!tmp_ctx) {
1978 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1981 if (in->value_ctr.values[i].blob == NULL) {
1982 talloc_free(tmp_ctx);
1983 return WERR_FOOBAR;
1986 if (in->value_ctr.values[i].blob->length == 0) {
1987 talloc_free(tmp_ctx);
1988 return WERR_FOOBAR;
1992 /* windows sometimes sends an extra two pad bytes here */
1993 ndr_err = ndr_pull_struct_blob(in->value_ctr.values[i].blob,
1994 tmp_ctx, &id3,
1995 (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3Binary);
1996 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1997 status = ndr_map_error2ntstatus(ndr_err);
1998 talloc_free(tmp_ctx);
1999 return ntstatus_to_werror(status);
2002 dn = ldb_dn_new(tmp_ctx, ctx->ldb, id3.dn);
2003 if (!dn) {
2004 talloc_free(tmp_ctx);
2005 /* If this fails, it must be out of memory, as it does not do much parsing */
2006 W_ERROR_HAVE_NO_MEMORY(dn);
2009 if (!GUID_all_zero(&id3.guid)) {
2010 status = GUID_to_ndr_blob(&id3.guid, tmp_ctx, &guid_blob);
2011 if (!NT_STATUS_IS_OK(status)) {
2012 talloc_free(tmp_ctx);
2013 return ntstatus_to_werror(status);
2016 ret = ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
2017 if (ret != LDB_SUCCESS) {
2018 talloc_free(tmp_ctx);
2019 return WERR_FOOBAR;
2021 talloc_free(guid_blob.data);
2024 if (id3.__ndr_size_sid) {
2025 DATA_BLOB sid_blob;
2026 ndr_err = ndr_push_struct_blob(&sid_blob, tmp_ctx, &id3.sid,
2027 (ndr_push_flags_fn_t)ndr_push_dom_sid);
2028 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2029 status = ndr_map_error2ntstatus(ndr_err);
2030 talloc_free(tmp_ctx);
2031 return ntstatus_to_werror(status);
2034 ret = ldb_dn_set_extended_component(dn, "SID", &sid_blob);
2035 if (ret != LDB_SUCCESS) {
2036 talloc_free(tmp_ctx);
2037 return WERR_FOOBAR;
2041 /* set binary stuff */
2042 dsdb_dn = dsdb_dn_construct(tmp_ctx, dn, id3.binary, attr->syntax->ldap_oid);
2043 if (!dsdb_dn) {
2044 /* If this fails, it must be out of memory, we know the ldap_oid is valid */
2045 talloc_free(tmp_ctx);
2046 W_ERROR_HAVE_NO_MEMORY(dsdb_dn);
2048 out->values[i] = data_blob_string_const(dsdb_dn_get_extended_linearized(out->values, dsdb_dn, 1));
2049 talloc_free(tmp_ctx);
2052 return WERR_OK;
2055 static WERROR dsdb_syntax_DN_BINARY_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
2056 const struct dsdb_attribute *attr,
2057 const struct ldb_message_element *in,
2058 TALLOC_CTX *mem_ctx,
2059 struct drsuapi_DsReplicaAttribute *out)
2061 unsigned int i;
2062 DATA_BLOB *blobs;
2064 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
2065 return WERR_FOOBAR;
2068 out->attid = dsdb_attribute_get_attid(attr,
2069 ctx->is_schema_nc);
2070 out->value_ctr.num_values = in->num_values;
2071 out->value_ctr.values = talloc_array(mem_ctx,
2072 struct drsuapi_DsAttributeValue,
2073 in->num_values);
2074 W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
2076 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
2077 W_ERROR_HAVE_NO_MEMORY(blobs);
2079 for (i=0; i < in->num_values; i++) {
2080 struct drsuapi_DsReplicaObjectIdentifier3Binary id3;
2081 enum ndr_err_code ndr_err;
2082 const DATA_BLOB *sid_blob;
2083 struct dsdb_dn *dsdb_dn;
2084 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2085 NTSTATUS status;
2087 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
2089 out->value_ctr.values[i].blob = &blobs[i];
2091 dsdb_dn = dsdb_dn_parse(tmp_ctx, ctx->ldb, &in->values[i], attr->syntax->ldap_oid);
2093 if (!dsdb_dn) {
2094 talloc_free(tmp_ctx);
2095 return ntstatus_to_werror(NT_STATUS_INVALID_PARAMETER);
2098 ZERO_STRUCT(id3);
2100 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &id3.guid, "GUID");
2101 if (!NT_STATUS_IS_OK(status) &&
2102 !NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2103 talloc_free(tmp_ctx);
2104 return ntstatus_to_werror(status);
2107 sid_blob = ldb_dn_get_extended_component(dsdb_dn->dn, "SID");
2108 if (sid_blob) {
2110 ndr_err = ndr_pull_struct_blob_all(sid_blob,
2111 tmp_ctx, &id3.sid,
2112 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
2113 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2114 status = ndr_map_error2ntstatus(ndr_err);
2115 talloc_free(tmp_ctx);
2116 return ntstatus_to_werror(status);
2120 id3.dn = ldb_dn_get_linearized(dsdb_dn->dn);
2122 /* get binary stuff */
2123 id3.binary = dsdb_dn->extra_part;
2125 ndr_err = ndr_push_struct_blob(&blobs[i], blobs, &id3, (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3Binary);
2126 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2127 status = ndr_map_error2ntstatus(ndr_err);
2128 talloc_free(tmp_ctx);
2129 return ntstatus_to_werror(status);
2131 talloc_free(tmp_ctx);
2134 return WERR_OK;
2137 static WERROR dsdb_syntax_DN_BINARY_validate_ldb(const struct dsdb_syntax_ctx *ctx,
2138 const struct dsdb_attribute *attr,
2139 const struct ldb_message_element *in)
2141 unsigned int i;
2143 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
2144 return WERR_FOOBAR;
2147 for (i=0; i < in->num_values; i++) {
2148 WERROR status;
2149 struct dsdb_dn *dsdb_dn;
2150 TALLOC_CTX *tmp_ctx = talloc_new(ctx->ldb);
2151 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
2153 status = dsdb_syntax_DN_validate_one_val(ctx,
2154 attr,
2155 &in->values[i],
2156 tmp_ctx, &dsdb_dn);
2157 if (!W_ERROR_IS_OK(status)) {
2158 talloc_free(tmp_ctx);
2159 return status;
2162 if (dsdb_dn->dn_format != DSDB_BINARY_DN) {
2163 talloc_free(tmp_ctx);
2164 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
2167 status = dsdb_syntax_DATA_BLOB_validate_one_val(ctx,
2168 attr,
2169 &dsdb_dn->extra_part);
2170 if (!W_ERROR_IS_OK(status)) {
2171 talloc_free(tmp_ctx);
2172 return status;
2175 talloc_free(tmp_ctx);
2178 return WERR_OK;
2181 static WERROR dsdb_syntax_DN_STRING_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
2182 const struct dsdb_attribute *attr,
2183 const struct drsuapi_DsReplicaAttribute *in,
2184 TALLOC_CTX *mem_ctx,
2185 struct ldb_message_element *out)
2187 return dsdb_syntax_DN_BINARY_drsuapi_to_ldb(ctx,
2188 attr,
2190 mem_ctx,
2191 out);
2194 static WERROR dsdb_syntax_DN_STRING_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
2195 const struct dsdb_attribute *attr,
2196 const struct ldb_message_element *in,
2197 TALLOC_CTX *mem_ctx,
2198 struct drsuapi_DsReplicaAttribute *out)
2200 return dsdb_syntax_DN_BINARY_ldb_to_drsuapi(ctx,
2201 attr,
2203 mem_ctx,
2204 out);
2207 static WERROR dsdb_syntax_DN_STRING_validate_ldb(const struct dsdb_syntax_ctx *ctx,
2208 const struct dsdb_attribute *attr,
2209 const struct ldb_message_element *in)
2211 unsigned int i;
2213 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
2214 return WERR_FOOBAR;
2217 for (i=0; i < in->num_values; i++) {
2218 WERROR status;
2219 struct dsdb_dn *dsdb_dn;
2220 TALLOC_CTX *tmp_ctx = talloc_new(ctx->ldb);
2221 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
2223 status = dsdb_syntax_DN_validate_one_val(ctx,
2224 attr,
2225 &in->values[i],
2226 tmp_ctx, &dsdb_dn);
2227 if (!W_ERROR_IS_OK(status)) {
2228 talloc_free(tmp_ctx);
2229 return status;
2232 if (dsdb_dn->dn_format != DSDB_STRING_DN) {
2233 talloc_free(tmp_ctx);
2234 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
2237 status = dsdb_syntax_UNICODE_validate_one_val(ctx,
2238 attr,
2239 &dsdb_dn->extra_part);
2240 if (!W_ERROR_IS_OK(status)) {
2241 talloc_free(tmp_ctx);
2242 return status;
2245 talloc_free(tmp_ctx);
2248 return WERR_OK;
2251 static WERROR dsdb_syntax_PRESENTATION_ADDRESS_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
2252 const struct dsdb_attribute *attr,
2253 const struct drsuapi_DsReplicaAttribute *in,
2254 TALLOC_CTX *mem_ctx,
2255 struct ldb_message_element *out)
2257 unsigned int i;
2259 out->flags = 0;
2260 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
2261 W_ERROR_HAVE_NO_MEMORY(out->name);
2263 out->num_values = in->value_ctr.num_values;
2264 out->values = talloc_array(mem_ctx, struct ldb_val, out->num_values);
2265 W_ERROR_HAVE_NO_MEMORY(out->values);
2267 for (i=0; i < out->num_values; i++) {
2268 size_t len;
2269 size_t converted_size = 0;
2270 char *str;
2272 if (in->value_ctr.values[i].blob == NULL) {
2273 return WERR_FOOBAR;
2276 if (in->value_ctr.values[i].blob->length < 4) {
2277 return WERR_FOOBAR;
2280 len = IVAL(in->value_ctr.values[i].blob->data, 0);
2282 if (len != in->value_ctr.values[i].blob->length) {
2283 return WERR_FOOBAR;
2286 if (!convert_string_talloc(out->values, CH_UTF16, CH_UNIX,
2287 in->value_ctr.values[i].blob->data+4,
2288 in->value_ctr.values[i].blob->length-4,
2289 (void **)&str, &converted_size)) {
2290 return WERR_FOOBAR;
2293 out->values[i] = data_blob_string_const(str);
2296 return WERR_OK;
2299 static WERROR dsdb_syntax_PRESENTATION_ADDRESS_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
2300 const struct dsdb_attribute *attr,
2301 const struct ldb_message_element *in,
2302 TALLOC_CTX *mem_ctx,
2303 struct drsuapi_DsReplicaAttribute *out)
2305 unsigned int i;
2306 DATA_BLOB *blobs;
2308 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
2309 return WERR_FOOBAR;
2312 out->attid = dsdb_attribute_get_attid(attr,
2313 ctx->is_schema_nc);
2314 out->value_ctr.num_values = in->num_values;
2315 out->value_ctr.values = talloc_array(mem_ctx,
2316 struct drsuapi_DsAttributeValue,
2317 in->num_values);
2318 W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
2320 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
2321 W_ERROR_HAVE_NO_MEMORY(blobs);
2323 for (i=0; i < in->num_values; i++) {
2324 uint8_t *data;
2325 size_t ret;
2327 out->value_ctr.values[i].blob = &blobs[i];
2329 if (!convert_string_talloc(blobs, CH_UNIX, CH_UTF16,
2330 in->values[i].data,
2331 in->values[i].length,
2332 (void **)&data, &ret)) {
2333 return WERR_FOOBAR;
2336 blobs[i] = data_blob_talloc(blobs, NULL, 4 + ret);
2337 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
2339 SIVAL(blobs[i].data, 0, 4 + ret);
2341 if (ret > 0) {
2342 memcpy(blobs[i].data + 4, data, ret);
2343 talloc_free(data);
2347 return WERR_OK;
2350 static WERROR dsdb_syntax_PRESENTATION_ADDRESS_validate_ldb(const struct dsdb_syntax_ctx *ctx,
2351 const struct dsdb_attribute *attr,
2352 const struct ldb_message_element *in)
2354 return dsdb_syntax_UNICODE_validate_ldb(ctx,
2355 attr,
2356 in);
2359 #define OMOBJECTCLASS(val) { .length = sizeof(val) - 1, .data = discard_const_p(uint8_t, val) }
2361 static const struct dsdb_syntax dsdb_syntaxes[] = {
2363 .name = "Boolean",
2364 .ldap_oid = LDB_SYNTAX_BOOLEAN,
2365 .oMSyntax = 1,
2366 .attributeSyntax_oid = "2.5.5.8",
2367 .drsuapi_to_ldb = dsdb_syntax_BOOL_drsuapi_to_ldb,
2368 .ldb_to_drsuapi = dsdb_syntax_BOOL_ldb_to_drsuapi,
2369 .validate_ldb = dsdb_syntax_BOOL_validate_ldb,
2370 .equality = "booleanMatch",
2371 .comment = "Boolean",
2372 .auto_normalise = true
2374 .name = "Integer",
2375 .ldap_oid = LDB_SYNTAX_INTEGER,
2376 .oMSyntax = 2,
2377 .attributeSyntax_oid = "2.5.5.9",
2378 .drsuapi_to_ldb = dsdb_syntax_INT32_drsuapi_to_ldb,
2379 .ldb_to_drsuapi = dsdb_syntax_INT32_ldb_to_drsuapi,
2380 .validate_ldb = dsdb_syntax_INT32_validate_ldb,
2381 .equality = "integerMatch",
2382 .comment = "Integer",
2383 .ldb_syntax = LDB_SYNTAX_SAMBA_INT32,
2384 .auto_normalise = true
2386 .name = "String(Octet)",
2387 .ldap_oid = LDB_SYNTAX_OCTET_STRING,
2388 .oMSyntax = 4,
2389 .attributeSyntax_oid = "2.5.5.10",
2390 .drsuapi_to_ldb = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2391 .ldb_to_drsuapi = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2392 .validate_ldb = dsdb_syntax_DATA_BLOB_validate_ldb,
2393 .equality = "octetStringMatch",
2394 .comment = "Octet String",
2396 .name = "String(Sid)",
2397 .ldap_oid = LDB_SYNTAX_OCTET_STRING,
2398 .oMSyntax = 4,
2399 .attributeSyntax_oid = "2.5.5.17",
2400 .drsuapi_to_ldb = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2401 .ldb_to_drsuapi = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2402 .validate_ldb = dsdb_syntax_DATA_BLOB_validate_ldb,
2403 .equality = "octetStringMatch",
2404 .comment = "Octet String - Security Identifier (SID)",
2405 .ldb_syntax = LDB_SYNTAX_SAMBA_SID
2407 .name = "String(Object-Identifier)",
2408 .ldap_oid = "1.3.6.1.4.1.1466.115.121.1.38",
2409 .oMSyntax = 6,
2410 .attributeSyntax_oid = "2.5.5.2",
2411 .drsuapi_to_ldb = dsdb_syntax_OID_drsuapi_to_ldb,
2412 .ldb_to_drsuapi = dsdb_syntax_OID_ldb_to_drsuapi,
2413 .validate_ldb = dsdb_syntax_OID_validate_ldb,
2414 .equality = "caseIgnoreMatch", /* Would use "objectIdentifierMatch" but most are ldap attribute/class names */
2415 .comment = "OID String",
2416 .ldb_syntax = LDB_SYNTAX_DIRECTORY_STRING
2418 .name = "Enumeration",
2419 .ldap_oid = LDB_SYNTAX_INTEGER,
2420 .oMSyntax = 10,
2421 .attributeSyntax_oid = "2.5.5.9",
2422 .drsuapi_to_ldb = dsdb_syntax_INT32_drsuapi_to_ldb,
2423 .ldb_to_drsuapi = dsdb_syntax_INT32_ldb_to_drsuapi,
2424 .validate_ldb = dsdb_syntax_INT32_validate_ldb,
2425 .ldb_syntax = LDB_SYNTAX_SAMBA_INT32,
2426 .auto_normalise = true
2428 /* not used in w2k3 forest */
2429 .name = "String(Numeric)",
2430 .ldap_oid = "1.3.6.1.4.1.1466.115.121.1.36",
2431 .oMSyntax = 18,
2432 .attributeSyntax_oid = "2.5.5.6",
2433 .drsuapi_to_ldb = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2434 .ldb_to_drsuapi = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2435 .validate_ldb = dsdb_syntax_DATA_BLOB_validate_ldb,
2436 .equality = "numericStringMatch",
2437 .substring = "numericStringSubstringsMatch",
2438 .comment = "Numeric String",
2439 .ldb_syntax = LDB_SYNTAX_DIRECTORY_STRING,
2441 .name = "String(Printable)",
2442 .ldap_oid = "1.3.6.1.4.1.1466.115.121.1.44",
2443 .oMSyntax = 19,
2444 .attributeSyntax_oid = "2.5.5.5",
2445 .drsuapi_to_ldb = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2446 .ldb_to_drsuapi = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2447 .validate_ldb = dsdb_syntax_DATA_BLOB_validate_ldb,
2448 .ldb_syntax = LDB_SYNTAX_OCTET_STRING,
2450 .name = "String(Teletex)",
2451 .ldap_oid = "1.2.840.113556.1.4.905",
2452 .oMSyntax = 20,
2453 .attributeSyntax_oid = "2.5.5.4",
2454 .drsuapi_to_ldb = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2455 .ldb_to_drsuapi = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2456 .validate_ldb = dsdb_syntax_DATA_BLOB_validate_ldb,
2457 .equality = "caseIgnoreMatch",
2458 .substring = "caseIgnoreSubstringsMatch",
2459 .comment = "Case Insensitive String",
2460 .ldb_syntax = LDB_SYNTAX_DIRECTORY_STRING,
2462 .name = "String(IA5)",
2463 .ldap_oid = "1.3.6.1.4.1.1466.115.121.1.26",
2464 .oMSyntax = 22,
2465 .attributeSyntax_oid = "2.5.5.5",
2466 .drsuapi_to_ldb = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2467 .ldb_to_drsuapi = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2468 .validate_ldb = dsdb_syntax_DATA_BLOB_validate_ldb,
2469 .equality = "caseExactIA5Match",
2470 .comment = "Printable String",
2471 .ldb_syntax = LDB_SYNTAX_OCTET_STRING,
2473 .name = "String(UTC-Time)",
2474 .ldap_oid = "1.3.6.1.4.1.1466.115.121.1.53",
2475 .oMSyntax = 23,
2476 .attributeSyntax_oid = "2.5.5.11",
2477 .drsuapi_to_ldb = dsdb_syntax_NTTIME_UTC_drsuapi_to_ldb,
2478 .ldb_to_drsuapi = dsdb_syntax_NTTIME_UTC_ldb_to_drsuapi,
2479 .validate_ldb = dsdb_syntax_NTTIME_UTC_validate_ldb,
2480 .equality = "generalizedTimeMatch",
2481 .comment = "UTC Time",
2482 .auto_normalise = true
2484 .name = "String(Generalized-Time)",
2485 .ldap_oid = "1.3.6.1.4.1.1466.115.121.1.24",
2486 .oMSyntax = 24,
2487 .attributeSyntax_oid = "2.5.5.11",
2488 .drsuapi_to_ldb = dsdb_syntax_NTTIME_drsuapi_to_ldb,
2489 .ldb_to_drsuapi = dsdb_syntax_NTTIME_ldb_to_drsuapi,
2490 .validate_ldb = dsdb_syntax_NTTIME_validate_ldb,
2491 .equality = "generalizedTimeMatch",
2492 .comment = "Generalized Time",
2493 .ldb_syntax = LDB_SYNTAX_UTC_TIME,
2494 .auto_normalise = true
2496 /* not used in w2k3 schema */
2497 .name = "String(Case Sensitive)",
2498 .ldap_oid = "1.2.840.113556.1.4.1362",
2499 .oMSyntax = 27,
2500 .attributeSyntax_oid = "2.5.5.3",
2501 .drsuapi_to_ldb = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2502 .ldb_to_drsuapi = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2503 .validate_ldb = dsdb_syntax_DATA_BLOB_validate_ldb,
2504 .equality = "caseExactMatch",
2505 .substring = "caseExactSubstringsMatch",
2506 /* TODO (kim): according to LDAP rfc we should be using same comparison
2507 * as Directory String (LDB_SYNTAX_DIRECTORY_STRING), but case sensitive.
2508 * But according to ms docs binary compare should do the job:
2509 * http://msdn.microsoft.com/en-us/library/cc223200(v=PROT.10).aspx */
2510 .ldb_syntax = LDB_SYNTAX_OCTET_STRING,
2512 .name = "String(Unicode)",
2513 .ldap_oid = LDB_SYNTAX_DIRECTORY_STRING,
2514 .oMSyntax = 64,
2515 .attributeSyntax_oid = "2.5.5.12",
2516 .drsuapi_to_ldb = dsdb_syntax_UNICODE_drsuapi_to_ldb,
2517 .ldb_to_drsuapi = dsdb_syntax_UNICODE_ldb_to_drsuapi,
2518 .validate_ldb = dsdb_syntax_UNICODE_validate_ldb,
2519 .equality = "caseIgnoreMatch",
2520 .substring = "caseIgnoreSubstringsMatch",
2521 .comment = "Directory String",
2523 .name = "Interval/LargeInteger",
2524 .ldap_oid = "1.2.840.113556.1.4.906",
2525 .oMSyntax = 65,
2526 .attributeSyntax_oid = "2.5.5.16",
2527 .drsuapi_to_ldb = dsdb_syntax_INT64_drsuapi_to_ldb,
2528 .ldb_to_drsuapi = dsdb_syntax_INT64_ldb_to_drsuapi,
2529 .validate_ldb = dsdb_syntax_INT64_validate_ldb,
2530 .equality = "integerMatch",
2531 .comment = "Large Integer",
2532 .ldb_syntax = LDB_SYNTAX_INTEGER,
2533 .auto_normalise = true
2535 .name = "String(NT-Sec-Desc)",
2536 .ldap_oid = LDB_SYNTAX_SAMBA_SECURITY_DESCRIPTOR,
2537 .oMSyntax = 66,
2538 .attributeSyntax_oid = "2.5.5.15",
2539 .drsuapi_to_ldb = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2540 .ldb_to_drsuapi = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2541 .validate_ldb = dsdb_syntax_DATA_BLOB_validate_ldb,
2543 .name = "Object(DS-DN)",
2544 .ldap_oid = LDB_SYNTAX_DN,
2545 .oMSyntax = 127,
2546 .oMObjectClass = OMOBJECTCLASS("\x2b\x0c\x02\x87\x73\x1c\x00\x85\x4a"),
2547 .attributeSyntax_oid = "2.5.5.1",
2548 .drsuapi_to_ldb = dsdb_syntax_DN_drsuapi_to_ldb,
2549 .ldb_to_drsuapi = dsdb_syntax_DN_ldb_to_drsuapi,
2550 .validate_ldb = dsdb_syntax_DN_validate_ldb,
2551 .equality = "distinguishedNameMatch",
2552 .comment = "Object(DS-DN) == a DN",
2554 .name = "Object(DN-Binary)",
2555 .ldap_oid = DSDB_SYNTAX_BINARY_DN,
2556 .oMSyntax = 127,
2557 .oMObjectClass = OMOBJECTCLASS("\x2a\x86\x48\x86\xf7\x14\x01\x01\x01\x0b"),
2558 .attributeSyntax_oid = "2.5.5.7",
2559 .drsuapi_to_ldb = dsdb_syntax_DN_BINARY_drsuapi_to_ldb,
2560 .ldb_to_drsuapi = dsdb_syntax_DN_BINARY_ldb_to_drsuapi,
2561 .validate_ldb = dsdb_syntax_DN_BINARY_validate_ldb,
2562 .equality = "octetStringMatch",
2563 .comment = "OctetString: Binary+DN",
2565 /* not used in w2k3 schema, but used in Exchange schema*/
2566 .name = "Object(OR-Name)",
2567 .ldap_oid = DSDB_SYNTAX_OR_NAME,
2568 .oMSyntax = 127,
2569 .oMObjectClass = OMOBJECTCLASS("\x56\x06\x01\x02\x05\x0b\x1D"),
2570 .attributeSyntax_oid = "2.5.5.7",
2571 .drsuapi_to_ldb = dsdb_syntax_DN_BINARY_drsuapi_to_ldb,
2572 .ldb_to_drsuapi = dsdb_syntax_DN_BINARY_ldb_to_drsuapi,
2573 .validate_ldb = dsdb_syntax_DN_BINARY_validate_ldb,
2574 .equality = "caseIgnoreMatch",
2575 .ldb_syntax = LDB_SYNTAX_DN,
2578 * TODO: verify if DATA_BLOB is correct here...!
2580 * repsFrom and repsTo are the only attributes using
2581 * this attribute syntax, but they're not replicated...
2583 .name = "Object(Replica-Link)",
2584 .ldap_oid = "1.3.6.1.4.1.1466.115.121.1.40",
2585 .oMSyntax = 127,
2586 .oMObjectClass = OMOBJECTCLASS("\x2a\x86\x48\x86\xf7\x14\x01\x01\x01\x06"),
2587 .attributeSyntax_oid = "2.5.5.10",
2588 .drsuapi_to_ldb = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2589 .ldb_to_drsuapi = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2590 .validate_ldb = dsdb_syntax_DATA_BLOB_validate_ldb,
2592 .name = "Object(Presentation-Address)",
2593 .ldap_oid = "1.3.6.1.4.1.1466.115.121.1.43",
2594 .oMSyntax = 127,
2595 .oMObjectClass = OMOBJECTCLASS("\x2b\x0c\x02\x87\x73\x1c\x00\x85\x5c"),
2596 .attributeSyntax_oid = "2.5.5.13",
2597 .drsuapi_to_ldb = dsdb_syntax_PRESENTATION_ADDRESS_drsuapi_to_ldb,
2598 .ldb_to_drsuapi = dsdb_syntax_PRESENTATION_ADDRESS_ldb_to_drsuapi,
2599 .validate_ldb = dsdb_syntax_PRESENTATION_ADDRESS_validate_ldb,
2600 .comment = "Presentation Address",
2601 .ldb_syntax = LDB_SYNTAX_DIRECTORY_STRING,
2603 /* not used in w2k3 schema */
2604 .name = "Object(Access-Point)",
2605 .ldap_oid = "1.3.6.1.4.1.1466.115.121.1.2",
2606 .oMSyntax = 127,
2607 .oMObjectClass = OMOBJECTCLASS("\x2b\x0c\x02\x87\x73\x1c\x00\x85\x3e"),
2608 .attributeSyntax_oid = "2.5.5.14",
2609 .drsuapi_to_ldb = dsdb_syntax_FOOBAR_drsuapi_to_ldb,
2610 .ldb_to_drsuapi = dsdb_syntax_FOOBAR_ldb_to_drsuapi,
2611 .validate_ldb = dsdb_syntax_FOOBAR_validate_ldb,
2612 .ldb_syntax = LDB_SYNTAX_DIRECTORY_STRING,
2614 /* not used in w2k3 schema */
2615 .name = "Object(DN-String)",
2616 .ldap_oid = DSDB_SYNTAX_STRING_DN,
2617 .oMSyntax = 127,
2618 .oMObjectClass = OMOBJECTCLASS("\x2a\x86\x48\x86\xf7\x14\x01\x01\x01\x0c"),
2619 .attributeSyntax_oid = "2.5.5.14",
2620 .drsuapi_to_ldb = dsdb_syntax_DN_STRING_drsuapi_to_ldb,
2621 .ldb_to_drsuapi = dsdb_syntax_DN_STRING_ldb_to_drsuapi,
2622 .validate_ldb = dsdb_syntax_DN_STRING_validate_ldb,
2623 .equality = "octetStringMatch",
2624 .comment = "OctetString: String+DN",
2628 const struct dsdb_syntax *find_syntax_map_by_ad_oid(const char *ad_oid)
2630 unsigned int i;
2631 for (i=0; dsdb_syntaxes[i].ldap_oid; i++) {
2632 if (strcasecmp(ad_oid, dsdb_syntaxes[i].attributeSyntax_oid) == 0) {
2633 return &dsdb_syntaxes[i];
2636 return NULL;
2639 const struct dsdb_syntax *find_syntax_map_by_ad_syntax(int oMSyntax)
2641 unsigned int i;
2642 for (i=0; dsdb_syntaxes[i].ldap_oid; i++) {
2643 if (oMSyntax == dsdb_syntaxes[i].oMSyntax) {
2644 return &dsdb_syntaxes[i];
2647 return NULL;
2650 const struct dsdb_syntax *find_syntax_map_by_standard_oid(const char *standard_oid)
2652 unsigned int i;
2653 for (i=0; dsdb_syntaxes[i].ldap_oid; i++) {
2654 if (strcasecmp(standard_oid, dsdb_syntaxes[i].ldap_oid) == 0) {
2655 return &dsdb_syntaxes[i];
2658 return NULL;
2661 const struct dsdb_syntax *dsdb_syntax_for_attribute(const struct dsdb_attribute *attr)
2663 unsigned int i;
2665 for (i=0; i < ARRAY_SIZE(dsdb_syntaxes); i++) {
2666 if (attr->oMSyntax != dsdb_syntaxes[i].oMSyntax) continue;
2668 if (attr->oMObjectClass.length != dsdb_syntaxes[i].oMObjectClass.length) continue;
2670 if (attr->oMObjectClass.length) {
2671 int ret;
2672 ret = memcmp(attr->oMObjectClass.data,
2673 dsdb_syntaxes[i].oMObjectClass.data,
2674 attr->oMObjectClass.length);
2675 if (ret != 0) continue;
2678 if (strcmp(attr->attributeSyntax_oid, dsdb_syntaxes[i].attributeSyntax_oid) != 0) continue;
2680 return &dsdb_syntaxes[i];
2683 return NULL;
2686 WERROR dsdb_attribute_drsuapi_to_ldb(struct ldb_context *ldb,
2687 const struct dsdb_schema *schema,
2688 const struct dsdb_schema_prefixmap *pfm_remote,
2689 const struct drsuapi_DsReplicaAttribute *in,
2690 TALLOC_CTX *mem_ctx,
2691 struct ldb_message_element *out)
2693 const struct dsdb_attribute *sa;
2694 struct dsdb_syntax_ctx syntax_ctx;
2695 uint32_t attid_local;
2697 /* use default syntax conversion context */
2698 dsdb_syntax_ctx_init(&syntax_ctx, ldb, schema);
2699 syntax_ctx.pfm_remote = pfm_remote;
2701 switch (dsdb_pfm_get_attid_type(in->attid)) {
2702 case DSDB_ATTID_TYPE_PFM:
2703 /* map remote ATTID to local ATTID */
2704 if (!dsdb_syntax_attid_from_remote_attid(&syntax_ctx, mem_ctx, in->attid, &attid_local)) {
2705 DEBUG(0,(__location__ ": Can't find local ATTID for 0x%08X\n",
2706 in->attid));
2707 return WERR_FOOBAR;
2709 break;
2710 case DSDB_ATTID_TYPE_INTID:
2711 /* use IntId value directly */
2712 attid_local = in->attid;
2713 break;
2714 default:
2715 /* we should never get here */
2716 DEBUG(0,(__location__ ": Invalid ATTID type passed for conversion - 0x%08X\n",
2717 in->attid));
2718 return WERR_INVALID_PARAMETER;
2721 sa = dsdb_attribute_by_attributeID_id(schema, attid_local);
2722 if (!sa) {
2723 DEBUG(1,(__location__ ": Unknown attributeID_id 0x%08X\n", in->attid));
2724 return WERR_FOOBAR;
2727 return sa->syntax->drsuapi_to_ldb(&syntax_ctx, sa, in, mem_ctx, out);
2730 WERROR dsdb_attribute_ldb_to_drsuapi(struct ldb_context *ldb,
2731 const struct dsdb_schema *schema,
2732 const struct ldb_message_element *in,
2733 TALLOC_CTX *mem_ctx,
2734 struct drsuapi_DsReplicaAttribute *out)
2736 const struct dsdb_attribute *sa;
2737 struct dsdb_syntax_ctx syntax_ctx;
2739 sa = dsdb_attribute_by_lDAPDisplayName(schema, in->name);
2740 if (!sa) {
2741 return WERR_FOOBAR;
2744 /* use default syntax conversion context */
2745 dsdb_syntax_ctx_init(&syntax_ctx, ldb, schema);
2747 return sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, in, mem_ctx, out);