s4-dsdb: use GUID_to_ndr_blob()
[obnox/samba/samba-obnox.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         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                 int 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                 if (!NT_STATUS_IS_OK(status) && a->value_ctr.num_values == 0) {
132                         /* w2k8-r2 occasionally sends bogus empty
133                            attributes with rubbish attribute IDs. The
134                            only think we can do is discard these */
135                         DEBUG(0,(__location__ ": Discarding bogus empty DsReplicaAttribute with attid 0x%x\n",
136                                  a->attid));
137                         ZERO_STRUCTP(e);
138                         continue;
139                 }
140                 W_ERROR_NOT_OK_RETURN(status);
141
142                 m->attid                        = a->attid;
143                 m->version                      = d->version;
144                 m->originating_change_time      = d->originating_change_time;
145                 m->originating_invocation_id    = d->originating_invocation_id;
146                 m->originating_usn              = d->originating_usn;
147                 m->local_usn                    = 0;
148
149                 if (d->originating_change_time > whenChanged) {
150                         whenChanged = d->originating_change_time;
151                 }
152
153                 if (a->attid == DRSUAPI_ATTRIBUTE_name) {
154                         name_a = a;
155                         name_d = d;
156                         rdn_m = &md->ctr.ctr1.array[md->ctr.ctr1.count];
157                 }
158         }
159
160         /* delete any empty elements */
161         for (i=0; i < msg->num_elements; i++) {
162                 if (msg->elements[i].name == NULL) {
163                         ldb_msg_remove_element(msg, &msg->elements[i]);
164                         i--;
165                 }
166         }
167
168         if (rdn_m) {
169                 struct ldb_message_element *el;
170                 el = ldb_msg_find_element(msg, rdn_attr->lDAPDisplayName);
171                 if (!el) {
172                         ret = ldb_msg_add_value(msg, rdn_attr->lDAPDisplayName, rdn_value, NULL);
173                         if (ret != LDB_SUCCESS) {
174                                 return WERR_FOOBAR;
175                         }
176                 } else {
177                         if (el->num_values != 1) {
178                                 DEBUG(0,(__location__ ": Unexpected num_values=%u\n",
179                                          el->num_values));
180                                 return WERR_FOOBAR;                             
181                         }
182                         if (!ldb_val_equal_exact(&el->values[0], rdn_value)) {
183                                 DEBUG(0,(__location__ ": RDN value changed? '%*.*s' '%*.*s'\n",
184                                          (int)el->values[0].length, (int)el->values[0].length, el->values[0].data,
185                                          (int)rdn_value->length, (int)rdn_value->length, rdn_value->data));
186                                 return WERR_FOOBAR;                             
187                         }
188                 }
189
190                 rdn_m->attid                            = rdn_attid;
191                 rdn_m->version                          = name_d->version;
192                 rdn_m->originating_change_time          = name_d->originating_change_time;
193                 rdn_m->originating_invocation_id        = name_d->originating_invocation_id;
194                 rdn_m->originating_usn                  = name_d->originating_usn;
195                 rdn_m->local_usn                        = 0;
196                 md->ctr.ctr1.count++;
197
198         }
199
200         whenChanged_t = nt_time_to_unix(whenChanged);
201         whenChanged_s = ldb_timestring(msg, whenChanged_t);
202         W_ERROR_HAVE_NO_MEMORY(whenChanged_s);
203
204         nt_status = GUID_to_ndr_blob(&in->object.identifier->guid, msg, &guid_value);
205         if (!NT_STATUS_IS_OK(nt_status)) {
206                 return ntstatus_to_werror(nt_status);
207         }
208
209         out->msg                = msg;
210         out->guid_value         = guid_value;
211         out->when_changed       = whenChanged_s;
212         out->meta_data          = md;
213         return WERR_OK;
214 }
215
216 WERROR dsdb_extended_replicated_objects_convert(struct ldb_context *ldb,
217                                                 const char *partition_dn,
218                                                 const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
219                                                 uint32_t object_count,
220                                                 const struct drsuapi_DsReplicaObjectListItemEx *first_object,
221                                                 uint32_t linked_attributes_count,
222                                                 const struct drsuapi_DsReplicaLinkedAttribute *linked_attributes,
223                                                 const struct repsFromTo1 *source_dsa,
224                                                 const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector,
225                                                 const DATA_BLOB *gensec_skey,
226                                                 TALLOC_CTX *mem_ctx,
227                                                 struct dsdb_extended_replicated_objects **objects)
228 {
229         WERROR status;
230         const struct dsdb_schema *schema;
231         struct dsdb_extended_replicated_objects *out;
232         const struct drsuapi_DsReplicaObjectListItemEx *cur;
233         uint32_t i;
234
235         schema = dsdb_get_schema(ldb);
236         if (!schema) {
237                 return WERR_DS_SCHEMA_NOT_LOADED;
238         }
239
240         status = dsdb_schema_pfm_contains_drsuapi_pfm(schema->prefixmap, mapping_ctr);
241         W_ERROR_NOT_OK_RETURN(status);
242
243         out = talloc_zero(mem_ctx, struct dsdb_extended_replicated_objects);
244         W_ERROR_HAVE_NO_MEMORY(out);
245         out->version            = DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION;
246
247         out->partition_dn       = ldb_dn_new(out, ldb, partition_dn);
248         W_ERROR_HAVE_NO_MEMORY(out->partition_dn);
249
250         out->source_dsa         = source_dsa;
251         out->uptodateness_vector= uptodateness_vector;
252
253         out->num_objects        = object_count;
254         out->objects            = talloc_array(out,
255                                                struct dsdb_extended_replicated_object,
256                                                out->num_objects);
257         W_ERROR_HAVE_NO_MEMORY(out->objects);
258
259         /* pass the linked attributes down to the repl_meta_data
260            module */
261         out->linked_attributes_count = linked_attributes_count;
262         out->linked_attributes       = linked_attributes;
263
264         for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
265                 if (i == out->num_objects) {
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                         DEBUG(0,("Failed to convert object %s\n", cur->object.identifier->dn));
274                         return status;
275                 }
276         }
277         if (i != out->num_objects) {
278                 return WERR_FOOBAR;
279         }
280
281         *objects = out;
282         return WERR_OK;
283 }
284
285 WERROR dsdb_extended_replicated_objects_commit(struct ldb_context *ldb,
286                                                struct dsdb_extended_replicated_objects *objects,
287                                                uint64_t *notify_uSN)
288 {
289         struct ldb_result *ext_res;
290         int ret;
291         uint64_t seq_num1, seq_num2;
292
293         /* TODO: handle linked attributes */
294
295         /* wrap the extended operation in a transaction 
296            See [MS-DRSR] 3.3.2 Transactions
297          */
298         ret = ldb_transaction_start(ldb);
299         if (ret != LDB_SUCCESS) {
300                 DEBUG(0,(__location__ " Failed to start transaction\n"));
301                 return WERR_FOOBAR;
302         }
303
304         ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num1);
305         if (ret != LDB_SUCCESS) {
306                 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
307                 ldb_transaction_cancel(ldb);
308                 return WERR_FOOBAR;             
309         }
310
311         ret = ldb_extended(ldb, DSDB_EXTENDED_REPLICATED_OBJECTS_OID, objects, &ext_res);
312         if (ret != LDB_SUCCESS) {
313                 DEBUG(0,("Failed to apply records: %s: %s\n",
314                          ldb_errstring(ldb), ldb_strerror(ret)));
315                 ldb_transaction_cancel(ldb);
316                 return WERR_FOOBAR;
317         }
318         talloc_free(ext_res);
319
320         ret = ldb_transaction_prepare_commit(ldb);
321         if (ret != LDB_SUCCESS) {
322                 DEBUG(0,(__location__ " Failed to prepare commit of transaction\n"));
323                 return WERR_FOOBAR;
324         }
325
326         ret = dsdb_load_partition_usn(ldb, objects->partition_dn, &seq_num2);
327         if (ret != LDB_SUCCESS) {
328                 DEBUG(0,(__location__ " Failed to load partition uSN\n"));
329                 ldb_transaction_cancel(ldb);
330                 return WERR_FOOBAR;             
331         }
332
333         /* if this replication partner didn't need to be notified
334            before this transaction then it still doesn't need to be
335            notified, as the changes came from this server */    
336         if (seq_num2 > seq_num1 && seq_num1 <= *notify_uSN) {
337                 *notify_uSN = seq_num2;
338         }
339
340         ret = ldb_transaction_commit(ldb);
341         if (ret != LDB_SUCCESS) {
342                 DEBUG(0,(__location__ " Failed to commit transaction\n"));
343                 return WERR_FOOBAR;
344         }
345
346
347         DEBUG(2,("Replicated %u objects (%u linked attributes) for %s\n",
348                  objects->num_objects, objects->linked_attributes_count,
349                  ldb_dn_get_linearized(objects->partition_dn)));
350                  
351         return WERR_OK;
352 }
353
354 static WERROR dsdb_convert_object(struct ldb_context *ldb,
355                                   const struct dsdb_schema *schema,
356                                   const struct drsuapi_DsReplicaObjectListItem *in,
357                                   TALLOC_CTX *mem_ctx,
358                                   struct ldb_message **_msg)
359 {
360         WERROR status;
361         uint32_t i;
362         struct ldb_message *msg;
363
364         if (!in->object.identifier) {
365                 return WERR_FOOBAR;
366         }
367
368         if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
369                 return WERR_FOOBAR;
370         }
371
372         msg = ldb_msg_new(mem_ctx);
373         W_ERROR_HAVE_NO_MEMORY(msg);
374
375         msg->dn = ldb_dn_new(msg, ldb, in->object.identifier->dn);
376         W_ERROR_HAVE_NO_MEMORY(msg->dn);
377
378         msg->num_elements       = in->object.attribute_ctr.num_attributes;
379         msg->elements           = talloc_array(msg, struct ldb_message_element,
380                                                msg->num_elements);
381         W_ERROR_HAVE_NO_MEMORY(msg->elements);
382
383         for (i=0; i < msg->num_elements; i++) {
384                 struct drsuapi_DsReplicaAttribute *a;
385                 struct ldb_message_element *e;
386
387                 a = &in->object.attribute_ctr.attributes[i];
388                 e = &msg->elements[i];
389
390                 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, a, msg->elements, e);
391                 W_ERROR_NOT_OK_RETURN(status);
392         }
393
394
395         *_msg = msg;
396
397         return WERR_OK;
398 }
399
400 WERROR dsdb_origin_objects_commit(struct ldb_context *ldb,
401                                   TALLOC_CTX *mem_ctx,
402                                   const struct drsuapi_DsReplicaObjectListItem *first_object,
403                                   uint32_t *_num,
404                                   struct drsuapi_DsReplicaObjectIdentifier2 **_ids)
405 {
406         WERROR status;
407         const struct dsdb_schema *schema;
408         const struct drsuapi_DsReplicaObjectListItem *cur;
409         struct ldb_message **objects;
410         struct drsuapi_DsReplicaObjectIdentifier2 *ids;
411         uint32_t i;
412         uint32_t num_objects = 0;
413         const char * const attrs[] = {
414                 "objectGUID",
415                 "objectSid",
416                 NULL
417         };
418         struct ldb_result *res;
419         int ret;
420
421         schema = dsdb_get_schema(ldb);
422         if (!schema) {
423                 return WERR_DS_SCHEMA_NOT_LOADED;
424         }
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         for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
447                 status = dsdb_convert_object(ldb, schema,
448                                              cur, objects, &objects[i]);
449                 if (!W_ERROR_IS_OK(status)) {
450                         goto cancel;
451                 }
452         }
453
454         ids = talloc_array(mem_ctx,
455                            struct drsuapi_DsReplicaObjectIdentifier2,
456                            num_objects);
457         if (ids == NULL) {
458                 status = WERR_NOMEM;
459                 goto cancel;
460         }
461
462         for (i=0; i < num_objects; i++) {
463                 struct dom_sid *sid = NULL;
464                 struct ldb_request *add_req;
465
466                 DEBUG(6,(__location__ ": adding %s\n", 
467                          ldb_dn_get_linearized(objects[i]->dn)));
468
469                 ret = ldb_build_add_req(&add_req,
470                                         ldb,
471                                         objects,
472                                         objects[i],
473                                         NULL,
474                                         NULL,
475                                         ldb_op_default_callback,
476                                         NULL);
477                 if (ret != LDB_SUCCESS) {
478                         status = WERR_DS_INTERNAL_FAILURE;
479                         goto cancel;
480                 }
481
482                 ret = ldb_request_add_control(add_req, LDB_CONTROL_RELAX_OID, true, NULL);
483                 if (ret != LDB_SUCCESS) {
484                         status = WERR_DS_INTERNAL_FAILURE;
485                         goto cancel;
486                 }
487                 
488                 ret = ldb_request(ldb, add_req);
489                 if (ret == LDB_SUCCESS) {
490                         ret = ldb_wait(add_req->handle, LDB_WAIT_ALL);
491                 }
492                 if (ret != LDB_SUCCESS) {
493                         DEBUG(0,(__location__ ": Failed add of %s - %s\n",
494                                  ldb_dn_get_linearized(objects[i]->dn), ldb_errstring(ldb)));
495                         status = WERR_DS_INTERNAL_FAILURE;
496                         goto cancel;
497                 }
498
499                 talloc_free(add_req);
500
501                 ret = ldb_search(ldb, objects, &res, objects[i]->dn,
502                                  LDB_SCOPE_BASE, attrs,
503                                  "(objectClass=*)");
504                 if (ret != LDB_SUCCESS) {
505                         status = WERR_DS_INTERNAL_FAILURE;
506                         goto cancel;
507                 }
508                 ids[i].guid = samdb_result_guid(res->msgs[0], "objectGUID");
509                 sid = samdb_result_dom_sid(objects, res->msgs[0], "objectSid");
510                 if (sid) {
511                         ids[i].sid = *sid;
512                 } else {
513                         ZERO_STRUCT(ids[i].sid);
514                 }
515         }
516
517         ret = ldb_transaction_commit(ldb);
518         if (ret != LDB_SUCCESS) {
519                 return WERR_DS_INTERNAL_FAILURE;
520         }
521
522         talloc_free(objects);
523
524         *_num = num_objects;
525         *_ids = ids;
526         return WERR_OK;
527
528 cancel:
529         talloc_free(objects);
530         ldb_transaction_cancel(ldb);
531         return status;
532 }