Second part of fix for bug #8219 - SMB Panic from Windows 7 Client.
[Samba.git] / source3 / rpcclient / cmd_drsuapi.c
blob3f1ecab75035eaaa1b5451e05edc63511f8eca7c
1 /*
2 Unix SMB/CIFS implementation.
3 RPC pipe client
5 Copyright (C) Guenther Deschner 2008
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.
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.
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/>.
21 #include "includes.h"
22 #include "rpcclient.h"
23 #include "../librpc/gen_ndr/ndr_drsuapi_c.h"
25 static WERROR cracknames(struct rpc_pipe_client *cli,
26 TALLOC_CTX *mem_ctx,
27 struct policy_handle *bind_handle,
28 enum drsuapi_DsNameFormat format_offered,
29 enum drsuapi_DsNameFormat format_desired,
30 int argc,
31 const char **argv,
32 union drsuapi_DsNameCtr *ctr)
34 NTSTATUS status;
35 WERROR werr;
36 int i;
37 uint32_t level = 1;
38 union drsuapi_DsNameRequest req;
39 uint32_t level_out;
40 struct drsuapi_DsNameString *names;
41 struct dcerpc_binding_handle *b = cli->binding_handle;
43 names = TALLOC_ZERO_ARRAY(mem_ctx, struct drsuapi_DsNameString, argc);
44 W_ERROR_HAVE_NO_MEMORY(names);
46 for (i=0; i<argc; i++) {
47 names[i].str = argv[i];
50 req.req1.codepage = 1252; /* german */
51 req.req1.language = 0x00000407; /* german */
52 req.req1.count = argc;
53 req.req1.names = names;
54 req.req1.format_flags = DRSUAPI_DS_NAME_FLAG_NO_FLAGS;
55 req.req1.format_offered = format_offered;
56 req.req1.format_desired = format_desired;
58 status = dcerpc_drsuapi_DsCrackNames(b, mem_ctx,
59 bind_handle,
60 level,
61 &req,
62 &level_out,
63 ctr,
64 &werr);
65 if (!NT_STATUS_IS_OK(status)) {
66 return ntstatus_to_werror(status);
69 if (!W_ERROR_IS_OK(werr)) {
70 return werr;
73 return WERR_OK;
76 static WERROR cmd_drsuapi_cracknames(struct rpc_pipe_client *cli,
77 TALLOC_CTX *mem_ctx, int argc,
78 const char **argv)
80 NTSTATUS status;
81 WERROR werr;
82 int i;
84 struct GUID bind_guid;
85 struct policy_handle bind_handle;
86 struct dcerpc_binding_handle *b = cli->binding_handle;
88 union drsuapi_DsNameCtr ctr;
90 if (argc < 2) {
91 printf("usage: %s name\n", argv[0]);
92 return WERR_OK;
95 GUID_from_string(DRSUAPI_DS_BIND_GUID, &bind_guid);
97 status = dcerpc_drsuapi_DsBind(b, mem_ctx,
98 &bind_guid,
99 NULL,
100 &bind_handle,
101 &werr);
103 if (!NT_STATUS_IS_OK(status)) {
104 return ntstatus_to_werror(status);
107 if (!W_ERROR_IS_OK(werr)) {
108 return werr;
111 werr = cracknames(cli, mem_ctx,
112 &bind_handle,
113 DRSUAPI_DS_NAME_FORMAT_UNKNOWN,
114 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
116 argv+1,
117 &ctr);
119 if (!W_ERROR_IS_OK(werr)) {
120 goto out;
123 for (i=0; i < ctr.ctr1->count; i++) {
124 printf("status: %d\n",
125 ctr.ctr1->array[i].status);
126 printf("dns_domain_name: %s\n",
127 ctr.ctr1->array[i].dns_domain_name);
128 printf("result_name: %s\n",
129 ctr.ctr1->array[i].result_name);
132 out:
133 if (is_valid_policy_hnd(&bind_handle)) {
134 WERROR _werr;
135 dcerpc_drsuapi_DsUnbind(b, mem_ctx, &bind_handle, &_werr);
138 return werr;
141 static void display_domain_controller_info_01(struct drsuapi_DsGetDCConnection01 *r)
143 printf("client_ip_address:\t%s\n", r->client_ip_address);
144 printf("unknown2:\t%d\n", r->unknown2);
145 printf("connection_time:\t%d\n", r->connection_time);
146 printf("unknown4:\t%d\n", r->unknown4);
147 printf("unknown5:\t%d\n", r->unknown5);
148 printf("unknown6:\t%d\n", r->unknown6);
149 printf("client_account:\t%s\n", r->client_account);
152 static void display_domain_controller_info_1(struct drsuapi_DsGetDCInfo1 *r)
154 printf("netbios_name:\t%s\n", r->netbios_name);
155 printf("dns_name:\t%s\n", r->dns_name);
156 printf("site_name:\t%s\n", r->site_name);
157 printf("computer_dn:\t%s\n", r->computer_dn);
158 printf("server_dn:\t%s\n", r->server_dn);
159 printf("is_pdc:\t\t%s\n", r->is_pdc ? "true" : "false");
160 printf("is_enabled:\t%s\n", r->is_enabled ? "true" : "false");
163 static void display_domain_controller_info_2(struct drsuapi_DsGetDCInfo2 *r)
165 printf("netbios_name:\t%s\n", r->netbios_name);
166 printf("dns_name:\t%s\n", r->dns_name);
167 printf("site_name:\t%s\n", r->site_name);
168 printf("site_dn:\t%s\n", r->site_dn);
169 printf("computer_dn:\t%s\n", r->computer_dn);
170 printf("server_dn:\t%s\n", r->server_dn);
171 printf("ntds_dn:\t%s\n", r->ntds_dn);
172 printf("is_pdc:\t\t%s\n", r->is_pdc ? "true" : "false");
173 printf("is_enabled:\t%s\n", r->is_enabled ? "true" : "false");
174 printf("is_gc:\t\t%s\n", r->is_gc ? "true" : "false");
175 printf("site_guid:\t%s\n", GUID_string(talloc_tos(), &r->site_guid));
176 printf("computer_guid:\t%s\n", GUID_string(talloc_tos(), &r->computer_guid));
177 printf("server_guid:\t%s\n", GUID_string(talloc_tos(), &r->server_guid));
178 printf("ntds_guid:\t%s\n", GUID_string(talloc_tos(), &r->ntds_guid));
181 static void display_domain_controller_info_3(struct drsuapi_DsGetDCInfo3 *r)
183 printf("netbios_name:\t%s\n", r->netbios_name);
184 printf("dns_name:\t%s\n", r->dns_name);
185 printf("site_name:\t%s\n", r->site_name);
186 printf("site_dn:\t%s\n", r->site_dn);
187 printf("computer_dn:\t%s\n", r->computer_dn);
188 printf("server_dn:\t%s\n", r->server_dn);
189 printf("ntds_dn:\t%s\n", r->ntds_dn);
190 printf("is_pdc:\t\t%s\n", r->is_pdc ? "true" : "false");
191 printf("is_enabled:\t%s\n", r->is_enabled ? "true" : "false");
192 printf("is_gc:\t\t%s\n", r->is_gc ? "true" : "false");
193 printf("is_rodc:\t%s\n", r->is_rodc ? "true" : "false");
194 printf("site_guid:\t%s\n", GUID_string(talloc_tos(), &r->site_guid));
195 printf("computer_guid:\t%s\n", GUID_string(talloc_tos(), &r->computer_guid));
196 printf("server_guid:\t%s\n", GUID_string(talloc_tos(), &r->server_guid));
197 printf("ntds_guid:\t%s\n", GUID_string(talloc_tos(), &r->ntds_guid));
200 static void display_domain_controller_info(int32_t level,
201 union drsuapi_DsGetDCInfoCtr *ctr)
203 int i;
205 switch (level) {
206 case DRSUAPI_DC_CONNECTION_CTR_01:
207 for (i=0; i<ctr->ctr01.count; i++) {
208 printf("----------\n");
209 display_domain_controller_info_01(&ctr->ctr01.array[i]);
211 break;
212 case DRSUAPI_DC_INFO_CTR_1:
213 for (i=0; i<ctr->ctr1.count; i++) {
214 printf("----------\n");
215 display_domain_controller_info_1(&ctr->ctr1.array[i]);
217 break;
218 case DRSUAPI_DC_INFO_CTR_2:
219 for (i=0; i<ctr->ctr2.count; i++) {
220 printf("----------\n");
221 display_domain_controller_info_2(&ctr->ctr2.array[i]);
223 break;
224 case DRSUAPI_DC_INFO_CTR_3:
225 for (i=0; i<ctr->ctr3.count; i++) {
226 printf("----------\n");
227 display_domain_controller_info_3(&ctr->ctr3.array[i]);
229 break;
230 default:
231 break;
235 static WERROR cmd_drsuapi_getdcinfo(struct rpc_pipe_client *cli,
236 TALLOC_CTX *mem_ctx, int argc,
237 const char **argv)
239 NTSTATUS status;
240 WERROR werr;
242 struct GUID bind_guid;
243 struct policy_handle bind_handle;
244 struct dcerpc_binding_handle *b = cli->binding_handle;
246 const char *domain = NULL;
247 int32_t level = 1;
248 int32_t level_out;
249 union drsuapi_DsGetDCInfoRequest req;
250 union drsuapi_DsGetDCInfoCtr ctr;
252 if (argc < 2) {
253 printf("usage: %s domain [level]\n", argv[0]);
254 return WERR_OK;
257 domain = argv[1];
258 if (argc >= 3) {
259 level = atoi(argv[2]);
262 GUID_from_string(DRSUAPI_DS_BIND_GUID, &bind_guid);
264 status = dcerpc_drsuapi_DsBind(b, mem_ctx,
265 &bind_guid,
266 NULL,
267 &bind_handle,
268 &werr);
270 if (!NT_STATUS_IS_OK(status)) {
271 return ntstatus_to_werror(status);
274 if (!W_ERROR_IS_OK(werr)) {
275 return werr;
278 req.req1.domain_name = domain;
279 req.req1.level = level;
281 status = dcerpc_drsuapi_DsGetDomainControllerInfo(b, mem_ctx,
282 &bind_handle,
284 &req,
285 &level_out,
286 &ctr,
287 &werr);
288 if (!NT_STATUS_IS_OK(status)) {
289 werr = ntstatus_to_werror(status);
290 goto out;
293 if (!W_ERROR_IS_OK(werr)) {
294 goto out;
297 display_domain_controller_info(level_out, &ctr);
298 out:
299 if (is_valid_policy_hnd(&bind_handle)) {
300 WERROR _werr;
301 dcerpc_drsuapi_DsUnbind(b, mem_ctx, &bind_handle, &_werr);
304 return werr;
307 static WERROR cmd_drsuapi_getncchanges(struct rpc_pipe_client *cli,
308 TALLOC_CTX *mem_ctx, int argc,
309 const char **argv)
311 NTSTATUS status;
312 WERROR werr;
314 struct policy_handle bind_handle;
315 struct dcerpc_binding_handle *b = cli->binding_handle;
317 struct GUID bind_guid;
318 struct drsuapi_DsBindInfoCtr bind_info;
319 struct drsuapi_DsBindInfo28 info28;
321 const char *nc_dn = NULL;
323 DATA_BLOB session_key;
325 uint32_t level = 8;
326 bool single = false;
327 uint32_t level_out = 0;
328 union drsuapi_DsGetNCChangesRequest req;
329 union drsuapi_DsGetNCChangesCtr ctr;
330 struct drsuapi_DsReplicaObjectIdentifier nc;
331 struct dom_sid null_sid;
333 struct drsuapi_DsGetNCChangesCtr1 *ctr1 = NULL;
334 struct drsuapi_DsGetNCChangesCtr6 *ctr6 = NULL;
335 uint32_t out_level = 0;
336 int y;
338 uint32_t supported_extensions = 0;
339 uint32_t replica_flags = DRSUAPI_DRS_WRIT_REP |
340 DRSUAPI_DRS_INIT_SYNC |
341 DRSUAPI_DRS_PER_SYNC |
342 DRSUAPI_DRS_GET_ANC |
343 DRSUAPI_DRS_NEVER_SYNCED;
345 if (argc > 3) {
346 printf("usage: %s [naming_context_or_object_dn [single]]\n", argv[0]);
347 return WERR_OK;
350 if (argc >= 2) {
351 nc_dn = argv[1];
354 if (argc == 3) {
355 if (strequal(argv[2], "single")) {
356 single = true;
357 } else {
358 printf("warning: ignoring unknown argument '%s'\n",
359 argv[2]);
363 ZERO_STRUCT(info28);
365 ZERO_STRUCT(null_sid);
366 ZERO_STRUCT(req);
368 GUID_from_string(DRSUAPI_DS_BIND_GUID, &bind_guid);
370 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_BASE;
371 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ASYNC_REPLICATION;
372 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_REMOVEAPI;
373 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_MOVEREQ_V2;
374 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHG_COMPRESS;
375 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V1;
376 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_RESTORE_USN_OPTIMIZATION;
377 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_KCC_EXECUTE;
378 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRY_V2;
379 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_LINKED_VALUE_REPLICATION;
380 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V2;
381 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_INSTANCE_TYPE_NOT_REQ_ON_MOD;
382 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_CRYPTO_BIND;
383 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_REPL_INFO;
384 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_STRONG_ENCRYPTION;
385 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_DCINFO_V01;
386 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_TRANSITIVE_MEMBERSHIP;
387 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADD_SID_HISTORY;
388 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_POST_BETA3;
389 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GET_MEMBERSHIPS2;
390 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V6;
391 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_NONDOMAIN_NCS;
392 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8;
393 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V5;
394 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V6;
395 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_ADDENTRYREPLY_V3;
396 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_GETCHGREPLY_V7;
397 info28.supported_extensions |= DRSUAPI_SUPPORTED_EXTENSION_VERIFY_OBJECT;
398 info28.site_guid = GUID_zero();
399 info28.pid = 0;
400 info28.repl_epoch = 0;
402 bind_info.length = 28;
403 bind_info.info.info28 = info28;
405 status = dcerpc_drsuapi_DsBind(b, mem_ctx,
406 &bind_guid,
407 &bind_info,
408 &bind_handle,
409 &werr);
411 if (!NT_STATUS_IS_OK(status)) {
412 return ntstatus_to_werror(status);
415 if (!W_ERROR_IS_OK(werr)) {
416 return werr;
419 if (bind_info.length == 24) {
420 supported_extensions = bind_info.info.info24.supported_extensions;
421 } else if (bind_info.length == 28) {
422 supported_extensions = bind_info.info.info28.supported_extensions;
423 } else if (bind_info.length == 48) {
424 supported_extensions = bind_info.info.info48.supported_extensions;
427 if (!nc_dn) {
429 union drsuapi_DsNameCtr crack_ctr;
430 const char *name;
432 name = talloc_asprintf(mem_ctx, "%s\\", lp_workgroup());
433 W_ERROR_HAVE_NO_MEMORY(name);
435 werr = cracknames(cli, mem_ctx,
436 &bind_handle,
437 DRSUAPI_DS_NAME_FORMAT_UNKNOWN,
438 DRSUAPI_DS_NAME_FORMAT_FQDN_1779,
440 &name,
441 &crack_ctr);
442 if (!W_ERROR_IS_OK(werr)) {
443 return werr;
446 if (crack_ctr.ctr1->count != 1) {
447 return WERR_NO_SUCH_DOMAIN;
450 if (crack_ctr.ctr1->array[0].status != DRSUAPI_DS_NAME_STATUS_OK) {
451 return WERR_NO_SUCH_DOMAIN;
454 nc_dn = talloc_strdup(mem_ctx, crack_ctr.ctr1->array[0].result_name);
455 W_ERROR_HAVE_NO_MEMORY(nc_dn);
457 printf("using: %s\n", nc_dn);
460 nc.dn = nc_dn;
461 nc.guid = GUID_zero();
462 nc.sid = null_sid;
464 if (supported_extensions & DRSUAPI_SUPPORTED_EXTENSION_GETCHGREQ_V8) {
465 level = 8;
466 req.req8.naming_context = &nc;
467 req.req8.replica_flags = replica_flags;
468 req.req8.max_object_count = 402;
469 req.req8.max_ndr_size = 402116;
470 if (single) {
471 req.req8.extended_op = DRSUAPI_EXOP_REPL_OBJ;
473 } else {
474 level = 5;
475 req.req5.naming_context = &nc;
476 req.req5.replica_flags = replica_flags;
477 req.req5.max_object_count = 402;
478 req.req5.max_ndr_size = 402116;
479 if (single) {
480 req.req5.extended_op = DRSUAPI_EXOP_REPL_OBJ;
484 for (y=0; ;y++) {
486 if (level == 8) {
487 DEBUG(1,("start[%d] tmp_higest_usn: %llu , highest_usn: %llu\n",y,
488 (long long)req.req8.highwatermark.tmp_highest_usn,
489 (long long)req.req8.highwatermark.highest_usn));
492 status = dcerpc_drsuapi_DsGetNCChanges(b, mem_ctx,
493 &bind_handle,
494 level,
495 &req,
496 &level_out,
497 &ctr,
498 &werr);
499 if (!NT_STATUS_IS_OK(status)) {
500 werr = ntstatus_to_werror(status);
501 printf("Failed to get NC Changes: %s",
502 get_friendly_nt_error_msg(status));
503 goto out;
506 if (!W_ERROR_IS_OK(werr)) {
507 printf("Failed to get NC Changes: %s",
508 get_friendly_werror_msg(werr));
509 goto out;
512 if (level_out == 1) {
513 out_level = 1;
514 ctr1 = &ctr.ctr1;
515 } else if (level_out == 2 && ctr.ctr2.mszip1.ts) {
516 out_level = 1;
517 ctr1 = &ctr.ctr2.mszip1.ts->ctr1;
520 status = cli_get_session_key(mem_ctx, cli, &session_key);
521 if (!NT_STATUS_IS_OK(status)) {
522 printf("Failed to get Session Key: %s",
523 nt_errstr(status));
524 return ntstatus_to_werror(status);
527 if (out_level == 1) {
528 DEBUG(1,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
529 (long long)ctr1->new_highwatermark.tmp_highest_usn,
530 (long long)ctr1->new_highwatermark.highest_usn));
531 #if 0
532 libnet_dssync_decrypt_attributes(mem_ctx,
533 &session_key,
534 ctr1->first_object);
535 #endif
536 if (ctr1->more_data) {
537 req.req5.highwatermark = ctr1->new_highwatermark;
538 continue;
542 if (level_out == 6) {
543 out_level = 6;
544 ctr6 = &ctr.ctr6;
545 } else if (level_out == 7
546 && ctr.ctr7.level == 6
547 && ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_MSZIP
548 && ctr.ctr7.ctr.mszip6.ts) {
549 out_level = 6;
550 ctr6 = &ctr.ctr7.ctr.mszip6.ts->ctr6;
551 } else if (level_out == 7
552 && ctr.ctr7.level == 6
553 && ctr.ctr7.type == DRSUAPI_COMPRESSION_TYPE_XPRESS
554 && ctr.ctr7.ctr.xpress6.ts) {
555 out_level = 6;
556 ctr6 = &ctr.ctr7.ctr.xpress6.ts->ctr6;
559 if (out_level == 6) {
560 DEBUG(1,("end[%d] tmp_highest_usn: %llu , highest_usn: %llu\n",y,
561 (long long)ctr6->new_highwatermark.tmp_highest_usn,
562 (long long)ctr6->new_highwatermark.highest_usn));
563 #if 0
564 libnet_dssync_decrypt_attributes(mem_ctx,
565 &session_key,
566 ctr6->first_object);
567 #endif
568 if (ctr6->more_data) {
569 req.req8.highwatermark = ctr6->new_highwatermark;
570 continue;
574 break;
577 out:
578 return werr;
581 /* List of commands exported by this module */
583 struct cmd_set drsuapi_commands[] = {
585 { "DRSUAPI" },
586 { "dscracknames", RPC_RTYPE_WERROR, NULL, cmd_drsuapi_cracknames, &ndr_table_drsuapi.syntax_id, NULL, "Crack Name", "" },
587 { "dsgetdcinfo", RPC_RTYPE_WERROR, NULL, cmd_drsuapi_getdcinfo, &ndr_table_drsuapi.syntax_id, NULL, "Get Domain Controller Info", "" },
588 { "dsgetncchanges", RPC_RTYPE_WERROR, NULL, cmd_drsuapi_getncchanges, &ndr_table_drsuapi.syntax_id, NULL, "Get NC Changes", "" },
589 { NULL }