rpc_client: let cli_get_session_key() return talloced session key.
[samba.git] / source3 / libnet / libnet_dssync.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Stefan (metze) Metzmacher 2005
5    Copyright (C) Guenther Deschner 2008
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 "libnet/libnet_dssync.h"
24
25 /****************************************************************
26 ****************************************************************/
27
28 static int libnet_dssync_free_context(struct dssync_context *ctx)
29 {
30         if (!ctx) {
31                 return 0;
32         }
33
34         if (is_valid_policy_hnd(&ctx->bind_handle) && ctx->cli) {
35                 rpccli_drsuapi_DsUnbind(ctx->cli, ctx, &ctx->bind_handle, NULL);
36         }
37
38         return 0;
39 }
40
41 /****************************************************************
42 ****************************************************************/
43
44 NTSTATUS libnet_dssync_init_context(TALLOC_CTX *mem_ctx,
45                                     struct dssync_context **ctx_p)
46 {
47         struct dssync_context *ctx;
48
49         ctx = TALLOC_ZERO_P(mem_ctx, struct dssync_context);
50         NT_STATUS_HAVE_NO_MEMORY(ctx);
51
52         talloc_set_destructor(ctx, libnet_dssync_free_context);
53
54         *ctx_p = ctx;
55
56         return NT_STATUS_OK;
57 }
58
59 /****************************************************************
60 ****************************************************************/
61
62 static NTSTATUS libnet_dssync_bind(TALLOC_CTX *mem_ctx,
63                                    struct dssync_context *ctx)
64 {
65         NTSTATUS status;
66         WERROR werr;
67
68         struct GUID bind_guid;
69         struct drsuapi_DsBindInfoCtr bind_info;
70         struct drsuapi_DsBindInfo28 info28;
71
72         ZERO_STRUCT(info28);
73
74         GUID_from_string(DRSUAPI_DS_BIND_GUID, &bind_guid);
75
76         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
77         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
78         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
79         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
80         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
81         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
82         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
83         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
84         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
85         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
86         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
87         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
88         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
89         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
90         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
91         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
92         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
93         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
94         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
95         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
96         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
97         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
98         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
99         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
100         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
101         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
102         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
103         info28.supported_extensions     |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
104         info28.site_guid                = GUID_zero();
105         info28.u1                       = 508;
106         info28.repl_epoch               = 0;
107
108         bind_info.length = 28;
109         bind_info.info.info28 = info28;
110
111         status = rpccli_drsuapi_DsBind(ctx->cli, mem_ctx,
112                                        &bind_guid,
113                                        &bind_info,
114                                        &ctx->bind_handle,
115                                        &werr);
116
117         if (!NT_STATUS_IS_OK(status)) {
118                 return status;
119         }
120
121         if (!W_ERROR_IS_OK(werr)) {
122                 return werror_to_ntstatus(werr);
123         }
124
125         return status;
126 }
127
128 /****************************************************************
129 ****************************************************************/
130
131 static NTSTATUS libnet_dssync_lookup_nc(TALLOC_CTX *mem_ctx,
132                                         struct dssync_context *ctx)
133 {
134         NTSTATUS status;
135         WERROR werr;
136         int32_t level = 1;
137         union drsuapi_DsNameRequest req;
138         int32_t level_out;
139         struct drsuapi_DsNameString names[1];
140         union drsuapi_DsNameCtr ctr;
141
142         names[0].str = talloc_asprintf(mem_ctx, "%s\\", ctx->domain_name);
143         NT_STATUS_HAVE_NO_MEMORY(names[0].str);
144
145         req.req1.codepage       = 1252; /* german */
146         req.req1.language       = 0x00000407; /* german */
147         req.req1.count          = 1;
148         req.req1.names          = names;
149         req.req1.format_flags   = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
150         req.req1.format_offered = DRSUAPI_DS_NAME_FORMAT_UKNOWN;
151         req.req1.format_desired = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
152
153         status = rpccli_drsuapi_DsCrackNames(ctx->cli, mem_ctx,
154                                              &ctx->bind_handle,
155                                              level,
156                                              &req,
157                                              &level_out,
158                                              &ctr,
159                                              &werr);
160         if (!NT_STATUS_IS_OK(status)) {
161                 return status;
162         }
163
164         if (!W_ERROR_IS_OK(werr)) {
165                 return werror_to_ntstatus(werr);
166         }
167
168         if (ctr.ctr1->count != 1) {
169                 return NT_STATUS_UNSUCCESSFUL;
170         }
171
172         if (ctr.ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
173                 return NT_STATUS_UNSUCCESSFUL;
174         }
175
176         ctx->nc_dn = talloc_strdup(mem_ctx, ctr.ctr1->array[0].result_name);
177         NT_STATUS_HAVE_NO_MEMORY(ctx->nc_dn);
178
179         return NT_STATUS_OK;
180 }
181
182 /****************************************************************
183 ****************************************************************/
184
185 static NTSTATUS libnet_dssync_init(TALLOC_CTX *mem_ctx,
186                                    struct dssync_context *ctx)
187 {
188         NTSTATUS status;
189
190         status = libnet_dssync_bind(mem_ctx, ctx);
191         if (!NT_STATUS_IS_OK(status)) {
192                 return status;
193         }
194
195         if (!ctx->nc_dn) {
196                 status = libnet_dssync_lookup_nc(mem_ctx, ctx);
197         }
198
199         return status;
200 }
201
202 /****************************************************************
203 ****************************************************************/
204
205 static NTSTATUS libnet_dssync_process(TALLOC_CTX *mem_ctx,
206                                       struct dssync_context *ctx)
207 {
208         NTSTATUS status;
209         WERROR werr;
210
211         int32_t level = 8;
212         int32_t level_out = 0;
213         union drsuapi_DsGetNCChangesRequest req;
214         union drsuapi_DsGetNCChangesCtr ctr;
215         struct drsuapi_DsReplicaObjectIdentifier nc;
216         struct dom_sid null_sid;
217
218         struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
219         struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
220         int32_t out_level = 0;
221         int y;
222
223         ZERO_STRUCT(null_sid);
224         ZERO_STRUCT(req);
225
226         nc.dn = ctx->nc_dn;
227         nc.guid = GUID_zero();
228         nc.sid = null_sid;
229
230         req.req8.naming_context         = &nc;
231         req.req8.replica_flags          = DRSUAPI_DS_REPLICA_NEIGHBOUR_WRITEABLE |
232                                           DRSUAPI_DS_REPLICA_NEIGHBOUR_SYNC_ON_STARTUP |
233                                           DRSUAPI_DS_REPLICA_NEIGHBOUR_DO_SCHEDULED_SYNCS |
234                                           DRSUAPI_DS_REPLICA_NEIGHBOUR_RETURN_OBJECT_PARENTS |
235                                           DRSUAPI_DS_REPLICA_NEIGHBOUR_NEVER_SYNCED;
236         req.req8.max_object_count       = 402;
237         req.req8.max_ndr_size           = 402116;
238
239         for (y=0; ;y++) {
240
241                 if (level == 8) {
242                         DEBUG(1,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
243                                 (long long)req.req8.highwatermark.tmp_highest_usn,
244                                 (long long)req.req8.highwatermark.highest_usn));
245                 }
246
247                 status = rpccli_drsuapi_DsGetNCChanges(ctx->cli, mem_ctx,
248                                                        &ctx->bind_handle,
249                                                        level,
250                                                        &req,
251                                                        &level_out,
252                                                        &ctr,
253                                                        &werr);
254                 if (!NT_STATUS_IS_OK(status)) {
255                         werr = ntstatus_to_werror(status);
256                         goto out;
257                 }
258
259                 if (!W_ERROR_IS_OK(werr)) {
260                         goto out;
261                 }
262
263                 if (level_out == 1) {
264                         out_level = 1;
265                         ctr1 = &ctr.ctr1;
266                 } else if (level_out == 2) {
267                         out_level = 1;
268                         ctr1 = ctr.ctr2.ctr.mszip1.ctr1;
269                 }
270
271                 status = cli_get_session_key(mem_ctx, ctx->cli, &ctx->session_key);
272                 if (!NT_STATUS_IS_OK(status)) {
273                         return status;
274                 }
275
276                 if (out_level == 1) {
277                         DEBUG(1,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
278                                 (long long)ctr1->new_highwatermark.tmp_highest_usn,
279                                 (long long)ctr1->new_highwatermark.highest_usn));
280
281                         if (ctx->processing_fn) {
282                                 status = ctx->processing_fn(mem_ctx,
283                                                             ctr1->first_object,
284                                                             ctx);
285                                 if (!NT_STATUS_IS_OK(status)) {
286                                         goto out;
287                                 }
288                         }
289
290                         if (ctr1->new_highwatermark.tmp_highest_usn > ctr1->new_highwatermark.highest_usn) {
291                                 req.req5.highwatermark = ctr1->new_highwatermark;
292                                 continue;
293                         }
294                 }
295
296                 if (level_out == 6) {
297                         out_level = 6;
298                         ctr6 = &ctr.ctr6;
299                 } else if (level_out == 7
300                            && ctr.ctr7.level == 6
301                            && ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP) {
302                         out_level = 6;
303                         ctr6 = ctr.ctr7.ctr.mszip6.ctr6;
304                 }
305
306                 if (out_level == 6) {
307                         DEBUG(1,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
308                                 (long long)ctr6->new_highwatermark.tmp_highest_usn,
309                                 (long long)ctr6->new_highwatermark.highest_usn));
310
311                         if (ctx->processing_fn) {
312                                 status = ctx->processing_fn(mem_ctx,
313                                                             ctr6->first_object,
314                                                             ctx);
315                                 if (!NT_STATUS_IS_OK(status)) {
316                                         goto out;
317                                 }
318                         }
319
320                         if (ctr6->new_highwatermark.tmp_highest_usn > ctr6->new_highwatermark.highest_usn) {
321                                 req.req8.highwatermark = ctr6->new_highwatermark;
322                                 continue;
323                         }
324                 }
325
326                 break;
327         }
328
329  out:
330         return status;
331 }
332
333 /****************************************************************
334 ****************************************************************/
335
336 NTSTATUS libnet_dssync(TALLOC_CTX *mem_ctx,
337                        struct dssync_context *ctx)
338 {
339         NTSTATUS status;
340
341         status = libnet_dssync_init(mem_ctx, ctx);
342         if (!NT_STATUS_IS_OK(status)) {
343                 goto out;
344         }
345
346         status = libnet_dssync_process(mem_ctx, ctx);
347         if (!NT_STATUS_IS_OK(status)) {
348                 goto out;
349         }
350
351  out:
352         return status;
353 }