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/>.
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
,
39 struct dsdb_extended_replicated_object
*out
)
44 struct ldb_message
*msg
;
45 struct replPropertyMetaDataBlob
*md
;
46 struct ldb_val guid_value
;
47 NTTIME whenChanged
= 0;
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
;
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
;
61 if (!in
->object
.identifier
) {
65 if (!in
->object
.identifier
->dn
|| !in
->object
.identifier
->dn
[0]) {
69 if (in
->object
.attribute_ctr
.num_attributes
!= 0 && !in
->meta_data_ctr
) {
73 if (in
->object
.attribute_ctr
.num_attributes
!= in
->meta_data_ctr
->count
) {
77 sid
= &in
->object
.identifier
->sid
;
78 if (sid
->num_auths
> 0) {
79 rid
= sid
->sub_auths
[sid
->num_auths
- 1];
82 msg
= ldb_msg_new(mem_ctx
);
83 W_ERROR_HAVE_NO_MEMORY(msg
);
85 msg
->dn
= ldb_dn_new(msg
, ldb
, in
->object
.identifier
->dn
);
86 W_ERROR_HAVE_NO_MEMORY(msg
->dn
);
88 rdn_name
= ldb_dn_get_rdn_name(msg
->dn
);
89 rdn_attr
= dsdb_attribute_by_lDAPDisplayName(schema
, rdn_name
);
93 rdn_attid
= rdn_attr
->attributeID_id
;
94 rdn_value
= ldb_dn_get_rdn_val(msg
->dn
);
96 msg
->num_elements
= in
->object
.attribute_ctr
.num_attributes
;
97 msg
->elements
= talloc_array(msg
, struct ldb_message_element
,
99 W_ERROR_HAVE_NO_MEMORY(msg
->elements
);
101 md
= talloc(mem_ctx
, struct replPropertyMetaDataBlob
);
102 W_ERROR_HAVE_NO_MEMORY(md
);
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
);
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
;
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
];
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
);
130 status
= dsdb_attribute_drsuapi_to_ldb(ldb
, schema
, a
, msg
->elements
, e
);
131 W_ERROR_NOT_OK_RETURN(status
);
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
;
140 if (d
->originating_change_time
> whenChanged
) {
141 whenChanged
= d
->originating_change_time
;
144 if (a
->attid
== DRSUAPI_ATTRIBUTE_name
) {
147 rdn_m
= &md
->ctr
.ctr1
.array
[md
->ctr
.ctr1
.count
];
152 struct ldb_message_element
*el
;
153 el
= ldb_msg_find_element(msg
, rdn_attr
->lDAPDisplayName
);
155 ret
= ldb_msg_add_value(msg
, rdn_attr
->lDAPDisplayName
, rdn_value
, NULL
);
156 if (ret
!= LDB_SUCCESS
) {
160 if (el
->num_values
!= 1) {
161 DEBUG(0,(__location__
": Unexpected num_values=%u\n",
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
));
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
++;
183 whenChanged_t
= nt_time_to_unix(whenChanged
);
184 whenChanged_s
= ldb_timestring(msg
, whenChanged_t
);
185 W_ERROR_HAVE_NO_MEMORY(whenChanged_s
);
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
);
193 out
->guid_value
= guid_value
;
194 out
->when_changed
= whenChanged_s
;
199 WERROR
dsdb_extended_replicated_objects_convert(struct ldb_context
*ldb
,
200 const char *partition_dn
,
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
,
210 struct dsdb_extended_replicated_objects
**objects
)
213 const struct dsdb_schema
*schema
;
214 struct dsdb_extended_replicated_objects
*out
;
215 const struct drsuapi_DsReplicaObjectListItemEx
*cur
;
218 schema
= dsdb_get_schema(ldb
);
220 return WERR_DS_SCHEMA_NOT_LOADED
;
223 status
= dsdb_schema_pfm_contains_drsuapi_pfm(schema
->prefixmap
, mapping_ctr
);
224 W_ERROR_NOT_OK_RETURN(status
);
226 out
= talloc_zero(mem_ctx
, struct dsdb_extended_replicated_objects
);
227 W_ERROR_HAVE_NO_MEMORY(out
);
228 out
->version
= DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION
;
230 out
->partition_dn
= ldb_dn_new(out
, ldb
, partition_dn
);
231 W_ERROR_HAVE_NO_MEMORY(out
->partition_dn
);
233 out
->source_dsa
= source_dsa
;
234 out
->uptodateness_vector
= uptodateness_vector
;
236 out
->num_objects
= object_count
;
237 out
->objects
= talloc_array(out
,
238 struct dsdb_extended_replicated_object
,
240 W_ERROR_HAVE_NO_MEMORY(out
->objects
);
242 /* pass the linked attributes down to the repl_meta_data
244 out
->linked_attributes_count
= linked_attributes_count
;
245 out
->linked_attributes
= linked_attributes
;
247 for (i
=0, cur
= first_object
; cur
; cur
= cur
->next_object
, i
++) {
248 if (i
== out
->num_objects
) {
252 status
= dsdb_convert_object_ex(ldb
, schema
,
254 out
->objects
, &out
->objects
[i
]);
255 if (!W_ERROR_IS_OK(status
)) {
256 DEBUG(0,("Failed to convert object %s\n", cur
->object
.identifier
->dn
));
260 if (i
!= out
->num_objects
) {
268 WERROR
dsdb_extended_replicated_objects_commit(struct ldb_context
*ldb
,
269 struct dsdb_extended_replicated_objects
*objects
,
270 uint64_t *notify_uSN
)
272 struct ldb_result
*ext_res
;
274 uint64_t seq_num1
, seq_num2
;
276 /* TODO: handle linked attributes */
278 /* wrap the extended operation in a transaction
279 See [MS-DRSR] 3.3.2 Transactions
281 ret
= ldb_transaction_start(ldb
);
282 if (ret
!= LDB_SUCCESS
) {
283 DEBUG(0,(__location__
" Failed to start transaction\n"));
287 ret
= dsdb_load_partition_usn(ldb
, objects
->partition_dn
, &seq_num1
, NULL
);
288 if (ret
!= LDB_SUCCESS
) {
289 DEBUG(0,(__location__
" Failed to load partition uSN\n"));
290 ldb_transaction_cancel(ldb
);
294 ret
= ldb_extended(ldb
, DSDB_EXTENDED_REPLICATED_OBJECTS_OID
, objects
, &ext_res
);
295 if (ret
!= LDB_SUCCESS
) {
296 DEBUG(0,("Failed to apply records: %s: %s\n",
297 ldb_errstring(ldb
), ldb_strerror(ret
)));
298 ldb_transaction_cancel(ldb
);
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: %s\n",
306 ldb_errstring(ldb
)));
310 ret
= dsdb_load_partition_usn(ldb
, objects
->partition_dn
, &seq_num2
, NULL
);
311 if (ret
!= LDB_SUCCESS
) {
312 DEBUG(0,(__location__
" Failed to load partition uSN\n"));
313 ldb_transaction_cancel(ldb
);
317 /* if this replication partner didn't need to be notified
318 before this transaction then it still doesn't need to be
319 notified, as the changes came from this server */
320 if (seq_num2
> seq_num1
&& seq_num1
<= *notify_uSN
) {
321 *notify_uSN
= seq_num2
;
324 ret
= ldb_transaction_commit(ldb
);
325 if (ret
!= LDB_SUCCESS
) {
326 DEBUG(0,(__location__
" Failed to commit transaction\n"));
331 DEBUG(2,("Replicated %u objects (%u linked attributes) for %s\n",
332 objects
->num_objects
, objects
->linked_attributes_count
,
333 ldb_dn_get_linearized(objects
->partition_dn
)));
338 static WERROR
dsdb_convert_object(struct ldb_context
*ldb
,
339 const struct dsdb_schema
*schema
,
340 const struct drsuapi_DsReplicaObjectListItem
*in
,
342 struct ldb_message
**_msg
)
346 struct ldb_message
*msg
;
348 if (!in
->object
.identifier
) {
352 if (!in
->object
.identifier
->dn
|| !in
->object
.identifier
->dn
[0]) {
356 msg
= ldb_msg_new(mem_ctx
);
357 W_ERROR_HAVE_NO_MEMORY(msg
);
359 msg
->dn
= ldb_dn_new(msg
, ldb
, in
->object
.identifier
->dn
);
360 W_ERROR_HAVE_NO_MEMORY(msg
->dn
);
362 msg
->num_elements
= in
->object
.attribute_ctr
.num_attributes
;
363 msg
->elements
= talloc_array(msg
, struct ldb_message_element
,
365 W_ERROR_HAVE_NO_MEMORY(msg
->elements
);
367 for (i
=0; i
< msg
->num_elements
; i
++) {
368 struct drsuapi_DsReplicaAttribute
*a
;
369 struct ldb_message_element
*e
;
371 a
= &in
->object
.attribute_ctr
.attributes
[i
];
372 e
= &msg
->elements
[i
];
374 status
= dsdb_attribute_drsuapi_to_ldb(ldb
, schema
, a
, msg
->elements
, e
);
375 W_ERROR_NOT_OK_RETURN(status
);
384 WERROR
dsdb_origin_objects_commit(struct ldb_context
*ldb
,
386 const struct drsuapi_DsReplicaObjectListItem
*first_object
,
388 struct drsuapi_DsReplicaObjectIdentifier2
**_ids
)
391 const struct dsdb_schema
*schema
;
392 const struct drsuapi_DsReplicaObjectListItem
*cur
;
393 struct ldb_message
**objects
;
394 struct drsuapi_DsReplicaObjectIdentifier2
*ids
;
396 uint32_t num_objects
= 0;
397 const char * const attrs
[] = {
402 struct ldb_result
*res
;
405 schema
= dsdb_get_schema(ldb
);
407 return WERR_DS_SCHEMA_NOT_LOADED
;
410 for (cur
= first_object
; cur
; cur
= cur
->next_object
) {
414 if (num_objects
== 0) {
418 ret
= ldb_transaction_start(ldb
);
419 if (ret
!= LDB_SUCCESS
) {
420 return WERR_DS_INTERNAL_FAILURE
;
423 objects
= talloc_array(mem_ctx
, struct ldb_message
*,
425 if (objects
== NULL
) {
430 for (i
=0, cur
= first_object
; cur
; cur
= cur
->next_object
, i
++) {
431 status
= dsdb_convert_object(ldb
, schema
,
432 cur
, objects
, &objects
[i
]);
433 if (!W_ERROR_IS_OK(status
)) {
438 ids
= talloc_array(mem_ctx
,
439 struct drsuapi_DsReplicaObjectIdentifier2
,
446 for (i
=0; i
< num_objects
; i
++) {
447 struct dom_sid
*sid
= NULL
;
448 struct ldb_request
*add_req
;
450 DEBUG(6,(__location__
": adding %s\n",
451 ldb_dn_get_linearized(objects
[i
]->dn
)));
453 ret
= ldb_build_add_req(&add_req
,
459 ldb_op_default_callback
,
461 if (ret
!= LDB_SUCCESS
) {
462 status
= WERR_DS_INTERNAL_FAILURE
;
466 ret
= ldb_request_add_control(add_req
, LDB_CONTROL_RELAX_OID
, true, NULL
);
467 if (ret
!= LDB_SUCCESS
) {
468 status
= WERR_DS_INTERNAL_FAILURE
;
472 ret
= ldb_request(ldb
, add_req
);
473 if (ret
== LDB_SUCCESS
) {
474 ret
= ldb_wait(add_req
->handle
, LDB_WAIT_ALL
);
476 if (ret
!= LDB_SUCCESS
) {
477 DEBUG(0,(__location__
": Failed add of %s - %s\n",
478 ldb_dn_get_linearized(objects
[i
]->dn
), ldb_errstring(ldb
)));
479 status
= WERR_DS_INTERNAL_FAILURE
;
483 talloc_free(add_req
);
485 ret
= ldb_search(ldb
, objects
, &res
, objects
[i
]->dn
,
486 LDB_SCOPE_BASE
, attrs
,
488 if (ret
!= LDB_SUCCESS
) {
489 status
= WERR_DS_INTERNAL_FAILURE
;
492 ids
[i
].guid
= samdb_result_guid(res
->msgs
[0], "objectGUID");
493 sid
= samdb_result_dom_sid(objects
, res
->msgs
[0], "objectSid");
497 ZERO_STRUCT(ids
[i
].sid
);
501 ret
= ldb_transaction_commit(ldb
);
502 if (ret
!= LDB_SUCCESS
) {
503 return WERR_DS_INTERNAL_FAILURE
;
506 talloc_free(objects
);
513 talloc_free(objects
);
514 ldb_transaction_cancel(ldb
);