Move DRSUAPI per-attribute decryption into a common file
[samba.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(struct ldb_context *ldb,
35                                   const struct dsdb_schema *schema,
36                                   struct dsdb_extended_replicated_objects *ctr,
37                                   const struct drsuapi_DsReplicaObjectListItemEx *in,
38                                   const DATA_BLOB *gensec_skey,
39                                   TALLOC_CTX *mem_ctx,
40                                   struct dsdb_extended_replicated_object *out)
41 {
42         NTSTATUS nt_status;
43         enum ndr_err_code ndr_err;
44         WERROR status;
45         uint32_t i;
46         struct ldb_message *msg;
47         struct replPropertyMetaDataBlob *md;
48         struct ldb_val guid_value;
49         NTTIME whenChanged = 0;
50         time_t whenChanged_t;
51         const char *whenChanged_s;
52         const char *rdn_name = NULL;
53         const struct ldb_val *rdn_value = NULL;
54         const struct dsdb_attribute *rdn_attr = NULL;
55         uint32_t rdn_attid;
56         struct drsuapi_DsReplicaAttribute *name_a = NULL;
57         struct drsuapi_DsReplicaMetaData *name_d = NULL;
58         struct replPropertyMetaData1 *rdn_m = NULL;
59         struct dom_sid *sid = NULL;
60         uint32_t rid = 0;
61         int ret;
62
63         if (!in->object.identifier) {
64                 return WERR_FOOBAR;
65         }
66
67         if (!in->object.identifier->dn || !in->object.identifier->dn[0]) {
68                 return WERR_FOOBAR;
69         }
70
71         if (in->object.attribute_ctr.num_attributes != 0 && !in->meta_data_ctr) {
72                 return WERR_FOOBAR;
73         }
74
75         if (in->object.attribute_ctr.num_attributes != in->meta_data_ctr->count) {
76                 return WERR_FOOBAR;
77         }
78
79         sid = &in->object.identifier->sid;
80         if (sid->num_auths > 0) {
81                 rid = sid->sub_auths[sid->num_auths - 1];
82         }
83
84         msg = ldb_msg_new(mem_ctx);
85         W_ERROR_HAVE_NO_MEMORY(msg);
86
87         msg->dn                 = ldb_dn_new(msg, ldb, in->object.identifier->dn);
88         W_ERROR_HAVE_NO_MEMORY(msg->dn);
89
90         rdn_name        = ldb_dn_get_rdn_name(msg->dn);
91         rdn_attr        = dsdb_attribute_by_lDAPDisplayName(schema, rdn_name);
92         if (!rdn_attr) {
93                 return WERR_FOOBAR;
94         }
95         rdn_attid       = rdn_attr->attributeID_id;
96         rdn_value       = ldb_dn_get_rdn_val(msg->dn);
97
98         msg->num_elements       = in->object.attribute_ctr.num_attributes;
99         msg->elements           = talloc_array(msg, struct ldb_message_element,
100                                                msg->num_elements);
101         W_ERROR_HAVE_NO_MEMORY(msg->elements);
102
103         md = talloc(mem_ctx, struct replPropertyMetaDataBlob);
104         W_ERROR_HAVE_NO_MEMORY(md);
105
106         md->version             = 1;
107         md->reserved            = 0;
108         md->ctr.ctr1.count      = in->meta_data_ctr->count;
109         md->ctr.ctr1.reserved   = 0;
110         md->ctr.ctr1.array      = talloc_array(mem_ctx,
111                                                struct replPropertyMetaData1,
112                                                md->ctr.ctr1.count + 1); /* +1 because of the RDN attribute */
113         W_ERROR_HAVE_NO_MEMORY(md->ctr.ctr1.array);
114
115         for (i=0; i < in->meta_data_ctr->count; i++) {
116                 struct drsuapi_DsReplicaAttribute *a;
117                 struct drsuapi_DsReplicaMetaData *d;
118                 struct replPropertyMetaData1 *m;
119                 struct ldb_message_element *e;
120
121                 a = &in->object.attribute_ctr.attributes[i];
122                 d = &in->meta_data_ctr->meta_data[i];
123                 m = &md->ctr.ctr1.array[i];
124                 e = &msg->elements[i];
125
126                 status = drsuapi_decrypt_attribute(a->value_ctr.values[0].blob, gensec_skey, rid, a);
127                 W_ERROR_NOT_OK_RETURN(status);
128
129                 status = dsdb_attribute_drsuapi_to_ldb(ldb, schema, a, msg->elements, e);
130                 W_ERROR_NOT_OK_RETURN(status);
131
132                 m->attid                        = a->attid;
133                 m->version                      = d->version;
134                 m->originating_change_time      = d->originating_change_time;
135                 m->originating_invocation_id    = d->originating_invocation_id;
136                 m->originating_usn              = d->originating_usn;
137                 m->local_usn                    = 0;
138
139                 if (d->originating_change_time > whenChanged) {
140                         whenChanged = d->originating_change_time;
141                 }
142
143                 if (a->attid == DRSUAPI_ATTRIBUTE_name) {
144                         name_a = a;
145                         name_d = d;
146                         rdn_m = &md->ctr.ctr1.array[md->ctr.ctr1.count];
147                 }
148         }
149
150         if (rdn_m) {
151                 ret = ldb_msg_add_value(msg, rdn_attr->lDAPDisplayName, rdn_value, NULL);
152                 if (ret != LDB_SUCCESS) {
153                         return WERR_FOOBAR;
154                 }
155
156                 rdn_m->attid                            = rdn_attid;
157                 rdn_m->version                          = name_d->version;
158                 rdn_m->originating_change_time          = name_d->originating_change_time;
159                 rdn_m->originating_invocation_id        = name_d->originating_invocation_id;
160                 rdn_m->originating_usn                  = name_d->originating_usn;
161                 rdn_m->local_usn                        = 0;
162                 md->ctr.ctr1.count++;
163
164         }
165
166         whenChanged_t = nt_time_to_unix(whenChanged);
167         whenChanged_s = ldb_timestring(msg, whenChanged_t);
168         W_ERROR_HAVE_NO_MEMORY(whenChanged_s);
169
170         ndr_err = ndr_push_struct_blob(&guid_value, msg, 
171                                        lp_iconv_convenience(ldb_get_opaque(ldb, "loadparm")),
172                                        &in->object.identifier->guid,
173                                          (ndr_push_flags_fn_t)ndr_push_GUID);
174         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
175                 nt_status = ndr_map_error2ntstatus(ndr_err);
176                 return ntstatus_to_werror(nt_status);
177         }
178
179         out->msg                = msg;
180         out->guid_value         = guid_value;
181         out->when_changed       = whenChanged_s;
182         out->meta_data          = md;
183         return WERR_OK;
184 }
185
186 WERROR dsdb_extended_replicated_objects_commit(struct ldb_context *ldb,
187                                                const char *partition_dn,
188                                                const struct drsuapi_DsReplicaOIDMapping_Ctr *mapping_ctr,
189                                                uint32_t object_count,
190                                                const struct drsuapi_DsReplicaObjectListItemEx *first_object,
191                                                uint32_t linked_attributes_count,
192                                                const struct drsuapi_DsReplicaLinkedAttribute *linked_attributes,
193                                                const struct repsFromTo1 *source_dsa,
194                                                const struct drsuapi_DsReplicaCursor2CtrEx *uptodateness_vector,
195                                                const DATA_BLOB *gensec_skey,
196                                                TALLOC_CTX *mem_ctx,
197                                                struct dsdb_extended_replicated_objects **_out)
198 {
199         WERROR status;
200         const struct dsdb_schema *schema;
201         struct dsdb_extended_replicated_objects *out;
202         struct ldb_result *ext_res;
203         const struct drsuapi_DsReplicaObjectListItemEx *cur;
204         uint32_t i;
205         int ret;
206
207         schema = dsdb_get_schema(ldb);
208         if (!schema) {
209                 return WERR_DS_SCHEMA_NOT_LOADED;
210         }
211
212         status = dsdb_verify_oid_mappings_drsuapi(schema, mapping_ctr);
213         W_ERROR_NOT_OK_RETURN(status);
214
215         out = talloc_zero(mem_ctx, struct dsdb_extended_replicated_objects);
216         W_ERROR_HAVE_NO_MEMORY(out);
217         out->version            = DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION;
218
219         out->partition_dn       = ldb_dn_new(out, ldb, partition_dn);
220         W_ERROR_HAVE_NO_MEMORY(out->partition_dn);
221
222         out->source_dsa         = source_dsa;
223         out->uptodateness_vector= uptodateness_vector;
224
225         out->num_objects        = object_count;
226         out->objects            = talloc_array(out,
227                                                struct dsdb_extended_replicated_object,
228                                                out->num_objects);
229         W_ERROR_HAVE_NO_MEMORY(out->objects);
230
231         for (i=0, cur = first_object; cur; cur = cur->next_object, i++) {
232                 if (i == out->num_objects) {
233                         return WERR_FOOBAR;
234                 }
235
236                 status = dsdb_convert_object(ldb, schema, out, cur, gensec_skey, out->objects, &out->objects[i]);
237                 W_ERROR_NOT_OK_RETURN(status);
238         }
239         if (i != out->num_objects) {
240                 return WERR_FOOBAR;
241         }
242
243         /* TODO: handle linked attributes */
244
245         ret = ldb_extended(ldb, DSDB_EXTENDED_REPLICATED_OBJECTS_OID, out, &ext_res);
246         if (ret != LDB_SUCCESS) {
247                 DEBUG(0,("Failed to apply records: %s: %s\n",
248                          ldb_errstring(ldb), ldb_strerror(ret)));
249                 talloc_free(out);
250                 return WERR_FOOBAR;
251         }
252         talloc_free(ext_res);
253
254         if (_out) {
255                 *_out = out;
256         } else {
257                 talloc_free(out);
258         }
259
260         return WERR_OK;
261 }