020d5f1b0714a7a4e7add1c31230d67c82e6a669
[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                         ret = ldb_msg_add_value(msg, rdn_attr->lDAPDisplayName, rdn_value, NULL);
157                         if (ret != LDB_SUCCESS) {
158                                 return WERR_FOOBAR;
159                         }
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;                             
165                         }
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;                             
171                         }
172                 }
173
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++;
181
182         }
183
184         whenChanged_t = nt_time_to_unix(whenChanged);
185         whenChanged_s = ldb_timestring(msg, whenChanged_t);
186         W_ERROR_HAVE_NO_MEMORY(whenChanged_s);
187
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);
195         }
196
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;
202 }
203
204 WERROR dsdb_extended_replicated_objects_convert(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 **objects)
216 {
217         WERROR status;
218         const struct dsdb_schema *schema;
219         struct dsdb_extended_replicated_objects *out;
220         const struct drsuapi_DsReplicaObjectListItemEx *cur;
221         uint32_t i;
222
223         schema = dsdb_get_schema(ldb);
224         if (!schema) {
225                 return WERR_DS_SCHEMA_NOT_LOADED;
226         }
227
228         status = dsdb_schema_pfm_contains_drsuapi_pfm(schema->prefixmap, mapping_ctr);
229         W_ERROR_NOT_OK_RETURN(status);
230
231         out = talloc_zero(mem_ctx, struct dsdb_extended_replicated_objects);
232         W_ERROR_HAVE_NO_MEMORY(out);
233         out->version            = DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION;
234
235         out->partition_dn       = ldb_dn_new(out, ldb, partition_dn);
236         W_ERROR_HAVE_NO_MEMORY(out->partition_dn);
237
238         out->source_dsa         = source_dsa;
239         out->uptodateness_vector= uptodateness_vector;
240
241         out->num_objects        = object_count;
242         out->objects            = talloc_array(out,
243                                                struct dsdb_extended_replicated_object,
244                                                out->num_objects);
245         W_ERROR_HAVE_NO_MEMORY(out->objects);
246
247         /* pass the linked attributes down to the repl_meta_data
248            module */
249         out->linked_attributes_count = linked_attributes_count;
250         out->linked_attributes       = linked_attributes;
251
252         for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
253                 if (i == out->num_objects) {
254                         return WERR_FOOBAR;
255                 }
256
257                 status = dsdb_convert_object_ex(ldb, schema,
258                                                 cur, gensec_skey,
259                                                 out->objects, &out->objects[i]);
260                 if (!W_ERROR_IS_OK(status)) {
261                         DEBUG(0,("Failed to convert object %s\n", cur->object.identifier->dn));
262                         return status;
263                 }
264         }
265         if (i != out->num_objects) {
266                 return WERR_FOOBAR;
267         }
268
269         *objects = out;
270         return WERR_OK;
271 }
272
273 WERROR dsdb_extended_replicated_objects_commit(struct ldb_context *ldb,
274                                                struct dsdb_extended_replicated_objects *objects,
275                                                uint64_t *notify_uSN)
276 {
277         struct ldb_result *ext_res;
278         int ret;
279         uint64_t seq_num1, seq_num2;
280
281         /* TODO: handle linked attributes */
282
283         /* wrap the extended operation in a transaction 
284            See [MS-DRSR] 3.3.2 Transactions
285          */
286         ret = ldb_transaction_start(ldb);
287         if (ret != LDB_SUCCESS) {
288                 DEBUG(0,(__location__ " Failed to start transaction\n"));
289                 return WERR_FOOBAR;
290         }
291
292         ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num1);
293         if (ret != LDB_SUCCESS) {
294                 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
295                 ldb_transaction_cancel(ldb);
296                 return WERR_FOOBAR;             
297         }
298
299         ret = ldb_extended(ldb, DSDB_EXTENDED_REPLICATED_OBJECTS_OID, objects, &ext_res);
300         if (ret != LDB_SUCCESS) {
301                 DEBUG(0,("Failed to apply records: %s: %s\n",
302                          ldb_errstring(ldb), ldb_strerror(ret)));
303                 ldb_transaction_cancel(ldb);
304                 return WERR_FOOBAR;
305         }
306         talloc_free(ext_res);
307
308         ret = ldb_transaction_prepare_commit(ldb);
309         if (ret != LDB_SUCCESS) {
310                 DEBUG(0,(__location__ " Failed to prepare commit of transaction\n"));
311                 return WERR_FOOBAR;
312         }
313
314         ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num2);
315         if (ret != LDB_SUCCESS) {
316                 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
317                 ldb_transaction_cancel(ldb);
318                 return WERR_FOOBAR;             
319         }
320
321         /* if this replication partner didn't need to be notified
322            before this transaction then it still doesn't need to be
323            notified, as the changes came from this server */    
324         if (seq_num2 > seq_num1 && seq_num1 <= *notify_uSN) {
325                 *notify_uSN = seq_num2;
326         }
327
328         ret = ldb_transaction_commit(ldb);
329         if (ret != LDB_SUCCESS) {
330                 DEBUG(0,(__location__ " Failed to commit transaction\n"));
331                 return WERR_FOOBAR;
332         }
333
334
335         DEBUG(2,("Replicated %u objects (%u linked attributes) for %s\n",
336                  objects->num_objects, objects->linked_attributes_count,
337                  ldb_dn_get_linearized(objects->partition_dn)));
338                  
339         return WERR_OK;
340 }
341
342 static WERROR dsdb_convert_object(struct ldb_context *ldb,
343                                   const struct dsdb_schema *schema,
344                                   const struct drsuapi_DsReplicaObjectListItem *in,
345                                   TALLOC_CTX *mem_ctx,
346                                   struct ldb_message **_msg)
347 {
348         WERROR status;
349         uint32_t i;
350         struct ldb_message *msg;
351
352         if (!in->object.identifier) {
353                 return WERR_FOOBAR;
354         }
355
356         if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
357                 return WERR_FOOBAR;
358         }
359
360         msg = ldb_msg_new(mem_ctx);
361         W_ERROR_HAVE_NO_MEMORY(msg);
362
363         msg->dn = ldb_dn_new(msg, ldb, in->object.identifier->dn);
364         W_ERROR_HAVE_NO_MEMORY(msg->dn);
365
366         msg->num_elements       = in->object.attribute_ctr.num_attributes;
367         msg->elements           = talloc_array(msg, struct ldb_message_element,
368                                                msg->num_elements);
369         W_ERROR_HAVE_NO_MEMORY(msg->elements);
370
371         for (i=0; i < msg->num_elements; i++) {
372                 struct drsuapi_DsReplicaAttribute *a;
373                 struct ldb_message_element *e;
374
375                 a = &in->object.attribute_ctr.attributes[i];
376                 e = &msg->elements[i];
377
378                 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, a, msg->elements, e);
379                 W_ERROR_NOT_OK_RETURN(status);
380         }
381
382
383         *_msg = msg;
384
385         return WERR_OK;
386 }
387
388 WERROR dsdb_origin_objects_commit(struct ldb_context *ldb,
389                                   TALLOC_CTX *mem_ctx,
390                                   const struct drsuapi_DsReplicaObjectListItem *first_object,
391                                   uint32_t *_num,
392                                   struct drsuapi_DsReplicaObjectIdentifier2 **_ids)
393 {
394         WERROR status;
395         const struct dsdb_schema *schema;
396         const struct drsuapi_DsReplicaObjectListItem *cur;
397         struct ldb_message **objects;
398         struct drsuapi_DsReplicaObjectIdentifier2 *ids;
399         uint32_t i;
400         uint32_t num_objects = 0;
401         const char * const attrs[] = {
402                 "objectGUID",
403                 "objectSid",
404                 NULL
405         };
406         struct ldb_result *res;
407         int ret;
408
409         schema = dsdb_get_schema(ldb);
410         if (!schema) {
411                 return WERR_DS_SCHEMA_NOT_LOADED;
412         }
413
414         for (cur = first_object; cur; cur = cur->next_object) {
415                 num_objects++;
416         }
417
418         if (num_objects == 0) {
419                 return WERR_OK;
420         }
421
422         ret = ldb_transaction_start(ldb);
423         if (ret != LDB_SUCCESS) {
424                 return WERR_DS_INTERNAL_FAILURE;
425         }
426
427         objects = talloc_array(mem_ctx, struct ldb_message *,
428                                num_objects);
429         if (objects == NULL) {
430                 status = WERR_NOMEM;
431                 goto cancel;
432         }
433
434         for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
435                 status = dsdb_convert_object(ldb, schema,
436                                              cur, objects, &objects[i]);
437                 if (!W_ERROR_IS_OK(status)) {
438                         goto cancel;
439                 }
440         }
441
442         ids = talloc_array(mem_ctx,
443                            struct drsuapi_DsReplicaObjectIdentifier2,
444                            num_objects);
445         if (ids == NULL) {
446                 status = WERR_NOMEM;
447                 goto cancel;
448         }
449
450         for (i=0; i < num_objects; i++) {
451                 struct dom_sid *sid = NULL;
452                 struct ldb_request *add_req;
453
454                 DEBUG(6,(__location__ ": adding %s\n", 
455                          ldb_dn_get_linearized(objects[i]->dn)));
456
457                 ret = ldb_build_add_req(&add_req,
458                                         ldb,
459                                         objects,
460                                         objects[i],
461                                         NULL,
462                                         NULL,
463                                         ldb_op_default_callback,
464                                         NULL);
465                 if (ret != LDB_SUCCESS) {
466                         status = WERR_DS_INTERNAL_FAILURE;
467                         goto cancel;
468                 }
469
470                 ret = ldb_request_add_control(add_req, LDB_CONTROL_RELAX_OID, true, NULL);
471                 if (ret != LDB_SUCCESS) {
472                         status = WERR_DS_INTERNAL_FAILURE;
473                         goto cancel;
474                 }
475                 
476                 ret = ldb_request(ldb, add_req);
477                 if (ret == LDB_SUCCESS) {
478                         ret = ldb_wait(add_req->handle, LDB_WAIT_ALL);
479                 }
480                 if (ret != LDB_SUCCESS) {
481                         DEBUG(0,(__location__ ": Failed add of %s - %s\n",
482                                  ldb_dn_get_linearized(objects[i]->dn), ldb_errstring(ldb)));
483                         status = WERR_DS_INTERNAL_FAILURE;
484                         goto cancel;
485                 }
486
487                 talloc_free(add_req);
488
489                 ret = ldb_search(ldb, objects, &res, objects[i]->dn,
490                                  LDB_SCOPE_BASE, attrs,
491                                  "(objectClass=*)");
492                 if (ret != LDB_SUCCESS) {
493                         status = WERR_DS_INTERNAL_FAILURE;
494                         goto cancel;
495                 }
496                 ids[i].guid = samdb_result_guid(res->msgs[0], "objectGUID");
497                 sid = samdb_result_dom_sid(objects, res->msgs[0], "objectSid");
498                 if (sid) {
499                         ids[i].sid = *sid;
500                 } else {
501                         ZERO_STRUCT(ids[i].sid);
502                 }
503         }
504
505         ret = ldb_transaction_commit(ldb);
506         if (ret != LDB_SUCCESS) {
507                 return WERR_DS_INTERNAL_FAILURE;
508         }
509
510         talloc_free(objects);
511
512         *_num = num_objects;
513         *_ids = ids;
514         return WERR_OK;
515
516 cancel:
517         talloc_free(objects);
518         ldb_transaction_cancel(ldb);
519         return status;
520 }