s4:registry - "LDB backend" - don't test for "0" as string termination on binary...
[Samba/gebeck_regimport.git] / source3 / rpcclient / cmd_netlogon.c
blob6317f8b389a1c47e615bb424dfbd94a16fadc965
1 /*
2 Unix SMB/CIFS implementation.
3 RPC pipe client
5 Copyright (C) Tim Potter 2000
6 Copyright (C) Guenther Deschner 2008
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "rpcclient.h"
24 #include "../libcli/auth/libcli_auth.h"
25 #include "../librpc/gen_ndr/cli_netlogon.h"
27 static WERROR cmd_netlogon_logon_ctrl2(struct rpc_pipe_client *cli,
28 TALLOC_CTX *mem_ctx, int argc,
29 const char **argv)
31 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
32 WERROR werr;
33 const char *logon_server = cli->desthost;
34 enum netr_LogonControlCode function_code = NETLOGON_CONTROL_REDISCOVER;
35 uint32_t level = 1;
36 union netr_CONTROL_DATA_INFORMATION data;
37 union netr_CONTROL_QUERY_INFORMATION query;
38 const char *domain = lp_workgroup();
40 if (argc > 5) {
41 fprintf(stderr, "Usage: %s <logon_server> <function_code> "
42 "<level> <domain>\n", argv[0]);
43 return WERR_OK;
46 if (argc >= 2) {
47 logon_server = argv[1];
50 if (argc >= 3) {
51 function_code = atoi(argv[2]);
54 if (argc >= 4) {
55 level = atoi(argv[3]);
58 if (argc >= 5) {
59 domain = argv[4];
62 switch (function_code) {
63 case NETLOGON_CONTROL_REDISCOVER:
64 case NETLOGON_CONTROL_TC_QUERY:
65 data.domain = domain;
66 break;
67 default:
68 break;
71 status = rpccli_netr_LogonControl2(cli, mem_ctx,
72 logon_server,
73 function_code,
74 level,
75 &data,
76 &query,
77 &werr);
78 if (!NT_STATUS_IS_OK(status)) {
79 return ntstatus_to_werror(status);
82 if (!W_ERROR_IS_OK(werr)) {
83 return werr;
86 /* Display results */
88 return werr;
91 static WERROR cmd_netlogon_getanydcname(struct rpc_pipe_client *cli,
92 TALLOC_CTX *mem_ctx, int argc,
93 const char **argv)
95 const char *dcname = NULL;
96 WERROR werr;
97 NTSTATUS status;
98 int old_timeout;
100 if (argc != 2) {
101 fprintf(stderr, "Usage: %s domainname\n", argv[0]);
102 return WERR_OK;
105 /* Make sure to wait for our DC's reply */
106 old_timeout = rpccli_set_timeout(cli, 30000); /* 30 seconds. */
107 rpccli_set_timeout(cli, MAX(old_timeout, 30000)); /* At least 30 sec */
109 status = rpccli_netr_GetAnyDCName(cli, mem_ctx,
110 cli->desthost,
111 argv[1],
112 &dcname,
113 &werr);
114 rpccli_set_timeout(cli, old_timeout);
116 if (!NT_STATUS_IS_OK(status)) {
117 return ntstatus_to_werror(status);
120 if (!W_ERROR_IS_OK(werr)) {
121 return werr;
124 /* Display results */
126 printf("%s\n", dcname);
128 return werr;
131 static WERROR cmd_netlogon_getdcname(struct rpc_pipe_client *cli,
132 TALLOC_CTX *mem_ctx, int argc,
133 const char **argv)
135 const char *dcname = NULL;
136 NTSTATUS status;
137 WERROR werr;
138 int old_timeout;
140 if (argc != 2) {
141 fprintf(stderr, "Usage: %s domainname\n", argv[0]);
142 return WERR_OK;
145 /* Make sure to wait for our DC's reply */
146 old_timeout = rpccli_set_timeout(cli, 30000); /* 30 seconds. */
147 rpccli_set_timeout(cli, MAX(30000, old_timeout)); /* At least 30 sec */
149 status = rpccli_netr_GetDcName(cli, mem_ctx,
150 cli->desthost,
151 argv[1],
152 &dcname,
153 &werr);
154 rpccli_set_timeout(cli, old_timeout);
156 if (!NT_STATUS_IS_OK(status)) {
157 return ntstatus_to_werror(status);
160 if (!W_ERROR_IS_OK(werr)) {
161 return werr;
164 /* Display results */
166 printf("%s\n", dcname);
168 return werr;
171 static WERROR cmd_netlogon_dsr_getdcname(struct rpc_pipe_client *cli,
172 TALLOC_CTX *mem_ctx, int argc,
173 const char **argv)
175 NTSTATUS result;
176 WERROR werr = WERR_OK;
177 uint32 flags = DS_RETURN_DNS_NAME;
178 const char *server_name = cli->desthost;
179 const char *domain_name;
180 struct GUID domain_guid = GUID_zero();
181 struct GUID site_guid = GUID_zero();
182 struct netr_DsRGetDCNameInfo *info = NULL;
184 if (argc < 2) {
185 fprintf(stderr, "Usage: %s [domain_name] [domain_guid] "
186 "[site_guid] [flags]\n", argv[0]);
187 return WERR_OK;
190 if (argc >= 2)
191 domain_name = argv[1];
193 if (argc >= 3) {
194 if (!NT_STATUS_IS_OK(GUID_from_string(argv[2], &domain_guid))) {
195 return WERR_NOMEM;
199 if (argc >= 4) {
200 if (!NT_STATUS_IS_OK(GUID_from_string(argv[3], &site_guid))) {
201 return WERR_NOMEM;
205 if (argc >= 5)
206 sscanf(argv[4], "%x", &flags);
208 debug_dsdcinfo_flags(1,flags);
210 result = rpccli_netr_DsRGetDCName(cli, mem_ctx,
211 server_name,
212 domain_name,
213 &domain_guid,
214 &site_guid,
215 flags,
216 &info,
217 &werr);
219 if (W_ERROR_IS_OK(werr)) {
220 d_printf("DsGetDcName gave: %s\n",
221 NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info));
222 return WERR_OK;
225 printf("rpccli_netlogon_dsr_getdcname returned %s\n",
226 win_errstr(werr));
228 return werr;
231 static WERROR cmd_netlogon_dsr_getdcnameex(struct rpc_pipe_client *cli,
232 TALLOC_CTX *mem_ctx, int argc,
233 const char **argv)
235 WERROR result;
236 NTSTATUS status;
237 uint32_t flags = DS_RETURN_DNS_NAME;
238 const char *server_name = cli->desthost;
239 const char *domain_name;
240 const char *site_name = NULL;
241 struct GUID domain_guid = GUID_zero();
242 struct netr_DsRGetDCNameInfo *info = NULL;
244 if (argc < 2) {
245 fprintf(stderr, "Usage: %s [domain_name] [domain_guid] "
246 "[site_name] [flags]\n", argv[0]);
247 return WERR_OK;
250 domain_name = argv[1];
252 if (argc >= 3) {
253 if (!NT_STATUS_IS_OK(GUID_from_string(argv[2], &domain_guid))) {
254 return WERR_NOMEM;
258 if (argc >= 4) {
259 site_name = argv[3];
262 if (argc >= 5) {
263 sscanf(argv[4], "%x", &flags);
266 debug_dsdcinfo_flags(1,flags);
268 status = rpccli_netr_DsRGetDCNameEx(cli, mem_ctx,
269 server_name,
270 domain_name,
271 &domain_guid,
272 site_name,
273 flags,
274 &info,
275 &result);
276 if (!NT_STATUS_IS_OK(status)) {
277 return ntstatus_to_werror(status);
280 if (!W_ERROR_IS_OK(result)) {
281 return result;
284 d_printf("DsRGetDCNameEx gave %s\n",
285 NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info));
287 return result;
290 static WERROR cmd_netlogon_dsr_getdcnameex2(struct rpc_pipe_client *cli,
291 TALLOC_CTX *mem_ctx, int argc,
292 const char **argv)
294 WERROR result;
295 NTSTATUS status;
296 uint32_t flags = DS_RETURN_DNS_NAME;
297 const char *server_name = cli->desthost;
298 const char *domain_name = NULL;
299 const char *client_account = NULL;
300 uint32_t mask = 0;
301 const char *site_name = NULL;
302 struct GUID domain_guid = GUID_zero();
303 struct netr_DsRGetDCNameInfo *info = NULL;
305 if (argc < 2) {
306 fprintf(stderr, "Usage: %s [client_account] [acb_mask] "
307 "[domain_name] [domain_guid] [site_name] "
308 "[flags]\n", argv[0]);
309 return WERR_OK;
312 if (argc >= 2) {
313 client_account = argv[1];
316 if (argc >= 3) {
317 mask = atoi(argv[2]);
320 if (argc >= 4) {
321 domain_name = argv[3];
324 if (argc >= 5) {
325 if (!NT_STATUS_IS_OK(GUID_from_string(argv[4], &domain_guid))) {
326 return WERR_NOMEM;
330 if (argc >= 6) {
331 site_name = argv[5];
334 if (argc >= 7) {
335 sscanf(argv[6], "%x", &flags);
338 debug_dsdcinfo_flags(1,flags);
340 status = rpccli_netr_DsRGetDCNameEx2(cli, mem_ctx,
341 server_name,
342 client_account,
343 mask,
344 domain_name,
345 &domain_guid,
346 site_name,
347 flags,
348 &info,
349 &result);
350 if (!NT_STATUS_IS_OK(status)) {
351 return ntstatus_to_werror(status);
354 if (!W_ERROR_IS_OK(result)) {
355 return result;
358 d_printf("DsRGetDCNameEx2 gave %s\n",
359 NDR_PRINT_STRUCT_STRING(mem_ctx, netr_DsRGetDCNameInfo, info));
361 return result;
365 static WERROR cmd_netlogon_dsr_getsitename(struct rpc_pipe_client *cli,
366 TALLOC_CTX *mem_ctx, int argc,
367 const char **argv)
369 WERROR werr;
370 NTSTATUS status;
371 const char *sitename = NULL;
373 if (argc != 2) {
374 fprintf(stderr, "Usage: %s computername\n", argv[0]);
375 return WERR_OK;
378 status = rpccli_netr_DsRGetSiteName(cli, mem_ctx,
379 argv[1],
380 &sitename,
381 &werr);
382 if (!NT_STATUS_IS_OK(status)) {
383 return ntstatus_to_werror(status);
386 if (!W_ERROR_IS_OK(werr)) {
387 printf("rpccli_netlogon_dsr_gesitename returned %s\n",
388 nt_errstr(werror_to_ntstatus(werr)));
389 return werr;
392 printf("Computer %s is on Site: %s\n", argv[1], sitename);
394 return WERR_OK;
397 static WERROR cmd_netlogon_logon_ctrl(struct rpc_pipe_client *cli,
398 TALLOC_CTX *mem_ctx, int argc,
399 const char **argv)
401 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
402 WERROR werr;
403 const char *logon_server = cli->desthost;
404 enum netr_LogonControlCode function_code = 1;
405 uint32_t level = 1;
406 union netr_CONTROL_QUERY_INFORMATION info;
408 if (argc > 4) {
409 fprintf(stderr, "Usage: %s <logon_server> <function_code> "
410 "<level>\n", argv[0]);
411 return WERR_OK;
414 if (argc >= 2) {
415 logon_server = argv[1];
418 if (argc >= 3) {
419 function_code = atoi(argv[2]);
422 if (argc >= 4) {
423 level = atoi(argv[3]);
426 status = rpccli_netr_LogonControl(cli, mem_ctx,
427 logon_server,
428 function_code,
429 level,
430 &info,
431 &werr);
432 if (!NT_STATUS_IS_OK(status)) {
433 return ntstatus_to_werror(status);
436 if (!W_ERROR_IS_OK(werr)) {
437 return werr;
440 /* Display results */
442 return werr;
445 /* Display sam synchronisation information */
447 static void display_sam_sync(struct netr_DELTA_ENUM_ARRAY *r)
449 uint32_t i, j;
451 for (i=0; i < r->num_deltas; i++) {
453 union netr_DELTA_UNION u = r->delta_enum[i].delta_union;
454 union netr_DELTA_ID_UNION id = r->delta_enum[i].delta_id_union;
456 switch (r->delta_enum[i].delta_type) {
457 case NETR_DELTA_DOMAIN:
458 printf("Domain: %s\n",
459 u.domain->domain_name.string);
460 break;
461 case NETR_DELTA_GROUP:
462 printf("Group: %s\n",
463 u.group->group_name.string);
464 break;
465 case NETR_DELTA_DELETE_GROUP:
466 printf("Delete Group: %d\n",
467 u.delete_account.unknown);
468 break;
469 case NETR_DELTA_RENAME_GROUP:
470 printf("Rename Group: %s -> %s\n",
471 u.rename_group->OldName.string,
472 u.rename_group->NewName.string);
473 break;
474 case NETR_DELTA_USER:
475 printf("Account: %s\n",
476 u.user->account_name.string);
477 break;
478 case NETR_DELTA_DELETE_USER:
479 printf("Delete User: %d\n",
480 id.rid);
481 break;
482 case NETR_DELTA_RENAME_USER:
483 printf("Rename user: %s -> %s\n",
484 u.rename_user->OldName.string,
485 u.rename_user->NewName.string);
486 break;
487 case NETR_DELTA_GROUP_MEMBER:
488 for (j=0; j < u.group_member->num_rids; j++) {
489 printf("rid 0x%x, attrib 0x%08x\n",
490 u.group_member->rids[j],
491 u.group_member->attribs[j]);
493 break;
494 case NETR_DELTA_ALIAS:
495 printf("Alias: %s\n",
496 u.alias->alias_name.string);
497 break;
498 case NETR_DELTA_DELETE_ALIAS:
499 printf("Delete Alias: %d\n",
500 r->delta_enum[i].delta_id_union.rid);
501 break;
502 case NETR_DELTA_RENAME_ALIAS:
503 printf("Rename alias: %s -> %s\n",
504 u.rename_alias->OldName.string,
505 u.rename_alias->NewName.string);
506 break;
507 case NETR_DELTA_ALIAS_MEMBER:
508 for (j=0; j < u.alias_member->sids.num_sids; j++) {
509 fstring sid_str;
510 sid_to_fstring(sid_str,
511 u.alias_member->sids.sids[j].sid);
512 printf("%s\n", sid_str);
514 break;
515 case NETR_DELTA_POLICY:
516 printf("Policy\n");
517 break;
518 case NETR_DELTA_TRUSTED_DOMAIN:
519 printf("Trusted Domain: %s\n",
520 u.trusted_domain->domain_name.string);
521 break;
522 case NETR_DELTA_DELETE_TRUST:
523 printf("Delete Trust: %d\n",
524 u.delete_trust.unknown);
525 break;
526 case NETR_DELTA_ACCOUNT:
527 printf("Account\n");
528 break;
529 case NETR_DELTA_DELETE_ACCOUNT:
530 printf("Delete Account: %d\n",
531 u.delete_account.unknown);
532 break;
533 case NETR_DELTA_SECRET:
534 printf("Secret\n");
535 break;
536 case NETR_DELTA_DELETE_SECRET:
537 printf("Delete Secret: %d\n",
538 u.delete_secret.unknown);
539 break;
540 case NETR_DELTA_DELETE_GROUP2:
541 printf("Delete Group2: %s\n",
542 u.delete_group->account_name);
543 break;
544 case NETR_DELTA_DELETE_USER2:
545 printf("Delete User2: %s\n",
546 u.delete_user->account_name);
547 break;
548 case NETR_DELTA_MODIFY_COUNT:
549 printf("sam sequence update: 0x%016llx\n",
550 (unsigned long long) *u.modified_count);
551 break;
552 default:
553 printf("unknown delta type 0x%02x\n",
554 r->delta_enum[i].delta_type);
555 break;
560 /* Perform sam synchronisation */
562 static NTSTATUS cmd_netlogon_sam_sync(struct rpc_pipe_client *cli,
563 TALLOC_CTX *mem_ctx, int argc,
564 const char **argv)
566 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
567 const char *logon_server = cli->desthost;
568 const char *computername = global_myname();
569 struct netr_Authenticator credential;
570 struct netr_Authenticator return_authenticator;
571 enum netr_SamDatabaseID database_id = SAM_DATABASE_DOMAIN;
572 uint16_t restart_state = 0;
573 uint32_t sync_context = 0;
575 if (argc > 2) {
576 fprintf(stderr, "Usage: %s [database_id]\n", argv[0]);
577 return NT_STATUS_OK;
580 if (argc == 2) {
581 database_id = atoi(argv[1]);
584 /* Synchronise sam database */
586 do {
587 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
589 netlogon_creds_client_authenticator(cli->dc, &credential);
591 result = rpccli_netr_DatabaseSync2(cli, mem_ctx,
592 logon_server,
593 computername,
594 &credential,
595 &return_authenticator,
596 database_id,
597 restart_state,
598 &sync_context,
599 &delta_enum_array,
600 0xffff);
602 /* Check returned credentials. */
603 if (!netlogon_creds_client_check(cli->dc,
604 &return_authenticator.cred)) {
605 DEBUG(0,("credentials chain check failed\n"));
606 return NT_STATUS_ACCESS_DENIED;
609 if (NT_STATUS_IS_ERR(result)) {
610 break;
613 /* Display results */
615 display_sam_sync(delta_enum_array);
617 TALLOC_FREE(delta_enum_array);
619 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
621 return result;
624 /* Perform sam delta synchronisation */
626 static NTSTATUS cmd_netlogon_sam_deltas(struct rpc_pipe_client *cli,
627 TALLOC_CTX *mem_ctx, int argc,
628 const char **argv)
630 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
631 uint32_t tmp;
632 const char *logon_server = cli->desthost;
633 const char *computername = global_myname();
634 struct netr_Authenticator credential;
635 struct netr_Authenticator return_authenticator;
636 enum netr_SamDatabaseID database_id = SAM_DATABASE_DOMAIN;
637 uint64_t sequence_num;
639 if (argc != 3) {
640 fprintf(stderr, "Usage: %s database_id seqnum\n", argv[0]);
641 return NT_STATUS_OK;
644 database_id = atoi(argv[1]);
645 tmp = atoi(argv[2]);
647 sequence_num = tmp & 0xffff;
649 do {
650 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
652 netlogon_creds_client_authenticator(cli->dc, &credential);
654 result = rpccli_netr_DatabaseDeltas(cli, mem_ctx,
655 logon_server,
656 computername,
657 &credential,
658 &return_authenticator,
659 database_id,
660 &sequence_num,
661 &delta_enum_array,
662 0xffff);
664 /* Check returned credentials. */
665 if (!netlogon_creds_client_check(cli->dc,
666 &return_authenticator.cred)) {
667 DEBUG(0,("credentials chain check failed\n"));
668 return NT_STATUS_ACCESS_DENIED;
671 if (NT_STATUS_IS_ERR(result)) {
672 break;
675 /* Display results */
677 display_sam_sync(delta_enum_array);
679 TALLOC_FREE(delta_enum_array);
681 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
683 return result;
686 /* Log on a domain user */
688 static NTSTATUS cmd_netlogon_sam_logon(struct rpc_pipe_client *cli,
689 TALLOC_CTX *mem_ctx, int argc,
690 const char **argv)
692 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
693 int logon_type = NetlogonNetworkInformation;
694 const char *username, *password;
695 int auth_level = 2;
696 uint32 logon_param = 0;
697 const char *workstation = NULL;
699 /* Check arguments */
701 if (argc < 3 || argc > 7) {
702 fprintf(stderr, "Usage: samlogon <username> <password> [workstation]"
703 "[logon_type (1 or 2)] [auth level (2 or 3)] [logon_parameter]\n");
704 return NT_STATUS_OK;
707 username = argv[1];
708 password = argv[2];
710 if (argc >= 4)
711 workstation = argv[3];
713 if (argc >= 5)
714 sscanf(argv[4], "%i", &logon_type);
716 if (argc >= 6)
717 sscanf(argv[5], "%i", &auth_level);
719 if (argc == 7)
720 sscanf(argv[6], "%x", &logon_param);
722 /* Perform the sam logon */
724 result = rpccli_netlogon_sam_logon(cli, mem_ctx, logon_param, lp_workgroup(), username, password, workstation, logon_type);
726 if (!NT_STATUS_IS_OK(result))
727 goto done;
729 done:
730 return result;
733 /* Change the trust account password */
735 static NTSTATUS cmd_netlogon_change_trust_pw(struct rpc_pipe_client *cli,
736 TALLOC_CTX *mem_ctx, int argc,
737 const char **argv)
739 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
741 /* Check arguments */
743 if (argc > 1) {
744 fprintf(stderr, "Usage: change_trust_pw");
745 return NT_STATUS_OK;
748 /* Perform the sam logon */
750 result = trust_pw_find_change_and_store_it(cli, mem_ctx,
751 lp_workgroup());
753 if (!NT_STATUS_IS_OK(result))
754 goto done;
756 done:
757 return result;
760 static WERROR cmd_netlogon_gettrustrid(struct rpc_pipe_client *cli,
761 TALLOC_CTX *mem_ctx, int argc,
762 const char **argv)
764 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
765 WERROR werr = WERR_GENERAL_FAILURE;
766 const char *server_name = cli->desthost;
767 const char *domain_name = lp_workgroup();
768 uint32_t rid = 0;
770 if (argc < 1 || argc > 3) {
771 fprintf(stderr, "Usage: %s <server_name> <domain_name>\n",
772 argv[0]);
773 return WERR_OK;
776 if (argc >= 2) {
777 server_name = argv[1];
780 if (argc >= 3) {
781 domain_name = argv[2];
784 status = rpccli_netr_LogonGetTrustRid(cli, mem_ctx,
785 server_name,
786 domain_name,
787 &rid,
788 &werr);
789 if (!NT_STATUS_IS_OK(status)) {
790 goto done;
793 if (W_ERROR_IS_OK(werr)) {
794 printf("Rid: %d\n", rid);
796 done:
797 return werr;
800 static WERROR cmd_netlogon_dsr_enumtrustdom(struct rpc_pipe_client *cli,
801 TALLOC_CTX *mem_ctx, int argc,
802 const char **argv)
804 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
805 WERROR werr = WERR_GENERAL_FAILURE;
806 const char *server_name = cli->desthost;
807 uint32_t trust_flags = NETR_TRUST_FLAG_IN_FOREST;
808 struct netr_DomainTrustList trusts;
810 if (argc < 1 || argc > 3) {
811 fprintf(stderr, "Usage: %s <server_name> <trust_flags>\n",
812 argv[0]);
813 return WERR_OK;
816 if (argc >= 2) {
817 server_name = argv[1];
820 if (argc >= 3) {
821 sscanf(argv[2], "%x", &trust_flags);
824 status = rpccli_netr_DsrEnumerateDomainTrusts(cli, mem_ctx,
825 server_name,
826 trust_flags,
827 &trusts,
828 &werr);
829 if (!NT_STATUS_IS_OK(status)) {
830 goto done;
833 if (W_ERROR_IS_OK(werr)) {
834 int i;
836 printf("%d domains returned\n", trusts.count);
838 for (i=0; i<trusts.count; i++ ) {
839 printf("%s (%s)\n",
840 trusts.array[i].dns_name,
841 trusts.array[i].netbios_name);
844 done:
845 return werr;
848 static WERROR cmd_netlogon_deregisterdnsrecords(struct rpc_pipe_client *cli,
849 TALLOC_CTX *mem_ctx, int argc,
850 const char **argv)
852 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
853 WERROR werr = WERR_GENERAL_FAILURE;
854 const char *server_name = cli->desthost;
855 const char *domain = lp_workgroup();
856 const char *dns_host = NULL;
858 if (argc < 1 || argc > 4) {
859 fprintf(stderr, "Usage: %s <server_name> <domain_name> "
860 "<dns_host>\n", argv[0]);
861 return WERR_OK;
864 if (argc >= 2) {
865 server_name = argv[1];
868 if (argc >= 3) {
869 domain = argv[2];
872 if (argc >= 4) {
873 dns_host = argv[3];
876 status = rpccli_netr_DsrDeregisterDNSHostRecords(cli, mem_ctx,
877 server_name,
878 domain,
879 NULL,
880 NULL,
881 dns_host,
882 &werr);
883 if (!NT_STATUS_IS_OK(status)) {
884 goto done;
887 if (W_ERROR_IS_OK(werr)) {
888 printf("success\n");
890 done:
891 return werr;
894 static WERROR cmd_netlogon_dsr_getforesttrustinfo(struct rpc_pipe_client *cli,
895 TALLOC_CTX *mem_ctx, int argc,
896 const char **argv)
898 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
899 WERROR werr = WERR_GENERAL_FAILURE;
900 const char *server_name = cli->desthost;
901 const char *trusted_domain_name = NULL;
902 struct lsa_ForestTrustInformation *info = NULL;
903 uint32_t flags = 0;
905 if (argc < 1 || argc > 4) {
906 fprintf(stderr, "Usage: %s <server_name> <trusted_domain_name> "
907 "<flags>\n", argv[0]);
908 return WERR_OK;
911 if (argc >= 2) {
912 server_name = argv[1];
915 if (argc >= 3) {
916 trusted_domain_name = argv[2];
919 if (argc >= 4) {
920 sscanf(argv[3], "%x", &flags);
923 status = rpccli_netr_DsRGetForestTrustInformation(cli, mem_ctx,
924 server_name,
925 trusted_domain_name,
926 flags,
927 &info,
928 &werr);
929 if (!NT_STATUS_IS_OK(status)) {
930 goto done;
933 if (W_ERROR_IS_OK(werr)) {
934 printf("success\n");
936 done:
937 return werr;
940 static WERROR cmd_netlogon_enumtrusteddomains(struct rpc_pipe_client *cli,
941 TALLOC_CTX *mem_ctx, int argc,
942 const char **argv)
944 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
945 WERROR werr = WERR_GENERAL_FAILURE;
946 const char *server_name = cli->desthost;
947 struct netr_Blob blob;
950 if (argc < 1 || argc > 3) {
951 fprintf(stderr, "Usage: %s <server_name>\n", argv[0]);
952 return WERR_OK;
955 if (argc >= 2) {
956 server_name = argv[1];
959 status = rpccli_netr_NetrEnumerateTrustedDomains(cli, mem_ctx,
960 server_name,
961 &blob,
962 &werr);
963 if (!NT_STATUS_IS_OK(status)) {
964 goto done;
967 if (W_ERROR_IS_OK(werr)) {
968 printf("success\n");
969 dump_data(1, blob.data, blob.length);
971 done:
972 return werr;
975 static WERROR cmd_netlogon_enumtrusteddomainsex(struct rpc_pipe_client *cli,
976 TALLOC_CTX *mem_ctx, int argc,
977 const char **argv)
979 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
980 WERROR werr = WERR_GENERAL_FAILURE;
981 const char *server_name = cli->desthost;
982 struct netr_DomainTrustList list;
984 if (argc < 1 || argc > 3) {
985 fprintf(stderr, "Usage: %s <server_name>\n", argv[0]);
986 return WERR_OK;
989 if (argc >= 2) {
990 server_name = argv[1];
993 status = rpccli_netr_NetrEnumerateTrustedDomainsEx(cli, mem_ctx,
994 server_name,
995 &list,
996 &werr);
997 if (!NT_STATUS_IS_OK(status)) {
998 goto done;
1001 if (W_ERROR_IS_OK(werr)) {
1002 printf("success\n");
1004 done:
1005 return werr;
1008 static WERROR cmd_netlogon_getdcsitecoverage(struct rpc_pipe_client *cli,
1009 TALLOC_CTX *mem_ctx, int argc,
1010 const char **argv)
1012 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1013 WERROR werr = WERR_GENERAL_FAILURE;
1014 const char *server_name = cli->desthost;
1015 struct DcSitesCtr *ctr = NULL;
1017 if (argc < 1 || argc > 3) {
1018 fprintf(stderr, "Usage: %s <server_name>\n", argv[0]);
1019 return WERR_OK;
1022 if (argc >= 2) {
1023 server_name = argv[1];
1026 status = rpccli_netr_DsrGetDcSiteCoverageW(cli, mem_ctx,
1027 server_name,
1028 &ctr,
1029 &werr);
1030 if (!NT_STATUS_IS_OK(status)) {
1031 goto done;
1034 if (W_ERROR_IS_OK(werr) && ctr->num_sites) {
1035 int i;
1036 printf("sites covered by this DC: %d\n", ctr->num_sites);
1037 for (i=0; i<ctr->num_sites; i++) {
1038 printf("%s\n", ctr->sites[i].string);
1041 done:
1042 return werr;
1045 static NTSTATUS cmd_netlogon_database_redo(struct rpc_pipe_client *cli,
1046 TALLOC_CTX *mem_ctx, int argc,
1047 const char **argv)
1049 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1050 const char *server_name = cli->desthost;
1051 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
1052 struct netr_Authenticator clnt_creds, srv_cred;
1053 struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
1054 unsigned char trust_passwd_hash[16];
1055 enum netr_SchannelType sec_channel_type = 0;
1056 struct netr_ChangeLogEntry e;
1057 uint32_t rid = 500;
1059 if (argc > 2) {
1060 fprintf(stderr, "Usage: %s <user rid>\n", argv[0]);
1061 return NT_STATUS_OK;
1064 if (argc == 2) {
1065 sscanf(argv[1], "%d", &rid);
1068 if (!secrets_fetch_trust_account_password(lp_workgroup(),
1069 trust_passwd_hash,
1070 NULL, &sec_channel_type)) {
1071 return NT_STATUS_UNSUCCESSFUL;
1074 status = rpccli_netlogon_setup_creds(cli,
1075 server_name, /* server name */
1076 lp_workgroup(), /* domain */
1077 global_myname(), /* client name */
1078 global_myname(), /* machine account name */
1079 trust_passwd_hash,
1080 sec_channel_type,
1081 &neg_flags);
1083 if (!NT_STATUS_IS_OK(status)) {
1084 return status;
1087 netlogon_creds_client_authenticator(cli->dc, &clnt_creds);
1089 ZERO_STRUCT(e);
1091 e.object_rid = rid;
1092 e.db_index = SAM_DATABASE_DOMAIN;
1093 e.delta_type = NETR_DELTA_USER;
1095 status = rpccli_netr_DatabaseRedo(cli, mem_ctx,
1096 server_name,
1097 global_myname(),
1098 &clnt_creds,
1099 &srv_cred,
1101 0, /* is calculated automatically */
1102 &delta_enum_array);
1104 if (!netlogon_creds_client_check(cli->dc, &srv_cred.cred)) {
1105 DEBUG(0,("credentials chain check failed\n"));
1106 return NT_STATUS_ACCESS_DENIED;
1109 return status;
1112 static NTSTATUS cmd_netlogon_capabilities(struct rpc_pipe_client *cli,
1113 TALLOC_CTX *mem_ctx, int argc,
1114 const char **argv)
1116 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1117 struct netr_Authenticator credential;
1118 struct netr_Authenticator return_authenticator;
1119 union netr_Capabilities capabilities;
1120 uint32_t level = 1;
1122 if (argc > 2) {
1123 fprintf(stderr, "Usage: %s <level>\n", argv[0]);
1124 return NT_STATUS_OK;
1127 if (argc == 2) {
1128 level = atoi(argv[1]);
1131 ZERO_STRUCT(return_authenticator);
1133 netlogon_creds_client_authenticator(cli->dc, &credential);
1135 status = rpccli_netr_LogonGetCapabilities(cli, mem_ctx,
1136 cli->desthost,
1137 global_myname(),
1138 &credential,
1139 &return_authenticator,
1140 level,
1141 &capabilities);
1143 if (!netlogon_creds_client_check(cli->dc,
1144 &return_authenticator.cred)) {
1145 DEBUG(0,("credentials chain check failed\n"));
1146 return NT_STATUS_ACCESS_DENIED;
1149 printf("capabilities: 0x%08x\n", capabilities.server_capabilities);
1151 return status;
1154 /* List of commands exported by this module */
1156 struct cmd_set netlogon_commands[] = {
1158 { "NETLOGON" },
1160 { "logonctrl2", RPC_RTYPE_WERROR, NULL, cmd_netlogon_logon_ctrl2, &ndr_table_netlogon.syntax_id, NULL, "Logon Control 2", "" },
1161 { "getanydcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getanydcname, &ndr_table_netlogon.syntax_id, NULL, "Get trusted DC name", "" },
1162 { "getdcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getdcname, &ndr_table_netlogon.syntax_id, NULL, "Get trusted PDC name", "" },
1163 { "dsr_getdcname", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcname, &ndr_table_netlogon.syntax_id, NULL, "Get trusted DC name", "" },
1164 { "dsr_getdcnameex", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcnameex, &ndr_table_netlogon.syntax_id, NULL, "Get trusted DC name", "" },
1165 { "dsr_getdcnameex2", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getdcnameex2, &ndr_table_netlogon.syntax_id, NULL, "Get trusted DC name", "" },
1166 { "dsr_getsitename", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getsitename, &ndr_table_netlogon.syntax_id, NULL, "Get sitename", "" },
1167 { "dsr_getforesttrustinfo", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_getforesttrustinfo, &ndr_table_netlogon.syntax_id, NULL, "Get Forest Trust Info", "" },
1168 { "logonctrl", RPC_RTYPE_WERROR, NULL, cmd_netlogon_logon_ctrl, &ndr_table_netlogon.syntax_id, NULL, "Logon Control", "" },
1169 { "samsync", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_sync, NULL, &ndr_table_netlogon.syntax_id, NULL, "Sam Synchronisation", "" },
1170 { "samdeltas", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_deltas, NULL, &ndr_table_netlogon.syntax_id, NULL, "Query Sam Deltas", "" },
1171 { "samlogon", RPC_RTYPE_NTSTATUS, cmd_netlogon_sam_logon, NULL, &ndr_table_netlogon.syntax_id, NULL, "Sam Logon", "" },
1172 { "change_trust_pw", RPC_RTYPE_NTSTATUS, cmd_netlogon_change_trust_pw, NULL, &ndr_table_netlogon.syntax_id, NULL, "Change Trust Account Password", "" },
1173 { "gettrustrid", RPC_RTYPE_WERROR, NULL, cmd_netlogon_gettrustrid, &ndr_table_netlogon.syntax_id, NULL, "Get trust rid", "" },
1174 { "dsr_enumtrustdom", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_enumtrustdom, &ndr_table_netlogon.syntax_id, NULL, "Enumerate trusted domains", "" },
1175 { "dsenumdomtrusts", RPC_RTYPE_WERROR, NULL, cmd_netlogon_dsr_enumtrustdom, &ndr_table_netlogon.syntax_id, NULL, "Enumerate all trusted domains in an AD forest", "" },
1176 { "deregisterdnsrecords", RPC_RTYPE_WERROR, NULL, cmd_netlogon_deregisterdnsrecords, &ndr_table_netlogon.syntax_id, NULL, "Deregister DNS records", "" },
1177 { "netrenumtrusteddomains", RPC_RTYPE_WERROR, NULL, cmd_netlogon_enumtrusteddomains, &ndr_table_netlogon.syntax_id, NULL, "Enumerate trusted domains", "" },
1178 { "netrenumtrusteddomainsex", RPC_RTYPE_WERROR, NULL, cmd_netlogon_enumtrusteddomainsex, &ndr_table_netlogon.syntax_id, NULL, "Enumerate trusted domains", "" },
1179 { "getdcsitecoverage", RPC_RTYPE_WERROR, NULL, cmd_netlogon_getdcsitecoverage, &ndr_table_netlogon.syntax_id, NULL, "Get the Site-Coverage from a DC", "" },
1180 { "database_redo", RPC_RTYPE_NTSTATUS, cmd_netlogon_database_redo, NULL, &ndr_table_netlogon.syntax_id, NULL, "Replicate single object from a DC", "" },
1181 { "capabilities", RPC_RTYPE_NTSTATUS, cmd_netlogon_capabilities, NULL, &ndr_table_netlogon.syntax_id, NULL, "Return Capabilities", "" },
1183 { NULL }