s4:dsdb/repl/replicated_objects - Applicate also here the new "lDAPDisplayName" generator
[ira/wip.git] / source4 / dsdb / repl / replicated_objects.c
1 /* 
2    Unix SMB/CIFS mplementation.
3    Helper functions for applying replicated objects
4    
5    Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
6     
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.
11    
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.
16    
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/>.
19    
20 */
21
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"
33
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)
40 {
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;
61
62         if (!in->object.identifier) {
63                 return WERR_FOOBAR;
64         }
65
66         if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
67                 return WERR_FOOBAR;
68         }
69
70         if (in->object.attribute_ctr.num_attributes != 0 && !in->meta_data_ctr) {
71                 return WERR_FOOBAR;
72         }
73
74         if (in->object.attribute_ctr.num_attributes != in->meta_data_ctr->count) {
75                 return WERR_FOOBAR;
76         }
77
78         sid = &in->object.identifier->sid;
79         if (sid->num_auths > 0) {
80                 rid = sid->sub_auths[sid->num_auths - 1];
81         }
82
83         msg = ldb_msg_new(mem_ctx);
84         W_ERROR_HAVE_NO_MEMORY(msg);
85
86         msg->dn                 = ldb_dn_new(msg, ldb, in->object.identifier->dn);
87         W_ERROR_HAVE_NO_MEMORY(msg->dn);
88
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;
93         }
94         rdn_attid       = rdn_attr->attributeID_id;
95         rdn_value       = ldb_dn_get_rdn_val(msg->dn);
96
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);
101
102         md = talloc(mem_ctx, struct replPropertyMetaDataBlob);
103         W_ERROR_HAVE_NO_MEMORY(md);
104
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);
113
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;
120
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];
125
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);
129                 }
130
131                 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, a, msg->elements, e);
132                 W_ERROR_NOT_OK_RETURN(status);
133
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;
140
141                 if (d->originating_change_time > whenChanged) {
142                         whenChanged = d->originating_change_time;
143                 }
144
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];
149                 }
150         }
151
152         if (rdn_m) {
153                 struct ldb_message_element *el;
154                 el = ldb_msg_find_element(msg, rdn_attr->lDAPDisplayName);
155                 if (!el) {
156                         /* we assume that the RDN has prefix "CN" */
157                         ret = ldb_msg_add_string(msg, rdn_attr->lDAPDisplayName,
158                                 samdb_cn_to_lDAPDisplayName(mem_ctx,
159                                         (const char *) rdn_value->data));
160                         if (ret != LDB_SUCCESS) {
161                                 return WERR_FOOBAR;
162                         }
163                 } else {
164                         if (el->num_values != 1) {
165                                 DEBUG(0,(__location__ ": Unexpected num_values=%u\n",
166                                          el->num_values));
167                                 return WERR_FOOBAR;                             
168                         }
169                         if (!ldb_val_equal_exact(&el->values[0], rdn_value)) {
170                                 DEBUG(0,(__location__ ": RDN value changed? '%*.*s' '%*.*s'\n",
171                                          (int)el->values[0].length, (int)el->values[0].length, el->values[0].data,
172                                          (int)rdn_value->length, (int)rdn_value->length, rdn_value->data));
173                                 return WERR_FOOBAR;                             
174                         }
175                 }
176
177                 rdn_m->attid                            = rdn_attid;
178                 rdn_m->version                          = name_d->version;
179                 rdn_m->originating_change_time          = name_d->originating_change_time;
180                 rdn_m->originating_invocation_id        = name_d->originating_invocation_id;
181                 rdn_m->originating_usn                  = name_d->originating_usn;
182                 rdn_m->local_usn                        = 0;
183                 md->ctr.ctr1.count++;
184
185         }
186
187         whenChanged_t = nt_time_to_unix(whenChanged);
188         whenChanged_s = ldb_timestring(msg, whenChanged_t);
189         W_ERROR_HAVE_NO_MEMORY(whenChanged_s);
190
191         ndr_err = ndr_push_struct_blob(&guid_value, msg, 
192                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
193                                        &in->object.identifier->guid,
194                                          (ndr_push_flags_fn_t)ndr_push_GUID);
195         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
196                 nt_status = ndr_map_error2ntstatus(ndr_err);
197                 return ntstatus_to_werror(nt_status);
198         }
199
200         out->msg                = msg;
201         out->guid_value         = guid_value;
202         out->when_changed       = whenChanged_s;
203         out->meta_data          = md;
204         return WERR_OK;
205 }
206
207 WERROR dsdb_extended_replicated_objects_convert(struct ldb_context *ldb,
208                                                 const char *partition_dn,
209                                                 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
210                                                 uint32_t object_count,
211                                                 const struct drsuapi_DsReplicaObjectListItemEx *first_object,
212                                                 uint32_t linked_attributes_count,
213                                                 const struct drsuapi_DsReplicaLinkedAttribute *linked_attributes,
214                                                 const struct repsFromTo1 *source_dsa,
215                                                 const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector,
216                                                 const DATA_BLOB *gensec_skey,
217                                                 TALLOC_CTX *mem_ctx,
218                                                 struct dsdb_extended_replicated_objects **objects)
219 {
220         WERROR status;
221         const struct dsdb_schema *schema;
222         struct dsdb_extended_replicated_objects *out;
223         const struct drsuapi_DsReplicaObjectListItemEx *cur;
224         uint32_t i;
225
226         schema = dsdb_get_schema(ldb);
227         if (!schema) {
228                 return WERR_DS_SCHEMA_NOT_LOADED;
229         }
230
231         status = dsdb_schema_pfm_contains_drsuapi_pfm(schema->prefixmap, mapping_ctr);
232         W_ERROR_NOT_OK_RETURN(status);
233
234         out = talloc_zero(mem_ctx, struct dsdb_extended_replicated_objects);
235         W_ERROR_HAVE_NO_MEMORY(out);
236         out->version            = DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION;
237
238         out->partition_dn       = ldb_dn_new(out, ldb, partition_dn);
239         W_ERROR_HAVE_NO_MEMORY(out->partition_dn);
240
241         out->source_dsa         = source_dsa;
242         out->uptodateness_vector= uptodateness_vector;
243
244         out->num_objects        = object_count;
245         out->objects            = talloc_array(out,
246                                                struct dsdb_extended_replicated_object,
247                                                out->num_objects);
248         W_ERROR_HAVE_NO_MEMORY(out->objects);
249
250         /* pass the linked attributes down to the repl_meta_data
251            module */
252         out->linked_attributes_count = linked_attributes_count;
253         out->linked_attributes       = linked_attributes;
254
255         for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
256                 if (i == out->num_objects) {
257                         return WERR_FOOBAR;
258                 }
259
260                 status = dsdb_convert_object_ex(ldb, schema,
261                                                 cur, gensec_skey,
262                                                 out->objects, &out->objects[i]);
263                 if (!W_ERROR_IS_OK(status)) {
264                         DEBUG(0,("Failed to convert object %s\n", cur->object.identifier->dn));
265                         return status;
266                 }
267         }
268         if (i != out->num_objects) {
269                 return WERR_FOOBAR;
270         }
271
272         *objects = out;
273         return WERR_OK;
274 }
275
276 WERROR dsdb_extended_replicated_objects_commit(struct ldb_context *ldb,
277                                                struct dsdb_extended_replicated_objects *objects,
278                                                uint64_t *notify_uSN)
279 {
280         struct ldb_result *ext_res;
281         int ret;
282         uint64_t seq_num1, seq_num2;
283
284         /* TODO: handle linked attributes */
285
286         /* wrap the extended operation in a transaction 
287            See [MS-DRSR] 3.3.2 Transactions
288          */
289         ret = ldb_transaction_start(ldb);
290         if (ret != LDB_SUCCESS) {
291                 DEBUG(0,(__location__ " Failed to start transaction\n"));
292                 return WERR_FOOBAR;
293         }
294
295         ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num1);
296         if (ret != LDB_SUCCESS) {
297                 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
298                 ldb_transaction_cancel(ldb);
299                 return WERR_FOOBAR;             
300         }
301
302         ret = ldb_extended(ldb, DSDB_EXTENDED_REPLICATED_OBJECTS_OID, objects, &ext_res);
303         if (ret != LDB_SUCCESS) {
304                 DEBUG(0,("Failed to apply records: %s: %s\n",
305                          ldb_errstring(ldb), ldb_strerror(ret)));
306                 ldb_transaction_cancel(ldb);
307                 return WERR_FOOBAR;
308         }
309         talloc_free(ext_res);
310
311         ret = ldb_transaction_prepare_commit(ldb);
312         if (ret != LDB_SUCCESS) {
313                 DEBUG(0,(__location__ " Failed to prepare commit of transaction\n"));
314                 return WERR_FOOBAR;
315         }
316
317         ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num2);
318         if (ret != LDB_SUCCESS) {
319                 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
320                 ldb_transaction_cancel(ldb);
321                 return WERR_FOOBAR;             
322         }
323
324         /* if this replication partner didn't need to be notified
325            before this transaction then it still doesn't need to be
326            notified, as the changes came from this server */    
327         if (seq_num2 > seq_num1 && seq_num1 <= *notify_uSN) {
328                 *notify_uSN = seq_num2;
329         }
330
331         ret = ldb_transaction_commit(ldb);
332         if (ret != LDB_SUCCESS) {
333                 DEBUG(0,(__location__ " Failed to commit transaction\n"));
334                 return WERR_FOOBAR;
335         }
336
337
338         DEBUG(2,("Replicated %u objects (%u linked attributes) for %s\n",
339                  objects->num_objects, objects->linked_attributes_count,
340                  ldb_dn_get_linearized(objects->partition_dn)));
341                  
342         return WERR_OK;
343 }
344
345 static WERROR dsdb_convert_object(struct ldb_context *ldb,
346                                   const struct dsdb_schema *schema,
347                                   const struct drsuapi_DsReplicaObjectListItem *in,
348                                   TALLOC_CTX *mem_ctx,
349                                   struct ldb_message **_msg)
350 {
351         WERROR status;
352         uint32_t i;
353         struct ldb_message *msg;
354
355         if (!in->object.identifier) {
356                 return WERR_FOOBAR;
357         }
358
359         if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
360                 return WERR_FOOBAR;
361         }
362
363         msg = ldb_msg_new(mem_ctx);
364         W_ERROR_HAVE_NO_MEMORY(msg);
365
366         msg->dn = ldb_dn_new(msg, ldb, in->object.identifier->dn);
367         W_ERROR_HAVE_NO_MEMORY(msg->dn);
368
369         msg->num_elements       = in->object.attribute_ctr.num_attributes;
370         msg->elements           = talloc_array(msg, struct ldb_message_element,
371                                                msg->num_elements);
372         W_ERROR_HAVE_NO_MEMORY(msg->elements);
373
374         for (i=0; i < msg->num_elements; i++) {
375                 struct drsuapi_DsReplicaAttribute *a;
376                 struct ldb_message_element *e;
377
378                 a = &in->object.attribute_ctr.attributes[i];
379                 e = &msg->elements[i];
380
381                 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, a, msg->elements, e);
382                 W_ERROR_NOT_OK_RETURN(status);
383         }
384
385
386         *_msg = msg;
387
388         return WERR_OK;
389 }
390
391 WERROR dsdb_origin_objects_commit(struct ldb_context *ldb,
392                                   TALLOC_CTX *mem_ctx,
393                                   const struct drsuapi_DsReplicaObjectListItem *first_object,
394                                   uint32_t *_num,
395                                   struct drsuapi_DsReplicaObjectIdentifier2 **_ids)
396 {
397         WERROR status;
398         const struct dsdb_schema *schema;
399         const struct drsuapi_DsReplicaObjectListItem *cur;
400         struct ldb_message **objects;
401         struct drsuapi_DsReplicaObjectIdentifier2 *ids;
402         uint32_t i;
403         uint32_t num_objects = 0;
404         const char * const attrs[] = {
405                 "objectGUID",
406                 "objectSid",
407                 NULL
408         };
409         struct ldb_result *res;
410         int ret;
411
412         schema = dsdb_get_schema(ldb);
413         if (!schema) {
414                 return WERR_DS_SCHEMA_NOT_LOADED;
415         }
416
417         for (cur = first_object; cur; cur = cur->next_object) {
418                 num_objects++;
419         }
420
421         if (num_objects == 0) {
422                 return WERR_OK;
423         }
424
425         ret = ldb_transaction_start(ldb);
426         if (ret != LDB_SUCCESS) {
427                 return WERR_DS_INTERNAL_FAILURE;
428         }
429
430         objects = talloc_array(mem_ctx, struct ldb_message *,
431                                num_objects);
432         if (objects == NULL) {
433                 status = WERR_NOMEM;
434                 goto cancel;
435         }
436
437         for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
438                 status = dsdb_convert_object(ldb, schema,
439                                              cur, objects, &objects[i]);
440                 if (!W_ERROR_IS_OK(status)) {
441                         goto cancel;
442                 }
443         }
444
445         ids = talloc_array(mem_ctx,
446                            struct drsuapi_DsReplicaObjectIdentifier2,
447                            num_objects);
448         if (ids == NULL) {
449                 status = WERR_NOMEM;
450                 goto cancel;
451         }
452
453         for (i=0; i < num_objects; i++) {
454                 struct dom_sid *sid = NULL;
455                 struct ldb_request *add_req;
456
457                 DEBUG(6,(__location__ ": adding %s\n", 
458                          ldb_dn_get_linearized(objects[i]->dn)));
459
460                 ret = ldb_build_add_req(&add_req,
461                                         ldb,
462                                         objects,
463                                         objects[i],
464                                         NULL,
465                                         NULL,
466                                         ldb_op_default_callback,
467                                         NULL);
468                 if (ret != LDB_SUCCESS) {
469                         status = WERR_DS_INTERNAL_FAILURE;
470                         goto cancel;
471                 }
472
473                 ret = ldb_request_add_control(add_req, LDB_CONTROL_RELAX_OID, true, NULL);
474                 if (ret != LDB_SUCCESS) {
475                         status = WERR_DS_INTERNAL_FAILURE;
476                         goto cancel;
477                 }
478                 
479                 ret = ldb_request(ldb, add_req);
480                 if (ret == LDB_SUCCESS) {
481                         ret = ldb_wait(add_req->handle, LDB_WAIT_ALL);
482                 }
483                 if (ret != LDB_SUCCESS) {
484                         DEBUG(0,(__location__ ": Failed add of %s - %s\n",
485                                  ldb_dn_get_linearized(objects[i]->dn), ldb_errstring(ldb)));
486                         status = WERR_DS_INTERNAL_FAILURE;
487                         goto cancel;
488                 }
489
490                 talloc_free(add_req);
491
492                 ret = ldb_search(ldb, objects, &res, objects[i]->dn,
493                                  LDB_SCOPE_BASE, attrs,
494                                  "(objectClass=*)");
495                 if (ret != LDB_SUCCESS) {
496                         status = WERR_DS_INTERNAL_FAILURE;
497                         goto cancel;
498                 }
499                 ids[i].guid = samdb_result_guid(res->msgs[0], "objectGUID");
500                 sid = samdb_result_dom_sid(objects, res->msgs[0], "objectSid");
501                 if (sid) {
502                         ids[i].sid = *sid;
503                 } else {
504                         ZERO_STRUCT(ids[i].sid);
505                 }
506         }
507
508         ret = ldb_transaction_commit(ldb);
509         if (ret != LDB_SUCCESS) {
510                 return WERR_DS_INTERNAL_FAILURE;
511         }
512
513         talloc_free(objects);
514
515         *_num = num_objects;
516         *_ids = ids;
517         return WERR_OK;
518
519 cancel:
520         talloc_free(objects);
521         ldb_transaction_cancel(ldb);
522         return status;
523 }