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