s4/drs: broke out the core of the getncchanges code
[nivanova/samba-autobuild/.git] / source4 / rpc_server / drsuapi / getncchanges.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    implement the DRSUpdateRefs call
5
6    Copyright (C) Anatoliy Atanasov 2009
7    Copyright (C) Andrew Tridgell 2009
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/ndr_drsuapi.h"
25 #include "rpc_server/dcerpc_server.h"
26 #include "rpc_server/common/common.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "lib/ldb/include/ldb_errors.h"
29 #include "param/param.h"
30 #include "librpc/gen_ndr/ndr_drsblobs.h"
31 #include "auth/auth.h"
32 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
33
34 /* 
35   drsuapi_DsGetNCChanges
36 */
37 static WERROR get_nc_changes_build_object(struct drsuapi_DsReplicaObjectListItemEx *obj,
38                                           struct ldb_message *msg,
39                                           struct ldb_context *sam_ctx,
40                                           struct ldb_dn *ncRoot_dn,
41                                           struct dsdb_schema *schema)
42 {
43         const struct ldb_val *md_value;
44         int i;
45         struct ldb_dn *obj_dn;
46
47         if (ldb_dn_compare(ncRoot_dn, msg->dn) == 0) {
48                 obj->is_nc_prefix = true;
49                 obj->parent_object_guid = NULL;
50         } else {
51                 obj->is_nc_prefix = false;
52                 obj->parent_object_guid = talloc(obj, struct GUID);
53                 *obj->parent_object_guid = samdb_result_guid(msg, "parentGUID");
54         }
55         obj->next_object = NULL;
56         
57         obj->meta_data_ctr = talloc(obj, struct drsuapi_DsReplicaMetaDataCtr);
58         md_value = ldb_msg_find_ldb_val(msg, "replPropertyMetaData");
59         if (md_value) {
60                 struct replPropertyMetaDataBlob md;
61                 enum ndr_err_code ndr_err;
62                 ndr_err = ndr_pull_struct_blob(md_value, obj,
63                                                lp_iconv_convenience(ldb_get_opaque(sam_ctx, "loadparm")), &md,
64                                                (ndr_pull_flags_fn_t)ndr_pull_replPropertyMetaDataBlob);
65                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
66                         return WERR_DS_DRA_INTERNAL_ERROR;
67                 }
68                 
69                 if (md.version != 1) {
70                         return WERR_DS_DRA_INTERNAL_ERROR;
71                 }
72                 
73                 obj->meta_data_ctr->count = md.ctr.ctr1.count;
74                 obj->meta_data_ctr->meta_data = talloc_array(obj, struct drsuapi_DsReplicaMetaData, md.ctr.ctr1.count);
75                 for (i=0; i<md.ctr.ctr1.count; i++) {
76                         obj->meta_data_ctr->meta_data[i].originating_change_time = md.ctr.ctr1.array[i].originating_change_time;
77                         obj->meta_data_ctr->meta_data[i].version = md.ctr.ctr1.array[i].version;
78                         obj->meta_data_ctr->meta_data[i].originating_invocation_id = md.ctr.ctr1.array[i].originating_invocation_id;
79                         obj->meta_data_ctr->meta_data[i].originating_usn = md.ctr.ctr1.array[i].originating_usn;
80                 }
81         } else {
82                 obj->meta_data_ctr->meta_data = talloc(obj, struct drsuapi_DsReplicaMetaData);
83                 obj->meta_data_ctr->count = 0;
84         }
85         obj->object.identifier = talloc(obj, struct drsuapi_DsReplicaObjectIdentifier);
86         obj_dn = ldb_msg_find_attr_as_dn(sam_ctx, obj, msg, "distinguishedName");
87         obj->object.identifier->dn = ldb_dn_get_linearized(obj_dn);
88         obj->object.identifier->guid = GUID_zero();
89         ZERO_STRUCT(obj->object.identifier->sid);
90         
91         obj->object.attribute_ctr.num_attributes = msg->num_elements;
92         /* Exclude non-replicate attributes from the responce.*/
93         for (i=0; i<msg->num_elements; i++) {
94                 const struct dsdb_attribute *sa;
95                 sa = dsdb_attribute_by_lDAPDisplayName(schema, msg->elements[i].name);
96                 if (sa && sa->systemFlags & SYSTEM_FLAG_CR_NTDS_NC) {
97                         ldb_msg_remove_attr(msg, msg->elements[i].name);
98                         obj->object.attribute_ctr.num_attributes--;
99                 }
100         }
101         obj->object.attribute_ctr.attributes = talloc_array(obj, struct drsuapi_DsReplicaAttribute,
102                                                                       obj->object.attribute_ctr.num_attributes);
103         for (i=0; i<obj->object.attribute_ctr.num_attributes; i++) {
104                 dsdb_attribute_ldb_to_drsuapi(sam_ctx, schema,&msg->elements[i], obj,
105                                               &obj->object.attribute_ctr.attributes[i]);
106         }
107
108         return WERR_OK;
109 }
110
111 /* 
112   drsuapi_DsGetNCChanges
113 */
114 WERROR dcesrv_drsuapi_DsGetNCChanges(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
115                                      struct drsuapi_DsGetNCChanges *r)
116 {
117         struct ldb_result *site_res;
118         struct drsuapi_DsReplicaObjectIdentifier *ncRoot;
119         struct ldb_context *sam_ctx;
120         struct ldb_dn *ncRoot_dn;
121         int ret;
122         int i;
123         struct dsdb_schema *schema;
124         struct drsuapi_DsReplicaOIDMapping_Ctr *ctr;
125         time_t t = time(NULL);
126         NTTIME now;
127         struct drsuapi_DsReplicaObjectListItemEx *currentObject;
128
129         /*
130          * connect to the samdb
131          */
132         sam_ctx = samdb_connect(mem_ctx, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx, dce_call->conn->auth_state.session_info);
133         if (!sam_ctx) {
134                 return WERR_FOOBAR;
135         }
136
137         /* Check request revision. */
138         if (r->in.level != 8) {
139                 return WERR_REVISION_MISMATCH;
140         }
141
142         /* Perform access checks. */
143         if (r->in.req->req8.naming_context == NULL) {
144                 return WERR_DS_DRA_INVALID_PARAMETER;
145         }
146
147         ncRoot = r->in.req->req8.naming_context;
148         if (ncRoot == NULL) {
149                 return WERR_DS_DRA_BAD_NC;
150         }
151
152         DEBUG(4,("DsGetNSChanges with uSHChanged >= %llu\n", 
153                  (unsigned long long)r->in.req->req8.highwatermark.highest_usn));
154
155         /* Construct response. */
156         ncRoot_dn = ldb_dn_new(mem_ctx, sam_ctx, ncRoot->dn);
157         ret = drsuapi_search_with_extended_dn(sam_ctx, mem_ctx, &site_res,
158                          ncRoot_dn, LDB_SCOPE_SUBTREE, NULL,
159                                               "(&(uSNChanged>=%llu)(objectClass=*))", 
160                                               (unsigned long long)r->in.req->req8.highwatermark.highest_usn);
161         if (ret != LDB_SUCCESS) {
162                 return WERR_DS_DRA_INTERNAL_ERROR;
163         }
164
165         *r->out.level_out = 6;
166         r->out.ctr->ctr6.naming_context = talloc(mem_ctx, struct drsuapi_DsReplicaObjectIdentifier);
167         *r->out.ctr->ctr6.naming_context = *ncRoot;
168         /* TODO: linked attributes*/
169         r->out.ctr->ctr6.linked_attributes_count = 0;
170         r->out.ctr->ctr6.linked_attributes = NULL;
171
172         r->out.ctr->ctr6.object_count = 0;
173         r->out.ctr->ctr6.more_data = false;
174         r->out.ctr->ctr6.uptodateness_vector = NULL;
175
176         /* Prefix mapping */
177         schema = dsdb_get_schema(sam_ctx);
178         if (!schema) {
179                 DEBUG(0,("No schema in sam_ctx\n"));
180                 return WERR_DS_DRA_INTERNAL_ERROR;
181         }
182
183         dsdb_get_oid_mappings_drsuapi(schema, true, mem_ctx, &ctr);
184         r->out.ctr->ctr6.mapping_ctr = *ctr;
185
186         r->out.ctr->ctr6.source_dsa_guid = *(samdb_ntds_objectGUID(sam_ctx));
187         r->out.ctr->ctr6.source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
188
189         r->out.ctr->ctr6.old_highwatermark = r->in.req->req8.highwatermark;
190         r->out.ctr->ctr6.new_highwatermark = r->in.req->req8.highwatermark;
191
192         r->out.ctr->ctr6.first_object = talloc(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
193         currentObject = r->out.ctr->ctr6.first_object;
194
195         for(i=0; i<site_res->count; i++) {
196                 int uSN;
197                 WERROR werr;
198
199                 uSN = ldb_msg_find_attr_as_int(site_res->msgs[i], "uSNChanged", -1);
200                 r->out.ctr->ctr6.object_count++;
201                 if (uSN > r->out.ctr->ctr6.new_highwatermark.highest_usn) {
202                         r->out.ctr->ctr6.new_highwatermark.highest_usn = uSN;
203                 }
204
205                 werr = get_nc_changes_build_object(currentObject, site_res->msgs[i], sam_ctx, ncRoot_dn, schema);
206                 if (!W_ERROR_IS_OK(werr)) {
207                         return werr;
208                 }
209                 if (i == (site_res->count-1)) {
210                         break;
211                 }
212                 currentObject->next_object = talloc(mem_ctx, struct drsuapi_DsReplicaObjectListItemEx);
213                 currentObject = currentObject->next_object;
214         }
215
216         r->out.ctr->ctr6.uptodateness_vector = talloc(mem_ctx, struct drsuapi_DsReplicaCursor2CtrEx);
217
218         r->out.ctr->ctr6.uptodateness_vector->version = 2;
219         r->out.ctr->ctr6.uptodateness_vector->count = 1;
220         r->out.ctr->ctr6.uptodateness_vector->reserved1 = 0;
221         r->out.ctr->ctr6.uptodateness_vector->reserved2 = 0;
222         r->out.ctr->ctr6.uptodateness_vector->cursors = talloc(mem_ctx, struct drsuapi_DsReplicaCursor2);
223
224         r->out.ctr->ctr6.uptodateness_vector->cursors[0].source_dsa_invocation_id = *(samdb_ntds_invocation_id(sam_ctx));
225         r->out.ctr->ctr6.uptodateness_vector->cursors[0].highest_usn = r->out.ctr->ctr6.new_highwatermark.highest_usn;
226         unix_to_nt_time(&now, t);
227         r->out.ctr->ctr6.uptodateness_vector->cursors[0].last_sync_success = now;
228
229         return WERR_OK;
230 }