idl: Use DRSUAPI_ATTID_ prefix instead of DRSUAPI_ATTRIBUTE_ for ATTID values
[sfrench/samba-autobuild/.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 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         WERROR status;
43         uint32_t i;
44         struct ldb_message *msg;
45         struct replPropertyMetaDataBlob *md;
46         struct ldb_val guid_value;
47         NTTIME whenChanged = 0;
48         time_t whenChanged_t;
49         const char *whenChanged_s;
50         const char *rdn_name = NULL;
51         const struct ldb_val *rdn_value = NULL;
52         const struct dsdb_attribute *rdn_attr = NULL;
53         uint32_t rdn_attid;
54         struct drsuapi_DsReplicaAttribute *name_a = NULL;
55         struct drsuapi_DsReplicaMetaData *name_d = NULL;
56         struct replPropertyMetaData1 *rdn_m = NULL;
57         struct dom_sid *sid = NULL;
58         uint32_t rid = 0;
59         int ret;
60
61         if (!in->object.identifier) {
62                 return WERR_FOOBAR;
63         }
64
65         if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
66                 return WERR_FOOBAR;
67         }
68
69         if (in->object.attribute_ctr.num_attributes != 0 && !in->meta_data_ctr) {
70                 return WERR_FOOBAR;
71         }
72
73         if (in->object.attribute_ctr.num_attributes != in->meta_data_ctr->count) {
74                 return WERR_FOOBAR;
75         }
76
77         sid = &in->object.identifier->sid;
78         if (sid->num_auths > 0) {
79                 rid = sid->sub_auths[sid->num_auths - 1];
80         }
81
82         msg = ldb_msg_new(mem_ctx);
83         W_ERROR_HAVE_NO_MEMORY(msg);
84
85         msg->dn                 = ldb_dn_new(msg, ldb, in->object.identifier->dn);
86         W_ERROR_HAVE_NO_MEMORY(msg->dn);
87
88         rdn_name        = ldb_dn_get_rdn_name(msg->dn);
89         rdn_attr        = dsdb_attribute_by_lDAPDisplayName(schema, rdn_name);
90         if (!rdn_attr) {
91                 return WERR_FOOBAR;
92         }
93         rdn_attid       = rdn_attr->attributeID_id;
94         rdn_value       = ldb_dn_get_rdn_val(msg->dn);
95
96         msg->num_elements       = in->object.attribute_ctr.num_attributes;
97         msg->elements           = talloc_array(msg, struct ldb_message_element,
98                                                msg->num_elements);
99         W_ERROR_HAVE_NO_MEMORY(msg->elements);
100
101         md = talloc(mem_ctx, struct replPropertyMetaDataBlob);
102         W_ERROR_HAVE_NO_MEMORY(md);
103
104         md->version             = 1;
105         md->reserved            = 0;
106         md->ctr.ctr1.count      = in->meta_data_ctr->count;
107         md->ctr.ctr1.reserved   = 0;
108         md->ctr.ctr1.array      = talloc_array(mem_ctx,
109                                                struct replPropertyMetaData1,
110                                                md->ctr.ctr1.count + 1); /* +1 because of the RDN attribute */
111         W_ERROR_HAVE_NO_MEMORY(md->ctr.ctr1.array);
112
113         for (i=0; i < in->meta_data_ctr->count; i++) {
114                 struct drsuapi_DsReplicaAttribute *a;
115                 struct drsuapi_DsReplicaMetaData *d;
116                 struct replPropertyMetaData1 *m;
117                 struct ldb_message_element *e;
118                 uint32_t j;
119
120                 a = &in->object.attribute_ctr.attributes[i];
121                 d = &in->meta_data_ctr->meta_data[i];
122                 m = &md->ctr.ctr1.array[i];
123                 e = &msg->elements[i];
124
125                 for (j=0; j<a->value_ctr.num_values; j++) {
126                         status = drsuapi_decrypt_attribute(a->value_ctr.values[j].blob, gensec_skey, rid, a);
127                         W_ERROR_NOT_OK_RETURN(status);
128                 }
129
130                 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, a, msg->elements, e);
131                 W_ERROR_NOT_OK_RETURN(status);
132
133                 m->attid                        = a->attid;
134                 m->version                      = d->version;
135                 m->originating_change_time      = d->originating_change_time;
136                 m->originating_invocation_id    = d->originating_invocation_id;
137                 m->originating_usn              = d->originating_usn;
138                 m->local_usn                    = 0;
139
140                 if (d->originating_change_time > whenChanged) {
141                         whenChanged = d->originating_change_time;
142                 }
143
144                 if (a->attid == DRSUAPI_ATTID_name) {
145                         name_a = a;
146                         name_d = d;
147                         rdn_m = &md->ctr.ctr1.array[md->ctr.ctr1.count];
148                 }
149         }
150
151         if (rdn_m) {
152                 struct ldb_message_element *el;
153                 el = ldb_msg_find_element(msg, rdn_attr->lDAPDisplayName);
154                 if (!el) {
155                         ret = ldb_msg_add_value(msg, rdn_attr->lDAPDisplayName, rdn_value, NULL);
156                         if (ret != LDB_SUCCESS) {
157                                 return WERR_FOOBAR;
158                         }
159                 } else {
160                         if (el->num_values != 1) {
161                                 DEBUG(0,(__location__ ": Unexpected num_values=%u\n",
162                                          el->num_values));
163                                 return WERR_FOOBAR;                             
164                         }
165                         if (!ldb_val_equal_exact(&el->values[0], rdn_value)) {
166                                 DEBUG(0,(__location__ ": RDN value changed? '%*.*s' '%*.*s'\n",
167                                          (int)el->values[0].length, (int)el->values[0].length, el->values[0].data,
168                                          (int)rdn_value->length, (int)rdn_value->length, rdn_value->data));
169                                 return WERR_FOOBAR;                             
170                         }
171                 }
172
173                 rdn_m->attid                            = rdn_attid;
174                 rdn_m->version                          = name_d->version;
175                 rdn_m->originating_change_time          = name_d->originating_change_time;
176                 rdn_m->originating_invocation_id        = name_d->originating_invocation_id;
177                 rdn_m->originating_usn                  = name_d->originating_usn;
178                 rdn_m->local_usn                        = 0;
179                 md->ctr.ctr1.count++;
180
181         }
182
183         whenChanged_t = nt_time_to_unix(whenChanged);
184         whenChanged_s = ldb_timestring(msg, whenChanged_t);
185         W_ERROR_HAVE_NO_MEMORY(whenChanged_s);
186
187         nt_status = GUID_to_ndr_blob(&in->object.identifier->guid, msg, &guid_value);
188         if (!NT_STATUS_IS_OK(nt_status)) {
189                 return ntstatus_to_werror(nt_status);
190         }
191
192         out->msg                = msg;
193         out->guid_value         = guid_value;
194         out->when_changed       = whenChanged_s;
195         out->meta_data          = md;
196         return WERR_OK;
197 }
198
199 WERROR dsdb_extended_replicated_objects_convert(struct ldb_context *ldb,
200                                                 const char *partition_dn_str,
201                                                 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
202                                                 uint32_t object_count,
203                                                 const struct drsuapi_DsReplicaObjectListItemEx *first_object,
204                                                 uint32_t linked_attributes_count,
205                                                 const struct drsuapi_DsReplicaLinkedAttribute *linked_attributes,
206                                                 const struct repsFromTo1 *source_dsa,
207                                                 const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector,
208                                                 const DATA_BLOB *gensec_skey,
209                                                 TALLOC_CTX *mem_ctx,
210                                                 struct dsdb_extended_replicated_objects **objects)
211 {
212         WERROR status;
213         struct ldb_dn *partition_dn;
214         const struct dsdb_schema *schema;
215         struct dsdb_extended_replicated_objects *out;
216         const struct drsuapi_DsReplicaObjectListItemEx *cur;
217         uint32_t i;
218
219         out = talloc_zero(mem_ctx, struct dsdb_extended_replicated_objects);
220         W_ERROR_HAVE_NO_MEMORY(out);
221         out->version            = DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION;
222
223         /* Get the schema, and ensure it's kept valid for as long as 'out' which may contain pointers to it */
224         schema = dsdb_get_schema(ldb, out);
225         if (!schema) {
226                 talloc_free(out);
227                 return WERR_DS_SCHEMA_NOT_LOADED;
228         }
229
230         partition_dn = ldb_dn_new(out, ldb, partition_dn_str);
231         W_ERROR_HAVE_NO_MEMORY_AND_FREE(partition_dn, out);
232
233         if (ldb_dn_compare(partition_dn, ldb_get_schema_basedn(ldb)) != 0) {
234                 /*
235                  * check for schema changes in case
236                  * we are not replicating Schema NC
237                  */
238                 status = dsdb_schema_info_cmp(schema, mapping_ctr);
239                 if (!W_ERROR_IS_OK(status)) {
240                         DEBUG(1,("Remote schema has changed while replicating %s\n",
241                                  partition_dn_str));
242                         talloc_free(out);
243                         return status;
244                 }
245         }
246
247         out->partition_dn       = partition_dn;
248
249         out->source_dsa         = source_dsa;
250         out->uptodateness_vector= uptodateness_vector;
251
252         out->num_objects        = object_count;
253         out->objects            = talloc_array(out,
254                                                struct dsdb_extended_replicated_object,
255                                                out->num_objects);
256         W_ERROR_HAVE_NO_MEMORY_AND_FREE(out->objects, out);
257
258         /* pass the linked attributes down to the repl_meta_data
259            module */
260         out->linked_attributes_count = linked_attributes_count;
261         out->linked_attributes       = linked_attributes;
262
263         for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
264                 if (i == out->num_objects) {
265                         talloc_free(out);
266                         return WERR_FOOBAR;
267                 }
268
269                 status = dsdb_convert_object_ex(ldb, schema,
270                                                 cur, gensec_skey,
271                                                 out->objects, &out->objects[i]);
272                 if (!W_ERROR_IS_OK(status)) {
273                         talloc_free(out);
274                         DEBUG(0,("Failed to convert object %s: %s\n",
275                                  cur->object.identifier->dn,
276                                  win_errstr(status)));
277                         return status;
278                 }
279         }
280         if (i != out->num_objects) {
281                 talloc_free(out);
282                 return WERR_FOOBAR;
283         }
284
285         *objects = out;
286         return WERR_OK;
287 }
288
289 WERROR dsdb_extended_replicated_objects_commit(struct ldb_context *ldb,
290                                                struct dsdb_extended_replicated_objects *objects,
291                                                uint64_t *notify_uSN)
292 {
293         struct ldb_result *ext_res;
294         int ret;
295         uint64_t seq_num1, seq_num2;
296
297         /* TODO: handle linked attributes */
298
299         /* wrap the extended operation in a transaction 
300            See [MS-DRSR] 3.3.2 Transactions
301          */
302         ret = ldb_transaction_start(ldb);
303         if (ret != LDB_SUCCESS) {
304                 DEBUG(0,(__location__ " Failed to start transaction\n"));
305                 return WERR_FOOBAR;
306         }
307
308         ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num1, NULL);
309         if (ret != LDB_SUCCESS) {
310                 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
311                 ldb_transaction_cancel(ldb);
312                 return WERR_FOOBAR;             
313         }
314
315         ret = ldb_extended(ldb, DSDB_EXTENDED_REPLICATED_OBJECTS_OID, objects, &ext_res);
316         if (ret != LDB_SUCCESS) {
317                 DEBUG(0,("Failed to apply records: %s: %s\n",
318                          ldb_errstring(ldb), ldb_strerror(ret)));
319                 ldb_transaction_cancel(ldb);
320                 return WERR_FOOBAR;
321         }
322         talloc_free(ext_res);
323
324         ret = ldb_transaction_prepare_commit(ldb);
325         if (ret != LDB_SUCCESS) {
326                 DEBUG(0,(__location__ " Failed to prepare commit of transaction: %s\n",
327                          ldb_errstring(ldb)));
328                 return WERR_FOOBAR;
329         }
330
331         ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num2, NULL);
332         if (ret != LDB_SUCCESS) {
333                 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
334                 ldb_transaction_cancel(ldb);
335                 return WERR_FOOBAR;             
336         }
337
338         /* if this replication partner didn't need to be notified
339            before this transaction then it still doesn't need to be
340            notified, as the changes came from this server */    
341         if (seq_num2 > seq_num1 && seq_num1 <= *notify_uSN) {
342                 *notify_uSN = seq_num2;
343         }
344
345         ret = ldb_transaction_commit(ldb);
346         if (ret != LDB_SUCCESS) {
347                 DEBUG(0,(__location__ " Failed to commit transaction\n"));
348                 return WERR_FOOBAR;
349         }
350
351
352         DEBUG(2,("Replicated %u objects (%u linked attributes) for %s\n",
353                  objects->num_objects, objects->linked_attributes_count,
354                  ldb_dn_get_linearized(objects->partition_dn)));
355                  
356         return WERR_OK;
357 }
358
359 static WERROR dsdb_convert_object(struct ldb_context *ldb,
360                                   const struct dsdb_schema *schema,
361                                   const struct drsuapi_DsReplicaObjectListItem *in,
362                                   TALLOC_CTX *mem_ctx,
363                                   struct ldb_message **_msg)
364 {
365         WERROR status;
366         unsigned int i;
367         struct ldb_message *msg;
368
369         if (!in->object.identifier) {
370                 return WERR_FOOBAR;
371         }
372
373         if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
374                 return WERR_FOOBAR;
375         }
376
377         msg = ldb_msg_new(mem_ctx);
378         W_ERROR_HAVE_NO_MEMORY(msg);
379
380         msg->dn = ldb_dn_new(msg, ldb, in->object.identifier->dn);
381         W_ERROR_HAVE_NO_MEMORY(msg->dn);
382
383         msg->num_elements       = in->object.attribute_ctr.num_attributes;
384         msg->elements           = talloc_array(msg, struct ldb_message_element,
385                                                msg->num_elements);
386         W_ERROR_HAVE_NO_MEMORY(msg->elements);
387
388         for (i=0; i < msg->num_elements; i++) {
389                 struct drsuapi_DsReplicaAttribute *a;
390                 struct ldb_message_element *e;
391
392                 a = &in->object.attribute_ctr.attributes[i];
393                 e = &msg->elements[i];
394
395                 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, a, msg->elements, e);
396                 W_ERROR_NOT_OK_RETURN(status);
397         }
398
399
400         *_msg = msg;
401
402         return WERR_OK;
403 }
404
405 WERROR dsdb_origin_objects_commit(struct ldb_context *ldb,
406                                   TALLOC_CTX *mem_ctx,
407                                   const struct drsuapi_DsReplicaObjectListItem *first_object,
408                                   uint32_t *_num,
409                                   struct drsuapi_DsReplicaObjectIdentifier2 **_ids)
410 {
411         WERROR status;
412         const struct dsdb_schema *schema;
413         const struct drsuapi_DsReplicaObjectListItem *cur;
414         struct ldb_message **objects;
415         struct drsuapi_DsReplicaObjectIdentifier2 *ids;
416         uint32_t i;
417         uint32_t num_objects = 0;
418         const char * const attrs[] = {
419                 "objectGUID",
420                 "objectSid",
421                 NULL
422         };
423         struct ldb_result *res;
424         int ret;
425
426         for (cur = first_object; cur; cur = cur->next_object) {
427                 num_objects++;
428         }
429
430         if (num_objects == 0) {
431                 return WERR_OK;
432         }
433
434         ret = ldb_transaction_start(ldb);
435         if (ret != LDB_SUCCESS) {
436                 return WERR_DS_INTERNAL_FAILURE;
437         }
438
439         objects = talloc_array(mem_ctx, struct ldb_message *,
440                                num_objects);
441         if (objects == NULL) {
442                 status = WERR_NOMEM;
443                 goto cancel;
444         }
445
446         schema = dsdb_get_schema(ldb, objects);
447         if (!schema) {
448                 return WERR_DS_SCHEMA_NOT_LOADED;
449         }
450
451         for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
452                 status = dsdb_convert_object(ldb, schema,
453                                              cur, objects, &objects[i]);
454                 if (!W_ERROR_IS_OK(status)) {
455                         goto cancel;
456                 }
457         }
458
459         ids = talloc_array(mem_ctx,
460                            struct drsuapi_DsReplicaObjectIdentifier2,
461                            num_objects);
462         if (ids == NULL) {
463                 status = WERR_NOMEM;
464                 goto cancel;
465         }
466
467         for (i=0; i < num_objects; i++) {
468                 struct dom_sid *sid = NULL;
469                 struct ldb_request *add_req;
470
471                 DEBUG(6,(__location__ ": adding %s\n", 
472                          ldb_dn_get_linearized(objects[i]->dn)));
473
474                 ret = ldb_build_add_req(&add_req,
475                                         ldb,
476                                         objects,
477                                         objects[i],
478                                         NULL,
479                                         NULL,
480                                         ldb_op_default_callback,
481                                         NULL);
482                 if (ret != LDB_SUCCESS) {
483                         status = WERR_DS_INTERNAL_FAILURE;
484                         goto cancel;
485                 }
486
487                 ret = ldb_request_add_control(add_req, LDB_CONTROL_RELAX_OID, true, NULL);
488                 if (ret != LDB_SUCCESS) {
489                         status = WERR_DS_INTERNAL_FAILURE;
490                         goto cancel;
491                 }
492                 
493                 ret = ldb_request(ldb, add_req);
494                 if (ret == LDB_SUCCESS) {
495                         ret = ldb_wait(add_req->handle, LDB_WAIT_ALL);
496                 }
497                 if (ret != LDB_SUCCESS) {
498                         DEBUG(0,(__location__ ": Failed add of %s - %s\n",
499                                  ldb_dn_get_linearized(objects[i]->dn), ldb_errstring(ldb)));
500                         status = WERR_DS_INTERNAL_FAILURE;
501                         goto cancel;
502                 }
503
504                 talloc_free(add_req);
505
506                 ret = ldb_search(ldb, objects, &res, objects[i]->dn,
507                                  LDB_SCOPE_BASE, attrs,
508                                  "(objectClass=*)");
509                 if (ret != LDB_SUCCESS) {
510                         status = WERR_DS_INTERNAL_FAILURE;
511                         goto cancel;
512                 }
513                 ids[i].guid = samdb_result_guid(res->msgs[0], "objectGUID");
514                 sid = samdb_result_dom_sid(objects, res->msgs[0], "objectSid");
515                 if (sid) {
516                         ids[i].sid = *sid;
517                 } else {
518                         ZERO_STRUCT(ids[i].sid);
519                 }
520         }
521
522         ret = ldb_transaction_commit(ldb);
523         if (ret != LDB_SUCCESS) {
524                 return WERR_DS_INTERNAL_FAILURE;
525         }
526
527         talloc_free(objects);
528
529         *_num = num_objects;
530         *_ids = ids;
531         return WERR_OK;
532
533 cancel:
534         talloc_free(objects);
535         ldb_transaction_cancel(ldb);
536         return status;
537 }