s4-netlogon: implement dcesrv_netr_DsRAddressToSitenamesExW
[Samba/aatanasov.git] / source4 / dsdb / repl / replicated_objects.c
blob5ae622eeaad300b50cc72dcd395d9f6fae664c98
1 /*
2 Unix SMB/CIFS mplementation.
3 Helper functions for applying replicated objects
5 Copyright (C) Stefan Metzmacher <metze@samba.org> 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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "dsdb/samdb/samdb.h"
24 #include "lib/ldb/include/ldb_errors.h"
25 #include "../lib/util/dlinklist.h"
26 #include "librpc/gen_ndr/ndr_misc.h"
27 #include "librpc/gen_ndr/ndr_drsuapi.h"
28 #include "librpc/gen_ndr/ndr_drsblobs.h"
29 #include "../lib/crypto/crypto.h"
30 #include "../libcli/drsuapi/drsuapi.h"
31 #include "libcli/auth/libcli_auth.h"
32 #include "param/param.h"
34 static WERROR dsdb_convert_object_ex(struct ldb_context *ldb,
35 const struct dsdb_schema *schema,
36 const struct drsuapi_DsReplicaObjectListItemEx *in,
37 const DATA_BLOB *gensec_skey,
38 TALLOC_CTX *mem_ctx,
39 struct dsdb_extended_replicated_object *out)
41 NTSTATUS nt_status;
42 enum ndr_err_code ndr_err;
43 WERROR status;
44 uint32_t i;
45 struct ldb_message *msg;
46 struct replPropertyMetaDataBlob *md;
47 struct ldb_val guid_value;
48 NTTIME whenChanged = 0;
49 time_t whenChanged_t;
50 const char *whenChanged_s;
51 const char *rdn_name = NULL;
52 const struct ldb_val *rdn_value = NULL;
53 const struct dsdb_attribute *rdn_attr = NULL;
54 uint32_t rdn_attid;
55 struct drsuapi_DsReplicaAttribute *name_a = NULL;
56 struct drsuapi_DsReplicaMetaData *name_d = NULL;
57 struct replPropertyMetaData1 *rdn_m = NULL;
58 struct dom_sid *sid = NULL;
59 uint32_t rid = 0;
60 int ret;
62 if (!in->object.identifier) {
63 return WERR_FOOBAR;
66 if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
67 return WERR_FOOBAR;
70 if (in->object.attribute_ctr.num_attributes != 0 && !in->meta_data_ctr) {
71 return WERR_FOOBAR;
74 if (in->object.attribute_ctr.num_attributes != in->meta_data_ctr->count) {
75 return WERR_FOOBAR;
78 sid = &in->object.identifier->sid;
79 if (sid->num_auths > 0) {
80 rid = sid->sub_auths[sid->num_auths - 1];
83 msg = ldb_msg_new(mem_ctx);
84 W_ERROR_HAVE_NO_MEMORY(msg);
86 msg->dn = ldb_dn_new(msg, ldb, in->object.identifier->dn);
87 W_ERROR_HAVE_NO_MEMORY(msg->dn);
89 rdn_name = ldb_dn_get_rdn_name(msg->dn);
90 rdn_attr = dsdb_attribute_by_lDAPDisplayName(schema, rdn_name);
91 if (!rdn_attr) {
92 return WERR_FOOBAR;
94 rdn_attid = rdn_attr->attributeID_id;
95 rdn_value = ldb_dn_get_rdn_val(msg->dn);
97 msg->num_elements = in->object.attribute_ctr.num_attributes;
98 msg->elements = talloc_array(msg, struct ldb_message_element,
99 msg->num_elements);
100 W_ERROR_HAVE_NO_MEMORY(msg->elements);
102 md = talloc(mem_ctx, struct replPropertyMetaDataBlob);
103 W_ERROR_HAVE_NO_MEMORY(md);
105 md->version = 1;
106 md->reserved = 0;
107 md->ctr.ctr1.count = in->meta_data_ctr->count;
108 md->ctr.ctr1.reserved = 0;
109 md->ctr.ctr1.array = talloc_array(mem_ctx,
110 struct replPropertyMetaData1,
111 md->ctr.ctr1.count + 1); /* +1 because of the RDN attribute */
112 W_ERROR_HAVE_NO_MEMORY(md->ctr.ctr1.array);
114 for (i=0; i < in->meta_data_ctr->count; i++) {
115 struct drsuapi_DsReplicaAttribute *a;
116 struct drsuapi_DsReplicaMetaData *d;
117 struct replPropertyMetaData1 *m;
118 struct ldb_message_element *e;
119 int j;
121 a = &in->object.attribute_ctr.attributes[i];
122 d = &in->meta_data_ctr->meta_data[i];
123 m = &md->ctr.ctr1.array[i];
124 e = &msg->elements[i];
126 for (j=0; j<a->value_ctr.num_values; j++) {
127 status = drsuapi_decrypt_attribute(a->value_ctr.values[j].blob, gensec_skey, rid, a);
128 W_ERROR_NOT_OK_RETURN(status);
131 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, a, msg->elements, e);
132 W_ERROR_NOT_OK_RETURN(status);
134 m->attid = a->attid;
135 m->version = d->version;
136 m->originating_change_time = d->originating_change_time;
137 m->originating_invocation_id = d->originating_invocation_id;
138 m->originating_usn = d->originating_usn;
139 m->local_usn = 0;
141 if (d->originating_change_time > whenChanged) {
142 whenChanged = d->originating_change_time;
145 if (a->attid == DRSUAPI_ATTRIBUTE_name) {
146 name_a = a;
147 name_d = d;
148 rdn_m = &md->ctr.ctr1.array[md->ctr.ctr1.count];
152 if (rdn_m) {
153 struct ldb_message_element *el;
154 el = ldb_msg_find_element(msg, rdn_attr->lDAPDisplayName);
155 if (!el) {
156 ret = ldb_msg_add_value(msg, rdn_attr->lDAPDisplayName, rdn_value, NULL);
157 if (ret != LDB_SUCCESS) {
158 return WERR_FOOBAR;
160 } else {
161 if (el->num_values != 1) {
162 DEBUG(0,(__location__ ": Unexpected num_values=%u\n",
163 el->num_values));
164 return WERR_FOOBAR;
166 if (!ldb_val_equal_exact(&el->values[0], rdn_value)) {
167 DEBUG(0,(__location__ ": RDN value changed? '%*.*s' '%*.*s'\n",
168 (int)el->values[0].length, (int)el->values[0].length, el->values[0].data,
169 (int)rdn_value->length, (int)rdn_value->length, rdn_value->data));
170 return WERR_FOOBAR;
174 rdn_m->attid = rdn_attid;
175 rdn_m->version = name_d->version;
176 rdn_m->originating_change_time = name_d->originating_change_time;
177 rdn_m->originating_invocation_id = name_d->originating_invocation_id;
178 rdn_m->originating_usn = name_d->originating_usn;
179 rdn_m->local_usn = 0;
180 md->ctr.ctr1.count++;
184 whenChanged_t = nt_time_to_unix(whenChanged);
185 whenChanged_s = ldb_timestring(msg, whenChanged_t);
186 W_ERROR_HAVE_NO_MEMORY(whenChanged_s);
188 ndr_err = ndr_push_struct_blob(&guid_value, msg,
189 lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
190 &in->object.identifier->guid,
191 (ndr_push_flags_fn_t)ndr_push_GUID);
192 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
193 nt_status = ndr_map_error2ntstatus(ndr_err);
194 return ntstatus_to_werror(nt_status);
197 out->msg = msg;
198 out->guid_value = guid_value;
199 out->when_changed = whenChanged_s;
200 out->meta_data = md;
201 return WERR_OK;
204 WERROR dsdb_extended_replicated_objects_commit(struct ldb_context *ldb,
205 const char *partition_dn,
206 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
207 uint32_t object_count,
208 const struct drsuapi_DsReplicaObjectListItemEx *first_object,
209 uint32_t linked_attributes_count,
210 const struct drsuapi_DsReplicaLinkedAttribute *linked_attributes,
211 const struct repsFromTo1 *source_dsa,
212 const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector,
213 const DATA_BLOB *gensec_skey,
214 TALLOC_CTX *mem_ctx,
215 struct dsdb_extended_replicated_objects **_out,
216 uint64_t *notify_uSN)
218 WERROR status;
219 const struct dsdb_schema *schema;
220 struct dsdb_extended_replicated_objects *out;
221 struct ldb_result *ext_res;
222 const struct drsuapi_DsReplicaObjectListItemEx *cur;
223 uint32_t i;
224 int ret;
225 uint64_t seq_num1, seq_num2;
227 schema = dsdb_get_schema(ldb);
228 if (!schema) {
229 return WERR_DS_SCHEMA_NOT_LOADED;
232 status = dsdb_verify_oid_mappings_drsuapi(schema, mapping_ctr);
233 W_ERROR_NOT_OK_RETURN(status);
235 out = talloc_zero(mem_ctx, struct dsdb_extended_replicated_objects);
236 W_ERROR_HAVE_NO_MEMORY(out);
237 out->version = DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION;
239 out->partition_dn = ldb_dn_new(out, ldb, partition_dn);
240 W_ERROR_HAVE_NO_MEMORY(out->partition_dn);
242 out->source_dsa = source_dsa;
243 out->uptodateness_vector= uptodateness_vector;
245 out->num_objects = object_count;
246 out->objects = talloc_array(out,
247 struct dsdb_extended_replicated_object,
248 out->num_objects);
249 W_ERROR_HAVE_NO_MEMORY(out->objects);
251 /* pass the linked attributes down to the repl_meta_data
252 module */
253 out->linked_attributes_count = linked_attributes_count;
254 out->linked_attributes = linked_attributes;
256 for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
257 if (i == out->num_objects) {
258 return WERR_FOOBAR;
261 status = dsdb_convert_object_ex(ldb, schema,
262 cur, gensec_skey,
263 out->objects, &out->objects[i]);
264 if (!W_ERROR_IS_OK(status)) {
265 DEBUG(0,("Failed to convert object %s\n", cur->object.identifier->dn));
266 return status;
269 if (i != out->num_objects) {
270 return WERR_FOOBAR;
273 /* TODO: handle linked attributes */
275 /* wrap the extended operation in a transaction
276 See [MS-DRSR] 3.3.2 Transactions
278 ret = ldb_transaction_start(ldb);
279 if (ret != LDB_SUCCESS) {
280 DEBUG(0,(__location__ " Failed to start transaction\n"));
281 talloc_free(out);
282 return WERR_FOOBAR;
285 ret = dsdb_load_partition_usn(ldb, out->partition_dn, &seq_num1);
286 if (ret != LDB_SUCCESS) {
287 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
288 talloc_free(out);
289 ldb_transaction_cancel(ldb);
290 return WERR_FOOBAR;
293 ret = ldb_extended(ldb, DSDB_EXTENDED_REPLICATED_OBJECTS_OID, out, &ext_res);
294 if (ret != LDB_SUCCESS) {
295 DEBUG(0,("Failed to apply records: %s: %s\n",
296 ldb_errstring(ldb), ldb_strerror(ret)));
297 talloc_free(out);
298 ldb_transaction_cancel(ldb);
299 return WERR_FOOBAR;
301 talloc_free(ext_res);
303 ret = ldb_transaction_prepare_commit(ldb);
304 if (ret != LDB_SUCCESS) {
305 DEBUG(0,(__location__ " Failed to prepare commit of transaction\n"));
306 talloc_free(out);
307 return WERR_FOOBAR;
310 ret = dsdb_load_partition_usn(ldb, out->partition_dn, &seq_num2);
311 if (ret != LDB_SUCCESS) {
312 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
313 talloc_free(out);
314 ldb_transaction_cancel(ldb);
315 return WERR_FOOBAR;
318 /* if this replication partner didn't need to be notified
319 before this transaction then it still doesn't need to be
320 notified, as the changes came from this server */
321 if (seq_num2 > seq_num1 && seq_num1 <= *notify_uSN) {
322 *notify_uSN = seq_num2;
325 ret = ldb_transaction_commit(ldb);
326 if (ret != LDB_SUCCESS) {
327 DEBUG(0,(__location__ " Failed to commit transaction\n"));
328 talloc_free(out);
329 return WERR_FOOBAR;
333 DEBUG(2,("Replicated %u objects (%u linked attributes) for %s\n",
334 out->num_objects, out->linked_attributes_count,
335 ldb_dn_get_linearized(out->partition_dn)));
338 if (_out) {
339 *_out = out;
340 } else {
341 talloc_free(out);
344 return WERR_OK;
347 static WERROR dsdb_convert_object(struct ldb_context *ldb,
348 const struct dsdb_schema *schema,
349 const struct drsuapi_DsReplicaObjectListItem *in,
350 TALLOC_CTX *mem_ctx,
351 struct ldb_message **_msg)
353 WERROR status;
354 uint32_t i;
355 struct ldb_message *msg;
357 if (!in->object.identifier) {
358 return WERR_FOOBAR;
361 if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
362 return WERR_FOOBAR;
365 msg = ldb_msg_new(mem_ctx);
366 W_ERROR_HAVE_NO_MEMORY(msg);
368 msg->dn = ldb_dn_new(msg, ldb, in->object.identifier->dn);
369 W_ERROR_HAVE_NO_MEMORY(msg->dn);
371 msg->num_elements = in->object.attribute_ctr.num_attributes;
372 msg->elements = talloc_array(msg, struct ldb_message_element,
373 msg->num_elements);
374 W_ERROR_HAVE_NO_MEMORY(msg->elements);
376 for (i=0; i < msg->num_elements; i++) {
377 struct drsuapi_DsReplicaAttribute *a;
378 struct ldb_message_element *e;
380 a = &in->object.attribute_ctr.attributes[i];
381 e = &msg->elements[i];
383 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, a, msg->elements, e);
384 W_ERROR_NOT_OK_RETURN(status);
388 *_msg = msg;
390 return WERR_OK;
393 WERROR dsdb_origin_objects_commit(struct ldb_context *ldb,
394 TALLOC_CTX *mem_ctx,
395 const struct drsuapi_DsReplicaObjectListItem *first_object,
396 uint32_t *_num,
397 struct drsuapi_DsReplicaObjectIdentifier2 **_ids)
399 WERROR status;
400 const struct dsdb_schema *schema;
401 const struct drsuapi_DsReplicaObjectListItem *cur;
402 struct ldb_message **objects;
403 struct drsuapi_DsReplicaObjectIdentifier2 *ids;
404 uint32_t i;
405 uint32_t num_objects = 0;
406 const char * const attrs[] = {
407 "objectGUID",
408 "objectSid",
409 NULL
411 struct ldb_result *res;
412 int ret;
414 schema = dsdb_get_schema(ldb);
415 if (!schema) {
416 return WERR_DS_SCHEMA_NOT_LOADED;
419 for (cur = first_object; cur; cur = cur->next_object) {
420 num_objects++;
423 if (num_objects == 0) {
424 return WERR_OK;
427 objects = talloc_array(mem_ctx, struct ldb_message *,
428 num_objects);
429 W_ERROR_HAVE_NO_MEMORY(objects);
431 for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
432 status = dsdb_convert_object(ldb, schema,
433 cur, objects, &objects[i]);
434 W_ERROR_NOT_OK_RETURN(status);
437 ids = talloc_array(mem_ctx,
438 struct drsuapi_DsReplicaObjectIdentifier2,
439 num_objects);
440 W_ERROR_HAVE_NO_MEMORY(objects);
442 for (i=0; i < num_objects; i++) {
443 struct dom_sid *sid = NULL;
444 ret = ldb_add(ldb, objects[i]);
445 if (ret != 0) {
446 goto cancel;
448 ret = ldb_search(ldb, objects, &res, objects[i]->dn,
449 LDB_SCOPE_BASE, attrs,
450 "(objectClass=*)");
451 if (ret != 0) {
452 goto cancel;
454 ids[i].guid = samdb_result_guid(res->msgs[0], "objectGUID");
455 sid = samdb_result_dom_sid(objects, res->msgs[0], "objectSid");
456 if (sid) {
457 ids[i].sid = *sid;
458 } else {
459 ZERO_STRUCT(ids[i].sid);
463 talloc_free(objects);
465 *_num = num_objects;
466 *_ids = ids;
467 return WERR_OK;
468 cancel:
469 talloc_free(objects);
470 ldb_transaction_cancel(ldb);
471 return WERR_FOOBAR;