s4:dsdb/schema: move messages for unknown attids to higher debug levels during resolving
[Samba.git] / source4 / dsdb / schema / schema_syntax.c
blob2e85a4ef02ca73d852b5fd6c5ca7d052224013e6
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_attid_from_oid(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_DS_ATT_NOT_DEF_IN_SCHEMA;
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_DS_ATT_NOT_DEF_IN_SCHEMA;
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_DS_ATT_NOT_DEF_IN_SCHEMA;
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_DS_ATT_NOT_DEF_IN_SCHEMA;
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_DS_ATT_NOT_DEF_IN_SCHEMA;
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_DS_ATT_NOT_DEF_IN_SCHEMA;
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_DS_ATT_NOT_DEF_IN_SCHEMA;
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_DS_ATT_NOT_DEF_IN_SCHEMA;
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_DS_ATT_NOT_DEF_IN_SCHEMA;
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_DS_ATT_NOT_DEF_IN_SCHEMA;
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->rangeUpper) {
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_DS_ATT_NOT_DEF_IN_SCHEMA;
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_DS_ATT_NOT_DEF_IN_SCHEMA;
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_DS_ATT_NOT_DEF_IN_SCHEMA;
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, vo;
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);
1012 vo = v;
1014 /* convert remote ATTID to local ATTID */
1015 if (!dsdb_syntax_attid_from_remote_attid(ctx, mem_ctx, v, &v)) {
1016 DEBUG(1,(__location__ ": Failed to map remote ATTID to local ATTID!\n"));
1017 return WERR_FOOBAR;
1020 c = dsdb_class_by_governsID_id(ctx->schema, v);
1021 if (!c) {
1022 int dbg_level = ctx->schema->resolving_in_progress ? 10 : 0;
1023 DEBUG(dbg_level,(__location__ ": %s unknown local governsID 0x%08X remote 0x%08X%s\n",
1024 attr->lDAPDisplayName, v, vo,
1025 ctx->schema->resolving_in_progress ? "resolving in progress" : ""));
1026 return WERR_DS_OBJ_CLASS_NOT_DEFINED;
1029 str = talloc_strdup(out->values, c->lDAPDisplayName);
1030 W_ERROR_HAVE_NO_MEMORY(str);
1032 /* the values need to be reversed */
1033 out->values[out->num_values - (i + 1)] = data_blob_string_const(str);
1036 return WERR_OK;
1039 static WERROR _dsdb_syntax_OID_attr_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
1040 const struct dsdb_attribute *attr,
1041 const struct drsuapi_DsReplicaAttribute *in,
1042 TALLOC_CTX *mem_ctx,
1043 struct ldb_message_element *out)
1045 unsigned int i;
1047 out->flags = 0;
1048 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
1049 W_ERROR_HAVE_NO_MEMORY(out->name);
1051 out->num_values = in->value_ctr.num_values;
1052 out->values = talloc_array(mem_ctx, struct ldb_val, out->num_values);
1053 W_ERROR_HAVE_NO_MEMORY(out->values);
1055 for (i=0; i < out->num_values; i++) {
1056 uint32_t v, vo;
1057 const struct dsdb_attribute *a;
1058 const char *str;
1060 if (in->value_ctr.values[i].blob == NULL) {
1061 DEBUG(0, ("Attribute has no value\n"));
1062 return WERR_FOOBAR;
1065 if (in->value_ctr.values[i].blob->length != 4) {
1066 DEBUG(0, ("Attribute has a value with 0 length\n"));
1067 return WERR_FOOBAR;
1070 v = IVAL(in->value_ctr.values[i].blob->data, 0);
1071 vo = v;
1073 /* convert remote ATTID to local ATTID */
1074 if (!dsdb_syntax_attid_from_remote_attid(ctx, mem_ctx, v, &v)) {
1075 DEBUG(1,(__location__ ": Failed to map remote ATTID to local ATTID!\n"));
1076 return WERR_FOOBAR;
1079 a = dsdb_attribute_by_attributeID_id(ctx->schema, v);
1080 if (!a) {
1081 int dbg_level = ctx->schema->resolving_in_progress ? 10 : 0;
1082 DEBUG(dbg_level,(__location__ ": %s unknown local attributeID_id 0x%08X remote 0x%08X%s\n",
1083 attr->lDAPDisplayName, v, vo,
1084 ctx->schema->resolving_in_progress ? "resolving in progress" : ""));
1085 return WERR_DS_ATT_NOT_DEF_IN_SCHEMA;
1088 str = talloc_strdup(out->values, a->lDAPDisplayName);
1089 W_ERROR_HAVE_NO_MEMORY(str);
1091 /* the values need to be reversed */
1092 out->values[out->num_values - (i + 1)] = data_blob_string_const(str);
1095 return WERR_OK;
1098 static WERROR _dsdb_syntax_OID_oid_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
1099 const struct dsdb_attribute *attr,
1100 const struct drsuapi_DsReplicaAttribute *in,
1101 TALLOC_CTX *mem_ctx,
1102 struct ldb_message_element *out)
1104 unsigned int i;
1105 const struct dsdb_schema_prefixmap *prefixmap;
1107 if (ctx->pfm_remote != NULL) {
1108 prefixmap = ctx->pfm_remote;
1109 } else {
1110 prefixmap = ctx->schema->prefixmap;
1112 SMB_ASSERT(prefixmap);
1114 out->flags = 0;
1115 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
1116 W_ERROR_HAVE_NO_MEMORY(out->name);
1118 out->num_values = in->value_ctr.num_values;
1119 out->values = talloc_array(mem_ctx, struct ldb_val, out->num_values);
1120 W_ERROR_HAVE_NO_MEMORY(out->values);
1122 for (i=0; i < out->num_values; i++) {
1123 uint32_t attid;
1124 WERROR status;
1125 const char *oid;
1127 if (in->value_ctr.values[i].blob == NULL) {
1128 return WERR_FOOBAR;
1131 if (in->value_ctr.values[i].blob->length != 4) {
1132 return WERR_FOOBAR;
1135 attid = IVAL(in->value_ctr.values[i].blob->data, 0);
1137 status = dsdb_schema_pfm_oid_from_attid(prefixmap, attid,
1138 out->values, &oid);
1139 if (!W_ERROR_IS_OK(status)) {
1140 DEBUG(0,(__location__ ": Error: Unknown ATTID 0x%08X\n",
1141 attid));
1142 return status;
1145 out->values[i] = data_blob_string_const(oid);
1148 return WERR_OK;
1151 static WERROR _dsdb_syntax_auto_OID_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
1152 const struct dsdb_attribute *attr,
1153 const struct ldb_message_element *in,
1154 TALLOC_CTX *mem_ctx,
1155 struct drsuapi_DsReplicaAttribute *out)
1157 unsigned int i;
1158 DATA_BLOB *blobs;
1160 out->attid= dsdb_attribute_get_attid(attr,
1161 ctx->is_schema_nc);
1162 out->value_ctr.num_values= in->num_values;
1163 out->value_ctr.values= talloc_array(mem_ctx,
1164 struct drsuapi_DsAttributeValue,
1165 in->num_values);
1166 W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1168 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1169 W_ERROR_HAVE_NO_MEMORY(blobs);
1171 for (i=0; i < in->num_values; i++) {
1172 const struct dsdb_class *obj_class;
1173 const struct dsdb_attribute *obj_attr;
1174 struct ldb_val *v;
1176 out->value_ctr.values[i].blob= &blobs[i];
1178 blobs[i] = data_blob_talloc(blobs, NULL, 4);
1179 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
1181 /* in DRS windows puts the classes in the opposite
1182 order to the order used in ldap */
1183 v = &in->values[(in->num_values-1)-i];
1185 if ((obj_class = dsdb_class_by_lDAPDisplayName_ldb_val(ctx->schema, v))) {
1186 SIVAL(blobs[i].data, 0, obj_class->governsID_id);
1187 } else if ((obj_attr = dsdb_attribute_by_lDAPDisplayName_ldb_val(ctx->schema, v))) {
1188 SIVAL(blobs[i].data, 0, obj_attr->attributeID_id);
1189 } else {
1190 uint32_t attid;
1191 WERROR werr;
1192 werr = dsdb_schema_pfm_attid_from_oid(ctx->schema->prefixmap,
1193 (const char *)v->data,
1194 &attid);
1195 W_ERROR_NOT_OK_RETURN(werr);
1196 SIVAL(blobs[i].data, 0, attid);
1202 return WERR_OK;
1205 static WERROR _dsdb_syntax_OID_obj_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)
1211 unsigned int i;
1212 DATA_BLOB *blobs;
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);
1222 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1223 W_ERROR_HAVE_NO_MEMORY(blobs);
1225 for (i=0; i < in->num_values; i++) {
1226 const struct dsdb_class *obj_class;
1228 out->value_ctr.values[i].blob= &blobs[i];
1230 blobs[i] = data_blob_talloc(blobs, NULL, 4);
1231 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
1233 /* in DRS windows puts the classes in the opposite
1234 order to the order used in ldap */
1235 obj_class = dsdb_class_by_lDAPDisplayName(ctx->schema,
1236 (const char *)in->values[(in->num_values-1)-i].data);
1237 if (!obj_class) {
1238 return WERR_FOOBAR;
1240 SIVAL(blobs[i].data, 0, obj_class->governsID_id);
1244 return WERR_OK;
1247 static WERROR _dsdb_syntax_OID_attr_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
1248 const struct dsdb_attribute *attr,
1249 const struct ldb_message_element *in,
1250 TALLOC_CTX *mem_ctx,
1251 struct drsuapi_DsReplicaAttribute *out)
1253 unsigned int i;
1254 DATA_BLOB *blobs;
1256 out->attid= dsdb_attribute_get_attid(attr,
1257 ctx->is_schema_nc);
1258 out->value_ctr.num_values= in->num_values;
1259 out->value_ctr.values= talloc_array(mem_ctx,
1260 struct drsuapi_DsAttributeValue,
1261 in->num_values);
1262 W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1264 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1265 W_ERROR_HAVE_NO_MEMORY(blobs);
1267 for (i=0; i < in->num_values; i++) {
1268 const struct dsdb_attribute *obj_attr;
1270 out->value_ctr.values[i].blob= &blobs[i];
1272 blobs[i] = data_blob_talloc(blobs, NULL, 4);
1273 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
1275 obj_attr = dsdb_attribute_by_lDAPDisplayName(ctx->schema, (const char *)in->values[i].data);
1276 if (!obj_attr) {
1277 DEBUG(0, ("Unable to find attribute %s in the schema\n", (const char *)in->values[i].data));
1278 return WERR_FOOBAR;
1280 SIVAL(blobs[i].data, 0, obj_attr->attributeID_id);
1284 return WERR_OK;
1287 static WERROR _dsdb_syntax_OID_oid_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
1288 const struct dsdb_attribute *attr,
1289 const struct ldb_message_element *in,
1290 TALLOC_CTX *mem_ctx,
1291 struct drsuapi_DsReplicaAttribute *out)
1293 unsigned int i;
1294 DATA_BLOB *blobs;
1296 out->attid= dsdb_attribute_get_attid(attr,
1297 ctx->is_schema_nc);
1298 out->value_ctr.num_values= in->num_values;
1299 out->value_ctr.values= talloc_array(mem_ctx,
1300 struct drsuapi_DsAttributeValue,
1301 in->num_values);
1302 W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1304 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1305 W_ERROR_HAVE_NO_MEMORY(blobs);
1307 for (i=0; i < in->num_values; i++) {
1308 uint32_t attid;
1309 WERROR status;
1311 out->value_ctr.values[i].blob= &blobs[i];
1313 blobs[i] = data_blob_talloc(blobs, NULL, 4);
1314 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
1316 status = dsdb_schema_pfm_attid_from_oid(ctx->schema->prefixmap,
1317 (const char *)in->values[i].data,
1318 &attid);
1319 W_ERROR_NOT_OK_RETURN(status);
1321 SIVAL(blobs[i].data, 0, attid);
1324 return WERR_OK;
1327 static WERROR dsdb_syntax_OID_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
1328 const struct dsdb_attribute *attr,
1329 const struct drsuapi_DsReplicaAttribute *in,
1330 TALLOC_CTX *mem_ctx,
1331 struct ldb_message_element *out)
1333 WERROR werr;
1335 switch (attr->attributeID_id) {
1336 case DRSUAPI_ATTID_objectClass:
1337 case DRSUAPI_ATTID_subClassOf:
1338 case DRSUAPI_ATTID_auxiliaryClass:
1339 case DRSUAPI_ATTID_systemAuxiliaryClass:
1340 case DRSUAPI_ATTID_systemPossSuperiors:
1341 case DRSUAPI_ATTID_possSuperiors:
1342 werr = _dsdb_syntax_OID_obj_drsuapi_to_ldb(ctx, attr, in, mem_ctx, out);
1343 break;
1344 case DRSUAPI_ATTID_systemMustContain:
1345 case DRSUAPI_ATTID_systemMayContain:
1346 case DRSUAPI_ATTID_mustContain:
1347 case DRSUAPI_ATTID_rDNAttId:
1348 case DRSUAPI_ATTID_transportAddressAttribute:
1349 case DRSUAPI_ATTID_mayContain:
1350 werr = _dsdb_syntax_OID_attr_drsuapi_to_ldb(ctx, attr, in, mem_ctx, out);
1351 break;
1352 case DRSUAPI_ATTID_governsID:
1353 case DRSUAPI_ATTID_attributeID:
1354 case DRSUAPI_ATTID_attributeSyntax:
1355 werr = _dsdb_syntax_OID_oid_drsuapi_to_ldb(ctx, attr, in, mem_ctx, out);
1356 break;
1357 default:
1358 DEBUG(0,(__location__ ": Unknown handling for attributeID_id for %s\n",
1359 attr->lDAPDisplayName));
1360 return _dsdb_syntax_auto_OID_drsuapi_to_ldb(ctx, attr, in, mem_ctx, out);
1363 /* When we are doing the vampire of a schema, we don't want
1364 * the inability to reference an OID to get in the way.
1365 * Otherwise, we won't get the new schema with which to
1366 * understand this */
1367 if (!W_ERROR_IS_OK(werr) && ctx->schema->relax_OID_conversions) {
1368 return _dsdb_syntax_OID_oid_drsuapi_to_ldb(ctx, attr, in, mem_ctx, out);
1370 return werr;
1373 static WERROR dsdb_syntax_OID_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
1374 const struct dsdb_attribute *attr,
1375 const struct ldb_message_element *in,
1376 TALLOC_CTX *mem_ctx,
1377 struct drsuapi_DsReplicaAttribute *out)
1379 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
1380 return WERR_DS_ATT_NOT_DEF_IN_SCHEMA;
1383 switch (attr->attributeID_id) {
1384 case DRSUAPI_ATTID_objectClass:
1385 case DRSUAPI_ATTID_subClassOf:
1386 case DRSUAPI_ATTID_auxiliaryClass:
1387 case DRSUAPI_ATTID_systemAuxiliaryClass:
1388 case DRSUAPI_ATTID_systemPossSuperiors:
1389 case DRSUAPI_ATTID_possSuperiors:
1390 return _dsdb_syntax_OID_obj_ldb_to_drsuapi(ctx, attr, in, mem_ctx, out);
1391 case DRSUAPI_ATTID_systemMustContain:
1392 case DRSUAPI_ATTID_systemMayContain:
1393 case DRSUAPI_ATTID_mustContain:
1394 case DRSUAPI_ATTID_rDNAttId:
1395 case DRSUAPI_ATTID_transportAddressAttribute:
1396 case DRSUAPI_ATTID_mayContain:
1397 return _dsdb_syntax_OID_attr_ldb_to_drsuapi(ctx, attr, in, mem_ctx, out);
1398 case DRSUAPI_ATTID_governsID:
1399 case DRSUAPI_ATTID_attributeID:
1400 case DRSUAPI_ATTID_attributeSyntax:
1401 return _dsdb_syntax_OID_oid_ldb_to_drsuapi(ctx, attr, in, mem_ctx, out);
1404 DEBUG(0,(__location__ ": Unknown handling for attributeID_id for %s\n",
1405 attr->lDAPDisplayName));
1407 return _dsdb_syntax_auto_OID_ldb_to_drsuapi(ctx, attr, in, mem_ctx, out);
1410 static WERROR _dsdb_syntax_OID_validate_numericoid(const struct dsdb_syntax_ctx *ctx,
1411 const struct dsdb_attribute *attr,
1412 const struct ldb_message_element *in)
1414 unsigned int i;
1415 TALLOC_CTX *tmp_ctx;
1417 tmp_ctx = talloc_new(ctx->ldb);
1418 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1420 for (i=0; i < in->num_values; i++) {
1421 DATA_BLOB blob;
1422 char *oid_out;
1423 const char *oid = (const char*)in->values[i].data;
1425 if (in->values[i].length == 0) {
1426 talloc_free(tmp_ctx);
1427 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1430 if (!ber_write_OID_String(tmp_ctx, &blob, oid)) {
1431 DEBUG(0,("ber_write_OID_String() failed for %s\n", oid));
1432 talloc_free(tmp_ctx);
1433 return WERR_INVALID_PARAMETER;
1436 if (!ber_read_OID_String(tmp_ctx, blob, &oid_out)) {
1437 DEBUG(0,("ber_read_OID_String() failed for %s\n",
1438 hex_encode_talloc(tmp_ctx, blob.data, blob.length)));
1439 talloc_free(tmp_ctx);
1440 return WERR_INVALID_PARAMETER;
1443 if (strcmp(oid, oid_out) != 0) {
1444 talloc_free(tmp_ctx);
1445 return WERR_INVALID_PARAMETER;
1449 talloc_free(tmp_ctx);
1450 return WERR_OK;
1453 static WERROR dsdb_syntax_OID_validate_ldb(const struct dsdb_syntax_ctx *ctx,
1454 const struct dsdb_attribute *attr,
1455 const struct ldb_message_element *in)
1457 WERROR status;
1458 struct drsuapi_DsReplicaAttribute drs_tmp;
1459 struct ldb_message_element ldb_tmp;
1460 TALLOC_CTX *tmp_ctx;
1462 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
1463 return WERR_DS_ATT_NOT_DEF_IN_SCHEMA;
1466 switch (attr->attributeID_id) {
1467 case DRSUAPI_ATTID_governsID:
1468 case DRSUAPI_ATTID_attributeID:
1469 case DRSUAPI_ATTID_attributeSyntax:
1470 return _dsdb_syntax_OID_validate_numericoid(ctx, attr, in);
1474 * TODO: optimize and verify this code
1477 tmp_ctx = talloc_new(ctx->ldb);
1478 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1480 status = dsdb_syntax_OID_ldb_to_drsuapi(ctx,
1481 attr,
1483 tmp_ctx,
1484 &drs_tmp);
1485 if (!W_ERROR_IS_OK(status)) {
1486 talloc_free(tmp_ctx);
1487 return status;
1490 status = dsdb_syntax_OID_drsuapi_to_ldb(ctx,
1491 attr,
1492 &drs_tmp,
1493 tmp_ctx,
1494 &ldb_tmp);
1495 if (!W_ERROR_IS_OK(status)) {
1496 talloc_free(tmp_ctx);
1497 return status;
1500 talloc_free(tmp_ctx);
1501 return WERR_OK;
1504 static WERROR dsdb_syntax_UNICODE_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
1505 const struct dsdb_attribute *attr,
1506 const struct drsuapi_DsReplicaAttribute *in,
1507 TALLOC_CTX *mem_ctx,
1508 struct ldb_message_element *out)
1510 unsigned int i;
1512 out->flags = 0;
1513 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
1514 W_ERROR_HAVE_NO_MEMORY(out->name);
1516 out->num_values = in->value_ctr.num_values;
1517 out->values = talloc_array(mem_ctx, struct ldb_val, out->num_values);
1518 W_ERROR_HAVE_NO_MEMORY(out->values);
1520 for (i=0; i < out->num_values; i++) {
1521 size_t converted_size = 0;
1522 char *str;
1524 if (in->value_ctr.values[i].blob == NULL) {
1525 return WERR_FOOBAR;
1528 if (in->value_ctr.values[i].blob->length == 0) {
1529 return WERR_FOOBAR;
1532 if (!convert_string_talloc(out->values,
1533 CH_UTF16, CH_UNIX,
1534 in->value_ctr.values[i].blob->data,
1535 in->value_ctr.values[i].blob->length,
1536 (void **)&str, &converted_size)) {
1537 return WERR_FOOBAR;
1540 out->values[i] = data_blob_const(str, converted_size);
1543 return WERR_OK;
1546 static WERROR dsdb_syntax_UNICODE_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
1547 const struct dsdb_attribute *attr,
1548 const struct ldb_message_element *in,
1549 TALLOC_CTX *mem_ctx,
1550 struct drsuapi_DsReplicaAttribute *out)
1552 unsigned int i;
1553 DATA_BLOB *blobs;
1555 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
1556 return WERR_DS_ATT_NOT_DEF_IN_SCHEMA;
1559 out->attid = dsdb_attribute_get_attid(attr,
1560 ctx->is_schema_nc);
1561 out->value_ctr.num_values = in->num_values;
1562 out->value_ctr.values = talloc_array(mem_ctx,
1563 struct drsuapi_DsAttributeValue,
1564 in->num_values);
1565 W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1567 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1568 W_ERROR_HAVE_NO_MEMORY(blobs);
1570 for (i=0; i < in->num_values; i++) {
1571 out->value_ctr.values[i].blob = &blobs[i];
1573 if (!convert_string_talloc(blobs,
1574 CH_UNIX, CH_UTF16,
1575 in->values[i].data, in->values[i].length,
1576 (void **)&blobs[i].data, &blobs[i].length)) {
1577 return WERR_FOOBAR;
1581 return WERR_OK;
1584 static WERROR dsdb_syntax_UNICODE_validate_one_val(const struct dsdb_syntax_ctx *ctx,
1585 const struct dsdb_attribute *attr,
1586 const struct ldb_val *val)
1588 void *dst = NULL;
1589 size_t size;
1590 bool ok;
1592 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
1593 return WERR_DS_ATT_NOT_DEF_IN_SCHEMA;
1596 ok = convert_string_talloc(ctx->ldb,
1597 CH_UNIX, CH_UTF16,
1598 val->data,
1599 val->length,
1600 (void **)&dst,
1601 &size);
1602 TALLOC_FREE(dst);
1603 if (!ok) {
1604 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1607 if (attr->rangeLower) {
1608 if ((size/2) < *attr->rangeLower) {
1609 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1613 if (attr->rangeUpper) {
1614 if ((size/2) > *attr->rangeUpper) {
1615 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1619 return WERR_OK;
1622 static WERROR dsdb_syntax_UNICODE_validate_ldb(const struct dsdb_syntax_ctx *ctx,
1623 const struct dsdb_attribute *attr,
1624 const struct ldb_message_element *in)
1626 WERROR status;
1627 unsigned int i;
1629 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
1630 return WERR_DS_ATT_NOT_DEF_IN_SCHEMA;
1633 for (i=0; i < in->num_values; i++) {
1634 if (in->values[i].length == 0) {
1635 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1638 status = dsdb_syntax_UNICODE_validate_one_val(ctx,
1639 attr,
1640 &in->values[i]);
1641 if (!W_ERROR_IS_OK(status)) {
1642 return status;
1646 return WERR_OK;
1649 static WERROR dsdb_syntax_one_DN_drsuapi_to_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
1650 const struct dsdb_syntax *syntax,
1651 const DATA_BLOB *in, DATA_BLOB *out)
1653 struct drsuapi_DsReplicaObjectIdentifier3 id3;
1654 enum ndr_err_code ndr_err;
1655 DATA_BLOB guid_blob;
1656 struct ldb_dn *dn;
1657 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1658 int ret;
1659 NTSTATUS status;
1661 if (!tmp_ctx) {
1662 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1665 if (in == NULL) {
1666 talloc_free(tmp_ctx);
1667 return WERR_FOOBAR;
1670 if (in->length == 0) {
1671 talloc_free(tmp_ctx);
1672 return WERR_FOOBAR;
1676 /* windows sometimes sends an extra two pad bytes here */
1677 ndr_err = ndr_pull_struct_blob(in,
1678 tmp_ctx, &id3,
1679 (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3);
1680 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1681 status = ndr_map_error2ntstatus(ndr_err);
1682 talloc_free(tmp_ctx);
1683 return ntstatus_to_werror(status);
1686 dn = ldb_dn_new(tmp_ctx, ldb, id3.dn);
1687 if (!dn) {
1688 talloc_free(tmp_ctx);
1689 /* If this fails, it must be out of memory, as it does not do much parsing */
1690 W_ERROR_HAVE_NO_MEMORY(dn);
1693 if (!GUID_all_zero(&id3.guid)) {
1694 status = GUID_to_ndr_blob(&id3.guid, tmp_ctx, &guid_blob);
1695 if (!NT_STATUS_IS_OK(status)) {
1696 talloc_free(tmp_ctx);
1697 return ntstatus_to_werror(status);
1700 ret = ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
1701 if (ret != LDB_SUCCESS) {
1702 talloc_free(tmp_ctx);
1703 return WERR_FOOBAR;
1705 talloc_free(guid_blob.data);
1708 if (id3.__ndr_size_sid) {
1709 DATA_BLOB sid_blob;
1710 ndr_err = ndr_push_struct_blob(&sid_blob, tmp_ctx, &id3.sid,
1711 (ndr_push_flags_fn_t)ndr_push_dom_sid);
1712 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1713 status = ndr_map_error2ntstatus(ndr_err);
1714 talloc_free(tmp_ctx);
1715 return ntstatus_to_werror(status);
1718 ret = ldb_dn_set_extended_component(dn, "SID", &sid_blob);
1719 if (ret != LDB_SUCCESS) {
1720 talloc_free(tmp_ctx);
1721 return WERR_FOOBAR;
1725 *out = data_blob_string_const(ldb_dn_get_extended_linearized(mem_ctx, dn, 1));
1726 talloc_free(tmp_ctx);
1727 return WERR_OK;
1730 static WERROR dsdb_syntax_DN_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
1731 const struct dsdb_attribute *attr,
1732 const struct drsuapi_DsReplicaAttribute *in,
1733 TALLOC_CTX *mem_ctx,
1734 struct ldb_message_element *out)
1736 unsigned int i;
1738 out->flags = 0;
1739 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
1740 W_ERROR_HAVE_NO_MEMORY(out->name);
1742 out->num_values = in->value_ctr.num_values;
1743 out->values = talloc_array(mem_ctx, struct ldb_val, out->num_values);
1744 W_ERROR_HAVE_NO_MEMORY(out->values);
1746 for (i=0; i < out->num_values; i++) {
1747 WERROR status = dsdb_syntax_one_DN_drsuapi_to_ldb(out->values, ctx->ldb, attr->syntax,
1748 in->value_ctr.values[i].blob,
1749 &out->values[i]);
1750 if (!W_ERROR_IS_OK(status)) {
1751 return status;
1756 return WERR_OK;
1759 static WERROR dsdb_syntax_DN_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
1760 const struct dsdb_attribute *attr,
1761 const struct ldb_message_element *in,
1762 TALLOC_CTX *mem_ctx,
1763 struct drsuapi_DsReplicaAttribute *out)
1765 unsigned int i;
1766 DATA_BLOB *blobs;
1768 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
1769 return WERR_DS_ATT_NOT_DEF_IN_SCHEMA;
1772 out->attid = dsdb_attribute_get_attid(attr,
1773 ctx->is_schema_nc);
1774 out->value_ctr.num_values = in->num_values;
1775 out->value_ctr.values = talloc_array(mem_ctx,
1776 struct drsuapi_DsAttributeValue,
1777 in->num_values);
1778 W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
1780 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
1781 W_ERROR_HAVE_NO_MEMORY(blobs);
1783 for (i=0; i < in->num_values; i++) {
1784 struct drsuapi_DsReplicaObjectIdentifier3 id3;
1785 enum ndr_err_code ndr_err;
1786 struct ldb_dn *dn;
1787 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1788 NTSTATUS status;
1790 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1792 out->value_ctr.values[i].blob = &blobs[i];
1794 dn = ldb_dn_from_ldb_val(tmp_ctx, ctx->ldb, &in->values[i]);
1796 W_ERROR_HAVE_NO_MEMORY(dn);
1798 ZERO_STRUCT(id3);
1800 status = dsdb_get_extended_dn_guid(dn, &id3.guid, "GUID");
1801 if (!NT_STATUS_IS_OK(status) &&
1802 !NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1803 talloc_free(tmp_ctx);
1804 return ntstatus_to_werror(status);
1807 status = dsdb_get_extended_dn_sid(dn, &id3.sid, "SID");
1808 if (!NT_STATUS_IS_OK(status) &&
1809 !NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1810 talloc_free(tmp_ctx);
1811 return ntstatus_to_werror(status);
1814 id3.dn = ldb_dn_get_linearized(dn);
1816 ndr_err = ndr_push_struct_blob(&blobs[i], blobs, &id3, (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3);
1817 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1818 status = ndr_map_error2ntstatus(ndr_err);
1819 talloc_free(tmp_ctx);
1820 return ntstatus_to_werror(status);
1822 talloc_free(tmp_ctx);
1825 return WERR_OK;
1828 static WERROR dsdb_syntax_DN_validate_one_val(const struct dsdb_syntax_ctx *ctx,
1829 const struct dsdb_attribute *attr,
1830 const struct ldb_val *val,
1831 TALLOC_CTX *mem_ctx,
1832 struct dsdb_dn **_dsdb_dn)
1834 static const char * const extended_list[] = { "GUID", "SID", NULL };
1835 enum ndr_err_code ndr_err;
1836 struct GUID guid;
1837 struct dom_sid sid;
1838 const DATA_BLOB *sid_blob;
1839 struct dsdb_dn *dsdb_dn;
1840 struct ldb_dn *dn;
1841 char *dn_str;
1842 struct ldb_dn *dn2;
1843 char *dn2_str;
1844 int num_components;
1845 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1846 NTSTATUS status;
1848 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1850 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
1851 return WERR_DS_ATT_NOT_DEF_IN_SCHEMA;
1854 dsdb_dn = dsdb_dn_parse(tmp_ctx, ctx->ldb, val,
1855 attr->syntax->ldap_oid);
1856 if (!dsdb_dn) {
1857 talloc_free(tmp_ctx);
1858 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1860 dn = dsdb_dn->dn;
1862 dn2 = ldb_dn_copy(tmp_ctx, dn);
1863 if (dn == NULL) {
1864 talloc_free(tmp_ctx);
1865 return WERR_NOMEM;
1868 num_components = ldb_dn_get_comp_num(dn);
1870 status = dsdb_get_extended_dn_guid(dn, &guid, "GUID");
1871 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1872 num_components++;
1873 } else if (!NT_STATUS_IS_OK(status)) {
1874 talloc_free(tmp_ctx);
1875 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1878 sid_blob = ldb_dn_get_extended_component(dn, "SID");
1879 if (sid_blob) {
1880 num_components++;
1881 ndr_err = ndr_pull_struct_blob_all(sid_blob,
1882 tmp_ctx,
1883 &sid,
1884 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
1885 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1886 talloc_free(tmp_ctx);
1887 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1891 /* Do not allow links to the RootDSE */
1892 if (num_components == 0) {
1893 talloc_free(tmp_ctx);
1894 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1898 * We need to check that only "GUID" and "SID" are
1899 * specified as extended components, we do that
1900 * by comparing the dn's after removing all components
1901 * from one dn and only the allowed subset from the other
1902 * one.
1904 ldb_dn_extended_filter(dn, extended_list);
1906 dn_str = ldb_dn_get_extended_linearized(tmp_ctx, dn, 0);
1907 if (dn_str == NULL) {
1908 talloc_free(tmp_ctx);
1909 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1911 dn2_str = ldb_dn_get_extended_linearized(tmp_ctx, dn2, 0);
1912 if (dn2_str == NULL) {
1913 talloc_free(tmp_ctx);
1914 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1917 if (strcmp(dn_str, dn2_str) != 0) {
1918 talloc_free(tmp_ctx);
1919 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1922 *_dsdb_dn = talloc_move(mem_ctx, &dsdb_dn);
1923 talloc_free(tmp_ctx);
1924 return WERR_OK;
1927 static WERROR dsdb_syntax_DN_validate_ldb(const struct dsdb_syntax_ctx *ctx,
1928 const struct dsdb_attribute *attr,
1929 const struct ldb_message_element *in)
1931 unsigned int i;
1933 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
1934 return WERR_DS_ATT_NOT_DEF_IN_SCHEMA;
1937 for (i=0; i < in->num_values; i++) {
1938 WERROR status;
1939 struct dsdb_dn *dsdb_dn;
1940 TALLOC_CTX *tmp_ctx = talloc_new(ctx->ldb);
1941 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1943 status = dsdb_syntax_DN_validate_one_val(ctx,
1944 attr,
1945 &in->values[i],
1946 tmp_ctx, &dsdb_dn);
1947 if (!W_ERROR_IS_OK(status)) {
1948 talloc_free(tmp_ctx);
1949 return status;
1952 if (dsdb_dn->dn_format != DSDB_NORMAL_DN) {
1953 talloc_free(tmp_ctx);
1954 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
1957 talloc_free(tmp_ctx);
1960 return WERR_OK;
1963 static WERROR dsdb_syntax_DN_BINARY_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
1964 const struct dsdb_attribute *attr,
1965 const struct drsuapi_DsReplicaAttribute *in,
1966 TALLOC_CTX *mem_ctx,
1967 struct ldb_message_element *out)
1969 unsigned int i;
1970 int ret;
1972 out->flags = 0;
1973 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
1974 W_ERROR_HAVE_NO_MEMORY(out->name);
1976 out->num_values = in->value_ctr.num_values;
1977 out->values = talloc_array(mem_ctx, struct ldb_val, out->num_values);
1978 W_ERROR_HAVE_NO_MEMORY(out->values);
1980 for (i=0; i < out->num_values; i++) {
1981 struct drsuapi_DsReplicaObjectIdentifier3Binary id3;
1982 enum ndr_err_code ndr_err;
1983 DATA_BLOB guid_blob;
1984 struct ldb_dn *dn;
1985 struct dsdb_dn *dsdb_dn;
1986 NTSTATUS status;
1987 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
1988 if (!tmp_ctx) {
1989 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
1992 if (in->value_ctr.values[i].blob == NULL) {
1993 talloc_free(tmp_ctx);
1994 return WERR_FOOBAR;
1997 if (in->value_ctr.values[i].blob->length == 0) {
1998 talloc_free(tmp_ctx);
1999 return WERR_FOOBAR;
2003 /* windows sometimes sends an extra two pad bytes here */
2004 ndr_err = ndr_pull_struct_blob(in->value_ctr.values[i].blob,
2005 tmp_ctx, &id3,
2006 (ndr_pull_flags_fn_t)ndr_pull_drsuapi_DsReplicaObjectIdentifier3Binary);
2007 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2008 status = ndr_map_error2ntstatus(ndr_err);
2009 talloc_free(tmp_ctx);
2010 return ntstatus_to_werror(status);
2013 dn = ldb_dn_new(tmp_ctx, ctx->ldb, id3.dn);
2014 if (!dn) {
2015 talloc_free(tmp_ctx);
2016 /* If this fails, it must be out of memory, as it does not do much parsing */
2017 W_ERROR_HAVE_NO_MEMORY(dn);
2020 if (!GUID_all_zero(&id3.guid)) {
2021 status = GUID_to_ndr_blob(&id3.guid, tmp_ctx, &guid_blob);
2022 if (!NT_STATUS_IS_OK(status)) {
2023 talloc_free(tmp_ctx);
2024 return ntstatus_to_werror(status);
2027 ret = ldb_dn_set_extended_component(dn, "GUID", &guid_blob);
2028 if (ret != LDB_SUCCESS) {
2029 talloc_free(tmp_ctx);
2030 return WERR_FOOBAR;
2032 talloc_free(guid_blob.data);
2035 if (id3.__ndr_size_sid) {
2036 DATA_BLOB sid_blob;
2037 ndr_err = ndr_push_struct_blob(&sid_blob, tmp_ctx, &id3.sid,
2038 (ndr_push_flags_fn_t)ndr_push_dom_sid);
2039 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2040 status = ndr_map_error2ntstatus(ndr_err);
2041 talloc_free(tmp_ctx);
2042 return ntstatus_to_werror(status);
2045 ret = ldb_dn_set_extended_component(dn, "SID", &sid_blob);
2046 if (ret != LDB_SUCCESS) {
2047 talloc_free(tmp_ctx);
2048 return WERR_FOOBAR;
2052 /* set binary stuff */
2053 dsdb_dn = dsdb_dn_construct(tmp_ctx, dn, id3.binary, attr->syntax->ldap_oid);
2054 if (!dsdb_dn) {
2055 /* If this fails, it must be out of memory, we know the ldap_oid is valid */
2056 talloc_free(tmp_ctx);
2057 W_ERROR_HAVE_NO_MEMORY(dsdb_dn);
2059 out->values[i] = data_blob_string_const(dsdb_dn_get_extended_linearized(out->values, dsdb_dn, 1));
2060 talloc_free(tmp_ctx);
2063 return WERR_OK;
2066 static WERROR dsdb_syntax_DN_BINARY_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
2067 const struct dsdb_attribute *attr,
2068 const struct ldb_message_element *in,
2069 TALLOC_CTX *mem_ctx,
2070 struct drsuapi_DsReplicaAttribute *out)
2072 unsigned int i;
2073 DATA_BLOB *blobs;
2075 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
2076 return WERR_DS_ATT_NOT_DEF_IN_SCHEMA;
2079 out->attid = dsdb_attribute_get_attid(attr,
2080 ctx->is_schema_nc);
2081 out->value_ctr.num_values = in->num_values;
2082 out->value_ctr.values = talloc_array(mem_ctx,
2083 struct drsuapi_DsAttributeValue,
2084 in->num_values);
2085 W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
2087 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
2088 W_ERROR_HAVE_NO_MEMORY(blobs);
2090 for (i=0; i < in->num_values; i++) {
2091 struct drsuapi_DsReplicaObjectIdentifier3Binary id3;
2092 enum ndr_err_code ndr_err;
2093 const DATA_BLOB *sid_blob;
2094 struct dsdb_dn *dsdb_dn;
2095 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
2096 NTSTATUS status;
2098 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
2100 out->value_ctr.values[i].blob = &blobs[i];
2102 dsdb_dn = dsdb_dn_parse(tmp_ctx, ctx->ldb, &in->values[i], attr->syntax->ldap_oid);
2104 if (!dsdb_dn) {
2105 talloc_free(tmp_ctx);
2106 return ntstatus_to_werror(NT_STATUS_INVALID_PARAMETER);
2109 ZERO_STRUCT(id3);
2111 status = dsdb_get_extended_dn_guid(dsdb_dn->dn, &id3.guid, "GUID");
2112 if (!NT_STATUS_IS_OK(status) &&
2113 !NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2114 talloc_free(tmp_ctx);
2115 return ntstatus_to_werror(status);
2118 sid_blob = ldb_dn_get_extended_component(dsdb_dn->dn, "SID");
2119 if (sid_blob) {
2121 ndr_err = ndr_pull_struct_blob_all(sid_blob,
2122 tmp_ctx, &id3.sid,
2123 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
2124 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2125 status = ndr_map_error2ntstatus(ndr_err);
2126 talloc_free(tmp_ctx);
2127 return ntstatus_to_werror(status);
2131 id3.dn = ldb_dn_get_linearized(dsdb_dn->dn);
2133 /* get binary stuff */
2134 id3.binary = dsdb_dn->extra_part;
2136 ndr_err = ndr_push_struct_blob(&blobs[i], blobs, &id3, (ndr_push_flags_fn_t)ndr_push_drsuapi_DsReplicaObjectIdentifier3Binary);
2137 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2138 status = ndr_map_error2ntstatus(ndr_err);
2139 talloc_free(tmp_ctx);
2140 return ntstatus_to_werror(status);
2142 talloc_free(tmp_ctx);
2145 return WERR_OK;
2148 static WERROR dsdb_syntax_DN_BINARY_validate_ldb(const struct dsdb_syntax_ctx *ctx,
2149 const struct dsdb_attribute *attr,
2150 const struct ldb_message_element *in)
2152 unsigned int i;
2154 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
2155 return WERR_DS_ATT_NOT_DEF_IN_SCHEMA;
2158 for (i=0; i < in->num_values; i++) {
2159 WERROR status;
2160 struct dsdb_dn *dsdb_dn;
2161 TALLOC_CTX *tmp_ctx = talloc_new(ctx->ldb);
2162 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
2164 status = dsdb_syntax_DN_validate_one_val(ctx,
2165 attr,
2166 &in->values[i],
2167 tmp_ctx, &dsdb_dn);
2168 if (!W_ERROR_IS_OK(status)) {
2169 talloc_free(tmp_ctx);
2170 return status;
2173 if (dsdb_dn->dn_format != DSDB_BINARY_DN) {
2174 talloc_free(tmp_ctx);
2175 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
2178 status = dsdb_syntax_DATA_BLOB_validate_one_val(ctx,
2179 attr,
2180 &dsdb_dn->extra_part);
2181 if (!W_ERROR_IS_OK(status)) {
2182 talloc_free(tmp_ctx);
2183 return status;
2186 talloc_free(tmp_ctx);
2189 return WERR_OK;
2192 static WERROR dsdb_syntax_DN_STRING_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
2193 const struct dsdb_attribute *attr,
2194 const struct drsuapi_DsReplicaAttribute *in,
2195 TALLOC_CTX *mem_ctx,
2196 struct ldb_message_element *out)
2198 return dsdb_syntax_DN_BINARY_drsuapi_to_ldb(ctx,
2199 attr,
2201 mem_ctx,
2202 out);
2205 static WERROR dsdb_syntax_DN_STRING_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
2206 const struct dsdb_attribute *attr,
2207 const struct ldb_message_element *in,
2208 TALLOC_CTX *mem_ctx,
2209 struct drsuapi_DsReplicaAttribute *out)
2211 return dsdb_syntax_DN_BINARY_ldb_to_drsuapi(ctx,
2212 attr,
2214 mem_ctx,
2215 out);
2218 static WERROR dsdb_syntax_DN_STRING_validate_ldb(const struct dsdb_syntax_ctx *ctx,
2219 const struct dsdb_attribute *attr,
2220 const struct ldb_message_element *in)
2222 unsigned int i;
2224 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
2225 return WERR_DS_ATT_NOT_DEF_IN_SCHEMA;
2228 for (i=0; i < in->num_values; i++) {
2229 WERROR status;
2230 struct dsdb_dn *dsdb_dn;
2231 TALLOC_CTX *tmp_ctx = talloc_new(ctx->ldb);
2232 W_ERROR_HAVE_NO_MEMORY(tmp_ctx);
2234 status = dsdb_syntax_DN_validate_one_val(ctx,
2235 attr,
2236 &in->values[i],
2237 tmp_ctx, &dsdb_dn);
2238 if (!W_ERROR_IS_OK(status)) {
2239 talloc_free(tmp_ctx);
2240 return status;
2243 if (dsdb_dn->dn_format != DSDB_STRING_DN) {
2244 talloc_free(tmp_ctx);
2245 return WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
2248 status = dsdb_syntax_UNICODE_validate_one_val(ctx,
2249 attr,
2250 &dsdb_dn->extra_part);
2251 if (!W_ERROR_IS_OK(status)) {
2252 talloc_free(tmp_ctx);
2253 return status;
2256 talloc_free(tmp_ctx);
2259 return WERR_OK;
2262 static WERROR dsdb_syntax_PRESENTATION_ADDRESS_drsuapi_to_ldb(const struct dsdb_syntax_ctx *ctx,
2263 const struct dsdb_attribute *attr,
2264 const struct drsuapi_DsReplicaAttribute *in,
2265 TALLOC_CTX *mem_ctx,
2266 struct ldb_message_element *out)
2268 unsigned int i;
2270 out->flags = 0;
2271 out->name = talloc_strdup(mem_ctx, attr->lDAPDisplayName);
2272 W_ERROR_HAVE_NO_MEMORY(out->name);
2274 out->num_values = in->value_ctr.num_values;
2275 out->values = talloc_array(mem_ctx, struct ldb_val, out->num_values);
2276 W_ERROR_HAVE_NO_MEMORY(out->values);
2278 for (i=0; i < out->num_values; i++) {
2279 size_t len;
2280 size_t converted_size = 0;
2281 char *str;
2283 if (in->value_ctr.values[i].blob == NULL) {
2284 return WERR_FOOBAR;
2287 if (in->value_ctr.values[i].blob->length < 4) {
2288 return WERR_FOOBAR;
2291 len = IVAL(in->value_ctr.values[i].blob->data, 0);
2293 if (len != in->value_ctr.values[i].blob->length) {
2294 return WERR_FOOBAR;
2297 if (!convert_string_talloc(out->values, CH_UTF16, CH_UNIX,
2298 in->value_ctr.values[i].blob->data+4,
2299 in->value_ctr.values[i].blob->length-4,
2300 (void **)&str, &converted_size)) {
2301 return WERR_FOOBAR;
2304 out->values[i] = data_blob_string_const(str);
2307 return WERR_OK;
2310 static WERROR dsdb_syntax_PRESENTATION_ADDRESS_ldb_to_drsuapi(const struct dsdb_syntax_ctx *ctx,
2311 const struct dsdb_attribute *attr,
2312 const struct ldb_message_element *in,
2313 TALLOC_CTX *mem_ctx,
2314 struct drsuapi_DsReplicaAttribute *out)
2316 unsigned int i;
2317 DATA_BLOB *blobs;
2319 if (attr->attributeID_id == DRSUAPI_ATTID_INVALID) {
2320 return WERR_DS_ATT_NOT_DEF_IN_SCHEMA;
2323 out->attid = dsdb_attribute_get_attid(attr,
2324 ctx->is_schema_nc);
2325 out->value_ctr.num_values = in->num_values;
2326 out->value_ctr.values = talloc_array(mem_ctx,
2327 struct drsuapi_DsAttributeValue,
2328 in->num_values);
2329 W_ERROR_HAVE_NO_MEMORY(out->value_ctr.values);
2331 blobs = talloc_array(mem_ctx, DATA_BLOB, in->num_values);
2332 W_ERROR_HAVE_NO_MEMORY(blobs);
2334 for (i=0; i < in->num_values; i++) {
2335 uint8_t *data;
2336 size_t ret;
2338 out->value_ctr.values[i].blob = &blobs[i];
2340 if (!convert_string_talloc(blobs, CH_UNIX, CH_UTF16,
2341 in->values[i].data,
2342 in->values[i].length,
2343 (void **)&data, &ret)) {
2344 return WERR_FOOBAR;
2347 blobs[i] = data_blob_talloc(blobs, NULL, 4 + ret);
2348 W_ERROR_HAVE_NO_MEMORY(blobs[i].data);
2350 SIVAL(blobs[i].data, 0, 4 + ret);
2352 if (ret > 0) {
2353 memcpy(blobs[i].data + 4, data, ret);
2354 talloc_free(data);
2358 return WERR_OK;
2361 static WERROR dsdb_syntax_PRESENTATION_ADDRESS_validate_ldb(const struct dsdb_syntax_ctx *ctx,
2362 const struct dsdb_attribute *attr,
2363 const struct ldb_message_element *in)
2365 return dsdb_syntax_UNICODE_validate_ldb(ctx,
2366 attr,
2367 in);
2370 #define OMOBJECTCLASS(val) { .length = sizeof(val) - 1, .data = discard_const_p(uint8_t, val) }
2372 static const struct dsdb_syntax dsdb_syntaxes[] = {
2374 .name = "Boolean",
2375 .ldap_oid = LDB_SYNTAX_BOOLEAN,
2376 .oMSyntax = 1,
2377 .attributeSyntax_oid = "2.5.5.8",
2378 .drsuapi_to_ldb = dsdb_syntax_BOOL_drsuapi_to_ldb,
2379 .ldb_to_drsuapi = dsdb_syntax_BOOL_ldb_to_drsuapi,
2380 .validate_ldb = dsdb_syntax_BOOL_validate_ldb,
2381 .equality = "booleanMatch",
2382 .comment = "Boolean",
2383 .auto_normalise = true
2385 .name = "Integer",
2386 .ldap_oid = LDB_SYNTAX_INTEGER,
2387 .oMSyntax = 2,
2388 .attributeSyntax_oid = "2.5.5.9",
2389 .drsuapi_to_ldb = dsdb_syntax_INT32_drsuapi_to_ldb,
2390 .ldb_to_drsuapi = dsdb_syntax_INT32_ldb_to_drsuapi,
2391 .validate_ldb = dsdb_syntax_INT32_validate_ldb,
2392 .equality = "integerMatch",
2393 .comment = "Integer",
2394 .ldb_syntax = LDB_SYNTAX_SAMBA_INT32,
2395 .auto_normalise = true
2397 .name = "String(Octet)",
2398 .ldap_oid = LDB_SYNTAX_OCTET_STRING,
2399 .oMSyntax = 4,
2400 .attributeSyntax_oid = "2.5.5.10",
2401 .drsuapi_to_ldb = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2402 .ldb_to_drsuapi = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2403 .validate_ldb = dsdb_syntax_DATA_BLOB_validate_ldb,
2404 .equality = "octetStringMatch",
2405 .comment = "Octet String",
2406 .userParameters = true,
2407 .ldb_syntax = LDB_SYNTAX_SAMBA_OCTET_STRING
2409 .name = "String(Sid)",
2410 .ldap_oid = LDB_SYNTAX_OCTET_STRING,
2411 .oMSyntax = 4,
2412 .attributeSyntax_oid = "2.5.5.17",
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 = "octetStringMatch",
2417 .comment = "Octet String - Security Identifier (SID)",
2418 .ldb_syntax = LDB_SYNTAX_SAMBA_SID
2420 .name = "String(Object-Identifier)",
2421 .ldap_oid = "1.3.6.1.4.1.1466.115.121.1.38",
2422 .oMSyntax = 6,
2423 .attributeSyntax_oid = "2.5.5.2",
2424 .drsuapi_to_ldb = dsdb_syntax_OID_drsuapi_to_ldb,
2425 .ldb_to_drsuapi = dsdb_syntax_OID_ldb_to_drsuapi,
2426 .validate_ldb = dsdb_syntax_OID_validate_ldb,
2427 .equality = "caseIgnoreMatch", /* Would use "objectIdentifierMatch" but most are ldap attribute/class names */
2428 .comment = "OID String",
2429 .ldb_syntax = LDB_SYNTAX_DIRECTORY_STRING
2431 .name = "Enumeration",
2432 .ldap_oid = LDB_SYNTAX_INTEGER,
2433 .oMSyntax = 10,
2434 .attributeSyntax_oid = "2.5.5.9",
2435 .drsuapi_to_ldb = dsdb_syntax_INT32_drsuapi_to_ldb,
2436 .ldb_to_drsuapi = dsdb_syntax_INT32_ldb_to_drsuapi,
2437 .validate_ldb = dsdb_syntax_INT32_validate_ldb,
2438 .ldb_syntax = LDB_SYNTAX_SAMBA_INT32,
2439 .auto_normalise = true
2441 /* not used in w2k3 forest */
2442 .name = "String(Numeric)",
2443 .ldap_oid = "1.3.6.1.4.1.1466.115.121.1.36",
2444 .oMSyntax = 18,
2445 .attributeSyntax_oid = "2.5.5.6",
2446 .drsuapi_to_ldb = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2447 .ldb_to_drsuapi = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2448 .validate_ldb = dsdb_syntax_DATA_BLOB_validate_ldb,
2449 .equality = "numericStringMatch",
2450 .substring = "numericStringSubstringsMatch",
2451 .comment = "Numeric String",
2452 .ldb_syntax = LDB_SYNTAX_DIRECTORY_STRING,
2454 .name = "String(Printable)",
2455 .ldap_oid = "1.3.6.1.4.1.1466.115.121.1.44",
2456 .oMSyntax = 19,
2457 .attributeSyntax_oid = "2.5.5.5",
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 .ldb_syntax = LDB_SYNTAX_SAMBA_OCTET_STRING,
2463 .name = "String(Teletex)",
2464 .ldap_oid = "1.2.840.113556.1.4.905",
2465 .oMSyntax = 20,
2466 .attributeSyntax_oid = "2.5.5.4",
2467 .drsuapi_to_ldb = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2468 .ldb_to_drsuapi = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2469 .validate_ldb = dsdb_syntax_DATA_BLOB_validate_ldb,
2470 .equality = "caseIgnoreMatch",
2471 .substring = "caseIgnoreSubstringsMatch",
2472 .comment = "Case Insensitive String",
2473 .ldb_syntax = LDB_SYNTAX_DIRECTORY_STRING,
2475 .name = "String(IA5)",
2476 .ldap_oid = "1.3.6.1.4.1.1466.115.121.1.26",
2477 .oMSyntax = 22,
2478 .attributeSyntax_oid = "2.5.5.5",
2479 .drsuapi_to_ldb = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2480 .ldb_to_drsuapi = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2481 .validate_ldb = dsdb_syntax_DATA_BLOB_validate_ldb,
2482 .equality = "caseExactIA5Match",
2483 .comment = "Printable String",
2484 .ldb_syntax = LDB_SYNTAX_SAMBA_OCTET_STRING,
2486 .name = "String(UTC-Time)",
2487 .ldap_oid = "1.3.6.1.4.1.1466.115.121.1.53",
2488 .oMSyntax = 23,
2489 .attributeSyntax_oid = "2.5.5.11",
2490 .drsuapi_to_ldb = dsdb_syntax_NTTIME_UTC_drsuapi_to_ldb,
2491 .ldb_to_drsuapi = dsdb_syntax_NTTIME_UTC_ldb_to_drsuapi,
2492 .validate_ldb = dsdb_syntax_NTTIME_UTC_validate_ldb,
2493 .equality = "generalizedTimeMatch",
2494 .comment = "UTC Time",
2495 .auto_normalise = true
2497 .name = "String(Generalized-Time)",
2498 .ldap_oid = "1.3.6.1.4.1.1466.115.121.1.24",
2499 .oMSyntax = 24,
2500 .attributeSyntax_oid = "2.5.5.11",
2501 .drsuapi_to_ldb = dsdb_syntax_NTTIME_drsuapi_to_ldb,
2502 .ldb_to_drsuapi = dsdb_syntax_NTTIME_ldb_to_drsuapi,
2503 .validate_ldb = dsdb_syntax_NTTIME_validate_ldb,
2504 .equality = "generalizedTimeMatch",
2505 .comment = "Generalized Time",
2506 .auto_normalise = true
2508 /* not used in w2k3 schema */
2509 .name = "String(Case Sensitive)",
2510 .ldap_oid = "1.2.840.113556.1.4.1362",
2511 .oMSyntax = 27,
2512 .attributeSyntax_oid = "2.5.5.3",
2513 .drsuapi_to_ldb = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2514 .ldb_to_drsuapi = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2515 .validate_ldb = dsdb_syntax_DATA_BLOB_validate_ldb,
2516 .equality = "caseExactMatch",
2517 .substring = "caseExactSubstringsMatch",
2518 /* TODO (kim): according to LDAP rfc we should be using same comparison
2519 * as Directory String (LDB_SYNTAX_DIRECTORY_STRING), but case sensitive.
2520 * But according to ms docs binary compare should do the job:
2521 * http://msdn.microsoft.com/en-us/library/cc223200(v=PROT.10).aspx */
2522 .ldb_syntax = LDB_SYNTAX_SAMBA_OCTET_STRING,
2524 .name = "String(Unicode)",
2525 .ldap_oid = LDB_SYNTAX_DIRECTORY_STRING,
2526 .oMSyntax = 64,
2527 .attributeSyntax_oid = "2.5.5.12",
2528 .drsuapi_to_ldb = dsdb_syntax_UNICODE_drsuapi_to_ldb,
2529 .ldb_to_drsuapi = dsdb_syntax_UNICODE_ldb_to_drsuapi,
2530 .validate_ldb = dsdb_syntax_UNICODE_validate_ldb,
2531 .equality = "caseIgnoreMatch",
2532 .substring = "caseIgnoreSubstringsMatch",
2533 .comment = "Directory String",
2535 .name = "Interval/LargeInteger",
2536 .ldap_oid = "1.2.840.113556.1.4.906",
2537 .oMSyntax = 65,
2538 .attributeSyntax_oid = "2.5.5.16",
2539 .drsuapi_to_ldb = dsdb_syntax_INT64_drsuapi_to_ldb,
2540 .ldb_to_drsuapi = dsdb_syntax_INT64_ldb_to_drsuapi,
2541 .validate_ldb = dsdb_syntax_INT64_validate_ldb,
2542 .equality = "integerMatch",
2543 .comment = "Large Integer",
2544 .ldb_syntax = LDB_SYNTAX_INTEGER,
2545 .auto_normalise = true
2547 .name = "String(NT-Sec-Desc)",
2548 .ldap_oid = LDB_SYNTAX_SAMBA_SECURITY_DESCRIPTOR,
2549 .oMSyntax = 66,
2550 .attributeSyntax_oid = "2.5.5.15",
2551 .drsuapi_to_ldb = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2552 .ldb_to_drsuapi = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2553 .validate_ldb = dsdb_syntax_DATA_BLOB_validate_ldb,
2555 .name = "Object(DS-DN)",
2556 .ldap_oid = LDB_SYNTAX_DN,
2557 .oMSyntax = 127,
2558 .oMObjectClass = OMOBJECTCLASS("\x2b\x0c\x02\x87\x73\x1c\x00\x85\x4a"),
2559 .attributeSyntax_oid = "2.5.5.1",
2560 .drsuapi_to_ldb = dsdb_syntax_DN_drsuapi_to_ldb,
2561 .ldb_to_drsuapi = dsdb_syntax_DN_ldb_to_drsuapi,
2562 .validate_ldb = dsdb_syntax_DN_validate_ldb,
2563 .equality = "distinguishedNameMatch",
2564 .comment = "Object(DS-DN) == a DN",
2566 .name = "Object(DN-Binary)",
2567 .ldap_oid = DSDB_SYNTAX_BINARY_DN,
2568 .oMSyntax = 127,
2569 .oMObjectClass = OMOBJECTCLASS("\x2a\x86\x48\x86\xf7\x14\x01\x01\x01\x0b"),
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 = "octetStringMatch",
2575 .comment = "OctetString: Binary+DN",
2577 /* not used in w2k3 schema, but used in Exchange schema*/
2578 .name = "Object(OR-Name)",
2579 .ldap_oid = DSDB_SYNTAX_OR_NAME,
2580 .oMSyntax = 127,
2581 .oMObjectClass = OMOBJECTCLASS("\x56\x06\x01\x02\x05\x0b\x1D"),
2582 .attributeSyntax_oid = "2.5.5.7",
2583 .drsuapi_to_ldb = dsdb_syntax_DN_BINARY_drsuapi_to_ldb,
2584 .ldb_to_drsuapi = dsdb_syntax_DN_BINARY_ldb_to_drsuapi,
2585 .validate_ldb = dsdb_syntax_DN_BINARY_validate_ldb,
2586 .equality = "caseIgnoreMatch",
2587 .ldb_syntax = LDB_SYNTAX_DN,
2590 * TODO: verify if DATA_BLOB is correct here...!
2592 * repsFrom and repsTo are the only attributes using
2593 * this attribute syntax, but they're not replicated...
2595 .name = "Object(Replica-Link)",
2596 .ldap_oid = "1.3.6.1.4.1.1466.115.121.1.40",
2597 .oMSyntax = 127,
2598 .oMObjectClass = OMOBJECTCLASS("\x2a\x86\x48\x86\xf7\x14\x01\x01\x01\x06"),
2599 .attributeSyntax_oid = "2.5.5.10",
2600 .drsuapi_to_ldb = dsdb_syntax_DATA_BLOB_drsuapi_to_ldb,
2601 .ldb_to_drsuapi = dsdb_syntax_DATA_BLOB_ldb_to_drsuapi,
2602 .validate_ldb = dsdb_syntax_DATA_BLOB_validate_ldb,
2604 .name = "Object(Presentation-Address)",
2605 .ldap_oid = "1.3.6.1.4.1.1466.115.121.1.43",
2606 .oMSyntax = 127,
2607 .oMObjectClass = OMOBJECTCLASS("\x2b\x0c\x02\x87\x73\x1c\x00\x85\x5c"),
2608 .attributeSyntax_oid = "2.5.5.13",
2609 .drsuapi_to_ldb = dsdb_syntax_PRESENTATION_ADDRESS_drsuapi_to_ldb,
2610 .ldb_to_drsuapi = dsdb_syntax_PRESENTATION_ADDRESS_ldb_to_drsuapi,
2611 .validate_ldb = dsdb_syntax_PRESENTATION_ADDRESS_validate_ldb,
2612 .comment = "Presentation Address",
2613 .ldb_syntax = LDB_SYNTAX_DIRECTORY_STRING,
2615 /* not used in w2k3 schema */
2616 .name = "Object(Access-Point)",
2617 .ldap_oid = DSDB_SYNTAX_ACCESS_POINT,
2618 .oMSyntax = 127,
2619 .oMObjectClass = OMOBJECTCLASS("\x2b\x0c\x02\x87\x73\x1c\x00\x85\x3e"),
2620 .attributeSyntax_oid = "2.5.5.14",
2621 .drsuapi_to_ldb = dsdb_syntax_FOOBAR_drsuapi_to_ldb,
2622 .ldb_to_drsuapi = dsdb_syntax_FOOBAR_ldb_to_drsuapi,
2623 .validate_ldb = dsdb_syntax_FOOBAR_validate_ldb,
2624 .ldb_syntax = LDB_SYNTAX_DIRECTORY_STRING,
2626 /* not used in w2k3 schema */
2627 .name = "Object(DN-String)",
2628 .ldap_oid = DSDB_SYNTAX_STRING_DN,
2629 .oMSyntax = 127,
2630 .oMObjectClass = OMOBJECTCLASS("\x2a\x86\x48\x86\xf7\x14\x01\x01\x01\x0c"),
2631 .attributeSyntax_oid = "2.5.5.14",
2632 .drsuapi_to_ldb = dsdb_syntax_DN_STRING_drsuapi_to_ldb,
2633 .ldb_to_drsuapi = dsdb_syntax_DN_STRING_ldb_to_drsuapi,
2634 .validate_ldb = dsdb_syntax_DN_STRING_validate_ldb,
2635 .equality = "octetStringMatch",
2636 .comment = "OctetString: String+DN",
2640 const struct dsdb_syntax *find_syntax_map_by_ad_oid(const char *ad_oid)
2642 unsigned int i;
2643 for (i=0; dsdb_syntaxes[i].ldap_oid; i++) {
2644 if (strcasecmp(ad_oid, dsdb_syntaxes[i].attributeSyntax_oid) == 0) {
2645 return &dsdb_syntaxes[i];
2648 return NULL;
2651 const struct dsdb_syntax *find_syntax_map_by_ad_syntax(int oMSyntax)
2653 unsigned int i;
2654 for (i=0; dsdb_syntaxes[i].ldap_oid; i++) {
2655 if (oMSyntax == dsdb_syntaxes[i].oMSyntax) {
2656 return &dsdb_syntaxes[i];
2659 return NULL;
2662 const struct dsdb_syntax *find_syntax_map_by_standard_oid(const char *standard_oid)
2664 unsigned int i;
2665 for (i=0; dsdb_syntaxes[i].ldap_oid; i++) {
2666 if (strcasecmp(standard_oid, dsdb_syntaxes[i].ldap_oid) == 0) {
2667 return &dsdb_syntaxes[i];
2670 return NULL;
2673 const struct dsdb_syntax *dsdb_syntax_for_attribute(const struct dsdb_attribute *attr)
2675 unsigned int i;
2677 for (i=0; i < ARRAY_SIZE(dsdb_syntaxes); i++) {
2679 * We must pretend that userParamters was declared
2680 * binary string, so we can store the 'UTF16' (not
2681 * really string) structure as given over SAMR to samba
2683 if (dsdb_syntaxes[i].userParameters &&
2684 (strcasecmp(attr->lDAPDisplayName, "userParameters") == 0))
2686 return &dsdb_syntaxes[i];
2688 if (attr->oMSyntax != dsdb_syntaxes[i].oMSyntax) continue;
2690 if (attr->oMObjectClass.length != dsdb_syntaxes[i].oMObjectClass.length) continue;
2692 if (attr->oMObjectClass.length) {
2693 int ret;
2694 ret = memcmp(attr->oMObjectClass.data,
2695 dsdb_syntaxes[i].oMObjectClass.data,
2696 attr->oMObjectClass.length);
2697 if (ret != 0) continue;
2700 if (strcmp(attr->attributeSyntax_oid, dsdb_syntaxes[i].attributeSyntax_oid) != 0) continue;
2702 return &dsdb_syntaxes[i];
2705 return NULL;
2708 WERROR dsdb_attribute_drsuapi_remote_to_local(const struct dsdb_syntax_ctx *ctx,
2709 enum drsuapi_DsAttributeId remote_attid_as_enum,
2710 enum drsuapi_DsAttributeId *local_attid_as_enum,
2711 const struct dsdb_attribute **_sa)
2713 TALLOC_CTX *frame = talloc_stackframe();
2714 const struct dsdb_attribute *sa;
2715 uint32_t attid_local;
2716 bool ok;
2718 if (ctx->pfm_remote == NULL) {
2719 smb_panic(__location__);
2722 switch (dsdb_pfm_get_attid_type(remote_attid_as_enum)) {
2723 case DSDB_ATTID_TYPE_PFM:
2724 /* map remote ATTID to local ATTID */
2725 ok = dsdb_syntax_attid_from_remote_attid(ctx, frame,
2726 remote_attid_as_enum,
2727 &attid_local);
2728 if (!ok) {
2729 DEBUG(0,(__location__ ": Can't find local ATTID for 0x%08X\n",
2730 remote_attid_as_enum));
2731 TALLOC_FREE(frame);
2732 return WERR_DS_ATT_NOT_DEF_IN_SCHEMA;
2734 break;
2735 case DSDB_ATTID_TYPE_INTID:
2736 /* use IntId value directly */
2737 attid_local = remote_attid_as_enum;
2738 break;
2739 default:
2740 /* we should never get here */
2741 DEBUG(0,(__location__ ": Invalid ATTID type passed for conversion - 0x%08X\n",
2742 remote_attid_as_enum));
2743 TALLOC_FREE(frame);
2744 return WERR_INVALID_PARAMETER;
2747 sa = dsdb_attribute_by_attributeID_id(ctx->schema, attid_local);
2748 if (!sa) {
2749 int dbg_level = ctx->schema->resolving_in_progress ? 10 : 0;
2750 DEBUG(dbg_level,(__location__ ": Unknown local attributeID_id 0x%08X remote 0x%08X%s\n",
2751 attid_local, remote_attid_as_enum,
2752 ctx->schema->resolving_in_progress ? "resolving in progress" : ""));
2753 TALLOC_FREE(frame);
2754 return WERR_DS_ATT_NOT_DEF_IN_SCHEMA;
2758 * We return the same class of attid as we were given. That
2759 * is, we trust the remote server not to use an
2760 * msDS-IntId value in the schema partition
2762 if (local_attid_as_enum != NULL) {
2763 *local_attid_as_enum = (enum drsuapi_DsAttributeId)attid_local;
2766 if (_sa != NULL) {
2767 *_sa = sa;
2770 TALLOC_FREE(frame);
2771 return WERR_OK;
2774 WERROR dsdb_attribute_drsuapi_to_ldb(struct ldb_context *ldb,
2775 const struct dsdb_schema *schema,
2776 const struct dsdb_schema_prefixmap *pfm_remote,
2777 const struct drsuapi_DsReplicaAttribute *in,
2778 TALLOC_CTX *mem_ctx,
2779 struct ldb_message_element *out,
2780 enum drsuapi_DsAttributeId *local_attid_as_enum)
2782 struct dsdb_syntax_ctx syntax_ctx;
2783 const struct dsdb_attribute *sa = NULL;
2784 WERROR werr;
2786 /* use default syntax conversion context */
2787 dsdb_syntax_ctx_init(&syntax_ctx, ldb, schema);
2788 syntax_ctx.pfm_remote = pfm_remote;
2790 werr = dsdb_attribute_drsuapi_remote_to_local(&syntax_ctx,
2791 in->attid,
2792 local_attid_as_enum,
2793 &sa);
2794 if (!W_ERROR_IS_OK(werr)) {
2795 return werr;
2798 return sa->syntax->drsuapi_to_ldb(&syntax_ctx, sa, in, mem_ctx, out);
2801 WERROR dsdb_attribute_ldb_to_drsuapi(struct ldb_context *ldb,
2802 const struct dsdb_schema *schema,
2803 const struct ldb_message_element *in,
2804 TALLOC_CTX *mem_ctx,
2805 struct drsuapi_DsReplicaAttribute *out)
2807 const struct dsdb_attribute *sa;
2808 struct dsdb_syntax_ctx syntax_ctx;
2810 sa = dsdb_attribute_by_lDAPDisplayName(schema, in->name);
2811 if (!sa) {
2812 return WERR_DS_ATT_NOT_DEF_IN_SCHEMA;
2815 /* use default syntax conversion context */
2816 dsdb_syntax_ctx_init(&syntax_ctx, ldb, schema);
2818 return sa->syntax->ldb_to_drsuapi(&syntax_ctx, sa, in, mem_ctx, out);