2 Unix SMB/CIFS mplementation.
3 Helper functions for applying replicated objects
5 Copyright (C) Stefan Metzmacher 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "dsdb/samdb/samdb.h"
25 #include "lib/ldb/include/ldb_errors.h"
26 #include "lib/util/dlinklist.h"
27 #include "librpc/gen_ndr/ndr_misc.h"
28 #include "librpc/gen_ndr/ndr_drsuapi.h"
29 #include "librpc/gen_ndr/ndr_drsblobs.h"
31 static WERROR dsdb_convert_object(struct ldb_context *ldb,
32 const struct dsdb_schema *schema,
33 struct dsdb_extended_replicated_objects *ctr,
34 const struct drsuapi_DsReplicaObjectListItemEx *in,
36 struct dsdb_extended_replicated_object *out)
41 struct ldb_message *msg;
42 struct replPropertyMetaDataBlob *md;
43 struct ldb_val guid_value;
44 NTTIME whenChanged = 0;
46 const char *whenChanged_s;
47 const char *rdn_name = NULL;
48 const struct ldb_val *rdn_value = NULL;
49 const struct dsdb_attribute *rdn_attr = NULL;
51 struct drsuapi_DsReplicaAttribute *name_a = NULL;
52 struct drsuapi_DsReplicaMetaData *name_d = NULL;
53 struct replPropertyMetaData1 *rdn_m = NULL;
56 if (!in->object.identifier) {
60 if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
64 msg = ldb_msg_new(mem_ctx);
65 W_ERROR_HAVE_NO_MEMORY(msg);
67 msg->dn = ldb_dn_new(msg, ldb, in->object.identifier->dn);
68 W_ERROR_HAVE_NO_MEMORY(msg->dn);
70 rdn_name = ldb_dn_get_rdn_name(msg->dn);
71 rdn_attr = dsdb_attribute_by_lDAPDisplayName(schema, rdn_name);
75 rdn_attid = rdn_attr->attributeID_id;
76 rdn_value = ldb_dn_get_rdn_val(msg->dn);
78 msg->num_elements = in->object.attribute_ctr.num_attributes;
79 msg->elements = talloc_array(msg, struct ldb_message_element,
81 W_ERROR_HAVE_NO_MEMORY(msg->elements);
83 for (i=0; i < msg->num_elements; i++) {
84 status = dsdb_attribute_drsuapi_to_ldb(schema,
85 &in->object.attribute_ctr.attributes[i],
86 msg->elements, &msg->elements[i]);
87 W_ERROR_NOT_OK_RETURN(status);
90 if (in->object.attribute_ctr.num_attributes != 0 && !in->meta_data_ctr) {
94 if (in->object.attribute_ctr.num_attributes != in->meta_data_ctr->count) {
98 md = talloc(mem_ctx, struct replPropertyMetaDataBlob);
99 W_ERROR_HAVE_NO_MEMORY(md);
103 md->ctr.ctr1.count = in->meta_data_ctr->count;
104 md->ctr.ctr1.reserved = 0;
105 md->ctr.ctr1.array = talloc_array(mem_ctx,
106 struct replPropertyMetaData1,
107 md->ctr.ctr1.count + 1); /* +1 because of the RDN attribute */
108 W_ERROR_HAVE_NO_MEMORY(md->ctr.ctr1.array);
110 for (i=0; i < in->meta_data_ctr->count; i++) {
111 struct drsuapi_DsReplicaAttribute *a;
112 struct drsuapi_DsReplicaMetaData *d;
113 struct replPropertyMetaData1 *m;
115 a = &in->object.attribute_ctr.attributes[i];
116 d = &in->meta_data_ctr->meta_data[i];
117 m = &md->ctr.ctr1.array[i];
120 m->version = d->version;
121 m->orginating_time = d->orginating_time;
122 m->orginating_invocation_id = d->orginating_invocation_id;
123 m->orginating_usn = d->orginating_usn;
126 if (d->orginating_time > whenChanged) {
127 whenChanged = d->orginating_time;
130 if (a->attid == DRSUAPI_ATTRIBUTE_name) {
133 rdn_m = &md->ctr.ctr1.array[md->ctr.ctr1.count];
138 ret = ldb_msg_add_value(msg, rdn_attr->lDAPDisplayName, rdn_value, NULL);
139 if (ret != LDB_SUCCESS) {
143 rdn_m->attid = rdn_attid;
144 rdn_m->version = name_d->version;
145 rdn_m->orginating_time = name_d->orginating_time;
146 rdn_m->orginating_invocation_id = name_d->orginating_invocation_id;
147 rdn_m->orginating_usn = name_d->orginating_usn;
148 rdn_m->local_usn = 0;
149 md->ctr.ctr1.count++;
153 whenChanged_t = nt_time_to_unix(whenChanged);
154 whenChanged_s = ldb_timestring(msg, whenChanged_t);
155 W_ERROR_HAVE_NO_MEMORY(whenChanged_s);
157 nt_status = ndr_push_struct_blob(&guid_value, msg, &in->object.identifier->guid,
158 (ndr_push_flags_fn_t)ndr_push_GUID);
159 if (!NT_STATUS_IS_OK(nt_status)) {
160 return ntstatus_to_werror(nt_status);
164 out->guid_value = guid_value;
165 out->when_changed = whenChanged_s;
170 WERROR dsdb_extended_replicated_objects_commit(struct ldb_context *ldb,
171 const char *partition_dn,
172 const struct dsdb_schema *schema,
173 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
174 uint32_t object_count,
175 const struct drsuapi_DsReplicaObjectListItemEx *first_object,
176 uint32_t linked_attributes_count,
177 const struct drsuapi_DsReplicaLinkedAttribute *linked_attributes,
178 const struct repsFromTo1 *source_dsa,
179 const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector,
181 struct dsdb_extended_replicated_objects **_out)
184 struct dsdb_extended_replicated_objects *out;
185 struct ldb_result *ext_res;
186 const struct drsuapi_DsReplicaObjectListItemEx *cur;
190 status = dsdb_verify_oid_mappings(schema, mapping_ctr);
191 W_ERROR_NOT_OK_RETURN(status);
193 out = talloc_zero(mem_ctx, struct dsdb_extended_replicated_objects);
194 W_ERROR_HAVE_NO_MEMORY(out);
196 out->partition_dn = ldb_dn_new(out, ldb, partition_dn);
197 W_ERROR_HAVE_NO_MEMORY(out->partition_dn);
199 out->source_dsa = source_dsa;
200 out->uptodateness_vector= uptodateness_vector;
202 out->num_objects = object_count;
203 out->objects = talloc_array(out,
204 struct dsdb_extended_replicated_object,
206 W_ERROR_HAVE_NO_MEMORY(out->objects);
208 for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
209 if (i == out->num_objects) {
213 status = dsdb_convert_object(ldb, schema, out, cur, out->objects, &out->objects[i]);
214 W_ERROR_NOT_OK_RETURN(status);
216 if (i != out->num_objects) {
220 /* TODO: handle linked attributes */
222 ret = ldb_extended(ldb, DSDB_EXTENDED_REPLICATED_OBJECTS_OID, out, &ext_res);
223 if (ret != LDB_SUCCESS) {
224 DEBUG(0,("Failed to apply records: %d\n",
229 talloc_free(ext_res);