r21839: add my email address
[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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20    
21 */
22
23 #include "includes.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "lib/ldb/include/ldb_errors.h"
26 #include "lib/util/dlinklist.h"
27 #include "librpc/gen_ndr/ndr_misc.h"
28 #include "librpc/gen_ndr/ndr_drsuapi.h"
29 #include "librpc/gen_ndr/ndr_drsblobs.h"
30 #include "lib/crypto/crypto.h"
31 #include "libcli/auth/libcli_auth.h"
32
33 static WERROR dsdb_decrypt_attribute_value(TALLOC_CTX *mem_ctx,
34                                            const DATA_BLOB *gensec_skey,
35                                            bool rid_crypt,
36                                            uint32_t rid,
37                                            DATA_BLOB *in,
38                                            DATA_BLOB *out)
39 {
40         DATA_BLOB confounder;
41         DATA_BLOB enc_buffer;
42
43         struct MD5Context md5;
44         uint8_t _enc_key[16];
45         DATA_BLOB enc_key;
46
47         DATA_BLOB dec_buffer;
48
49         uint32_t crc32_given;
50         uint32_t crc32_calc;
51         DATA_BLOB checked_buffer;
52
53         DATA_BLOB plain_buffer;
54
55         /*
56          * users with rid == 0 should not exist
57          */
58         if (rid_crypt && rid == 0) {
59                 return WERR_DS_DRA_INVALID_PARAMETER;
60         }
61
62         /* 
63          * the first 16 bytes at the beginning are the confounder
64          * followed by the 4 byte crc32 checksum
65          */
66         if (in->length < 20) {
67                 return WERR_DS_DRA_INVALID_PARAMETER;
68         }
69         confounder = data_blob_const(in->data, 16);
70         enc_buffer = data_blob_const(in->data + 16, in->length - 16);
71
72         /* 
73          * build the encryption key md5 over the session key followed
74          * by the confounder
75          * 
76          * here the gensec session key is used and
77          * not the dcerpc ncacn_ip_tcp "SystemLibraryDTC" key!
78          */
79         enc_key = data_blob_const(_enc_key, sizeof(_enc_key));
80         MD5Init(&md5);
81         MD5Update(&md5, gensec_skey->data, gensec_skey->length);
82         MD5Update(&md5, confounder.data, confounder.length);
83         MD5Final(enc_key.data, &md5);
84
85         /*
86          * copy the encrypted buffer part and 
87          * decrypt it using the created encryption key using arcfour
88          */
89         dec_buffer = data_blob_const(enc_buffer.data, enc_buffer.length);
90         arcfour_crypt_blob(dec_buffer.data, dec_buffer.length, &enc_key);
91
92         /* 
93          * the first 4 byte are the crc32 checksum
94          * of the remaining bytes
95          */
96         crc32_given = IVAL(dec_buffer.data, 0);
97         crc32_calc = crc32_calc_buffer(dec_buffer.data + 4 , dec_buffer.length - 4);
98         if (crc32_given != crc32_calc) {
99                 return WERR_SEC_E_DECRYPT_FAILURE;
100         }
101         checked_buffer = data_blob_const(dec_buffer.data + 4, dec_buffer.length - 4);
102
103         plain_buffer = data_blob_talloc(mem_ctx, checked_buffer.data, checked_buffer.length);
104         W_ERROR_HAVE_NO_MEMORY(plain_buffer.data);
105
106         if (rid_crypt) {
107                 uint32_t i, num_hashes;
108
109                 if ((checked_buffer.length % 16) != 0) {
110                         return WERR_DS_DRA_INVALID_PARAMETER;
111                 }
112
113                 num_hashes = plain_buffer.length / 16;
114                 for (i = 0; i < num_hashes; i++) {
115                         uint32_t offset = i * 16;
116                         sam_rid_crypt(rid, checked_buffer.data + offset, plain_buffer.data + offset, 0);
117                 }
118         }
119
120         *out = plain_buffer;
121         return WERR_OK;
122 }
123
124 static WERROR dsdb_decrypt_attribute(const DATA_BLOB *gensec_skey,
125                                      uint32_t rid,
126                                      struct drsuapi_DsReplicaAttribute *attr)
127 {
128         WERROR status;
129         TALLOC_CTX *mem_ctx;
130         DATA_BLOB *enc_data;
131         DATA_BLOB plain_data;
132         bool rid_crypt = false;
133
134         if (attr->value_ctr.num_values == 0) {
135                 return WERR_OK;
136         }
137
138         switch (attr->attid) {
139         case DRSUAPI_ATTRIBUTE_dBCSPwd:
140         case DRSUAPI_ATTRIBUTE_unicodePwd:
141         case DRSUAPI_ATTRIBUTE_ntPwdHistory:
142         case DRSUAPI_ATTRIBUTE_lmPwdHistory:
143                 rid_crypt = true;
144                 break;
145         case DRSUAPI_ATTRIBUTE_supplementalCredentials:
146         case DRSUAPI_ATTRIBUTE_priorValue:
147         case DRSUAPI_ATTRIBUTE_currentValue:
148         case DRSUAPI_ATTRIBUTE_trustAuthOutgoing:
149         case DRSUAPI_ATTRIBUTE_trustAuthIncoming:
150         case DRSUAPI_ATTRIBUTE_initialAuthOutgoing:
151         case DRSUAPI_ATTRIBUTE_initialAuthIncoming:
152                 break;
153         default:
154                 return WERR_OK;
155         }
156
157         if (attr->value_ctr.num_values > 1) {
158                 return WERR_DS_DRA_INVALID_PARAMETER;
159         }
160
161         if (!attr->value_ctr.values[0].blob) {
162                 return WERR_DS_DRA_INVALID_PARAMETER;
163         }
164
165         mem_ctx         = attr->value_ctr.values[0].blob;
166         enc_data        = attr->value_ctr.values[0].blob;
167
168         status = dsdb_decrypt_attribute_value(mem_ctx,
169                                               gensec_skey,
170                                               rid_crypt,
171                                               rid,
172                                               enc_data,
173                                               &plain_data);
174         W_ERROR_NOT_OK_RETURN(status);
175
176         talloc_free(attr->value_ctr.values[0].blob->data);
177         *attr->value_ctr.values[0].blob = plain_data;
178
179         return WERR_OK;
180 }
181
182 static WERROR dsdb_convert_object(struct ldb_context *ldb,
183                                   const struct dsdb_schema *schema,
184                                   struct dsdb_extended_replicated_objects *ctr,
185                                   const struct drsuapi_DsReplicaObjectListItemEx *in,
186                                   const DATA_BLOB *gensec_skey,
187                                   TALLOC_CTX *mem_ctx,
188                                   struct dsdb_extended_replicated_object *out)
189 {
190         NTSTATUS nt_status;
191         WERROR status;
192         uint32_t i;
193         struct ldb_message *msg;
194         struct replPropertyMetaDataBlob *md;
195         struct ldb_val guid_value;
196         NTTIME whenChanged = 0;
197         time_t whenChanged_t;
198         const char *whenChanged_s;
199         const char *rdn_name = NULL;
200         const struct ldb_val *rdn_value = NULL;
201         const struct dsdb_attribute *rdn_attr = NULL;
202         uint32_t rdn_attid;
203         struct drsuapi_DsReplicaAttribute *name_a = NULL;
204         struct drsuapi_DsReplicaMetaData *name_d = NULL;
205         struct replPropertyMetaData1 *rdn_m = NULL;
206         struct dom_sid *sid = NULL;
207         uint32_t rid = 0;
208         int ret;
209
210         if (!in->object.identifier) {
211                 return WERR_FOOBAR;
212         }
213
214         if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
215                 return WERR_FOOBAR;
216         }
217
218         if (in->object.attribute_ctr.num_attributes != 0 && !in->meta_data_ctr) {
219                 return WERR_FOOBAR;
220         }
221
222         if (in->object.attribute_ctr.num_attributes != in->meta_data_ctr->count) {
223                 return WERR_FOOBAR;
224         }
225
226         sid = &in->object.identifier->sid;
227         if (sid->num_auths > 0) {
228                 rid = sid->sub_auths[sid->num_auths - 1];
229         }
230
231         msg = ldb_msg_new(mem_ctx);
232         W_ERROR_HAVE_NO_MEMORY(msg);
233
234         msg->dn                 = ldb_dn_new(msg, ldb, in->object.identifier->dn);
235         W_ERROR_HAVE_NO_MEMORY(msg->dn);
236
237         rdn_name        = ldb_dn_get_rdn_name(msg->dn);
238         rdn_attr        = dsdb_attribute_by_lDAPDisplayName(schema, rdn_name);
239         if (!rdn_attr) {
240                 return WERR_FOOBAR;
241         }
242         rdn_attid       = rdn_attr->attributeID_id;
243         rdn_value       = ldb_dn_get_rdn_val(msg->dn);
244
245         msg->num_elements       = in->object.attribute_ctr.num_attributes;
246         msg->elements           = talloc_array(msg, struct ldb_message_element,
247                                                msg->num_elements);
248         W_ERROR_HAVE_NO_MEMORY(msg->elements);
249
250         md = talloc(mem_ctx, struct replPropertyMetaDataBlob);
251         W_ERROR_HAVE_NO_MEMORY(md);
252
253         md->version             = 1;
254         md->reserved            = 0;
255         md->ctr.ctr1.count      = in->meta_data_ctr->count;
256         md->ctr.ctr1.reserved   = 0;
257         md->ctr.ctr1.array      = talloc_array(mem_ctx,
258                                                struct replPropertyMetaData1,
259                                                md->ctr.ctr1.count + 1); /* +1 because of the RDN attribute */
260         W_ERROR_HAVE_NO_MEMORY(md->ctr.ctr1.array);
261
262         for (i=0; i < in->meta_data_ctr->count; i++) {
263                 struct drsuapi_DsReplicaAttribute *a;
264                 struct drsuapi_DsReplicaMetaData *d;
265                 struct replPropertyMetaData1 *m;
266                 struct ldb_message_element *e;
267
268                 a = &in->object.attribute_ctr.attributes[i];
269                 d = &in->meta_data_ctr->meta_data[i];
270                 m = &md->ctr.ctr1.array[i];
271                 e = &msg->elements[i];
272
273                 status = dsdb_decrypt_attribute(gensec_skey, rid, a);
274                 W_ERROR_NOT_OK_RETURN(status);
275
276                 status = dsdb_attribute_drsuapi_to_ldb(schema, a, msg->elements, e);
277                 W_ERROR_NOT_OK_RETURN(status);
278
279                 m->attid                        = a->attid;
280                 m->version                      = d->version;
281                 m->originating_change_time      = d->originating_change_time;
282                 m->originating_invocation_id    = d->originating_invocation_id;
283                 m->originating_usn              = d->originating_usn;
284                 m->local_usn                    = 0;
285
286                 if (d->originating_change_time > whenChanged) {
287                         whenChanged = d->originating_change_time;
288                 }
289
290                 if (a->attid == DRSUAPI_ATTRIBUTE_name) {
291                         name_a = a;
292                         name_d = d;
293                         rdn_m = &md->ctr.ctr1.array[md->ctr.ctr1.count];
294                 }
295         }
296
297         if (rdn_m) {
298                 ret = ldb_msg_add_value(msg, rdn_attr->lDAPDisplayName, rdn_value, NULL);
299                 if (ret != LDB_SUCCESS) {
300                         return WERR_FOOBAR;
301                 }
302
303                 rdn_m->attid                            = rdn_attid;
304                 rdn_m->version                          = name_d->version;
305                 rdn_m->originating_change_time          = name_d->originating_change_time;
306                 rdn_m->originating_invocation_id        = name_d->originating_invocation_id;
307                 rdn_m->originating_usn                  = name_d->originating_usn;
308                 rdn_m->local_usn                        = 0;
309                 md->ctr.ctr1.count++;
310
311         }
312
313         whenChanged_t = nt_time_to_unix(whenChanged);
314         whenChanged_s = ldb_timestring(msg, whenChanged_t);
315         W_ERROR_HAVE_NO_MEMORY(whenChanged_s);
316
317         nt_status = ndr_push_struct_blob(&guid_value, msg, &in->object.identifier->guid,
318                                          (ndr_push_flags_fn_t)ndr_push_GUID);
319         if (!NT_STATUS_IS_OK(nt_status)) {
320                 return ntstatus_to_werror(nt_status);
321         }
322
323         out->msg                = msg;
324         out->guid_value         = guid_value;
325         out->when_changed       = whenChanged_s;
326         out->meta_data          = md;
327         return WERR_OK;
328 }
329
330 WERROR dsdb_extended_replicated_objects_commit(struct ldb_context *ldb,
331                                                const char *partition_dn,
332                                                const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
333                                                uint32_t object_count,
334                                                const struct drsuapi_DsReplicaObjectListItemEx *first_object,
335                                                uint32_t linked_attributes_count,
336                                                const struct drsuapi_DsReplicaLinkedAttribute *linked_attributes,
337                                                const struct repsFromTo1 *source_dsa,
338                                                const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector,
339                                                const DATA_BLOB *gensec_skey,
340                                                TALLOC_CTX *mem_ctx,
341                                                struct dsdb_extended_replicated_objects **_out)
342 {
343         WERROR status;
344         const struct dsdb_schema *schema;
345         struct dsdb_extended_replicated_objects *out;
346         struct ldb_result *ext_res;
347         const struct drsuapi_DsReplicaObjectListItemEx *cur;
348         uint32_t i;
349         int ret;
350
351         schema = dsdb_get_schema(ldb);
352         if (!schema) {
353                 return WERR_DS_SCHEMA_NOT_LOADED;
354         }
355
356         status = dsdb_verify_oid_mappings_drsuapi(schema, mapping_ctr);
357         W_ERROR_NOT_OK_RETURN(status);
358
359         out = talloc_zero(mem_ctx, struct dsdb_extended_replicated_objects);
360         W_ERROR_HAVE_NO_MEMORY(out);
361         out->version            = DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION;
362
363         out->partition_dn       = ldb_dn_new(out, ldb, partition_dn);
364         W_ERROR_HAVE_NO_MEMORY(out->partition_dn);
365
366         out->source_dsa         = source_dsa;
367         out->uptodateness_vector= uptodateness_vector;
368
369         out->num_objects        = object_count;
370         out->objects            = talloc_array(out,
371                                                struct dsdb_extended_replicated_object,
372                                                out->num_objects);
373         W_ERROR_HAVE_NO_MEMORY(out->objects);
374
375         for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
376                 if (i == out->num_objects) {
377                         return WERR_FOOBAR;
378                 }
379
380                 status = dsdb_convert_object(ldb, schema, out, cur, gensec_skey, out->objects, &out->objects[i]);
381                 W_ERROR_NOT_OK_RETURN(status);
382         }
383         if (i != out->num_objects) {
384                 return WERR_FOOBAR;
385         }
386
387         /* TODO: handle linked attributes */
388
389         ret = ldb_extended(ldb, DSDB_EXTENDED_REPLICATED_OBJECTS_OID, out, &ext_res);
390         if (ret != LDB_SUCCESS) {
391                 DEBUG(0,("Failed to apply records: %d: %s\n",
392                         ret, ldb_strerror(ret)));
393                 talloc_free(out);
394                 return WERR_FOOBAR;
395         }
396         talloc_free(ext_res);
397
398         if (_out) {
399                 *_out = out;
400         } else {
401                 talloc_free(out);
402         }
403
404         return WERR_OK;
405 }